Exemplo n.º 1
0
  /**
   * Removes the lock or associated znode if you no longer require the lock. this also removes your
   * request in the queue for locking in case you do not already hold the lock.
   *
   * @throws RuntimeException throws a runtime exception if it cannot connect to zookeeper.
   */
  public synchronized void unlock() throws RuntimeException {

    if (!isClosed() && id != null) {
      // we don't need to retry this operation in the case of failure
      // as ZK will remove ephemeral files and we don't wanna hang
      // this process when closing if we cannot reconnect to ZK
      try {

        ZooKeeperOperation zopdel =
            new ZooKeeperOperation() {
              public boolean execute() throws KeeperException, InterruptedException {
                zookeeper.delete(id, -1);
                return Boolean.TRUE;
              }
            };
        zopdel.execute();
      } catch (InterruptedException e) {
        LOG.warn("Caught: " + e, e);
        // set that we have been interrupted.
        Thread.currentThread().interrupt();
      } catch (KeeperException.NoNodeException e) {
        // do nothing
      } catch (KeeperException e) {
        LOG.warn("Caught: " + e, e);
        throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
      } finally {
        if (callback != null) {
          callback.lockReleased();
        }
        id = null;
      }
    }
  }
Exemplo n.º 2
0
    @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();
    }
Exemplo n.º 3
0
  /**
   * Lista os {@link DataNode}s conectados no momento ao ClusterService.
   *
   * @return datanodes
   * @throws KeeperException
   * @throws InterruptedException
   * @throws IOException
   */
  public static List<NodeStatus> getDatanodesConnected() {
    List<NodeStatus> datanodes = new ArrayList<NodeStatus>();
    try {
      List<String> nodesIds = zk.getChildren(ZkConf.DATANODES_PATH, true);
      LOG.info("Cluster com " + nodesIds.size() + " datanode(s) ativo(s) no momento.");

      for (String hostName : nodesIds) {
        String path = ZkConf.DATANODES_PATH + "/" + hostName;
        byte[] bytes = zk.getData(path, true, null);
        NodeStatus node = (NodeStatus) Serializer.toObject(bytes);
        datanodes.add(node);
      }

    } catch (ClassNotFoundException e) {
      LOG.error(e.getMessage(), e);
    } catch (KeeperException e) {
      LOG.error(e.getMessage(), e);
    } catch (InterruptedException e) {
      LOG.error(e.getMessage(), e);
    } catch (IOException e) {
      LOG.error(e.getMessage(), e);
    }

    return datanodes;
  }
Exemplo n.º 4
0
  /**
   * 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);
      }
    }
  }
Exemplo n.º 5
0
  @Override
  public void initializeSecurity(TCredentials credentials, String principal, byte[] token)
      throws AccumuloSecurityException {
    try {
      // remove old settings from zookeeper first, if any
      IZooReaderWriter zoo = ZooReaderWriter.getInstance();
      synchronized (zooCache) {
        zooCache.clear();
        if (zoo.exists(ZKUserPath)) {
          zoo.recursiveDelete(ZKUserPath, NodeMissingPolicy.SKIP);
          log.info("Removed " + ZKUserPath + "/" + " from zookeeper");
        }

        // prep parent node of users with root username
        zoo.putPersistentData(ZKUserPath, principal.getBytes(UTF_8), NodeExistsPolicy.FAIL);

        constructUser(principal, ZKSecurityTool.createPass(token));
      }
    } catch (KeeperException e) {
      log.error("{}", e.getMessage(), e);
      throw new RuntimeException(e);
    } catch (InterruptedException e) {
      log.error("{}", e.getMessage(), e);
      throw new RuntimeException(e);
    } catch (AccumuloException e) {
      log.error("{}", e.getMessage(), e);
      throw new RuntimeException(e);
    }
  }
Exemplo n.º 6
0
 @Override
 public void changePassword(String principal, AuthenticationToken token)
     throws AccumuloSecurityException {
   if (!(token instanceof PasswordToken))
     throw new AccumuloSecurityException(principal, SecurityErrorCode.INVALID_TOKEN);
   PasswordToken pt = (PasswordToken) token;
   if (userExists(principal)) {
     try {
       synchronized (zooCache) {
         zooCache.clear(ZKUserPath + "/" + principal);
         ZooReaderWriter.getInstance()
             .putPrivatePersistentData(
                 ZKUserPath + "/" + principal,
                 ZKSecurityTool.createPass(pt.getPassword()),
                 NodeExistsPolicy.OVERWRITE);
       }
     } catch (KeeperException e) {
       log.error("{}", e.getMessage(), e);
       throw new AccumuloSecurityException(principal, SecurityErrorCode.CONNECTION_ERROR, e);
     } catch (InterruptedException e) {
       log.error("{}", e.getMessage(), e);
       throw new RuntimeException(e);
     } catch (AccumuloException e) {
       log.error("{}", e.getMessage(), e);
       throw new AccumuloSecurityException(principal, SecurityErrorCode.DEFAULT_SECURITY_ERROR, e);
     }
   } else
     throw new AccumuloSecurityException(
         principal, SecurityErrorCode.USER_DOESNT_EXIST); // user doesn't exist
 }
Exemplo n.º 7
0
  private void enode_test_1() throws IOException, InterruptedException, KeeperException {
    checkRoot();
    String parentName = testDirOnZK;
    String nodeName = parentName + "/enode_abc";
    ZooKeeper zk = new ZooKeeper(hostPort, 10000, this);

    Stat stat = zk.exists(parentName, false);
    if (stat == null) {
      try {
        zk.create(parentName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
      } catch (KeeperException ke) {
        fail("Creating node " + parentName + ke.getMessage());
      }
    }

    try {
      zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    } catch (KeeperException ke) {
      int code = ke.getCode();
      boolean valid = code == KeeperException.Code.NodeExists;
      if (!valid) {
        fail("Unexpected exception code for createin: " + ke.getMessage());
      }
    }

    stat = zk.exists(nodeName, false);
    if (stat == null) {
      fail("node " + nodeName + " should exist");
    }
    System.out.println("Closing client with sessionid: 0x" + Long.toHexString(zk.getSessionId()));
    zk.close();
    zk = new ZooKeeper(hostPort, 10000, this);

    for (int i = 0; i < 10; i++) {
      System.out.println("i = " + i);
      stat = zk.exists(nodeName, false);
      if (stat != null) {
        System.out.println("node " + nodeName + " should not exist after reconnection close");
      } else {
        System.out.println("node " + nodeName + " is gone after reconnection close!");
        break;
      }
      Thread.sleep(5000);
    }
    deleteZKDir(zk, nodeName);
    zk.close();
  }
Exemplo n.º 8
0
  private void checkRoot() throws IOException, InterruptedException {
    ZooKeeper zk = new ZooKeeper(hostPort, 10000, this);

    try {
      zk.create(dirOnZK, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (KeeperException.NodeExistsException ke) {
      // expected, sort of
    } catch (KeeperException ke) {
      fail("Unexpected exception code for create " + dirOnZK + ": " + ke.getMessage());
    }

    try {
      zk.create(testDirOnZK, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (KeeperException.NodeExistsException ke) {
      // expected, sort of
    } catch (KeeperException ke) {
      fail("Unexpected exception code for create " + testDirOnZK + ": " + ke.getMessage());
    }

    zk.close();
  }
Exemplo n.º 9
0
  public static synchronized void managerDatanodesCreated(String hostName, NodeStatus nodeLocal) {
    try {

      if (nodeLocal.getNodesResponding().contains(hostName)) {
        String path = ZkConf.DATANODES_PATH + "/" + HOSTNAME;
        nodeLocal.getNodesResponding().remove(nodeLocal.getNodesResponding().indexOf(hostName));
        zk.setData(path, Serializer.fromObject(nodeLocal), -1);
      }

      if (HOSTNAME.equals(hostName)) {
        loadDada();

        if (managerDatanodesResponding.containsKey(hostName)) {
          managerDatanodesResponding.remove(hostName);
          zk.setData(
              ZkConf.MANAGER_NODES_RESPONDING,
              Serializer.fromObject((Serializable) managerDatanodesResponding),
              -1);
        }

        if (datanodesDesconnected.contains(hostName)) {
          datanodesDesconnected.remove(hostName);

          if (historicSendDatanodesDesconnected.containsKey(hostName)) {
            historicSendDatanodesDesconnected.remove(hostName);
          }

          zk.setData(
              ZkConf.DATANODES_DESCONNECTED,
              Serializer.fromObject((Serializable) datanodesDesconnected),
              -1);
          zk.setData(
              ZkConf.HISTORIC_SEND,
              Serializer.fromObject((Serializable) historicSendDatanodesDesconnected),
              -1);
        }

        if (datanodesConnected.size() > 1) {
          new SupportReplyText().checkRepliesText(datanodesReplyReceive);
          new SupportReplyImage().checkRepliesImage(datanodesReplyReceive);
        }

        clear();
      }
    } catch (KeeperException e) {
      LOG.error(e.getMessage(), e);
    } catch (InterruptedException e) {
      LOG.error(e.getMessage(), e);
    } catch (IOException e) {
      LOG.error(e.getMessage(), e);
    }
  }
Exemplo n.º 10
0
 /**
  * Constructor
  *
  * @param connectString
  * @param root
  */
 FIFOQueue(String connectString, String root) throws IOException {
   super(connectString);
   this.root = root;
   if (zk != null) {
     try {
       Stat s = zk.exists(root, false);
       if (s == null) {
         zk.create(root, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
       }
     } catch (KeeperException e) {
       System.out.println("NO:" + e.getMessage());
     } catch (InterruptedException e) {
       System.out.println("NO:" + e.getMessage());
     }
   }
 }
Exemplo n.º 11
0
  private static void loadDada() {
    try {

      managerDatanodesResponding = getManagerDatanodesResponding();
      historicSendDatanodesDesconnected = getHistoricSendDatanodesDesconnected();
      datanodesDesconnected = getDatanodesDesconnected();

    } catch (KeeperException e) {
      LOG.error(e.getMessage(), e);
    } catch (InterruptedException e) {
      LOG.error(e.getMessage(), e);
    } catch (IOException e) {
      LOG.error(e.getMessage(), e);
    } catch (ClassNotFoundException e) {
      LOG.error(e.getMessage(), e);
    }
  }
Exemplo n.º 12
0
  public static synchronized void managerNodesChanged(String hostName, NodeStatus nodeLocal) {
    try {

      String path = ZkConf.DATANODES_PATH + "/" + hostName;

      List<String> listSend = getListDatanodesSendReply(hostName);
      if (listSend.contains(HOSTNAME)) {
        zk.exists(path, true);
      }

      if (hostName.equals(HOSTNAME)) {
        managerDatanodesResponding = getManagerDatanodesResponding();

        byte[] bytes = zk.getData(path, true, null);
        List<String> nodesResponding = new ArrayList<String>();
        NodeStatus datanode = (NodeStatus) Serializer.toObject(bytes);

        managerDatanodesResponding.put(datanode.getHostname(), datanode.getNodesResponding());
        nodesResponding = managerDatanodesResponding.get(datanode.getHostname());
        if (nodesResponding.isEmpty()) managerDatanodesResponding.remove(datanode.getHostname());

        nodeLocal.setNodesResponding(datanode.getNodesResponding());
        zk.setData(
            ZkConf.MANAGER_NODES_RESPONDING,
            Serializer.fromObject((Serializable) managerDatanodesResponding),
            -1);
      }

      if (nodeLocal.getNodesResponding().size() > 0) {
        LOG.info("Este datanode esta respondendo pelo(s): " + nodeLocal.getNodesResponding());
      } else {
        LOG.info("Este datanode não está respondendo por nenhum outro.");
      }

      clear();

    } catch (KeeperException e) {
      LOG.error(e.getMessage(), e);
    } catch (InterruptedException e) {
      LOG.error(e.getMessage(), e);
    } catch (IOException e) {
      LOG.error(e.getMessage(), e);
    } catch (ClassNotFoundException e) {
      LOG.error(e.getMessage(), e);
    }
  }
Exemplo n.º 13
0
  @Override
  public void stop() {
    // clean up old ZK configuration
    try {
      IZKClient client = ((FabricServiceImpl) fabricService).getZooKeeper();
      client.deleteWithChildren(FABRIC_ROOT_PATH);
    } catch (InterruptedException e) {
      String msg = "Error cleaning up old ZK config: " + e.getMessage();
      LOG.error(msg, e);
      throw new BeanCreationException(msg, e);
    } catch (KeeperException e) {
      String msg = "Error cleaning up old ZK config: " + e.getMessage();
      LOG.error(msg, e);
      throw new BeanCreationException(msg, e);
    }

    running = false;
  }
 /**
  * Try to set a lock in another server's znode.
  *
  * @param znode the server names of the other server
  * @return true if the lock was acquired, false in every other cases
  */
 public boolean lockOtherRS(String znode) {
   try {
     String otherRSNameZNode = this.zookeeperWrapper.getZNode(this.rsZNode, znode);
     if (otherRSNameZNode.equals(rsServerNameZnode)) {
       LOG.warn("Won't lock because this is us");
       return false;
     }
     this.zookeeperWrapper.writeZNode(otherRSNameZNode, RS_LOCK_ZNODE, rsServerNameZnode, true);
   } catch (InterruptedException e) {
     LOG.error(e);
     return false;
   } catch (KeeperException e) {
     LOG.debug("Won't lock " + znode + " because " + e.getMessage());
     // TODO see if the other still exists!!
     return false;
   }
   return true;
 }
Exemplo n.º 15
0
  public static void main(String args[]) throws IOException {
    // 启动Server
    MainServer.start();

    // 坐等服务启动完毕
    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    // 好吧,开始
    System.out.println("好吧,开始");
    String connectString = "localhost:" + MainServer.CLIENT_PORT;

    FIFOQueue q = new FIFOQueue(connectString, "/app1");
    int i;
    Integer max = new Integer(5);

    System.out.println("Producer");
    for (i = 0; i < max; i++) {
      try {
        q.produce(10 + i);
      } catch (KeeperException e) {
        System.out.println("error: " + e.getMessage());
      } catch (InterruptedException e) {
        System.out.println("error: " + e.getMessage());
      }
    }

    System.out.println("生产完毕,去消费吧...");

    for (i = 0; i < max; i++) {
      try {
        int r = q.consume();
        System.out.println("Item: " + r);
      } catch (KeeperException e) {
        i--;
        e.printStackTrace();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
Exemplo n.º 16
0
 @Override
 public void dropUser(String user) throws AccumuloSecurityException {
   try {
     synchronized (zooCache) {
       zooCache.clear();
       ZooReaderWriter.getInstance()
           .recursiveDelete(ZKUserPath + "/" + user, NodeMissingPolicy.FAIL);
     }
   } catch (InterruptedException e) {
     log.error("{}", e.getMessage(), e);
     throw new RuntimeException(e);
   } catch (KeeperException e) {
     if (e.code().equals(KeeperException.Code.NONODE)) {
       throw new AccumuloSecurityException(user, SecurityErrorCode.USER_DOESNT_EXIST, e);
     }
     log.error("{}", e.getMessage(), e);
     throw new AccumuloSecurityException(user, SecurityErrorCode.CONNECTION_ERROR, e);
   }
 }
Exemplo n.º 17
0
  private static List<NodeStatus> getDatanodesExists(List<String> datanodes) {
    List<NodeStatus> datanodesExits = new ArrayList<NodeStatus>();

    for (String hostName : datanodes) {
      String path = ZkConf.DATANODES_PATH + "/" + hostName;
      try {
        if (zk.exists(path, true) != null) {
          byte[] bytes = zk.getData(path, true, null);
          NodeStatus datanode = (NodeStatus) Serializer.toObject(bytes);
          datanodesExits.add(datanode);
        }
      } catch (KeeperException e) {
        LOG.error(e.getMessage(), e);
      } catch (InterruptedException e) {
        LOG.error(e.getMessage(), e);
      } catch (IOException e) {
        LOG.error(e.getMessage(), e);
      } catch (ClassNotFoundException e) {
        LOG.error(e.getMessage(), e);
      }
    }

    return datanodesExits;
  }
Exemplo n.º 18
0
  public static synchronized void managerDatanodesDeleted(
      String hostNameDesc, NodeStatus nodeLocal) {
    try {

      if (cacheDatanodesReplyReceive.contains(hostNameDesc)) {
        String path;
        NodeStatus leader = null;

        loadDada();

        if (!datanodesDesconnected.contains(hostNameDesc)) {
          datanodesDesconnected.add(hostNameDesc);
        }

        List<String> listSend = getListDatanodesSendReply(hostNameDesc);
        if (!historicSendDatanodesDesconnected.containsKey(hostNameDesc)) {
          historicSendDatanodesDesconnected.put(hostNameDesc, listSend);
        }

        leader = getDatanodeLeader(hostNameDesc);
        if (HOSTNAME.equals(leader.getHostname())) {
          zk.setData(
              ZkConf.DATANODES_DESCONNECTED,
              Serializer.fromObject((Serializable) datanodesDesconnected),
              -1);
          zk.setData(
              ZkConf.HISTORIC_SEND,
              Serializer.fromObject((Serializable) historicSendDatanodesDesconnected),
              -1);
        }

        if (!managerDatanodesResponding.containsKey(hostNameDesc)) {
          List<NodeStatus> nodesExists = null;
          NodeStatus nodeResponder = null;

          nodesExists = getDatanodesExists(listSend);
          nodeResponder = getDatanodeResponder(nodesExists, hostNameDesc);

          if (nodeResponder != null) {
            if (nodeResponder.getHostname().equals(HOSTNAME)) {
              path = ZkConf.DATANODES_PATH + "/" + HOSTNAME;
              nodeLocal.getNodesResponding().add(hostNameDesc);
              try {
                zk.setData(path, Serializer.fromObject(nodeLocal), -1);
              } catch (KeeperException e) {
              }
            }
          }

        } else {

          List<NodeStatus> nodesExists = null;
          NodeStatus nodeResponder = null;

          nodesExists = getDatanodesExists(listSend);
          nodeResponder = getDatanodeResponder(nodesExists, hostNameDesc);

          if (nodeResponder != null) {
            if (nodeResponder.getHostname().equals(HOSTNAME)) {
              path = ZkConf.DATANODES_PATH + "/" + HOSTNAME;
              nodeLocal.getNodesResponding().add(hostNameDesc);
              try {
                zk.setData(path, Serializer.fromObject(nodeLocal), -1);
              } catch (KeeperException e) {
              }
            }
          }

          Thread.sleep(2000);
          nodesExists.clear();

          List<String> listNodesResponding = managerDatanodesResponding.get(hostNameDesc);
          List<String> listHistoric = new ArrayList<String>();

          for (String nodeName : listNodesResponding) {
            listHistoric = historicSendDatanodesDesconnected.get(nodeName);

            nodesExists = getDatanodesExists(listHistoric);
            nodeResponder = getDatanodeResponder(nodesExists, nodeName);

            if ((nodeResponder != null) && (!nodeResponder.getHostname().equals(""))) {
              path = ZkConf.DATANODES_PATH + "/" + nodeResponder.getHostname();
              nodeResponder.getNodesResponding().add(nodeName);
              try {
                zk.setData(path, Serializer.fromObject(nodeResponder), -1);
              } catch (KeeperException e) {
              }
            }
          }
        }

        if (managerDatanodesResponding.containsKey(hostNameDesc)) {
          managerDatanodesResponding.remove(hostNameDesc);
        }

        if (HOSTNAME.equals(leader.getHostname())) {
          zk.setData(
              ZkConf.MANAGER_NODES_RESPONDING,
              Serializer.fromObject((Serializable) managerDatanodesResponding),
              -1);
        }
      }

      List<String> datanodesReceive = getListDatanodesReceiveReply(hostNameDesc);
      if (datanodesReceive.contains(HOSTNAME)) {
        if (datanodesConnected.size() > 1) {
          new SupportReplyText().checkRepliesText(datanodesReplyReceive);
          new SupportReplyImage().checkRepliesImage(datanodesReplyReceive);
        }
      }

    } catch (InterruptedException e) {
      LOG.error(e.getMessage(), e);
    } catch (IOException e) {
      LOG.error(e.getMessage(), e);
    } catch (ClassNotFoundException e) {
      LOG.error(e.getMessage(), e);
    } catch (KeeperException e) {
      LOG.error(e.getMessage(), e);
    }
  }
Exemplo n.º 19
0
 private void delete_create_get_set_test_1()
     throws IOException, InterruptedException, KeeperException {
   checkRoot();
   ZooKeeper zk = new ZooKeeper(hostPort, 10000, this);
   String parentName = testDirOnZK;
   String nodeName = parentName + "/benwashere";
   try {
     zk.delete(nodeName, -1);
   } catch (KeeperException ke) {
     int code = ke.getCode();
     boolean valid = code == KeeperException.Code.NoNode || code == KeeperException.Code.NotEmpty;
     if (!valid) {
       fail("Unexpected exception code for delete: " + ke.getMessage());
     }
   }
   try {
     zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
   } catch (KeeperException ke) {
     int code = ke.getCode();
     boolean valid = code == KeeperException.Code.NodeExists;
     if (!valid) {
       fail("Unexpected exception code for create: " + ke.getMessage());
     }
   }
   try {
     zk.setData(nodeName, "hi".getBytes(), 5700);
     fail("Should have gotten BadVersion exception");
   } catch (KeeperException ke) {
     if (ke.getCode() != Code.BadVersion) {
       fail("Should have gotten BadVersion exception");
     }
   }
   zk.setData(nodeName, "hi".getBytes(), -1);
   Stat st = new Stat();
   byte[] bytes = zk.getData(nodeName, false, st);
   String retrieved = new String(bytes);
   if (!"hi".equals(retrieved)) {
     fail("The retrieved data [" + retrieved + "] is differented than the expected [hi]");
   }
   try {
     zk.delete(nodeName, 6800);
     fail("Should have gotten BadVersion exception");
   } catch (KeeperException ke) {
     int code = ke.getCode();
     boolean valid =
         code == KeeperException.Code.NotEmpty || code == KeeperException.Code.BadVersion;
     if (!valid) {
       fail("Unexpected exception code for delete: " + ke.getMessage());
     }
   }
   try {
     zk.delete(nodeName, -1);
   } catch (KeeperException ke) {
     int code = ke.getCode();
     boolean valid = code == KeeperException.Code.NotEmpty;
     if (!valid) {
       fail("Unexpected exception code for delete: " + code);
     }
   }
   deleteZKDir(zk, nodeName);
   zk.close();
 }
Exemplo n.º 20
0
  private void enode_test_2() throws IOException, InterruptedException, KeeperException {
    checkRoot();
    String parentName = testDirOnZK;
    String nodeName = parentName + "/enode_abc";
    ZooKeeper zk = new ZooKeeper(hostPort, 10000, this);
    ZooKeeper zk_1 = new ZooKeeper(hostPort, 10000, this);

    Stat stat = zk_1.exists(parentName, false);
    if (stat == null) {
      try {
        zk.create(parentName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
      } catch (KeeperException ke) {
        fail("Creating node " + parentName + ke.getMessage());
      }
    }

    stat = zk_1.exists(nodeName, false);
    if (stat != null) {

      try {
        zk.delete(nodeName, -1);
      } catch (KeeperException ke) {
        int code = ke.getCode();
        boolean valid =
            code == KeeperException.Code.NoNode || code == KeeperException.Code.NotEmpty;
        if (!valid) {
          fail("Unexpected exception code for delete: " + ke.getMessage());
        }
      }
    }

    List<String> firstGen = zk_1.getChildren(parentName, true);

    try {
      zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    } catch (KeeperException ke) {
      int code = ke.getCode();
      boolean valid = code == KeeperException.Code.NodeExists;
      if (!valid) {
        fail("Unexpected exception code for createin: " + ke.getMessage());
      }
    }

    Thread.sleep(5000);
    WatchedEvent event = events.poll(10, TimeUnit.SECONDS);
    if (event == null) {
      throw new IOException("No event was delivered promptly");
    }
    if (event.getType() != EventType.NodeChildrenChanged
        || !event.getPath().equalsIgnoreCase(parentName)) {
      fail("Unexpected event was delivered: " + event.toString());
    }

    stat = zk_1.exists(nodeName, false);
    if (stat == null) {
      fail("node " + nodeName + " should exist");
    }

    try {
      zk.delete(parentName, -1);
      fail("Should be impossible to delete a non-empty node " + parentName);
    } catch (KeeperException ke) {
      int code = ke.getCode();
      boolean valid = code == KeeperException.Code.NotEmpty;
      if (!valid) {
        fail("Unexpected exception code for delete: " + code);
      }
    }

    try {
      zk.create(nodeName + "/def", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
      fail("Should be impossible to create child off Ephemeral node " + nodeName);
    } catch (KeeperException ke) {
      int code = ke.getCode();
      boolean valid = code == KeeperException.Code.NoChildrenForEphemerals;
      if (!valid) {
        fail("Unexpected exception code for createin: " + code);
      }
    }

    try {
      List<String> children = zk.getChildren(nodeName, false);
      if (children.size() > 0) {
        fail("ephemeral node " + nodeName + " should not have children");
      }
    } catch (KeeperException ke) {
      int code = ke.getCode();
      boolean valid = code == KeeperException.Code.NoNode;
      if (!valid) {
        fail("Unexpected exception code for createin: " + code);
      }
    }
    firstGen = zk_1.getChildren(parentName, true);
    stat = zk_1.exists(nodeName, true);
    if (stat == null) {
      fail("node " + nodeName + " should exist");
    }
    System.out.println("session id of zk: " + zk.getSessionId());
    System.out.println("session id of zk_1: " + zk_1.getSessionId());
    zk.close();

    stat = zk_1.exists("nosuchnode", false);

    event = this.getEvent(10);
    if (event == null) {
      throw new AssertionFailedError("First event was not delivered promptly");
    }
    if (!((event.getType() == EventType.NodeChildrenChanged
            && event.getPath().equalsIgnoreCase(parentName))
        || (event.getType() == EventType.NodeDeleted
            && event.getPath().equalsIgnoreCase(nodeName)))) {
      System.out.print(
          parentName
              + " "
              + EventType.NodeChildrenChanged
              + " "
              + nodeName
              + " "
              + EventType.NodeDeleted);
      fail("Unexpected first event was delivered: " + event.toString());
    }

    event = this.getEvent(10);

    if (event == null) {
      throw new AssertionFailedError("Second event was not delivered promptly");
    }
    if (!((event.getType() == EventType.NodeChildrenChanged
            && event.getPath().equalsIgnoreCase(parentName))
        || (event.getType() == EventType.NodeDeleted
            && event.getPath().equalsIgnoreCase(nodeName)))) {
      System.out.print(
          parentName
              + " "
              + EventType.NodeChildrenChanged
              + " "
              + nodeName
              + " "
              + EventType.NodeDeleted);
      fail("Unexpected second event was delivered: " + event.toString());
    }

    firstGen = zk_1.getChildren(parentName, false);
    stat = zk_1.exists(nodeName, false);
    if (stat != null) {
      fail("node " + nodeName + " should have been deleted");
    }
    if (firstGen.contains(nodeName)) {
      fail("node " + nodeName + " should not be a children");
    }
    deleteZKDir(zk_1, nodeName);
    zk_1.close();
  }
  @Override
  public void process(WatchedEvent event) {
    log.info("已经触发了" + event.getType() + "事件!");
    String event_path = event.getPath();
    ZooKeeper zookeeper = zkmanager.getZookeeper();
    if (event.getType() == Event.EventType.NodeDataChanged) {
      try {
        if (!this.phpClientList.isEmpty()) {
          for (PhpClientDO client : this.phpClientList) {
            String baseConfigPath = client.getChildPath();
            if (baseConfigPath.equals(event_path)) {
              String app_db_name_path = client.getNodePath();
              String db_config_name = client.getDbConfigName();
              List<String> list = new ArrayList<String>();
              byte[] bytes = new byte[0];
              if (zookeeper.exists(baseConfigPath, false) != null) {
                bytes = zookeeper.getData(baseConfigPath, true, null);
              }
              if (zookeeper.exists(app_db_name_path, false) != null) {
                list = zookeeper.getChildren(app_db_name_path, true);
              }
              log.info("NodeDataChanged path is : " + event_path);
              String str = FileUtil.listToString(list, bytes, db_config_name);
              FileUtil.writeFile(client.getGenerateFilePath(), "<?php\n" + str);
            }
          }
        }
      } catch (KeeperException e) {
        log.error(e.getMessage());
      } catch (InterruptedException e) {
        log.error(e.getMessage());
      }
    }
    if (event.getType() == Event.EventType.NodeChildrenChanged) {
      try {
        if (!this.phpClientList.isEmpty()) {
          for (PhpClientDO client : this.phpClientList) {
            String app_db_name_path = client.getNodePath();
            if (app_db_name_path.equals(event_path)) {
              String baseConfigPath = client.getChildPath();
              String db_config_name = client.getDbConfigName();
              List<String> list = new ArrayList<String>();
              byte[] bytes = new byte[0];
              if (zookeeper.exists(baseConfigPath, false) != null) {
                bytes = zookeeper.getData(baseConfigPath, true, null);
              }
              if (zookeeper.exists(app_db_name_path, false) != null) {
                list = zookeeper.getChildren(app_db_name_path, true);
              }
              log.info("NodeChildrenChanged path is : " + event_path);
              String str = FileUtil.listToString(list, bytes, db_config_name);
              FileUtil.writeFile(client.getGenerateFilePath(), "<?php\n" + str);
            }
          }
        }
      } catch (KeeperException e) {
        log.error(e.getMessage());
      } catch (InterruptedException e) {
        log.error(e.getMessage());
      }
    } else if (event.getState() == KeeperState.SyncConnected) {
      log.info("收到ZK连接成功事件!");
    } else if (event.getState() == KeeperState.Expired) {
      log.error("会话超时,等待重新建立ZK连接...");
      try {
        zkmanager.reConnection();

        if (!this.phpClientList.isEmpty()) {
          for (PhpClientDO client : this.phpClientList) {
            if (zookeeper.exists(client.getNodePath(), false) != null) {
              this.zkmanager.getZookeeper().getChildren(client.getNodePath(), true);
            }
            if (zookeeper.exists(client.getChildPath(), false) != null) {
              this.zkmanager.getZookeeper().getData(client.getChildPath(), true, null);
            }
          }
        }
      } catch (Exception e) {
        log.error(e.getMessage());
      }
    }
  }
Exemplo n.º 22
0
  /**
   * Handles event from ZooKeeper for this coordinate.
   *
   * @param event
   */
  @Override
  public void process(WatchedEvent event) {
    LOG.info("Got an event from ZooKeeper " + event.toString());
    synchronized (lastStatusVersionMonitor) {
      switch (event.getType()) {
        case None:
          switch (event.getState()) {
            case SyncConnected:
              break;
            case Disconnected:
            case AuthFailed:
            case Expired:
            default:
              // If we lost connection, we don't attempt to register another watcher as
              // this might be blocking forever. Parent will try to reconnect (reclaim)
              // later.
              isSynchronizedWithZooKeeper.set(false);
              sendEventToCoordinateListener(
                  CoordinateListener.Event.NO_CONNECTION_TO_STORAGE, event.toString());

              return;
          }
          return;

        case NodeDeleted:
          // If node is deleted, we have no node to place a new watcher so we stop watching.
          isSynchronizedWithZooKeeper.set(false);
          sendEventToCoordinateListener(CoordinateListener.Event.NOT_OWNER, event.toString());
          return;

        case NodeDataChanged:
          LOG.fine("Node data changed, check versions.");
          boolean verifiedSynchronized = false;
          try {
            final Stat stat = zkClient.getZookeeper().exists(path, this);
            if (stat == null) {
              LOG.info("Could not stat path, setting out of synch, will retry claim");
            } else {
              LOG.fine("Previous version is " + lastStatusVersion + " now is " + stat.getVersion());
              if (stat.getVersion() != lastStatusVersion) {
                LOG.fine("Version mismatch, sending out of sync.");
              } else {
                verifiedSynchronized = true;
              }
            }
          } catch (KeeperException e) {
            LOG.fine(
                "Problems with zookeeper, sending consistencyState out of sync: " + e.getMessage());
          } catch (InterruptedException e) {
            LOG.fine("Got interrupted: " + e.getMessage());
            return;
          } finally {
            isSynchronizedWithZooKeeper.set(verifiedSynchronized);
          }

          if (verifiedSynchronized) {
            sendEventToCoordinateListener(
                CoordinateListener.Event.COORDINATE_OUT_OF_SYNC, event.toString());
          }
          return;

        case NodeChildrenChanged:
        case NodeCreated:
          // This should not happen..
          isSynchronizedWithZooKeeper.set(false);
          sendEventToCoordinateListener(
              CoordinateListener.Event.COORDINATE_OUT_OF_SYNC, event.toString());
          return;
      }
    }
  }
  /**
   * This method will be called inside the ProcessRequestThread, which is a singleton, so there will
   * be a single thread calling this code.
   *
   * @param request
   */
  @SuppressWarnings("unchecked")
  protected void pRequest(Request request) throws RequestProcessorException {
    // LOG.info("Prep>>> cxid = " + request.cxid + " type = " +
    // request.type + " id = 0x" + Long.toHexString(request.sessionId));
    TxnHeader txnHeader = null;
    Record txn = null;
    try {
      switch (request.type) {
        case OpCode.create:
          txnHeader =
              new TxnHeader(
                  request.sessionId, request.cxid, zks.getNextZxid(), zks.getTime(), OpCode.create);
          zks.sessionTracker.checkSession(request.sessionId, request.getOwner());
          CreateRequest createRequest = new CreateRequest();
          ZooKeeperServer.byteBuffer2Record(request.request, createRequest);
          String path = createRequest.getPath();
          int lastSlash = path.lastIndexOf('/');
          if (lastSlash == -1 || path.indexOf('\0') != -1 || failCreate) {
            LOG.info(
                "Invalid path " + path + " with session 0x" + Long.toHexString(request.sessionId));
            throw new KeeperException.BadArgumentsException(path);
          }
          if (!fixupACL(request.authInfo, createRequest.getAcl())) {
            throw new KeeperException.InvalidACLException(path);
          }
          String parentPath = path.substring(0, lastSlash);
          ChangeRecord parentRecord = getRecordForPath(parentPath);

          checkACL(zks, parentRecord.acl, ZooDefs.Perms.CREATE, request.authInfo);
          int parentCVersion = parentRecord.stat.getCversion();
          CreateMode createMode = CreateMode.fromFlag(createRequest.getFlags());
          if (createMode.isSequential()) {
            path = path + String.format(Locale.ENGLISH, "%010d", parentCVersion);
          }
          try {
            PathUtils.validatePath(path);
          } catch (IllegalArgumentException ie) {
            LOG.info(
                "Invalid path " + path + " with session 0x" + Long.toHexString(request.sessionId));
            throw new KeeperException.BadArgumentsException(path);
          }
          try {
            if (getRecordForPath(path) != null) {
              throw new KeeperException.NodeExistsException(path);
            }
          } catch (KeeperException.NoNodeException e) {
            // ignore this one
          }
          boolean ephemeralParent = parentRecord.stat.getEphemeralOwner() != 0;
          if (ephemeralParent) {
            throw new KeeperException.NoChildrenForEphemeralsException(path);
          }
          txn =
              new CreateTxn(
                  path, createRequest.getData(), createRequest.getAcl(), createMode.isEphemeral());
          StatPersisted s = new StatPersisted();
          if (createMode.isEphemeral()) {
            s.setEphemeralOwner(request.sessionId);
          }
          parentRecord = parentRecord.duplicate(txnHeader.getZxid());
          parentRecord.childCount++;
          parentRecord.stat.setCversion(parentRecord.stat.getCversion() + 1);
          addChangeRecord(parentRecord);
          addChangeRecord(
              new ChangeRecord(txnHeader.getZxid(), path, s, 0, createRequest.getAcl()));

          break;
        case OpCode.delete:
          txnHeader =
              new TxnHeader(
                  request.sessionId, request.cxid, zks.getNextZxid(), zks.getTime(), OpCode.delete);
          zks.sessionTracker.checkSession(request.sessionId, request.getOwner());
          DeleteRequest deleteRequest = new DeleteRequest();
          ZooKeeperServer.byteBuffer2Record(request.request, deleteRequest);
          path = deleteRequest.getPath();
          lastSlash = path.lastIndexOf('/');
          if (lastSlash == -1
              || path.indexOf('\0') != -1
              || zks.getZKDatabase().isSpecialPath(path)) {
            throw new KeeperException.BadArgumentsException(path);
          }
          parentPath = path.substring(0, lastSlash);
          parentRecord = getRecordForPath(parentPath);
          ChangeRecord nodeRecord = getRecordForPath(path);
          checkACL(zks, parentRecord.acl, ZooDefs.Perms.DELETE, request.authInfo);
          int version = deleteRequest.getVersion();
          if (version != -1 && nodeRecord.stat.getVersion() != version) {
            throw new KeeperException.BadVersionException(path);
          }
          if (nodeRecord.childCount > 0) {
            throw new KeeperException.NotEmptyException(path);
          }
          txn = new DeleteTxn(path);
          parentRecord = parentRecord.duplicate(txnHeader.getZxid());
          parentRecord.childCount--;
          parentRecord.stat.setCversion(parentRecord.stat.getCversion() + 1);
          addChangeRecord(parentRecord);
          addChangeRecord(new ChangeRecord(txnHeader.getZxid(), path, null, -1, null));
          break;
        case OpCode.setData:
          txnHeader =
              new TxnHeader(
                  request.sessionId,
                  request.cxid,
                  zks.getNextZxid(),
                  zks.getTime(),
                  OpCode.setData);
          zks.sessionTracker.checkSession(request.sessionId, request.getOwner());
          SetDataRequest setDataRequest = new SetDataRequest();
          ZooKeeperServer.byteBuffer2Record(request.request, setDataRequest);
          path = setDataRequest.getPath();
          nodeRecord = getRecordForPath(path);
          checkACL(zks, nodeRecord.acl, ZooDefs.Perms.WRITE, request.authInfo);
          version = setDataRequest.getVersion();
          int currentVersion = nodeRecord.stat.getVersion();
          if (version != -1 && version != currentVersion) {
            throw new KeeperException.BadVersionException(path);
          }
          version = currentVersion + 1;
          txn = new SetDataTxn(path, setDataRequest.getData(), version);
          nodeRecord = nodeRecord.duplicate(txnHeader.getZxid());
          nodeRecord.stat.setVersion(version);
          addChangeRecord(nodeRecord);
          break;
        case OpCode.setACL:
          txnHeader =
              new TxnHeader(
                  request.sessionId, request.cxid, zks.getNextZxid(), zks.getTime(), OpCode.setACL);
          zks.sessionTracker.checkSession(request.sessionId, request.getOwner());
          SetACLRequest setAclRequest = new SetACLRequest();
          ZooKeeperServer.byteBuffer2Record(request.request, setAclRequest);
          path = setAclRequest.getPath();
          if (!fixupACL(request.authInfo, setAclRequest.getAcl())) {
            throw new KeeperException.InvalidACLException(path);
          }
          nodeRecord = getRecordForPath(path);
          checkACL(zks, nodeRecord.acl, ZooDefs.Perms.ADMIN, request.authInfo);
          version = setAclRequest.getVersion();
          currentVersion = nodeRecord.stat.getAversion();
          if (version != -1 && version != currentVersion) {
            throw new KeeperException.BadVersionException(path);
          }
          version = currentVersion + 1;
          txn = new SetACLTxn(path, setAclRequest.getAcl(), version);
          nodeRecord = nodeRecord.duplicate(txnHeader.getZxid());
          nodeRecord.stat.setAversion(version);
          addChangeRecord(nodeRecord);
          break;
        case OpCode.createSession:
          txnHeader =
              new TxnHeader(
                  request.sessionId,
                  request.cxid,
                  zks.getNextZxid(),
                  zks.getTime(),
                  OpCode.createSession);
          request.request.rewind();
          int to = request.request.getInt();
          txn = new CreateSessionTxn(to);
          request.request.rewind();
          zks.sessionTracker.addSession(request.sessionId, to);
          zks.setOwner(request.sessionId, request.getOwner());
          break;
        case OpCode.closeSession:
          txnHeader =
              new TxnHeader(
                  request.sessionId,
                  request.cxid,
                  zks.getNextZxid(),
                  zks.getTime(),
                  OpCode.closeSession);
          // We don't want to do this check since the session expiration thread
          // queues up this operation without being the session owner.
          // this request is the last of the session so it should be ok
          // zks.sessionTracker.checkSession(request.sessionId, request.getOwner());
          HashSet<String> es = zks.getZKDatabase().getEphemerals(request.sessionId);
          synchronized (zks.outstandingChanges) {
            for (ChangeRecord c : zks.outstandingChanges) {
              if (c.stat == null) {
                // Doing a delete
                es.remove(c.path);
              } else if (c.stat.getEphemeralOwner() == request.sessionId) {
                es.add(c.path);
              }
            }
            for (String path2Delete : es) {
              addChangeRecord(new ChangeRecord(txnHeader.getZxid(), path2Delete, null, 0, null));
            }

            zks.sessionTracker.setSessionClosing(request.sessionId);
          }

          LOG.info(
              "Processed session termination for sessionid: 0x"
                  + Long.toHexString(request.sessionId));
          break;
        case OpCode.sync:
        case OpCode.exists:
        case OpCode.getData:
        case OpCode.getACL:
        case OpCode.getChildren:
        case OpCode.getChildren2:
        case OpCode.ping:
        case OpCode.setWatches:
          zks.sessionTracker.checkSession(request.sessionId, request.getOwner());
          break;
      }
    } catch (KeeperException e) {
      if (txnHeader != null) {
        txnHeader.setType(OpCode.error);
        txn = new ErrorTxn(e.code().intValue());
      }
      LOG.info(
          "Got user-level KeeperException when processing "
              + request.toString()
              + " Error Path:"
              + e.getPath()
              + " Error:"
              + e.getMessage());
      request.setException(e);
    } catch (Exception e) {
      // log at error level as we are returning a marshalling
      // error to the user
      LOG.error("Failed to process " + request, e);

      StringBuilder sb = new StringBuilder();
      ByteBuffer bb = request.request;
      if (bb != null) {
        bb.rewind();
        while (bb.hasRemaining()) {
          sb.append(Integer.toHexString(bb.get() & 0xff));
        }
      } else {
        sb.append("request buffer is null");
      }

      LOG.error("Dumping request buffer: 0x" + sb.toString());
      if (txnHeader != null) {
        txnHeader.setType(OpCode.error);
        txn = new ErrorTxn(Code.MARSHALLINGERROR.intValue());
      }
    }
    request.hdr = txnHeader;
    request.txn = txn;
    request.zxid = zks.getZxid();
    nextProcessor.processRequest(request);
  }