From 46d29feebed413eda2c2d4c2b51a9f6b57d820c0 Mon Sep 17 00:00:00 2001 From: leon Date: Fri, 16 Sep 2016 15:13:55 +0800 Subject: [PATCH] 1.0.11-SNAPSHOT fix bug --- .../replicator/RedisSocketReplicator.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/moilioncircle/redis/replicator/RedisSocketReplicator.java b/src/main/java/com/moilioncircle/redis/replicator/RedisSocketReplicator.java index f920b47e..0a9790ad 100644 --- a/src/main/java/com/moilioncircle/redis/replicator/RedisSocketReplicator.java +++ b/src/main/java/com/moilioncircle/redis/replicator/RedisSocketReplicator.java @@ -92,6 +92,7 @@ public void open() throws IOException { SyncMode syncMode = trySync(reply); if (syncMode == SyncMode.PSYNC) { //heart beat send REPLCONF ACK ${slave offset} + heartBeat = new Timer("heart beat"); heartBeat.schedule(new TimerTask() { @Override @@ -100,11 +101,10 @@ public void run() { send("REPLCONF".getBytes(), "ACK".getBytes(), String.valueOf(configuration.getOffset()).getBytes()); } catch (IOException e) { //NOP - logger.error("error", e); } - } }, configuration.getHeartBeatDelay(), configuration.getHeartBeatPeriod()); + logger.info("heart beat started."); } //sync command while (connected.get()) { @@ -140,7 +140,7 @@ public void handle(long len) { //connected = false break; } catch (SocketException | SocketTimeoutException | InterruptedException | EOFException e) { - logger.error(e); + logger.error("socket error", e); //close socket manual if (!connected.get()) { break; @@ -152,7 +152,7 @@ public void handle(long len) { //server disconnect connection EOFException close(); //retry psync in next loop. - logger.info("retry connect to redis."); + logger.info("reconnect to redis-server. retry times:" + i); try { Thread.sleep(configuration.getRetryTimeInterval()); } catch (InterruptedException e1) { @@ -305,15 +305,28 @@ private void connect() throws IOException { } @Override - public void close() throws IOException { + public void close() { if (!connected.compareAndSet(true, false)) return; if (heartBeat != null) { heartBeat.cancel(); heartBeat = null; + logger.info("heart beat canceled."); + } + try { + if (inputStream != null) inputStream.close(); + } catch (IOException e) { + //NOP + } + try { + if (outputStream != null) outputStream.close(); + } catch (IOException e) { + //NOP + } + try { + if (socket != null && !socket.isClosed()) socket.close(); + } catch (IOException e) { + //NOP } - if (inputStream != null) inputStream.close(); - if (outputStream != null) outputStream.close(); - if (socket != null && !socket.isClosed()) socket.close(); doCloseListener(); logger.info("channel closed"); }