/** * Finds the RPC port of the given tablet's leader tserver. * @param tablet a LocatedTablet * @return the host and port of the given tablet's leader tserver * @throws Exception if we are unable to find the leader tserver */ public HostAndPort findLeaderTabletServer(LocatedTablet tablet) throws Exception { LocatedTablet.Replica leader = null; DeadlineTracker deadlineTracker = new DeadlineTracker(); deadlineTracker.setDeadline(DEFAULT_SLEEP); while (leader == null) { if (deadlineTracker.timedOut()) { fail("Timed out while trying to find a leader for this table"); } leader = tablet.getLeaderReplica(); if (leader == null) { LOG.info("Sleeping while waiting for a tablet LEADER to arise, currently slept {} ms", deadlineTracker.getElapsedMillis()); Thread.sleep(50); } } return new HostAndPort(leader.getRpcHost(), leader.getRpcPort()); }
@Test public void testNoLeader() throws Exception { final int requestBatchSize = 10; CreateTableOptions options = getBasicCreateTableOptions(); KuduTable table = client.createTable( "testNoLeader-" + System.currentTimeMillis(), basicSchema, options); // Lookup the current locations so that we can pass some valid information to discoverTablets. List<LocatedTablet> tablets = asyncClient .locateTable(table, null, null, requestBatchSize, DEFAULT_SLEEP) .join(DEFAULT_SLEEP); LocatedTablet tablet = tablets.get(0); LocatedTablet.Replica leader = tablet.getLeaderReplica(); // Fake a master lookup that only returns one follower for the tablet. List<Master.TabletLocationsPB> tabletLocations = new ArrayList<>(); Master.TabletLocationsPB.Builder tabletPb = Master.TabletLocationsPB.newBuilder(); tabletPb.setPartition(ProtobufUtils.getFakePartitionPB()); tabletPb.setTabletId(ByteString.copyFrom(tablet.getTabletId())); tabletPb.addReplicas(ProtobufUtils.getFakeTabletReplicaPB( "master", leader.getRpcHost(), leader.getRpcPort(), Metadata.RaftPeerPB.Role.FOLLOWER)); tabletLocations.add(tabletPb.build()); try { asyncClient.discoverTablets(table, new byte[0], requestBatchSize, tabletLocations, 1000); fail("discoverTablets should throw an exception if there's no leader"); } catch (NoLeaderFoundException ex) { // Expected. } }