@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()));
  }
  @Test
  public void testCheckVersion() throws Exception {
    CuratorFramework client =
        CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    try {
      client.create().forPath("/foo");
      Stat stat = client.setData().forPath("/foo", "new".getBytes()); // up the version

      try {
        client
            .inTransaction()
            .check()
            .withVersion(stat.getVersion() + 1)
            .forPath("/foo") // force a bad version
            .and()
            .create()
            .forPath("/bar")
            .and()
            .commit();

        Assert.fail();
      } catch (KeeperException.BadVersionException correct) {
        // correct
      }

      Assert.assertNull(client.checkExists().forPath("/bar"));
    } finally {
      client.close();
    }
  }
  @Test
  public void testSomeNodes() throws Exception {

    Timing timing = new Timing();
    ChildReaper reaper = null;
    CuratorFramework client =
        CuratorFrameworkFactory.newClient(
            server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
      client.start();

      Random r = new Random();
      int nonEmptyNodes = 0;
      for (int i = 0; i < 10; ++i) {
        client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i));
        if (r.nextBoolean()) {
          client.create().forPath("/test/" + Integer.toString(i) + "/foo");
          ++nonEmptyNodes;
        }
      }

      reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1);
      reaper.start();

      timing.forWaiting().sleepABit();

      Stat stat = client.checkExists().forPath("/test");
      Assert.assertEquals(stat.getNumChildren(), nonEmptyNodes);
    } finally {
      Closeables.closeQuietly(reaper);
      Closeables.closeQuietly(client);
    }
  }
  /**
   * Try to atomically update a node in ZooKeeper, creating it if it doesn't exist. This is meant to
   * be used within an optimistic concurrency model.
   *
   * @param pathSuffix suffix to use to build path in ZooKeeper.
   * @param f function used to initialize the node, or transform the data already there.
   * @return true if node was created/updated, false if a concurrent modification occurred and
   *     succeeded while trying to update/create the node.
   * @throws Exception
   */
  private boolean tryAtomicUpdate(final String pathSuffix, final NodeFunction f) throws Exception {
    final String path = buildZookeeperPath(pathSuffix);
    final Stat stat = zk.checkExists().forPath(path);

    if (stat == null) {
      try {
        zk.create()
            .creatingParentsIfNeeded()
            .withMode(CreateMode.PERSISTENT)
            .forPath(path, f.initialize());
      } catch (KeeperException.NodeExistsException e) {
        LOG.debug("Concurrent creation of " + path + ", retrying", e);
        return false;
      }
    } else {
      Mod<byte[]> newVal = f.apply(zk.getData().forPath(path));

      if (newVal.hasModification()) {
        try {
          zk.setData().withVersion(stat.getVersion()).forPath(path, newVal.get());
        } catch (KeeperException.BadVersionException e) {
          LOG.debug("Concurrent update to " + path + ", retrying.", e);
          return false;
        }
      }
    }

    return true;
  }
 private static boolean checkStale(SolrZkClient zkClient, String zkPath, int currentVersion) {
   if (zkPath == null) return false;
   try {
     Stat stat = zkClient.exists(zkPath, null, true);
     if (stat == null) {
       if (currentVersion > -1) return true;
       return false;
     }
     if (stat.getVersion() > currentVersion) {
       log.info(
           zkPath + " is stale will need an update from {} to {}",
           currentVersion,
           stat.getVersion());
       return true;
     }
     return false;
   } catch (KeeperException.NoNodeException nne) {
     // no problem
   } catch (KeeperException e) {
     log.error("error refreshing solrconfig ", e);
   } catch (InterruptedException e) {
     Thread.currentThread().isInterrupted();
   }
   return false;
 }
Exemple #6
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();
    }
  /**
   * Creates the serialized value of the object and stores this in ZooKeeper under the path. It
   * updates the lastStatusVersion. It does not set a watcher for the path.
   */
  private void updateCoordinateData() throws CoordinateMissingException, CloudnameException {
    if (!started.get()) {
      throw new IllegalStateException("Not started.");
    }

    if (!zkClient.isConnected()) {
      throw new CloudnameException("No proper connection with zookeeper.");
    }

    synchronized (lastStatusVersionMonitor) {
      try {
        Stat stat =
            zkClient
                .getZookeeper()
                .setData(
                    path,
                    zkCoordinateData.snapshot().serialize().getBytes(Util.CHARSET_NAME),
                    lastStatusVersion);
        LOG.fine("Updated coordinate, latest version is " + stat.getVersion());
        lastStatusVersion = stat.getVersion();
      } catch (KeeperException.NoNodeException e) {
        throw new CoordinateMissingException("Coordinate does not exist " + path);
      } catch (KeeperException e) {
        throw new CloudnameException(
            "ZooKeeper errror in updateCoordinateData: " + e.getMessage(), e);
      } catch (UnsupportedEncodingException e) {
        throw new CloudnameException(e);
      } catch (InterruptedException e) {
        throw new CloudnameException(e);
      } catch (IOException e) {
        throw new CloudnameException(e);
      }
    }
  }
  // @wjw_note: 把任务分配给taskServerList里随机的一个server!
  private void assignServer2Task(List<String> taskServerList, String taskPath) throws Exception {
    // @wjw_note: 清除过期的无Runner的Task
    {
      Stat stat = new Stat();
      this.getZooKeeper().getData(taskPath, null, stat);
      if (getSystemTime() - stat.getMtime() > TASK_EXPIRE_TIME) {
        ZKTools.deleteTree(this.getZooKeeper(), taskPath);
        LOG.warn("清除过期的无Runner的Task[" + taskPath + "]");
        return;
      }
    }

    int index = random.nextInt(taskServerList.size());
    String serverId = taskServerList.get(index);
    this.getZooKeeper()
        .create(taskPath + "/" + serverId, null, this.zkManager.getAcl(), CreateMode.PERSISTENT);

    if (LOG.isDebugEnabled()) {
      StringBuilder buffer = new StringBuilder();
      buffer
          .append("Assign server [")
          .append(serverId)
          .append("]")
          .append(" to task [")
          .append(taskPath)
          .append("]");
      LOG.debug(buffer.toString());
    }
  }
Exemple #10
0
  /**
   * Change the shared value value irrespective of its previous state
   *
   * @param newValue new value
   * @throws Exception ZK errors, interruptions, etc.
   */
  public void setValue(byte[] newValue) throws Exception {
    Preconditions.checkState(state.get() == State.STARTED, "not started");

    client.setData().forPath(path, newValue);
    stat.setVersion(stat.getVersion() + 1);
    value = Arrays.copyOf(newValue, newValue.length);
  }
Exemple #11
0
  private String __watchStrValueNode(final WatchBag bag) {
    StringValueWatcher lw = (StringValueWatcher) bag.watch;
    if (lw == null) {
      return null;
    }

    final Watcher w =
        new Watcher() {
          public void process(org.apache.zookeeper.WatchedEvent we) {
            if (we.getState() == Event.KeeperState.Expired) {
              handleExpired();
            } else if (we.getType() == Event.EventType.NodeDeleted) {

            } else if (we.getType() == Event.EventType.NodeCreated) {
              if (bag.watch != null) __watchStrValueNode(bag);
            } else if (we.getType() == Event.EventType.NodeDataChanged) {
              final StringValueWatcher lw = (StringValueWatcher) bag.watch;
              if (lw != null) {
                final String s = __watchStrValueNode(bag);
                if (s != null) {
                  new Thread(
                          new Runnable() {
                            public void run() {
                              lw.valueChanged(s);
                            }
                          },
                          "StringWatcher-trigger-thread")
                      .start();
                }
              }
            }
          }
        };

    try {
      Stat stat = new Stat();
      String ss = new String(zooKeeper.getData(bag.path, w, stat));
      if (bag.lastid == stat.getMzxid()) {
        log.warn("str watch get a dupmxxid:{}", bag.lastid);
        return null;
      }
      bag.lastid = stat.getMzxid();
      return ss;

    } catch (KeeperException.ConnectionLossException e) {
      log.debug("Str watch ConnectionLossException:{}", bag.path);
      this.watchTries.add(
          new RetryRun() {
            public void run() throws KeeperException, InterruptedException {
              if (bag.watch != null) zooKeeper.getData(bag.path, w, null);
            }
          });

    } catch (KeeperException e) {
      log.error(" watchStrValueNode KeeperException:{}", bag.path);
    } catch (Exception e) {
      log.error("watchStrValueNode Exception:{}", e, bag.path);
    }
    return null;
  }
  public ScheduleDataManager4ZK(ZKManager aZkManager) throws Exception {
    this.zkManager = aZkManager;
    this.gson =
        new GsonBuilder()
            .registerTypeAdapter(Timestamp.class, new TimestampTypeAdapter())
            .setDateFormat(ScheduleUtil.DATA_FORMAT_YYYYMMDDHHMMSS)
            .create();
    this.pathServer = this.zkManager.getRootPath() + "/" + NODE_SERVER;
    this.pathTask = this.zkManager.getRootPath() + "/" + NODE_TASK;
    this.random = new Random();
    if (this.getZooKeeper().exists(this.pathServer, false) == null) {
      ZKTools.createPath(
          getZooKeeper(), this.pathServer, CreateMode.PERSISTENT, this.zkManager.getAcl());
    }

    loclaBaseTime = System.currentTimeMillis();
    String tempPath =
        this.zkManager
            .getZooKeeper()
            .create(
                this.zkManager.getRootPath() + "/systime",
                null,
                this.zkManager.getAcl(),
                CreateMode.EPHEMERAL_SEQUENTIAL);
    Stat tempStat = this.zkManager.getZooKeeper().exists(tempPath, false);
    zkBaseTime = tempStat.getCtime();
    ZKTools.deleteTree(getZooKeeper(), tempPath);
    if (Math.abs(this.zkBaseTime - this.loclaBaseTime) > 5000) {
      LOG.error(
          "请注意,Zookeeper服务器时间与本地时间相差 : " + Math.abs(this.zkBaseTime - this.loclaBaseTime) + " ms");
    }
  }
Exemple #13
0
 public void updateRecord(MetaRecord rec) {
   try {
     long ts = System.currentTimeMillis();
     putRecord(rec, ts);
     String node = ZKUtil.joinZNode(H2MetaTableTracker.NODE_NAME, Integer.toString(rec.getId()));
     // setData会异步触发所有机器(包括本机)上的H2MetaTableTracker.nodeDataChanged
     // 然后触发下列调用:
     // =>org.h2.engine.Database.updateDatabaseObject(int)
     //  =>org.h2.engine.Database.update(Session, DbObject)
     //      =>org.h2.engine.Database.addMeta0(Session, DbObject, boolean)
     //          =>又到此方法
     // 所以会造成循环
     synchronized (this) { // 避免setData后立刻触发nodeDataChanged,此时IdVersion还未更新
       ZKUtil.setData(watcher, node, Bytes.toBytes(ts));
       // setData后watch不见了,所以要继续watch,监听其他人对此node的修改
       // ZKUtil.watchAndCheckExists(watcher, node);
       Stat stat = new Stat();
       ZKUtil.getDataAndWatch(watcher, node, stat);
       // 这里记录下id的最新版本,触发nodeDataChanged时再检查一下是否版本一样,
       // 如果不大于这里的版本那么就不再执行updateDatabaseObject操作
       tracker.updateIdVersion(rec.getId(), stat.getVersion());
     }
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
  @Test
  public void testNamespace() throws Exception {
    Timing timing = new Timing();
    ChildReaper reaper = null;
    CuratorFramework client =
        CuratorFrameworkFactory.builder()
            .connectString(server.getConnectString())
            .sessionTimeoutMs(timing.session())
            .connectionTimeoutMs(timing.connection())
            .retryPolicy(new RetryOneTime(1))
            .namespace("foo")
            .build();
    try {
      client.start();

      for (int i = 0; i < 10; ++i) {
        client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i));
      }

      reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1);
      reaper.start();

      timing.forWaiting().sleepABit();

      Stat stat = client.checkExists().forPath("/test");
      Assert.assertEquals(stat.getNumChildren(), 0);

      stat = client.usingNamespace(null).checkExists().forPath("/foo/test");
      Assert.assertNotNull(stat);
      Assert.assertEquals(stat.getNumChildren(), 0);
    } finally {
      Closeables.closeQuietly(reaper);
      Closeables.closeQuietly(client);
    }
  }
Exemple #15
0
 /**
  * Deletes an existing unassigned node that is in the specified state for the specified region.
  *
  * <p>If a node does not already exist for this region, a {@link NoNodeException} will be thrown.
  *
  * <p>No watcher is set whether this succeeds or not.
  *
  * <p>Returns false if the node was not in the proper state but did exist.
  *
  * <p>This method is used during table disables when a region finishes successfully closing. This
  * is the Master acknowledging completion of the specified regions transition to being closed.
  *
  * @param zkw zk reference
  * @param regionName region to be deleted from zk
  * @param expectedState state region must be in for delete to complete
  * @param expectedVersion of the znode that is to be deleted. If expectedVersion need not be
  *     compared while deleting the znode pass -1
  * @throws KeeperException if unexpected zookeeper exception
  * @throws KeeperException.NoNodeException if node does not exist
  */
 public static boolean deleteNode(
     ZooKeeperWatcher zkw, String regionName, EventType expectedState, int expectedVersion)
     throws KeeperException, KeeperException.NoNodeException {
   LOG.debug(
       zkw.prefix(
           "Deleting existing unassigned "
               + "node for "
               + regionName
               + " that is in expected state "
               + expectedState));
   String node = getNodeName(zkw, regionName);
   zkw.sync(node);
   Stat stat = new Stat();
   byte[] bytes = ZKUtil.getDataNoWatch(zkw, node, stat);
   if (bytes == null) {
     // If it came back null, node does not exist.
     throw KeeperException.create(Code.NONODE);
   }
   RegionTransitionData data = RegionTransitionData.fromBytes(bytes);
   if (!data.getEventType().equals(expectedState)) {
     LOG.warn(
         zkw.prefix(
             "Attempting to delete unassigned "
                 + "node "
                 + regionName
                 + " in "
                 + expectedState
                 + " state but node is in "
                 + data.getEventType()
                 + " state"));
     return false;
   }
   if (expectedVersion != -1 && stat.getVersion() != expectedVersion) {
     LOG.warn(
         "The node we are trying to delete is not the expected one. " + "Got a version mismatch");
     return false;
   }
   synchronized (zkw.getNodes()) {
     // TODO: Does this go here or only if we successfully delete node?
     zkw.getNodes().remove(node);
     if (!ZKUtil.deleteNode(zkw, node, stat.getVersion())) {
       LOG.warn(
           zkw.prefix(
               "Attempting to delete "
                   + "unassigned node in "
                   + expectedState
                   + " state but "
                   + "after verifying it was in OPENED state, we got a version mismatch"));
       return false;
     }
     LOG.debug(
         zkw.prefix(
             "Successfully deleted unassigned node for region "
                 + regionName
                 + " in expected state "
                 + expectedState));
     return true;
   }
 }
Exemple #16
0
 void trigeWatcher() {
   try {
     Stat s = zk.exists("/root", false); // 此处不设置watcher
     zk.setData("/root", "a".getBytes(), s.getVersion()); // 修改数据时需要提供version,version设为-1表示强制修改
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  @Test
  public void testSyncGet() {
    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 = new Stat();
    ZNRecord getRecord = accessor.get(path, stat, 0);
    Assert.assertNull(getRecord);

    try {
      accessor.get(path, stat, AccessOption.THROW_EXCEPTION_IFNOTEXIST);
      Assert.fail("Should throw exception if not exist");
    } catch (Exception e) {
      // OK
    }

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

    getRecord = accessor.get(path, stat, 0);
    Assert.assertNotNull(getRecord);
    Assert.assertEquals(getRecord.getId(), "msg_0");
    Assert.assertEquals(stat.getVersion(), 0);

    record.setSimpleField("key0", "value0");
    success = accessor.set(path, record, AccessOption.PERSISTENT);
    Assert.assertTrue(success);

    getRecord = accessor.get(path, stat, 0);
    Assert.assertNotNull(getRecord);
    Assert.assertEquals(record.getSimpleFields().size(), 1);
    Assert.assertNotNull(getRecord.getSimpleField("key0"));
    Assert.assertEquals(getRecord.getSimpleField("key0"), "value0");
    Assert.assertEquals(stat.getVersion(), 1);

    ZNRecord newRecord = new ZNRecord("msg_0");
    newRecord.setSimpleField("key1", "value1");
    success = accessor.update(path, new ZNRecordUpdater(newRecord), AccessOption.PERSISTENT);
    Assert.assertTrue(success);

    getRecord = accessor.get(path, stat, 0);
    Assert.assertNotNull(getRecord);
    Assert.assertEquals(getRecord.getSimpleFields().size(), 2);
    Assert.assertNotNull(getRecord.getSimpleField("key0"));
    Assert.assertEquals(getRecord.getSimpleField("key0"), "value0");
    Assert.assertNotNull(getRecord.getSimpleField("key1"));
    Assert.assertEquals(getRecord.getSimpleField("key1"), "value1");
    Assert.assertEquals(stat.getVersion(), 2);

    System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis()));
  }
 @Override
 public long count() {
   try {
     Stat stat = zkConnection.getClient().checkExists().forPath(Paths.STREAMS);
     return stat == null ? 0 : stat.getNumChildren();
   } catch (Exception e) {
     throw ZooKeeperUtils.wrapThrowable(e);
   }
 }
Exemple #19
0
 @PUT
 @Path("blueprint/{name}")
 public Response updateBlueprint(
     @Context UriInfo uri, @PathParam("name") String oldName, ConfigManifest blueprint) {
   Response res;
   try {
     ZooKeeper zk = Controller.getInstance().getZKInstance();
     String newName = oldName;
     if (blueprint.getUrl() != null) {
       newName = ZookeeperUtil.getBaseURL(blueprint.getUrl().toString());
     } else {
       blueprint.setUrl(uri.getAbsolutePath().toURL());
     }
     byte[] data = JAXBUtil.write(blueprint);
     Stat stat =
         zk.exists(
             CommonConfigurationKeys.ZOOKEEPER_CONFIG_BLUEPRINT_PATH_DEFAULT + '/' + newName,
             false);
     if (stat != null && oldName.equals(newName)) {
       // Update existing blueprint
       String path =
           CommonConfigurationKeys.ZOOKEEPER_CONFIG_BLUEPRINT_PATH_DEFAULT + '/' + oldName;
       zk.delete(path, stat.getVersion());
       zk.create(
           CommonConfigurationKeys.ZOOKEEPER_CONFIG_BLUEPRINT_PATH_DEFAULT + '/' + newName,
           data,
           Ids.OPEN_ACL_UNSAFE,
           CreateMode.PERSISTENT);
     } else if (stat != null) {
       // Conflict in name change
       throw new WebApplicationException(409);
     } else {
       // Create new blueprint
       try {
         zk.create(
             CommonConfigurationKeys.ZOOKEEPER_CONFIG_BLUEPRINT_PATH_DEFAULT + '/' + newName,
             data,
             Ids.OPEN_ACL_UNSAFE,
             CreateMode.PERSISTENT);
       } catch (KeeperException.NodeExistsException e) {
         throw new WebApplicationException(409);
       }
     }
     res = Response.noContent().build();
     return res;
   } catch (WebApplicationException e) {
     throw e;
   } catch (Exception e) {
     LOG.error(ExceptionUtil.getStackTrace(e));
     throw new WebApplicationException(500);
   }
 }
  /**
   * sync update
   *
   * @return: updatedData on success, or null on fail
   */
  public T update(
      String path, DataUpdater<T> updater, List<String> createPaths, Stat stat, int options) {
    CreateMode mode = AccessOption.getMode(options);
    if (mode == null) {
      LOG.error("Invalid update mode. options: " + options);
      return null;
    }

    boolean retry;
    T updatedData = null;
    do {
      retry = false;
      try {
        Stat readStat = new Stat();
        T oldData = (T) _zkClient.readData(path, readStat);
        T newData = updater.update(oldData);
        Stat setStat = _zkClient.writeDataGetStat(path, newData, readStat.getVersion());
        if (stat != null) {
          DataTree.copyStat(setStat, stat);
        }

        updatedData = newData;
      } catch (ZkBadVersionException e) {
        retry = true;
      } catch (ZkNoNodeException e) {
        // node not exist, try create
        try {
          T newData = updater.update(null);
          RetCode rc = create(path, newData, createPaths, options);
          switch (rc) {
            case OK:
              updatedData = newData;
              break;
            case NODE_EXISTS:
              retry = true;
              break;
            default:
              LOG.error("Fail to update path by creating: " + path);
              return null;
          }
        } catch (Exception e1) {
          LOG.error("Exception while updating path by creating: " + path, e1);
          return null;
        }
      } catch (Exception e) {
        LOG.error("Exception while updating path: " + path, e);
        return null;
      }
    } while (retry);

    return updatedData;
  }
Exemple #21
0
        @Override
        public void process(WatchedEvent event) {
          // this lock is important so that when changes start happening, we
          // won't run into any concurrency issues

          synchronized (WatchedNode.this) {
            if (!cancelled) {
              if (event.getState() == KeeperState.SyncConnected) {
                // If connected update data and notify listeners
                try {
                  if (event.getType().equals(Event.EventType.NodeCreated)) {
                    watchForData();
                  } else if (event.getType().equals(Event.EventType.NodeDeleted)) {
                    // Previous version notified is null, and we will notify with null
                    previousVersion = null;
                    watchForCreation();
                  } else if (event.getType().equals(Event.EventType.NodeDataChanged)) {
                    watchForData();
                  }
                } catch (KeeperException e) {
                  LOG.error("Exception while trying to update our cached value for " + nodePath, e);
                } catch (InterruptedException e) {
                  if (LOG.isTraceEnabled()) {
                    LOG.trace(
                        "Interrupted while trying to update our cached value for " + nodePath, e);
                  }
                }
                // Notify of new value if either we didn't notify of any value, or the node has
                // changed
                long currentVersion = stat.getCtime() + stat.getMtime();
                if (previousVersion == null || !previousVersion.equals(currentVersion)) {
                  try {
                    synchronized (listeners) {
                      for (WatchedNodeListener<T> listener : listeners) {
                        listener.onWatchedNodeChange(value);
                      }
                    }
                  } finally {
                    previousVersion = currentVersion;
                  }
                }
              } else {
                // Not sync connected, do nothing
                if (LOG.isDebugEnabled()) {
                  LOG.debug("Not sync connected anymore for watched node " + nodePath);
                }
              }
            }
          }
        }
Exemple #22
0
  private void __watchChildren(final WatchBag bag) {
    ChildrenWatcher cw = (ChildrenWatcher) bag.watch;
    if (cw == null) {
      return;
    }
    final Watcher w =
        new Watcher() {

          public void process(WatchedEvent we) {

            if (we.getState() == Event.KeeperState.Expired) {

              handleExpired();
            } else if (we.getType() == Event.EventType.NodeChildrenChanged) {

              __watchChildren(bag);

            } else if (we.getType() == Event.EventType.NodeCreated) {
              __watchChildren(bag);
            } else if (we.getType() == Event.EventType.NodeDeleted) {
              __watchChildren(bag);
            } else if (we.getType() == Event.EventType.NodeDataChanged) {
              log.warn("watch childern NodeDataChanged:{}", bag.path);
            }
          }
        };

    try {
      Stat stat = new Stat();
      java.util.List<java.lang.String> result = zooKeeper.getChildren(bag.path, w, stat);
      if (bag.lastid == stat.getCversion()) {
        log.warn("children watch get a dupmxxid:{}", bag.lastid);
        return;
      }
      bag.lastid = stat.getCversion();
      cw.childrenCome(result);
    } catch (KeeperException.ConnectionLossException e) {
      log.debug("watch children  ConnectionLossException{}", bag.path);
      this.watchTries.add(
          new RetryRun() {
            public void run() throws KeeperException, InterruptedException {
              if (bag.watch != null) zooKeeper.getChildren(bag.path, w, null);
            }
          });
    } catch (KeeperException e) {
      log.error("watch children KeeperException:{}", bag.path);
    } catch (Exception e) {
      log.error("watch children Exception:{}", e, bag.path);
    }
  }
Exemple #23
0
  public Integer getVersion(CuratorFramework zk, String path, boolean watch) throws Exception {
    String npath = PathUtils.normalize_path(path);
    Stat stat = null;
    if (existsNode(zk, npath, watch)) {
      if (watch) {
        stat = zk.checkExists().watched().forPath(PathUtils.normalize_path(path));
      } else {
        stat = zk.checkExists().forPath(PathUtils.normalize_path(path));
      }
      return Integer.valueOf(stat.getVersion());
    }

    return null;
  }
Exemple #24
0
 @DELETE
 @Path("blueprint/{name}")
 public Response deleteStack(@PathParam("name") String name) {
   ZooKeeper zk = Controller.getInstance().getZKInstance();
   try {
     String path = ZookeeperUtil.getConfigManifestPath(name);
     Stat current = zk.exists(path, false);
     zk.delete(path, current.getVersion());
   } catch (Exception e) {
     LOG.error(ExceptionUtil.getStackTrace(e));
     throw new WebApplicationException(500);
   }
   Response res = Response.noContent().build();
   return res;
 }
Exemple #25
0
  /**
   * Changes the shared value only if its value has not changed since this client last read it. If
   * the value has changed, the value is not set and this client's view of the value is updated.
   * i.e. if the value is not successful you can get the updated value by calling {@link
   * #getValue()}.
   *
   * @param newValue the new value to attempt
   * @return true if the change attempt was successful, false if not. If the change was not
   *     successful, {@link #getValue()} will return the updated value
   * @throws Exception ZK errors, interruptions, etc.
   */
  public boolean trySetValue(byte[] newValue) throws Exception {
    Preconditions.checkState(state.get() == State.STARTED, "not started");

    try {
      client.setData().withVersion(stat.getVersion()).forPath(path, newValue);
      stat.setVersion(stat.getVersion() + 1);
      value = Arrays.copyOf(newValue, newValue.length);
      return true;
    } catch (KeeperException.BadVersionException ignore) {
      // ignore
    }

    readValue();
    return false;
  }
  public Long getNextProcessId(Long channelId, Long pipelineId) {
    String processRoot = ManagePathUtils.getProcessRoot(channelId, pipelineId);
    IZkConnection connection = zookeeper.getConnection();
    // zkclient会将获取stat信息和正常的操作分开,使用原生的zk进行优化
    ZooKeeper orginZk = ((ZooKeeperx) connection).getZookeeper();

    Stat processParentStat = new Stat();
    // 获取所有的process列表
    try {
      orginZk.getChildren(processRoot, false, processParentStat);
      return (Long) ((processParentStat.getCversion() + processParentStat.getNumChildren()) / 2L);
    } catch (Exception e) {
      return -1L;
    }
  }
 public int hashCode() {
   int result = 17;
   int ret;
   ret = stat.hashCode();
   result = 37 * result + ret;
   return result;
 }
  @Test(timeout = 60000)
  public void testBadVersionOnTwoAllocators() throws Exception {
    String allocationPath = "/allocation-bad-version";
    zkc.get()
        .create(allocationPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Stat stat = new Stat();
    byte[] data = zkc.get().getData(allocationPath, false, stat);
    Versioned<byte[]> allocationData =
        new Versioned<byte[]>(data, new ZkVersion(stat.getVersion()));

    SimpleLedgerAllocator allocator1 =
        new SimpleLedgerAllocator(
            allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    SimpleLedgerAllocator allocator2 =
        new SimpleLedgerAllocator(
            allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    allocator1.allocate();
    // wait until allocated
    ZKTransaction txn1 = newTxn();
    LedgerHandle lh = FutureUtils.result(allocator1.tryObtain(txn1, NULL_LISTENER));
    allocator2.allocate();
    ZKTransaction txn2 = newTxn();
    try {
      FutureUtils.result(allocator2.tryObtain(txn2, NULL_LISTENER));
      fail(
          "Should fail allocating on second allocator as allocator1 is starting allocating something.");
    } catch (ZKException zke) {
      assertEquals(KeeperException.Code.BADVERSION, zke.getKeeperExceptionCode());
    }
    FutureUtils.result(txn1.execute());
    Utils.close(allocator1);
    Utils.close(allocator2);

    long eid = lh.addEntry("hello world".getBytes());
    lh.close();
    LedgerHandle readLh =
        bkc.get()
            .openLedger(lh.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
    Enumeration<LedgerEntry> entries = readLh.readEntries(eid, eid);
    int i = 0;
    while (entries.hasMoreElements()) {
      LedgerEntry entry = entries.nextElement();
      assertEquals("hello world", new String(entry.getEntry(), UTF_8));
      ++i;
    }
    assertEquals(1, i);
  }
 public boolean setData(ZookeeperConnection zkConn, String path, String content)
     throws IOException {
   if (zkConn != null) {
     try {
       Stat stat = zkConn.createZookeeper().setData(path, content.getBytes(), -1);
       System.out.print("stat:aversion:" + stat.getAversion() + "|" + stat.getCversion());
       zkConn.close();
     } catch (InterruptedException e) {
       e.printStackTrace();
       return false;
     } catch (KeeperException e) {
       e.printStackTrace();
       return false;
     }
   }
   return true;
 }
 public PositionEventData getCanalCursor(String destination, short clientId) {
   String path = String.format(CANAL_CURSOR_PATH, destination, String.valueOf(clientId));
   try {
     IZkConnection connection = zookeeper.getConnection();
     // zkclient会将获取stat信息和正常的操作分开,使用原生的zk进行优化
     ZooKeeper orginZk = ((ZooKeeperx) connection).getZookeeper();
     Stat stat = new Stat();
     byte[] bytes = orginZk.getData(path, false, stat);
     PositionEventData eventData = new PositionEventData();
     eventData.setCreateTime(new Date(stat.getCtime()));
     eventData.setModifiedTime(new Date(stat.getMtime()));
     eventData.setPosition(new String(bytes, "UTF-8"));
     return eventData;
   } catch (Exception e) {
     return null;
   }
 }