Skip to content
Permalink
Browse files

0003959: Merged cherry-picked commit from 3.9 to 3.10

  • Loading branch information...
davecramer authored and jaredfrees committed May 15, 2019
1 parent 17a39bc commit ea4e3601ffa545221ebf0384e91ef3ee83285661
@@ -42,7 +42,7 @@
* This is static because the MongoClient is thread safe and wraps a pool of
* connections
*/
protected static Map<String, MongoClient> clients = new HashMap<String, MongoClient>();
protected final static Map<String, MongoClient> clients = new HashMap<String, MongoClient>();

protected DB currentDB;

@@ -52,36 +52,31 @@ public SimpleMongoClientManager(IParameterService parameterService, String name)
}

@Override
public MongoClient get() {
public synchronized MongoClient get() {
MongoClient client = clients.get(name);
if (client == null) {
synchronized (clients) {
if (client == null) {
int port = 27017;
String host = "localhost";

if (parameterService != null) {
port = parameterService.getInt(name + MongoConstants.PORT, port);
host = parameterService.getString(name + MongoConstants.HOST, host);
}
String dbUrl = "mongodb://" + host + ":" + port;
if (parameterService != null) {
dbUrl = parameterService.getString(name + MongoConstants.URL, dbUrl);
}
try {
client = new MongoClient(new MongoClientURI(dbUrl));
clients.put(name, client);
} catch (UnknownHostException e) {
throw new SymmetricException(e);
}
}
int port = 27017;
String host = "localhost";
if (parameterService != null) {
port = parameterService.getInt(name + MongoConstants.PORT, port);
host = parameterService.getString(name + MongoConstants.HOST, host);
}
String dbUrl = "mongodb://" + host + ":" + port;
if (parameterService != null) {
dbUrl = parameterService.getString(name + MongoConstants.URL, dbUrl);
}
try {
client = new MongoClient(new MongoClientURI(dbUrl));
clients.put(name, client);
} catch (UnknownHostException e) {
throw new SymmetricException(e);
}
}
return client;
}

@Override
public DB getDB(String name) {
public synchronized DB getDB(String name) {
if (currentDB == null || !currentDB.getName().equals(name)) {
currentDB = get().getDB(name);
/**
@@ -151,28 +151,23 @@ public synchronized void rereadParameters() {
getParameters();
}

protected TypedProperties getParameters() {
protected synchronized TypedProperties getParameters() {
long timeoutTime = System.currentTimeMillis() - cacheTimeoutInMs;
// Quick check if cache is timed out
// see if the parameters have timed out
if (parameters == null || (cacheTimeoutInMs > 0 && lastTimeParameterWereCached < timeoutTime)) {
synchronized (this) {
// Synchronized check to prevent calling reread parameters multiple times
if (parameters == null || (cacheTimeoutInMs > 0 && lastTimeParameterWereCached < timeoutTime)) {
try {
parameters = rereadApplicationParameters();
SymmetricUtils.replaceSystemAndEnvironmentVariables(parameters);
lastTimeParameterWereCached = System.currentTimeMillis();
cacheTimeoutInMs = getInt(ParameterConstants.PARAMETER_REFRESH_PERIOD_IN_MS);
} catch (SqlException ex) {
if (parameters != null) {
log.warn("Could not read database parameters. We will try again later", ex);
} else {
log.error("Could not read database parameters and they have not yet been initialized");
throw ex;
}
throw ex;
}
try {
parameters = rereadApplicationParameters();
SymmetricUtils.replaceSystemAndEnvironmentVariables(parameters);
lastTimeParameterWereCached = System.currentTimeMillis();
cacheTimeoutInMs = getInt(ParameterConstants.PARAMETER_REFRESH_PERIOD_IN_MS);
} catch (SqlException ex) {
if (parameters != null) {
log.warn("Could not read database parameters. We will try again later", ex);
} else {
log.error("Could not read database parameters and they have not yet been initialized");
throw ex;
}
throw ex;
}
}
return parameters;
@@ -187,7 +182,7 @@ public Date getLastTimeParameterWereCached() {
}


public String getExternalId() {
public synchronized String getExternalId() {
if (externalId==null) {
String value = getString(ParameterConstants.EXTERNAL_ID);
value = substituteScripts(value);
@@ -196,10 +191,10 @@ public String getExternalId() {
log.debug("External Id eval results in: {}",externalId);
}
}
return externalId;
return externalId;
}

public String getSyncUrl() {
public synchronized String getSyncUrl() {
if (syncUrl==null) {
String value = getString(ParameterConstants.SYNC_URL);
value = substituteScripts(value);
@@ -211,10 +206,10 @@ public String getSyncUrl() {
log.debug("Sync URL eval results in: {}",syncUrl);
}
}
return syncUrl;
return syncUrl;
}

public String getNodeGroupId() {
public synchronized String getNodeGroupId() {
if (nodeGroupId==null) {
String value = getString(ParameterConstants.NODE_GROUP_ID);
value = substituteScripts(value);
@@ -223,10 +218,10 @@ public String getNodeGroupId() {
log.debug("Node Group Id eval results in: {}",nodeGroupId);
}
}
return nodeGroupId;
return nodeGroupId;
}

public String getRegistrationUrl() {
public synchronized String getRegistrationUrl() {
if (registrationUrl==null) {
String value = getString(ParameterConstants.REGISTRATION_URL);
value = substituteScripts(value);
@@ -238,10 +233,10 @@ public String getRegistrationUrl() {
log.debug("Registration URL eval results in: {}",registrationUrl);
}
}
return registrationUrl;
return registrationUrl;
}

public String getEngineName() {
public synchronized String getEngineName() {
if (engineName==null) {
String value = getString(ParameterConstants.ENGINE_NAME,"SymmetricDS");
value = substituteScripts(value);
@@ -263,7 +258,7 @@ public String getEngineName() {
return replacementValues;
}

public void setDatabaseHasBeenInitialized(boolean databaseHasBeenInitialized) {
public synchronized void setDatabaseHasBeenInitialized(boolean databaseHasBeenInitialized) {
if (this.databaseHasBeenInitialized != databaseHasBeenInitialized) {
this.databaseHasBeenInitialized = databaseHasBeenInitialized;
this.parameters = null;
@@ -272,7 +267,7 @@ public void setDatabaseHasBeenInitialized(boolean databaseHasBeenInitialized) {

abstract public TypedProperties getDatabaseParameters(String externalId, String nodeGroupId);

protected TypedProperties rereadDatabaseParameters(Properties p) {
protected synchronized TypedProperties rereadDatabaseParameters(Properties p) {
if (databaseHasBeenInitialized) {
TypedProperties properties = getDatabaseParameters(ParameterConstants.ALL,
ParameterConstants.ALL);
@@ -281,7 +276,6 @@ protected TypedProperties rereadDatabaseParameters(Properties p) {
properties.putAll(getDatabaseParameters(
p.getProperty(ParameterConstants.EXTERNAL_ID),
p.getProperty(ParameterConstants.NODE_GROUP_ID)));
databaseHasBeenInitialized = true;
return properties;
} else {
return new TypedProperties();
@@ -96,25 +96,21 @@ public NodeCommunicationService(IClusterService clusterService, INodeService nod
}
}

private final void initialize() {
private synchronized final void initialize() {
if (!initialized) {
synchronized (this) {
if (!initialized) {
if (clusterService.isClusteringEnabled()) {
try {
int locksCleared = sqlTemplate.update(getSql("clearLocksOnRestartSql"),
clusterService.getServerId());
if (locksCleared > 0) {
log.info("Cleared {} node communication locks for {}", locksCleared,
clusterService.getServerId());
}
} finally {
initialized = true;
}
} else {
initialized = true;
if (clusterService.isClusteringEnabled()) {
try {
int locksCleared = sqlTemplate.update(getSql("clearLocksOnRestartSql"),
clusterService.getServerId());
if (locksCleared > 0) {
log.info("Cleared {} node communication locks for {}", locksCleared,
clusterService.getServerId());
}
} finally {
initialized = true;
}
} else {
initialized = true;
}
}
}

1 comment on commit ea4e360

@jaredfrees

This comment has been minimized.

Copy link
Contributor

commented on ea4e360 May 15, 2019

I messed up the commit message and didn't give a good description. Here is the original commit to branch 3.9 for more information: 53a7207

Please sign in to comment.
You can’t perform that action at this time.