private static SortedMap<String, String> getMap(Instance instance, boolean nameAsKey) { ZooCache zc = getZooCache(instance); List<String> tableIds = zc.getChildren(ZooUtil.getRoot(instance) + Constants.ZTABLES); TreeMap<String, String> tableMap = new TreeMap<String, String>(); Map<String, String> namespaceIdToNameMap = new HashMap<String, String>(); for (String tableId : tableIds) { byte[] tableName = zc.get( ZooUtil.getRoot(instance) + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_NAME); byte[] nId = zc.get( ZooUtil.getRoot(instance) + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_NAMESPACE); String namespaceName = Constants.DEFAULT_NAMESPACE; // create fully qualified table name if (nId != null) { String namespaceId = new String(nId, Constants.UTF8); if (!namespaceId.equals(Constants.DEFAULT_NAMESPACE_ID)) { try { namespaceName = namespaceIdToNameMap.get(namespaceId); if (namespaceName == null) { namespaceName = Namespaces.getNamespaceName(instance, namespaceId); namespaceIdToNameMap.put(namespaceId, namespaceName); } } catch (NamespaceNotFoundException e) { log.error( "Table (" + tableId + ") contains reference to namespace (" + namespaceId + ") that doesn't exist"); continue; } } } if (tableName != null) { String tableNameStr = qualified(new String(tableName, Constants.UTF8), namespaceName); if (nameAsKey) tableMap.put(tableNameStr, tableId); else tableMap.put(tableId, tableNameStr); } } return tableMap; }
/** Given a zooCache and instanceId, look up the instance name. */ public static String lookupInstanceName(ZooCache zooCache, UUID instanceId) { ArgumentChecker.notNull(zooCache, instanceId); for (String name : zooCache.getChildren(Constants.ZROOT + Constants.ZINSTANCES)) { String instanceNamePath = Constants.ZROOT + Constants.ZINSTANCES + "/" + name; byte[] bytes = zooCache.get(instanceNamePath); UUID iid = UUID.fromString(new String(bytes, Constants.UTF8)); if (iid.equals(instanceId)) { return name; } } return null; }
public static Pair<String, ClientService.Client> getConnection( ClientContext context, boolean preferCachedConnections, long rpcTimeout) throws TTransportException { checkArgument(context != null, "context is null"); // create list of servers ArrayList<ThriftTransportKey> servers = new ArrayList<ThriftTransportKey>(); // 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); ClientService.Client client = ThriftUtil.createClient(new ClientService.Client.Factory(), pair.getSecond()); opened = true; warnedAboutTServersBeingDown = false; return new Pair<String, ClientService.Client>(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; } } } }
public static boolean exists(Instance instance, String tableId) { ZooCache zc = getZooCache(instance); List<String> tableIds = zc.getChildren(ZooUtil.getRoot(instance) + Constants.ZTABLES); return tableIds.contains(tableId); }