예제 #1
0
  public String proc(HBResult result, boolean putOpinion)
      throws NodeExistsException, MgmtZooKeeperException {
    HeartbeatTarget target = result.getTarget();
    HBRefData data = target.getRefData();

    if (target.getHB().equals(Constant.HB_MONITOR_NO)) {
      Logger.debug(
          "{} is not a target of heartbeat. {}",
          target.getNodeType().toString() + target.getName(),
          result);
      return null;
    }

    switch (target.getNodeType()) {
      case PGS:
        pgs(data, result, putOpinion);
        break;

      case RS:
      case GW:
        common(data, result, putOpinion);
        break;

      default:
        Logger.warn("An unkown type of a heartbeat target. type: {}", target.getNodeType());
        break;
    }

    return null;
  }
예제 #2
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);
  }
예제 #3
0
  private void removeMyOpinion(HBRefData refData, HBResult result, String newState)
      throws MgmtZooKeeperException {
    String path = makePathOfMyOpinion(result.getTarget().getTargetOfHeartbeatPath());

    zookeeper.deleteZNode(path, -1);

    Logger.info(result.toString());

    refData.setLastState(Constant.SERVER_STATE_UNKNOWN);
    refData.setLastStateTimestamp(0L);
    refData.setSubmitMyOpinion(false);
  }
예제 #4
0
  @Test
  public void slowCommandLog()
      throws SecurityException, NoSuchFieldException, IllegalArgumentException,
          IllegalAccessException {
    Statistics.updateElapsedTimeForCommands("192.168.0.10", 10000, "ping", "pong", 2000, 1000);

    Field f = Logger.getLogHistory().getClass().getDeclaredField("logs");
    f.setAccessible(true);
    @SuppressWarnings("unchecked")
    List<Log> logs = (List<Log>) f.get(Logger.getLogHistory());

    boolean find = false;
    for (Log log : logs) {
      int index =
          log.toString()
              .indexOf(
                  "CC Slow cmd. 192.168.0.10:10000, elapsed: 2000ms, request: \"ping\", reply: \"pong\"");
      if (index != -1) {
        find = true;
        break;
      }
    }
    assertTrue("Slow command log not found.", find);
  }
예제 #5
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);
      }
    }
  }