Java Code Examples for org.apache.helix.HelixManager#addExternalViewChangeListener()

The following examples show how to use org.apache.helix.HelixManager#addExternalViewChangeListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenericLeaderStandbyModel.java    From helix with Apache License 2.0 6 votes vote down vote up
@Transition(to = "LEADER", from = "STANDBY")
public void onBecomeLeaderFromStandby(Message message, NotificationContext context)
    throws Exception {
  LOG.info("Become LEADER from STANDBY");
  HelixManager manager = context.getManager();
  if (manager == null) {
    throw new IllegalArgumentException("Require HelixManager in notification conext");
  }
  for (ChangeType notificationType : _notificationTypes) {
    if (notificationType == ChangeType.LIVE_INSTANCE) {
      manager.addLiveInstanceChangeListener(_particHolder);
    } else if (notificationType == ChangeType.CONFIG) {
      manager.addConfigChangeListener(_particHolder);
    } else if (notificationType == ChangeType.EXTERNAL_VIEW) {
      manager.addExternalViewChangeListener(_particHolder);
    } else {
      LOG.error("Unsupport notificationType:" + notificationType.toString());
    }
  }
}
 
Example 2
Source File: TestBasicSpectator.java    From helix with Apache License 2.0 6 votes vote down vote up
@Test
public void TestSpectator() throws Exception {
  HelixManager relayHelixManager =
      HelixManagerFactory.getZKHelixManager(CLUSTER_NAME, null, InstanceType.SPECTATOR, ZK_ADDR);

  relayHelixManager.connect();
  relayHelixManager.addExternalViewChangeListener(this);

  _gSetupTool.addResourceToCluster(CLUSTER_NAME, "NextDB", 64, STATE_MODEL);
  _gSetupTool.rebalanceStorageCluster(CLUSTER_NAME, "NextDB", 3);

  boolean result =
      ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(
          ZK_ADDR, CLUSTER_NAME));

  Assert.assertTrue(result);

  Assert.assertTrue(_externalViewChanges.containsKey("NextDB"));
  Assert.assertTrue(_externalViewChanges.containsKey(TEST_DB));

}
 
Example 3
Source File: TaskStateModel.java    From helix with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example 4
Source File: TestMessageThrottle2.java    From helix with Apache License 2.0 5 votes vote down vote up
void registerWith(HelixManager helixManager) throws Exception {
  helixManager.addIdealStateChangeListener(this);
  helixManager.addInstanceConfigChangeListener(this);
  helixManager.addExternalViewChangeListener(this);
  helixManager.addLiveInstanceChangeListener(this);
  helixManager.addControllerListener(this);
}
 
Example 5
Source File: TestCorrectnessOnConnectivityLoss.java    From helix with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Test
public void testSpectator() throws Exception {
  Map<String, Integer> stateReachedCounts = Maps.newHashMap();
  HelixManager participant =
      HelixManagerFactory.getZKHelixManager(_clusterName, "localhost_12918",
          InstanceType.PARTICIPANT, ZK_ADDR);
  participant.getStateMachineEngine().registerStateModelFactory("OnlineOffline",
      new MyStateModelFactory(stateReachedCounts));
  participant.connect();

  RoutingTableProvider routingTableProvider = new RoutingTableProvider();
  try {
    HelixManager spectator = HelixManagerFactory
        .getZKHelixManager(_clusterName, "spectator", InstanceType.SPECTATOR, ZK_ADDR);
    spectator.connect();
    spectator.addConfigChangeListener(routingTableProvider);
    spectator.addExternalViewChangeListener(routingTableProvider);
    Thread.sleep(1000);

    // Now let's stop the ZK server; this should do nothing
    TestHelper.stopZkServer(_zkServer);
    Thread.sleep(1000);

    // Verify routing table still works
    Assert.assertEquals(routingTableProvider.getInstances("resource0", "ONLINE").size(), 1);
    Assert.assertEquals(routingTableProvider.getInstances("resource0", "OFFLINE").size(), 0);
  } finally {
    routingTableProvider.shutdown();
    if (participant.isConnected()) {
      participant.disconnect();
    }
  }
}
 
Example 6
Source File: TestZeroReplicaAvoidance.java    From helix with Apache License 2.0 5 votes vote down vote up
@Test
public void testDelayedRebalancer() throws Exception {
  System.out.println("START testDelayedRebalancer at " + new Date(System.currentTimeMillis()));
  HelixManager manager =
      HelixManagerFactory.getZKHelixManager(CLUSTER_NAME, null, InstanceType.SPECTATOR, ZK_ADDR);
  manager.connect();
  manager.addExternalViewChangeListener(this);
  manager.addIdealStateChangeListener(this);
  enablePersistBestPossibleAssignment(_gZkClient, CLUSTER_NAME, true);

  // Start half number of nodes.
  int i = 0;
  for (; i < NUM_NODE / 2; i++) {
    _participants.get(i).syncStart();
  }

  int replica = 3;
  int partition = 30;
  for (String stateModel : TestStateModels) {
    String db = "Test-DB-" + stateModel;
    createResourceWithDelayedRebalance(CLUSTER_NAME, db, stateModel, partition, replica, replica,
        0);
  }
  Assert.assertTrue(_clusterVerifier.verifyByPolling(50000L, 100L));

  _startListen = true;
  DelayedTransition.setDelay(5);

  // add the other half of nodes.
  for (; i < NUM_NODE; i++) {
    _participants.get(i).syncStart();
  }
  Assert.assertTrue(_clusterVerifier.verify(70000L));
  Assert.assertTrue(_testSuccess);

  if (manager.isConnected()) {
    manager.disconnect();
  }
  System.out.println("END testDelayedRebalancer at " + new Date(System.currentTimeMillis()));
}
 
Example 7
Source File: MockSpectatorProcess.java    From helix with Apache License 2.0 5 votes vote down vote up
public void start() {

    try {
      HelixManager manager =
          HelixManagerFactory.getZKHelixManager(clusterName, null, InstanceType.SPECTATOR,
              zkConnectString);

      manager.connect();
      manager.addExternalViewChangeListener(_routingTableProvider);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
Example 8
Source File: FileStoreStateModel.java    From helix with Apache License 2.0 5 votes vote down vote up
public FileStoreStateModel(HelixManager manager, String resource, String partition) {
  String clusterName = manager.getClusterName();
  String instanceName = manager.getInstanceName();
  instanceConfig = manager.getClusterManagmentTool().getInstanceConfig(clusterName, instanceName);
  replicator = new Replicator(instanceConfig, resource, partition);
  try {
    manager.addExternalViewChangeListener(replicator);
  } catch (Exception e) {
    e.printStackTrace();
  }
  _partition = partition;
  _serverId = instanceName;
}
 
Example 9
Source File: TestZeroReplicaAvoidance.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test
public void testWagedRebalancer() throws Exception {
  System.out.println("START testWagedRebalancer at " + new Date(System.currentTimeMillis()));
  HelixManager manager =
      HelixManagerFactory.getZKHelixManager(CLUSTER_NAME, null, InstanceType.SPECTATOR, ZK_ADDR);
  manager.connect();
  manager.addExternalViewChangeListener(this);
  manager.addIdealStateChangeListener(this);
  enablePersistBestPossibleAssignment(_gZkClient, CLUSTER_NAME, true);

  // Start half number of nodes.
  int i = 0;
  for (; i < NUM_NODE / 2; i++) {
    _participants.get(i).syncStart();
  }

  int replica = 3;
  int partition = 30;
  for (String stateModel : TestStateModels) {
    String db = "Test-DB-" + stateModel;
    createResourceWithWagedRebalance(CLUSTER_NAME, db, stateModel, partition, replica, replica);
  }
  // TODO remove this sleep after fix https://github.com/apache/helix/issues/526
  Thread.sleep(1000);
  Assert.assertTrue(_clusterVerifier.verifyByPolling(50000L, 100L));

  _startListen = true;
  DelayedTransition.setDelay(5);

  // add the other half of nodes.
  for (; i < NUM_NODE; i++) {
    _participants.get(i).syncStart();
  }
  Assert.assertTrue(_clusterVerifier.verify(70000L));
  Assert.assertTrue(_testSuccess);

  if (manager.isConnected()) {
    manager.disconnect();
  }
  System.out.println("END testWagedRebalancer at " + new Date(System.currentTimeMillis()));
}