/** * @param cnxn * @param sessionId * @param xid * @param bb */ private void submitRequest(ServerCnxn cnxn, long sessionId, int type, int xid, ByteBuffer bb, List<Id> authInfo) { Request si = new Request(cnxn, sessionId, xid, type, bb, authInfo); submitRequest(si); }
private Request makeUpgradeRequest(long sessionId) { // Make sure to atomically check local session status, upgrade // session, and make the session creation request. This is to // avoid another thread upgrading the session in parallel. synchronized (upgradeableSessionTracker) { if (upgradeableSessionTracker.isLocalSession(sessionId)) { int timeout = upgradeableSessionTracker.upgradeSession(sessionId); ByteBuffer to = ByteBuffer.allocate(4); to.putInt(timeout); return new Request( null, sessionId, 0, OpCode.createSession, to, null); } } return null; }
private void close(long sessionId) { Request si = new Request(null, sessionId, 0, OpCode.closeSession, null, null); setLocalSessionFlag(si); submitRequest(si); }
long createSession(ServerCnxn cnxn, byte passwd[], int timeout) { if (passwd == null) { // Possible since it's just deserialized from a packet on the wire. passwd = new byte[0]; } long sessionId = sessionTracker.createSession(timeout); Random r = new Random(sessionId ^ superSecret); r.nextBytes(passwd); ByteBuffer to = ByteBuffer.allocate(4); to.putInt(timeout); cnxn.setSessionId(sessionId); Request si = new Request(cnxn, sessionId, 0, OpCode.createSession, to, null); setLocalSessionFlag(si); submitRequest(si); return sessionId; }
public void logRequest(TxnHeader hdr, Record txn) { Request request = new Request(hdr.getClientId(), hdr.getCxid(), hdr.getType(), hdr, txn, hdr.getZxid()); if ((request.zxid & 0xffffffffL) != 0) { pendingTxns.add(request); } syncProcessor.processRequest(request); }
private Request newRequest(Record rec, int type, int sessionId, int xid) throws IOException { ByteArrayOutputStream boas = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(boas); rec.serialize(boa, "request"); ByteBuffer bb = ByteBuffer.wrap(boas.toByteArray()); return new Request(null, sessionId, xid, type, bb, new ArrayList<Id>()); }
private Request createRequest(Record record, int opCode) throws IOException { // encoding ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); record.serialize(boa, "request"); baos.close(); // Id List<Id> ids = Arrays.asList(Ids.ANYONE_ID_UNSAFE); return new Request(null, 1l, 0, opCode, ByteBuffer.wrap(baos.toByteArray()), ids); }
@Override public void shutdown() { /** * Add a request so that something is there for SyncRequestProcessor * to process, while we are in shutdown flow */ Request request = new Request(null, 0, 0, ZooDefs.OpCode.delete, ByteBuffer.wrap("/deadLockIssue".getBytes()), null); processRequest(request); super.shutdown(); } }
private void addCommittedProposal(TxnHeader hdr, Record txn) { Request r = new Request(0, hdr.getCxid(), hdr.getType(), hdr, txn, hdr.getZxid()); addCommittedProposal(r); }
private Request makeGetDataRequest(String path, long sessionId) throws IOException { ByteArrayOutputStream boas = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(boas); GetDataRequest getDataRequest = new GetDataRequest(path, false); getDataRequest.serialize(boa, "request"); ByteBuffer bb = ByteBuffer.wrap(boas.toByteArray()); return new Request(null, sessionId, 1, ZooDefs.OpCode.getData, bb, new ArrayList<Id>()); }
private Request makeCreateRequest(String path, long sessionId) throws IOException { ByteArrayOutputStream boas = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(boas); CreateRequest createRequest = new CreateRequest(path, "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL.toFlag()); createRequest.serialize(boa, "request"); ByteBuffer bb = ByteBuffer.wrap(boas.toByteArray()); return new Request(null, sessionId, 1, ZooDefs.OpCode.create2, bb, new ArrayList<Id>()); }
private Request createEphemeralRequest(String path, long sessionId) throws IOException { ByteArrayOutputStream boas = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(boas); CreateRequest createRequest = new CreateRequest(path, "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL.toFlag()); createRequest.serialize(boa, "request"); ByteBuffer bb = ByteBuffer.wrap(boas.toByteArray()); return new Request(null, sessionId, 1, ZooDefs.OpCode.create2, bb, new ArrayList<Id>()); }
public void sendReadRequest() throws Exception { ByteArrayOutputStream boas = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(boas); GetDataRequest getDataRequest = new GetDataRequest( "/session" + Long.toHexString(sessionId) + "-" + nodeId, false); getDataRequest.serialize(boa, "request"); ByteBuffer bb = ByteBuffer.wrap(boas.toByteArray()); Request req = new Request(null, sessionId, ++cxid, OpCode.getData, bb, new ArrayList<Id>()); zks.getFirstProcessor().processRequest(req); }
public void sendWriteRequest() throws Exception { ByteArrayOutputStream boas = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(boas); CreateRequest createReq = new CreateRequest( "/session" + Long.toHexString(sessionId) + "-" + (++nodeId), new byte[0], Ids.OPEN_ACL_UNSAFE, 1); createReq.serialize(boa, "request"); ByteBuffer bb = ByteBuffer.wrap(boas.toByteArray()); Request req = new Request(null, sessionId, ++cxid, OpCode.create, bb, new ArrayList<Id>()); zks.getFirstProcessor().processRequest(req); }
@Test public void testSerializeRequestRequestHeaderIsNull() { Request request = new Request(0, 0, 0, null, null, 0); byte[] data = SerializeUtils.serializeRequest(request); assertNull(data); }
private void append(ZKDatabase zkdb, int i) throws IOException { TxnHeader hdr = new TxnHeader(1, 1, i, 1, ZooDefs.OpCode.setData); Record txn = new SetDataTxn("/foo" + i, new byte[0], 1); Request req = new Request(0, 0, 0, hdr, txn, 0); zkdb.append(req); zkdb.commit(); }
@Test public void testPRequest() throws Exception { pLatch = new CountDownLatch(1); processor = new PrepRequestProcessor(zks, new MyRequestProcessor()); Request foo = new Request(null, 1l, 1, OpCode.create, ByteBuffer.allocate(3), null); processor.pRequest(foo); Assert.assertEquals("Request should have marshalling error", new ErrorTxn(KeeperException.Code.MARSHALLINGERROR.intValue()), outcome.getTxn()); Assert.assertTrue("request hasn't been processed in chain", pLatch.await(5, TimeUnit.SECONDS)); }
@Test public void testLatencyMetrics() { // Given ... ServerStats serverStats = new ServerStats(providerMock); // When incremented... Request fakeRequest = new Request(0, 0, 0, null, null, 0); serverStats.updateLatency(fakeRequest, fakeRequest.createTime + 1000); serverStats.updateLatency(fakeRequest, fakeRequest.createTime + 2000); // Then ... assertThat("Max latency check", 2000L, lessThanOrEqualTo(serverStats.getMaxLatency())); assertThat("Min latency check", 1000L, lessThanOrEqualTo(serverStats.getMinLatency())); Assert.assertEquals((double)1500, serverStats.getAvgLatency(), (double)200); // When reset... serverStats.resetLatency(); // Then ... assertAllLatencyZero(serverStats); }
@Test public void testGetTxnLogSyncElapsedTime() throws IOException { FileTxnSnapLog fileTxnSnapLog = createFileTxnSnapLogWithAutoCreateDataDir(logDir, snapDir, "true"); TxnHeader hdr = new TxnHeader(1, 1, 1, 1, ZooDefs.OpCode.setData); Record txn = new SetDataTxn("/foo", new byte[0], 1); Request req = new Request(0, 0, 0, hdr, txn, 0); try { fileTxnSnapLog.append(req); fileTxnSnapLog.commit(); long syncElapsedTime = fileTxnSnapLog.getTxnLogElapsedSyncTime(); Assert.assertNotEquals("Did not update syncElapsedTime!", -1L, syncElapsedTime); } finally { fileTxnSnapLog.close(); } }
@Test public void testReset() { // Given ... ServerStats serverStats = new ServerStats(providerMock); assertAllPacketsZero(serverStats); assertAllLatencyZero(serverStats); // When ... Request fakeRequest = new Request(0, 0, 0, null, null, 0); serverStats.incrementPacketsSent(); serverStats.incrementPacketsReceived(); serverStats.updateLatency(fakeRequest, fakeRequest.createTime + 1000); serverStats.reset(); // Then ... assertAllPacketsZero(serverStats); assertAllLatencyZero(serverStats); }