Skip to content

Commit be0d8d0

Browse files
Jiajun Wanghuizhilu
authored andcommitted
Add various operational metrics
Add the following metrics: * Max/Min/Avg latency for read and write requests. * Total request count for read and write requests. * Connection/session create and close count.
1 parent 642bb52 commit be0d8d0

9 files changed

Lines changed: 391 additions & 26 deletions

File tree

zookeeper-server/src/main/java/org/apache/zookeeper/server/FinalRequestProcessor.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,19 @@ public void processRequest(Request request) {
201201
AuditHelper.addAuditLog(request, rc);
202202
switch (request.type) {
203203
case OpCode.ping: {
204+
zks.serverStats().updateLatency(request.type, request, Time.currentElapsedTime());
205+
204206
lastOp = "PING";
205-
updateStats(request, lastOp, lastZxid);
207+
updateStats(request.type, request, lastOp, lastZxid);
206208

207209
cnxn.sendResponse(new ReplyHeader(ClientCnxn.PING_XID, lastZxid, 0), null, "response");
208210
return;
209211
}
210212
case OpCode.createSession: {
213+
zks.serverStats().updateLatency(request.type, request, Time.currentElapsedTime());
214+
211215
lastOp = "SESS";
212-
updateStats(request, lastOp, lastZxid);
216+
updateStats(request.type, request, lastOp, lastZxid);
213217

214218
zks.finishSessionInit(request.cnxn, true);
215219
return;
@@ -615,7 +619,28 @@ public void processRequest(Request request) {
615619

616620
ReplyHeader hdr = new ReplyHeader(request.cxid, lastZxid, err.intValue());
617621

618-
updateStats(request, lastOp, lastZxid);
622+
int type = request.type;
623+
if (type == OpCode.multi) {
624+
// check if contains only read operation
625+
boolean containsWrite = false;
626+
for (ProcessTxnResult subTxnResult : rc.multiResult) {
627+
switch (subTxnResult.type) {
628+
case OpCode.create:
629+
case OpCode.delete:
630+
case OpCode.setData:
631+
containsWrite = true;
632+
break;
633+
case OpCode.check:
634+
case OpCode.error:
635+
default:
636+
break;
637+
}
638+
}
639+
if (!containsWrite) {
640+
type = OpCode.check;
641+
}
642+
}
643+
updateStats(type, request, lastOp, lastZxid);
619644

620645
try {
621646
if (path == null || rsp == null) {
@@ -694,13 +719,12 @@ public void shutdown() {
694719
LOG.info("shutdown of request processor complete");
695720
}
696721

697-
private void updateStats(Request request, String lastOp, long lastZxid) {
722+
private void updateStats(int type, Request request, String lastOp, long lastZxid) {
698723
if (request.cnxn == null) {
699724
return;
700725
}
701726
long currentTime = Time.currentElapsedTime();
702-
zks.serverStats().updateLatency(request, currentTime);
727+
zks.serverStats().updateLatency(type, request, currentTime);
703728
request.cnxn.updateStatsForResponse(request.cxid, lastZxid, lastOp, request.createTime, currentTime);
704729
}
705-
706730
}

zookeeper-server/src/main/java/org/apache/zookeeper/server/NIOServerCnxnFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ private void addCnxn(NIOServerCnxn cnxn) throws IOException {
830830
set.add(cnxn);
831831

832832
cnxns.add(cnxn);
833+
increaseConnectionCreateCount();
833834
touchCnxn(cnxn);
834835
}
835836

zookeeper-server/src/main/java/org/apache/zookeeper/server/NettyServerCnxnFactory.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -722,15 +722,18 @@ public InetSocketAddress getLocalAddress() {
722722

723723
private void addCnxn(final NettyServerCnxn cnxn) {
724724
cnxns.add(cnxn);
725-
InetAddress addr = ((InetSocketAddress) cnxn.getChannel().remoteAddress()).getAddress();
726-
727-
ipMap.compute(addr, (a, cnxnCount) -> {
728-
if (cnxnCount == null) {
729-
cnxnCount = new AtomicInteger();
730-
}
731-
cnxnCount.incrementAndGet();
732-
return cnxnCount;
733-
});
725+
increaseConnectionCreateCount();
726+
synchronized (ipMap) {
727+
InetAddress addr =
728+
((InetSocketAddress) cnxn.getChannel().remoteAddress()).getAddress();
729+
ipMap.compute(addr, (a, cnxnCount) -> {
730+
if (cnxnCount == null) {
731+
cnxnCount = new AtomicInteger();
732+
}
733+
cnxnCount.incrementAndGet();
734+
return cnxnCount;
735+
});
736+
}
734737
}
735738

736739
void removeCnxnFromIpMap(NettyServerCnxn cnxn, InetAddress remoteAddress) {

zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerCnxnFactory.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,42 @@ public static ServerCnxnFactory createFactory(InetSocketAddress addr, int maxCli
200200

201201
private final ConcurrentHashMap<ServerCnxn, ConnectionBean> connectionBeans = new ConcurrentHashMap<ServerCnxn, ConnectionBean>();
202202

203+
// Connection create count is increased after connection established. But this connection may not establish a session.
204+
// In this case, registerConnection won't be called.
205+
// So the following method need to be called in the implementation class.
206+
public void increaseConnectionCreateCount() {
207+
if (zkServer != null) {
208+
zkServer.serverStats().incrementConnectionCreated();
209+
}
210+
}
211+
212+
private void increaseConnectionCloseCount() {
213+
if (zkServer != null) {
214+
zkServer.serverStats().incrementConnectionClosed();
215+
}
216+
}
217+
218+
private void increaseSessionCreateCount() {
219+
if (zkServer != null) {
220+
zkServer.serverStats().incrementSessionCreated();
221+
}
222+
}
223+
224+
private void increaseSessionCloseCount() {
225+
if (zkServer != null) {
226+
zkServer.serverStats().incrementSessionClosed();
227+
}
228+
}
229+
203230
// Connection set is relied on heavily by four letter commands
204231
// Construct a ConcurrentHashSet using a ConcurrentHashMap
205232
protected final Set<ServerCnxn> cnxns = Collections.newSetFromMap(new ConcurrentHashMap<ServerCnxn, Boolean>());
206233
public void unregisterConnection(ServerCnxn serverCnxn) {
234+
increaseConnectionCloseCount();
207235
ConnectionBean jmxConnectionBean = connectionBeans.remove(serverCnxn);
208236
if (jmxConnectionBean != null) {
209237
MBeanRegistry.getInstance().unregister(jmxConnectionBean);
238+
increaseSessionCloseCount();
210239
}
211240
}
212241

@@ -216,6 +245,7 @@ public void registerConnection(ServerCnxn serverCnxn) {
216245
try {
217246
MBeanRegistry.getInstance().register(jmxConnectionBean, zkServer.jmxServerBean);
218247
connectionBeans.put(serverCnxn, jmxConnectionBean);
248+
increaseSessionCreateCount();
219249
} catch (JMException e) {
220250
LOG.warn("Could not register connection", e);
221251
}

0 commit comments

Comments
 (0)