Package org.apache.zookeeper

Examples of org.apache.zookeeper.ZooKeeper


  private static final int ID_LENGTH_SIZE =  Bytes.SIZEOF_INT;

  public RecoverableZooKeeper(String quorumServers, int seesionTimeout,
      Watcher watcher, int maxRetries, int retryIntervalMillis)
  throws IOException {
    this.zk = new ZooKeeper(quorumServers, seesionTimeout, watcher);
    this.retryCounterFactory =
      new RetryCounterFactory(maxRetries, retryIntervalMillis);
    this.retryIntervalMillis = retryIntervalMillis;

    // the identifier = processID@hostName
View Full Code Here


   */
  public static void waitForBaseZNode(Configuration conf) throws IOException {
    LOG.info("Waiting until the base znode is available");
    String parentZNode = conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT,
        HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT);
    ZooKeeper zk = new ZooKeeper(ZKConfig.getZKQuorumServersString(conf),
        conf.getInt(HConstants.ZK_SESSION_TIMEOUT,
        HConstants.DEFAULT_ZK_SESSION_TIMEOUT), EmptyWatcher.instance);

    final int maxTimeMs = 10000;
    final int maxNumAttempts = maxTimeMs / HConstants.SOCKET_RETRY_WAIT_MS;

    KeeperException keeperEx = null;
    try {
      try {
        for (int attempt = 0; attempt < maxNumAttempts; ++attempt) {
          try {
            if (zk.exists(parentZNode, false) != null) {
              LOG.info("Parent znode exists: " + parentZNode);
              keeperEx = null;
              break;
            }
          } catch (KeeperException e) {
            keeperEx = e;
          }
          Threads.sleepWithoutInterrupt(HConstants.SOCKET_RETRY_WAIT_MS);
        }
      } finally {
        zk.close();
      }
    } catch (InterruptedException ex) {
      Thread.currentThread().interrupt();
    }

View Full Code Here

    }
    return ensureExists(znode.substring(0, index));
  }

  private boolean ensureExists(final String znode) {
    ZooKeeper zk = wrapper.getZooKeeper();
    try {
      Stat stat = zk.exists(znode, false);
      if (stat != null) {
        return true;
      }
      zk.create(znode, new byte[0], Ids.OPEN_ACL_UNSAFE,
        CreateMode.PERSISTENT);
      return true;
    } catch (KeeperException.NodeExistsException e) {
      return true;      // ok, move on.
    } catch (KeeperException.NoNodeException e) {
View Full Code Here

    }
  }

  @Override
  public User getUserForToken(String token) throws IOException {
    ZooKeeper zk = wrapper.getZooKeeper();
    try {
      byte[] data = zk.getData(usersZNode + "/" + token, null, null);
      if (data == null) {
        return null;
      }
      UserModel model =
        unmarshaller.unmarshalFromJSON(new ByteArrayInputStream(data),
View Full Code Here

  }

  private boolean ensureExists(final String znode, final CreateMode mode,
      final byte[] data) {
    try {
      ZooKeeper zk = wrapper.getZooKeeper();
      Stat stat = zk.exists(znode, false);
      if (stat != null) {
        zk.setData(znode, data, -1);
        return true;
      }
      zk.create(znode, data, Ids.OPEN_ACL_UNSAFE, mode);
      LOG.info("Created ZNode " + znode);
      return true;
    } catch (KeeperException.NodeExistsException e) {
      return true;      // ok, move on.
    } catch (KeeperException.NoNodeException e) {
View Full Code Here

  @Override
  public void setUp() throws Exception {
    super.setUp();
    authenticator = new ZooKeeperAuthenticator(conf);
    ZooKeeper zk = authenticator.wrapper.getZooKeeper();
    JSONJAXBContext context =
      new JSONJAXBContext(JSONConfiguration.natural().build(),
          UserModel.class);
    JSONMarshaller marshaller = context.createJSONMarshaller();
    if (zk.exists(ZooKeeperAuthenticator.USERS_ZNODE_ROOT + "/" +
          ADMIN_TOKEN, null) == null) {
      UserModel model = new UserModel();
      model.name = ADMIN_USERNAME;
      model.admin = true;
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      marshaller.marshallToJSON(model, os);
      zk.create(ZooKeeperAuthenticator.USERS_ZNODE_ROOT + "/" + ADMIN_TOKEN,
        os.toByteArray(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    if (zk.exists(ZooKeeperAuthenticator.USERS_ZNODE_ROOT + "/" +
          USER_TOKEN, null) == null) {
      UserModel model = new UserModel();
      model.name = USER_USERNAME;
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      marshaller.marshallToJSON(model, os);
      zk.create(ZooKeeperAuthenticator.USERS_ZNODE_ROOT + "/" + USER_TOKEN,
        os.toByteArray(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    if (zk.exists(ZooKeeperAuthenticator.USERS_ZNODE_ROOT + "/" +
          DISABLED_TOKEN, null) == null) {
      UserModel model = new UserModel();
      model.name = DISABLED_USERNAME;
      model.disabled = true;
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      marshaller.marshallToJSON(model, os);
      zk.create(ZooKeeperAuthenticator.USERS_ZNODE_ROOT + "/" +DISABLED_TOKEN,
        os.toByteArray(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
  }
View Full Code Here

        IMocksControl c = EasyMock.createNiceControl();

        BundleContext ctx = c.createMock(BundleContext.class);
        ZooKeeperDiscovery zkd = c.createMock(ZooKeeperDiscovery.class);

        ZooKeeper zk = c.createMock(ZooKeeper.class);
        EasyMock.expect(zk.getState()).andReturn(ZooKeeper.States.CONNECTED).anyTimes();

        EasyMock.expect(zkd.getZookeeper()).andReturn(zk).anyTimes();

        EndpointListenerTrackerCustomizer.Interest interest = new EndpointListenerTrackerCustomizer.Interest();

        String scope = "(myProp=test)";
        String interf = "es.schaaf.test";
        String node = Util.getZooKeeperPath(interf);

        final InterfaceDataMonitorListenerImpl idmli = c.createMock(InterfaceDataMonitorListenerImpl.class);

        InterfaceMonitor im = new InterfaceMonitor(zk, interf, interest, scope, ctx) {
            @Override
            protected InterfaceDataMonitorListenerImpl createInterfaceDataMonitorListener(ZooKeeper zk,
                                                                                          String intf,
                                                                                          Interest zkd,
                                                                                          String scope,
                                                                                          BundleContext bctx) {
                return idmli;
            }
        };

        idmli.change();
        EasyMock.expectLastCall().once();

        zk.exists(EasyMock.eq(node), EasyMock.eq(im), EasyMock.eq(im), EasyMock.anyObject());
        EasyMock.expectLastCall().once();

        EasyMock.expect(zk.exists(EasyMock.eq(node), EasyMock.eq(false))).andReturn(new Stat()).anyTimes();

        EasyMock.expect(zk.getChildren(EasyMock.eq(node), EasyMock.eq(im))).andReturn(Collections.EMPTY_LIST)
            .once();

        c.replay();

        im.start();
View Full Code Here

        IMocksControl c = EasyMock.createNiceControl();

        BundleContext ctx = c.createMock(BundleContext.class);
        ZooKeeperDiscovery zkd = c.createMock(ZooKeeperDiscovery.class);

        ZooKeeper zk = c.createMock(ZooKeeper.class);

        EasyMock.expect(zkd.getZookeeper()).andReturn(zk).anyTimes();

       
        String path = "/osgi/service_registry/myClass/google.de#80##test";
        EasyMock.expect(
                        zk.create(EasyMock.eq(path),
                                  (byte[])EasyMock.anyObject(), EasyMock.eq(Ids.OPEN_ACL_UNSAFE), EasyMock
                                      .eq(CreateMode.EPHEMERAL))).andReturn("").once();
       
        zk.delete(EasyMock.eq("/osgi/service_registry/myClass/google.de#80##test"), EasyMock.eq(-1));
        EasyMock.expectLastCall().once();

        c.replay();

        EndpointListenerImpl eli = new EndpointListenerImpl(zkd, ctx);
View Full Code Here

   */
  public static void waitForBaseZNode(Configuration conf) throws IOException {
    LOG.info("Waiting until the base znode is available");
    String parentZNode = conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT,
        HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT);
    ZooKeeper zk = new ZooKeeper(ZKConfig.getZKQuorumServersString(conf),
        conf.getInt(HConstants.ZK_SESSION_TIMEOUT,
        HConstants.DEFAULT_ZK_SESSION_TIMEOUT), EmptyWatcher.instance);

    final int maxTimeMs = 10000;
    final int maxNumAttempts = maxTimeMs / HConstants.SOCKET_RETRY_WAIT_MS;

    KeeperException keeperEx = null;
    try {
      try {
        for (int attempt = 0; attempt < maxNumAttempts; ++attempt) {
          try {
            if (zk.exists(parentZNode, false) != null) {
              LOG.info("Parent znode exists: " + parentZNode);
              keeperEx = null;
              break;
            }
          } catch (KeeperException e) {
            keeperEx = e;
          }
          Threads.sleepWithoutInterrupt(HConstants.SOCKET_RETRY_WAIT_MS);
        }
      } finally {
        zk.close();
      }
    } catch (InterruptedException ex) {
      Thread.currentThread().interrupt();
    }

View Full Code Here

        System.out.println("In Old connection New session");
      }
    };
    _zkClient.subscribeStateChanges(listener);
    ZkConnection connection = ((ZkConnection) _zkClient.getConnection());
    ZooKeeper zookeeper = connection.getZookeeper();
    System.out.println("old sessionId= " + zookeeper.getSessionId());
    try
    {
      Watcher watcher = new Watcher(){
        @Override
        public void process(WatchedEvent event)
        {
          System.out.println("In New connection In process event:"+ event);
        }
      };
      ZooKeeper newZookeeper = new ZooKeeper(connection.getServers(),
          zookeeper.getSessionTimeout(), watcher , zookeeper.getSessionId(),
          zookeeper.getSessionPasswd());
      Thread.sleep(3000);
      System.out.println("New sessionId= " + newZookeeper.getSessionId());
      Thread.sleep(3000);
      newZookeeper.close();
      Thread.sleep(10000);
      connection = ((ZkConnection) _zkClient.getConnection());
      zookeeper = connection.getZookeeper();
      System.out.println("After session expiry sessionId= " + zookeeper.getSessionId());
    } catch (Exception e)
View Full Code Here

TOP

Related Classes of org.apache.zookeeper.ZooKeeper

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.