diff --git a/src/main/java/com/moilioncircle/redis/replicator/AbstractReplicator.java b/src/main/java/com/moilioncircle/redis/replicator/AbstractReplicator.java index a24a96b9..fa5372e5 100644 --- a/src/main/java/com/moilioncircle/redis/replicator/AbstractReplicator.java +++ b/src/main/java/com/moilioncircle/redis/replicator/AbstractReplicator.java @@ -34,6 +34,7 @@ public abstract class AbstractReplicator implements Replicator { protected RedisInputStream inputStream; protected BlockingQueue eventQueue; + protected Configuration configuration; protected final ConcurrentHashMap> commands = new ConcurrentHashMap<>(); protected final List filters = new CopyOnWriteArrayList<>(); protected final List listeners = new CopyOnWriteArrayList<>(); @@ -140,6 +141,11 @@ public void submitEvent(Object object) throws InterruptedException { eventQueue.put(object); } + @Override + public boolean verbose() { + return configuration == null ? false : configuration.isVerbose(); + } + @Override public void buildInCommandParserRegister() { addCommandParser(CommandName.name("PING"), new PingParser()); diff --git a/src/main/java/com/moilioncircle/redis/replicator/Configuration.java b/src/main/java/com/moilioncircle/redis/replicator/Configuration.java index 9b8442b2..812c75df 100644 --- a/src/main/java/com/moilioncircle/redis/replicator/Configuration.java +++ b/src/main/java/com/moilioncircle/redis/replicator/Configuration.java @@ -78,6 +78,11 @@ public static Configuration defaultSetting() { */ private int eventQueueSize = 1000; + /** + * trace event log + */ + private boolean verbose = false; + /** * psync master run id */ @@ -191,4 +196,31 @@ public Configuration setEventQueueSize(int eventQueueSize) { this.eventQueueSize = eventQueueSize; return this; } + + public boolean isVerbose() { + return verbose; + } + + public Configuration setVerbose(boolean verbose) { + this.verbose = verbose; + return this; + } + + @Override + public String toString() { + return "Configuration{" + + "connectionTimeout=" + connectionTimeout + + ", readTimeout=" + readTimeout + + ", receiveBufferSize=" + receiveBufferSize + + ", sendBufferSize=" + sendBufferSize + + ", retries=" + retries + + ", bufferSize=" + bufferSize + + ", authPassword='" + authPassword + '\'' + + ", discardRdbEvent=" + discardRdbEvent + + ", eventQueueSize=" + eventQueueSize + + ", verbose=" + verbose + + ", masterRunId='" + masterRunId + '\'' + + ", offset=" + offset + + '}'; + } } diff --git a/src/main/java/com/moilioncircle/redis/replicator/RedisFileReplicator.java b/src/main/java/com/moilioncircle/redis/replicator/RedisFileReplicator.java index 844117a0..310bbe5d 100644 --- a/src/main/java/com/moilioncircle/redis/replicator/RedisFileReplicator.java +++ b/src/main/java/com/moilioncircle/redis/replicator/RedisFileReplicator.java @@ -32,8 +32,9 @@ public RedisFileReplicator(File file, Configuration configuration) throws FileNo } public RedisFileReplicator(InputStream in, Configuration configuration) { - this.inputStream = new RedisInputStream(in, configuration.getBufferSize()); - this.eventQueue = new ArrayBlockingQueue<>(configuration.getEventQueueSize()); + this.configuration = configuration; + this.inputStream = new RedisInputStream(in, this.configuration.getBufferSize()); + this.eventQueue = new ArrayBlockingQueue<>(this.configuration.getEventQueueSize()); } @Override diff --git a/src/main/java/com/moilioncircle/redis/replicator/RedisReplicator.java b/src/main/java/com/moilioncircle/redis/replicator/RedisReplicator.java index 92196567..72edfa69 100644 --- a/src/main/java/com/moilioncircle/redis/replicator/RedisReplicator.java +++ b/src/main/java/com/moilioncircle/redis/replicator/RedisReplicator.java @@ -134,6 +134,11 @@ public void submitEvent(Object object) throws InterruptedException { replicator.submitEvent(object); } + @Override + public boolean verbose() { + return replicator.verbose(); + } + @Override public void open() throws IOException { replicator.open(); diff --git a/src/main/java/com/moilioncircle/redis/replicator/RedisSocketReplicator.java b/src/main/java/com/moilioncircle/redis/replicator/RedisSocketReplicator.java index c2f17230..93af69b3 100644 --- a/src/main/java/com/moilioncircle/redis/replicator/RedisSocketReplicator.java +++ b/src/main/java/com/moilioncircle/redis/replicator/RedisSocketReplicator.java @@ -46,7 +46,6 @@ class RedisSocketReplicator extends AbstractReplicator { private final String host; private final int port; - private final Configuration configuration; private RedisOutputStream outputStream; private Socket socket; private ReplyParser replyParser; @@ -115,7 +114,8 @@ public void handle(long len) { }); //command if (obj instanceof Object[]) { - if (logger.isDebugEnabled()) logger.debug(Arrays.deepToString((Object[]) obj)); + if (configuration.isVerbose() && logger.isDebugEnabled()) + logger.debug(Arrays.deepToString((Object[]) obj)); Object[] command = (Object[]) obj; CommandName cmdName = CommandName.name((String) command[0]); @@ -130,7 +130,7 @@ public void handle(long len) { Command parsedCommand = operations.parse(cmdName, params); //submit event - this.eventQueue.put(parsedCommand); + this.submitEvent(parsedCommand); } else { if (logger.isInfoEnabled()) logger.info("Redis reply:" + obj); } @@ -183,7 +183,7 @@ private void parseDump(final AbstractReplicator replicator) throws IOException { String reply = (String) replyParser.parse(new BulkReplyHandler() { @Override public String handle(long len, RedisInputStream in) throws IOException { - if (logger.isDebugEnabled()) logger.debug("RDB dump file size:" + len); + logger.info("RDB dump file size:" + len); if (configuration.isDiscardRdbEvent()) { logger.info("Discard " + len + " bytes"); in.skip(len); @@ -195,7 +195,8 @@ public String handle(long len, RedisInputStream in) throws IOException { } }); //sync command - if (!reply.equals("OK")) throw new AssertionError("SYNC failed." + reply); + if (reply.equals("OK")) return; + throw new AssertionError("SYNC failed." + reply); } private void auth(String password) throws IOException { @@ -305,7 +306,7 @@ public void close() throws IOException { heartBeat.cancel(); heartBeat = null; } - if (logger.isInfoEnabled()) logger.info("channel closed"); + logger.info("channel closed"); } private enum SyncMode {SYNC, PSYNC} diff --git a/src/main/java/com/moilioncircle/redis/replicator/Replicator.java b/src/main/java/com/moilioncircle/redis/replicator/Replicator.java index edea950f..d5ed6e42 100644 --- a/src/main/java/com/moilioncircle/redis/replicator/Replicator.java +++ b/src/main/java/com/moilioncircle/redis/replicator/Replicator.java @@ -65,5 +65,7 @@ public interface Replicator extends Closeable { void submitEvent(Object object) throws InterruptedException; + boolean verbose(); + void open() throws IOException; } diff --git a/src/main/java/com/moilioncircle/redis/replicator/rdb/Rdb6Parser.java b/src/main/java/com/moilioncircle/redis/replicator/rdb/Rdb6Parser.java index 1c25514f..9f8b5e05 100644 --- a/src/main/java/com/moilioncircle/redis/replicator/rdb/Rdb6Parser.java +++ b/src/main/java/com/moilioncircle/redis/replicator/rdb/Rdb6Parser.java @@ -109,7 +109,7 @@ protected long rdbLoad() throws IOException, InterruptedException { throw new AssertionError("Un-except value-type:" + type); } if (kv == null) continue; - if (logger.isDebugEnabled()) logger.debug(kv); + if (replicator.verbose() && logger.isDebugEnabled()) logger.debug(kv); //submit event this.replicator.submitEvent(kv); } diff --git a/src/main/java/com/moilioncircle/redis/replicator/rdb/Rdb7Parser.java b/src/main/java/com/moilioncircle/redis/replicator/rdb/Rdb7Parser.java index ffb062e5..ce758446 100644 --- a/src/main/java/com/moilioncircle/redis/replicator/rdb/Rdb7Parser.java +++ b/src/main/java/com/moilioncircle/redis/replicator/rdb/Rdb7Parser.java @@ -125,7 +125,7 @@ protected long rdbLoad() throws IOException, InterruptedException { throw new AssertionError("Un-except value-type:" + type); } if (kv == null) continue; - if (logger.isDebugEnabled()) logger.debug(kv); + if (replicator.verbose() && logger.isDebugEnabled()) logger.debug(kv); //submit event this.replicator.submitEvent(kv); }