org.apache.zookeeper.server.NIOServerCnxn Java Examples

The following examples show how to use org.apache.zookeeper.server.NIOServerCnxn. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MiniAvatarCluster.java    From RDFS with Apache License 2.0 6 votes vote down vote up
public static void createAndStartZooKeeper() 
  throws IOException, ConfigException, InterruptedException {
  ServerConfig zkConf = createZooKeeperConf();

  zooKeeper = new ZooKeeperServer();
  FileTxnSnapLog ftxn = new 
    FileTxnSnapLog(new File(zkConf.getDataLogDir()),
                   new File(zkConf.getDataDir()));
  zooKeeper.setTxnLogFactory(ftxn);
  zooKeeper.setTickTime(zkConf.getTickTime());
  zooKeeper.setMinSessionTimeout(zkConf.getMinSessionTimeout());
  zooKeeper.setMaxSessionTimeout(zkConf.getMaxSessionTimeout());

  cnxnFactory =
    new NIOServerCnxn.Factory(zkConf.getClientPortAddress(),
                              zkConf.getMaxClientCnxns());
  cnxnFactory.startup(zooKeeper);

}
 
Example #2
Source File: EmbeddedZookeeper.java    From jesos with Apache License 2.0 5 votes vote down vote up
public EmbeddedZookeeper(final int port)
                throws IOException
{
    this.port = port;
    zkDataDir = Files.createTempDir();
    zkServer = new ZooKeeperServer();

    final FileTxnSnapLog ftxn = new FileTxnSnapLog(zkDataDir, zkDataDir);
    zkServer.setTxnLogFactory(ftxn);

    cnxnFactory = new NIOServerCnxn.Factory(new InetSocketAddress(this.port), 0);
}
 
Example #3
Source File: ChaosMonkeyCnxnFactory.java    From xian with Apache License 2.0 4 votes vote down vote up
/**
 * Build a connection with a Chaos Monkey ZookeeperServer
 */
protected NIOServerCnxn createConnection(SocketChannel sock, SelectionKey sk) throws IOException
{
    return new NIOServerCnxn(zkServer, sock, sk, this);
}
 
Example #4
Source File: ChaosMonkeyCnxnFactory.java    From xian with Apache License 2.0 4 votes vote down vote up
@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();
        }
    }
}