Permalink
Browse files

Reduced synchronization when lazily initializing failureDetector.

1 parent 0ebb193 commit e4cd7fe655c3afa811109f1ebc54965600b9ffb5 @afeinberg afeinberg committed Jan 30, 2010
Showing with 12 additions and 5 deletions.
  1. +12 −5 src/java/voldemort/client/AbstractStoreClientFactory.java
@@ -86,7 +86,7 @@
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;
@@ -194,14 +194,21 @@ public AbstractStoreClientFactory(ClientConfig config) {
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) {

0 comments on commit e4cd7fe

Please sign in to comment.