use of org.apache.helix.HelixAdmin in project helix by apache.
the class WorkflowRebalancer method scheduleSingleJob.
/**
* Posts new job to cluster
*/
private void scheduleSingleJob(String jobResource, JobConfig jobConfig) {
HelixAdmin admin = _manager.getClusterManagmentTool();
IdealState jobIS = admin.getResourceIdealState(_manager.getClusterName(), jobResource);
if (jobIS != null) {
LOG.info("Job " + jobResource + " idealstate already exists!");
return;
}
// Set up job resource based on partitions from target resource
TaskUtil.createUserContent(_manager.getHelixPropertyStore(), jobResource, new ZNRecord(TaskUtil.USER_CONTENT_NODE));
int numIndependentTasks = jobConfig.getTaskConfigMap().size();
int numPartitions = numIndependentTasks;
if (numPartitions == 0) {
IdealState targetIs = admin.getResourceIdealState(_manager.getClusterName(), jobConfig.getTargetResource());
if (targetIs == null) {
LOG.warn("Target resource does not exist for job " + jobResource);
// do not need to fail here, the job will be marked as failure immediately when job starts running.
} else {
numPartitions = targetIs.getPartitionSet().size();
}
}
admin.addResource(_manager.getClusterName(), jobResource, numPartitions, TaskConstants.STATE_MODEL_NAME);
HelixDataAccessor accessor = _manager.getHelixDataAccessor();
// Set the job configuration
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
HelixProperty resourceConfig = new HelixProperty(jobResource);
resourceConfig.getRecord().getSimpleFields().putAll(jobConfig.getResourceConfigMap());
Map<String, TaskConfig> taskConfigMap = jobConfig.getTaskConfigMap();
if (taskConfigMap != null) {
for (TaskConfig taskConfig : taskConfigMap.values()) {
resourceConfig.getRecord().setMapField(taskConfig.getId(), taskConfig.getConfigMap());
}
}
accessor.setProperty(keyBuilder.resourceConfig(jobResource), resourceConfig);
// Push out new ideal state based on number of target partitions
IdealStateBuilder builder = new CustomModeISBuilder(jobResource);
builder.setRebalancerMode(IdealState.RebalanceMode.TASK);
builder.setNumReplica(1);
builder.setNumPartitions(numPartitions);
builder.setStateModel(TaskConstants.STATE_MODEL_NAME);
if (jobConfig.getInstanceGroupTag() != null) {
builder.setNodeGroup(jobConfig.getInstanceGroupTag());
}
if (jobConfig.isDisableExternalView()) {
builder.disableExternalView();
}
jobIS = builder.build();
for (int i = 0; i < numPartitions; i++) {
jobIS.getRecord().setListField(jobResource + "_" + i, new ArrayList<String>());
jobIS.getRecord().setMapField(jobResource + "_" + i, new HashMap<String, String>());
}
jobIS.setRebalancerClassName(JobRebalancer.class.getName());
admin.setResourceIdealState(_manager.getClusterName(), jobResource, jobIS);
}
use of org.apache.helix.HelixAdmin in project helix by apache.
the class TestDisableCustomCodeRunner method test.
@Test
public void test() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
// participant port
TestHelper.setupCluster(// participant port
clusterName, // participant port
ZK_ADDR, // participant port
12918, // participant name prefix
"localhost", // resource name prefix
"TestDB", // resources
1, // partitions per resource
PARTITION_NUM, // number of nodes
N, // replicas
2, "MasterSlave", // do rebalance
true);
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller");
controller.syncStart();
// start participants
Map<String, MockParticipantManager> participants = new HashMap<String, MockParticipantManager>();
Map<String, HelixCustomCodeRunner> customCodeRunners = new HashMap<String, HelixCustomCodeRunner>();
Map<String, DummyCallback> callbacks = new HashMap<String, DummyCallback>();
for (int i = 0; i < N; i++) {
String instanceName = "localhost_" + (12918 + i);
participants.put(instanceName, new MockParticipantManager(ZK_ADDR, clusterName, instanceName));
customCodeRunners.put(instanceName, new HelixCustomCodeRunner(participants.get(instanceName), ZK_ADDR));
callbacks.put(instanceName, new DummyCallback());
customCodeRunners.get(instanceName).invoke(callbacks.get(instanceName)).on(ChangeType.LIVE_INSTANCE).usingLeaderStandbyModel("TestParticLeader").start();
participants.get(instanceName).syncStart();
}
boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// Make sure callback is registered
BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
final HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
final String customCodeRunnerResource = customCodeRunners.get("localhost_12918").getResourceName();
ExternalView extView = accessor.getProperty(keyBuilder.externalView(customCodeRunnerResource));
Map<String, String> instanceStates = extView.getStateMap(customCodeRunnerResource + "_0");
String leader = null;
for (String instance : instanceStates.keySet()) {
String state = instanceStates.get(instance);
if ("LEADER".equals(state)) {
leader = instance;
break;
}
}
Assert.assertNotNull(leader);
for (String instance : callbacks.keySet()) {
DummyCallback callback = callbacks.get(instance);
if (instance.equals(leader)) {
Assert.assertTrue(callback.isInitTypeInvoked());
} else {
Assert.assertFalse(callback.isInitTypeInvoked());
}
callback.reset();
}
// Disable custom-code runner resource
HelixAdmin admin = new ZKHelixAdmin(_gZkClient);
admin.enableResource(clusterName, customCodeRunnerResource, false);
// Verify that states of custom-code runner are all OFFLINE
result = TestHelper.verify(new TestHelper.Verifier() {
@Override
public boolean verify() throws Exception {
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
ExternalView extView = accessor.getProperty(keyBuilder.externalView(customCodeRunnerResource));
if (extView == null) {
return false;
}
Set<String> partitionSet = extView.getPartitionSet();
if (partitionSet == null || partitionSet.size() != PARTITION_NUM) {
return false;
}
for (String partition : partitionSet) {
Map<String, String> instanceStates = extView.getStateMap(partition);
for (String state : instanceStates.values()) {
if (!"OFFLINE".equals(state)) {
return false;
}
}
}
return true;
}
}, 10 * 1000);
Assert.assertTrue(result);
// Change live-instance should not invoke any custom-code runner
LiveInstance fakeInstance = new LiveInstance("fakeInstance");
fakeInstance.setSessionId("fakeSessionId");
fakeInstance.setHelixVersion("0.6");
accessor.setProperty(keyBuilder.liveInstance("fakeInstance"), fakeInstance);
Thread.sleep(1000);
for (Map.Entry<String, DummyCallback> e : callbacks.entrySet()) {
String instance = e.getKey();
DummyCallback callback = e.getValue();
Assert.assertFalse(callback.isInitTypeInvoked());
Assert.assertFalse(callback.isCallbackTypeInvoked());
// Ensure that we were told that a leader stopped being the leader
if (instance.equals(leader)) {
Assert.assertTrue(callback.isFinalizeTypeInvoked());
}
}
// Remove fake instance
accessor.removeProperty(keyBuilder.liveInstance("fakeInstance"));
// Re-enable custom-code runner
admin.enableResource(clusterName, customCodeRunnerResource, true);
result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// Verify that custom-invoke is invoked again
extView = accessor.getProperty(keyBuilder.externalView(customCodeRunnerResource));
instanceStates = extView.getStateMap(customCodeRunnerResource + "_0");
leader = null;
for (String instance : instanceStates.keySet()) {
String state = instanceStates.get(instance);
if ("LEADER".equals(state)) {
leader = instance;
break;
}
}
Assert.assertNotNull(leader);
for (String instance : callbacks.keySet()) {
DummyCallback callback = callbacks.get(instance);
if (instance.equals(leader)) {
Assert.assertTrue(callback.isInitTypeInvoked());
} else {
Assert.assertFalse(callback.isInitTypeInvoked());
}
callback.reset();
}
// Add a fake instance should invoke custom-code runner
accessor.setProperty(keyBuilder.liveInstance("fakeInstance"), fakeInstance);
Thread.sleep(1000);
for (String instance : callbacks.keySet()) {
DummyCallback callback = callbacks.get(instance);
if (instance.equals(leader)) {
Assert.assertTrue(callback.isCallbackTypeInvoked());
} else {
Assert.assertFalse(callback.isCallbackTypeInvoked());
}
}
// Clean up
controller.syncStop();
for (MockParticipantManager participant : participants.values()) {
participant.syncStop();
}
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.HelixAdmin in project helix by apache.
the class TestDisableResource method enableResource.
private void enableResource(String clusterName, boolean enabled) {
HelixAdmin admin = new ZKHelixAdmin(_gZkClient);
admin.enableResource(clusterName, "TestDB0", enabled);
}
use of org.apache.helix.HelixAdmin in project helix by apache.
the class TestReelectedPipelineCorrectness method testReelection.
@Test
public void testReelection() throws Exception {
final int NUM_CONTROLLERS = 2;
final int NUM_PARTICIPANTS = 4;
final int NUM_PARTITIONS = 8;
final int NUM_REPLICAS = 2;
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
ClusterSetup setupTool = new ClusterSetup(ZK_ADDR);
// Set up cluster
// participant port
TestHelper.setupCluster(// participant port
clusterName, // participant port
ZK_ADDR, // participant port
12918, // participant name prefix
"localhost", // resource name prefix
"TestDB", // resources
1, // partitions per resource
NUM_PARTITIONS, // number of nodes
NUM_PARTICIPANTS, // replicas
NUM_REPLICAS, "MasterSlave", RebalanceMode.FULL_AUTO, // do rebalance
true);
// configure distributed controllers
String controllerCluster = clusterName + "_controllers";
setupTool.addCluster(controllerCluster, true);
for (int i = 0; i < NUM_CONTROLLERS; i++) {
setupTool.addInstanceToCluster(controllerCluster, "controller_" + i);
}
setupTool.activateCluster(clusterName, controllerCluster, true);
// start participants
MockParticipantManager[] participants = new MockParticipantManager[NUM_PARTICIPANTS];
for (int i = 0; i < NUM_PARTICIPANTS; i++) {
final String instanceName = "localhost_" + (12918 + i);
participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
participants[i].syncStart();
}
// start controllers
ClusterDistributedController[] controllers = new ClusterDistributedController[NUM_CONTROLLERS];
for (int i = 0; i < NUM_CONTROLLERS; i++) {
controllers[i] = new ClusterDistributedController(ZK_ADDR, controllerCluster, "controller_" + i);
controllers[i].syncStart();
}
Thread.sleep(1000);
// Ensure a balanced cluster
boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// Disable the leader, resulting in a leader election
HelixDataAccessor accessor = participants[0].getHelixDataAccessor();
LiveInstance leader = accessor.getProperty(accessor.keyBuilder().controllerLeader());
int totalWait = 0;
while (leader == null && totalWait < CHECK_TIMEOUT) {
Thread.sleep(CHECK_INTERVAL);
totalWait += CHECK_INTERVAL;
leader = accessor.getProperty(accessor.keyBuilder().controllerLeader());
}
if (totalWait >= CHECK_TIMEOUT) {
Assert.fail("No leader was ever elected!");
}
String leaderId = leader.getId();
String standbyId = (leaderId.equals("controller_0")) ? "controller_1" : "controller_0";
HelixAdmin admin = setupTool.getClusterManagementTool();
admin.enableInstance(controllerCluster, leaderId, false);
// Stop a participant to make sure that the leader election worked
Thread.sleep(500);
participants[0].syncStop();
Thread.sleep(500);
result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// Disable the original standby (leaving 0 active controllers) and kill another participant
admin.enableInstance(controllerCluster, standbyId, false);
Thread.sleep(500);
participants[1].syncStop();
// Also change the ideal state
IdealState idealState = admin.getResourceIdealState(clusterName, "TestDB0");
idealState.setMaxPartitionsPerInstance(1);
admin.setResourceIdealState(clusterName, "TestDB0", idealState);
Thread.sleep(500);
// Also disable an instance in the main cluster
admin.enableInstance(clusterName, "localhost_12920", false);
// Re-enable the original leader
admin.enableInstance(controllerCluster, leaderId, true);
// Now check that both the ideal state and the live instances are adhered to by the rebalance
Thread.sleep(500);
result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// cleanup
for (int i = 0; i < NUM_CONTROLLERS; i++) {
controllers[i].syncStop();
}
for (int i = 2; i < NUM_PARTICIPANTS; i++) {
participants[i].syncStop();
}
System.out.println("STOP " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.HelixAdmin in project helix by apache.
the class TestInvalidResourceRebalance method testResourceRebalanceSkipped.
/**
* Ensure that the Helix controller doesn't attempt to rebalance resources with invalid ideal
* states
*/
@Test
public void testResourceRebalanceSkipped() throws Exception {
final int NUM_PARTICIPANTS = 2;
final int NUM_PARTITIONS = 4;
final int NUM_REPLICAS = 2;
final String RESOURCE_NAME = "TestDB0";
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
// Set up cluster
// participant port
TestHelper.setupCluster(// participant port
clusterName, // participant port
ZK_ADDR, // participant port
12918, // participant name prefix
"localhost", // resource name prefix
"TestDB", // resources
1, // partitions per resource
NUM_PARTITIONS, // number of nodes
NUM_PARTICIPANTS, // replicas
NUM_REPLICAS, // use SEMI_AUTO mode
"MasterSlave", // use SEMI_AUTO mode
RebalanceMode.SEMI_AUTO, // do rebalance
true);
// start controller
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller");
controller.syncStart();
// add the ideal state spec (prevents non-CUSTOMIZED MasterSlave ideal states)
HelixAdmin helixAdmin = controller.getClusterManagmentTool();
Map<String, String> properties = Maps.newHashMap();
properties.put("IdealStateRule!sampleRuleName", "IDEAL_STATE_MODE=CUSTOMIZED,STATE_MODEL_DEF_REF=MasterSlave");
helixAdmin.setConfig(new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(clusterName).build(), properties);
// start participants
MockParticipantManager[] participants = new MockParticipantManager[NUM_PARTICIPANTS];
for (int i = 0; i < NUM_PARTICIPANTS; i++) {
final String instanceName = "localhost_" + (12918 + i);
participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
participants[i].syncStart();
}
Thread.sleep(1000);
boolean result = ClusterStateVerifier.verifyByZkCallback(new EmptyZkVerifier(clusterName, RESOURCE_NAME));
Assert.assertTrue(result, "External view and current state must be empty");
// cleanup
for (int i = 0; i < NUM_PARTICIPANTS; i++) {
participants[i].syncStop();
}
controller.syncStop();
}
Aggregations