Пример #1
0
  protected EntryPosition findStartPositionInternal(ErosaConnection connection) {
    MysqlConnection mysqlConnection = (MysqlConnection) connection;
    LogPosition logPosition = logPositionManager.getLatestIndexBy(destination);
    if (logPosition == null) { // 找不到历史成功记录
      EntryPosition entryPosition = null;
      if (masterInfo != null
          && mysqlConnection.getConnector().getAddress().equals(masterInfo.getAddress())) {
        entryPosition = masterPosition;
      } else if (standbyInfo != null
          && mysqlConnection.getConnector().getAddress().equals(standbyInfo.getAddress())) {
        entryPosition = standbyPosition;
      }

      if (entryPosition == null) {
        entryPosition = findEndPosition(mysqlConnection); // 默认从当前最后一个位置进行消费
      }

      // 判断一下是否需要按时间订阅
      if (StringUtils.isEmpty(entryPosition.getJournalName())) {
        // 如果没有指定binlogName,尝试按照timestamp进行查找
        if (entryPosition.getTimestamp() != null && entryPosition.getTimestamp() > 0L) {
          logger.warn(
              "prepare to find start position {}:{}:{}",
              new Object[] {"", "", entryPosition.getTimestamp()});
          return findByStartTimeStamp(mysqlConnection, entryPosition.getTimestamp());
        } else {
          logger.warn("prepare to find start position just show master status");
          return findEndPosition(mysqlConnection); // 默认从当前最后一个位置进行消费
        }
      } else {
        if (entryPosition.getPosition() != null && entryPosition.getPosition() > 0L) {
          // 如果指定binlogName + offest,直接返回
          logger.warn(
              "prepare to find start position {}:{}:{}",
              new Object[] {entryPosition.getJournalName(), entryPosition.getPosition(), ""});
          return entryPosition;
        } else {
          EntryPosition specificLogFilePosition = null;
          if (entryPosition.getTimestamp() != null && entryPosition.getTimestamp() > 0L) {
            // 如果指定binlogName + timestamp,但没有指定对应的offest,尝试根据时间找一下offest
            EntryPosition endPosition = findEndPosition(mysqlConnection);
            if (endPosition != null) {
              logger.warn(
                  "prepare to find start position {}:{}:{}",
                  new Object[] {entryPosition.getJournalName(), "", entryPosition.getTimestamp()});
              specificLogFilePosition =
                  findAsPerTimestampInSpecificLogFile(
                      mysqlConnection,
                      entryPosition.getTimestamp(),
                      endPosition,
                      entryPosition.getJournalName());
            }
          }

          if (specificLogFilePosition == null) {
            // position不存在,从文件头开始
            entryPosition.setPosition(BINLOG_START_OFFEST);
            return entryPosition;
          } else {
            return specificLogFilePosition;
          }
        }
      }
    } else {
      if (logPosition
          .getIdentity()
          .getSourceAddress()
          .equals(mysqlConnection.getConnector().getAddress())) {
        logger.warn("prepare to find start position just last position");
        return logPosition.getPostion();
      } else {
        // 针对切换的情况,考虑回退时间
        long newStartTimestamp =
            logPosition.getPostion().getTimestamp() - fallbackIntervalInSeconds * 1000;
        logger.warn(
            "prepare to find start position by switch {}:{}:{}",
            new Object[] {"", "", logPosition.getPostion().getTimestamp()});
        return findByStartTimeStamp(mysqlConnection, newStartTimestamp);
      }
    }
  }