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");
    }
  }
示例#2
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());
 }
示例#3
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);
                }
              }
            }
          }
        }
 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;
   }
 }
  public static void checkSystemTime(ZooKeeper zooKeeper, long tolerance)
      throws KeeperException, InterruptedException, BlurException {
    String path =
        zooKeeper.create(
            "/" + UUID.randomUUID().toString(), null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    long now = System.currentTimeMillis();
    Stat stat = zooKeeper.exists(path, false);
    zooKeeper.delete(path, -1);
    long ctime = stat.getCtime();

    long low = now - tolerance;
    long high = now + tolerance;
    if (!(low <= ctime && ctime <= high)) {
      throw new BlurException(
          "The system time is too far out of sync with Zookeeper, check your system time and try again.",
          null);
    }
  }