Java Code Examples for org.apache.hadoop.hbase.zookeeper.ZKUtil#deleteNode()

The following examples show how to use org.apache.hadoop.hbase.zookeeper.ZKUtil#deleteNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MasterMetaBootstrap.java    From hbase with Apache License 2.0 6 votes vote down vote up
private void unassignExcessMetaReplica(int numMetaReplicasConfigured) {
  final ZKWatcher zooKeeper = master.getZooKeeper();
  // unassign the unneeded replicas (for e.g., if the previous master was configured
  // with a replication of 3 and now it is 2, we need to unassign the 1 unneeded replica)
  try {
    List<String> metaReplicaZnodes = zooKeeper.getMetaReplicaNodes();
    for (String metaReplicaZnode : metaReplicaZnodes) {
      int replicaId = zooKeeper.getZNodePaths().getMetaReplicaIdFromZnode(metaReplicaZnode);
      if (replicaId >= numMetaReplicasConfigured) {
        RegionState r = MetaTableLocator.getMetaRegionState(zooKeeper, replicaId);
        LOG.info("Closing excess replica of meta region " + r.getRegion());
        // send a close and wait for a max of 30 seconds
        ServerManager.closeRegionSilentlyAndWait(master.getAsyncClusterConnection(),
            r.getServerName(), r.getRegion(), 30000);
        ZKUtil.deleteNode(zooKeeper, zooKeeper.getZNodePaths().getZNodeForReplica(replicaId));
      }
    }
  } catch (Exception ex) {
    // ignore the exception since we don't want the master to be wedged due to potential
    // issues in the cleanup of the extra regions. We can do that cleanup via hbck or manually
    LOG.warn("Ignoring exception " + ex);
  }
}
 
Example 2
Source File: ZKReplicationQueueStorage.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public void removeLastSequenceIds(String peerId) throws ReplicationException {
  String suffix = "-" + peerId;
  try {
    StringBuilder sb = new StringBuilder(regionsZNode);
    int regionsZNodeLength = regionsZNode.length();
    int levelOneLength = regionsZNodeLength + 3;
    int levelTwoLength = levelOneLength + 3;
    List<String> levelOneDirs = ZKUtil.listChildrenNoWatch(zookeeper, regionsZNode);
    // it is possible that levelOneDirs is null if we haven't write any last pushed sequence ids
    // yet, so we need an extra check here.
    if (CollectionUtils.isEmpty(levelOneDirs)) {
      return;
    }
    for (String levelOne : levelOneDirs) {
      sb.append(ZNodePaths.ZNODE_PATH_SEPARATOR).append(levelOne);
      for (String levelTwo : ZKUtil.listChildrenNoWatch(zookeeper, sb.toString())) {
        sb.append(ZNodePaths.ZNODE_PATH_SEPARATOR).append(levelTwo);
        for (String znode : ZKUtil.listChildrenNoWatch(zookeeper, sb.toString())) {
          if (znode.endsWith(suffix)) {
            sb.append(ZNodePaths.ZNODE_PATH_SEPARATOR).append(znode);
            ZKUtil.deleteNode(zookeeper, sb.toString());
            sb.setLength(levelTwoLength);
          }
        }
        sb.setLength(levelOneLength);
      }
      sb.setLength(regionsZNodeLength);
    }
  } catch (KeeperException e) {
    throw new ReplicationException("Failed to remove all last sequence ids, peerId=" + peerId, e);
  }
}
 
Example 3
Source File: ZKSecretWatcher.java    From hbase with Apache License 2.0 5 votes vote down vote up
public void removeKeyFromZK(AuthenticationKey key) {
  String keyZNode = getKeyNode(key.getKeyId());
  try {
    ZKUtil.deleteNode(watcher, keyZNode);
  } catch (KeeperException.NoNodeException nne) {
    LOG.error("Non-existent znode "+keyZNode+" for key "+key.getKeyId(), nne);
  } catch (KeeperException ke) {
    LOG.error(HBaseMarkers.FATAL, "Failed removing znode "+keyZNode+" for key "+
        key.getKeyId(), ke);
    watcher.abort("Unhandled zookeeper error removing znode "+keyZNode+
        " for key "+key.getKeyId(), ke);
  }
}
 
Example 4
Source File: HBaseFsck.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void cleanupHbckZnode() {
  try {
    if (zkw != null && hbckZodeCreated) {
      ZKUtil.deleteNode(zkw, hbckEphemeralNodePath);
      hbckZodeCreated = false;
    }
  } catch (KeeperException e) {
    // Ignore
    if (!e.code().equals(KeeperException.Code.NONODE)) {
      LOG.warn("Delete HBCK znode " + hbckEphemeralNodePath + " failed ", e);
    }
  }
}
 
Example 5
Source File: TestReplicationTrackerZKImpl.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetListOfRegionServers() throws Exception {
  // 0 region servers
  assertEquals(0, rt.getListOfRegionServers().size());

  // 1 region server
  ZKUtil.createWithParents(zkw,
    ZNodePaths.joinZNode(zkw.getZNodePaths().rsZNode, "hostname1.example.org:1234"));
  List<String> rss = rt.getListOfRegionServers();
  assertEquals(rss.toString(), 1, rss.size());

  // 2 region servers
  ZKUtil.createWithParents(zkw,
    ZNodePaths.joinZNode(zkw.getZNodePaths().rsZNode, "hostname2.example.org:1234"));
  rss = rt.getListOfRegionServers();
  assertEquals(rss.toString(), 2, rss.size());

  // 1 region server
  ZKUtil.deleteNode(zkw,
    ZNodePaths.joinZNode(zkw.getZNodePaths().rsZNode, "hostname2.example.org:1234"));
  rss = rt.getListOfRegionServers();
  assertEquals(1, rss.size());

  // 0 region server
  ZKUtil.deleteNode(zkw,
    ZNodePaths.joinZNode(zkw.getZNodePaths().rsZNode, "hostname1.example.org:1234"));
  rss = rt.getListOfRegionServers();
  assertEquals(rss.toString(), 0, rss.size());
}
 
Example 6
Source File: TestReplicationTrackerZKImpl.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegionServerRemovedEvent() throws Exception {
  ZKUtil.createAndWatch(zkw,
    ZNodePaths.joinZNode(zkw.getZNodePaths().rsZNode, "hostname2.example.org:1234"),
    HConstants.EMPTY_BYTE_ARRAY);
  rt.registerListener(new DummyReplicationListener());
  // delete one
  ZKUtil.deleteNode(zkw,
    ZNodePaths.joinZNode(zkw.getZNodePaths().rsZNode, "hostname2.example.org:1234"));
  // wait for event
  while (rsRemovedCount.get() < 1) {
    Thread.sleep(5);
  }
  assertEquals("hostname2.example.org:1234", rsRemovedData);
}
 
Example 7
Source File: TestActiveMasterManager.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test public void testRestartMaster() throws IOException, KeeperException {
  ZKWatcher zk = new ZKWatcher(TEST_UTIL.getConfiguration(),
    "testActiveMasterManagerFromZK", null, true);
  try {
    ZKUtil.deleteNode(zk, zk.getZNodePaths().masterAddressZNode);
    ZKUtil.deleteNode(zk, zk.getZNodePaths().clusterStateZNode);
  } catch(KeeperException.NoNodeException nne) {}

  // Create the master node with a dummy address
  ServerName master = ServerName.valueOf("localhost", 1, System.currentTimeMillis());
  // Should not have a master yet
  DummyMaster dummyMaster = new DummyMaster(zk,master);
  ClusterStatusTracker clusterStatusTracker =
    dummyMaster.getClusterStatusTracker();
  ActiveMasterManager activeMasterManager =
    dummyMaster.getActiveMasterManager();
  assertFalse(activeMasterManager.clusterHasActiveMaster.get());
  assertFalse(activeMasterManager.getActiveMasterServerName().isPresent());

  // First test becoming the active master uninterrupted
  MonitoredTask status = Mockito.mock(MonitoredTask.class);
  clusterStatusTracker.setClusterUp();

  activeMasterManager.blockUntilBecomingActiveMaster(100, status);
  assertTrue(activeMasterManager.clusterHasActiveMaster.get());
  assertMaster(zk, master);
  assertMaster(zk, activeMasterManager.getActiveMasterServerName().get());

  // Now pretend master restart
  DummyMaster secondDummyMaster = new DummyMaster(zk,master);
  ActiveMasterManager secondActiveMasterManager =
    secondDummyMaster.getActiveMasterManager();
  assertFalse(secondActiveMasterManager.clusterHasActiveMaster.get());
  activeMasterManager.blockUntilBecomingActiveMaster(100, status);
  assertTrue(activeMasterManager.clusterHasActiveMaster.get());
  assertMaster(zk, master);
  assertMaster(zk, activeMasterManager.getActiveMasterServerName().get());
  assertMaster(zk, secondActiveMasterManager.getActiveMasterServerName().get());
}
 
Example 8
Source File: HBaseFsck.java    From hbase with Apache License 2.0 4 votes vote down vote up
private void unassignMetaReplica(HbckRegionInfo hi)
  throws IOException, InterruptedException, KeeperException {
  undeployRegions(hi);
  ZKUtil.deleteNode(zkw,
    zkw.getZNodePaths().getZNodeForReplica(hi.getMetaEntry().getRegionInfo().getReplicaId()));
}
 
Example 9
Source File: HRegionServer.java    From hbase with Apache License 2.0 4 votes vote down vote up
private void deleteMyEphemeralNode() throws KeeperException {
  ZKUtil.deleteNode(this.zooKeeper, getMyEphemeralNodePath());
}
 
Example 10
Source File: TestMetaShutdownHandler.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * This test will test the expire handling of a meta-carrying
 * region server.
 * After HBaseMiniCluster is up, we will delete the ephemeral
 * node of the meta-carrying region server, which will trigger
 * the expire of this region server on the master.
 * On the other hand, we will slow down the abort process on
 * the region server so that it is still up during the master SSH.
 * We will check that the master SSH is still successfully done.
 */
@Test
public void testExpireMetaRegionServer() throws Exception {
  MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
  HMaster master = cluster.getMaster();
  RegionStates regionStates = master.getAssignmentManager().getRegionStates();
  ServerName metaServerName =
    regionStates.getRegionServerOfRegion(RegionInfoBuilder.FIRST_META_REGIONINFO);
  if (master.getServerName().equals(metaServerName) || metaServerName == null ||
    !metaServerName.equals(cluster.getServerHoldingMeta())) {
    // Move meta off master
    metaServerName =
      cluster.getLiveRegionServerThreads().get(0).getRegionServer().getServerName();
    master.move(RegionInfoBuilder.FIRST_META_REGIONINFO.getEncodedNameAsBytes(),
      Bytes.toBytes(metaServerName.getServerName()));
    TEST_UTIL.waitUntilNoRegionsInTransition(60000);
    metaServerName =
      regionStates.getRegionServerOfRegion(RegionInfoBuilder.FIRST_META_REGIONINFO);
  }
  RegionState metaState = MetaTableLocator.getMetaRegionState(master.getZooKeeper());
  assertEquals("Wrong state for meta!", RegionState.State.OPEN, metaState.getState());
  assertNotEquals("Meta is on master!", metaServerName, master.getServerName());

  // Delete the ephemeral node of the meta-carrying region server.
  // This is trigger the expire of this region server on the master.
  String rsEphemeralNodePath =
      ZNodePaths.joinZNode(master.getZooKeeper().getZNodePaths().rsZNode,
              metaServerName.toString());
  ZKUtil.deleteNode(master.getZooKeeper(), rsEphemeralNodePath);
  LOG.info("Deleted the znode for the RegionServer hosting hbase:meta; waiting on SSH");
  // Wait for SSH to finish
  final ServerManager serverManager = master.getServerManager();
  final ServerName priorMetaServerName = metaServerName;
  TEST_UTIL.waitFor(120000, 200, new Waiter.Predicate<Exception>() {
    @Override
    public boolean evaluate() throws Exception {
      return !serverManager.isServerOnline(priorMetaServerName)
          && !serverManager.areDeadServersInProgress();
    }
  });
  LOG.info("Past wait on RIT");
  TEST_UTIL.waitUntilNoRegionsInTransition(60000);
  // Now, make sure meta is assigned
  assertTrue("Meta should be assigned",
    regionStates.isRegionOnline(RegionInfoBuilder.FIRST_META_REGIONINFO));
  // Now, make sure meta is registered in zk
  metaState = MetaTableLocator.getMetaRegionState(master.getZooKeeper());
  assertEquals("Meta should not be in transition", RegionState.State.OPEN, metaState.getState());
  assertEquals("Meta should be assigned", metaState.getServerName(),
    regionStates.getRegionServerOfRegion(RegionInfoBuilder.FIRST_META_REGIONINFO));
  assertNotEquals("Meta should be assigned on a different server", metaState.getServerName(),
    metaServerName);
}
 
Example 11
Source File: TestActiveMasterManager.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Unit tests that uses ZooKeeper but does not use the master-side methods
 * but rather acts directly on ZK.
 * @throws Exception
 */
@Test
public void testActiveMasterManagerFromZK() throws Exception {
  ZKWatcher zk = new ZKWatcher(TEST_UTIL.getConfiguration(),
    "testActiveMasterManagerFromZK", null, true);
  try {
    ZKUtil.deleteNode(zk, zk.getZNodePaths().masterAddressZNode);
    ZKUtil.deleteNode(zk, zk.getZNodePaths().clusterStateZNode);
  } catch(KeeperException.NoNodeException nne) {}

  // Create the master node with a dummy address
  ServerName firstMasterAddress =
      ServerName.valueOf("localhost", 1, System.currentTimeMillis());
  ServerName secondMasterAddress =
      ServerName.valueOf("localhost", 2, System.currentTimeMillis());

  // Should not have a master yet
  DummyMaster ms1 = new DummyMaster(zk,firstMasterAddress);
  ActiveMasterManager activeMasterManager =
    ms1.getActiveMasterManager();
  assertFalse(activeMasterManager.clusterHasActiveMaster.get());
  assertFalse(activeMasterManager.getActiveMasterServerName().isPresent());

  // First test becoming the active master uninterrupted
  ClusterStatusTracker clusterStatusTracker =
    ms1.getClusterStatusTracker();
  clusterStatusTracker.setClusterUp();
  activeMasterManager.blockUntilBecomingActiveMaster(100,
      Mockito.mock(MonitoredTask.class));
  assertTrue(activeMasterManager.clusterHasActiveMaster.get());
  assertMaster(zk, firstMasterAddress);
  assertMaster(zk, activeMasterManager.getActiveMasterServerName().get());

  // New manager will now try to become the active master in another thread
  WaitToBeMasterThread t = new WaitToBeMasterThread(zk, secondMasterAddress);
  t.start();
  // Wait for this guy to figure out there is another active master
  // Wait for 1 second at most
  int sleeps = 0;
  while(!t.manager.clusterHasActiveMaster.get() && sleeps < 100) {
    Thread.sleep(10);
    sleeps++;
  }

  // Both should see that there is an active master
  assertTrue(activeMasterManager.clusterHasActiveMaster.get());
  assertTrue(t.manager.clusterHasActiveMaster.get());
  // But secondary one should not be the active master
  assertFalse(t.isActiveMaster);
  // Verify the active master ServerName is populated in standby master.
  assertEquals(firstMasterAddress, t.manager.getActiveMasterServerName().get());

  // Close the first server and delete it's master node
  ms1.stop("stopping first server");

  // Use a listener to capture when the node is actually deleted
  NodeDeletionListener listener = new NodeDeletionListener(zk,
          zk.getZNodePaths().masterAddressZNode);
  zk.registerListener(listener);

  LOG.info("Deleting master node");
  ZKUtil.deleteNode(zk, zk.getZNodePaths().masterAddressZNode);

  // Wait for the node to be deleted
  LOG.info("Waiting for active master manager to be notified");
  listener.waitForDeletion();
  LOG.info("Master node deleted");

  // Now we expect the secondary manager to have and be the active master
  // Wait for 1 second at most
  sleeps = 0;
  while(!t.isActiveMaster && sleeps < 100) {
    Thread.sleep(10);
    sleeps++;
  }
  LOG.debug("Slept " + sleeps + " times");

  assertTrue(t.manager.clusterHasActiveMaster.get());
  assertTrue(t.isActiveMaster);
  assertEquals(secondMasterAddress, t.manager.getActiveMasterServerName().get());

  LOG.info("Deleting master node");

  ZKUtil.deleteNode(zk, zk.getZNodePaths().masterAddressZNode);
}