Skip to content

Commit

Permalink
Making the methods of RebalancePartitionsInfo synchronized so as to a…
Browse files Browse the repository at this point in the history
…void the race condition in asMap and removeStore
  • Loading branch information
Chinmay Soman committed Apr 23, 2013
1 parent ff2d58f commit 77e08d7
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/java/voldemort/client/rebalance/RebalancePartitionsInfo.java
Expand Up @@ -93,7 +93,7 @@ public RebalancePartitionsInfo(int stealerNodeId,
this.initialCluster = Utils.notNull(initialCluster);
}

private void findMaxReplicaType(HashMap<String, HashMap<Integer, List<Integer>>> storeToReplicaToPartitionList) {
private synchronized void findMaxReplicaType(HashMap<String, HashMap<Integer, List<Integer>>> storeToReplicaToPartitionList) {
for(Entry<String, HashMap<Integer, List<Integer>>> entry: storeToReplicaToPartitionList.entrySet()) {
for(Entry<Integer, List<Integer>> replicaToPartitionList: entry.getValue().entrySet()) {
if(replicaToPartitionList.getKey() > this.maxReplica) {
Expand All @@ -103,7 +103,7 @@ private void findMaxReplicaType(HashMap<String, HashMap<Integer, List<Integer>>>
}
}

public static RebalancePartitionsInfo create(String line) {
public synchronized static RebalancePartitionsInfo create(String line) {
try {
JsonReader reader = new JsonReader(new StringReader(line));
Map<String, ?> map = reader.readObject();
Expand All @@ -113,7 +113,7 @@ public static RebalancePartitionsInfo create(String line) {
}
}

public static RebalancePartitionsInfo create(Map<?, ?> map) {
public synchronized static RebalancePartitionsInfo create(Map<?, ?> map) {
int stealerId = (Integer) map.get("stealerId");
int donorId = (Integer) map.get("donorId");
List<String> unbalancedStoreList = Utils.uncheckedCast(map.get("unbalancedStores"));
Expand Down Expand Up @@ -160,7 +160,7 @@ public static RebalancePartitionsInfo create(Map<?, ?> map) {
attempt);
}

public ImmutableMap<String, Object> asMap() {
public synchronized ImmutableMap<String, Object> asMap() {
ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<String, Object>();

builder.put("stealerId", stealerId)
Expand Down Expand Up @@ -203,23 +203,23 @@ public ImmutableMap<String, Object> asMap() {
return builder.build();
}

public void setAttempt(int attempt) {
public synchronized void setAttempt(int attempt) {
this.attempt = attempt;
}

public int getDonorId() {
public synchronized int getDonorId() {
return donorId;
}

public int getAttempt() {
public synchronized int getAttempt() {
return attempt;
}

public int getStealerId() {
public synchronized int getStealerId() {
return stealerId;
}

public Cluster getInitialCluster() {
public synchronized Cluster getInitialCluster() {
return initialCluster;
}

Expand All @@ -229,35 +229,35 @@ public Cluster getInitialCluster() {
*
* @return Set of store names
*/
public Set<String> getUnbalancedStoreList() {
public synchronized Set<String> getUnbalancedStoreList() {
return storeToReplicaToAddPartitionList.keySet();
}

public HashMap<String, HashMap<Integer, List<Integer>>> getStoreToReplicaToAddPartitionList() {
public synchronized HashMap<String, HashMap<Integer, List<Integer>>> getStoreToReplicaToAddPartitionList() {
return this.storeToReplicaToAddPartitionList;
}

public HashMap<String, HashMap<Integer, List<Integer>>> getStoreToReplicaToDeletePartitionList() {
public synchronized HashMap<String, HashMap<Integer, List<Integer>>> getStoreToReplicaToDeletePartitionList() {
return this.storeToReplicaToDeletePartitionList;
}

public HashMap<Integer, List<Integer>> getReplicaToAddPartitionList(String storeName) {
public synchronized HashMap<Integer, List<Integer>> getReplicaToAddPartitionList(String storeName) {
return this.storeToReplicaToAddPartitionList.get(storeName);
}

public HashMap<Integer, List<Integer>> getReplicaToDeletePartitionList(String storeName) {
public synchronized HashMap<Integer, List<Integer>> getReplicaToDeletePartitionList(String storeName) {
return this.storeToReplicaToDeletePartitionList.get(storeName);
}

public void setStoreToReplicaToAddPartitionList(HashMap<String, HashMap<Integer, List<Integer>>> storeToReplicaToAddPartitionList) {
public synchronized void setStoreToReplicaToAddPartitionList(HashMap<String, HashMap<Integer, List<Integer>>> storeToReplicaToAddPartitionList) {
this.storeToReplicaToAddPartitionList = storeToReplicaToAddPartitionList;
}

public void setStoreToReplicaToDeletePartitionList(HashMap<String, HashMap<Integer, List<Integer>>> storeToReplicaToDeletePartitionList) {
public synchronized void setStoreToReplicaToDeletePartitionList(HashMap<String, HashMap<Integer, List<Integer>>> storeToReplicaToDeletePartitionList) {
this.storeToReplicaToDeletePartitionList = storeToReplicaToDeletePartitionList;
}

public void removeStore(String storeName) {
public synchronized void removeStore(String storeName) {
this.storeToReplicaToAddPartitionList.remove(storeName);
this.storeToReplicaToDeletePartitionList.remove(storeName);
}
Expand All @@ -267,7 +267,7 @@ public void removeStore(String storeName) {
*
* @return List of primary partitions
*/
public List<Integer> getStealMasterPartitions() {
public synchronized List<Integer> getStealMasterPartitions() {
Iterator<HashMap<Integer, List<Integer>>> iter = storeToReplicaToAddPartitionList.values()
.iterator();
List<Integer> primaryPartitionsBeingMoved = Lists.newArrayList();
Expand All @@ -280,7 +280,7 @@ public List<Integer> getStealMasterPartitions() {
}

@Override
public String toString() {
public synchronized String toString() {
StringBuffer sb = new StringBuffer();
sb.append("\nRebalancePartitionsInfo(" + getStealerId() + " ["
+ initialCluster.getNodeById(getStealerId()).getHost() + "] <--- " + getDonorId()
Expand Down Expand Up @@ -311,7 +311,7 @@ public String toString() {
return sb.toString();
}

public String toJsonString() {
public synchronized String toJsonString() {
Map<String, Object> map = asMap();

StringWriter writer = new StringWriter();
Expand Down

0 comments on commit 77e08d7

Please sign in to comment.