use of org.apache.helix.manager.zk.ZKHelixAdmin 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.manager.zk.ZKHelixAdmin 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();
}
}
}
}
use of org.apache.helix.manager.zk.ZKHelixAdmin in project helix by apache.
the class LockProcess method configureInstance.
/**
* Configure the instance, the configuration of each node is available to
* other nodes.
* @param instanceName
*/
private void configureInstance(String instanceName) {
ZKHelixAdmin helixAdmin = new ZKHelixAdmin(zkAddress);
List<String> instancesInCluster = helixAdmin.getInstancesInCluster(clusterName);
if (instancesInCluster == null || !instancesInCluster.contains(instanceName)) {
InstanceConfig config = new InstanceConfig(instanceName);
config.setHostName("localhost");
config.setPort("12000");
helixAdmin.addInstance(clusterName, config);
}
}
use of org.apache.helix.manager.zk.ZKHelixAdmin in project helix by apache.
the class Consumer method main.
public static void main(String[] args) throws Exception {
if (args.length < 3) {
System.err.println("USAGE: java Consumer zookeeperAddress (e.g. localhost:2181) consumerId (0-2), rabbitmqServer (e.g. localhost)");
System.exit(1);
}
final String zkAddr = args[0];
final String clusterName = SetupConsumerCluster.DEFAULT_CLUSTER_NAME;
final String consumerId = args[1];
final String mqServer = args[2];
ZkClient zkclient = null;
try {
// add node to cluster if not already added
zkclient = new ZkClient(zkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
ZKHelixAdmin admin = new ZKHelixAdmin(zkclient);
List<String> nodes = admin.getInstancesInCluster(clusterName);
if (!nodes.contains("consumer_" + consumerId)) {
InstanceConfig config = new InstanceConfig("consumer_" + consumerId);
config.setHostName("localhost");
config.setInstanceEnabled(true);
admin.addInstance(clusterName, config);
}
// start consumer
final Consumer consumer = new Consumer(zkAddr, clusterName, "consumer_" + consumerId, mqServer);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.out.println("Shutting down consumer_" + consumerId);
consumer.disconnect();
}
});
consumer.connect();
} finally {
if (zkclient != null) {
zkclient.close();
}
}
}
use of org.apache.helix.manager.zk.ZKHelixAdmin in project helix by apache.
the class SetupConsumerCluster method main.
public static void main(String[] args) {
if (args.length < 1) {
System.err.println("USAGE: java SetupConsumerCluster zookeeperAddress (e.g. localhost:2181)");
System.exit(1);
}
final String zkAddr = args[0];
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
admin.addStateModelDef(clusterName, DEFAULT_STATE_MODEL, new StateModelDefinition(StateModelConfigGenerator.generateConfigForOnlineOffline()));
// add resource "topic" which has 6 partitions
String resourceName = DEFAULT_RESOURCE_NAME;
admin.addResource(clusterName, resourceName, DEFAULT_PARTITION_NUMBER, DEFAULT_STATE_MODEL, RebalanceMode.FULL_AUTO.toString());
admin.rebalance(clusterName, resourceName, 1);
} finally {
if (zkclient != null) {
zkclient.close();
}
}
}
Aggregations