Search in sources :

Example 6 with ZooCacheFactory

use of org.apache.accumulo.fate.zookeeper.ZooCacheFactory in project accumulo by apache.

the class Tables method getZooCache.

/**
 * Return the cached ZooCache for provided instance. ZooCache is initially created with a watcher that will clear the TableMap cache for that instance when
 * WatchedEvent occurs.
 */
private static ZooCache getZooCache(final Instance instance) {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        sm.checkPermission(TABLES_PERMISSION);
    }
    final String uuid = instance.getInstanceID();
    try {
        return instanceToZooCache.get(uuid, () -> {
            final String zks = instance.getZooKeepers();
            final int timeOut = instance.getZooKeepersSessionTimeOut();
            return new ZooCacheFactory().getZooCache(zks, timeOut, watchedEvent -> instanceToMapCache.invalidate(uuid));
        });
    } catch (ExecutionException e) {
        throw new RuntimeException(e);
    }
}
Also used : ZooCacheFactory(org.apache.accumulo.fate.zookeeper.ZooCacheFactory) ExecutionException(java.util.concurrent.ExecutionException)

Example 7 with ZooCacheFactory

use of org.apache.accumulo.fate.zookeeper.ZooCacheFactory in project accumulo by apache.

the class ServerClient method getConnection.

public static <CT extends TServiceClient> Pair<String, CT> getConnection(ClientContext context, TServiceClientFactory<CT> factory, boolean preferCachedConnections, long rpcTimeout) throws TTransportException {
    checkArgument(context != null, "context is null");
    // create list of servers
    ArrayList<ThriftTransportKey> servers = new ArrayList<>();
    // add tservers
    Instance instance = context.getInstance();
    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));
        }
    }
    boolean opened = false;
    try {
        Pair<String, TTransport> pair = ThriftTransportPool.getInstance().getAnyTransport(servers, preferCachedConnections);
        CT client = ThriftUtil.createClient(factory, pair.getSecond());
        opened = true;
        warnedAboutTServersBeingDown = false;
        return new Pair<>(pair.getFirst(), client);
    } finally {
        if (!opened) {
            if (!warnedAboutTServersBeingDown) {
                if (servers.isEmpty()) {
                    log.warn("There are no tablet servers: check that zookeeper and accumulo are running.");
                } else {
                    log.warn("Failed to find an available server in the list of servers: {}", servers);
                }
                warnedAboutTServersBeingDown = true;
            }
        }
    }
}
Also used : ServerServices(org.apache.accumulo.core.util.ServerServices) Instance(org.apache.accumulo.core.client.Instance) ArrayList(java.util.ArrayList) ZooCache(org.apache.accumulo.fate.zookeeper.ZooCache) ZooCacheFactory(org.apache.accumulo.fate.zookeeper.ZooCacheFactory) TTransport(org.apache.thrift.transport.TTransport) Pair(org.apache.accumulo.core.util.Pair)

Example 8 with ZooCacheFactory

use of org.apache.accumulo.fate.zookeeper.ZooCacheFactory in project accumulo by apache.

the class ConditionalWriterImpl method invalidateSession.

/**
 * The purpose of this code is to ensure that a conditional mutation will not execute when its status is unknown. This allows a user to read the row when the
 * status is unknown and not have to worry about the tserver applying the mutation after the scan.
 *
 * <p>
 * If a conditional mutation is taking a long time to process, then this method will wait for it to finish... unless this exceeds timeout.
 */
private void invalidateSession(SessionID sessionId, HostAndPort location) throws AccumuloException, AccumuloSecurityException, TableNotFoundException {
    long sleepTime = 50;
    long startTime = System.currentTimeMillis();
    Instance instance = context.getInstance();
    LockID lid = new LockID(ZooUtil.getRoot(instance) + Constants.ZTSERVERS, sessionId.lockId);
    ZooCacheFactory zcf = new ZooCacheFactory();
    while (true) {
        if (!ZooLock.isLockHeld(zcf.getZooCache(instance.getZooKeepers(), instance.getZooKeepersSessionTimeOut()), lid)) {
            // ACCUMULO-1152 added a tserver lock check to the tablet location cache, so this invalidation prevents future attempts to contact the
            // tserver even its gone zombie and is still running w/o a lock
            locator.invalidateCache(context.getInstance(), location.toString());
            return;
        }
        try {
            // if the mutation is currently processing, this method will block until its done or times out
            invalidateSession(sessionId.sessionID, location);
            return;
        } catch (TApplicationException tae) {
            throw new AccumuloServerException(location.toString(), tae);
        } catch (TException e) {
            locator.invalidateCache(context.getInstance(), location.toString());
        }
        if ((System.currentTimeMillis() - startTime) + sleepTime > timeout)
            throw new TimedOutException(Collections.singleton(location.toString()));
        sleepUninterruptibly(sleepTime, TimeUnit.MILLISECONDS);
        sleepTime = Math.min(2 * sleepTime, MAX_SLEEP);
    }
}
Also used : TException(org.apache.thrift.TException) ZooCacheFactory(org.apache.accumulo.fate.zookeeper.ZooCacheFactory) Instance(org.apache.accumulo.core.client.Instance) TimedOutException(org.apache.accumulo.core.client.TimedOutException) LockID(org.apache.accumulo.fate.zookeeper.ZooUtil.LockID) TApplicationException(org.apache.thrift.TApplicationException)

Aggregations

ZooCacheFactory (org.apache.accumulo.fate.zookeeper.ZooCacheFactory)8 Instance (org.apache.accumulo.core.client.Instance)5 ZooCache (org.apache.accumulo.fate.zookeeper.ZooCache)5 ArrayList (java.util.ArrayList)3 ServerServices (org.apache.accumulo.core.util.ServerServices)2 TTransport (org.apache.thrift.transport.TTransport)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 AccumuloException (org.apache.accumulo.core.client.AccumuloException)1 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)1 ClientConfiguration (org.apache.accumulo.core.client.ClientConfiguration)1 Connector (org.apache.accumulo.core.client.Connector)1 NamespaceNotFoundException (org.apache.accumulo.core.client.NamespaceNotFoundException)1 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)1 TimedOutException (org.apache.accumulo.core.client.TimedOutException)1 ZooKeeperInstance (org.apache.accumulo.core.client.ZooKeeperInstance)1 ClientContext (org.apache.accumulo.core.client.impl.ClientContext)1 Credentials (org.apache.accumulo.core.client.impl.Credentials)1 MasterClient (org.apache.accumulo.core.client.impl.MasterClient)1