Skip to content

Commit

Permalink
Reduced synchronization when lazily initializing failureDetector.
Browse files Browse the repository at this point in the history
  • Loading branch information
afeinberg committed Jan 30, 2010
1 parent 59d4b24 commit 9dce097
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/java/voldemort/client/AbstractStoreClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public abstract class AbstractStoreClientFactory implements StoreClientFactory {
private final boolean isJmxEnabled;
private final RequestFormatType requestFormatType;
private final int jmxId;
protected FailureDetector failureDetector;
protected volatile FailureDetector failureDetector;
private final int maxBootstrapRetries;
private final StoreStats stats;
private final ClientConfig config;
Expand Down Expand Up @@ -194,14 +194,21 @@ public <K, V> Store<K, V> getRawStore(String storeName,
protected abstract FailureDetector initFailureDetector(final ClientConfig config,
final Collection<Node> nodes);

public synchronized FailureDetector getFailureDetector() {
if (failureDetector == null) {
public FailureDetector getFailureDetector() {
// first check: avoids locking as the field is volatile
FailureDetector result = failureDetector;
if (result == null) {
String clusterXml = bootstrapMetadataWithRetries(MetadataStore.CLUSTER_KEY, bootstrapUrls);
Cluster cluster = clusterMapper.readCluster(new StringReader(clusterXml));
failureDetector = initFailureDetector(config, cluster.getNodes());
synchronized(this) {
// second check: avoids double initialization
result = failureDetector;
if (result == null)
failureDetector = result = initFailureDetector(config, cluster.getNodes());
}
}

return failureDetector;
return result;
}

private CompressionStrategy getCompressionStrategy(SerializerDefinition serializerDef) {
Expand Down

0 comments on commit 9dce097

Please sign in to comment.