#603 RaftCore log format

This commit is contained in:
nkorange 2019-01-10 15:50:06 +08:00
parent 21f811e77b
commit 0b7649880a

View File

@ -31,6 +31,7 @@ import org.javatuples.Pair;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
@ -104,9 +105,8 @@ public class RaftCore {
RaftStore.load();
Loggers.RAFT.info("cache loaded, peer count: " + peers.size()
+ ", datum count: " + datums.size()
+ ", current term:" + peers.getTerm());
Loggers.RAFT.info("cache loaded, peer count: {}, datum count: {}, current term: {}",
peers.size(), datums.size(), peers.getTerm());
while (true) {
if (notifier.tasks.size() <= 0) {
@ -116,7 +116,7 @@ public class RaftCore {
System.out.println(notifier.tasks.size());
}
Loggers.RAFT.info("finish to load data from disk,cost: " + (System.currentTimeMillis() - start) + " ms.");
Loggers.RAFT.info("finish to load data from disk,cost: {} ms.", (System.currentTimeMillis() - start));
GlobalExecutor.register(new MasterElection());
GlobalExecutor.register1(new HeartBeat());
@ -131,8 +131,8 @@ public class RaftCore {
throw new Exception("peers is empty.");
}
Loggers.RAFT.info("timer started: leader timeout ms: " + GlobalExecutor.LEADER_TIMEOUT_MS
+ "; heart-beat timeout ms: " + GlobalExecutor.HEARTBEAT_INTVERAL_MS);
Loggers.RAFT.info("timer started: leader timeout ms: {}, heart-beat timeout ms: {}",
GlobalExecutor.LEADER_TIMEOUT_MS, GlobalExecutor.HEARTBEAT_INTVERAL_MS);
}
public static List<RaftListener> getListeners() {
@ -208,7 +208,7 @@ public class RaftCore {
@Override
public Integer onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
Loggers.RAFT.warn("RAFT", "failed to publish data to peer, datumId=" + datum.key + ", peer=" + server + ", http code=" + response.getStatusCode());
Loggers.RAFT.warn("[RAFT] failed to publish data to peer, datumId={}, peer={}, http code={}", datum.key, server, response.getStatusCode());
return 1;
}
return 0;
@ -223,7 +223,7 @@ public class RaftCore {
}
long end = System.currentTimeMillis();
Loggers.RAFT.info("signalPublish cost " + (end - start) + " ms" + " : " + key);
Loggers.RAFT.info("signalPublish cost {} ms, key: {}", (end - start), key);
}
@ -284,7 +284,8 @@ public class RaftCore {
@Override
public Integer onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
Loggers.RAFT.warn("RAFT", "failed to publish data to peer, datumId=" + datum.key + ", peer=" + server + ", http code=" + response.getStatusCode());
Loggers.RAFT.warn("[RAFT] failed to publish data to peer, datumId={}, peer={}, http code={}",
datum.key, server, response.getStatusCode());
return 1;
}
latch.countDown();
@ -301,12 +302,12 @@ public class RaftCore {
if (!latch.await(UtilsAndCommons.RAFT_PUBLISH_TIMEOUT, TimeUnit.MILLISECONDS)) {
// only majority servers return success can we consider this update success
Loggers.RAFT.info("data publish failed, caused failed to notify majority, key=" + key);
Loggers.RAFT.info("data publish failed, caused failed to notify majority, key={}", key);
throw new IllegalStateException("data publish failed, caused failed to notify majority, key=" + key);
}
long end = System.currentTimeMillis();
Loggers.RAFT.info("signalPublish cost " + (end - start) + " ms" + " : " + key);
Loggers.RAFT.info("signalPublish cost {} ms, key: {}", (end - start), key);
} finally {
RaftCore.OPERATE_LOCK.unlock();
}
@ -340,7 +341,7 @@ public class RaftCore {
@Override
public Integer onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
Loggers.RAFT.warn("RAFT", "failed to delete data from peer, datumId=" + key + ", peer=" + server + ", http code=" + response.getStatusCode());
Loggers.RAFT.warn("[RAFT] failed to delete data from peer, datumId={}, peer={}, http code={}", key, server, response.getStatusCode());
return 1;
}
@ -371,15 +372,15 @@ public class RaftCore {
}
if (!peers.isLeader(source.ip)) {
Loggers.RAFT.warn("peer(" + JSON.toJSONString(source) + ") tried to publish " +
"data but wasn't leader, leader: " + JSON.toJSONString(getLeader()));
Loggers.RAFT.warn("peer {} tried to publish data but wasn't leader, leader: {}",
JSON.toJSONString(source), JSON.toJSONString(getLeader()));
throw new IllegalStateException("peer(" + source.ip + ") tried to publish " +
"data but wasn't leader");
}
if (source.term.get() < local.term.get()) {
Loggers.RAFT.warn("out of date publish, pub-term: "
+ JSON.toJSONString(source) + ", cur-term: " + JSON.toJSONString(local));
Loggers.RAFT.warn("out of date publish, pub-term: {}, cur-term: {}",
JSON.toJSONString(source), JSON.toJSONString(local));
throw new IllegalStateException("out of date publish, pub-term:"
+ source.term.get() + ", cur-term: " + local.term.get());
}
@ -390,8 +391,8 @@ public class RaftCore {
if (datumOrigin != null && datumOrigin.timestamp.get() > datum.timestamp.get()) {
// refuse operation:
Loggers.RAFT.warn("out of date publish, pub-timestamp:"
+ datumOrigin.timestamp.get() + ", cur-timestamp: " + datum.timestamp.get());
Loggers.RAFT.warn("out of date publish, pub-timestamp: {}, cur-timestamp: {}",
datumOrigin.timestamp.get(), datum.timestamp.get());
return;
}
@ -419,7 +420,7 @@ public class RaftCore {
notifier.addTask(datum, Notifier.ApplyAction.CHANGE);
Loggers.RAFT.info("data added/updated, key=" + datum.key + ", term: " + local.term);
Loggers.RAFT.info("data added/updated, key={}, term={}", datum.key, local.term);
}
public static void onDelete(JSONObject params) throws Exception {
@ -435,14 +436,14 @@ public class RaftCore {
RaftPeer local = peers.local();
if (!peers.isLeader(source.ip)) {
Loggers.RAFT.warn("peer(" + JSON.toJSONString(source) + ") tried to publish " +
"data but wasn't leader, leader: " + JSON.toJSONString(getLeader()));
Loggers.RAFT.warn("peer {} tried to publish data but wasn't leader, leader: {}",
JSON.toJSONString(source), JSON.toJSONString(getLeader()));
throw new IllegalStateException("peer(" + source.ip + ") tried to publish data but wasn't leader");
}
if (source.term.get() < local.term.get()) {
Loggers.RAFT.warn("out of date publish, pub-term: "
+ JSON.toJSONString(source) + ", cur-term: " + JSON.toJSONString(local));
Loggers.RAFT.warn("out of date publish, pub-term: {}, cur-term: {}",
JSON.toJSONString(source), JSON.toJSONString(local));
throw new IllegalStateException("out of date publish, pub-term:"
+ source.term + ", cur-term: " + local.term);
}
@ -484,7 +485,7 @@ public class RaftCore {
sendVote();
} catch (Exception e) {
Loggers.RAFT.warn("RAFT", "error while master election", e);
Loggers.RAFT.warn("[RAFT] error while master election {}", e);
}
}
@ -496,7 +497,8 @@ public class RaftCore {
}
RaftPeer local = peers.get(NetUtils.localServer());
Loggers.RAFT.info("leader timeout, start voting,leader: " + JSON.toJSONString(getLeader()) + ", term: " + local.term);
Loggers.RAFT.info("leader timeout, start voting,leader: {}, term: {}",
JSON.toJSONString(getLeader()), local.term);
peers.reset();
@ -513,7 +515,7 @@ public class RaftCore {
@Override
public Integer onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
Loggers.RAFT.error("VIPSRV-RAFT", "vote failed: "
Loggers.RAFT.error("NACOS-RAFT {}", "vote failed: "
, response.getResponseBody() + " url:" + url);
return 1;
@ -658,7 +660,7 @@ public class RaftCore {
@Override
public Integer onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
Loggers.RAFT.error("VIPSRV-RAFT", "beat failed: " + response.getResponseBody() + ", peer: " + server);
Loggers.RAFT.error("NACOS-RAFT {}", "beat failed: " + response.getResponseBody() + ", peer: " + server);
return 1;
}
@ -669,11 +671,11 @@ public class RaftCore {
@Override
public void onThrowable(Throwable t) {
Loggers.RAFT.error("VIPSRV-RAFT", "error while sending heart-beat to peer: " + server, t);
Loggers.RAFT.error("NACOS-RAFT {} {}", "error while sending heart-beat to peer: " + server, t);
}
});
} catch (Exception e) {
Loggers.RAFT.error("VIPSRV-RAFT", "error while sending heart-beat to peer: " + server, e);
Loggers.RAFT.error("VIPSRV {} {}", "error while sending heart-beat to peer: " + server, e);
}
}
@ -793,7 +795,7 @@ public class RaftCore {
Datum oldDatum = RaftCore.getDatum(datum.key);
if (oldDatum != null && datum.timestamp.get() <= oldDatum.timestamp.get()) {
Loggers.RAFT.info("[VIPSRV-RAFT] timestamp is smaller than that of mine, key: " + datum.key
Loggers.RAFT.info("[NACOS-RAFT] timestamp is smaller than that of mine, key: " + datum.key
+ ",remote: " + datum.timestamp + ", local: " + oldDatum.timestamp);
continue;
}
@ -835,7 +837,7 @@ public class RaftCore {
batch.clear();
} catch (Exception e) {
Loggers.RAFT.error("VIPSRV-RAFT", "failed to handle beat entry, key=" + datumKey);
Loggers.RAFT.error("NACOS-RAFT {}", "failed to handle beat entry, key=" + datumKey);
}
}
@ -851,7 +853,7 @@ public class RaftCore {
try {
deleteDatum(deadKey);
} catch (Exception e) {
Loggers.RAFT.error("VIPSRV-RAFT", "failed to remove entry, key=" + deadKey, e);
Loggers.RAFT.error("NACOS-RAFT {}", "failed to remove entry, key=" + deadKey, e);
}
}
@ -882,16 +884,16 @@ public class RaftCore {
List<String> newServers = (List<String>) CollectionUtils.subtract(servers, oldServers);
if (!CollectionUtils.isEmpty(newServers)) {
peers.add(newServers);
Loggers.RAFT.info("[RAFT] server list is updated, new (" + newServers.size() + ") servers: " + newServers);
Loggers.RAFT.info("server list is updated, new (" + newServers.size() + ") servers: " + newServers);
}
List<String> deadServers = (List<String>) CollectionUtils.subtract(oldServers, servers);
if (!CollectionUtils.isEmpty(deadServers)) {
peers.remove(deadServers);
Loggers.RAFT.info("[RAFT] server list is updated, dead (" + deadServers.size() + ") servers: " + deadServers);
Loggers.RAFT.info("server list is updated, dead (" + deadServers.size() + ") servers: " + deadServers);
}
} catch (Exception e) {
Loggers.RAFT.info("[RAFT] error while updating server list.", e);
Loggers.RAFT.info("error while updating server list.", e);
}
}
}
@ -916,7 +918,7 @@ public class RaftCore {
Loggers.RAFT.info("add listener for switch or domain meta. ");
}
} else {
Loggers.RAFT.error("VIPSRV-RAFT", "faild to add listener: " + JSON.toJSONString(listener));
Loggers.RAFT.error("NACOS-RAFT {}", "faild to add listener: " + JSON.toJSONString(listener));
}
// if data present, notify immediately
for (Datum datum : datums.values()) {
@ -927,7 +929,7 @@ public class RaftCore {
try {
listener.onChange(datum.key, datum.value);
} catch (Exception e) {
Loggers.RAFT.error("VIPSRV-RAFT", "failed to notify listener", e);
Loggers.RAFT.error("NACOS-RAFT {}", "failed to notify listener", e);
}
}
}
@ -1066,15 +1068,17 @@ public class RaftCore {
continue;
}
} catch (Throwable e) {
Loggers.RAFT.error("VIPSRV-RAFT", "error while notifying listener of key: "
Loggers.RAFT.error("NACOS-RAFT {}", "error while notifying listener of key: "
+ datum.key, e);
}
}
Loggers.RAFT.debug("VIPSRV-RAFT", "datum change notified" +
if (Loggers.RAFT.isDebugEnabled()) {
Loggers.RAFT.debug("NACOS-RAFT {}", "datum change notified" +
", key: " + datum.key + "; listener count: " + count);
}
} catch (Throwable e) {
Loggers.RAFT.error("VIPSRV-RAFT", "Error while handling notifying task", e);
Loggers.RAFT.error("NACOS-RAFT {} {}", "Error while handling notifying task", e);
}
}
}