use of org.apache.helix.tools.StateModelConfigGenerator in project helix by apache.
the class IdealStateExample method main.
public static void main(String[] args) throws Exception {
if (args.length < 3) {
System.err.println("USAGE: IdealStateExample zkAddress clusterName idealStateMode (FULL_AUTO, SEMI_AUTO, or CUSTOMIZED) idealStateJsonFile (required for CUSTOMIZED mode)");
System.exit(1);
}
final String zkAddr = args[0];
final String clusterName = args[1];
final String idealStateRebalancerModeStr = args[2].toUpperCase();
String idealStateJsonFile = null;
RebalanceMode idealStateRebalancerMode = RebalanceMode.valueOf(idealStateRebalancerModeStr);
if (idealStateRebalancerMode == RebalanceMode.CUSTOMIZED) {
if (args.length < 4) {
System.err.println("Missng idealStateJsonFile for CUSTOMIZED ideal state mode");
System.exit(1);
}
idealStateJsonFile = args[3];
}
// add cluster {clusterName}
ZkClient zkclient = new ZkClient(zkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
ZKHelixAdmin admin = new ZKHelixAdmin(zkclient);
admin.addCluster(clusterName, true);
// add MasterSlave state mode definition
StateModelConfigGenerator generator = new StateModelConfigGenerator();
admin.addStateModelDef(clusterName, "MasterSlave", new StateModelDefinition(generator.generateConfigForMasterSlave()));
// add 3 participants: "localhost:{12918, 12919, 12920}"
for (int i = 0; i < 3; i++) {
int port = 12918 + i;
InstanceConfig config = new InstanceConfig("localhost_" + port);
config.setHostName("localhost");
config.setPort(Integer.toString(port));
config.setInstanceEnabled(true);
admin.addInstance(clusterName, config);
}
// add resource "TestDB" which has 4 partitions and uses MasterSlave state model
String resourceName = "TestDB";
if (idealStateRebalancerMode == RebalanceMode.SEMI_AUTO || idealStateRebalancerMode == RebalanceMode.FULL_AUTO) {
admin.addResource(clusterName, resourceName, 4, "MasterSlave", idealStateRebalancerModeStr);
// rebalance resource "TestDB" using 3 replicas
admin.rebalance(clusterName, resourceName, 3);
} else if (idealStateRebalancerMode == RebalanceMode.CUSTOMIZED) {
admin.addIdealState(clusterName, resourceName, idealStateJsonFile);
}
// start helix controller
new Thread(new Runnable() {
@Override
public void run() {
try {
HelixControllerMain.main(new String[] { "--zkSvr", zkAddr, "--cluster", clusterName });
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
// start 3 dummy participants
for (int i = 0; i < 3; i++) {
int port = 12918 + i;
final String instanceName = "localhost_" + port;
new Thread(new Runnable() {
@Override
public void run() {
DummyParticipant.main(new String[] { zkAddr, clusterName, instanceName });
}
}).start();
}
}
use of org.apache.helix.tools.StateModelConfigGenerator in project helix by apache.
the class BaseStageTest method setupStateModel.
protected void setupStateModel() {
Builder keyBuilder = accessor.keyBuilder();
ZNRecord masterSlave = new StateModelConfigGenerator().generateConfigForMasterSlave();
accessor.setProperty(keyBuilder.stateModelDef(masterSlave.getId()), new StateModelDefinition(masterSlave));
ZNRecord leaderStandby = new StateModelConfigGenerator().generateConfigForLeaderStandby();
accessor.setProperty(keyBuilder.stateModelDef(leaderStandby.getId()), new StateModelDefinition(leaderStandby));
ZNRecord onlineOffline = new StateModelConfigGenerator().generateConfigForOnlineOffline();
accessor.setProperty(keyBuilder.stateModelDef(onlineOffline.getId()), new StateModelDefinition(onlineOffline));
}
use of org.apache.helix.tools.StateModelConfigGenerator in project helix by apache.
the class SetupCluster method main.
public static void main(String[] args) {
if (args.length < 2) {
System.err.println("USAGE: java SetupCluster zookeeperAddress(e.g. localhost:2181) numberOfNodes");
System.exit(1);
}
final String zkAddr = args[0];
final int numNodes = Integer.parseInt(args[1]);
final String clusterName = DEFAULT_CLUSTER_NAME;
ZkClient zkclient = null;
try {
zkclient = new ZkClient(zkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
ZKHelixAdmin admin = new ZKHelixAdmin(zkclient);
// add cluster
admin.addCluster(clusterName, true);
// add state model definition
StateModelConfigGenerator generator = new StateModelConfigGenerator();
admin.addStateModelDef(clusterName, DEFAULT_STATE_MODEL, new StateModelDefinition(generator.generateConfigForOnlineOffline()));
// addNodes
for (int i = 0; i < numNodes; i++) {
String port = "" + (12001 + i);
String serverId = "localhost_" + port;
InstanceConfig config = new InstanceConfig(serverId);
config.setHostName("localhost");
config.setPort(port);
config.setInstanceEnabled(true);
admin.addInstance(clusterName, config);
}
// add resource "repository" which has 1 partition
String resourceName = DEFAULT_RESOURCE_NAME;
admin.addResource(clusterName, resourceName, DEFAULT_PARTITION_NUMBER, DEFAULT_STATE_MODEL, RebalanceMode.SEMI_AUTO.toString());
admin.rebalance(clusterName, resourceName, 1);
} finally {
if (zkclient != null) {
zkclient.close();
}
}
}
use of org.apache.helix.tools.StateModelConfigGenerator in project helix by apache.
the class LockManagerDemo method main.
/**
* LockManagerDemo clusterName, numInstances, lockGroupName, numLocks
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
final String zkAddress = "localhost:2199";
final String clusterName = "lock-manager-demo";
final String lockGroupName = "lock-group";
final int numInstances = 3;
final int numPartitions = 12;
final boolean startController = false;
HelixManager controllerManager = null;
Thread[] processArray;
processArray = new Thread[numInstances];
try {
startLocalZookeeper(2199);
HelixAdmin admin = new ZKHelixAdmin(zkAddress);
admin.addCluster(clusterName, true);
StateModelConfigGenerator generator = new StateModelConfigGenerator();
admin.addStateModelDef(clusterName, "OnlineOffline", new StateModelDefinition(generator.generateConfigForOnlineOffline()));
admin.addResource(clusterName, lockGroupName, numPartitions, "OnlineOffline", RebalanceMode.FULL_AUTO.toString());
admin.rebalance(clusterName, lockGroupName, 1);
for (int i = 0; i < numInstances; i++) {
final String instanceName = "localhost_" + (12000 + i);
processArray[i] = new Thread(new Runnable() {
@Override
public void run() {
LockProcess lockProcess = null;
try {
lockProcess = new LockProcess(clusterName, zkAddress, instanceName, startController);
lockProcess.start();
Thread.currentThread().join();
} catch (InterruptedException e) {
System.out.println(instanceName + "Interrupted");
if (lockProcess != null) {
lockProcess.stop();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
processArray[i].start();
}
Thread.sleep(3000);
controllerManager = HelixControllerMain.startHelixController(zkAddress, clusterName, "controller", HelixControllerMain.STANDALONE);
Thread.sleep(5000);
printStatus(admin, clusterName, lockGroupName);
System.out.println("Stopping localhost_12000");
processArray[0].interrupt();
Thread.sleep(3000);
printStatus(admin, clusterName, lockGroupName);
Thread.currentThread().join();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (controllerManager != null) {
controllerManager.disconnect();
}
for (Thread process : processArray) {
if (process != null) {
process.interrupt();
}
}
}
}
Aggregations