Beispiel #1
0
  private void updateProcessId() {
    try {
      // TODO: this is Sun JVM specific ...
      // String processName = (String) mbeanServer.get().getAttribute(new
      // ObjectName("java.lang:type=Runtime"), "Name");
      String processName = ManagementFactory.getRuntimeMXBean().getName();
      Long processId = Long.parseLong(processName.split("@")[0]);

      String runtimeIdentity = runtimeProperties.get().getRuntimeIdentity();
      String path = ZkPath.CONTAINER_PROCESS_ID.getPath(runtimeIdentity);
      Stat stat = exists(curator.get(), path);
      if (stat != null) {
        if (stat.getEphemeralOwner()
            != curator.get().getZookeeperClient().getZooKeeper().getSessionId()) {
          delete(curator.get(), path);
          if (processId != null) {
            create(curator.get(), path, processId.toString(), CreateMode.EPHEMERAL);
          }
        }
      } else {
        if (processId != null) {
          create(curator.get(), path, processId.toString(), CreateMode.EPHEMERAL);
        }
      }
    } catch (Exception ex) {
      LOGGER.error("Error while updating the process id.", ex);
    }
  }
    @Override
    public void run() {
      if (isSynchronizedWithZooKeeper.get() || !zkClient.isConnected() || !started.get()) {

        return;
      }
      if (checkVersion.getAndSet(false)) {
        try {
          synchronized (lastStatusVersionMonitor) {
            final Stat stat = zkClient.getZookeeper().exists(path, null);
            if (stat != null
                && zkClient.getZookeeper().getSessionId() == stat.getEphemeralOwner()) {
              zkClient.getZookeeper().delete(path, lastStatusVersion);
            }
          }
        } catch (InterruptedException e) {
          LOG.info("Interrupted");
          checkVersion.set(true);
        } catch (KeeperException e) {
          LOG.info("exception " + e.getMessage());
          checkVersion.set(true);
        }
      }
      LOG.info(
          "We are out-of-sync, have a zookeeper connection, and are started, trying reclaim: "
              + path
              + this);
      tryClaim();
    }
  @Test
  public void testSyncGetStat() {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String testName = className + "_" + methodName;

    System.out.println("START " + testName + " at " + new Date(System.currentTimeMillis()));

    String path = String.format("/%s/%s", testName, "msg_0");
    ZNRecord record = new ZNRecord("msg_0");
    ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);

    Stat stat = accessor.getStat(path, 0);
    Assert.assertNull(stat);

    boolean success = accessor.create(path, record, AccessOption.EPHEMERAL);
    Assert.assertTrue(success);

    stat = accessor.getStat(path, 0);
    Assert.assertNotNull(stat);
    Assert.assertEquals(stat.getVersion(), 0);
    Assert.assertNotSame(stat.getEphemeralOwner(), 0);

    System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis()));
  }
Beispiel #4
0
 public static void copyStat(Stat from, Stat to) {
   to.setAversion(from.getAversion());
   to.setCtime(from.getCtime());
   to.setCversion(from.getCversion());
   to.setCzxid(from.getCzxid());
   to.setMtime(from.getMtime());
   to.setMzxid(from.getMzxid());
   to.setPzxid(from.getPzxid());
   to.setVersion(from.getVersion());
   to.setEphemeralOwner(from.getEphemeralOwner());
   to.setDataLength(from.getDataLength());
   to.setNumChildren(from.getNumChildren());
 }
Beispiel #5
0
 /**
  * Check existence of <i>regPath</i> and wait it expired if possible
  *
  * @param regPath reg node path.
  * @return true if regPath exists, otherwise return false
  * @throws IOException if can't create reg path
  */
 protected boolean checkRegNodeAndWaitExpired(String regPath) throws IOException {
   final CountDownLatch prevNodeLatch = new CountDownLatch(1);
   Watcher zkPrevRegNodewatcher =
       new Watcher() {
         @Override
         public void process(WatchedEvent event) {
           // Check for prev znode deletion. Connection expiration is
           // not handling, since bookie has logic to shutdown.
           if (EventType.NodeDeleted == event.getType()) {
             prevNodeLatch.countDown();
           }
         }
       };
   try {
     Stat stat = zk.exists(regPath, zkPrevRegNodewatcher);
     if (null != stat) {
       // if the ephemeral owner isn't current zookeeper client
       // wait for it to be expired.
       if (stat.getEphemeralOwner() != zk.getSessionId()) {
         LOG.info(
             "Previous bookie registration znode: {} exists, so waiting zk sessiontimeout:"
                 + " {} ms for znode deletion",
             regPath,
             conf.getZkTimeout());
         // waiting for the previous bookie reg znode deletion
         if (!prevNodeLatch.await(conf.getZkTimeout(), TimeUnit.MILLISECONDS)) {
           throw new NodeExistsException(regPath);
         } else {
           return false;
         }
       }
       return true;
     } else {
       return false;
     }
   } catch (KeeperException ke) {
     LOG.error("ZK exception checking and wait ephemeral znode {} expired : ", regPath, ke);
     throw new IOException(
         "ZK exception checking and wait ephemeral znode " + regPath + " expired", ke);
   } catch (InterruptedException ie) {
     LOG.error("Interrupted checking and wait ephemeral znode {} expired : ", regPath, ie);
     throw new IOException(
         "Interrupted checking and wait ephemeral znode " + regPath + " expired", ie);
   }
 }