Skip to content

Commit

Permalink
0002666: Cache offline node database parameters used by node
Browse files Browse the repository at this point in the history
communication
  • Loading branch information
erilong committed Jul 2, 2016
1 parent 338745c commit 482e3c0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 16 deletions.
Expand Up @@ -72,6 +72,8 @@ public interface IParameterService {

public TypedProperties getDatabaseParameters(String externalId, String nodeGroupId);

public List<DatabaseParameter> getOfflineNodeParameters();

public TypedProperties getAllParameters();

public boolean isRegistrationServer();
Expand Down
Expand Up @@ -144,21 +144,26 @@ public synchronized void rereadParameters() {
}

protected TypedProperties getParameters() {
if (parameters == null
|| (cacheTimeoutInMs > 0 && lastTimeParameterWereCached < (System
.currentTimeMillis() - cacheTimeoutInMs))) {
try {
parameters = rereadApplicationParameters();
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;
long timeoutTime = System.currentTimeMillis() - cacheTimeoutInMs;
// Quick check if cache is 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();
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;
}
}
throw ex;
}
}
return parameters;
Expand Down
Expand Up @@ -257,7 +257,7 @@ protected List<Node> removeOfflineNodes(List<Node> nodes) {
if (parameterService.is(ParameterConstants.NODE_OFFLINE)) {
nodes.clear();
} else {
List<DatabaseParameter> parms = parameterService.getDatabaseParametersFor(ParameterConstants.NODE_OFFLINE);
List<DatabaseParameter> parms = parameterService.getOfflineNodeParameters();
for (DatabaseParameter parm : parms) {
Iterator<Node> iter = nodes.iterator();
while (iter.hasNext()) {
Expand All @@ -284,7 +284,7 @@ protected List<Node> getNodesToCommunicateWithOffline(CommunicationType communic
nodesToCommunicateWith.addAll(nodeService.findNodesToPull());
}
} else {
List<DatabaseParameter> parms = parameterService.getDatabaseParametersFor(ParameterConstants.NODE_OFFLINE);
List<DatabaseParameter> parms = parameterService.getOfflineNodeParameters();
nodesToCommunicateWith = new ArrayList<Node>(parms.size());
if (parms.size() > 0) {
List<Node> sourceNodes = null;
Expand Down
Expand Up @@ -52,6 +52,8 @@ public class ParameterService extends AbstractParameterService implements IParam
private ISqlTemplate sqlTemplate;

private Date lastUpdateTime;

private List<DatabaseParameter> offlineParameters;

public ParameterService(IDatabasePlatform platform, ITypedPropertiesFactory factory, String tablePrefix) {
this.tablePrefix = tablePrefix;
Expand Down Expand Up @@ -170,9 +172,14 @@ protected TypedProperties rereadApplicationParameters() {
TypedProperties p = this.factory.reload();
p.putAll(systemProperties);
p.putAll(rereadDatabaseParameters(p));
rereadOfflineNodeParameters();
return p;
}

protected synchronized void rereadOfflineNodeParameters() {
offlineParameters = getDatabaseParametersFor(ParameterConstants.NODE_OFFLINE);
}

public List<DatabaseParameter> getDatabaseParametersFor(String paramKey) {
return sqlTemplate.query(sql.getSql("selectParametersByKeySql"),
new DatabaseParameterMapper(), paramKey);
Expand All @@ -182,6 +189,13 @@ public TypedProperties getDatabaseParameters(String externalId, String nodeGroup
return readParametersFromDatabase("selectParametersSql", externalId, nodeGroupId);
}

public List<DatabaseParameter> getOfflineNodeParameters() {
if (offlineParameters == null) {
rereadOfflineNodeParameters();
}
return offlineParameters;
}

class DatabaseParameterMapper implements ISqlRowMapper<DatabaseParameter> {
IParameterFilter filter = extensionService != null ? extensionService.getExtensionPoint(IParameterFilter.class) : null;
public DatabaseParameter mapRow(Row row) {
Expand Down
Expand Up @@ -81,6 +81,10 @@ public TypedProperties getDatabaseParametersByNodeGroupId(String nodeGroupId) {
return null;
}

public List<DatabaseParameter> getOfflineNodeParameters() {
return null;
}

public String getTablePrefix() {
return "sym";
}
Expand Down

0 comments on commit 482e3c0

Please sign in to comment.