org.apache.helix.model.LiveInstance Java Examples

The following examples show how to use org.apache.helix.model.LiveInstance. 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: LeadControllerUtils.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
/**
 * Gets Helix leader in the cluster. Null if there is no leader.
 * @param helixManager helix manager
 * @return instance id of Helix cluster leader, e.g. localhost_9000.
 */
public static String getHelixClusterLeader(HelixManager helixManager) {
  HelixDataAccessor helixDataAccessor = helixManager.getHelixDataAccessor();
  PropertyKey propertyKey = helixDataAccessor.keyBuilder().controllerLeader();
  LiveInstance liveInstance = helixDataAccessor.getProperty(propertyKey);
  if (liveInstance == null) {
    LOGGER.warn("Helix leader ZNode is missing");
    return null;
  }
  String helixLeaderInstanceId = liveInstance.getInstanceName();
  String helixVersion = liveInstance.getHelixVersion();
  long modifiedTime = liveInstance.getModifiedTime();
  LOGGER.info("Getting Helix leader: {}, Helix version: {}, mtime: {}", helixLeaderInstanceId, helixVersion,
      modifiedTime);
  return helixLeaderInstanceId;
}
 
Example #2
Source File: TestInstanceCurrentState.java    From helix with Apache License 2.0 6 votes vote down vote up
@Test public void testAddedFieldsInCurrentState() {
  String instanceName = PARTICIPANT_PREFIX + "_" + _startPort;
  HelixDataAccessor accessor = _manager.getHelixDataAccessor();
  LiveInstance liveInstance =
      accessor.getProperty(accessor.keyBuilder().liveInstance(instanceName));
  CurrentState currentState = accessor.getProperty(accessor.keyBuilder()
      .currentState(instanceName, liveInstance.getEphemeralOwner(), WorkflowGenerator.DEFAULT_TGT_DB));
  // Test start time should happen after test start time
  Assert.assertTrue(
      currentState.getStartTime(WorkflowGenerator.DEFAULT_TGT_DB + "_0") >= _testStartTime);

  // Test end time is always larger than start time
  Assert.assertTrue(
      currentState.getEndTime(WorkflowGenerator.DEFAULT_TGT_DB + "_0") >= currentState
          .getStartTime(WorkflowGenerator.DEFAULT_TGT_DB + "_0"));

  // Final state is MASTER, so SLAVE will be the previous state
  Assert.assertEquals(currentState.getPreviousState(WorkflowGenerator.DEFAULT_TGT_DB + "_0"),
      "SLAVE");
}
 
Example #3
Source File: ClusterServiceImpl.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public ClusterInfo getClusterInfo(String clusterId) {
  ClusterInfo.Builder builder = new ClusterInfo.Builder(clusterId);
  PropertyKey.Builder keyBuilder = _dataAccessor.keyBuilder();
  LiveInstance controller =
      _dataAccessor.getProperty(_dataAccessor.keyBuilder().controllerLeader());
  if (controller != null) {
    builder.controller(controller.getInstanceName());
  } else {
    builder.controller("No Lead Controller");
  }

  return builder
      .paused(_dataAccessor.getBaseDataAccessor().exists(keyBuilder.pause().getPath(),
          AccessOption.PERSISTENT))
      .maintenance(_dataAccessor.getBaseDataAccessor().exists(keyBuilder.maintenance().getPath(),
          AccessOption.PERSISTENT))
      .idealStates(_dataAccessor.getChildNames(keyBuilder.idealStates()))
      .instances(_dataAccessor.getChildNames(keyBuilder.instances()))
      .liveInstances(_dataAccessor.getChildNames(keyBuilder.liveInstances())).build();
}
 
Example #4
Source File: TestThreadCountBasedTaskAssigner.java    From helix with Apache License 2.0 6 votes vote down vote up
private AssignableInstanceManager createAssignableInstanceManager(int count, int threadCount) {
  AssignableInstanceManager assignableInstanceManager = new AssignableInstanceManager();
  ClusterConfig clusterConfig = createClusterConfig(testQuotaTypes, testQuotaRatio, false);
  String instanceNameFormat = "instance-%s";
  Map<String, LiveInstance> liveInstanceMap = new HashMap<>();
  Map<String, InstanceConfig> instanceConfigMap = new HashMap<>();
  for (int i = 0; i < count; i++) {
    String instanceName = String.format(instanceNameFormat, i);
    liveInstanceMap.put(instanceName, createLiveInstance(
        new String[] { LiveInstance.InstanceResourceType.TASK_EXEC_THREAD.name() },
        new String[] { Integer.toString(threadCount) }, instanceName));
    instanceConfigMap.put(instanceName, new InstanceConfig(instanceName));
  }

  assignableInstanceManager
      .buildAssignableInstances(clusterConfig, new TaskDataCache(testClusterName),
          liveInstanceMap, instanceConfigMap);
  return assignableInstanceManager;
}
 
Example #5
Source File: SimpleClusterChangeHandler.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Update the liveness states of existing instances based on the input.
 * @param liveInstances the list of instances that are up.
 */
private void updateInstanceLiveness(List<LiveInstance> liveInstances) {
  synchronized (notificationLock) {
    Set<String> liveInstancesSet = new HashSet<>();
    for (LiveInstance liveInstance : liveInstances) {
      liveInstancesSet.add(liveInstance.getInstanceName());
    }
    for (String instanceName : instanceNameToAmbryDataNode.keySet()) {
      // Here we ignore live instance change it's about self instance. The reason is, during server's startup, current
      // node should be AVAILABLE but the list of live instances doesn't include current node since it hasn't joined yet.
      if (liveInstancesSet.contains(instanceName) || instanceName.equals(selfInstanceName)) {
        instanceNameToAmbryDataNode.get(instanceName).setState(HardwareState.AVAILABLE);
      } else {
        instanceNameToAmbryDataNode.get(instanceName).setState(HardwareState.UNAVAILABLE);
      }
    }
  }
}
 
Example #6
Source File: TestHandleSession.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public void onLiveInstanceChange(List<LiveInstance> liveInstances,
    NotificationContext changeContext) {
  if (changeContext.getType() != NotificationContext.Type.FINALIZE) {
    for (LiveInstance liveInstance : liveInstances) {
      if (_expectedLiveInstances.contains(liveInstance.getInstanceName())) {
        try {
          _manager.addCurrentStateChangeListener(
              (CurrentStateChangeListener) (instanceName, statesInfo, currentStateChangeContext) -> {
                // empty callback
              }, liveInstance.getInstanceName(), liveInstance.getEphemeralOwner());
        } catch (Exception e) {
          throw new HelixException("Unexpected exception in the test method.", e);
        }
      }
    }
  }
}
 
Example #7
Source File: CompatibilityCheckStage.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public void process(ClusterEvent event) throws Exception {
  HelixManager manager = event.getAttribute(AttributeName.helixmanager.name());
  BaseControllerDataProvider cache = event.getAttribute(AttributeName.ControllerDataProvider.name());
  if (manager == null || cache == null) {
    throw new StageException("Missing attributes in event:" + event
        + ". Requires HelixManager | DataCache");
  }

  HelixManagerProperties properties = manager.getProperties();
  Map<String, LiveInstance> liveInstanceMap = cache.getLiveInstances();
  for (LiveInstance liveInstance : liveInstanceMap.values()) {
    String participantVersion = liveInstance.getHelixVersion();
    if (!properties.isParticipantCompatible(participantVersion)) {
      String errorMsg =
          "incompatible participant. pipeline will not continue. " + "controller: "
              + manager.getInstanceName() + ", controllerVersion: " + properties.getVersion()
              + ", minimumSupportedParticipantVersion: "
              + properties.getProperty("minimum_supported_version.participant")
              + ", participant: " + liveInstance.getInstanceName() + ", participantVersion: "
              + participantVersion;
      LogUtil.logError(LOG, event.getEventId(), errorMsg);
      throw new StageException(errorMsg);
    }
  }
}
 
Example #8
Source File: SimpleClusterChangeHandler.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Triggered whenever there is a change in the list of live instances.
 * @param liveInstances the list of all live instances (not a change set) at the time of this call.
 * @param changeContext the {@link NotificationContext} associated.
 */
@Override
public void onLiveInstanceChange(List<LiveInstance> liveInstances, NotificationContext changeContext) {
  try {
    logger.debug("Live instance change triggered from {} with: {}", dcName, liveInstances);
    updateInstanceLiveness(liveInstances);
    if (!liveStateInitialized.get()) {
      logger.info("Received initial notification for live instance change from {}", dcName);
      liveStateInitialized.set(true);
    }
    helixClusterManagerMetrics.liveInstanceChangeTriggerCount.inc();
  } catch (Throwable t) {
    errorCount.incrementAndGet();
    throw t;
  }
}
 
Example #9
Source File: TestAssignableInstanceManager.java    From helix with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateAssignableInstances() {
  Map<String, LiveInstance> newLiveInstances = new HashMap<>();
  Map<String, InstanceConfig> newInstanceConfigs = new HashMap<>();

  // A brand new set of LiveInstances
  for (int i = NUM_PARTICIPANTS; i < NUM_PARTICIPANTS + 3; i++) {
    String instanceName = INSTANCE_PREFIX + i;
    newLiveInstances.put(instanceName, new LiveInstance(instanceName));
    newInstanceConfigs.put(instanceName, new InstanceConfig(instanceName));
  }

  _assignableInstanceManager.updateAssignableInstances(_clusterConfig, newLiveInstances,
      newInstanceConfigs);

  // Check that the assignable instance map contains new instances and there are no
  // TaskAssignResults due to previous live instances being removed
  Assert.assertEquals(_assignableInstanceManager.getTaskAssignResultMap().size(), 0);
  Assert.assertEquals(_assignableInstanceManager.getAssignableInstanceMap().size(),
      newLiveInstances.size());
  for (String instance : newLiveInstances.keySet()) {
    Assert
        .assertTrue(_assignableInstanceManager.getAssignableInstanceMap().containsKey(instance));
  }
}
 
Example #10
Source File: LeadControllerManagerTest.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
@BeforeMethod
public void setup() {
  _controllerMetrics = new ControllerMetrics(new MetricsRegistry());
  _helixManager = mock(HelixManager.class);
  HelixDataAccessor helixDataAccessor = mock(HelixDataAccessor.class);
  when(_helixManager.getHelixDataAccessor()).thenReturn(helixDataAccessor);

  PropertyKey.Builder keyBuilder = mock(PropertyKey.Builder.class);
  when(helixDataAccessor.keyBuilder()).thenReturn(keyBuilder);

  PropertyKey controllerLeader = mock(PropertyKey.class);
  when(keyBuilder.controllerLeader()).thenReturn(controllerLeader);
  _liveInstance = mock(LiveInstance.class);
  when(helixDataAccessor.getProperty(controllerLeader)).thenReturn(_liveInstance);

  String instanceId = LeadControllerUtils.generateParticipantInstanceId(CONTROLLER_HOST, CONTROLLER_PORT);
  when(_helixManager.getInstanceName()).thenReturn(instanceId);

  ConfigAccessor configAccessor = mock(ConfigAccessor.class);
  when(_helixManager.getConfigAccessor()).thenReturn(configAccessor);
  _resourceConfig = mock(ResourceConfig.class);
  when(configAccessor.getResourceConfig(any(), anyString())).thenReturn(_resourceConfig);
}
 
Example #11
Source File: TestP2PNoDuplicatedMessage.java    From helix with Apache License 2.0 6 votes vote down vote up
private void verifyP2PDisabled() {
  ResourceControllerDataProvider dataCache = new ResourceControllerDataProvider(CLUSTER_NAME);
  dataCache.refresh(_accessor);
  Map<String, LiveInstance> liveInstanceMap = dataCache.getLiveInstances();

  for (LiveInstance instance : liveInstanceMap.values()) {
    Map<String, CurrentState> currentStateMap =
        dataCache.getCurrentState(instance.getInstanceName(), instance.getEphemeralOwner());
    Assert.assertNotNull(currentStateMap);
    for (CurrentState currentState : currentStateMap.values()) {
      for (String partition : currentState.getPartitionStateMap().keySet()) {
        String state = currentState.getState(partition);
        if (state.equalsIgnoreCase("MASTER")) {
          String triggerHost = currentState.getTriggerHost(partition);
          Assert.assertEquals(triggerHost, _controllerName,
              state + " of " + partition + " on " + instance.getInstanceName()
                  + " was triggered by " + triggerHost);
        }
      }
    }
  }
}
 
Example #12
Source File: AutoRebalanceLiveInstanceChangeListener.java    From uReplicator with Apache License 2.0 6 votes vote down vote up
@Override
public void onLiveInstanceChange(final List<LiveInstance> liveInstances,
    NotificationContext changeContext) {
  LOGGER.info("AutoRebalanceLiveInstanceChangeListener.onLiveInstanceChange() wakes up!");
  _delayedScheuler.schedule(new Runnable() {
    @Override
    public void run() {
      try {
        rebalanceCurrentCluster(_helixMirrorMakerManager.getCurrentLiveInstances(),
            _helixMirrorMakerManager.getBlacklistedInstances(), true, false);
      } catch (Exception e) {
        LOGGER.error("Got exception during rebalance the whole cluster! ", e);
      }
    }
  }, _delayedAutoReblanceTimeInSeconds, TimeUnit.SECONDS);
}
 
Example #13
Source File: TestP2PNoDuplicatedMessage.java    From helix with Apache License 2.0 6 votes vote down vote up
private void verifyP2PEnabled(long startTime) {
  ResourceControllerDataProvider dataCache = new ResourceControllerDataProvider(CLUSTER_NAME);
  dataCache.refresh(_accessor);
  Map<String, LiveInstance> liveInstanceMap = dataCache.getLiveInstances();

  for (LiveInstance instance : liveInstanceMap.values()) {
    Map<String, CurrentState> currentStateMap =
        dataCache.getCurrentState(instance.getInstanceName(), instance.getEphemeralOwner());
    Assert.assertNotNull(currentStateMap);
    for (CurrentState currentState : currentStateMap.values()) {
      for (String partition : currentState.getPartitionStateMap().keySet()) {
        String state = currentState.getState(partition);
        long start = currentState.getStartTime(partition);
        if (state.equalsIgnoreCase("MASTER") && start > startTime) {
          String triggerHost = currentState.getTriggerHost(partition);
          if (!triggerHost.equals(_controllerName)) {
            p2pTrigged ++;
          }
          total ++;
        }
      }
    }
  }
}
 
Example #14
Source File: CustomizedViewRoutingTable.java    From helix with Apache License 2.0 5 votes vote down vote up
protected CustomizedViewRoutingTable(Collection<CustomizedView> customizedViews,
    Collection<InstanceConfig> instanceConfigs, Collection<LiveInstance> liveInstances,
    PropertyType propertytype, String customizedStateType) {
  super(Collections.<ExternalView> emptyList(), instanceConfigs, liveInstances,
      PropertyType.CUSTOMIZEDVIEW);
  _customizedStateType = customizedStateType;
  _customizedViews = new HashSet<>(customizedViews);
  refresh(_customizedViews);
}
 
Example #15
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 #16
Source File: GenericHelixController.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void onLiveInstanceChange(List<LiveInstance> liveInstances,
    NotificationContext changeContext) {
  logger.info("START: Generic GenericClusterController.onLiveInstanceChange() for cluster " + _clusterName);
  notifyCaches(changeContext, ChangeType.LIVE_INSTANCE);

  if (liveInstances == null) {
    liveInstances = Collections.emptyList();
  }

  // Go though the live instance list and make sure that we are observing them
  // accordingly. The action is done regardless of the paused flag.
  if (changeContext.getType() == NotificationContext.Type.INIT
      || changeContext.getType() == NotificationContext.Type.CALLBACK) {
    checkLiveInstancesObservation(liveInstances, changeContext);
  } else if (changeContext.getType() == NotificationContext.Type.FINALIZE) {
    // on finalize, should remove all message/current-state listeners
    logger.info("remove message/current-state listeners. lastSeenInstances: " + _lastSeenInstances
        + ", lastSeenSessions: " + _lastSeenSessions);
    liveInstances = Collections.emptyList();
    checkLiveInstancesObservation(liveInstances, changeContext);
  }

  pushToEventQueues(ClusterEventType.LiveInstanceChange, changeContext,
      Collections.<String, Object>singletonMap(AttributeName.eventData.name(), liveInstances));

  logger.info(
      "END: Generic GenericClusterController.onLiveInstanceChange() for cluster " + _clusterName);
}
 
Example #17
Source File: ControllerLiveInstanceChangeListener.java    From uReplicator with Apache License 2.0 5 votes vote down vote up
@Override
public void onLiveInstanceChange(final List<LiveInstance> liveInstances,
    NotificationContext changeContext) {
  LOGGER.info("ControllerLiveInstanceChangeListener.onLiveInstanceChange() wakes up!");
  _delayedScheduler.schedule(new Runnable() {
    @Override
    public void run() {
      try {
        rebalanceCurrentCluster(true);
      } catch (Exception e) {
        LOGGER.error("Got exception during rebalance the whole cluster! ", e);
      }
    }
  }, 5, TimeUnit.SECONDS);
}
 
Example #18
Source File: CustomizedStateComputationStage.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void process(ClusterEvent event) throws Exception {
  _eventId = event.getEventId();
  ResourceControllerDataProvider cache =
      event.getAttribute(AttributeName.ControllerDataProvider.name());
  final Map<String, Resource> resourceMap =
      event.getAttribute(AttributeName.RESOURCES_TO_REBALANCE.name());
  Set<String> aggregationEnabledTypes = cache.getAggregationEnabledCustomizedStateTypes();

  if (cache == null || resourceMap == null) {
    throw new StageException(
        "Missing attributes in event:" + event + ". Requires DataCache|RESOURCE");
  }

  Map<String, LiveInstance> liveInstances = cache.getLiveInstances();
  final CustomizedStateOutput customizedStateOutput = new CustomizedStateOutput();

  for (LiveInstance instance : liveInstances.values()) {
    String instanceName = instance.getInstanceName();
    // update customized states.
    for (String customizedStateType : aggregationEnabledTypes) {
      Map<String, CustomizedState> customizedStateMap =
          cache.getCustomizedState(instanceName, customizedStateType);
      updateCustomizedStates(instanceName, customizedStateType, customizedStateMap,
          customizedStateOutput, resourceMap);
    }
  }
  event.addAttribute(AttributeName.CUSTOMIZED_STATE.name(), customizedStateOutput);
}
 
Example #19
Source File: ServiceDiscovery.java    From helix with Apache License 2.0 5 votes vote down vote up
private void refreshCache(List<LiveInstance> liveInstances) {
  List<ServiceMetadata> services = new ArrayList<ServiceMetadata>();
  for (LiveInstance liveInstance : liveInstances) {
    ServiceMetadata metadata = new ServiceMetadata();
    ZNRecord rec = liveInstance.getRecord();
    metadata.setPort(Integer.parseInt(rec.getSimpleField("PORT")));
    metadata.setHost(rec.getSimpleField("HOST"));
    metadata.setServiceName(rec.getSimpleField("SERVICE_NAME"));
    services.add(metadata);
  }
  // protect against multiple threads updating this
  synchronized (this) {
    cache = services;
  }
}
 
Example #20
Source File: TestTopStateHandoffMetrics.java    From helix with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "fastCurrentStateInput")
public void testFastTopStateHandoffWithNoMissingTopStateAndOldInstanceCrash(TestCaseConfig cfg) {
  preSetup();
  event.addAttribute(AttributeName.LastRebalanceFinishTimeStamp.name(), 7500L);
  // By simulating last master instance crash, we now have:
  //  - M->S from 6000 to 7000
  //  - lastPipelineFinishTimestamp is 7500
  //  - S->M from 8000 to 9000
  // Therefore the recorded latency should be 9000 - 7500 = 1500, though original master crashed,
  // since this is a single top state handoff observed within 1 pipeline, we treat it as graceful,
  // and only record user latency for transiting to master
  Range<Long> expectedDuration = Range.closed(1500L, 1500L);
  Range<Long> expectedHelixLatency = Range.closed(500L, 500L);
  runStageAndVerify(
      cfg.initialCurrentStates, cfg.currentStateWithMissingTopState, cfg.finalCurrentState,
      new MissingStatesDataCacheInject() {
        @Override
        public void doInject(ResourceControllerDataProvider cache) {
          Map<String, LiveInstance> liMap = new HashMap<>(cache.getLiveInstances());
          liMap.remove("localhost_1");
          cache.setLiveInstances(new ArrayList<>(liMap.values()));
        }
      }, 1, 0,
      expectedDuration,
      DURATION_ZERO,
      expectedDuration, expectedHelixLatency
  );
  event.addAttribute(AttributeName.LastRebalanceFinishTimeStamp.name(),
      TopStateHandoffReportStage.TIMESTAMP_NOT_RECORDED);
}
 
Example #21
Source File: RoutingTable.java    From helix with Apache License 2.0 5 votes vote down vote up
protected RoutingTable(Collection<ExternalView> externalViews, Collection<InstanceConfig> instanceConfigs,
    Collection<LiveInstance> liveInstances, PropertyType propertytype) {
  // TODO Refactor these constructors so we don't have so many constructor.
  _propertyType = propertytype;
  _resourceInfoMap = new HashMap<>();
  _resourceGroupInfoMap = new HashMap<>();
  _liveInstances = new HashSet<>(liveInstances);
  _instanceConfigs = new HashSet<>(instanceConfigs);
  _externalViews = new HashSet<>(externalViews);
  refresh(_externalViews);
}
 
Example #22
Source File: CloudToStoreReplicationManager.java    From ambry with Apache License 2.0 5 votes vote down vote up
@Override
public void onLiveInstanceChange(List<LiveInstance> liveInstances, NotificationContext changeContext) {
  logger.info("Live instance change notification received. liveInstances: {}", liveInstances);
  Set<CloudDataNode> newVcrNodes = new HashSet<>();
  // react to change in liveness of vcr nodes if the instance was earlier reported by helix as part of
  // {@code onInstanceConfigChange} notification.
  synchronized (notificationLock) {
    for (LiveInstance liveInstance : liveInstances) {
      if (instanceNameToCloudDataNode.get().containsKey(liveInstance.getInstanceName())) {
        newVcrNodes.add(instanceNameToCloudDataNode.get().get(liveInstance.getInstanceName()));
      }
    }
    handleChangeInVcrNodes(newVcrNodes);
  }
}
 
Example #23
Source File: CustomizedStateCache.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
protected Set<PropertyKey> PopulateParticipantKeys(HelixDataAccessor accessor,
    Map<String, LiveInstance> liveInstanceMap) {
  Set<PropertyKey> participantStateKeys = new HashSet<>();
  PropertyKey.Builder keyBuilder = accessor.keyBuilder();
  for (String instanceName : liveInstanceMap.keySet()) {
    for (String customizedStateType : _aggregationEnabledTypes) {
      accessor.getChildNames(keyBuilder.customizedStates(instanceName, customizedStateType))
          .stream().forEach(resourceName -> participantStateKeys
          .add(keyBuilder.customizedState(instanceName, customizedStateType, resourceName)));
    }
  }
  return participantStateKeys;
}
 
Example #24
Source File: TestResetPartitionState.java    From helix with Apache License 2.0 5 votes vote down vote up
private void clearStatusUpdate(String clusterName, String instance, String resource,
    String partition) {
  // clear status update for error partition so verify() will not fail on
  // old errors
  ZKHelixDataAccessor accessor =
      new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
  Builder keyBuilder = accessor.keyBuilder();

  LiveInstance liveInstance = accessor.getProperty(keyBuilder.liveInstance(instance));
  accessor.removeProperty(keyBuilder.stateTransitionStatus(instance, liveInstance.getEphemeralOwner(),
      resource, partition));

}
 
Example #25
Source File: TestAutoRebalanceStrategy.java    From helix with Apache License 2.0 5 votes vote down vote up
private Map<String, Map<String, String>> getMapping(final Map<String, List<String>> listResult) {
  final Map<String, Map<String, String>> mapResult = new HashMap<String, Map<String, String>>();
  ResourceControllerDataProvider cache = new ResourceControllerDataProvider();
  MockAccessor accessor = new MockAccessor();
  Builder keyBuilder = accessor.keyBuilder();
  ClusterConfig clusterConfig = new ClusterConfig("TestCluster");
  accessor.setProperty(keyBuilder.clusterConfig(), clusterConfig);
  for (String node : _liveNodes) {
    LiveInstance liveInstance = new LiveInstance(node);
    liveInstance.setSessionId("testSession");
    accessor.setProperty(keyBuilder.liveInstance(node), liveInstance);
  }
  cache.refresh(accessor);

  IdealState is = new IdealState("resource");
  for (String partition : _partitions) {
    List<String> preferenceList = listResult.get(partition);
    Map<String, String> currentStateMap = _currentMapping.get(partition);
    Set<String> disabled = Collections.emptySet();
    Partition p = new Partition(partition);
    CurrentStateOutput currentStateOutput = new CurrentStateOutput();
    if (currentStateMap != null) {
      for (String instance : currentStateMap.keySet()) {
        currentStateOutput
            .setCurrentState("resource", p, instance, currentStateMap.get(instance));
      }
    }
    Map<String, String> assignment = new AutoRebalancer()
        .computeBestPossibleStateForPartition(cache.getLiveInstances().keySet(), _stateModelDef,
            preferenceList, currentStateOutput, disabled, is, clusterConfig, p,
            MonitoredAbnormalResolver.DUMMY_STATE_RESOLVER);
    mapResult.put(partition, assignment);
  }
  return mapResult;
}
 
Example #26
Source File: AssignerTestBase.java    From helix with Apache License 2.0 5 votes vote down vote up
LiveInstance createLiveInstance(String[] resourceTypes, String[] resourceCapacity,
    String instancename) {
  LiveInstance li = new LiveInstance(instancename);
  if (resourceCapacity != null && resourceTypes != null) {
    Map<String, String> resMap = new HashMap<>();
    for (int i = 0; i < resourceCapacity.length; i++) {
      resMap.put(resourceTypes[i], resourceCapacity[i]);
    }
    li.setResourceCapacityMap(resMap);
  }
  return li;
}
 
Example #27
Source File: TestRoutingTableProviderPeriodicRefresh.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
protected synchronized void refreshCurrentState(
    Map<String, Map<String, Map<String, CurrentState>>> currentStateMap,
    Collection<InstanceConfig> instanceConfigs, Collection<LiveInstance> liveInstances,
    String referenceKey) {
  super.refreshCurrentState(currentStateMap, instanceConfigs, liveInstances, "Test");
  _refreshCount++;
  if (DEBUG) {
    print();
  }
}
 
Example #28
Source File: ZkTestBase.java    From helix with Apache License 2.0 5 votes vote down vote up
protected List<LiveInstance> setupLiveInstances(String clusterName, int[] liveInstances) {
  HelixZkClient.ZkClientConfig clientConfig = new HelixZkClient.ZkClientConfig();
  clientConfig.setZkSerializer(new ZNRecordSerializer());

  List<LiveInstance> result = new ArrayList<>();

  for (int i = 0; i < liveInstances.length; i++) {
    String instance = "localhost_" + liveInstances[i];

    _liveInstanceOwners.putIfAbsent(clusterName, new HashMap<>());
    Map<String, HelixZkClient> clientMap = _liveInstanceOwners.get(clusterName);
    clientMap.putIfAbsent(instance, DedicatedZkClientFactory.getInstance()
        .buildZkClient(new HelixZkClient.ZkConnectionConfig(ZK_ADDR), clientConfig));
    HelixZkClient client = clientMap.get(instance);

        ZKHelixDataAccessor accessor =
        new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<>(client));
    Builder keyBuilder = accessor.keyBuilder();

    LiveInstance liveInstance = new LiveInstance(instance);
    // Keep setting the session id in the deprecated field for ensure the same behavior as a real participant.
    // Note the participant is doing so for backward compatibility.
    liveInstance.setSessionId(Long.toHexString(client.getSessionId()));
    // Please refer to the version requirement here: helix-core/src/main/resources/cluster-manager-version.properties
    // Ensuring version compatibility can avoid the warning message during test.
    liveInstance.setHelixVersion("0.4");
    accessor.setProperty(keyBuilder.liveInstance(instance), liveInstance);
    result.add(accessor.getProperty(keyBuilder.liveInstance(instance)));
  }
  return result;
}
 
Example #29
Source File: TestControllerLeadershipChange.java    From helix with Apache License 2.0 5 votes vote down vote up
private void setLeader(HelixManager manager) throws Exception {
  HelixDataAccessor accessor = manager.getHelixDataAccessor();
  final LiveInstance leader = new LiveInstance(manager.getInstanceName());
  leader.setLiveInstance(ManagementFactory.getRuntimeMXBean().getName());
  leader.setSessionId(manager.getSessionId());
  leader.setHelixVersion(manager.getVersion());

  // Delete the current controller leader node so it will trigger leader election
  while (!manager.isLeader()) {
    accessor.getBaseDataAccessor()
        .remove(PropertyPathBuilder.controllerLeader(manager.getClusterName()),
            AccessOption.EPHEMERAL);
    Thread.sleep(50);
  }
}
 
Example #30
Source File: ClusterResource.java    From helix with Apache License 2.0 5 votes vote down vote up
StringRepresentation getClusterRepresentation(String clusterName) throws JsonGenerationException,
    JsonMappingException, IOException {
  ZkClient zkClient =
      ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
  ClusterSetup setupTool = new ClusterSetup(zkClient);
  List<String> instances =
      setupTool.getClusterManagementTool().getInstancesInCluster(clusterName);

  ZNRecord clusterSummayRecord = new ZNRecord("Cluster Summary");
  clusterSummayRecord.setListField("participants", instances);

  List<String> resources =
      setupTool.getClusterManagementTool().getResourcesInCluster(clusterName);
  clusterSummayRecord.setListField("resources", resources);

  List<String> models = setupTool.getClusterManagementTool().getStateModelDefs(clusterName);
  clusterSummayRecord.setListField("stateModelDefs", models);

  HelixDataAccessor accessor =
      ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
  Builder keyBuilder = accessor.keyBuilder();

  LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
  if (leader != null) {
    clusterSummayRecord.setSimpleField("LEADER", leader.getInstanceName());
  } else {
    clusterSummayRecord.setSimpleField("LEADER", "");
  }
  StringRepresentation representation =
      new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(clusterSummayRecord),
          MediaType.APPLICATION_JSON);

  return representation;
}