Java Code Examples for org.apache.helix.model.IdealState#setStateModelFactoryName()

The following examples show how to use org.apache.helix.model.IdealState#setStateModelFactoryName() . 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: ZKHelixAdmin.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public void addResource(String clusterName, String resourceName, int partitions,
    String stateModelRef, String rebalancerMode, String rebalanceStrategy, int bucketSize,
    int maxPartitionsPerInstance) {
  if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
    throw new HelixException("cluster " + clusterName + " is not setup yet");
  }

  IdealState idealState = new IdealState(resourceName);
  idealState.setNumPartitions(partitions);
  idealState.setStateModelDefRef(stateModelRef);
  RebalanceMode mode =
      idealState.rebalanceModeFromString(rebalancerMode, RebalanceMode.SEMI_AUTO);
  idealState.setRebalanceMode(mode);
  idealState.setRebalanceStrategy(rebalanceStrategy);
  idealState.setReplicas("" + 0);
  idealState.setStateModelFactoryName(HelixConstants.DEFAULT_STATE_MODEL_FACTORY);
  if (maxPartitionsPerInstance > 0 && maxPartitionsPerInstance < Integer.MAX_VALUE) {
    idealState.setMaxPartitionsPerInstance(maxPartitionsPerInstance);
  }
  if (bucketSize > 0) {
    idealState.setBucketSize(bucketSize);
  }
  addResource(clusterName, resourceName, idealState);
}
 
Example 2
Source File: ClusterSetup.java    From helix with Apache License 2.0 6 votes vote down vote up
/**
 * Create an IdealState for a resource that belongs to a resource group We use
 * "resourceGroupName$resourceInstanceTag" as the IdealState znode name to differetiate different
 * resources from the same resourceGroup.
 */
public IdealState createIdealStateForResourceGroup(String resourceGroupName,
    String resourceTag, int numPartition, int replica, String rebalanceMode, String stateModelDefName) {
  String idealStateId = genIdealStateNameWithResourceTag(resourceGroupName, resourceTag);
  IdealState idealState = new IdealState(idealStateId);
  idealState.setNumPartitions(numPartition);
  idealState.setStateModelDefRef(stateModelDefName);
  IdealState.RebalanceMode mode =
      idealState.rebalanceModeFromString(rebalanceMode, IdealState.RebalanceMode.SEMI_AUTO);
  idealState.setRebalanceMode(mode);
  idealState.setReplicas("" + replica);
  idealState.setStateModelFactoryName(HelixConstants.DEFAULT_STATE_MODEL_FACTORY);
  idealState.setResourceGroupName(resourceGroupName);
  idealState.setInstanceGroupTag(resourceTag);
  idealState.enableGroupRouting(true);

  return idealState;
}
 
Example 3
Source File: HelixCustomCodeRunner.java    From helix with Apache License 2.0 4 votes vote down vote up
/**
 * This method will be invoked when there is a change in any subscribed
 * notificationTypes
 * @throws Exception
 */
public void start() throws Exception {
  if (_callback == null || _notificationTypes == null || _notificationTypes.size() == 0
      || _resourceName == null) {
    throw new IllegalArgumentException("Require callback | notificationTypes | resourceName");
  }

  LOG.info("Register participantLeader on " + _notificationTypes + " using " + _resourceName);

  _stateModelFty = new GenericLeaderStandbyStateModelFactory(_callback, _notificationTypes);

  StateMachineEngine stateMach = _manager.getStateMachineEngine();
  stateMach.registerStateModelFactory(LEADER_STANDBY, _stateModelFty, _resourceName);
  HelixZkClient zkClient = null;
  try {
    // manually add ideal state for participant leader using LeaderStandby
    // model
    HelixZkClient.ZkClientConfig clientConfig = new HelixZkClient.ZkClientConfig();
    clientConfig.setZkSerializer(new ZNRecordSerializer());
    zkClient = SharedZkClientFactory.getInstance()
        .buildZkClient(new HelixZkClient.ZkConnectionConfig(_zkAddr), clientConfig);

    HelixDataAccessor accessor =
        new ZKHelixDataAccessor(_manager.getClusterName(), new ZkBaseDataAccessor<>(zkClient));
    Builder keyBuilder = accessor.keyBuilder();

    IdealState idealState = new IdealState(_resourceName);
    idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
    idealState.setReplicas(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString());
    idealState.setNumPartitions(1);
    idealState.setStateModelDefRef(LEADER_STANDBY);
    idealState.setStateModelFactoryName(_resourceName);
    List<String> prefList = new ArrayList<String>(
        Arrays.asList(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString()));
    idealState.getRecord().setListField(_resourceName + "_0", prefList);

    List<String> idealStates = accessor.getChildNames(keyBuilder.idealStates());
    while (idealStates == null || !idealStates.contains(_resourceName)) {
      accessor.setProperty(keyBuilder.idealStates(_resourceName), idealState);
      idealStates = accessor.getChildNames(keyBuilder.idealStates());
    }

    LOG.info(
        "Set idealState for participantLeader:" + _resourceName + ", idealState:" + idealState);
  } finally {
    if (zkClient != null && !zkClient.isClosed()) {
      zkClient.close();
    }
  }
}
 
Example 4
Source File: IdealStateBuilder.java    From helix with Apache License 2.0 4 votes vote down vote up
/**
 * @return
 */
public IdealState build() {
  IdealState idealstate = new IdealState(_record);
  idealstate.setNumPartitions(numPartitions);
  idealstate.setStateModelDefRef(stateModel);
  idealstate.setStateModelFactoryName(stateModelFactoryName);
  idealstate.setRebalanceMode(rebalancerMode);
  idealstate.setReplicas("" + numReplica);

  if (minActiveReplica >= 0) {
    idealstate.setMinActiveReplicas(minActiveReplica);
  }

  if (rebalancerClassName != null) {
    idealstate.setRebalancerClassName(rebalancerClassName);
  }

  if (rebalanceStrategy != null) {
    idealstate.setRebalanceStrategy(rebalanceStrategy);
  }

  if (maxPartitionsPerNode > 0) {
    idealstate.setMaxPartitionsPerInstance(maxPartitionsPerNode);
  }

  if (disableExternalView != null) {
    idealstate.setDisableExternalView(disableExternalView);
  }

  if (enableGroupRouting != null) {
    idealstate.enableGroupRouting(enableGroupRouting);
  }

  if (resourceGroupName != null) {
    idealstate.setResourceGroupName(resourceGroupName);
  }

  if (resourceType != null) {
    idealstate.setResourceType(resourceType);
  }

  if (rebalanceDelayInMs >= 0) {
    idealstate.setRebalanceDelay(rebalanceDelayInMs);
  }

  if (delayRebalanceEnabled != null) {
    idealstate.setDelayRebalanceEnabled(delayRebalanceEnabled);
  }

  if (!idealstate.isValid()) {
    throw new HelixException("invalid ideal-state: " + idealstate);
  }
  return idealstate;
}
 
Example 5
Source File: TestAddStateModelFactoryAfterConnect.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test
public void testBasic() throws Exception {
  // Logger.getRootLogger().setLevel(Level.INFO);
  String className = TestHelper.getTestClassName();
  String methodName = TestHelper.getTestMethodName();
  String clusterName = className + "_" + methodName;
  final int n = 5;

  System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));

  MockParticipantManager[] participants = new MockParticipantManager[n];

  TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
      "localhost", // participant name prefix
      "TestDB", // resource name prefix
      1, // resources
      10, // partitions per resource
      n, // number of nodes
      3, // replicas
      "MasterSlave", true); // do rebalance

  ClusterControllerManager controller =
      new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
  controller.syncStart();

  // start participants
  for (int i = 0; i < n; i++) {
    String instanceName = "localhost_" + (12918 + i);

    participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
    participants[i].syncStart();
  }

  boolean result =
      ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR,
          clusterName));
  Assert.assertTrue(result);

  // add a new idealState without registering message handling factory
  ClusterSetup setupTool = new ClusterSetup(ZK_ADDR);
  setupTool.addResourceToCluster(clusterName, "TestDB1", 16, "MasterSlave");

  ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
  ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
  Builder keyBuilder = accessor.keyBuilder();
  IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB1"));
  idealState.setStateModelFactoryName("TestDB1_Factory");
  accessor.setProperty(keyBuilder.idealStates("TestDB1"), idealState);
  setupTool.rebalanceStorageCluster(clusterName, "TestDB1", 3);

  // assert that we have received OFFLINE->SLAVE messages for all partitions
  int totalMsgs = 0;
  for (int retry = 0; retry < 5; retry++) {
    Thread.sleep(100);
    totalMsgs = 0;
    for (int i = 0; i < n; i++) {
      List<Message> msgs =
          accessor.getChildValues(keyBuilder.messages(participants[i].getInstanceName()), true);
      totalMsgs += msgs.size();
    }

    if (totalMsgs == 48) // partition# x replicas
      break;
  }

  Assert
      .assertEquals(
          totalMsgs,
          48,
          "Should accumulated 48 unprocessed messages (1 O->S per partition per replica) because TestDB1 is added without state-model-factory but was "
              + totalMsgs);

  // register "TestDB1_Factory" state model factory
  // Logger.getRootLogger().setLevel(Level.INFO);
  for (int i = 0; i < n; i++) {
    participants[i].getStateMachineEngine()
        .registerStateModelFactory("MasterSlave", new MockMSModelFactory(), "TestDB1_Factory");
  }

  result =
      ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR,
          clusterName));
  Assert.assertTrue(result);

  // clean up
  // wait for all zk callbacks done
  controller.syncStop();
  for (int i = 0; i < 5; i++) {
    participants[i].syncStop();
  }
  deleteCluster(clusterName);

  System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));

}