use of org.apache.accumulo.fate.zookeeper.ServiceLock in project accumulo by apache.
the class ZombieTServer method main.
public static void main(String[] args) throws Exception {
int port = random.nextInt(30000) + 2000;
var context = new ServerContext(SiteConfiguration.auto());
TransactionWatcher watcher = new TransactionWatcher(context);
final ThriftClientHandler tch = new ThriftClientHandler(context, watcher);
Processor<Iface> processor = new Processor<>(tch);
ServerAddress serverPort = TServerUtils.startTServer(context.getConfiguration(), ThriftServerType.CUSTOM_HS_HA, processor, "ZombieTServer", "walking dead", 2, ThreadPools.DEFAULT_TIMEOUT_MILLISECS, 1000, 10 * 1024 * 1024, null, null, -1, HostAndPort.fromParts("0.0.0.0", port));
String addressString = serverPort.address.toString();
var zLockPath = ServiceLock.path(context.getZooKeeperRoot() + Constants.ZTSERVERS + "/" + addressString);
ZooReaderWriter zoo = context.getZooReaderWriter();
zoo.putPersistentData(zLockPath.toString(), new byte[] {}, NodeExistsPolicy.SKIP);
ServiceLock zlock = new ServiceLock(zoo.getZooKeeper(), zLockPath, UUID.randomUUID());
LockWatcher lw = new LockWatcher() {
@SuppressFBWarnings(value = "DM_EXIT", justification = "System.exit() is a bad idea here, but okay for now, since it's a test")
@Override
public void lostLock(final LockLossReason reason) {
try {
tch.halt(TraceUtil.traceInfo(), null, null);
} catch (Exception ex) {
log.error("Exception", ex);
System.exit(1);
}
}
@SuppressFBWarnings(value = "DM_EXIT", justification = "System.exit() is a bad idea here, but okay for now, since it's a test")
@Override
public void unableToMonitorLockNode(Exception e) {
try {
tch.halt(TraceUtil.traceInfo(), null, null);
} catch (Exception ex) {
log.error("Exception", ex);
System.exit(1);
}
}
};
byte[] lockContent = new ServerServices(addressString, Service.TSERV_CLIENT).toString().getBytes(UTF_8);
if (zlock.tryLock(lw, lockContent)) {
log.debug("Obtained tablet server lock {}", zlock.getLockPath());
}
// modify metadata
synchronized (tch) {
while (!tch.halted) {
tch.wait();
}
}
System.exit(0);
}
use of org.apache.accumulo.fate.zookeeper.ServiceLock in project accumulo by apache.
the class CompactionCoordinator method getCoordinatorLock.
/**
* Set up nodes and locks in ZooKeeper for this CompactionCoordinator
*
* @param clientAddress
* address of this Compactor
* @throws KeeperException
* zookeeper error
* @throws InterruptedException
* thread interrupted
*/
protected void getCoordinatorLock(HostAndPort clientAddress) throws KeeperException, InterruptedException {
LOG.info("trying to get coordinator lock");
final String coordinatorClientAddress = ExternalCompactionUtil.getHostPortString(clientAddress);
final String lockPath = getContext().getZooKeeperRoot() + Constants.ZCOORDINATOR_LOCK;
final UUID zooLockUUID = UUID.randomUUID();
while (true) {
CoordinatorLockWatcher coordinatorLockWatcher = new CoordinatorLockWatcher();
coordinatorLock = new ServiceLock(getContext().getZooReaderWriter().getZooKeeper(), ServiceLock.path(lockPath), zooLockUUID);
coordinatorLock.lock(coordinatorLockWatcher, coordinatorClientAddress.getBytes());
coordinatorLockWatcher.waitForChange();
if (coordinatorLockWatcher.isAcquiredLock()) {
break;
}
if (!coordinatorLockWatcher.isFailedToAcquireLock()) {
throw new IllegalStateException("manager lock in unknown state");
}
coordinatorLock.tryToCancelAsyncLockOrUnlock();
sleepUninterruptibly(1000, TimeUnit.MILLISECONDS);
}
}
use of org.apache.accumulo.fate.zookeeper.ServiceLock in project accumulo by apache.
the class Compactor method announceExistence.
/**
* Set up nodes and locks in ZooKeeper for this Compactor
*
* @param clientAddress
* address of this Compactor
* @throws KeeperException
* zookeeper error
* @throws InterruptedException
* thread interrupted
*/
protected void announceExistence(HostAndPort clientAddress) throws KeeperException, InterruptedException {
String hostPort = ExternalCompactionUtil.getHostPortString(clientAddress);
ZooReaderWriter zoo = getContext().getZooReaderWriter();
String compactorQueuePath = getContext().getZooKeeperRoot() + Constants.ZCOMPACTORS + "/" + this.queueName;
String zPath = compactorQueuePath + "/" + hostPort;
try {
zoo.mkdirs(compactorQueuePath);
zoo.putPersistentData(zPath, new byte[] {}, NodeExistsPolicy.SKIP);
} catch (KeeperException e) {
if (e.code() == KeeperException.Code.NOAUTH) {
LOG.error("Failed to write to ZooKeeper. Ensure that" + " accumulo.properties, specifically instance.secret, is consistent.");
}
throw e;
}
compactorLock = new ServiceLock(getContext().getZooReaderWriter().getZooKeeper(), ServiceLock.path(zPath), compactorId);
LockWatcher lw = new LockWatcher() {
@Override
public void lostLock(final LockLossReason reason) {
Halt.halt(1, () -> {
LOG.error("Compactor lost lock (reason = {}), exiting.", reason);
gcLogger.logGCInfo(getConfiguration());
});
}
@Override
public void unableToMonitorLockNode(final Exception e) {
Halt.halt(1, () -> LOG.error("Lost ability to monitor Compactor lock, exiting.", e));
}
};
try {
byte[] lockContent = new ServerServices(hostPort, Service.COMPACTOR_CLIENT).toString().getBytes(UTF_8);
for (int i = 0; i < 25; i++) {
zoo.putPersistentData(zPath, new byte[0], NodeExistsPolicy.SKIP);
if (compactorLock.tryLock(lw, lockContent)) {
LOG.debug("Obtained Compactor lock {}", compactorLock.getLockPath());
return;
}
LOG.info("Waiting for Compactor lock");
sleepUninterruptibly(5, TimeUnit.SECONDS);
}
String msg = "Too many retries, exiting.";
LOG.info(msg);
throw new RuntimeException(msg);
} catch (Exception e) {
LOG.info("Could not obtain tablet server lock, exiting.", e);
throw new RuntimeException(e);
}
}
use of org.apache.accumulo.fate.zookeeper.ServiceLock in project accumulo by apache.
the class SimpleGarbageCollector method getZooLock.
private void getZooLock(HostAndPort addr) throws KeeperException, InterruptedException {
var path = ServiceLock.path(getContext().getZooKeeperRoot() + Constants.ZGC_LOCK);
LockWatcher lockWatcher = new LockWatcher() {
@Override
public void lostLock(LockLossReason reason) {
Halt.halt("GC lock in zookeeper lost (reason = " + reason + "), exiting!", 1);
}
@Override
public void unableToMonitorLockNode(final Exception e) {
// ACCUMULO-3651 Level changed to error and FATAL added to message for slf4j compatibility
Halt.halt(-1, () -> log.error("FATAL: No longer able to monitor lock node ", e));
}
};
UUID zooLockUUID = UUID.randomUUID();
while (true) {
ServiceLock lock = new ServiceLock(getContext().getZooReaderWriter().getZooKeeper(), path, zooLockUUID);
if (lock.tryLock(lockWatcher, new ServerServices(addr.toString(), Service.GC_CLIENT).toString().getBytes())) {
log.debug("Got GC ZooKeeper lock");
return;
}
log.debug("Failed to get GC ZooKeeper lock, will retry");
sleepUninterruptibly(1, TimeUnit.SECONDS);
}
}
use of org.apache.accumulo.fate.zookeeper.ServiceLock in project accumulo by apache.
the class Manager method getManagerLock.
private void getManagerLock(final ServiceLockPath zManagerLoc) throws KeeperException, InterruptedException {
var zooKeeper = getContext().getZooReaderWriter().getZooKeeper();
log.info("trying to get manager lock");
final String managerClientAddress = getHostname() + ":" + getConfiguration().getPort(Property.MANAGER_CLIENTPORT)[0];
UUID zooLockUUID = UUID.randomUUID();
while (true) {
ManagerLockWatcher managerLockWatcher = new ManagerLockWatcher();
managerLock = new ServiceLock(zooKeeper, zManagerLoc, zooLockUUID);
managerLock.lock(managerLockWatcher, managerClientAddress.getBytes());
managerLockWatcher.waitForChange();
if (managerLockWatcher.acquiredLock) {
break;
}
if (!managerLockWatcher.failedToAcquireLock) {
throw new IllegalStateException("manager lock in unknown state");
}
managerLock.tryToCancelAsyncLockOrUnlock();
sleepUninterruptibly(TIME_TO_WAIT_BETWEEN_LOCK_CHECKS, TimeUnit.MILLISECONDS);
}
setManagerState(ManagerState.HAVE_LOCK);
}
Aggregations