Search in sources :

Example 1 with ServiceNotAvailableException

use of com.palantir.common.remoting.ServiceNotAvailableException in project atlasdb by palantir.

the class MultiNodeClockSkewMonitorIntegrationTest method registersMultipleExceptionsIfThereAreMultipleFailures.

@Test
public void registersMultipleExceptionsIfThereAreMultipleFailures() {
    Exception serviceNotAvailable = new ServiceNotAvailableException("foo");
    Exception badRequest = new BadRequestException("bar");
    when(remoteClock1.getSystemTimeInNanos()).thenThrow(serviceNotAvailable);
    when(remoteClock2.getSystemTimeInNanos()).thenThrow(badRequest);
    tickOneIteration();
    verify(events).exception(eq(serviceNotAvailable));
    verify(events).exception(eq(badRequest));
}
Also used : ServiceNotAvailableException(com.palantir.common.remoting.ServiceNotAvailableException) BadRequestException(javax.ws.rs.BadRequestException) BadRequestException(javax.ws.rs.BadRequestException) ServiceNotAvailableException(com.palantir.common.remoting.ServiceNotAvailableException) Test(org.junit.Test)

Example 2 with ServiceNotAvailableException

use of com.palantir.common.remoting.ServiceNotAvailableException in project atlasdb by palantir.

the class AwaitingLeadershipProxy method handleInvocation.

@Override
protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
    final LeadershipToken leadershipToken = leadershipTokenRef.get();
    if (leadershipToken == null) {
        throw notCurrentLeaderException("method invoked on a non-leader");
    }
    if (method.getName().equals("close") && args.length == 0) {
        isClosed = true;
        executor.shutdownNow();
        clearDelegate();
        return null;
    }
    Object delegate = delegateRef.get();
    StillLeadingStatus leading = null;
    for (int i = 0; i < MAX_NO_QUORUM_RETRIES; i++) {
        // TODO(nziebart): check if leadershipTokenRef has been nulled out between iterations?
        leading = leaderElectionService.isStillLeading(leadershipToken);
        if (leading != StillLeadingStatus.NO_QUORUM) {
            break;
        }
    }
    // and should assume we're not the leader
    if (leading == StillLeadingStatus.NOT_LEADING || leading == StillLeadingStatus.NO_QUORUM) {
        markAsNotLeading(leadershipToken, null);
    }
    if (isClosed) {
        throw new IllegalStateException("already closed proxy for " + interfaceClass.getName());
    }
    Preconditions.checkNotNull(delegate, "%s backing is null", interfaceClass.getName());
    try {
        return method.invoke(delegate, args);
    } catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof ServiceNotAvailableException || e.getTargetException() instanceof NotCurrentLeaderException) {
            markAsNotLeading(leadershipToken, e.getCause());
        }
        // Prevent blocked lock requests from receiving a non-retryable 500 on interrupts in case of a leader election.
        if (e.getTargetException() instanceof InterruptedException && !isStillCurrentToken(leadershipToken)) {
            throw notCurrentLeaderException("received an interrupt due to leader election.", e.getTargetException());
        }
        throw e.getTargetException();
    }
}
Also used : ServiceNotAvailableException(com.palantir.common.remoting.ServiceNotAvailableException) LeadershipToken(com.palantir.leader.LeaderElectionService.LeadershipToken) StillLeadingStatus(com.palantir.leader.LeaderElectionService.StillLeadingStatus) NotCurrentLeaderException(com.palantir.leader.NotCurrentLeaderException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with ServiceNotAvailableException

use of com.palantir.common.remoting.ServiceNotAvailableException in project atlasdb by palantir.

the class PaxosTransactionService method putUnlessExists.

@Override
public void putUnlessExists(long startTimestamp, long commitTimestamp) throws KeyAlreadyExistsException {
    try {
        byte[] finalValue = proposer.propose(startTimestamp, PtBytes.toBytes(commitTimestamp));
        long finalCommitTs = PtBytes.toLong(finalValue);
        try {
            // Make sure we do this put before we return because we want #get to succeed.
            kvStore.putAll(ImmutableMap.of(startTimestamp, finalCommitTs));
        } catch (KeyAlreadyExistsException e) {
        // this case isn't worrisome
        }
        if (commitTimestamp != finalCommitTs) {
            throw new KeyAlreadyExistsException("Key " + startTimestamp + " already exists and is mapped to " + finalCommitTs);
        }
    } catch (PaxosRoundFailureException e) {
        throw new ServiceNotAvailableException("Could not store trascaction");
    }
}
Also used : ServiceNotAvailableException(com.palantir.common.remoting.ServiceNotAvailableException) PaxosRoundFailureException(com.palantir.paxos.PaxosRoundFailureException) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException)

Example 4 with ServiceNotAvailableException

use of com.palantir.common.remoting.ServiceNotAvailableException in project atlasdb by palantir.

the class LockServiceImpl method lockWithFullLockResponse.

@Override
public // We're concerned about sanitizing logs at the info level and above. This method just logs at debug and info.
LockResponse lockWithFullLockResponse(LockClient client, LockRequest request) throws InterruptedException {
    Preconditions.checkNotNull(client);
    Preconditions.checkArgument(!client.equals(INTERNAL_LOCK_GRANT_CLIENT));
    Preconditions.checkArgument(request.getLockTimeout().compareTo(maxAllowedLockTimeout) <= 0, "Requested lock timeout (%s) is greater than maximum allowed lock timeout (%s)", request.getLockTimeout(), maxAllowedLockTimeout);
    long startTime = System.currentTimeMillis();
    if (requestLogger.isDebugEnabled()) {
        requestLogger.debug("LockServiceImpl processing lock request {} for requesting thread {}", UnsafeArg.of("lockRequest", request), SafeArg.of("requestingThread", request.getCreatingThreadName()));
    }
    Map<ClientAwareReadWriteLock, LockMode> locks = Maps.newLinkedHashMap();
    if (isShutDown.get()) {
        throw new ServiceNotAvailableException("This lock server is shut down.");
    }
    try {
        boolean indefinitelyBlocking = isIndefinitelyBlocking(request.getBlockingMode());
        if (indefinitelyBlocking) {
            indefinitelyBlockingThreads.add(Thread.currentThread());
        }
        outstandingLockRequestMultimap.put(client, request);
        Map<LockDescriptor, LockClient> failedLocks = Maps.newHashMap();
        @Nullable Long deadline = (request.getBlockingDuration() == null) ? null : System.nanoTime() + request.getBlockingDuration().toNanos();
        if (request.getBlockingMode() == BLOCK_UNTIL_TIMEOUT) {
            if (request.getLockGroupBehavior() == LOCK_AS_MANY_AS_POSSIBLE) {
                tryLocks(client, request, DO_NOT_BLOCK, null, LOCK_AS_MANY_AS_POSSIBLE, locks, failedLocks);
            }
        }
        tryLocks(client, request, request.getBlockingMode(), deadline, request.getLockGroupBehavior(), locks, failedLocks);
        if (request.getBlockingMode() == BlockingMode.BLOCK_INDEFINITELY_THEN_RELEASE) {
            if (log.isTraceEnabled()) {
                logNullResponse(client, request, null);
            }
            if (requestLogger.isDebugEnabled()) {
                requestLogger.debug("Timed out requesting {} for requesting thread {} after {} ms", UnsafeArg.of("request", request), SafeArg.of("threadName", request.getCreatingThreadName()), SafeArg.of("timeoutMillis", System.currentTimeMillis() - startTime));
            }
            return new LockResponse(failedLocks);
        }
        if (locks.isEmpty() || ((request.getLockGroupBehavior() == LOCK_ALL_OR_NONE) && (locks.size() < request.getLockDescriptors().size()))) {
            if (log.isTraceEnabled()) {
                logNullResponse(client, request, null);
            }
            if (requestLogger.isDebugEnabled()) {
                requestLogger.debug("Failed to acquire all locks for {} for requesting thread {} after {} ms", UnsafeArg.of("request", request), SafeArg.of("threadName", request.getCreatingThreadName()), SafeArg.of("waitMillis", System.currentTimeMillis() - startTime));
            }
            if (requestLogger.isTraceEnabled()) {
                logLockAcquisitionFailure(failedLocks);
            }
            return new LockResponse(null, failedLocks);
        }
        Builder<LockDescriptor, LockMode> lockDescriptorMap = ImmutableSortedMap.naturalOrder();
        for (Entry<ClientAwareReadWriteLock, LockMode> entry : locks.entrySet()) {
            lockDescriptorMap.put(entry.getKey().getDescriptor(), entry.getValue());
        }
        if (request.getVersionId() != null) {
            versionIdMap.put(client, request.getVersionId());
        }
        HeldLocksToken token = createHeldLocksToken(client, LockCollections.of(lockDescriptorMap.build()), LockCollections.of(locks), request.getLockTimeout(), request.getVersionId(), request.getCreatingThreadName());
        locks.clear();
        if (log.isTraceEnabled()) {
            logNullResponse(client, request, token);
        }
        if (Thread.interrupted()) {
            throw new InterruptedException("Interrupted while locking.");
        }
        if (requestLogger.isDebugEnabled()) {
            requestLogger.debug("Successfully acquired locks {} for requesting thread {} after {} ms", UnsafeArg.of("request", request), SafeArg.of("threadName", request.getCreatingThreadName()), SafeArg.of("waitMillis", System.currentTimeMillis() - startTime));
        }
        return new LockResponse(token, failedLocks);
    } finally {
        outstandingLockRequestMultimap.remove(client, request);
        indefinitelyBlockingThreads.remove(Thread.currentThread());
        try {
            for (Entry<ClientAwareReadWriteLock, LockMode> entry : locks.entrySet()) {
                entry.getKey().get(client, entry.getValue()).unlock();
            }
        } catch (Throwable e) {
            // (authorized)
            log.error("Internal lock server error: state has been corrupted!!", UnsafeArg.of("exception", e), SafeArg.of("stacktrace", e.getStackTrace()));
            throw Throwables.throwUncheckedException(e);
        }
    }
}
Also used : ServiceNotAvailableException(com.palantir.common.remoting.ServiceNotAvailableException) LockMode(com.palantir.lock.LockMode) LockDescriptor(com.palantir.lock.LockDescriptor) StringLockDescriptor(com.palantir.lock.StringLockDescriptor) LockResponse(com.palantir.lock.LockResponse) HeldLocksToken(com.palantir.lock.HeldLocksToken) SimpleHeldLocksToken(com.palantir.lock.SimpleHeldLocksToken) LockClient(com.palantir.lock.LockClient) Nullable(javax.annotation.Nullable)

Example 5 with ServiceNotAvailableException

use of com.palantir.common.remoting.ServiceNotAvailableException in project atlasdb by palantir.

the class MultiNodeClockSkewMonitorIntegrationTest method registersBothFailuresAndClockSkew.

@Test
public void registersBothFailuresAndClockSkew() {
    Exception serviceNotAvailable = new ServiceNotAvailableException("foo");
    when(remoteClock1.getSystemTimeInNanos()).thenThrow(serviceNotAvailable);
    // The first value is a peculiarity of the implementation. We get the local time and THEN query the remote
    // server.
    when(localClock.getSystemTimeInNanos()).thenReturn(100L, 110L, 120L);
    when(remoteClock2.getSystemTimeInNanos()).thenReturn(115L);
    tickOneIteration();
    when(localClock.getSystemTimeInNanos()).thenReturn(200L, 210L, 220L);
    when(remoteClock2.getSystemTimeInNanos()).thenReturn(515L + 110L);
    tickOneIteration();
    verify(events).clockSkew(REMOTE_HOST_2, 0L, 110L, 10L);
    verify(events).clockSkew(REMOTE_HOST_2, 400L, 90L, 10L);
    verify(events, times(2)).exception(serviceNotAvailable);
}
Also used : ServiceNotAvailableException(com.palantir.common.remoting.ServiceNotAvailableException) BadRequestException(javax.ws.rs.BadRequestException) ServiceNotAvailableException(com.palantir.common.remoting.ServiceNotAvailableException) Test(org.junit.Test)

Aggregations

ServiceNotAvailableException (com.palantir.common.remoting.ServiceNotAvailableException)5 BadRequestException (javax.ws.rs.BadRequestException)2 Test (org.junit.Test)2 KeyAlreadyExistsException (com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException)1 LeadershipToken (com.palantir.leader.LeaderElectionService.LeadershipToken)1 StillLeadingStatus (com.palantir.leader.LeaderElectionService.StillLeadingStatus)1 NotCurrentLeaderException (com.palantir.leader.NotCurrentLeaderException)1 HeldLocksToken (com.palantir.lock.HeldLocksToken)1 LockClient (com.palantir.lock.LockClient)1 LockDescriptor (com.palantir.lock.LockDescriptor)1 LockMode (com.palantir.lock.LockMode)1 LockResponse (com.palantir.lock.LockResponse)1 SimpleHeldLocksToken (com.palantir.lock.SimpleHeldLocksToken)1 StringLockDescriptor (com.palantir.lock.StringLockDescriptor)1 PaxosRoundFailureException (com.palantir.paxos.PaxosRoundFailureException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Nullable (javax.annotation.Nullable)1