Package org.apache.helix

Examples of org.apache.helix.HelixDataAccessor


  }

  Query prepareQuery(Map<String, Object> bindVariables, List<Object> additionalFunctionHandlers)
  {
    // DataAccessor accessor = _manager.getDataAccessor();
    HelixDataAccessor accessor = _manager.getHelixDataAccessor();
   
    // Get all the ZNRecords in the cluster and set them as bind variables
    Builder keyBuilder = accessor.keyBuilder();
//    List<ZNRecord> instanceConfigs = accessor.getChildValues(PropertyType.CONFIGS,
//        ConfigScopeProperty.PARTICIPANT.toString());
   
    List<ZNRecord> instanceConfigs = HelixProperty.convertToList(accessor.getChildValues(keyBuilder.instanceConfigs()));

    List<ZNRecord> liveInstances = HelixProperty.convertToList(accessor.getChildValues(keyBuilder.liveInstances()));
    List<ZNRecord> stateModelDefs = HelixProperty.convertToList(accessor.getChildValues(keyBuilder.stateModelDefs()));
   
    // Idealstates are stored in a map from resource name to idealState ZNRecord
    List<ZNRecord> idealStateList = HelixProperty.convertToList(accessor.getChildValues(keyBuilder.idealStates()));
   
    Map<String, ZNRecord> idealStatesMap = new HashMap<String, ZNRecord>();
    for (ZNRecord idealState : idealStateList)
    {
      idealStatesMap.put(idealState.getId(), idealState);
    }
    // Make up the partition list: for selecting partitions
    List<ZNRecord> partitions = new ArrayList<ZNRecord>();
    for (ZNRecord idealState : idealStateList)
    {
      for (String partitionName : idealState.getMapFields().keySet())
      {
        partitions.add(new ZNRecord(partitionName));
      }
    }
   
    List<ZNRecord> externalViewList = HelixProperty.convertToList(accessor.getChildValues(keyBuilder.externalViews()));
    // ExternalViews are stored in a map from resource name to idealState
    // ZNRecord
    Map<String, ZNRecord> externalViewMap = new HashMap<String, ZNRecord>();
    for (ZNRecord externalView : externalViewList)
    {
      externalViewMap.put(externalView.getId(), externalView);
    }
    // Map from instance name to a map from resource to current state ZNRecord
    Map<String, Map<String, ZNRecord>> currentStatesMap = new HashMap<String, Map<String, ZNRecord>>();
    // Map from instance name to a list of combined flat ZNRecordRow
    Map<String, List<ZNRecordRow>> flatCurrentStateMap = new HashMap<String, List<ZNRecordRow>>();

    for (ZNRecord instance : liveInstances)
    {
      String host = instance.getId();
      String sessionId = instance.getSimpleField(LiveInstanceProperty.SESSION_ID.toString());
      Map<String, ZNRecord> currentStates = new HashMap<String, ZNRecord>();
      List<ZNRecord> instanceCurrentStateList = new ArrayList<ZNRecord>();
      for (ZNRecord idealState : idealStateList)
      {
        String resourceName = idealState.getId();
       
        HelixProperty property = accessor.getProperty(keyBuilder.currentState(host, sessionId, resourceName));
        ZNRecord currentState =null;
        if (property == null)
        {
          _logger.warn("Resource " + resourceName + " has null currentState");
          currentState = new ZNRecord(resourceName);
View Full Code Here


                                 manager.getHelixDataAccessor());
      logger.error(errorMessage);
      throw new HelixException(errorMessage);
    }
    // DataAccessor accessor = manager.getDataAccessor();
    HelixDataAccessor accessor = manager.getHelixDataAccessor();

    String partitionName = message.getPartitionName();
    String fromState = message.getFromState();

    // Verify the fromState and current state of the stateModel
View Full Code Here

    String partitionKey = message.getPartitionName();
    String resource = message.getResourceName();
    String sessionId = message.getTgtSessionId();
    String instanceName = manager.getInstanceName();

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

    int bucketSize = message.getBucketSize();
    ZNRecordBucketizer bucketizer = new ZNRecordBucketizer(bucketSize);

    // Lock the helix manager so that the session id will not change when we update
    // the state model state. for zk current state it is OK as we have the per-session
    // current state node
    synchronized (manager)
    {
      if (!message.getTgtSessionId().equals(manager.getSessionId()))
      {
        logger.warn("Session id has changed. Skip postExecutionMessage. Old session "
            + message.getExecutionSessionId() + " , new session : "
            + manager.getSessionId());
        return;
      }

      if (taskResult.isSucess())
      {
        // String fromState = message.getFromState();
        String toState = message.getToState();
        _currentStateDelta.setState(partitionKey, toState);

        if (toState.equalsIgnoreCase("DROPPED"))
        {
          // for "OnOfflineToDROPPED" message, we need to remove the resource key record
          // from the current state of the instance because the resource key is dropped.
          // In the state model it will be stayed as "OFFLINE", which is OK.
          ZNRecordDelta delta =
              new ZNRecordDelta(_currentStateDelta.getRecord(), MergeOperation.SUBTRACT);
          // Don't subtract simple fields since they contain stateModelDefRef
          delta._record.getSimpleFields().clear();

          List<ZNRecordDelta> deltaList = new ArrayList<ZNRecordDelta>();
          deltaList.add(delta);
          _currentStateDelta.setDeltaList(deltaList);
        }
        else
        {
          // if the partition is not to be dropped, update _stateModel to the TO_STATE
          _stateModel.updateState(toState);
        }
      }
      else
      {
        if (exception instanceof HelixStateMismatchException)
        {
          // if fromState mismatch, set current state on zk to stateModel's current state
          logger.warn("Force CurrentState on Zk to be stateModel's CurrentState. partitionKey: "
              + partitionKey
              + ", currentState: "
              + _stateModel.getCurrentState()
              + ", message: " + message);
          _currentStateDelta.setState(partitionKey, _stateModel.getCurrentState());
        }
        else
        {
          StateTransitionError error =
              new StateTransitionError(ErrorType.INTERNAL, ErrorCode.ERROR, exception);
          if (exception instanceof InterruptedException)
          {
            if (_isTimeout)
            {
              error =
                  new StateTransitionError(ErrorType.INTERNAL,
                                           ErrorCode.TIMEOUT,
                                           exception);
            }
            else
            {
              // State transition interrupted but not caused by timeout. Keep the current
              // state in this case
              logger.error("State transition interrupted but not timeout. Not updating state. Partition : "
                  + message.getPartitionName() + " MsgId : " + message.getMsgId());
              return;
            }
          }
          _stateModel.rollbackOnError(message, context, error);
          _currentStateDelta.setState(partitionKey, "ERROR");
          _stateModel.updateState("ERROR");
        }
      }
    }
    try
    {
      // Update the ZK current state of the node
      PropertyKey key = keyBuilder.currentState(instanceName,
                              sessionId,
                              resource,
                              bucketizer.getBucketName(partitionKey));
      if (!_message.getGroupMessageMode())
      {
        accessor.updateProperty(key, _currentStateDelta);
      }
      else
      {
        _executor._groupMsgHandler.addCurStateUpdate(_message, key, _currentStateDelta);
      }
View Full Code Here

  {
    synchronized (_stateModel)
    {
      HelixTaskResult taskResult = new HelixTaskResult();
      HelixManager manager = context.getManager();
      HelixDataAccessor accessor = manager.getHelixDataAccessor();

      _statusUpdateUtil.logInfo(message,
                                HelixStateTransitionHandler.class,
                                "Message handling task begin execute",
                                accessor);
View Full Code Here

    {
      logger.error("Skip internal error " + e.getMessage() + " " + code);
      return;
    }
    HelixManager manager = _notificationContext.getManager();
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    Builder keyBuilder = accessor.keyBuilder();

    String instanceName = manager.getInstanceName();
    String partition = _message.getPartitionName();
    String resourceName = _message.getResourceName();
    CurrentState currentStateDelta = new CurrentState(resourceName);

    StateTransitionError error = new StateTransitionError(type, code, e);
    _stateModel.rollbackOnError(_message, _notificationContext, error);
    // if the transition is not canceled, it should go into error state
    if (code == ErrorCode.ERROR)
    {
      currentStateDelta.setState(partition, "ERROR");
      _stateModel.updateState("ERROR");

      accessor.updateProperty(keyBuilder.currentState(instanceName,
                                                      _message.getTgtSessionId(),
                                                      resourceName),
                              currentStateDelta);
    }
  }
View Full Code Here

    if (manager == null)
    {
      throw new StageException("HelixManager attribute value is null");
    }
    // DataAccessor dataAccessor = manager.getDataAccessor();
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    _cache.refresh(accessor);

    event.addAttribute("HealthDataCache", _cache);

    long processLatency = System.currentTimeMillis() - startTime;
View Full Code Here

  @Override
  public void onControllerChange(NotificationContext changeContext)
  {
    logger.info("START: GenericClusterController.onControllerChange()");
    HelixDataAccessor accessor = changeContext.getManager().getHelixDataAccessor();

    // double check if this controller is the leader
    Builder keyBuilder = accessor.keyBuilder();
    LiveInstance leader =
        accessor.getProperty(keyBuilder.controllerLeader());
    if (leader == null)
    {
      logger.warn("No controller exists for cluster:"
          + changeContext.getManager().getClusterName());
      return;
    }
    else
    {
      String leaderName = leader.getInstanceName();

      String instanceName = changeContext.getManager().getInstanceName();
      if (leaderName == null || !leaderName.equals(instanceName))
      {
        logger.warn("leader name does NOT match, my name: " + instanceName + ", leader: "
            + leader);
        return;
      }
    }

    PauseSignal pauseSignal = accessor.getProperty(keyBuilder.pause());
    if (pauseSignal != null)
    {
      _paused = true;
      logger.info("controller is now paused");
    }
View Full Code Here

    ErrorType type = ErrorType.INTERNAL;
    ErrorCode code = ErrorCode.ERROR;

    long start = System.currentTimeMillis();
    logger.info("msg:" + _message.getMsgId() + " handling task begin, at: " + start);
    HelixDataAccessor accessor = _manager.getHelixDataAccessor();
    _statusUpdateUtil.logInfo(_message,
                              HelixTask.class,
                              "Message handling task begin execute",
                              accessor);
    _message.setExecuteStartTimeStamp(new Date().getTime());

    // Handle the message
    try
    {
      taskResult = _handler.handleMessage();
      exception = taskResult.getException();
    }
    catch (InterruptedException e)
    {
      _statusUpdateUtil.logError(_message,
                                 HelixTask.class,
                                 e,
                                 "State transition interrupted, timeout:" + _isTimeout,
                                 accessor);
      logger.info("Message " + _message.getMsgId() + " is interrupted");
      taskResult.setInterrupted(true);
      taskResult.setException(e);
      exception = e;
    }
    catch (Exception e)
    {
      String errorMessage =
          "Exception while executing a message. " + e + " msgId: " + _message.getMsgId()
              + " type: " + _message.getMsgType();
      logger.error(errorMessage, e);
      _statusUpdateUtil.logError(_message, HelixTask.class, e, errorMessage, accessor);
      taskResult.setSuccess(false);
      taskResult.setException(e);
      taskResult.setMessage(e.getMessage());
      exception = e;
    }

    // Cancel the timer since the handling is done
    // it is fine if the TimerTask for canceling is called already
    if (timer != null)
    {
      timer.cancel();
    }

    if (taskResult.isSucess())
    {
      _statusUpdateUtil.logInfo(_message,
                                _handler.getClass(),
                                "Message handling task completed successfully",
                                accessor);
      logger.info("Message " + _message.getMsgId() + " completed.");
    }
    else if (taskResult.isInterrupted())
    {
      logger.info("Message " + _message.getMsgId() + " is interrupted");
      code = _isTimeout ? ErrorCode.TIMEOUT : ErrorCode.CANCEL;
      if (_isTimeout)
      {
        int retryCount = _message.getRetryCount();
        logger.info("Message timeout, retry count: " + retryCount + " MSGID:"
            + _message.getMsgId());
        _statusUpdateUtil.logInfo(_message,
                                  _handler.getClass(),
                                  "Message handling task timeout, retryCount:"
                                      + retryCount,
                                  accessor);
        // Notify the handler that timeout happens, and the number of retries left
        // In case timeout happens (time out and also interrupted)
        // we should retry the execution of the message by re-schedule it in
        if (retryCount > 0)
        {
          _message.setRetryCount(retryCount - 1);
          _executor.scheduleTask(_message, _handler, _notificationContext);
          return taskResult;
        }
      }
    }
    else
    // logging for errors
    {
      String errorMsg =
          "Message execution failed. msgId: " + _message.getMsgId()
              + taskResult.getMessage();
      if (exception != null)
      {
        errorMsg += exception;
      }
      logger.error(errorMsg, exception);
      _statusUpdateUtil.logError(_message, _handler.getClass(), errorMsg, accessor);
    }

    // Post-processing for the finished task
    try
    {
      if (!_message.getGroupMessageMode())
      {
        removeMessageFromZk(accessor, _message);
        reportMessageStat(_manager, _message, taskResult);
        sendReply(accessor, _message, taskResult);
      }
      else
      {
        GroupMessageInfo info = _executor._groupMsgHandler.onCompleteSubMessage(_message);
        if (info != null)
        {
          // TODO: changed to async update
          // group update current state
          Map<PropertyKey, CurrentState> curStateMap = info.merge();
          for (PropertyKey key : curStateMap.keySet())
          {
            accessor.updateProperty(key, curStateMap.get(key));
          }

          // remove group message
          removeMessageFromZk(accessor, _message);
          reportMessageStat(_manager, _message, taskResult);
View Full Code Here

       
      if(command.equalsIgnoreCase(ClusterSetup.addStateModelDef))
      {
        ZNRecord newStateModel = jsonParameters.getExtraParameter(JsonParameters.NEW_STATE_MODEL_DEF);
        HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
        
        accessor.setProperty(accessor.keyBuilder().stateModelDef(newStateModel.getId()), new StateModelDefinition(newStateModel) );
        getResponse().setEntity(getStateModelsRepresentation());
      }
      else
      {
          throw new HelixException("Unsupported command: " + command
View Full Code Here

    LOG.info("START HealthAggregationTask");

    if (_timer == null)
    {
      // Remove all the previous health check values, if any
      HelixDataAccessor accessor = _manager.getHelixDataAccessor();
      List<String> existingHealthRecordNames = accessor.getChildNames(accessor.keyBuilder().healthReports(_manager.getInstanceName()));
      for(String healthReportName : existingHealthRecordNames)
      {
        LOG.info("Removing old healthrecord " + healthReportName);
        accessor.removeProperty(accessor.keyBuilder().healthReport(_manager.getInstanceName(),healthReportName));
      }
     
      _timer = new Timer(true);
      _timer.scheduleAtFixedRate(this, new Random().nextInt(_delay), _period);
    }
View Full Code Here

TOP

Related Classes of org.apache.helix.HelixDataAccessor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.