Search in sources :

Example 11 with SafeRuntimeException

use of com.palantir.logsafe.exceptions.SafeRuntimeException in project atlasdb by palantir.

the class PaxosConsensusTestUtils method setup.

public static PaxosTestState setup(int numLeaders, int quorumSize) {
    List<LeaderElectionService> leaders = new ArrayList<>();
    List<PaxosAcceptor> acceptors = new ArrayList<>();
    List<PaxosLearner> learners = new ArrayList<>();
    List<AtomicBoolean> failureToggles = new ArrayList<>();
    ExecutorService executor = PTExecutors.newCachedThreadPool();
    DataSource sqliteDataSource = SqliteConnections.getDefaultConfiguredPooledDataSource(getSqlitePath());
    RuntimeException exception = new SafeRuntimeException("mock server failure");
    SplittingPaxosStateLog.LegacyOperationMarkers noop = ImmutableLegacyOperationMarkers.builder().markLegacyRead(() -> {
    }).markLegacyWrite(() -> {
    }).build();
    for (int i = 0; i < numLeaders; i++) {
        failureToggles.add(new AtomicBoolean(false));
        PaxosLearner learner = PaxosLearnerImpl.newSplittingLearner(getLearnerStorageParameters(i, sqliteDataSource), noop, PaxosKnowledgeEventRecorder.NO_OP);
        learners.add(ToggleableExceptionProxy.newProxyInstance(PaxosLearner.class, learner, failureToggles.get(i), exception));
        PaxosAcceptor acceptor = PaxosAcceptorImpl.newSplittingAcceptor(getAcceptorStorageParameters(i, sqliteDataSource), noop, Optional.empty());
        acceptors.add(ToggleableExceptionProxy.newProxyInstance(PaxosAcceptor.class, acceptor, failureToggles.get(i), exception));
    }
    PaxosAcceptorNetworkClient acceptorNetworkClient = SingleLeaderAcceptorNetworkClient.createLegacy(acceptors, quorumSize, Maps.toMap(acceptors, $ -> executor), PaxosConstants.CANCEL_REMAINING_CALLS);
    for (int i = 0; i < numLeaders; i++) {
        UUID leaderUuid = UUID.randomUUID();
        PaxosLearner ourLearner = learners.get(i);
        List<PaxosLearner> remoteLearners = learners.stream().filter(learner -> !learner.equals(ourLearner)).collect(ImmutableList.toImmutableList());
        PaxosLearnerNetworkClient learnerNetworkClient = SingleLeaderLearnerNetworkClient.createLegacy(ourLearner, remoteLearners, quorumSize, Maps.toMap(learners, $ -> executor), PaxosConstants.CANCEL_REMAINING_CALLS);
        LeaderElectionService leader = new LeaderElectionServiceBuilder().leaderUuid(leaderUuid).pingRate(Duration.ZERO).randomWaitBeforeProposingLeadership(Duration.ZERO).leaderAddressCacheTtl(Duration.ZERO).knowledge(ourLearner).acceptorClient(acceptorNetworkClient).learnerClient(learnerNetworkClient).leaderPinger(SingleLeaderPinger.createForTests(ImmutableMap.of(), Duration.ZERO, leaderUuid, true, Optional.empty())).build();
        leaders.add(SimulatingFailingServerProxy.newProxyInstance(LeaderElectionService.class, leader, SERVER_DELAY_TIME_MS, failureToggles.get(i)));
    }
    return new PaxosTestState(leaders, learners, failureToggles, executor);
}
Also used : SimulatingFailingServerProxy(com.palantir.leader.proxy.SimulatingFailingServerProxy) LeaderElectionService(com.palantir.leader.LeaderElectionService) LeaderElectionServiceBuilder(com.palantir.leader.LeaderElectionServiceBuilder) ImmutableMap(com.google.common.collect.ImmutableMap) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) ToggleableExceptionProxy(com.palantir.leader.proxy.ToggleableExceptionProxy) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FileUtils(org.apache.commons.io.FileUtils) UUID(java.util.UUID) PaxosKnowledgeEventRecorder(com.palantir.leader.PaxosKnowledgeEventRecorder) Maps(com.google.common.collect.Maps) File(java.io.File) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) PTExecutors(com.palantir.common.concurrent.PTExecutors) Paths(java.nio.file.Paths) Duration(java.time.Duration) DataSource(javax.sql.DataSource) Optional(java.util.Optional) Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) DataSource(javax.sql.DataSource) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) LeaderElectionServiceBuilder(com.palantir.leader.LeaderElectionServiceBuilder) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) ExecutorService(java.util.concurrent.ExecutorService) LeaderElectionService(com.palantir.leader.LeaderElectionService) UUID(java.util.UUID)

Example 12 with SafeRuntimeException

use of com.palantir.logsafe.exceptions.SafeRuntimeException in project atlasdb by palantir.

the class PaxosQuorumChecker method collectResponses.

private static <SERVICE, RESPONSE extends PaxosResponse> PaxosResponsesWithRemote<SERVICE, RESPONSE> collectResponses(ImmutableList<SERVICE> remotes, Function<SERVICE, RESPONSE> request, int quorumSize, Duration remoteRequestTimeout, Predicate<InProgressResponseState<SERVICE, RESPONSE>> shouldSkipNextRequest, boolean cancelRemainingCalls, MultiplexingCompletionService<SERVICE, RESPONSE> responseCompletionService) {
    PaxosResponseAccumulator<SERVICE, RESPONSE> receivedResponses = PaxosResponseAccumulator.newResponse(remotes.size(), quorumSize, shouldSkipNextRequest);
    // kick off all the requests
    List<Future<Map.Entry<SERVICE, RESPONSE>>> allFutures = new ArrayList<>();
    for (SERVICE remote : remotes) {
        try {
            allFutures.add(responseCompletionService.submit(remote, () -> request.apply(remote)));
        } catch (CheckedRejectedExecutionException e) {
            requestExecutionRejection.mark();
            receivedResponses.markFailure();
            if (shouldLogDiagnosticInformation()) {
                log.info("Quorum checker executor rejected task", e);
                log.info("Rate of execution rejections: {}", SafeArg.of("rate1m", requestExecutionRejection.getOneMinuteRate()));
            }
        }
    }
    List<Throwable> encounteredErrors = new ArrayList<>();
    boolean interrupted = false;
    try {
        long deadline = System.nanoTime() + remoteRequestTimeout.toNanos();
        while (receivedResponses.hasMoreRequests() && receivedResponses.shouldProcessNextRequest()) {
            try {
                Future<Map.Entry<SERVICE, RESPONSE>> responseFuture = responseCompletionService.poll(deadline - System.nanoTime(), TimeUnit.NANOSECONDS);
                if (timedOut(responseFuture)) {
                    break;
                }
                receivedResponses.add(responseFuture.get().getKey(), responseFuture.get().getValue());
            } catch (ExecutionException e) {
                receivedResponses.markFailure();
                encounteredErrors.add(e.getCause());
            }
        }
    } catch (InterruptedException e) {
        log.warn("paxos request interrupted", e);
        interrupted = true;
    } finally {
        if (cancelRemainingCalls) {
            cancelOutstandingRequestsAfterTimeout(allFutures);
        }
        if (interrupted) {
            Thread.currentThread().interrupt();
        }
        if (!receivedResponses.hasQuorum()) {
            RuntimeException exceptionForSuppression = new SafeRuntimeException("exception for suppresion");
            encounteredErrors.forEach(throwable -> {
                throwable.addSuppressed(exceptionForSuppression);
                log.warn(PAXOS_MESSAGE_ERROR, throwable);
            });
        }
    }
    return receivedResponses.collect();
}
Also used : ArrayList(java.util.ArrayList) CheckedRejectedExecutionException(com.palantir.common.concurrent.CheckedRejectedExecutionException) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) Future(java.util.concurrent.Future) CheckedRejectedExecutionException(com.palantir.common.concurrent.CheckedRejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map)

Example 13 with SafeRuntimeException

use of com.palantir.logsafe.exceptions.SafeRuntimeException in project atlasdb by palantir.

the class TimestampCorroboratingTimelockService method clocksWentBackwards.

private static RuntimeException clocksWentBackwards(TimestampBounds bounds, TimestampBoundsRecord currentBoundsRecord) {
    RuntimeException runtimeException = new SafeRuntimeException(CLOCKS_WENT_BACKWARDS_MESSAGE);
    log.error(CLOCKS_WENT_BACKWARDS_MESSAGE + ": bounds were {}, operation {}, fresh timestamp of {}.", SafeArg.of("persistedBounds", bounds), SafeArg.of("boundsRecordForCurrentRequest", currentBoundsRecord), runtimeException);
    throw runtimeException;
}
Also used : SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException)

Example 14 with SafeRuntimeException

use of com.palantir.logsafe.exceptions.SafeRuntimeException in project atlasdb by palantir.

the class ForkedJsonFormat method printToString.

/**
 * Like {@code print()}, but writes directly to a {@code String} and returns it.
 */
public static String printToString(UnknownFieldSet fields) {
    try {
        StringBuilder text = new StringBuilder();
        print(fields, text);
        return text.toString();
    } catch (IOException e) {
        throw new SafeRuntimeException("Writing to a StringBuilder threw an IOException (should never happen).", e);
    }
}
Also used : SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) IOException(java.io.IOException)

Example 15 with SafeRuntimeException

use of com.palantir.logsafe.exceptions.SafeRuntimeException in project conjure by palantir.

the class ConjureParser method parseAnnotated.

/**
 * Parse all files & imports (breadth-first).
 */
public static Map<String, AnnotatedConjureSourceFile> parseAnnotated(Collection<File> files) {
    CachingParser parser = new CachingParser();
    Map<String, AnnotatedConjureSourceFile> parsed = new HashMap<>();
    Queue<FileWithProvenance> toProcess = new ArrayDeque<>(files.stream().map(FileWithProvenance::of).collect(Collectors.toList()));
    while (!toProcess.isEmpty()) {
        FileWithProvenance nextFileWithProvenance = toProcess.poll();
        File nextFile;
        try {
            nextFile = nextFileWithProvenance.file().getCanonicalFile();
        } catch (IOException e) {
            throw new SafeRuntimeException("Couldn't canonicalize file path", e);
        }
        String key = nextFile.getAbsolutePath();
        if (!parsed.containsKey(key)) {
            AnnotatedConjureSourceFile annotatedConjureSourceFile = parseSingleFile(parser, nextFile, nextFileWithProvenance.importedFrom());
            parsed.put(key, annotatedConjureSourceFile);
            // Add all imports as files to be parsed
            annotatedConjureSourceFile.importProviders().values().stream().map(File::new).forEach(file -> toProcess.add(FileWithProvenance.of(file, nextFile)));
        }
    }
    return parsed;
}
Also used : SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) HashMap(java.util.HashMap) IOException(java.io.IOException) File(java.io.File) ArrayDeque(java.util.ArrayDeque)

Aggregations

SafeRuntimeException (com.palantir.logsafe.exceptions.SafeRuntimeException)43 IOException (java.io.IOException)13 Test (org.junit.jupiter.api.Test)7 Path (java.nio.file.Path)6 Response (com.palantir.dialogue.Response)5 ImmutableMap (com.google.common.collect.ImmutableMap)3 TestResponse (com.palantir.dialogue.TestResponse)3 DefaultInvocationContext (com.palantir.tritium.event.DefaultInvocationContext)3 InvocationContext (com.palantir.tritium.event.InvocationContext)3 File (java.io.File)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 ImmutableList (com.google.common.collect.ImmutableList)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 Value (com.palantir.atlasdb.keyvalue.api.Value)2 DisableNamespacesResponse (com.palantir.atlasdb.timelock.api.DisableNamespacesResponse)2 SuccessfulDisableNamespacesResponse (com.palantir.atlasdb.timelock.api.SuccessfulDisableNamespacesResponse)2 UnsuccessfulDisableNamespacesResponse (com.palantir.atlasdb.timelock.api.UnsuccessfulDisableNamespacesResponse)2 Channel (com.palantir.dialogue.Channel)2