org.apache.helix.PropertyKey.Builder Java Examples

The following examples show how to use org.apache.helix.PropertyKey.Builder. 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: ClusterSetup.java    From helix with Apache License 2.0 6 votes vote down vote up
@Deprecated
public ClusterSetup(String zkServerAddress) {
  // If the multi ZK config is enabled, use FederatedZkClient on multi-realm mode
  if (Boolean.parseBoolean(System.getProperty(SystemPropertyKeys.MULTI_ZK_ENABLED))) {
    try {
      _zkClient = new FederatedZkClient(
          new RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder().build(),
          new RealmAwareZkClient.RealmAwareZkClientConfig()
              .setZkSerializer(new ZNRecordSerializer()));
    } catch (IOException | InvalidRoutingDataException | IllegalStateException e) {
      throw new HelixException("Failed to create ConfigAccessor!", e);
    }
  } else {
    _zkClient = SharedZkClientFactory.getInstance()
        .buildZkClient(new HelixZkClient.ZkConnectionConfig(zkServerAddress));
    _zkClient.setZkSerializer(new ZNRecordSerializer());
  }

  _admin = new ZKHelixAdmin(_zkClient);
  _usesExternalZkClient = false;
}
 
Example #2
Source File: ZKHelixDataAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public boolean createStateModelDef(StateModelDefinition stateModelDef) {
  String path = PropertyPathBuilder.stateModelDef(_clusterName, stateModelDef.getId());
  HelixProperty property =
      getProperty(new PropertyKey.Builder(_clusterName).stateModelDef(stateModelDef.getId()));

  // Set new StateModelDefinition if it is different from old one.
  if (property != null) {
    // StateModelDefinition need to be updated
    if (!new StateModelDefinition(property.getRecord()).equals(stateModelDef)) {
      return stateModelDef.isValid() && _baseDataAccessor
          .set(path, stateModelDef.getRecord(), AccessOption.PERSISTENT);
    }
  } else {
    // StateModeDefinition does not exist
    return stateModelDef.isValid() && _baseDataAccessor
        .create(path, stateModelDef.getRecord(), AccessOption.PERSISTENT);
  }
  // StateModelDefinition exists but not need to be updated
  return true;
}
 
Example #3
Source File: DistributedLeaderElection.java    From helix with Apache License 2.0 6 votes vote down vote up
private void updateHistory(HelixManager manager) {
  HelixDataAccessor accessor = manager.getHelixDataAccessor();
  Builder keyBuilder = accessor.keyBuilder();
  final String clusterName = manager.getClusterName();
  final String instanceName = manager.getInstanceName();
  final String version = manager.getVersion();

  // Record a MaintenanceSignal history
  if (!accessor.getBaseDataAccessor().update(keyBuilder.controllerLeaderHistory().getPath(),
      oldRecord -> {
        if (oldRecord == null) {
          oldRecord = new ZNRecord(PropertyType.HISTORY.toString());
        }
        return new ControllerHistory(oldRecord).updateHistory(clusterName, instanceName,
            version);
      }, AccessOption.PERSISTENT)) {
    LOG.error("Failed to persist leader history to ZK!");
  }
}
 
Example #4
Source File: GenericLeaderStandbyModel.java    From helix with Apache License 2.0 6 votes vote down vote up
@Transition(to = "STANDBY", from = "LEADER")
public void onBecomeStandbyFromLeader(Message message, NotificationContext context) {
  LOG.info("Become STANDBY from LEADER");
  HelixManager manager = context.getManager();
  if (manager == null) {
    throw new IllegalArgumentException("Require HelixManager in notification conext");
  }

  Builder keyBuilder = new Builder(manager.getClusterName());
  for (ChangeType notificationType : _notificationTypes) {
    if (notificationType == ChangeType.LIVE_INSTANCE) {
      manager.removeListener(keyBuilder.liveInstances(), _particHolder);
    } else if (notificationType == ChangeType.CONFIG) {
      manager.removeListener(keyBuilder.instanceConfigs(), _particHolder);
    } else if (notificationType == ChangeType.EXTERNAL_VIEW) {
      manager.removeListener(keyBuilder.externalViews(), _particHolder);
    } else {
      LOG.error("Unsupport notificationType:" + notificationType.toString());
    }
  }
}
 
Example #5
Source File: ErrorResource.java    From helix with Apache License 2.0 6 votes vote down vote up
StringRepresentation getInstanceErrorsRepresentation(String clusterName, String instanceName,
    String resourceGroup) throws JsonGenerationException, JsonMappingException, IOException {
  ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
  String instanceSessionId =
      ClusterRepresentationUtil.getInstanceSessionId(zkClient, clusterName, instanceName);
  Builder keyBuilder = new PropertyKey.Builder(clusterName);
  String message =
      ClusterRepresentationUtil.getInstancePropertiesAsString(zkClient, clusterName,
          keyBuilder.stateTransitionErrors(instanceName, instanceSessionId, resourceGroup),
          // instanceSessionId
          // + "__"
          // + resourceGroup,
          MediaType.APPLICATION_JSON);
  StringRepresentation representation =
      new StringRepresentation(message, MediaType.APPLICATION_JSON);
  return representation;
}
 
Example #6
Source File: HelixTaskExecutor.java    From helix with Apache License 2.0 6 votes vote down vote up
private void syncSessionToController(HelixManager manager) {
  if (_lastSessionSyncTime == null ||
          System.currentTimeMillis() - _lastSessionSyncTime > SESSION_SYNC_INTERVAL) { // > delay since last sync
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    PropertyKey key = new Builder(manager.getClusterName()).controllerMessage(SESSION_SYNC);
    if (accessor.getProperty(key) == null) {
      LOG.info(String.format("Participant %s syncs session with controller", manager.getInstanceName()));
      Message msg = new Message(MessageType.PARTICIPANT_SESSION_CHANGE, SESSION_SYNC);
      msg.setSrcName(manager.getInstanceName());
      msg.setTgtSessionId("*");
      msg.setMsgState(MessageState.NEW);
      msg.setMsgId(SESSION_SYNC);

      Criteria cr = new Criteria();
      cr.setRecipientInstanceType(InstanceType.CONTROLLER);
      cr.setSessionSpecific(false);

      manager.getMessagingService().send(cr, msg);
      _lastSessionSyncTime = System.currentTimeMillis();
    }
  }
}
 
Example #7
Source File: DefaultMessagingService.java    From helix with Apache License 2.0 6 votes vote down vote up
private void sendNopMessageInternal() {
  try {
    Message nopMsg = new Message(MessageType.NO_OP, UUID.randomUUID().toString());
    nopMsg.setSrcName(_manager.getInstanceName());

    HelixDataAccessor accessor = _manager.getHelixDataAccessor();
    Builder keyBuilder = accessor.keyBuilder();

    if (_manager.getInstanceType() == InstanceType.CONTROLLER
        || _manager.getInstanceType() == InstanceType.CONTROLLER_PARTICIPANT) {
      nopMsg.setTgtName(InstanceType.CONTROLLER.name());
      accessor.setProperty(keyBuilder.controllerMessage(nopMsg.getId()), nopMsg);
    }

    if (_manager.getInstanceType() == InstanceType.PARTICIPANT
        || _manager.getInstanceType() == InstanceType.CONTROLLER_PARTICIPANT) {
      nopMsg.setTgtName(_manager.getInstanceName());
      accessor.setProperty(keyBuilder.message(nopMsg.getTgtName(), nopMsg.getId()), nopMsg);
    }
  } catch (Exception e) {
    _logger.error(e.toString());
  }
}
 
Example #8
Source File: CurrentStateResource.java    From helix with Apache License 2.0 6 votes vote down vote up
StringRepresentation getInstanceCurrentStateRepresentation(String clusterName,
    String instanceName, String resourceGroup) throws JsonGenerationException,
    JsonMappingException, IOException {
  ZkClient zkClient = (ZkClient) getRequest().getAttributes().get(RestAdminApplication.ZKCLIENT);
  String instanceSessionId =
      ClusterRepresentationUtil.getInstanceSessionId(zkClient, clusterName, instanceName);
  Builder keyBuilder = new PropertyKey.Builder(clusterName);

  String message =
      ClusterRepresentationUtil.getInstancePropertyAsString(zkClient, clusterName,
          keyBuilder.currentState(instanceName, instanceSessionId, resourceGroup),
          MediaType.APPLICATION_JSON);
  StringRepresentation representation =
      new StringRepresentation(message, MediaType.APPLICATION_JSON);
  return representation;
}
 
Example #9
Source File: TestAutoRebalance.java    From helix with Apache License 2.0 5 votes vote down vote up
@Test()
public void testAutoRebalance() throws Exception {
  // kill 1 node
  _participants[0].syncStop();

  boolean result = ClusterStateVerifier
      .verifyByZkCallback(new ExternalViewBalancedVerifier(_gZkClient, CLUSTER_NAME, TEST_DB));
  Assert.assertTrue(result);

  // add 2 nodes
  for (int i = 0; i < 2; i++) {
    String storageNodeName = PARTICIPANT_PREFIX + "_" + (1000 + i);
    _gSetupTool.addInstanceToCluster(CLUSTER_NAME, storageNodeName);

    MockParticipantManager participant =
        new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, storageNodeName.replace(':', '_'));
    _extraParticipants.add(participant);
    participant.syncStart();
  }
  Thread.sleep(100);
  result = ClusterStateVerifier.verifyByPolling(
      new ExternalViewBalancedVerifier(_gZkClient, CLUSTER_NAME, TEST_DB), 10000, 100);
  Assert.assertTrue(result);

  result = ClusterStateVerifier
      .verifyByZkCallback(new ExternalViewBalancedVerifier(_gZkClient, CLUSTER_NAME, db2));
  Assert.assertTrue(result);
  HelixDataAccessor accessor =
      new ZKHelixDataAccessor(CLUSTER_NAME, new ZkBaseDataAccessor<>(_gZkClient));
  Builder keyBuilder = accessor.keyBuilder();
  ExternalView ev = accessor.getProperty(keyBuilder.externalView(db2));
  Set<String> instancesSet = new HashSet<>();
  for (String partitionName : ev.getRecord().getMapFields().keySet()) {
    Map<String, String> assignmentMap = ev.getRecord().getMapField(partitionName);
    instancesSet.addAll(assignmentMap.keySet());
  }
  Assert.assertEquals(instancesSet.size(), 2);
}
 
Example #10
Source File: TestResourceComputationStage.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Case where we have one resource in IdealState
 * @throws Exception
 */
@Test
public void testSimple() throws Exception {
  int nodes = 5;
  List<String> instances = new ArrayList<String>();
  for (int i = 0; i < nodes; i++) {
    instances.add("localhost_" + i);
  }
  int partitions = 10;
  int replicas = 1;
  String resourceName = "testResource";
  ZNRecord record =
      DefaultIdealStateCalculator.calculateIdealState(instances, partitions, replicas,
          resourceName, "MASTER", "SLAVE");
  IdealState idealState = new IdealState(record);
  idealState.setStateModelDefRef("MasterSlave");

  HelixDataAccessor accessor = manager.getHelixDataAccessor();
  Builder keyBuilder = accessor.keyBuilder();
  accessor.setProperty(keyBuilder.idealStates(resourceName), idealState);
  event.addAttribute(AttributeName.ControllerDataProvider.name(),
      new ResourceControllerDataProvider());
  ResourceComputationStage stage = new ResourceComputationStage();
  runStage(event, new ReadClusterDataStage());
  runStage(event, stage);

  Map<String, Resource> resource = event.getAttribute(AttributeName.RESOURCES_TO_REBALANCE.name());
  AssertJUnit.assertEquals(1, resource.size());

  AssertJUnit.assertEquals(resource.keySet().iterator().next(), resourceName);
  AssertJUnit.assertEquals(resource.values().iterator().next().getResourceName(), resourceName);
  AssertJUnit.assertEquals(resource.values().iterator().next().getStateModelDefRef(),
      idealState.getStateModelDefRef());
  AssertJUnit
      .assertEquals(resource.values().iterator().next().getPartitions().size(), partitions);
}
 
Example #11
Source File: ZkTestBase.java    From helix with Apache License 2.0 5 votes vote down vote up
protected List<IdealState> setupIdealState(String clusterName, int[] nodes, String[] resources,
    int partitions, int replicas) {
  ZKHelixDataAccessor accessor =
      new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<>(_gZkClient));
  Builder keyBuilder = accessor.keyBuilder();

  List<IdealState> idealStates = new ArrayList<>();
  List<String> instances = new ArrayList<>();
  for (int i : nodes) {
    instances.add("localhost_" + i);
  }

  for (String resourceName : resources) {
    IdealState idealState = new IdealState(resourceName);
    for (int p = 0; p < partitions; p++) {
      List<String> value = new ArrayList<>();
      for (int r = 0; r < replicas; r++) {
        int n = nodes[(p + r) % nodes.length];
        value.add("localhost_" + n);
      }
      idealState.getRecord().setListField(resourceName + "_" + p, value);
    }

    idealState.setReplicas(Integer.toString(replicas));
    idealState.setStateModelDefRef("MasterSlave");
    idealState.setRebalanceMode(IdealState.RebalanceMode.SEMI_AUTO);
    idealState.setNumPartitions(partitions);
    idealStates.add(idealState);

    // System.out.println(idealState);
    accessor.setProperty(keyBuilder.idealStates(resourceName), idealState);
  }
  return idealStates;
}
 
Example #12
Source File: ZKHelixManager.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void addConfigChangeListener(ScopedConfigChangeListener listener, ConfigScopeProperty scope)
    throws Exception {
  Builder keyBuilder = new Builder(_clusterName);

  PropertyKey propertyKey = null;
  switch (scope) {
  case CLUSTER:
    propertyKey = keyBuilder.clusterConfigs();
    break;
  case PARTICIPANT:
    propertyKey = keyBuilder.instanceConfigs();
    break;
  case RESOURCE:
    propertyKey = keyBuilder.resourceConfigs();
    break;
  default:
    break;
  }

  if (propertyKey != null) {
    addListener(listener, propertyKey, ChangeType.CONFIG, new EventType[] {
      EventType.NodeChildrenChanged
    });
  } else {
    LOG.error("Can't add listener to config scope: " + scope);
  }
}
 
Example #13
Source File: ClusterRepresentationUtil.java    From helix with Apache License 2.0 5 votes vote down vote up
public static String getInstanceSessionId(ZkClient zkClient, String clusterName,
    String instanceName) {
  ZKHelixDataAccessor accessor =
      new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
  Builder keyBuilder = accessor.keyBuilder();

  ZNRecord liveInstance = accessor.getProperty(keyBuilder.liveInstance(instanceName)).getRecord();

  return liveInstance.getSimpleField(LiveInstanceProperty.SESSION_ID.toString());
}
 
Example #14
Source File: ZkTestBase.java    From helix with Apache License 2.0 5 votes vote down vote up
public void verifyEnabled(HelixZkClient zkClient, String clusterName, String instance,
    boolean wantEnabled) {
  ZKHelixDataAccessor accessor =
      new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
  Builder keyBuilder = accessor.keyBuilder();

  InstanceConfig config = accessor.getProperty(keyBuilder.instanceConfig(instance));
  AssertJUnit.assertEquals(wantEnabled, config.getInstanceEnabled());
}
 
Example #15
Source File: TestCompatibilityCheckStage.java    From helix with Apache License 2.0 5 votes vote down vote up
private void prepare(String controllerVersion, String participantVersion,
    String minSupportedParticipantVersion) {
  List<String> instances =
      Arrays.asList("localhost_0", "localhost_1", "localhost_2", "localhost_3", "localhost_4");
  int partitions = 10;
  int replicas = 1;

  // set ideal state
  String resourceName = "testResource";
  ZNRecord record =
      DefaultIdealStateCalculator.calculateIdealState(instances, partitions, replicas,
          resourceName, "MASTER", "SLAVE");
  IdealState idealState = new IdealState(record);
  idealState.setStateModelDefRef("MasterSlave");

  Builder keyBuilder = accessor.keyBuilder();
  accessor.setProperty(keyBuilder.idealStates(resourceName), idealState);

  // set live instances
  record = new ZNRecord("localhost_0");
  if (participantVersion != null) {
    record.setSimpleField(LiveInstanceProperty.HELIX_VERSION.toString(), participantVersion);
  }
  LiveInstance liveInstance = new LiveInstance(record);
  liveInstance.setSessionId("session_0");
  accessor.setProperty(keyBuilder.liveInstance("localhost_0"), liveInstance);

  if (controllerVersion != null) {
    ((MockManager) manager).setVersion(controllerVersion);
  }

  if (minSupportedParticipantVersion != null) {
    manager.getProperties().getProperties()
        .put("minimum_supported_version.participant", minSupportedParticipantVersion);
  }
  event.addAttribute(AttributeName.helixmanager.name(), manager);
  event.addAttribute(AttributeName.ControllerDataProvider.name(),
      new ResourceControllerDataProvider());
  runStage(event, new ReadClusterDataStage());
}
 
Example #16
Source File: ZKHelixManager.java    From helix with Apache License 2.0 5 votes vote down vote up
private RealmAwareZkClient createSingleRealmZkClient() {
  final String shardingKey = buildShardingKey();
  PathBasedZkSerializer zkSerializer =
      ChainedPathZkSerializer.builder(new ZNRecordSerializer()).build();

  RealmAwareZkClient.RealmAwareZkConnectionConfig connectionConfig =
      new RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder()
          .setRealmMode(RealmAwareZkClient.RealmMode.SINGLE_REALM)
          .setZkRealmShardingKey(shardingKey)
          .setSessionTimeout(_sessionTimeout).build();

  RealmAwareZkClient.RealmAwareZkClientConfig clientConfig =
      new RealmAwareZkClient.RealmAwareZkClientConfig();

  clientConfig.setZkSerializer(zkSerializer)
      .setConnectInitTimeout(_connectionInitTimeout)
      .setMonitorType(_instanceType.name())
      .setMonitorKey(_clusterName)
      .setMonitorInstanceName(_instanceName)
      .setMonitorRootPathOnly(isMonitorRootPathOnly());

  if (_instanceType == InstanceType.ADMINISTRATOR) {
    return resolveZkClient(SharedZkClientFactory.getInstance(), connectionConfig,
        clientConfig);
  }

  return resolveZkClient(DedicatedZkClientFactory.getInstance(), connectionConfig,
      clientConfig);
}
 
Example #17
Source File: ZKHelixDataAccessor.java    From helix with Apache License 2.0 5 votes vote down vote up
public ZKHelixDataAccessor(String clusterName, InstanceType instanceType,
    BaseDataAccessor<ZNRecord> baseDataAccessor) {
  _clusterName = clusterName;
  _instanceType = instanceType;
  _baseDataAccessor = baseDataAccessor;
  _propertyKeyBuilder = new PropertyKey.Builder(_clusterName);
}
 
Example #18
Source File: TestHelixTaskHandler.java    From helix with Apache License 2.0 5 votes vote down vote up
@Test()
public void testInvocation() throws Exception {
  HelixTaskExecutor executor = new HelixTaskExecutor();
  System.out.println("START TestCMTaskHandler.testInvocation()");
  Message message = new Message(MessageType.STATE_TRANSITION, "Some unique id");

  message.setSrcName("cm-instance-0");
  message.setTgtSessionId("1234");
  message.setFromState("Offline");
  message.setToState("Slave");
  message.setPartitionName("TestDB_0");
  message.setMsgId("Some unique message id");
  message.setResourceName("TestDB");
  message.setTgtName("localhost");
  message.setStateModelDef("MasterSlave");
  message.setStateModelFactoryName(HelixConstants.DEFAULT_STATE_MODEL_FACTORY);
  MockMasterSlaveStateModel stateModel = new MockMasterSlaveStateModel();
  NotificationContext context;
  MockManager manager = new MockManager("clusterName");
  HelixDataAccessor accessor = manager.getHelixDataAccessor();
  StateModelDefinition stateModelDef =
      new StateModelDefinition(StateModelConfigGenerator.generateConfigForMasterSlave());
  Builder keyBuilder = accessor.keyBuilder();
  accessor.setProperty(keyBuilder.stateModelDef("MasterSlave"), stateModelDef);

  context = new NotificationContext(manager);
  CurrentState currentStateDelta = new CurrentState("TestDB");
  currentStateDelta.setState("TestDB_0", "OFFLINE");

  HelixStateTransitionHandler stHandler =
      new HelixStateTransitionHandler(null, stateModel, message, context, currentStateDelta);
  HelixTask handler;
  handler = new HelixTask(message, context, stHandler, executor);
  handler.call();
  AssertJUnit.assertTrue(stateModel.stateModelInvoked);
  System.out.println("END TestCMTaskHandler.testInvocation() at "
      + new Date(System.currentTimeMillis()));
}
 
Example #19
Source File: TestCustomizedStateConfig.java    From helix with Apache License 2.0 5 votes vote down vote up
@Test(dependsOnMethods = "testCustomizedStateConfigNull")
public void testCustomizedStateConfig() {
  String className = getShortClassName();
  String clusterName = "CLUSTER_" + className;
  TestHelper.setupEmptyCluster(_gZkClient, clusterName);

  // Create dummy CustomizedStateConfig object
  CustomizedStateConfig.Builder customizedStateConfigBuilder =
      new CustomizedStateConfig.Builder();
  List<String> aggregationEnabledTypes = new ArrayList<String>();
  aggregationEnabledTypes.add("mockType1");
  aggregationEnabledTypes.add("mockType2");
  customizedStateConfigBuilder.setAggregationEnabledTypes(aggregationEnabledTypes);
  CustomizedStateConfig customizedStateConfig =
      customizedStateConfigBuilder.build();

  // Write the CustomizedStateConfig to Zookeeper
  ZKHelixDataAccessor accessor =
      new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(ZK_ADDR));
  Builder keyBuilder = accessor.keyBuilder();
  accessor.setProperty(keyBuilder.customizedStateConfig(),
      customizedStateConfig);

  // Read CustomizedStateConfig from Zookeeper and check the content
  ConfigAccessor _configAccessor = new ConfigAccessor(ZK_ADDR);
  CustomizedStateConfig customizedStateConfigFromZk =
      _configAccessor.getCustomizedStateConfig(clusterName);
  Assert.assertEquals(customizedStateConfigFromZk.getAggregationEnabledTypes().size(),
      2);
  Assert.assertEquals(aggregationEnabledTypes.get(0), "mockType1");
  Assert.assertEquals(aggregationEnabledTypes.get(1), "mockType2");
}
 
Example #20
Source File: DistributedLeaderElection.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * @return true if the current manager created a new controller node successfully.
 */
private boolean tryCreateController(HelixManager manager) {
  HelixDataAccessor accessor = manager.getHelixDataAccessor();
  Builder keyBuilder = accessor.keyBuilder();

  LiveInstance newLeader = new LiveInstance(manager.getInstanceName());
  newLeader.setLiveInstance(ManagementFactory.getRuntimeMXBean().getName());
  newLeader.setSessionId(manager.getSessionId());
  newLeader.setHelixVersion(manager.getVersion());
  try {
    if (accessor.createControllerLeader(newLeader)) {
      updateHistory(manager);
      return true;
    } else {
      LOG.info(
          "Unable to become leader probably because some other controller became the leader.");
    }
  } catch (Exception e) {
    LOG.error(
        "Exception when trying to updating leader record in cluster:" + manager.getClusterName()
            + ". Need to check again whether leader node has been created or not.", e);
  }

  LiveInstance currentLeader = accessor.getProperty(keyBuilder.controllerLeader());
  if (currentLeader != null) {
    String currentSession = currentLeader.getEphemeralOwner();
    LOG.info("Leader exists for cluster: " + manager.getClusterName() + ", currentLeader: "
        + currentLeader.getInstanceName() + ", leaderSessionId: " + currentSession);
    if (currentSession != null && currentSession.equals(newLeader.getEphemeralOwner())) {
      return true;
    } else {
      LOG.warn("The existing leader has a different session. Expected session Id: " + newLeader
          .getEphemeralOwner());
    }
  }
  return false;
}
 
Example #21
Source File: StateModelResource.java    From helix with Apache License 2.0 5 votes vote down vote up
StringRepresentation getStateModelRepresentation(String clusterName, String modelName)
    throws JsonGenerationException, JsonMappingException, IOException {
  Builder keyBuilder = new PropertyKey.Builder(clusterName);
  ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);

  String message =
      ClusterRepresentationUtil.getClusterPropertyAsString(zkClient, clusterName,
          keyBuilder.stateModelDef(modelName), MediaType.APPLICATION_JSON);

  StringRepresentation representation =
      new StringRepresentation(message, MediaType.APPLICATION_JSON);

  return representation;
}
 
Example #22
Source File: GenericHelixController.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
  try {
    if (_shouldRefreshCacheOption.orElse(
        _clusterEventType.equals(ClusterEventType.PeriodicalRebalance) || _clusterEventType
            .equals(ClusterEventType.OnDemandRebalance))) {
      requestDataProvidersFullRefresh();

      HelixDataAccessor accessor = _manager.getHelixDataAccessor();
      PropertyKey.Builder keyBuilder = accessor.keyBuilder();
      List<LiveInstance> liveInstances =
          accessor.getChildValues(keyBuilder.liveInstances(), true);

      if (liveInstances != null && !liveInstances.isEmpty()) {
        NotificationContext changeContext = new NotificationContext(_manager);
        changeContext.setType(NotificationContext.Type.CALLBACK);
        synchronized (_manager) {
          checkLiveInstancesObservation(liveInstances, changeContext);
        }
      }
    }
    forceRebalance(_manager, _clusterEventType);
  } catch (Throwable ex) {
    logger.error("Time task failed. Rebalance task type: " + _clusterEventType + ", cluster: "
        + _clusterName, ex);
  }
}
 
Example #23
Source File: ParticipantHealthReportCollectorImpl.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void reportHealthReportMessage(ZNRecord healthCheckInfoUpdate) {
  HelixDataAccessor accessor = _helixManager.getHelixDataAccessor();
  Builder keyBuilder = accessor.keyBuilder();
  if(!accessor.setProperty(keyBuilder.healthReport(_instanceName, healthCheckInfoUpdate.getId()),
      new HealthStat(healthCheckInfoUpdate))) {
    LOG.error("Failed to persist health report to zk!");
  }
}
 
Example #24
Source File: ControllerResource.java    From helix with Apache License 2.0 5 votes vote down vote up
StringRepresentation getControllerRepresentation(String clusterName)
    throws JsonGenerationException, JsonMappingException, IOException {
  Builder keyBuilder = new PropertyKey.Builder(clusterName);
  ZkClient zkClient =
      ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);

  ZKHelixDataAccessor accessor =
      new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));

  ZNRecord record = null;
  LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
  if (leader != null) {
    record = leader.getRecord();
  } else {
    record = new ZNRecord("");
    DateFormat formatter = new SimpleDateFormat("yyyyMMdd-HHmmss.SSSSSS");
    String time = formatter.format(new Date());
    Map<String, String> contentMap = new TreeMap<String, String>();
    contentMap.put("AdditionalInfo", "No leader exists");
    record.setMapField(Level.HELIX_INFO + "-" + time, contentMap);
  }

  boolean paused = (accessor.getProperty(keyBuilder.pause()) == null ? false : true);
  record.setSimpleField(PropertyType.PAUSE.toString(), "" + paused);

  String retVal = ClusterRepresentationUtil.ZNRecordToJson(record);
  StringRepresentation representation =
      new StringRepresentation(retVal, MediaType.APPLICATION_JSON);

  return representation;
}
 
Example #25
Source File: TestBestPossibleCalcStageCompatibility.java    From helix with Apache License 2.0 5 votes vote down vote up
protected List<IdealState> setupIdealStateDeprecated(int nodes, String[] resources,
    int partitions, int replicas, IdealStateModeProperty mode) {
  List<IdealState> idealStates = new ArrayList<IdealState>();
  List<String> instances = new ArrayList<String>();
  for (int i = 0; i < nodes; i++) {
    instances.add("localhost_" + i);
  }

  for (int i = 0; i < resources.length; i++) {
    String resourceName = resources[i];
    ZNRecord record = new ZNRecord(resourceName);
    for (int p = 0; p < partitions; p++) {
      List<String> value = new ArrayList<String>();
      for (int r = 0; r < replicas; r++) {
        value.add("localhost_" + (p + r + 1) % nodes);
      }
      record.setListField(resourceName + "_" + p, value);
    }
    IdealState idealState = new IdealState(record);
    idealState.setStateModelDefRef("MasterSlave");
    idealState.setIdealStateMode(mode.toString());
    idealState.setNumPartitions(partitions);
    idealStates.add(idealState);

    // System.out.println(idealState);

    Builder keyBuilder = accessor.keyBuilder();

    accessor.setProperty(keyBuilder.idealStates(resourceName), idealState);
  }
  return idealStates;
}
 
Example #26
Source File: CustomCodeInvoker.java    From helix with Apache License 2.0 5 votes vote down vote up
private void callParticipantCode(NotificationContext context) {
  // since ZkClient.unsubscribe() does not immediately remove listeners
  // from zk, it is possible that two listeners exist when leadership transfers
  // therefore, double check to make sure only one participant invokes the code
  if (context.getType() == Type.CALLBACK) {
    HelixManager manager = context.getManager();
    // DataAccessor accessor = manager.getDataAccessor();
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    Builder keyBuilder = accessor.keyBuilder();

    String instance = manager.getInstanceName();
    String sessionId = manager.getSessionId();

    // get resource name from partition key: "PARTICIPANT_LEADER_XXX_0"
    String resourceName = _partitionKey.substring(0, _partitionKey.lastIndexOf('_'));

    CurrentState curState =
        accessor.getProperty(keyBuilder.currentState(instance, sessionId, resourceName));
    if (curState == null) {
      return;
    }

    String state = curState.getState(_partitionKey);
    if (state == null || !state.equalsIgnoreCase("LEADER")) {
      return;
    }
  }

  try {
    _callback.onCallback(context);
  } catch (Exception e) {
    LOG.error("Error invoking callback:" + _callback, e);
  }
}
 
Example #27
Source File: Message.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Get the {@link PropertyKey} for this message
 * @param keyBuilder PropertyKey Builder
 * @param instanceName target instance
 * @return message PropertyKey
 */
public PropertyKey getKey(Builder keyBuilder, String instanceName) {
  if (isControlerMsg()) {
    return keyBuilder.controllerMessage(getId());
  } else {
    return keyBuilder.message(instanceName, getId());
  }
}
 
Example #28
Source File: HelixTask.java    From helix with Apache License 2.0 5 votes vote down vote up
private void forwardRelayMessages(HelixDataAccessor accessor, Message message,
    long taskCompletionTime) {
  if (message.hasRelayMessages()) {
    Map<String, Message> relayMessages = message.getRelayMessages();
    Builder keyBuilder = accessor.keyBuilder();

    // Ignore all relay messages if participant's session has changed.
    if (!_manager.getSessionId().equals(message.getTgtSessionId())) {
      logger.info(
          "Session id has been changed, ignore all relay messages attached with " + message
              .getId());
      return;
    }

    for (String instance : relayMessages.keySet()) {
      Message msg = relayMessages.get(instance);
      if (msg.getMsgSubType().equals(MessageType.RELAYED_MESSAGE.name())) {
        msg.setRelayTime(taskCompletionTime);
        if (msg.isExpired()) {
          logger.info(
              "Relay message expired, ignore " + msg.getId() + " to instance " + instance);
          continue;
        }
        PropertyKey msgKey = keyBuilder.message(instance, msg.getId());
        boolean success = accessor.getBaseDataAccessor()
            .create(msgKey.getPath(), msg.getRecord(), AccessOption.PERSISTENT);
        if (!success) {
          logger.warn("Failed to send relay message " + msg.getId() + " to " + instance);
        } else {
          logger.info("Send relay message " + msg.getId() + " to " + instance);
        }
      }
    }
  }
}
 
Example #29
Source File: HelixTask.java    From helix with Apache License 2.0 5 votes vote down vote up
private void sendReply(HelixDataAccessor replyDataAccessor, Message message,
    HelixTaskResult taskResult) {
  if (message.getCorrelationId() != null && !message.getMsgType()
      .equals(MessageType.TASK_REPLY.name())) {
    logger.info("Sending reply for message " + message.getCorrelationId());
    _statusUpdateUtil.logInfo(message, HelixTask.class, "Sending reply", _manager);

    taskResult.getTaskResultMap().put("SUCCESS", "" + taskResult.isSuccess());
    taskResult.getTaskResultMap().put("INTERRUPTED", "" + taskResult.isInterrupted());
    if (!taskResult.isSuccess()) {
      taskResult.getTaskResultMap().put("ERRORINFO", taskResult.getMessage());
    }
    Message replyMessage = Message
        .createReplyMessage(message, _manager.getInstanceName(), taskResult.getTaskResultMap());
    replyMessage.setSrcInstanceType(_manager.getInstanceType());

    Builder keyBuilder = replyDataAccessor.keyBuilder();
    if (message.getSrcInstanceType() == InstanceType.PARTICIPANT) {
      replyDataAccessor
          .setProperty(keyBuilder.message(message.getMsgSrc(), replyMessage.getMsgId()),
              replyMessage);
    } else if (message.getSrcInstanceType() == InstanceType.CONTROLLER) {
      replyDataAccessor
          .setProperty(keyBuilder.controllerMessage(replyMessage.getMsgId()), replyMessage);
    }
    _statusUpdateUtil.logInfo(message, HelixTask.class, String
        .format("1 msg replied to %s in cluster %s.", replyMessage.getTgtName(),
            message.getSrcClusterName() == null ?
                _manager.getClusterName() :
                message.getSrcClusterName()), _manager);
  }
}
 
Example #30
Source File: IntegrationTest.java    From helix with Apache License 2.0 5 votes vote down vote up
private static void printStatus(final HelixManager manager) {
  System.out.println("CLUSTER STATUS");
  HelixDataAccessor helixDataAccessor = manager.getHelixDataAccessor();
  Builder keyBuilder = helixDataAccessor.keyBuilder();
  System.out.println("External View \n"
      + helixDataAccessor.getProperty(keyBuilder.externalView("repository")));
}