Search in sources :

Example 11 with HelixManager

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();
        }
    });
}
Also used : HelixManager(org.apache.helix.HelixManager)

Example 12 with HelixManager

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");
}
Also used : HelixManager(org.apache.helix.HelixManager) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) ConfigAccessor(org.apache.helix.ConfigAccessor) HelixConfigScope(org.apache.helix.model.HelixConfigScope) Transition(org.apache.helix.participant.statemachine.Transition)

Example 13 with HelixManager

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();
        }
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) HelixManager(org.apache.helix.HelixManager) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) StateMachineEngine(org.apache.helix.participant.StateMachineEngine) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) ParseException(org.apache.commons.cli.ParseException)

Example 14 with HelixManager

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);
}
Also used : ExternalView(org.apache.helix.model.ExternalView) HelixManager(org.apache.helix.HelixManager) ArrayList(java.util.ArrayList) IdealState(org.apache.helix.model.IdealState) LinkedHashMap(java.util.LinkedHashMap) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 15 with HelixManager

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()));
}
Also used : ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) HelixManager(org.apache.helix.HelixManager) Builder(org.apache.helix.PropertyKey.Builder) PropertyPathBuilder(org.apache.helix.PropertyPathBuilder) DistributedLeaderElection(org.apache.helix.manager.zk.DistributedLeaderElection) Date(java.util.Date) NotificationContext(org.apache.helix.NotificationContext) HelixTimerTask(org.apache.helix.HelixTimerTask) LiveInstance(org.apache.helix.model.LiveInstance) GenericHelixController(org.apache.helix.controller.GenericHelixController) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Aggregations

HelixManager (org.apache.helix.HelixManager)115 Test (org.testng.annotations.Test)49 HelixDataAccessor (org.apache.helix.HelixDataAccessor)35 ZNRecord (org.apache.helix.ZNRecord)28 Message (org.apache.helix.model.Message)23 PropertyKey (org.apache.helix.PropertyKey)20 Date (java.util.Date)18 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)17 Builder (org.apache.helix.PropertyKey.Builder)16 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)12 HelixException (org.apache.helix.HelixException)11 ExternalView (org.apache.helix.model.ExternalView)11 NotificationContext (org.apache.helix.NotificationContext)10 LiveInstance (org.apache.helix.model.LiveInstance)10 IdealState (org.apache.helix.model.IdealState)9 Criteria (org.apache.helix.Criteria)8 HelixAdmin (org.apache.helix.HelixAdmin)8 ZKHelixManager (org.apache.helix.manager.zk.ZKHelixManager)8 StringWriter (java.io.StringWriter)7