Search in sources :

Example 21 with ZooCache

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

the class Tables method getTableState.

/**
 * Get the current state of the table using the tableid. The boolean clearCache, if true will clear the table state in zookeeper before fetching the state.
 * Added with ACCUMULO-4574.
 *
 * @param instance
 *          the Accumulo instance.
 * @param tableId
 *          the table id
 * @param clearCachedState
 *          if true clear the table state in zookeeper before checking status
 * @return the table state.
 */
public static TableState getTableState(Instance instance, Table.ID tableId, boolean clearCachedState) {
    String statePath = ZooUtil.getRoot(instance) + Constants.ZTABLES + "/" + tableId.canonicalID() + Constants.ZTABLE_STATE;
    if (clearCachedState) {
        Tables.clearCacheByPath(instance, statePath);
    }
    ZooCache zc = getZooCache(instance);
    byte[] state = zc.get(statePath);
    if (state == null)
        return TableState.UNKNOWN;
    return TableState.valueOf(new String(state, UTF_8));
}
Also used : ZooCache(org.apache.accumulo.fate.zookeeper.ZooCache)

Example 22 with ZooCache

use of org.apache.accumulo.fate.zookeeper.ZooCache 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 23 with ZooCache

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

the class Namespaces method exists.

public static boolean exists(Instance instance, Namespace.ID namespaceId) {
    ZooCache zc = getZooCache(instance);
    List<String> namespaceIds = zc.getChildren(ZooUtil.getRoot(instance) + Constants.ZNAMESPACES);
    return namespaceIds.contains(namespaceId.canonicalID());
}
Also used : ZooCache(org.apache.accumulo.fate.zookeeper.ZooCache)

Example 24 with ZooCache

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

the class Namespaces method getNamespaceName.

/**
 * Look for namespace name in ZK. Throw NamespaceNotFoundException if not found.
 */
public static String getNamespaceName(Instance instance, Namespace.ID namespaceId) throws NamespaceNotFoundException {
    String name;
    ZooCache zc = getZooCache(instance);
    byte[] path = zc.get(ZooUtil.getRoot(instance) + Constants.ZNAMESPACES + "/" + namespaceId.canonicalID() + Constants.ZNAMESPACE_NAME);
    if (path != null)
        name = new String(path, UTF_8);
    else
        throw new NamespaceNotFoundException(namespaceId.canonicalID(), null, "getNamespaceName() failed to find namespace");
    return name;
}
Also used : ZooCache(org.apache.accumulo.fate.zookeeper.ZooCache) NamespaceNotFoundException(org.apache.accumulo.core.client.NamespaceNotFoundException)

Example 25 with ZooCache

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

the class Namespaces method getAllNamespaces.

/**
 * Gets all the namespaces from ZK. The first arg (t) the BiConsumer accepts is the ID and the second (u) is the namespaceName.
 */
private static void getAllNamespaces(Instance instance, BiConsumer<String, String> biConsumer) {
    final ZooCache zc = getZooCache(instance);
    List<String> namespaceIds = zc.getChildren(ZooUtil.getRoot(instance) + Constants.ZNAMESPACES);
    for (String id : namespaceIds) {
        byte[] path = zc.get(ZooUtil.getRoot(instance) + Constants.ZNAMESPACES + "/" + id + Constants.ZNAMESPACE_NAME);
        if (path != null) {
            biConsumer.accept(id, new String(path, UTF_8));
        }
    }
}
Also used : ZooCache(org.apache.accumulo.fate.zookeeper.ZooCache)

Aggregations

ZooCache (org.apache.accumulo.fate.zookeeper.ZooCache)25 Instance (org.apache.accumulo.core.client.Instance)7 Test (org.junit.Test)6 ZooCacheFactory (org.apache.accumulo.fate.zookeeper.ZooCacheFactory)5 ClientConfiguration (org.apache.accumulo.core.client.ClientConfiguration)4 Connector (org.apache.accumulo.core.client.Connector)4 ZooReader (org.apache.accumulo.fate.zookeeper.ZooReader)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ClusterControl (org.apache.accumulo.cluster.ClusterControl)3 File (java.io.File)2 NamespaceNotFoundException (org.apache.accumulo.core.client.NamespaceNotFoundException)2 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)2 ServerServices (org.apache.accumulo.core.util.ServerServices)2 PropCacheKey (org.apache.accumulo.server.conf.ZooCachePropertyAccessor.PropCacheKey)2 Path (org.apache.hadoop.fs.Path)2 TTransport (org.apache.thrift.transport.TTransport)2 FileOutputStream (java.io.FileOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 URL (java.net.URL)1