Skip to content

Commit

Permalink
Further refactoring.
Browse files Browse the repository at this point in the history
src/java/voldemort/utils/ConsistencyFix.java
- rename inner class VoldemortInstance to ConsistencyFixContext
- Drop methods that were duplicated in StoreInstance

src/java/voldemort/utils/StoreInstance.java
- clean up comments for method getNodeIdListForPartitionIdList
  • Loading branch information
jayjwylie committed Mar 20, 2013
1 parent 7e98b10 commit 6ba10a1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
37 changes: 13 additions & 24 deletions src/java/voldemort/utils/ConsistencyFix.java
Expand Up @@ -48,12 +48,12 @@

public class ConsistencyFix {

private static class VoldemortInstance {
private static class ConsistencyFixContext {

private final AdminClient adminClient;
private final StoreInstance storeInstance;

public VoldemortInstance(String url, String storeName) throws Exception {
public ConsistencyFixContext(String url, String storeName) throws Exception {
System.out.println("Connecting to bootstrap server: " + url);
adminClient = new AdminClient(url, new AdminClientConfig(), 0);
Cluster cluster = adminClient.getAdminClientCluster();
Expand All @@ -69,33 +69,22 @@ public VoldemortInstance(String url, String storeName) throws Exception {
storeInstance = new StoreInstance(cluster, storeDefinition);
}

public String getStoreName() {
return storeInstance.getStoreDefinition().getName();
}

public AdminClient getAdminClient() {
return adminClient;
}

public List<Integer> getReplicationPartitionList(int partitionId) {
return storeInstance.getReplicationPartitionList(partitionId);
public StoreInstance getStoreInstance() {
return storeInstance;
}

public String getStoreName() {
return storeInstance.getStoreDefinition().getName();
}

public int getMasterPartitionId(String keyInHexFormat) throws DecoderException {
byte[] key = Hex.decodeHex(keyInHexFormat.toCharArray());
return storeInstance.getMasterPartitionId(key);
}

// Throws exception if duplicate nodes are found. I.e., partition list
// is assumed to be "replicating" partition list.
private List<Integer> getNodeIdListForPartitionIdList(List<Integer> partitionIds)
throws Exception {
return storeInstance.getNodeIdListForPartitionIdList(partitionIds);
}

public List<Integer> getReplicationNodeList(int partitionId) throws Exception {
return getNodeIdListForPartitionIdList(getReplicationPartitionList(partitionId));
}
}

public static void printUsage() {
Expand Down Expand Up @@ -240,7 +229,7 @@ public static void main(String[] args) throws Exception {
Utils.croak("Failure to create BufferedWriter for ouput file '" + options.outFile + "'");
}

VoldemortInstance vInstance = new VoldemortInstance(options.url, options.storeName);
ConsistencyFixContext vInstance = new ConsistencyFixContext(options.url, options.storeName);
for(String keyInHexFormat: options.keysInHexFormat) {
FixKeyResult fixKeyResult = fixKey(vInstance, keyInHexFormat, options.verbose);
if(fixKeyResult == FixKeyResult.SUCCESS) {
Expand Down Expand Up @@ -279,7 +268,7 @@ public String toString() {
* in a non-null object to be populated by this method.
* @return FixKeyResult
*/
private static ConsistencyFix.FixKeyResult doRead(final VoldemortInstance vInstance,
private static ConsistencyFix.FixKeyResult doRead(final ConsistencyFixContext vInstance,
final List<Integer> nodeIdList,
final byte[] keyInBytes,
final String keyInHexFormat,
Expand Down Expand Up @@ -464,7 +453,7 @@ private static List<NodeValue<ByteArray, byte[]>> resolveReadConflicts(boolean v
* non-null object to be populated by this method.
* @return
*/
private static ConsistencyFix.FixKeyResult doWriteBack(final VoldemortInstance vInstance,
private static ConsistencyFix.FixKeyResult doWriteBack(final ConsistencyFixContext vInstance,
boolean verbose,
final List<NodeValue<ByteArray, byte[]>> toReadRepair) {
if(verbose) {
Expand Down Expand Up @@ -505,7 +494,7 @@ private static ConsistencyFix.FixKeyResult doWriteBack(final VoldemortInstance v
// TODO: As a follow on, need to decide
// if queryKeys should offer a queryKey interface, and/or if repairEntry
// ought to offer a repairEntries interface.
public static ConsistencyFix.FixKeyResult fixKey(VoldemortInstance vInstance,
public static ConsistencyFix.FixKeyResult fixKey(ConsistencyFixContext vInstance,
String keyInHexFormat,
boolean verbose) {
if(verbose) {
Expand All @@ -519,7 +508,7 @@ public static ConsistencyFix.FixKeyResult fixKey(VoldemortInstance vInstance,
try {
keyInBytes = ByteUtils.fromHexString(keyInHexFormat);
masterPartitionId = vInstance.getMasterPartitionId(keyInHexFormat);
nodeIdList = vInstance.getReplicationNodeList(masterPartitionId);
nodeIdList = vInstance.getStoreInstance().getReplicationNodeList(masterPartitionId);
} catch(Exception exception) {
if(verbose) {
System.out.println("Aborting fixKey due to bad init.");
Expand Down
23 changes: 13 additions & 10 deletions src/java/voldemort/utils/StoreInstance.java
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.Set;

import voldemort.VoldemortException;
import voldemort.cluster.Cluster;
import voldemort.routing.RoutingStrategyFactory;
import voldemort.routing.RoutingStrategyType;
Expand Down Expand Up @@ -70,30 +71,32 @@ public int getNodeIdForPartitionId(int partitionId) {
return partitionIdToNodeIdMap.get(partitionId);
}

// TODO: Fix comment:
// Throws exception if duplicate nodes are found. I.e., partition list
// is assumed to be "replicating" partition list.
/**
* Converts from partitionId to nodeId. The list of partition IDs,
* partitionIds, is expected to be a "replicating partition list", i.e., the
* mapping from partition ID to node ID should be one to one.
*
* @param partitionIds
* @return
* @throws Exception
* @param partitionIds List of partition IDs for which to find the Node ID
* for the Node that owns the partition.
* @return List of node ids, one for each partition ID in partitionIds
* @throws VoldemortException If multiple partition IDs in partitionIds map
* to the same Node ID.
*/
public List<Integer> getNodeIdListForPartitionIdList(List<Integer> partitionIds)
throws Exception {
private List<Integer> getNodeIdListForPartitionIdList(List<Integer> partitionIds)
throws VoldemortException {
List<Integer> nodeIds = new ArrayList<Integer>(partitionIds.size());
for(Integer partitionId: partitionIds) {
int nodeId = getNodeIdForPartitionId(partitionId);
if(nodeIds.contains(nodeId)) {
throw new Exception("Node ID " + nodeId + " already in list of Node IDs.");
throw new VoldemortException("Node ID " + nodeId + " already in list of Node IDs.");
} else {
nodeIds.add(nodeId);
}
}
return nodeIds;
}

public List<Integer> getReplicationNodeList(int partitionId) throws Exception {
public List<Integer> getReplicationNodeList(int partitionId) throws VoldemortException {
return getNodeIdListForPartitionIdList(getReplicationPartitionList(partitionId));
}

Expand Down

0 comments on commit 6ba10a1

Please sign in to comment.