Skip to content

Commit 8835639

Browse files
committed
Enforcing concurrent gurantee on DStack setup
1 parent 92ee1af commit 8835639

2 files changed

Lines changed: 24 additions & 14 deletions

File tree

src/main/java/picoded/dstack/DStack.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
public class DStack extends CoreStack {
1313

1414
// List of provider backends - to fetch / initialize from
15-
protected ProviderConfig providerConfig;
15+
protected ProviderConfig providerConfig = null;
1616

1717
// Namespace listing
18-
protected GenericConvertList<Object> namespace;
18+
protected GenericConvertList<Object> namespace = null;
1919

2020
/**
2121
* Constructor with configuration map

src/main/java/picoded/dstack/stack/ProviderConfig.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.HashMap;
44
import java.util.List;
55
import java.util.Map;
6+
import java.util.concurrent.ConcurrentHashMap;
67

78
import picoded.core.struct.GenericConvertHashMap;
89
import picoded.core.struct.GenericConvertList;
@@ -115,7 +116,7 @@ protected GenericConvertMap<String, Object> getStackConfig(String name) {
115116
/**
116117
* Stores the respective stack providers
117118
*/
118-
protected Map<String, CoreStack> providerStackMap = new HashMap<>();
119+
protected final ConcurrentHashMap<String, CoreStack> providerStackMap = new ConcurrentHashMap<>();
119120

120121
/**
121122
* Get the stack of the provider specified by the name,
@@ -132,18 +133,27 @@ public CoreStack getProviderStack(String name) {
132133
return cache;
133134
}
134135

135-
// Cache not found, get config to initialize a new stack
136-
GenericConvertMap<String, Object> providerConfig = getStackConfig(name);
137-
if (providerConfig == null) {
138-
throw new IllegalArgumentException("Unknown provider name, config not found : " + name);
136+
synchronized(providerStackMap) {
137+
// Check the cache again (avoid race condition)
138+
cache = providerStackMap.get(name);
139+
if (cache != null) {
140+
return cache;
141+
}
142+
143+
// Cache not found, get config to initialize a new stack
144+
GenericConvertMap<String, Object> providerConfig = getStackConfig(name);
145+
if (providerConfig == null) {
146+
throw new IllegalArgumentException("Unknown provider name, config not found : " + name);
147+
}
148+
149+
// Initialization of stack and store into cache
150+
cache = initStack(providerConfig.getString("type"), providerConfig);
151+
providerStackMap.put(name, cache);
152+
153+
// Return result
154+
return cache;
139155
}
140-
141-
// Initialization of stack and store into cache
142-
cache = initStack(providerConfig.getString("type"), providerConfig);
143-
providerStackMap.put(name, cache);
144-
145-
// Return result
146-
return cache;
156+
147157
}
148158

149159
/**

0 commit comments

Comments
 (0)