Example #1
0
  public void testHosts() throws Exception {
    ZkRing ring = ZkRing.create(getZk(), coordinator, ring_group_root, 1, null, 1);
    assertEquals(0, ring.getHosts().size());

    Host host = ring.addHost(LOCALHOST);
    assertEquals(LOCALHOST, host.getAddress());
    for (int i = 0; i < 20; i++) {
      if (!ring.getHosts().isEmpty()) {
        break;
      }
      Thread.sleep(100);
    }
    assertEquals(Collections.singleton(host), ring.getHosts());

    assertEquals(LOCALHOST, ring.getHostByAddress(LOCALHOST).getAddress());
    ring.close();

    // assure that hosts reload well, too
    ring = new ZkRing(getZk(), ring_root, null, coordinator);
    assertEquals(1, ring.getHosts().size());

    assertEquals(Collections.singleton(host), ring.getHosts());

    assertEquals(LOCALHOST, ring.getHostByAddress(LOCALHOST).getAddress());

    assertTrue(ring.removeHost(LOCALHOST));
    assertNull(ring.getHostByAddress(LOCALHOST));
    assertFalse(ring.removeHost(LOCALHOST));

    ring.close();
  }
  public void testBothUp() throws IOException, TException, InterruptedException {

    MockIface iface1 = new Response1Iface();
    MockIface iface2 = new Response1Iface();

    startMockPartitionServerThread1(iface1, 1);
    startMockPartitionServerThread2(iface2, 1);

    Map<Host, List<HostConnection>> hostToConnectionsMap =
        new HashMap<Host, List<HostConnection>>();

    int tryLockTimeoutMs = 100;
    int establishConnectionTimeoutMs = 100;
    int queryTimeoutMs = 10;
    int bulkQueryTimeoutMs = 100;

    hostToConnectionsMap.put(
        mockHost1,
        Collections.singletonList(
            new HostConnection(
                mockHost1,
                tryLockTimeoutMs,
                establishConnectionTimeoutMs,
                queryTimeoutMs,
                bulkQueryTimeoutMs)));
    hostToConnectionsMap.put(
        mockHost2,
        Collections.singletonList(
            new HostConnection(
                mockHost2,
                tryLockTimeoutMs,
                establishConnectionTimeoutMs,
                queryTimeoutMs,
                bulkQueryTimeoutMs)));

    HostConnectionPool hostConnectionPool = new HostConnectionPool(hostToConnectionsMap, null);

    mockHost1.setState(HostState.SERVING);
    mockHost2.setState(HostState.SERVING);

    int numHits = 0;

    for (int i = 0; i < 10; ++i) {
      HankResponse response = hostConnectionPool.get(0, KEY_1, 1, null);
      assertEquals(RESPONSE_1, response);
      if (response.is_set_value()) {
        ++numHits;
      }
    }
    assertEquals("Gets should be distributed accross hosts", 5, iface1.numGets);
    assertEquals("Gets should be distributed accross hosts", 5, iface2.numGets);
    assertEquals("All keys should have been found", 10, numHits);
  }
Example #3
0
  public void testListenersPreservedWhenHostAdded() throws Exception {
    ZkRing rc = ZkRing.create(getZk(), getRoot() + "/ring-group-one", 1, null, 10);
    Host h1 = rc.addHost(new PartDaemonAddress("localhost", 1));
    MockHostCommandQueueChangeListener l1 = new MockHostCommandQueueChangeListener();
    h1.setCommandQueueChangeListener(l1);
    MockHostStateChangeListener l2 = new MockHostStateChangeListener();
    h1.setStateChangeListener(l2);

    rc.addHost(new PartDaemonAddress("localhost", 2));

    h1.setState(HostState.UPDATING);
    synchronized (l2) {
      l2.wait(WAIT_TIME);
    }
    assertEquals(h1, l2.calledWith);

    h1.enqueueCommand(HostCommand.EXECUTE_UPDATE);
    l1.waitForNotification();
    assertEquals(h1, l1.calledWith);
  }
  public void testDeterministicHostListShuffling()
      throws IOException, TException, InterruptedException {

    MockIface iface1 = new Response1Iface();
    MockIface iface2 = new Response1Iface();

    startMockPartitionServerThread1(iface1, 1);
    startMockPartitionServerThread2(iface2, 1);

    Map<Host, List<HostConnection>> hostToConnectionsMap =
        new HashMap<Host, List<HostConnection>>();

    int tryLockTimeoutMs = 100;
    int establishConnectionTimeoutMs = 100;
    int queryTimeoutMs = 100;
    int bulkQueryTimeoutMs = 100;

    hostToConnectionsMap.put(
        mockHost1,
        Collections.singletonList(
            new HostConnection(
                mockHost1,
                tryLockTimeoutMs,
                establishConnectionTimeoutMs,
                queryTimeoutMs,
                bulkQueryTimeoutMs)));
    hostToConnectionsMap.put(
        mockHost2,
        Collections.singletonList(
            new HostConnection(
                mockHost2,
                tryLockTimeoutMs,
                establishConnectionTimeoutMs,
                queryTimeoutMs,
                bulkQueryTimeoutMs)));

    mockHost1.setState(HostState.SERVING);
    mockHost2.setState(HostState.SERVING);

    for (int n = 0; n < 1024; ++n) {
      int numHits = 0;
      // Note: creating connection pools with a host shuffling seed
      HostConnectionPool hostConnectionPoolA = new HostConnectionPool(hostToConnectionsMap, n);
      HostConnectionPool hostConnectionPoolB = new HostConnectionPool(hostToConnectionsMap, n);

      // Connection pools should try the same host first for a given key hash
      final int keyHash = 42;
      for (int i = 0; i < 10; ++i) {
        HankResponse responseA = hostConnectionPoolA.get(0, KEY_1, 1, keyHash);
        HankResponse responseB = hostConnectionPoolB.get(0, KEY_1, 1, keyHash);
        assertEquals(RESPONSE_1, responseA);
        assertEquals(RESPONSE_1, responseB);
        if (responseA.is_set_value() && responseB.is_set_value()) {
          numHits += 2;
        }
      }
      assertNotSame("Gets should not be distributed accross hosts", iface1.numGets, iface2.numGets);
      assertTrue(
          "All gets should have been served by one host",
          (iface1.numGets == 20 && iface2.numGets == 0)
              || (iface1.numGets == 0 && iface2.numGets == 20));
      assertEquals("All keys should have been found", 20, numHits);
      iface1.clearCounts();
      iface2.clearCounts();
    }
  }
  public void testOneHanging() throws IOException, TException, InterruptedException {

    MockIface iface1 = new HangingIface();
    MockIface iface2 = new Response1Iface();

    startMockPartitionServerThread1(iface1, 1);
    startMockPartitionServerThread2(iface2, 1);

    Map<Host, List<HostConnection>> hostToConnectionsMap =
        new HashMap<Host, List<HostConnection>>();

    int tryLockTimeoutMs = 100;
    int establishConnectionTimeoutMs = 100;
    int queryTimeoutMs = 10;
    int bulkQueryTimeoutMs = 100;

    hostToConnectionsMap.put(
        mockHost1,
        Collections.singletonList(
            new HostConnection(
                mockHost1,
                tryLockTimeoutMs,
                establishConnectionTimeoutMs,
                queryTimeoutMs,
                bulkQueryTimeoutMs)));
    hostToConnectionsMap.put(
        mockHost2,
        Collections.singletonList(
            new HostConnection(
                mockHost2,
                tryLockTimeoutMs,
                establishConnectionTimeoutMs,
                queryTimeoutMs,
                bulkQueryTimeoutMs)));

    HostConnectionPool hostConnectionPool = new HostConnectionPool(hostToConnectionsMap, null);

    mockHost1.setState(HostState.SERVING);
    mockHost2.setState(HostState.SERVING);

    int numHits;

    // With max num retries = 1
    numHits = 0;
    iface1.clearCounts();
    iface2.clearCounts();
    for (int i = 0; i < 10; ++i) {
      HankResponse response = hostConnectionPool.get(0, KEY_1, 1, null);
      LOG.trace(
          "Num retries = 1, sequence index = "
              + i
              + " host 1 gets = "
              + iface1.numGets
              + ", host 2 gets = "
              + iface2.numGets);
      if (response.is_set_value()) {
        ++numHits;
      }
    }

    while (iface1.numGets != 5) {
      LOG.info("Waiting for all hanging calls to host 1 to finish...");
      Thread.sleep(100);
    }

    assertEquals("Half the requests should have failed with Host 1", 5, iface1.numGets);
    assertEquals("Half the requests should have succeeded with Host 2", 5, iface2.numGets);
    assertEquals("Half the keys should have been found", 5, numHits);

    // With max num retries = 2
    numHits = 0;
    iface1.clearCounts();
    iface2.clearCounts();
    for (int i = 0; i < 10; ++i) {
      HankResponse response = hostConnectionPool.get(0, KEY_1, 2, null);
      LOG.trace(
          "Num retries = 2, sequence index = "
              + i
              + " host 1 gets = "
              + iface1.numGets
              + ", host 2 gets = "
              + iface2.numGets);
      assertEquals(RESPONSE_1, response);
      if (response.is_set_value()) {
        ++numHits;
      }
    }

    while (iface1.numGets != 5) {
      LOG.info("Waiting for all hanging calls to host 1 to finish...");
      Thread.sleep(100);
    }

    assertEquals("Half the requests should have failed with Host 1", 5, iface1.numGets);
    assertEquals("Host 2 should have served all requests", 10, iface2.numGets);
    assertEquals("All keys should have been found", 10, numHits);
  }
 @Override
 public void setUp() throws Exception {
   super.setUp();
   mockHost1.setState(HostState.OFFLINE);
   mockHost2.setState(HostState.OFFLINE);
 }