Search in sources :

Example 6 with ServerInfo

use of com.twitter.distributedlog.thrift.service.ServerInfo in project distributedlog by twitter.

the class TestProxyClientManager method testCreateClientShouldHandshake.

@Test(timeout = 60000)
public void testCreateClientShouldHandshake() throws Exception {
    SocketAddress address = createSocketAddress(3000);
    MockProxyClientBuilder builder = new MockProxyClientBuilder();
    ServerInfo serverInfo = new ServerInfo();
    serverInfo.putToOwnerships(runtime.getMethodName() + "_stream", runtime.getMethodName() + "_owner");
    Pair<MockProxyClient, MockServerInfoService> mockProxyClient = createMockProxyClient(address, serverInfo);
    builder.provideProxyClient(address, mockProxyClient.getLeft());
    final AtomicReference<ServerInfo> resultHolder = new AtomicReference<ServerInfo>(null);
    final CountDownLatch doneLatch = new CountDownLatch(1);
    ProxyListener listener = new ProxyListener() {

        @Override
        public void onHandshakeSuccess(SocketAddress address, ProxyClient client, ServerInfo serverInfo) {
            resultHolder.set(serverInfo);
            doneLatch.countDown();
        }

        @Override
        public void onHandshakeFailure(SocketAddress address, ProxyClient client, Throwable cause) {
        }
    };
    ProxyClientManager clientManager = createProxyClientManager(builder, 0L);
    clientManager.registerProxyListener(listener);
    assertEquals("There should be no clients in the manager", 0, clientManager.getNumProxies());
    clientManager.createClient(address);
    assertEquals("Create client should build the proxy client", 1, clientManager.getNumProxies());
    // When a client is created, it would handshake with that proxy
    doneLatch.await();
    assertEquals("Handshake should return server info", serverInfo, resultHolder.get());
}
Also used : ServerInfo(com.twitter.distributedlog.thrift.service.ServerInfo) MockServerInfoService(com.twitter.distributedlog.client.proxy.MockDistributedLogServices.MockServerInfoService) AtomicReference(java.util.concurrent.atomic.AtomicReference) MockProxyClient(com.twitter.distributedlog.client.proxy.MockProxyClientBuilder.MockProxyClient) CountDownLatch(java.util.concurrent.CountDownLatch) MockProxyClient(com.twitter.distributedlog.client.proxy.MockProxyClientBuilder.MockProxyClient) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 7 with ServerInfo

use of com.twitter.distributedlog.thrift.service.ServerInfo in project distributedlog by twitter.

the class DistributedLogServiceImpl method handshakeWithClientInfo.

@Override
public Future<ServerInfo> handshakeWithClientInfo(ClientInfo clientInfo) {
    ServerInfo serverInfo = new ServerInfo();
    closeLock.readLock().lock();
    try {
        serverInfo.setServerStatus(serverStatus);
    } finally {
        closeLock.readLock().unlock();
    }
    if (clientInfo.isSetGetOwnerships() && !clientInfo.isGetOwnerships()) {
        return Future.value(serverInfo);
    }
    Optional<String> regex = Optional.absent();
    if (clientInfo.isSetStreamNameRegex()) {
        regex = Optional.of(clientInfo.getStreamNameRegex());
    }
    Map<String, String> ownershipMap = streamManager.getStreamOwnershipMap(regex);
    serverInfo.setOwnerships(ownershipMap);
    return Future.value(serverInfo);
}
Also used : ServerInfo(com.twitter.distributedlog.thrift.service.ServerInfo)

Aggregations

ServerInfo (com.twitter.distributedlog.thrift.service.ServerInfo)7 SocketAddress (java.net.SocketAddress)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 Stopwatch (com.google.common.base.Stopwatch)3 MockServerInfoService (com.twitter.distributedlog.client.proxy.MockDistributedLogServices.MockServerInfoService)3 MockProxyClient (com.twitter.distributedlog.client.proxy.MockProxyClientBuilder.MockProxyClient)3 InetSocketAddress (java.net.InetSocketAddress)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 FutureEventListener (com.twitter.util.FutureEventListener)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1