private void doInitiate() { try { dbhelper.flushDataDB(); LOGGER.info("Redis flushdb " + Infos.OK); String sql = ""; MySQLDbHelper mhelper = new MySQLDbHelper(masterDb); for (int i = 0; i < tables.size(); i++) { RedisSchnauzer table = tables.get(i); sql = table.getSQL(); if (StrHelp.empty(sql)) ErrorHelper.errExit(Infos.SQLNotFound + table.table.SlaveKey); ResultSet rs = mhelper.getRS(sql); switch (table.getType()) { case Set: case SortedSet: case String: initSet(table, rs); break; default: ErrorHelper.errExit(LOGGER.getName() + " '" + table.getType() + "' Not Impliment...."); } } } catch (Exception e) { ErrorHelper.errExit(Infos.Init + "RedisData" + Infos.Failed + ": " + e.getMessage()); } }
private void initSet(RedisSchnauzer table, ResultSet rs) { try { rs.first(); String preKey = table.table.SlaveKey; String key = ""; String member = ""; Double score = 0.0; String value = ""; int rowCount = 0; Boolean haveKey = table.table.haveKeyFields(); rs.beforeFirst(); RedisStructure type = table.getType(); while (rs.next()) { switch (type) { case Set: case SortedSet: if (haveKey) { key = rs.getString(1); member = rs.getString(2); score = rs.getDouble(3); } else { key = ""; member = rs.getString(1); score = rs.getDouble(2); } dbhelper.zincrby(preKey + key, score, member); break; case String: key = rs.getString(1); value = rs.getString(2); dbhelper.set(preKey + key, value); break; default: ErrorHelper.errExit(LOGGER.getName() + " '" + table.getType() + "' Not Impliment...."); } rowCount = rowCount + 1; } LOGGER.info("load " + preKey + " OK. rowcount=" + rowCount); rs.close(); } catch (Exception e) { ErrorHelper.errExit( LOGGER.getName() + Infos.Init + "RedisData" + Infos.Failed + ": " + table.table.SlaveKey + ": " + e.getMessage()); } }
@Override public boolean doWrite(ColumnTypeHelper helper, WriteRowsEvent event) { try { boolean doOne = false; for (int i = 0; i < tables.size(); i++) { RedisSchnauzer table = tables.get(i); if (StrHelp.notEqual(helper.databaseName, masterDb.dbname)) continue; if (StrHelp.notEqual(helper.tableName, table.getMasterTableName())) continue; doOne = true; try { innerWrite(helper, event, table); } catch (Exception e) { ErrorHelper.errExit( "[" + table.table.SlaveKey + "]" + Infos.DoInsert + Infos.Failed + e.getMessage()); } } if (!doOne) dbhelper.setBinlogKey( getBinLogName(helper), Long.toString(helper.position), helper.tableName); } catch (Exception e) { ErrorHelper.errExit(Infos.RepFailed + e.getMessage()); } return true; }
@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; }
private void getTheLastBinlogAndPos() { try { MySQLDbHelper mhelper = new MySQLDbHelper(masterDb); ResultSet rs = mhelper.getRS("show master logs"); String binlog = ""; Long pos = 0L; while (rs.next()) { binlog = rs.getString("log_name"); } rs.close(); rs = mhelper.getRS("show binlog events in '" + binlog + "'"); while (rs.next()) { pos = rs.getLong("pos"); } rs.close(); masterDb.binlog = binlog; masterDb.pos = pos; slaveDb.binlog = binlog; slaveDb.pos = pos; } catch (Exception e) { ErrorHelper.errExit("Get binlog filename " + Infos.Failed + ": " + e.getMessage()); } }
private void innerDelete(ColumnTypeHelper helper, DeleteRowsEvent event, RedisSchnauzer table) throws UnsupportedEncodingException { List<Row> rows = event.getRows(); LOGGER.info(rows.size() + Infos.Row); int scoreIndex = table.getMemberIndex(); int memberIndex = table.getScoreIndex(); int valueIndex = table.getValueIndex(); Double score = 0.0; String member = ""; String value = ""; for (int j = 0; j < rows.size(); j++) { Row row = rows.get(j); List<Column> columns = row.getColumns(); if (!table.needReplicate(columns, helper)) continue; switch (table.getType()) { case String: dbhelper.delete(table.getKey(columns)); dbhelper.setBinlogKey( getBinLogName(helper), Long.toString(helper.position), helper.tableName); break; case Set: score = -1.00; value = helper.getColStr( valueIndex, columns.get(valueIndex), (byte) 1, table.masterfields.get(valueIndex)); dbhelper.zincrby(table.getKey(columns), score, value); dbhelper.setBinlogKey( getBinLogName(helper), Long.toString(helper.position), helper.tableName); break; case SortedSet: score = Double.parseDouble( helper.getColStr( scoreIndex, columns.get(scoreIndex), (byte) 1, table.masterfields.get(scoreIndex))) * table.scorefield.multiplier; member = helper.getColStr( memberIndex, columns.get(memberIndex), (byte) 1, table.masterfields.get(memberIndex)); dbhelper.zincrby(table.getKey(columns), -1 * score, member); dbhelper.setBinlogKey( getBinLogName(helper), Long.toString(helper.position), helper.tableName); break; case List: ErrorHelper.errExit( "innerDelete====" + Infos.Illegal + "RedisStructure:" + table.getType().toString()); break; default: ErrorHelper.errExit( "innerDelete====" + Infos.Illegal + "RedisStructure:" + table.getType().toString()); break; } } }