public boolean isRpcThrottleEnabled() throws IOException { try { byte[] upData = ZKUtil.getData(zookeeper, rpcThrottleZNode); return upData == null || Bytes.toBoolean(upData); } catch (KeeperException | InterruptedException e) { throw new IOException("Failed to get rpc throttle", e); } }
@Override public boolean isPeerEnabled(String peerId) throws ReplicationException { try { return Arrays.equals(ENABLED_ZNODE_BYTES, ZKUtil.getData(zookeeper, getPeerStateNode(peerId))); } catch (KeeperException | InterruptedException e) { throw new ReplicationException("Unable to get status of the peer with id=" + peerId, e); } }
@Override public long getWALPosition(ServerName serverName, String queueId, String fileName) throws ReplicationException { byte[] bytes; try { bytes = ZKUtil.getData(zookeeper, getFileNode(serverName, queueId, fileName)); } catch (KeeperException | InterruptedException e) { throw new ReplicationException("Failed to get log position (serverName=" + serverName + ", queueId=" + queueId + ", fileName=" + fileName + ")", e); } try { return ZKUtil.parseWALPositionFrom(bytes); } catch (DeserializationException de) { LOG.warn("Failed parse log position (serverName={}, queueId={}, fileName={})", serverName, queueId, fileName); } // if we can not parse the position, start at the beginning of the wal file again return 0; }
private static void appendPeerState(ZKWatcher zkw, String znodeToProcess, StringBuilder sb) throws KeeperException, InvalidProtocolBufferException { String peerState = zkw.getConfiguration().get("zookeeper.znode.replication.peers.state", "peer-state"); int pblen = ProtobufUtil.lengthOfPBMagic(); for (String child : ZKUtil.listChildrenNoWatch(zkw, znodeToProcess)) { if (!child.equals(peerState)) { continue; } String peerStateZnode = ZNodePaths.joinZNode(znodeToProcess, child); sb.append("\n").append(peerStateZnode).append(": "); byte[] peerStateData; try { peerStateData = ZKUtil.getData(zkw, peerStateZnode); ReplicationProtos.ReplicationState.Builder builder = ReplicationProtos.ReplicationState.newBuilder(); ProtobufUtil.mergeFrom(builder, peerStateData, pblen, peerStateData.length - pblen); sb.append(builder.getState().name()); } catch (IOException ipbe) { LOG.warn("Got Exception while parsing peer: " + znodeToProcess, ipbe); } catch (InterruptedException e) { zkw.interruptedException(e); return; } } }
public static String readClusterIdZNode(ZKWatcher watcher) throws KeeperException { if (ZKUtil.checkExists(watcher, watcher.getZNodePaths().clusterIdZNode) != -1) { byte [] data; try { data = ZKUtil.getData(watcher, watcher.getZNodePaths().clusterIdZNode); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return null; } if (data != null) { try { return ClusterId.parseFrom(data).toString(); } catch (DeserializationException e) { throw ZKUtil.convert(e); } } } return null; }
@Override public void transitPeerSyncReplicationState(String peerId) throws ReplicationException { String newStateNode = getNewSyncReplicationStateNode(peerId); try { byte[] data = ZKUtil.getData(zookeeper, newStateNode); ZKUtil.multiOrSequential(zookeeper, Arrays.asList(ZKUtilOp.setData(newStateNode, NONE_STATE_ZNODE_BYTES), ZKUtilOp.setData(getSyncReplicationStateNode(peerId), data)), false); } catch (KeeperException | InterruptedException e) { throw new ReplicationException( "Error transiting sync replication state for peer with id=" + peerId, e); } }
/** * Get the info port of the backup master if it is available. * Return 0 if no backup master or zookeeper is unavailable * @param sn server name of backup master * @return info port or 0 if timed out or exceptions */ public int getBackupMasterInfoPort(final ServerName sn) { String backupZNode = ZNodePaths.joinZNode(watcher.getZNodePaths().backupMasterAddressesZNode, sn.toString()); try { byte[] data = ZKUtil.getData(watcher, backupZNode); final ZooKeeperProtos.Master backup = parse(data); if (backup == null) { return 0; } return backup.getInfoPort(); } catch (Exception e) { LOG.warn("Failed to get backup master: " + sn + "'s info port.", e); return 0; } }
@Override public ReplicationPeerConfig getPeerConfig(String peerId) throws ReplicationException { byte[] data; try { data = ZKUtil.getData(zookeeper, getPeerNode(peerId)); } catch (KeeperException | InterruptedException e) { throw new ReplicationException("Error getting configuration for peer with id=" + peerId, e); } if (data == null || data.length == 0) { throw new ReplicationException( "Replication peer config data shouldn't be empty, peerId=" + peerId); } try { return ReplicationPeerConfigUtil.parsePeerFrom(data); } catch (DeserializationException e) { throw new ReplicationException( "Failed to parse replication peer config for peer with id=" + peerId, e); } }
private Pair<ServerName, RegionServerInfo> getServerInfo(String name) throws KeeperException, IOException { ServerName serverName = ServerName.parseServerName(name); String nodePath = ZNodePaths.joinZNode(watcher.getZNodePaths().rsZNode, name); byte[] data; try { data = ZKUtil.getData(watcher, nodePath); } catch (InterruptedException e) { throw (InterruptedIOException) new InterruptedIOException().initCause(e); } if (data == null) { // we should receive a children changed event later and then we will expire it, so we still // need to add it to the region server set. LOG.warn("Server node {} does not exist, already dead?", name); return Pair.newPair(serverName, null); } if (data.length == 0 || !ProtobufUtil.isPBMagicPrefix(data)) { // this should not happen actually, unless we have bugs or someone has messed zk up. LOG.warn("Invalid data for region server node {} on zookeeper, data length = {}", name, data.length); return Pair.newPair(serverName, null); } RegionServerInfo.Builder builder = RegionServerInfo.newBuilder(); int magicLen = ProtobufUtil.lengthOfPBMagic(); ProtobufUtil.mergeFrom(builder, data, magicLen, data.length - magicLen); return Pair.newPair(serverName, builder.build()); }
private SyncReplicationState getSyncReplicationState(String peerId, String path) throws ReplicationException { try { byte[] data = ZKUtil.getData(zookeeper, path); if (data == null || data.length == 0) { if (ZKUtil.checkExists(zookeeper, getPeerNode(peerId)) != -1) { // should be a peer from previous version, set the sync replication state for it. ZKUtil.createSetData(zookeeper, path, NONE_STATE_ZNODE_BYTES); return SyncReplicationState.NONE; } else { throw new ReplicationException( "Replication peer sync state shouldn't be empty, peerId=" + peerId); } } return SyncReplicationState.parseFrom(data); } catch (KeeperException | InterruptedException | IOException e) { throw new ReplicationException( "Error getting sync replication state of path " + path + " for peer with id=" + peerId, e); } }
@Test public void testZookeeperNodesForReplicas() throws Exception { // Checks all the znodes exist when meta's replicas are enabled ZKWatcher zkw = TEST_UTIL.getZooKeeperWatcher(); Configuration conf = TEST_UTIL.getConfiguration(); String baseZNode = conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT, HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT); String primaryMetaZnode = ZNodePaths.joinZNode(baseZNode, conf.get("zookeeper.znode.metaserver", "meta-region-server")); // check that the data in the znode is parseable (this would also mean the znode exists) byte[] data = ZKUtil.getData(zkw, primaryMetaZnode); ProtobufUtil.toServerName(data); for (int i = 1; i < 3; i++) { String secZnode = ZNodePaths.joinZNode(baseZNode, conf.get("zookeeper.znode.metaserver", "meta-region-server") + "-" + i); String str = zkw.getZNodePaths().getZNodeForReplica(i); assertTrue(str.equals(secZnode)); // check that the data in the znode is parseable (this would also mean the znode exists) data = ZKUtil.getData(zkw, secZnode); ProtobufUtil.toServerName(data); } }
/** * Create a znode with data */ @Test public void testCreateWithParents() throws KeeperException, InterruptedException { byte[] expectedData = new byte[] { 1, 2, 3 }; ZKUtil.createWithParents(ZKW, "/l1/l2/l3/l4/testCreateWithParents", expectedData); byte[] data = ZKUtil.getData(ZKW, "/l1/l2/l3/l4/testCreateWithParents"); assertTrue(Bytes.equals(expectedData, data)); ZKUtil.deleteNodeRecursively(ZKW, "/l1"); ZKUtil.createWithParents(ZKW, "/testCreateWithParents", expectedData); data = ZKUtil.getData(ZKW, "/testCreateWithParents"); assertTrue(Bytes.equals(expectedData, data)); ZKUtil.deleteNodeRecursively(ZKW, "/testCreateWithParents"); }
/** * Test whether the splitlog correctly creates a task in zookeeper * @throws Exception */ @Test public void testTaskCreation() throws Exception { LOG.info("TestTaskCreation - test the creation of a task in zk"); slm = new SplitLogManager(master, conf); TaskBatch batch = new TaskBatch(); String tasknode = submitTaskAndWait(batch, "foo/1"); byte[] data = ZKUtil.getData(zkw, tasknode); SplitLogTask slt = SplitLogTask.parseFrom(data); LOG.info("Task node created " + slt.toString()); assertTrue(slt.isUnassigned(master.getServerName())); }
@Test public void testSimpleMulti() throws Exception { // null multi ZKUtil.multiOrSequential(zkw, null, false); // empty multi ZKUtil.multiOrSequential(zkw, new LinkedList<>(), false); // single create String path = ZNodePaths.joinZNode(zkw.getZNodePaths().baseZNode, "testSimpleMulti"); LinkedList<ZKUtilOp> singleCreate = new LinkedList<>(); singleCreate.add(ZKUtilOp.createAndFailSilent(path, new byte[0])); ZKUtil.multiOrSequential(zkw, singleCreate, false); assertTrue(ZKUtil.checkExists(zkw, path) != -1); // single setdata LinkedList<ZKUtilOp> singleSetData = new LinkedList<>(); byte [] data = Bytes.toBytes("foobar"); singleSetData.add(ZKUtilOp.setData(path, data)); ZKUtil.multiOrSequential(zkw, singleSetData, false); assertTrue(Bytes.equals(ZKUtil.getData(zkw, path), data)); // single delete LinkedList<ZKUtilOp> singleDelete = new LinkedList<>(); singleDelete.add(ZKUtilOp.deleteNodeFailSilent(path)); ZKUtil.multiOrSequential(zkw, singleDelete, false); assertTrue(ZKUtil.checkExists(zkw, path) == -1); }
@BeforeClass public static void setUp() throws Exception { // Set up the integration test util if (util == null) { util = new IntegrationTestingUtility(); } util.getConfiguration().setInt(HConstants.META_REPLICAS_NUM, 3); util.getConfiguration().setInt( StorefileRefresherChore.REGIONSERVER_STOREFILE_REFRESH_PERIOD, 1000); // Make sure there are three servers. util.initializeCluster(3); ZKWatcher zkw = util.getZooKeeperWatcher(); Configuration conf = util.getConfiguration(); String baseZNode = conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT, HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT); String primaryMetaZnode = ZNodePaths.joinZNode(baseZNode, conf.get("zookeeper.znode.metaserver", "meta-region-server")); // check that the data in the znode is parseable (this would also mean the znode exists) byte[] data = ZKUtil.getData(zkw, primaryMetaZnode); ProtobufUtil.toServerName(data); waitUntilZnodeAvailable(1); waitUntilZnodeAvailable(2); }
@Test public void testCleanZNode() throws Exception { ZKWatcher zkw = new ZKWatcher(TEST_UTIL.getConfiguration(), "testNodeTracker", new TestZKNodeTracker.StubAbortable()); final ServerName sn = ServerName.valueOf("127.0.0.1:52", 45L); ZKUtil.createAndFailSilent(zkw, TEST_UTIL.getConfiguration().get(HConstants.ZOOKEEPER_ZNODE_PARENT, HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT)); final String nodeName = zkw.getZNodePaths().masterAddressZNode; // Check that we manage the case when there is no data ZKUtil.createAndFailSilent(zkw, nodeName); MasterAddressTracker.deleteIfEquals(zkw, sn.toString()); assertNotNull(ZKUtil.getData(zkw, nodeName)); // Check that we don't delete if we're not supposed to ZKUtil.setData(zkw, nodeName, MasterAddressTracker.toByteArray(sn, 0)); MasterAddressTracker.deleteIfEquals(zkw, ServerName.valueOf("127.0.0.2:52", 45L).toString()); assertNotNull(ZKUtil.getData(zkw, nodeName)); // Check that we delete when we're supposed to ZKUtil.setData(zkw, nodeName,MasterAddressTracker.toByteArray(sn, 0)); MasterAddressTracker.deleteIfEquals(zkw, sn.toString()); assertNull(ZKUtil.getData(zkw, nodeName)); // Check that we support the case when the znode does not exist MasterAddressTracker.deleteIfEquals(zkw, sn.toString()); // must not throw an exception }
@Test public void testDeadWorker() throws Exception { LOG.info("testDeadWorker"); conf.setLong("hbase.splitlog.max.resubmit", 0); slm = new SplitLogManager(master, conf); TaskBatch batch = new TaskBatch(); String tasknode = submitTaskAndWait(batch, "foo/1"); int version = ZKUtil.checkExists(zkw, tasknode); final ServerName worker1 = ServerName.valueOf("worker1,1,1"); SplitLogTask slt = new SplitLogTask.Owned(worker1); ZKUtil.setData(zkw, tasknode, slt.toByteArray()); if (tot_mgr_heartbeat.sum() == 0) waitForCounter(tot_mgr_heartbeat, 0, 1, to/2); slm.handleDeadWorker(worker1); if (tot_mgr_resubmit.sum() == 0) waitForCounter(tot_mgr_resubmit, 0, 1, to+to/2); if (tot_mgr_resubmit_dead_server_task.sum() == 0) { waitForCounter(tot_mgr_resubmit_dead_server_task, 0, 1, to + to/2); } int version1 = ZKUtil.checkExists(zkw, tasknode); assertTrue(version1 > version); byte[] taskstate = ZKUtil.getData(zkw, tasknode); slt = SplitLogTask.parseFrom(taskstate); assertTrue(slt.isUnassigned(master.getServerName())); return; }
@Test public void testAcquireTaskAtStartup() throws Exception { LOG.info("testAcquireTaskAtStartup"); SplitLogCounters.resetCounters(); final String TATAS = "tatas"; final ServerName RS = ServerName.valueOf("rs,1,1"); RegionServerServices mockedRS = getRegionServer(RS); zkw.getRecoverableZooKeeper().create(ZKSplitLog.getEncodedNodeName(zkw, TATAS), new SplitLogTask.Unassigned(ServerName.valueOf("mgr,1,1")).toByteArray(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); SplitLogWorker slw = new SplitLogWorker(ds, TEST_UTIL.getConfiguration(), mockedRS, neverEndingTask); slw.start(); try { waitForCounter(SplitLogCounters.tot_wkr_task_acquired, 0, 1, WAIT_TIME); byte [] bytes = ZKUtil.getData(zkw, ZKSplitLog.getEncodedNodeName(zkw, TATAS)); SplitLogTask slt = SplitLogTask.parseFrom(bytes); assertTrue(slt.isOwned(RS)); } finally { stopSplitLogWorker(slw); } }
private TableState.State getTableStateInZK(ZKWatcher watcher, final TableName tableName) throws KeeperException, IOException, InterruptedException { String znode = ZNodePaths.joinZNode(watcher.getZNodePaths().tableZNode, tableName.getNameAsString()); byte [] data = ZKUtil.getData(watcher, znode); if (data == null || data.length <= 0) { return null; } try { ProtobufUtil.expectPBMagicPrefix(data); ZooKeeperProtos.DeprecatedTableState.Builder builder = ZooKeeperProtos.DeprecatedTableState.newBuilder(); int magicLen = ProtobufUtil.lengthOfPBMagic(); ProtobufUtil.mergeFrom(builder, data, magicLen, data.length - magicLen); return TableState.State.valueOf(builder.getState().toString()); } catch (IOException e) { KeeperException ke = new KeeperException.DataInconsistencyException(); ke.initCause(e); throw ke; } catch (DeserializationException e) { throw ZKUtil.convert(e); } } }
@Test public void testTaskResigned() throws Exception { LOG.info("TestTaskResigned - resubmit task node once in RESIGNED state"); assertEquals(0, tot_mgr_resubmit.sum()); slm = new SplitLogManager(master, conf); assertEquals(0, tot_mgr_resubmit.sum()); TaskBatch batch = new TaskBatch(); String tasknode = submitTaskAndWait(batch, "foo/1"); assertEquals(0, tot_mgr_resubmit.sum()); final ServerName worker1 = ServerName.valueOf("worker1,1,1"); assertEquals(0, tot_mgr_resubmit.sum()); SplitLogTask slt = new SplitLogTask.Resigned(worker1); assertEquals(0, tot_mgr_resubmit.sum()); ZKUtil.setData(zkw, tasknode, slt.toByteArray()); ZKUtil.checkExists(zkw, tasknode); // Could be small race here. if (tot_mgr_resubmit.sum() == 0) { waitForCounter(tot_mgr_resubmit, 0, 1, to/2); } assertEquals(1, tot_mgr_resubmit.sum()); byte[] taskstate = ZKUtil.getData(zkw, tasknode); slt = SplitLogTask.parseFrom(taskstate); assertTrue(slt.isUnassigned(master.getServerName())); }