Skip to content

Commit 9cf859e

Browse files
jorgeepditommaso
authored andcommitted
Fix overlapping file lock exception (nextflow-io#5489) [ci fast]
Signed-off-by: jorgee <[email protected]> Signed-off-by: Paolo Di Tommaso <[email protected]> Co-authored-by: Paolo Di Tommaso <[email protected]>
1 parent 86f7712 commit 9cf859e

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

modules/nextflow/src/main/groovy/nextflow/conda/CondaCache.groovy

+16-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import java.nio.file.Path
2222
import java.nio.file.Paths
2323
import java.util.concurrent.ConcurrentHashMap
2424

25+
import com.google.common.hash.Hashing
2526
import groovy.transform.CompileStatic
2627
import groovy.transform.PackageScope
2728
import groovy.util.logging.Slf4j
@@ -32,7 +33,6 @@ import nextflow.file.FileMutex
3233
import nextflow.util.CacheHelper
3334
import nextflow.util.Duration
3435
import nextflow.util.Escape
35-
import org.yaml.snakeyaml.Yaml
3636
/**
3737
* Handle Conda environment creation and caching
3838
*
@@ -166,6 +166,18 @@ class CondaCache {
166166
str.endsWith('.txt') && !str.contains('\n')
167167
}
168168

169+
static protected String sipHash(CharSequence data) {
170+
Hashing
171+
.sipHash24()
172+
.newHasher()
173+
.putUnencodedChars(data)
174+
.hash()
175+
.toString()
176+
}
177+
178+
static protected String sipHash(Path path) {
179+
sipHash(path.toAbsolutePath().normalize().toString())
180+
}
169181

170182
/**
171183
* Get the path on the file system where store a Conda environment
@@ -188,11 +200,8 @@ class CondaCache {
188200
try {
189201
final path = condaEnv as Path
190202
content = path.text
191-
final yaml = (Map)new Yaml().load(content)
192-
if( yaml.name )
193-
name = yaml.name
194-
else
195-
name = path.baseName
203+
name = 'env-' + sipHash(path)
204+
196205
}
197206
catch( NoSuchFileException e ) {
198207
throw new IllegalArgumentException("Conda environment file does not exist: $condaEnv")
@@ -205,7 +214,7 @@ class CondaCache {
205214
try {
206215
final path = condaEnv as Path
207216
content = path.text
208-
name = path.baseName
217+
name = 'env-' + sipHash(path)
209218
}
210219
catch( NoSuchFileException e ) {
211220
throw new IllegalArgumentException("Conda environment file does not exist: $condaEnv")

modules/nextflow/src/test/groovy/nextflow/conda/CondaCacheTest.groovy

+6-4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class CondaCacheTest extends Specification {
8989
def cache = Spy(CondaCache)
9090
def BASE = Paths.get('/conda/envs')
9191
def ENV = folder.resolve('foo.yml')
92+
def hash = CondaCache.sipHash(ENV)
9293
ENV.text = '''
9394
channels:
9495
- bioconda
@@ -99,13 +100,12 @@ class CondaCacheTest extends Specification {
99100
- bwa=0.7.15
100101
'''
101102
.stripIndent(true) // https://issues.apache.org/jira/browse/GROOVY-9423
102-
103103
when:
104104
def prefix = cache.condaPrefixPath(ENV.toString())
105105
then:
106106
1 * cache.isYamlFilePath(ENV.toString())
107107
1 * cache.getCacheDir() >> BASE
108-
prefix.toString() == '/conda/envs/foo-9416240708c49c4e627414b46a743664'
108+
prefix.toString() == "/conda/envs/env-${hash}-9416240708c49c4e627414b46a743664"
109109

110110
cleanup:
111111
folder?.deleteDir()
@@ -118,6 +118,7 @@ class CondaCacheTest extends Specification {
118118
def cache = Spy(CondaCache)
119119
def BASE = Paths.get('/conda/envs')
120120
def ENV = Files.createTempFile('test','.yml')
121+
def hash = CondaCache.sipHash(ENV)
121122
ENV.text = '''
122123
name: my-env-1.1
123124
channels:
@@ -135,7 +136,7 @@ class CondaCacheTest extends Specification {
135136
then:
136137
1 * cache.isYamlFilePath(ENV.toString())
137138
1 * cache.getCacheDir() >> BASE
138-
prefix.toString() == '/conda/envs/my-env-1.1-e7fafe40ca966397a2c0d9bed7181aa7'
139+
prefix.toString() == "/conda/envs/env-${hash}-e7fafe40ca966397a2c0d9bed7181aa7"
139140

140141
}
141142

@@ -146,6 +147,7 @@ class CondaCacheTest extends Specification {
146147
def cache = Spy(CondaCache)
147148
def BASE = Paths.get('/conda/envs')
148149
def ENV = folder.resolve('bar.txt')
150+
def hash = CondaCache.sipHash(ENV)
149151
ENV.text = '''
150152
star=2.5.4a
151153
bwa=0.7.15
@@ -159,7 +161,7 @@ class CondaCacheTest extends Specification {
159161
1 * cache.isYamlFilePath(ENV.toString())
160162
1 * cache.isTextFilePath(ENV.toString())
161163
1 * cache.getCacheDir() >> BASE
162-
prefix.toString() == '/conda/envs/bar-8a4aa7db8ddb8ce4eb4d450d4814a437'
164+
prefix.toString() == "/conda/envs/env-${hash}-8a4aa7db8ddb8ce4eb4d450d4814a437"
163165

164166
cleanup:
165167
folder?.deleteDir()

0 commit comments

Comments
 (0)