use of org.apache.helix.model.InstanceConfig 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.model.InstanceConfig in project helix by apache.
the class Quickstart method addNode.
private static void addNode() throws Exception {
NUM_NODES = NUM_NODES + 1;
int port = 12000 + NUM_NODES - 1;
InstanceConfig instanceConfig = new InstanceConfig("localhost_" + port);
instanceConfig.setHostName("localhost");
instanceConfig.setPort("" + port);
instanceConfig.setInstanceEnabled(true);
echo("ADDING NEW NODE :" + instanceConfig.getInstanceName() + ". Partitions will move from old nodes to the new node.");
admin.addInstance(CLUSTER_NAME, instanceConfig);
INSTANCE_CONFIG_LIST.add(instanceConfig);
MyProcess process = new MyProcess(instanceConfig.getInstanceName());
PROCESS_LIST.add(process);
admin.rebalance(CLUSTER_NAME, RESOURCE_NAME, 3);
process.start();
}
use of org.apache.helix.model.InstanceConfig in project helix by apache.
the class Replicator method start.
public void start() throws Exception {
// System.out.println("Starting replication for ");
isReplicationInitiated.set(true);
List<InstanceConfig> instances = getInstances(resourceName, partition, "MASTER");
if (instances.size() > 0) {
if (instances.size() == 1) {
InstanceConfig newMasterConfig = instances.get(0);
String master = newMasterConfig.getInstanceName();
if (currentMasterConfig == null || !master.equalsIgnoreCase(currentMasterConfig.getInstanceName())) {
System.out.println("Found new master:" + newMasterConfig.getInstanceName());
if (currentMasterConfig != null) {
stop();
}
currentMasterConfig = newMasterConfig;
startReplication(currentMasterConfig);
} else {
System.out.println("Already replicating from " + master);
}
} else {
System.out.println("Invalid number of masters found:" + instances);
}
} else {
System.out.println("No master found");
}
}
use of org.apache.helix.model.InstanceConfig in project helix by apache.
the class Replicator method main.
public static void main(String[] args) throws Exception {
InstanceConfig localInstanceConfig = new InstanceConfig("localhost_12001");
ZNRecord record = localInstanceConfig.getRecord();
record.setSimpleField("change_log_dir", "data/localhost_12001/translog");
record.setSimpleField("file_store_dir", "data/localhost_12001/filestore");
record.setSimpleField("check_point_dir", "data/localhost_12001/checkpoint");
InstanceConfig masterInstanceConfig = new InstanceConfig("localhost_12001");
record = masterInstanceConfig.getRecord();
record.setSimpleField("change_log_dir", "data/localhost_12000/translog");
record.setSimpleField("file_store_dir", "data/localhost_12000/filestore");
record.setSimpleField("check_point_dir", "data/localhost_12000/checkpoint");
Replicator replicator = new Replicator(localInstanceConfig, "resource", "partition");
replicator.startReplication(masterInstanceConfig);
}
use of org.apache.helix.model.InstanceConfig in project helix by apache.
the class TestHelixAdminScenariosRest method testGetInstances.
@Test
public void testGetInstances() throws IOException {
final String clusterName = "TestTagAwareness_testGetResources";
final String[] TAGS = { "tag1", "tag2" };
final String URL_BASE = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/instances";
_gSetupTool.addCluster(clusterName, true);
HelixAdmin admin = _gSetupTool.getClusterManagementTool();
// Add 4 participants, each with differint tag characteristics
InstanceConfig instance1 = new InstanceConfig("localhost_1");
instance1.addTag(TAGS[0]);
admin.addInstance(clusterName, instance1);
InstanceConfig instance2 = new InstanceConfig("localhost_2");
instance2.addTag(TAGS[1]);
admin.addInstance(clusterName, instance2);
InstanceConfig instance3 = new InstanceConfig("localhost_3");
instance3.addTag(TAGS[0]);
instance3.addTag(TAGS[1]);
admin.addInstance(clusterName, instance3);
InstanceConfig instance4 = new InstanceConfig("localhost_4");
admin.addInstance(clusterName, instance4);
// Now make a REST call for all resources
Reference resourceRef = new Reference(URL_BASE);
Request request = new Request(Method.GET, resourceRef);
Response response = _gClient.handle(request);
ListInstancesWrapper responseWrapper = ClusterRepresentationUtil.JsonToObject(ListInstancesWrapper.class, response.getEntityAsText());
Map<String, List<String>> tagInfo = responseWrapper.tagInfo;
// Ensure tag ownership is reported correctly
Assert.assertTrue(tagInfo.containsKey(TAGS[0]));
Assert.assertTrue(tagInfo.containsKey(TAGS[1]));
Assert.assertTrue(tagInfo.get(TAGS[0]).contains("localhost_1"));
Assert.assertFalse(tagInfo.get(TAGS[0]).contains("localhost_2"));
Assert.assertTrue(tagInfo.get(TAGS[0]).contains("localhost_3"));
Assert.assertFalse(tagInfo.get(TAGS[0]).contains("localhost_4"));
Assert.assertFalse(tagInfo.get(TAGS[1]).contains("localhost_1"));
Assert.assertTrue(tagInfo.get(TAGS[1]).contains("localhost_2"));
Assert.assertTrue(tagInfo.get(TAGS[1]).contains("localhost_3"));
Assert.assertFalse(tagInfo.get(TAGS[1]).contains("localhost_4"));
}
Aggregations