public void createBB() { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); boa.writeInt(-1, "len"); // We'll fill this in later if (requestHeader != null) { requestHeader.serialize(boa, "header"); } if (request instanceof ConnectRequest) { request.serialize(boa, "connect"); // append "am-I-allowed-to-be-readonly" flag boa.writeBool(readOnly, "readOnly"); } else if (request != null) { request.serialize(boa, "request"); } baos.close(); this.bb = ByteBuffer.wrap(baos.toByteArray()); this.bb.putInt(this.bb.capacity() - 4); this.bb.rewind(); } catch (IOException e) { LOG.warn("Ignoring unexpected exception", e); } }
/** * Create a connection request * * @return */ private ByteBuffer createConnRequest() { Random r = new Random(SESSION_ID ^ superSecret); byte p[] = new byte[16]; r.nextBytes(p); ConnectRequest conReq = new ConnectRequest(0, 1L, 30000, SESSION_ID, p); MockPacket packet = new MockPacket(null, null, conReq, null, null, false); return packet.createAndReturnBB(); }
/** * Create a connection request * * @return a serialized connection request */ private ByteBuffer createConnRequest() { Random r = new Random(SESSION_ID ^ superSecret); byte p[] = new byte[16]; r.nextBytes(p); ConnectRequest conReq = new ConnectRequest(0, 1L, 30000, SESSION_ID, p); MockPacket packet = new MockPacket(null, null, conReq, null, null, false); return packet.createAndReturnBB(); }
Packet(RequestHeader requestHeader, ReplyHeader replyHeader, Record request, Record response, WatchRegistration watchRegistration, boolean readOnly) { this.requestHeader = requestHeader; this.replyHeader = replyHeader; this.request = request; this.response = response; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); boa.writeInt(-1, "len"); // We'll fill this in later if (requestHeader != null) { requestHeader.serialize(boa, "header"); } if (request instanceof ConnectRequest) { request.serialize(boa, "connect"); // append "am-I-allowed-to-be-readonly" flag boa.writeBool(readOnly, "readOnly"); } else if (request != null) { request.serialize(boa, "request"); } baos.close(); this.bb = ByteBuffer.wrap(baos.toByteArray()); this.bb.putInt(this.bb.capacity() - 4); this.bb.rewind(); } catch (IOException e) { LOG.warn("Ignoring unexpected exception", e); } this.watchRegistration = watchRegistration; }
private EventType checkType(Record response) { if (response == null) { return EventType.other; } else if (response instanceof ConnectRequest) { return EventType.write; } else if (response instanceof CreateRequest) { return EventType.write; } else if (response instanceof DeleteRequest) { return EventType.write; } else if (response instanceof SetDataRequest) { return EventType.write; } else if (response instanceof SetACLRequest) { return EventType.write; } else if (response instanceof SetMaxChildrenRequest) { return EventType.write; } else if (response instanceof SetSASLRequest) { return EventType.write; } else if (response instanceof SetWatches) { return EventType.write; } else if (response instanceof SyncRequest) { return EventType.write; } else if (response instanceof ExistsRequest) { return EventType.read; } else if (response instanceof GetDataRequest) { return EventType.read; } else if (response instanceof GetMaxChildrenRequest) { return EventType.read; } else if (response instanceof GetACLRequest) { return EventType.read; } else if (response instanceof GetChildrenRequest) { return EventType.read; } else if (response instanceof GetChildren2Request) { return EventType.read; } else if (response instanceof GetSASLRequest) { return EventType.read; } else { return EventType.other; } }
void primeConnection() throws IOException { LOG.info("Socket connection established to " + clientCnxnSocket.getRemoteSocketAddress() + ", initiating session"); isFirstConnect = false; long sessId = (seenRwServerBefore) ? sessionId : 0; ConnectRequest conReq = new ConnectRequest(0, lastZxid, sessionTimeout, sessId, sessionPasswd); synchronized (outgoingQueue) { // We add backwards since we are pushing into the front // Only send if there's a pending watch // TODO: here we have the only remaining use of zooKeeper in // this class. It's to be eliminated! if (!disableAutoWatchReset) { List<String> dataWatches = zooKeeper.getDataWatches(); List<String> existWatches = zooKeeper.getExistWatches(); List<String> childWatches = zooKeeper.getChildWatches(); if (!dataWatches.isEmpty() || !existWatches.isEmpty() || !childWatches.isEmpty()) { Iterator<String> dataWatchesIter = prependChroot(dataWatches).iterator(); Iterator<String> existWatchesIter = prependChroot(existWatches).iterator(); Iterator<String> childWatchesIter = prependChroot(childWatches).iterator(); long setWatchesLastZxid = lastZxid; while (dataWatchesIter.hasNext() || existWatchesIter.hasNext() || childWatchesIter.hasNext()) { List<String> dataWatchesBatch = new ArrayList<String>(); List<String> existWatchesBatch = new ArrayList<String>(); List<String> childWatchesBatch = new ArrayList<String>(); int batchLength = 0; // Note, we may exceed our max length by a bit when we add the last // watch in the batch. This isn't ideal, but it makes the code simpler. while (batchLength < SET_WATCHES_MAX_LENGTH) { final String watch; if (dataWatchesIter.hasNext()) { watch = dataWatchesIter.next(); dataWatchesBatch.add(watch); } else if (existWatchesIter.hasNext()) { watch = existWatchesIter.next(); existWatchesBatch.add(watch); } else if (childWatchesIter.hasNext()) { watch = childWatchesIter.next(); childWatchesBatch.add(watch); } else { break; } batchLength += watch.length(); } SetWatches sw = new SetWatches(setWatchesLastZxid, dataWatchesBatch, existWatchesBatch, childWatchesBatch); RequestHeader h = new RequestHeader(); h.setType(ZooDefs.OpCode.setWatches); h.setXid(-8); Packet packet = new Packet(h, new ReplyHeader(), sw, null, null); outgoingQueue.addFirst(packet); } } } for (AuthData id : authInfo) { outgoingQueue.addFirst(new Packet(new RequestHeader(-4, OpCode.auth), null, new AuthPacket(0, id.scheme, id.data), null, null)); } outgoingQueue.addFirst(new Packet(null, null, conReq, null, null, readOnly)); } clientCnxnSocket.enableReadWriteOnly(); if (LOG.isDebugEnabled()) { LOG.debug("Session establishment request sent on " + clientCnxnSocket.getRemoteSocketAddress()); } }
/** * Test solution for ZOOKEEPER-1208. Verify that operations are not * accepted after a close session. * * We're using our own marshalling here in order to force an operation * after the session is closed (ZooKeeper.class will not allow this). Also * by filling the pipe with operations it increases the likelyhood that * the server will process the create before FinalRequestProcessor * removes the session from the tracker. */ @Test public void testCreateAfterCloseShouldFail() throws Exception { for (int i = 0; i < 10; i++) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); // open a connection boa.writeInt(44, "len"); ConnectRequest conReq = new ConnectRequest(0, 0, 30000, 0, new byte[16]); conReq.serialize(boa, "connect"); // close connection boa.writeInt(8, "len"); RequestHeader h = new RequestHeader(1, ZooDefs.OpCode.closeSession); h.serialize(boa, "header"); // create ephemeral znode boa.writeInt(52, "len"); // We'll fill this in later RequestHeader header = new RequestHeader(2, OpCode.create); header.serialize(boa, "header"); CreateRequest createReq = new CreateRequest("/foo" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, 1); createReq.serialize(boa, "request"); baos.close(); System.out.println("Length:" + baos.toByteArray().length); String hp[] = hostPort.split(":"); Socket sock = new Socket(hp[0], Integer.parseInt(hp[1])); InputStream resultStream = null; try { OutputStream outstream = sock.getOutputStream(); byte[] data = baos.toByteArray(); outstream.write(data); outstream.flush(); resultStream = sock.getInputStream(); byte[] b = new byte[10000]; int len; while ((len = resultStream.read(b)) >= 0) { // got results System.out.println("gotlen:" + len); } } finally { if (resultStream != null) { resultStream.close(); } sock.close(); } } ZooKeeper zk = createClient(); Assert.assertEquals(1, zk.getChildren("/", false).size()); zk.close(); }
public void run() { SocketChannel sChannel = null; try { /* * For future unwary socket programmers: although connect 'blocks' it * does not require an accept on the server side to return. Therefore * you can not assume that all the sockets are connected at the end of * this for loop. */ sChannel = SocketChannel.open(); sChannel.connect(new InetSocketAddress(host,port)); // Construct a connection request ConnectRequest conReq = new ConnectRequest(0, 0, 10000, 0, "password".getBytes()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); boa.writeInt(-1, "len"); conReq.serialize(boa, "connect"); baos.close(); ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); bb.putInt(bb.capacity() - 4); bb.rewind(); /* Send a connect request. Any socket that has been closed (or at least * not added to the cnxn list on the server) will not have any bytes to * read and get an eof. * * The trick here was finding a call that caused the server to put * bytes in the input stream without closing the cnxn. None of * the four letter commands do that, so we actually try to create * a session which should send us something back, while maintaining * the connection. */ int eof = sChannel.write(bb); // If the socket times out, we count that as Assert.failed - // the server should respond within 10s sChannel.socket().setSoTimeout(10000); if (!sChannel.socket().isClosed()){ eof = sChannel.socket().getInputStream().read(); if (eof != -1) { numConnected.incrementAndGet(); } } } catch (IOException io) { // "Connection reset by peer" } finally { if (sChannel != null) { try { sChannel.close(); } catch (Exception e) { } } } }
/** * Setup session, previous watches, authentication. */ void primeConnection() throws IOException { LOG.info("Socket connection established, initiating session, client: {}, server: {}", clientCnxnSocket.getLocalSocketAddress(), clientCnxnSocket.getRemoteSocketAddress()); isFirstConnect = false; long sessId = (seenRwServerBefore) ? sessionId : 0; ConnectRequest conReq = new ConnectRequest(0, lastZxid, sessionTimeout, sessId, sessionPasswd); // We add backwards since we are pushing into the front // Only send if there's a pending watch // TODO: here we have the only remaining use of zooKeeper in // this class. It's to be eliminated! if (!clientConfig.getBoolean(ZKClientConfig.DISABLE_AUTO_WATCH_RESET)) { List<String> dataWatches = zooKeeper.getDataWatches(); List<String> existWatches = zooKeeper.getExistWatches(); List<String> childWatches = zooKeeper.getChildWatches(); if (!dataWatches.isEmpty() || !existWatches.isEmpty() || !childWatches.isEmpty()) { Iterator<String> dataWatchesIter = prependChroot(dataWatches).iterator(); Iterator<String> existWatchesIter = prependChroot(existWatches).iterator(); Iterator<String> childWatchesIter = prependChroot(childWatches).iterator(); long setWatchesLastZxid = lastZxid; while (dataWatchesIter.hasNext() || existWatchesIter.hasNext() || childWatchesIter.hasNext()) { List<String> dataWatchesBatch = new ArrayList<String>(); List<String> existWatchesBatch = new ArrayList<String>(); List<String> childWatchesBatch = new ArrayList<String>(); int batchLength = 0; // Note, we may exceed our max length by a bit when we add the last // watch in the batch. This isn't ideal, but it makes the code simpler. while (batchLength < SET_WATCHES_MAX_LENGTH) { final String watch; if (dataWatchesIter.hasNext()) { watch = dataWatchesIter.next(); dataWatchesBatch.add(watch); } else if (existWatchesIter.hasNext()) { watch = existWatchesIter.next(); existWatchesBatch.add(watch); } else if (childWatchesIter.hasNext()) { watch = childWatchesIter.next(); childWatchesBatch.add(watch); } else { break; } batchLength += watch.length(); } SetWatches sw = new SetWatches(setWatchesLastZxid, dataWatchesBatch, existWatchesBatch, childWatchesBatch); RequestHeader header = new RequestHeader(-8, OpCode.setWatches); Packet packet = new Packet(header, new ReplyHeader(), sw, null, null); outgoingQueue.addFirst(packet); } } } for (AuthData id : authInfo) { outgoingQueue.addFirst(new Packet(new RequestHeader(-4, OpCode.auth), null, new AuthPacket(0, id.scheme, id.data), null, null)); } outgoingQueue.addFirst(new Packet(null, null, conReq, null, null, readOnly)); clientCnxnSocket.connectionPrimed(); if (LOG.isDebugEnabled()) { LOG.debug("Session establishment request sent on " + clientCnxnSocket.getRemoteSocketAddress()); } }
public void run() { SocketChannel sChannel = null; try { /* * For future unwary socket programmers: although connect 'blocks' it * does not require an accept on the server side to return. Therefore * you can not assume that all the sockets are connected at the end of * this for loop. */ sChannel = SocketChannel.open(); sChannel.connect(new InetSocketAddress(host,port)); // Construct a connection request ConnectRequest conReq = new ConnectRequest(0, 0, 10000, 0, "password".getBytes()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); boa.writeInt(-1, "len"); conReq.serialize(boa, "connect"); baos.close(); ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); bb.putInt(bb.capacity() - 4); bb.rewind(); /* Send a connect request. Any socket that has been closed (or at least * not added to the cnxn list on the server) will not have any bytes to * read and get an eof. * * The trick here was finding a call that caused the server to put * bytes in the input stream without closing the cnxn. None of * the four letter commands do that, so we actually try to create * a session which should send us something back, while maintaining * the connection. */ int eof = sChannel.write(bb); // If the socket times out, we count that as Assert.failed - // the server should respond within 10s sChannel.socket().setSoTimeout(10000); if (!sChannel.socket().isClosed()){ eof = sChannel.socket().getInputStream().read(); if (eof != -1) { numConnected.incrementAndGet(); } } } catch (IOException io) { // "Connection reset by peer" } finally { if (sChannel != null) { try { sChannel.close(); } catch (Exception e) { // Do nothing } } } }
void primeConnection() throws IOException { LOG.info("Socket connection established to " + clientCnxnSocket.getRemoteSocketAddress() + ", initiating session"); isFirstConnect = false; long sessId = (seenRwServerBefore) ? sessionId : 0; ConnectRequest conReq = new ConnectRequest(0, lastZxid, sessionTimeout, sessId, sessionPasswd); synchronized (outgoingQueue) { // We add backwards since we are pushing into the front // Only send if there's a pending watch // TODO: here we have the only remaining use of zooKeeper in // this class. It's to be eliminated! if (!disableAutoWatchReset) { List<String> dataWatches = zooKeeper.getDataWatches(); List<String> existWatches = zooKeeper.getExistWatches(); List<String> childWatches = zooKeeper.getChildWatches(); if (!dataWatches.isEmpty() || !existWatches.isEmpty() || !childWatches.isEmpty()) { SetWatches sw = new SetWatches(lastZxid, prependChroot(dataWatches), prependChroot(existWatches), prependChroot(childWatches)); RequestHeader h = new RequestHeader(); h.setType(ZooDefs.OpCode.setWatches); h.setXid(-8); Packet packet = new Packet(h, new ReplyHeader(), sw, null, null); outgoingQueue.addFirst(packet); } } for (AuthData id : authInfo) { outgoingQueue.addFirst(new Packet(new RequestHeader(-4, OpCode.auth), null, new AuthPacket(0, id.scheme, id.data), null, null)); } outgoingQueue.addFirst(new Packet(null, null, conReq, null, null, readOnly)); } clientCnxnSocket.enableReadWriteOnly(); if (LOG.isDebugEnabled()) { LOG.debug("Session establishment request sent on " + clientCnxnSocket.getRemoteSocketAddress()); } }
public void run() { try { /* * For future unwary socket programmers: although connect 'blocks' it * does not require an accept on the server side to return. Therefore * you can not assume that all the sockets are connected at the end of * this for loop. */ SocketChannel sChannel = SocketChannel.open(); sChannel.connect(new InetSocketAddress(host,port)); // Construct a connection request ConnectRequest conReq = new ConnectRequest(0, 0, 10000, 0, "password".getBytes()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); boa.writeInt(-1, "len"); conReq.serialize(boa, "connect"); baos.close(); ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); bb.putInt(bb.capacity() - 4); bb.rewind(); /* Send a connect request. Any socket that has been closed (or at least * not added to the cnxn list on the server) will not have any bytes to * read and get an eof. * * The trick here was finding a call that caused the server to put * bytes in the input stream without closing the cnxn. None of * the four letter commands do that, so we actually try to create * a session which should send us something back, while maintaining * the connection. */ int eof = sChannel.write(bb); // If the socket times out, we count that as Assert.failed - // the server should respond within 10s sChannel.socket().setSoTimeout(10000); if (!sChannel.socket().isClosed()){ eof = sChannel.socket().getInputStream().read(); if (eof != -1) { numConnected.incrementAndGet(); } } } catch (IOException io) { // "Connection reset by peer" } }
void primeConnection() throws IOException { LOG.info("Socket connection established to " + clientCnxnSocket.getRemoteSocketAddress() + ", initiating session"); isFirstConnect = false; long sessId = (seenRwServerBefore) ? sessionId : 0; ConnectRequest conReq = new ConnectRequest(0, lastZxid, sessionTimeout, sessId, sessionPasswd); synchronized (outgoingQueue) { // We add backwards since we are pushing into the front // Only send if there's a pending watch // TODO: here we have the only remaining use of zooKeeper in // this class. It's to be eliminated! if (!disableAutoWatchReset && (!zooKeeper.getDataWatches().isEmpty() || !zooKeeper.getExistWatches().isEmpty() || !zooKeeper .getChildWatches().isEmpty())) { SetWatches sw = new SetWatches(lastZxid, zooKeeper.getDataWatches(), zooKeeper.getExistWatches(), zooKeeper.getChildWatches()); RequestHeader h = new RequestHeader(); h.setType(ZooDefs.OpCode.setWatches); h.setXid(-8); Packet packet = new Packet(h, new ReplyHeader(), sw, null, null); outgoingQueue.addFirst(packet); } for (AuthData id : authInfo) { outgoingQueue.addFirst(new Packet(new RequestHeader(-4, OpCode.auth), null, new AuthPacket(0, id.scheme, id.data), null, null)); } outgoingQueue.addFirst(new Packet(null, null, conReq, null, null, readOnly)); } clientCnxnSocket.enableReadWriteOnly(); if (LOG.isDebugEnabled()) { LOG.debug("Session establishment request sent on " + clientCnxnSocket.getRemoteSocketAddress()); } }