Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/main/java/picoded/dstack/mongodb/MongoDBStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ public static String getFullConnectionURL_primary(GenericConvertMap<String, Obje
String protocol = config.getString("protocol", "mongodb");
String user = config.getString("user", null);
String pass = config.getString("pass", null);
String host = config.getString("host", "localhost");
String host = config.getString("host", null);
int port = config.getInt("port", 27017);

// Safety check
if (host == null || host.isEmpty()) {
throw new IllegalArgumentException("Missing valid host setting for MongoDB connection");
}

// Hanlding of option string
GenericConvertMap<String, Object> optMap = config.getGenericConvertStringMap("opt",
defaultOptJson);
Expand Down Expand Up @@ -257,7 +262,7 @@ public MongoDBStack(GenericConvertMap<String, Object> inConfig) {
// ------

// Get the full_url
String full_url = getFullConnectionURL_primary(inConfig);
String full_url = getFullConnectionURL_primary(dbConfig);

// Lets build using the stable API settings
ServerApi serverApi = ServerApi.builder().version(ServerApiVersion.V1).build();
Expand All @@ -275,7 +280,7 @@ public MongoDBStack(GenericConvertMap<String, Object> inConfig) {
// ------

// Null check for secondary connection
String config_sec_mode = config.getString("sec_mode", null);
String config_sec_mode = dbConfig.getString("sec_mode", null);
if (config_sec_mode == null) {
sec_mode = null;
sec_client_conn = null;
Expand All @@ -285,7 +290,7 @@ public MongoDBStack(GenericConvertMap<String, Object> inConfig) {
sec_mode = config_sec_mode.trim().toUpperCase();

// lets get the secondary connection
full_url = getFullConnectionURL_secondary(inConfig);
full_url = getFullConnectionURL_secondary(dbConfig);
serverApi = ServerApi.builder().version(ServerApiVersion.V1).build();
settings = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString(full_url)).serverApi(serverApi).build();
Expand Down
31 changes: 26 additions & 5 deletions src/main/java/picoded/dstack/stack/ProviderConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import picoded.core.conv.ConvertJSON;
import picoded.core.struct.GenericConvertHashMap;
import picoded.core.struct.GenericConvertList;
import picoded.core.struct.GenericConvertMap;
Expand Down Expand Up @@ -47,10 +50,15 @@ public class ProviderConfig {
//
//--------------------------------------------------------------------------

// Logger to use, for config file warnings
private static final Logger LOGGER = Logger.getLogger(ProviderConfig.class.getName());

/**
* Load the provider config with provider list
**/
public ProviderConfig(List<Object> inConfigList) {
providerConfigMap = new HashMap<>();
providerStackMap = new ConcurrentHashMap<>();
loadConfigArray(inConfigList);
}

Expand All @@ -63,7 +71,7 @@ public ProviderConfig(List<Object> inConfigList) {
/**
* Stores the internal config mapping by its name
**/
protected final Map<String, GenericConvertMap<String, Object>> providerConfigMap = new HashMap<>();;
protected final Map<String, GenericConvertMap<String, Object>> providerConfigMap;

/**
* Loads a configuration array of backend providers for dstack, into the provider map
Expand Down Expand Up @@ -116,7 +124,7 @@ protected GenericConvertMap<String, Object> getStackConfig(String name) {
/**
* Stores the respective stack providers
*/
protected final ConcurrentHashMap<String, CoreStack> providerStackMap = new ConcurrentHashMap<>();
protected final ConcurrentHashMap<String, CoreStack> providerStackMap;

/**
* Get the stack of the provider specified by the name,
Expand All @@ -133,7 +141,7 @@ public CoreStack getProviderStack(String name) {
return cache;
}

synchronized (providerStackMap) {
synchronized (this) {
// Check the cache again (avoid race condition)
cache = providerStackMap.get(name);
if (cache != null) {
Expand All @@ -143,17 +151,30 @@ public CoreStack getProviderStack(String name) {
// Cache not found, get config to initialize a new stack
GenericConvertMap<String, Object> providerConfig = getStackConfig(name);
if (providerConfig == null) {
LOGGER.log(Level.SEVERE, "Unknown provider name, config not found : " + name);
throw new IllegalArgumentException("Unknown provider name, config not found : " + name);
}

// Log the setup
String type = providerConfig.getString("type");
LOGGER.info("Setting DStack provider backend : " + name + " (" + type + ")");

// Initialization of stack and store into cache
cache = initStack(providerConfig.getString("type"), providerConfig);
try {
cache = initStack(type, providerConfig);
} catch (Exception e) {
// Log the error, as this is easily missed into an API error
LOGGER.log(Level.SEVERE, "Error while setting DStack provider : " + name + " (" + type
+ ")", e);
throw new RuntimeException(e);
}

// Save it into cache
providerStackMap.put(name, cache);

// Return result
return cache;
}

}

/**
Expand Down