static RemoteTablet getTablet(int leaderIndex, int localReplicaIndex) { Master.TabletLocationsPB.Builder tabletPb = Master.TabletLocationsPB.newBuilder(); tabletPb.setPartition(ProtobufUtils.getFakePartitionPB()); tabletPb.setTabletId(ByteString.copyFromUtf8("fake tablet")); List<ServerInfo> servers = new ArrayList<>(); for (int i = 0; i < 3; i++) { InetAddress addr; try { if (i == localReplicaIndex) { addr = InetAddress.getByName("127.0.0.1"); } else { addr = InetAddress.getByName("1.2.3.4"); } } catch (UnknownHostException e) { throw new RuntimeException(e); } String uuid = kUuids[i]; servers.add(new ServerInfo(uuid, new HostAndPort("host", 1000 + i), addr)); tabletPb.addReplicas(ProtobufUtils.getFakeTabletReplicaPB( uuid, "host", i, leaderIndex == i ? Metadata.RaftPeerPB.Role.LEADER : Metadata.RaftPeerPB.Role.FOLLOWER)); } return new RemoteTablet("fake table", tabletPb.build(), servers); } }
tabletPb.setPartition(partition); tabletPb.setTabletId(ByteString.copyFromUtf8("some id " + i)); tabletPb.addReplicas(ProtobufUtils.getFakeTabletReplicaPB( "uuid", badHostname + i, i, Metadata.RaftPeerPB.Role.FOLLOWER)); tabletLocations.add(tabletPb.build());
@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. } }