Search in sources :

Example 1 with ThriftTransportKey

use of org.apache.accumulo.core.client.impl.ThriftTransportKey in project accumulo by apache.

the class TransportCachingIT method testCachedTransport.

@Test
public void testCachedTransport() {
    Connector conn = getConnector();
    Instance instance = conn.getInstance();
    ClientConfiguration clientConf = cluster.getClientConfig();
    ClientContext context = new ClientContext(instance, new Credentials(getAdminPrincipal(), getAdminToken()), clientConf);
    long rpcTimeout = ConfigurationTypeHelper.getTimeInMillis(Property.GENERAL_RPC_TIMEOUT.getDefaultValue());
    // create list of servers
    ArrayList<ThriftTransportKey> servers = new ArrayList<>();
    // add tservers
    ZooCache zc = new ZooCacheFactory().getZooCache(instance.getZooKeepers(), instance.getZooKeepersSessionTimeOut());
    for (String tserver : zc.getChildren(ZooUtil.getRoot(instance) + Constants.ZTSERVERS)) {
        String path = ZooUtil.getRoot(instance) + Constants.ZTSERVERS + "/" + tserver;
        byte[] data = ZooUtil.getLockData(zc, path);
        if (data != null) {
            String strData = new String(data, UTF_8);
            if (!strData.equals("master"))
                servers.add(new ThriftTransportKey(new ServerServices(strData).getAddress(Service.TSERV_CLIENT), rpcTimeout, context));
        }
    }
    ThriftTransportPool pool = ThriftTransportPool.getInstance();
    TTransport first = null;
    while (null == first) {
        try {
            // Get a transport (cached or not)
            first = pool.getAnyTransport(servers, true).getSecond();
        } catch (TTransportException e) {
            log.warn("Failed to obtain transport to {}", servers);
        }
    }
    assertNotNull(first);
    // Return it to unreserve it
    pool.returnTransport(first);
    TTransport second = null;
    while (null == second) {
        try {
            // Get a cached transport (should be the first)
            second = pool.getAnyTransport(servers, true).getSecond();
        } catch (TTransportException e) {
            log.warn("Failed obtain 2nd transport to {}", servers);
        }
    }
    // We should get the same transport
    assertTrue("Expected the first and second to be the same instance", first == second);
    // Return the 2nd
    pool.returnTransport(second);
    TTransport third = null;
    while (null == third) {
        try {
            // Get a non-cached transport
            third = pool.getAnyTransport(servers, false).getSecond();
        } catch (TTransportException e) {
            log.warn("Failed obtain 2nd transport to {}", servers);
        }
    }
    assertFalse("Expected second and third transport to be different instances", second == third);
    pool.returnTransport(third);
}
Also used : Connector(org.apache.accumulo.core.client.Connector) ServerServices(org.apache.accumulo.core.util.ServerServices) Instance(org.apache.accumulo.core.client.Instance) ClientContext(org.apache.accumulo.core.client.impl.ClientContext) ArrayList(java.util.ArrayList) TTransportException(org.apache.thrift.transport.TTransportException) ThriftTransportPool(org.apache.accumulo.core.client.impl.ThriftTransportPool) ZooCache(org.apache.accumulo.fate.zookeeper.ZooCache) ZooCacheFactory(org.apache.accumulo.fate.zookeeper.ZooCacheFactory) ThriftTransportKey(org.apache.accumulo.core.client.impl.ThriftTransportKey) TTransport(org.apache.thrift.transport.TTransport) ClientConfiguration(org.apache.accumulo.core.client.ClientConfiguration) Credentials(org.apache.accumulo.core.client.impl.Credentials) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 ClientConfiguration (org.apache.accumulo.core.client.ClientConfiguration)1 Connector (org.apache.accumulo.core.client.Connector)1 Instance (org.apache.accumulo.core.client.Instance)1 ClientContext (org.apache.accumulo.core.client.impl.ClientContext)1 Credentials (org.apache.accumulo.core.client.impl.Credentials)1 ThriftTransportKey (org.apache.accumulo.core.client.impl.ThriftTransportKey)1 ThriftTransportPool (org.apache.accumulo.core.client.impl.ThriftTransportPool)1 ServerServices (org.apache.accumulo.core.util.ServerServices)1 ZooCache (org.apache.accumulo.fate.zookeeper.ZooCache)1 ZooCacheFactory (org.apache.accumulo.fate.zookeeper.ZooCacheFactory)1 TTransport (org.apache.thrift.transport.TTransport)1 TTransportException (org.apache.thrift.transport.TTransportException)1 Test (org.junit.Test)1