Search in sources :

Example 6 with ServiceLock

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);
}
Also used : Processor(org.apache.accumulo.core.tabletserver.thrift.TabletClientService.Processor) ServerServices(org.apache.accumulo.core.util.ServerServices) ServerAddress(org.apache.accumulo.server.rpc.ServerAddress) ZooReaderWriter(org.apache.accumulo.fate.zookeeper.ZooReaderWriter) Iface(org.apache.accumulo.core.tabletserver.thrift.TabletClientService.Iface) TransactionWatcher(org.apache.accumulo.server.zookeeper.TransactionWatcher) ServerContext(org.apache.accumulo.server.ServerContext) ServiceLock(org.apache.accumulo.fate.zookeeper.ServiceLock) LockWatcher(org.apache.accumulo.fate.zookeeper.ServiceLock.LockWatcher) LockLossReason(org.apache.accumulo.fate.zookeeper.ServiceLock.LockLossReason)

Example 7 with ServiceLock

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);
    }
}
Also used : ServiceLock(org.apache.accumulo.fate.zookeeper.ServiceLock) UUID(java.util.UUID)

Example 8 with ServiceLock

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);
    }
}
Also used : ServerServices(org.apache.accumulo.core.util.ServerServices) ServiceLock(org.apache.accumulo.fate.zookeeper.ServiceLock) LockWatcher(org.apache.accumulo.fate.zookeeper.ServiceLock.LockWatcher) ZooReaderWriter(org.apache.accumulo.fate.zookeeper.ZooReaderWriter) LockLossReason(org.apache.accumulo.fate.zookeeper.ServiceLock.LockLossReason) KeeperException(org.apache.zookeeper.KeeperException) TTransportException(org.apache.thrift.transport.TTransportException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) ThriftSecurityException(org.apache.accumulo.core.clientImpl.thrift.ThriftSecurityException) RetriesExceededException(org.apache.accumulo.server.compaction.RetryableThriftCall.RetriesExceededException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) KeeperException(org.apache.zookeeper.KeeperException) TException(org.apache.thrift.TException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ThriftTableOperationException(org.apache.accumulo.core.clientImpl.thrift.ThriftTableOperationException) UnknownCompactionIdException(org.apache.accumulo.core.compaction.thrift.UnknownCompactionIdException)

Example 9 with ServiceLock

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);
    }
}
Also used : ServerServices(org.apache.accumulo.core.util.ServerServices) ServiceLock(org.apache.accumulo.fate.zookeeper.ServiceLock) LockWatcher(org.apache.accumulo.fate.zookeeper.ServiceLock.LockWatcher) LockLossReason(org.apache.accumulo.fate.zookeeper.ServiceLock.LockLossReason) UUID(java.util.UUID) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) FileNotFoundException(java.io.FileNotFoundException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 10 with ServiceLock

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);
}
Also used : ServiceLock(org.apache.accumulo.fate.zookeeper.ServiceLock) UUID(java.util.UUID)

Aggregations

ServiceLock (org.apache.accumulo.fate.zookeeper.ServiceLock)16 ZooReaderWriter (org.apache.accumulo.fate.zookeeper.ZooReaderWriter)8 Test (org.junit.Test)8 LockLossReason (org.apache.accumulo.fate.zookeeper.ServiceLock.LockLossReason)5 LockWatcher (org.apache.accumulo.fate.zookeeper.ServiceLock.LockWatcher)5 UUID (java.util.UUID)4 ServerServices (org.apache.accumulo.core.util.ServerServices)4 IOException (java.io.IOException)3 KeeperException (org.apache.zookeeper.KeeperException)3 ZooKeeper (org.apache.zookeeper.ZooKeeper)3 UnknownHostException (java.net.UnknownHostException)2 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)2 ServerContext (org.apache.accumulo.server.ServerContext)2 TException (org.apache.thrift.TException)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 FileNotFoundException (java.io.FileNotFoundException)1 Field (java.lang.reflect.Field)1 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)1 ThriftSecurityException (org.apache.accumulo.core.clientImpl.thrift.ThriftSecurityException)1