Exemple #1
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;
  }
Exemple #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());
 }
Exemple #3
0
  /**
   * @param path 要Watch的节点的路径
   * @param serverAddress:服务器名,如果null,只watch,不set
   * @param watch
   */
  private final void __watchMaster(final WatchBag bag)
        // throws KeeperException, InterruptedException
      {
    MasterWatcher watch = (MasterWatcher) bag.watch;
    if (watch == null) {
      return;
    }

    final InterMasterWatcher iw = new InterMasterWatcher(bag);
    iw.path = bag.path;
    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) {

              if (bag.payload == null) {
                iw.setMaster(null);
              }
              watchExecutorService.submit(
                  new Runnable() {
                    public void run() {
                      try {
                        Thread.currentThread();
                        Thread.sleep((long) (300));
                      } catch (InterruptedException e) {
                      }
                      __watchMaster(bag);
                    }
                  });
              // 否则不能删除Cluster

            } else if (we.getType() == Event.EventType.NodeCreated) {
              __watchMaster(bag);
            } else if (we.getType() == Event.EventType.NodeDataChanged) {
              __watchMaster(bag);
              log.error("Node data should not change:{}", we);
            }
          }
        };

    try {

      try {
        Stat stat = new Stat();
        byte[] master = this.zooKeeper.getData(bag.path, false, stat);
        if (stat.getMzxid() == bag.lastid) {
          log.warn("master watch Get a dupmxxid:{}", bag.lastid);
        }
        bag.lastid = stat.getMzxid();

        iw.setMaster(new String(master));
      } catch (KeeperException.NoNodeException e) {
        String serverAddress = bag.payload;
        if (serverAddress != null) {
          try {
            this.zooKeeper.create(
                bag.path,
                serverAddress.getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL);
            iw.setMaster(serverAddress);

          } catch (KeeperException.NodeExistsException ee) {
            // 刚才没有,现在有了,zk会触发的,所以忽略
          } catch (KeeperException.NoNodeException ee) {
            // 没有父节点,在删除schema
            watch.exceptionCaught(ee);
          }

        } else {
          iw.setMaster(null);
        }
      }

    } catch (Exception e) {
      log.error("Error while get Master", e);
      //  System.exit(-1);
      watch.exceptionCaught(e);
    }
    try {
      this.zooKeeper.exists(bag.path, w);
    } catch (KeeperException.ConnectionLossException e) {
      this.watchTries.add(
          new RetryRun() {
            public void run() throws KeeperException, InterruptedException {
              zooKeeper.exists(bag.path, w);
            }
          });
    } catch (Exception ex) {
      log.warn("ZK Error while watch Master", ex);
    }
  }
Exemple #4
0
  private long __watchLongValueNode(final WatchBag bag) {

    LongValueWatcher lw = (LongValueWatcher) bag.watch;
    if (lw == null) {
      return Long.MAX_VALUE;
    }

    final Watcher w =
        new Watcher() {
          public void process(org.apache.zookeeper.WatchedEvent we) {
            //  System.out.println(we);
            if (we.getState() == Event.KeeperState.Expired) {
              handleExpired();
            } else if (we.getType() == Event.EventType.NodeDeleted) {
              log.warn("NodeDeleted:{}", bag.path);
            } else if (we.getType() == Event.EventType.NodeCreated) {

              if (bag.watch != null) {
                __watchLongValueNode(bag);
              }

            } else if (we.getType() == Event.EventType.NodeDataChanged) {
              final LongValueWatcher lw = (LongValueWatcher) bag.watch;
              if (lw != null) {
                final long lv = __watchLongValueNode(bag);
                if (lv != Long.MAX_VALUE) {
                  new Thread(
                          new Runnable() {
                            public void run() {
                              lw.valueChanged(lv);
                            }
                          },
                          "LongWatcher-trigger-thread")
                      .start();
                }
              }
            }
          }
        };

    try {
      Stat stat = new Stat();
      String s = new String(this.zooKeeper.getData(bag.path, w, stat));
      if (bag.lastid == stat.getMzxid()) {
        log.warn("long watch get a dupmxxid:{}", bag.lastid);
        return Long.MAX_VALUE;
      }
      bag.lastid = stat.getMzxid();
      long lv = Long.parseLong(s);

      return lv;

    } catch (KeeperException.ConnectionLossException e) {
      log.debug("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("KeeperException:{}", bag.path);
    } catch (InterruptedException e) {
      log.error("watchTimeNode InterruptedException:{}", bag.path);
    } catch (NumberFormatException e) {
      log.error("watchTimeNode NumberFormatException:{}", bag.path);
    } catch (Exception e) {
      log.error("watchTimeNode Exception:{}", e, bag.path);
    }
    return Long.MAX_VALUE;
  }