use of org.apache.zookeeper.proto.ConnectRequest in project zookeeper by apache.
the class ZooKeeperServer method processConnectRequest.
public void processConnectRequest(ServerCnxn cnxn, ByteBuffer incomingBuffer) throws IOException {
BinaryInputArchive bia = BinaryInputArchive.getArchive(new ByteBufferInputStream(incomingBuffer));
ConnectRequest connReq = new ConnectRequest();
connReq.deserialize(bia, "connect");
if (LOG.isDebugEnabled()) {
LOG.debug("Session establishment request from client " + cnxn.getRemoteSocketAddress() + " client's lastZxid is 0x" + Long.toHexString(connReq.getLastZxidSeen()));
}
boolean readOnly = false;
try {
readOnly = bia.readBool("readOnly");
cnxn.isOldClient = false;
} catch (IOException e) {
// this is ok -- just a packet from an old client which
// doesn't contain readOnly field
LOG.warn("Connection request from old client " + cnxn.getRemoteSocketAddress() + "; will be dropped if server is in r-o mode");
}
if (!readOnly && this instanceof ReadOnlyZooKeeperServer) {
String msg = "Refusing session request for not-read-only client " + cnxn.getRemoteSocketAddress();
LOG.info(msg);
throw new CloseRequestException(msg);
}
if (connReq.getLastZxidSeen() > zkDb.dataTree.lastProcessedZxid) {
String msg = "Refusing session request for client " + cnxn.getRemoteSocketAddress() + " as it has seen zxid 0x" + Long.toHexString(connReq.getLastZxidSeen()) + " our last zxid is 0x" + Long.toHexString(getZKDatabase().getDataTreeLastProcessedZxid()) + " client must try another server";
LOG.info(msg);
throw new CloseRequestException(msg);
}
int sessionTimeout = connReq.getTimeOut();
byte[] passwd = connReq.getPasswd();
int minSessionTimeout = getMinSessionTimeout();
if (sessionTimeout < minSessionTimeout) {
sessionTimeout = minSessionTimeout;
}
int maxSessionTimeout = getMaxSessionTimeout();
if (sessionTimeout > maxSessionTimeout) {
sessionTimeout = maxSessionTimeout;
}
cnxn.setSessionTimeout(sessionTimeout);
// We don't want to receive any packets until we are sure that the
// session is setup
cnxn.disableRecv();
long sessionId = connReq.getSessionId();
if (sessionId == 0) {
LOG.info("Client attempting to establish new session at " + cnxn.getRemoteSocketAddress());
createSession(cnxn, passwd, sessionTimeout);
} else {
long clientSessionId = connReq.getSessionId();
LOG.info("Client attempting to renew session 0x" + Long.toHexString(clientSessionId) + " at " + cnxn.getRemoteSocketAddress());
if (serverCnxnFactory != null) {
serverCnxnFactory.closeSession(sessionId);
}
if (secureServerCnxnFactory != null) {
secureServerCnxnFactory.closeSession(sessionId);
}
cnxn.setSessionId(sessionId);
reopenSession(cnxn, sessionId, passwd, sessionTimeout);
}
}
use of org.apache.zookeeper.proto.ConnectRequest in project zookeeper by apache.
the class WatchLeakTest method createConnRequest.
/**
* 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();
}
use of org.apache.zookeeper.proto.ConnectRequest in project zookeeper by apache.
the class SessionInvalidationTest method testCreateAfterCloseShouldFail.
/**
* 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
// We'll fill this in later
boa.writeInt(52, "len");
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();
}
Aggregations