下面列出了怎么用org.apache.zookeeper.server.Request的API类实例代码及写法,或者点击链接到github查看源代码。
@Around("processRequest()")
public void timedProcessRequest(ProceedingJoinPoint joinPoint) throws Throwable {
joinPoint.proceed();
Request request = (Request) joinPoint.getArgs()[0];
String type = requestTypeMap.getOrDefault(request.type, "unknown");
requests.labels(type).inc();
long latencyMs = Time.currentElapsedTime() - request.createTime;
String latencyLabel = isWriteRequest(request.type) ? "write" : "read";
requestsLatency.labels(latencyLabel).observe(latencyMs);
}
@Override
public void submitRequest(Request si)
{
long remaining = firstError != 0 ? LOCKOUT_DURATION_MS - (System.currentTimeMillis() - firstError) : 0;
if ( si.type != ZooDefs.OpCode.createSession && si.type != ZooDefs.OpCode.sync && si.type != ZooDefs.OpCode.ping
&& firstError != 0 && remaining > 0 )
{
log.debug("Rejected : " + si.toString());
// Still reject request
log.debug("Still not ready for " + remaining + "ms");
((NIOServerCnxn)si.cnxn).close();
return;
}
// Submit the request to the legacy Zookeeper server
log.debug("Applied : " + si.toString());
super.submitRequest(si);
// Raise an error if a lock is created
if ( si.type == ZooDefs.OpCode.create )
{
CreateRequest createRequest = new CreateRequest();
try
{
ByteBuffer duplicate = si.request.duplicate();
duplicate.rewind();
ByteBufferInputStream.byteBuffer2Record(duplicate, createRequest);
if ( createRequest.getPath().startsWith(CHAOS_ZNODE_PREFIX)
&& firstError == 0 )
{
firstError = System.currentTimeMillis();
// The znode has been created, close the connection and don't tell it to client
log.warn("Closing connection right after " + createRequest.getPath() + " creation");
((NIOServerCnxn)si.cnxn).close();
}
}
catch ( Exception e )
{
// Should not happen
((NIOServerCnxn)si.cnxn).close();
}
}
}
@Override
public void addCommittedProposal(Request request) {
zkdb.addCommittedProposal(request);
}
@Override
public boolean append(Request si) throws IOException {
return zkdb.append(si);
}
@Override
public void submitRequest(Request si)
{
long remaining = firstError != 0 ? LOCKOUT_DURATION_MS - (System.currentTimeMillis() - firstError) : 0;
if ( si.type != ZooDefs.OpCode.createSession && si.type != ZooDefs.OpCode.sync && si.type != ZooDefs.OpCode.ping
&& firstError != 0 && remaining > 0 )
{
log.debug("Rejected : " + si.toString());
// Still reject request
log.debug("Still not ready for " + remaining + "ms");
Compatibility.serverCnxnClose(si.cnxn);
return;
}
// Submit the request to the legacy Zookeeper server
log.debug("Applied : " + si.toString());
super.submitRequest(si);
// Raise an error if a lock is created
if ( (si.type == ZooDefs.OpCode.create) || (si.type == ZooDefs.OpCode.create2) )
{
CreateRequest createRequest = new CreateRequest();
try
{
ByteBuffer duplicate = si.request.duplicate();
duplicate.rewind();
ByteBufferInputStream.byteBuffer2Record(duplicate, createRequest);
if ( createRequest.getPath().startsWith(CHAOS_ZNODE_PREFIX)
&& firstError == 0 )
{
firstError = System.currentTimeMillis();
// The znode has been created, close the connection and don't tell it to client
log.warn("Closing connection right after " + createRequest.getPath() + " creation");
Compatibility.serverCnxnClose(si.cnxn);
}
}
catch ( Exception e )
{
// Should not happen
Compatibility.serverCnxnClose(si.cnxn);
}
}
}