Skip to content

Instantly share code, notes, and snippets.

@rsumbaly
Created October 21, 2010 00:41
Show Gist options
  • Save rsumbaly/637672 to your computer and use it in GitHub Desktop.
Save rsumbaly/637672 to your computer and use it in GitHub Desktop.
Convert consistent routing xmls to zone
package voldemort;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import voldemort.cluster.Cluster;
import voldemort.cluster.Node;
import voldemort.cluster.Zone;
import voldemort.routing.RoutingStrategyType;
import voldemort.store.StoreDefinition;
import voldemort.store.StoreDefinitionBuilder;
import voldemort.store.slop.strategy.HintedHandoffStrategyType;
import voldemort.xml.ClusterMapper;
import voldemort.xml.StoreDefinitionsMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
public class UpdateXml {
public static void main(String args[]) throws IOException {
if(args.length != 2) {
System.err.println("<cluster.xml_path> <stores.xml_path>");
System.exit(-1);
}
String clusterXmlPath = args[0];
String storesXmlPath = args[1];
File clusterXml = new File(clusterXmlPath);
File storesXml = new File(storesXmlPath);
File parentDir = clusterXml.getParentFile();
String directoryName = clusterXml.getParentFile().getName();
if(!clusterXml.exists() || !storesXml.exists()) {
System.err.println("<cluster.xml_path> <stores.xml_path>");
System.exit(-1);
}
ClusterMapper clusterMapper = new ClusterMapper();
Cluster cluster = clusterMapper.readCluster(clusterXml);
StoreDefinitionsMapper storeDefMapper = new StoreDefinitionsMapper();
List<StoreDefinition> storeDefs = storeDefMapper.readStoreList(storesXml);
// Generate two zones
List<Zone> zones = Lists.newArrayList();
LinkedList<Integer> ll0 = Lists.newLinkedList();
ll0.add(1);
LinkedList<Integer> ll1 = Lists.newLinkedList();
ll1.add(0);
zones.add(new Zone(0, ll0));
zones.add(new Zone(1, ll1));
// Generate the number of nodes
List<Node> newNodes = Lists.newArrayListWithCapacity(2 * cluster.getNumberOfNodes());
for(Node node: cluster.getNodes()) {
List<Integer> partitions = Lists.newArrayList(node.getPartitionIds());
Collections.shuffle(partitions);
// Keep in first zone
List<Integer> firstPartitions = Lists.newArrayList();
int numPartitionsPerZone = partitions.size() / 2;
if(numPartitionsPerZone <= 0) {
System.err.println("Can't copy partitions from one to another since found "
+ partitions.size() + " partition(s) on node " + node);
System.exit(-1);
}
for(int i = 0; i < numPartitionsPerZone; i++) {
firstPartitions.add(partitions.get(i));
}
// Push to second zone
List<Integer> secondPartitions = Lists.newArrayList();
for(int i = numPartitionsPerZone; i < partitions.size(); i++) {
secondPartitions.add(partitions.get(i));
}
newNodes.add(new Node(node.getId(),
node.getHost(),
node.getHttpPort(),
node.getSocketPort(),
node.getAdminPort(),
0,
firstPartitions));
newNodes.add(new Node(cluster.getNumberOfNodes() + node.getId(),
"FILLME",
node.getHttpPort(),
node.getSocketPort(),
node.getAdminPort(),
1,
secondPartitions));
}
Cluster newCluster = new Cluster(cluster.getName(), newNodes, zones);
// ////////WITH STORES.XML /////////
List<StoreDefinition> newStoreDefs = Lists.newArrayListWithExpectedSize(storeDefs.size());
for(StoreDefinition def: storeDefs) {
HashMap<Integer, Integer> zoneReplicationFactor = Maps.newHashMap();
zoneReplicationFactor.put(0, def.getReplicationFactor());
zoneReplicationFactor.put(1, def.getReplicationFactor());
StoreDefinition newDef = new StoreDefinitionBuilder().setName(def.getName())
.setType(def.getType())
.setKeySerializer(def.getKeySerializer())
.setValueSerializer(def.getValueSerializer())
.setRoutingPolicy(def.getRoutingPolicy())
.setRoutingStrategyType(RoutingStrategyType.ZONE_STRATEGY)
.setReplicationFactor(def.getReplicationFactor() * 2)
.setPreferredReads(def.getPreferredReads())
.setRequiredReads(def.getRequiredReads())
.setPreferredWrites(def.getPreferredWrites())
.setRequiredWrites(def.getRequiredWrites())
.setRetentionPeriodDays(def.getRetentionDays())
.setRetentionScanThrottleRate(def.getRetentionScanThrottleRate())
.setZoneReplicationFactor(zoneReplicationFactor)
.setZoneCountReads(0)
.setZoneCountWrites(0)
.setHintedHandoffStrategy(HintedHandoffStrategyType.PROXIMITY_STRATEGY)
.build();
newStoreDefs.add(newDef);
}
// ///// WRITE THE FILES //////////
File newClusterXml = new File(parentDir, directoryName + "_cluster.xml");
File newStoresXml = new File(parentDir, directoryName + "_stores.xml");
String clusterString = clusterMapper.writeCluster(newCluster);
String storesString = storeDefMapper.writeStoreList(newStoreDefs);
FileOutputStream stream = new FileOutputStream(newClusterXml);
stream.write(clusterString.getBytes());
stream.close();
stream = new FileOutputStream(newStoresXml);
stream.write(storesString.getBytes());
stream.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment