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);
}
}
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;
}
}
}
}
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);
}
}
Aggregations