use of org.apache.helix.HelixManager in project helix by apache.
the class TaskExecutionDemo method startController.
private static void startController(String zkAddr, String clusterName) throws Exception {
final HelixManager manager = HelixControllerMain.startHelixController(zkAddr, clusterName, null, HelixControllerMain.STANDALONE);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.out.println("Shutting down cluster manager: " + manager.getInstanceName());
manager.disconnect();
}
});
}
use of org.apache.helix.HelixManager in project helix by apache.
the class TaskStateModel method onBecomeOnlineFromOffline.
@Transition(to = "ONLINE", from = "OFFLINE")
public void onBecomeOnlineFromOffline(Message message, NotificationContext context) throws Exception {
LOG.debug(_workerId + " becomes ONLINE from OFFLINE for " + _partition);
ConfigAccessor clusterConfig = context.getManager().getConfigAccessor();
HelixManager manager = context.getManager();
HelixConfigScope clusterScope = new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(manager.getClusterName()).build();
String json = clusterConfig.get(clusterScope, message.getResourceName());
Dag.Node node = Dag.Node.fromJson(json);
Set<String> parentIds = node.getParentIds();
String resourceName = message.getResourceName();
int numPartitions = node.getNumPartitions();
Task task = _taskFactory.createTask(resourceName, parentIds, manager, _taskResultStore);
manager.addExternalViewChangeListener(task);
LOG.debug("Starting task for " + _partition + "...");
int partitionNum = Integer.parseInt(_partition.split("_")[1]);
task.execute(resourceName, numPartitions, partitionNum);
LOG.debug("Task for " + _partition + " done");
}
use of org.apache.helix.HelixManager in project helix by apache.
the class HelixAgentMain method main.
public static void main(String[] args) throws Exception {
CommandLine cmd = processCommandLineArgs(args);
String zkAddress = cmd.getOptionValue(zkAddr);
String clusterName = cmd.getOptionValue(cluster);
String instance = cmd.getOptionValue(instanceName);
String stateModelName = cmd.getOptionValue(stateModel);
HelixManager manager = new ZKHelixManager(clusterName, instance, InstanceType.PARTICIPANT, zkAddress);
StateMachineEngine stateMach = manager.getStateMachineEngine();
stateMach.registerStateModelFactory(stateModelName, new AgentStateModelFactory());
Runtime.getRuntime().addShutdownHook(new HelixAgentShutdownHook(manager));
try {
manager.connect();
Thread.currentThread().join();
} catch (Exception e) {
LOG.error(e.toString());
} finally {
if (manager != null && manager.isConnected()) {
manager.disconnect();
}
}
}
use of org.apache.helix.HelixManager in project helix by apache.
the class TestResourceAccessor method createDummyMapping.
/**
* Creates a setup where the health API can be tested.
*
* @param clusterName
* @param resourceName
* @param idealStateParams
* @param partitionReplicaStates maps partitionName to its replicas' states
* @throws Exception
*/
private void createDummyMapping(String clusterName, String resourceName, Map<String, String> idealStateParams, Map<String, List<String>> partitionReplicaStates) throws Exception {
IdealState idealState = new IdealState(resourceName);
// 2
idealState.setMinActiveReplicas(Integer.parseInt(idealStateParams.get("MinActiveReplicas")));
// MasterSlave
idealState.setStateModelDefRef(idealStateParams.get("StateModelDefRef"));
// 3
idealState.setMaxPartitionsPerInstance(Integer.parseInt(idealStateParams.get("MaxPartitionsPerInstance")));
// 3
idealState.setReplicas(idealStateParams.get("Replicas"));
// 3
idealState.setNumPartitions(Integer.parseInt(idealStateParams.get("NumPartitions")));
idealState.enable(false);
Map<String, List<String>> partitionNames = new LinkedHashMap<>();
List<String> dummyPrefList = new ArrayList<>();
for (int i = 0; i < Integer.parseInt(idealStateParams.get("MaxPartitionsPerInstance")); i++) {
dummyPrefList.add(ANY_INSTANCE);
partitionNames.put("p" + i, dummyPrefList);
}
idealState.getRecord().getListFields().putAll(partitionNames);
if (!_gSetupTool.getClusterManagementTool().getClusters().contains(clusterName)) {
_gSetupTool.getClusterManagementTool().addCluster(clusterName);
}
_gSetupTool.getClusterManagementTool().setResourceIdealState(clusterName, resourceName, idealState);
// Set ExternalView's replica states for a given parameter map
ExternalView externalView = new ExternalView(resourceName);
Map<String, Map<String, String>> mappingCurrent = new LinkedHashMap<>();
List<String> partitionReplicaStatesList = new ArrayList<>(partitionReplicaStates.keySet());
for (int k = 0; k < partitionReplicaStatesList.size(); k++) {
Map<String, String> replicaStatesForPartition = new LinkedHashMap<>();
List<String> replicaStateList = partitionReplicaStates.get(partitionReplicaStatesList.get(k));
for (int i = 0; i < replicaStateList.size(); i++) {
replicaStatesForPartition.put("r" + i, replicaStateList.get(i));
}
mappingCurrent.put("p" + k, replicaStatesForPartition);
}
externalView.getRecord().getMapFields().putAll(mappingCurrent);
HelixManager helixManager = HelixManagerFactory.getZKHelixManager(clusterName, "p1", InstanceType.ADMINISTRATOR, ZK_ADDR);
helixManager.connect();
HelixDataAccessor helixDataAccessor = helixManager.getHelixDataAccessor();
helixDataAccessor.setProperty(helixDataAccessor.keyBuilder().externalView(resourceName), externalView);
}
use of org.apache.helix.HelixManager in project helix by apache.
the class TestDistControllerElection method testControllerParticipant.
@Test()
public void testControllerParticipant() throws Exception {
String className = getShortClassName();
LOG.info("RUN " + className + " at " + new Date(System.currentTimeMillis()));
final String clusterName = CONTROLLER_CLUSTER_PREFIX + "_" + className + "_" + "testControllerParticipant";
String path = "/" + clusterName;
if (_gZkClient.exists(path)) {
_gZkClient.deleteRecursively(path);
}
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_gZkClient));
Builder keyBuilder = accessor.keyBuilder();
TestHelper.setupEmptyCluster(_gZkClient, clusterName);
final String controllerName = "controller_0";
HelixManager manager = new MockZKHelixManager(clusterName, controllerName, InstanceType.CONTROLLER_PARTICIPANT, _gZkClient);
GenericHelixController controller0 = new GenericHelixController();
List<HelixTimerTask> timerTasks = Collections.emptyList();
DistributedLeaderElection election = new DistributedLeaderElection(manager, controller0, timerTasks);
NotificationContext context = new NotificationContext(manager);
context.setType(NotificationContext.Type.CALLBACK);
election.onControllerChange(context);
LiveInstance liveInstance = accessor.getProperty(keyBuilder.controllerLeader());
AssertJUnit.assertEquals(controllerName, liveInstance.getInstanceName());
// path = PropertyPathConfig.getPath(PropertyType.LEADER, clusterName);
// ZNRecord leaderRecord = _gZkClient.<ZNRecord> readData(path);
// AssertJUnit.assertEquals(controllerName, leaderRecord.getSimpleField("LEADER"));
// AssertJUnit.assertNotNull(election.getController());
// AssertJUnit.assertNotNull(election.getLeader());
manager = new MockZKHelixManager(clusterName, "controller_1", InstanceType.CONTROLLER_PARTICIPANT, _gZkClient);
GenericHelixController controller1 = new GenericHelixController();
election = new DistributedLeaderElection(manager, controller1, timerTasks);
context = new NotificationContext(manager);
context.setType(NotificationContext.Type.CALLBACK);
election.onControllerChange(context);
liveInstance = accessor.getProperty(keyBuilder.controllerLeader());
AssertJUnit.assertEquals(controllerName, liveInstance.getInstanceName());
// leaderRecord = _gZkClient.<ZNRecord> readData(path);
// AssertJUnit.assertEquals(controllerName, leaderRecord.getSimpleField("LEADER"));
// AssertJUnit.assertNull(election.getController());
// AssertJUnit.assertNull(election.getLeader());
LOG.info("END " + getShortClassName() + " at " + new Date(System.currentTimeMillis()));
}
Aggregations