@Override
  public boolean registgerTableReplicator(DBConnectorConfig master) {
    try {
      tables.clear();
      ConfigGetHelper conf = new ConfigGetHelper(SchnauzerRunner.SchnauzerID);
      conf.getRedisTables(tables);
      MasterDBHelper mdbhelper = new MasterDBHelper(master);
      for (int i = 0; i < this.tables.size(); i++) {
        RedisSchnauzer table = (RedisSchnauzer) this.tables.get(i);
        ResultSet rs = mdbhelper.getTableFields(table.getMasterTableName());
        Boolean haveCol = false;
        while (rs.next()) {
          haveCol = true;
          RepField field = new RepField(rs.getString("column_name"), "", "");
          String characterset = rs.getString("character_set_name");
          if (characterset != null && !characterset.equals("")) field.characterset = characterset;
          table.addMasterField(field);
        }
        if (!haveCol) {
          ErrorHelper.errExit(
              String.format(Infos.TableNotExist, table.getMasterTableName(), masterDb.dbname));
        }
        rs.close();
        LOGGER.info("regist " + table.table.SlaveKey + Infos.OK);
      }

    } catch (Exception e) {
      LOGGER.error(Infos.GetTableConfigs + Infos.Failed + e.toString());
      return false;
    }
    return true;
  }
示例#2
0
  @Override
  public boolean doUpdate(ColumnTypeHelper helper, UpdateRowsEvent event) {
    String preSql = "";
    String aftSql = "";
    boolean doOne = false;
    String[] sqls = null;
    int sqlsIndex = 0;
    doBeforeUpdate(helper, event);
    for (int i = 0; i < tables.size(); i++) {
      RDBSchnauzer rep = (RDBSchnauzer) tables.get(i);
      if (notEqualS(helper.databaseName, this.masterDb.dbname)) continue;
      if (notEqualS(helper.tableName, rep.getMasterTableName())) continue;
      List<Pair<Row>> rows = event.getRows();
      String[] tableSqls = new String[rows.size() + 1];
      int pos = 1;
      byte[] unsigntags = rep.getUnsignedTags();
      for (int j = 0; j < rows.size(); j++) {
        String sql = "Update " + rep.getSlaveTableName() + "  set ";
        Pair<Row> pRow = rows.get(j);
        Row row = pRow.getAfter();
        Row preRow = pRow.getBefore();

        List<Column> columns = row.getColumns();
        List<Column> pcolumns = preRow.getColumns();
        if (!rep.needReplicate(columns)) continue;
        try {
          preSql =
              helper.getUpdataStr(pcolumns, rep.getColumnNames(), unsigntags, rep.getFullFields());
          aftSql =
              helper.getUpdataStr(columns, rep.getColumnNames(), unsigntags, rep.getFullFields());
        } catch (Exception e) {
          mailAndLog(Infos.Create + Infos.DoUpdate + Infos.Failed, e.getMessage());
          return false;
        }

        if (isEqualS(preSql, aftSql)) continue;
        sql += aftSql;
        try {
          sql += " where " + getWhereStr(rep, columns, helper);
        } catch (Exception e) {
          mailAndLog(Infos.Create + Infos.DoUpdate + Infos.Failed, e.getMessage());
          return false;
        }

        tableSqls[pos++] = sql;
        doOne = true;
      }
      if (sqls == null) {
        sqls = new String[tables.size() * rows.size() + 1];
        for (int k = 0; k < sqls.length; k++) sqls[k] = "";
        sqls[0] = getStatusUpdateSql(helper);
      }
      int sIndex = 0;
      for (int j = 0; j < tableSqls.length; j++) {
        sIndex = (sqlsIndex + 1);
        if ((tableSqls[j] == null) || (tableSqls[j].isEmpty())) continue;
        if ((sqls[sIndex] != null) && (!sqls[sIndex].isEmpty())) sqls[i + 1] += ";";
        sqls[++sqlsIndex] += tableSqls[j];
      }
    }

    String[] errInfo = new String[1];

    if (!doOne || !logSql(sqls)) {
      autoSetPosition(helper);
      return true;
    }

    if (dbhelper.excuteSqlByTransaction(sqls, errInfo)) {
      this.slaveDb.binlog = this.getBinLogName(helper);
      this.slaveDb.pos = helper.position;
      this.slaveDb.tablepos = helper.tableMapPos;
      LOGGER.info(Infos.DoUpdate + Infos.OK);
      return true;
    }

    String sInfo = getWarning(sqls, errInfo);
    mailAndLog(Infos.DoUpdate + Infos.Failed, sInfo);
    this.slaveDb.errorMsg = "Update command faild";
    HeartBeatInfo hinfo = new HeartBeatInfo();
    ConfigGetHelper conf = new ConfigGetHelper(Integer.toString(this.slaveDb.masterID));
    conf.getHeartBeatSet(hinfo);
    if (hinfo.port > 0 && !hinfo.host.isEmpty()) {
      LOGGER.info(Infos.SendErrorInfo);
      LocalInfo info = LocalInfoGetter.getLocalInfo();
      hinfo.SerialNo = info.getSNStr();
      HeartBeatSender beatSender = new HeartBeatSender(this.masterDb, this.slaveDb, hinfo);
      beatSender.sendError();
    }
    rollBackToTableEvent(helper);
    return false;
  }