示例#1
0
  private void putMyOpinion(
      long stateTimestamp, HBRefData refData, HBResult result, String newState)
      throws MgmtZooKeeperException, NodeExistsException {
    String path = makePathOfMyOpinion(result.getTarget().getTargetOfHeartbeatPath());
    byte[] data;

    data = makeDataOfMyOpinion(refData, stateTimestamp, result, newState);

    try {
      zookeeper.createEphemeralZNode(path, data);
    } catch (NodeExistsException e) {
      Logger.error(
          "Put my opinion fail. path: {}, opinion: {}",
          path,
          makeStringOfMyOpinion(refData, stateTimestamp, newState),
          e);
      throw e;
    } catch (MgmtZooKeeperException e) {
      Logger.error(
          "Put my opinion fail. path: {}, opinion: {}",
          path,
          makeStringOfMyOpinion(refData, stateTimestamp, newState),
          e);
      throw e;
    }

    Logger.info(result.toString());

    refData.setLastState(newState);
    refData.setLastStateTimestamp(stateTimestamp);
    refData.setSubmitMyOpinion(true);
  }
示例#2
0
  private void pgs(HBRefData refData, HBResult result, boolean putOpinion)
      throws MgmtZooKeeperException, NodeExistsException {
    String newState = result.getState();
    long stateTimestamp = Constant.DEFAULT_STATE_TIMESTAMP;
    HBRefData.ZKData zkData = refData.getZkData();

    if (result.getState().equals(Constant.SERVER_STATE_NORMAL)) {
      String[] resAry = result.getResponse().split(" ");
      if (resAry.length < 3) {
        Logger.error(
            "Invalid response. from: {}, response: {}",
            result.getRemoteIP() + ":" + result.getRemoteIP(),
            result.getResponse());
        newState = Constant.SERVER_STATE_FAILURE;
      }

      if (resAry[1].equals("1")) {
        newState = Constant.SERVER_STATE_LCONN;
      } else if (resAry[1].equals("0")) {
        newState = Constant.SERVER_STATE_FAILURE;
      }

      stateTimestamp = Long.parseLong(resAry[2]);
    } else if (result.getState().equals(Constant.SERVER_STATE_FAILURE)) {
      stateTimestamp = zkData.stateTimestamp + 1;
    }

    if (stateTimestamp == zkData.stateTimestamp && newState.equals(zkData.state)) {
      if (refData.isSubmitMyOpinion()) {
        if (putOpinion) {
          removeMyOpinion(refData, result, newState);
        } else {
          workflowExecutor.perform(OPINION_PUBLISH, result);
        }
      }
    } else if (newState.equals(zkData.state)
        && newState.equals(Constant.SERVER_STATE_FAILURE)
        && stateTimestamp - 1 == zkData.stateTimestamp) {
      if (refData.isSubmitMyOpinion()) {
        if (putOpinion) {
          removeMyOpinion(refData, result, newState);
        } else {
          workflowExecutor.perform(OPINION_PUBLISH, result);
        }
      }
    }
    /**
     * HBC assumes that dead PGS`s version is zk_version + 1, because Dead PGS`s version(timestamp)
     * is 0. so this routine is located at the bottom of these conditional statements. Otherwise, if
     * PGS was in dead, then this function increases PGS`s version and puts an opinion to the
     * ZooKeeper repeatedly.
     */
    else if (!newState.equals(refData.getLastState())
        || stateTimestamp != refData.getLastStateTimestamp()) {
      if (putOpinion) {
        if (refData.isSubmitMyOpinion()) {
          removeMyOpinion(refData, result, newState);
        }
        putMyOpinion(stateTimestamp, refData, result, newState);
      } else {
        workflowExecutor.perform(OPINION_PUBLISH, result);
      }
    }
  }