Search in sources :

Example 1 with CompositeException

use of rx.exceptions.CompositeException in project azure-sdk-for-java by Azure.

the class ExternalChildResourceCollectionImpl method commitAsync.

/**
     * Commits the changes in the external child resource childCollection.
     * <p/>
     * This method returns an observable stream, its observer's onNext will be called for each successfully
     * committed resource followed by one call to 'onCompleted' or one call to 'onError' with a
     * {@link CompositeException } containing the list of exceptions where each exception describes the reason
     * for failure of a resource commit.
     *
     * @return the observable stream
     */
public Observable<FluentModelTImpl> commitAsync() {
    final ExternalChildResourceCollectionImpl<FluentModelTImpl, FluentModelT, InnerModelT, ParentImplT, ParentT> self = this;
    List<FluentModelTImpl> items = new ArrayList<>();
    for (FluentModelTImpl item : this.childCollection.values()) {
        items.add(item);
    }
    final List<Throwable> exceptionsList = Collections.synchronizedList(new ArrayList<Throwable>());
    Observable<FluentModelTImpl> deleteStream = Observable.from(items).filter(new Func1<FluentModelTImpl, Boolean>() {

        @Override
        public Boolean call(FluentModelTImpl childResource) {
            return childResource.pendingOperation() == ExternalChildResourceImpl.PendingOperation.ToBeRemoved;
        }
    }).flatMap(new Func1<FluentModelTImpl, Observable<FluentModelTImpl>>() {

        @Override
        public Observable<FluentModelTImpl> call(final FluentModelTImpl childResource) {
            return childResource.deleteAsync().map(new Func1<Void, FluentModelTImpl>() {

                @Override
                public FluentModelTImpl call(Void response) {
                    return childResource;
                }
            }).doOnNext(new Action1<FluentModelTImpl>() {

                @Override
                public void call(FluentModelTImpl childResource) {
                    childResource.setPendingOperation(ExternalChildResourceImpl.PendingOperation.None);
                    self.childCollection.remove(childResource.name());
                }
            }).onErrorResumeNext(new Func1<Throwable, Observable<FluentModelTImpl>>() {

                @Override
                public Observable<FluentModelTImpl> call(Throwable throwable) {
                    exceptionsList.add(throwable);
                    return Observable.empty();
                }
            });
        }
    });
    Observable<FluentModelTImpl> createStream = Observable.from(items).filter(new Func1<FluentModelTImpl, Boolean>() {

        @Override
        public Boolean call(FluentModelTImpl childResource) {
            return childResource.pendingOperation() == ExternalChildResourceImpl.PendingOperation.ToBeCreated;
        }
    }).flatMap(new Func1<FluentModelTImpl, Observable<FluentModelTImpl>>() {

        @Override
        public Observable<FluentModelTImpl> call(final FluentModelTImpl childResource) {
            return childResource.createAsync().map(new Func1<FluentModelT, FluentModelTImpl>() {

                @Override
                public FluentModelTImpl call(FluentModelT fluentModelT) {
                    return childResource;
                }
            }).doOnNext(new Action1<FluentModelTImpl>() {

                @Override
                public void call(FluentModelTImpl fluentModelT) {
                    childResource.setPendingOperation(ExternalChildResourceImpl.PendingOperation.None);
                }
            }).onErrorResumeNext(new Func1<Throwable, Observable<? extends FluentModelTImpl>>() {

                @Override
                public Observable<FluentModelTImpl> call(Throwable throwable) {
                    self.childCollection.remove(childResource.name());
                    exceptionsList.add(throwable);
                    return Observable.empty();
                }
            });
        }
    });
    Observable<FluentModelTImpl> updateStream = Observable.from(items).filter(new Func1<FluentModelTImpl, Boolean>() {

        @Override
        public Boolean call(FluentModelTImpl childResource) {
            return childResource.pendingOperation() == ExternalChildResourceImpl.PendingOperation.ToBeUpdated;
        }
    }).flatMap(new Func1<FluentModelTImpl, Observable<FluentModelTImpl>>() {

        @Override
        public Observable<FluentModelTImpl> call(final FluentModelTImpl childResource) {
            return childResource.updateAsync().map(new Func1<FluentModelT, FluentModelTImpl>() {

                @Override
                public FluentModelTImpl call(FluentModelT e) {
                    return childResource;
                }
            }).doOnNext(new Action1<FluentModelTImpl>() {

                @Override
                public void call(FluentModelTImpl childResource) {
                    childResource.setPendingOperation(ExternalChildResourceImpl.PendingOperation.None);
                }
            }).onErrorResumeNext(new Func1<Throwable, Observable<? extends FluentModelTImpl>>() {

                @Override
                public Observable<FluentModelTImpl> call(Throwable throwable) {
                    exceptionsList.add(throwable);
                    return Observable.empty();
                }
            });
        }
    });
    final PublishSubject<FluentModelTImpl> aggregatedErrorStream = PublishSubject.create();
    Observable<FluentModelTImpl> operationsStream = Observable.merge(deleteStream, createStream, updateStream).doOnTerminate(new Action0() {

        @Override
        public void call() {
            if (clearAfterCommit()) {
                self.childCollection.clear();
            }
            if (exceptionsList.isEmpty()) {
                aggregatedErrorStream.onCompleted();
            } else {
                aggregatedErrorStream.onError(new CompositeException(exceptionsList));
            }
        }
    });
    Observable<FluentModelTImpl> stream = Observable.concat(operationsStream, aggregatedErrorStream);
    return stream;
}
Also used : Action0(rx.functions.Action0) CompositeException(rx.exceptions.CompositeException) ArrayList(java.util.ArrayList) Observable(rx.Observable) Func1(rx.functions.Func1)

Example 2 with CompositeException

use of rx.exceptions.CompositeException in project azure-sdk-for-java by Azure.

the class DAGErrorTests method testCompositeError.

@Test
public void testCompositeError() {
    // Terminate on error strategy used in this task group is
    // TaskGroupTerminateOnErrorStrategy::TERMINATE_ON_INPROGRESS_TASKS_COMPLETION
    // Tasks marked X (B & G) will fault. B and G are not depends on each other.
    // If B start at time 't0'th ms then G starts ~'t1 = (t0 + 250)'th ms.
    // After B start, it asynchronously wait and emit an error at time '(t0 + 3500)' ms.
    // In this setup, G gets ~3250 ms to start before B emits error. Eventually G also
    // emit error.
    // The final stream, emits result of all tasks that B and G directly or indirectly
    // depends on and terminate with composite exception (that composes exception from
    // B and G).
    /**
         *                                                                        |--------->[M](0)
         *                                                                        |
         *                                                       |==============>[J](1)----->[N](0)
         *                                 X                     |
         *   |------------------>[D](4)-->[B](3)--------------->[A](2)======================>[K](0)
         *   |                             ^                     ^
         *   |                             |                     |
         *  [F](6)---->[E](5)--------------|                     |
         *   |          |         X                              |
         *   |          |------->[G](4)-->[C](3)------------------
         *   |                    |
         *   |                    |============================>[L](2)---------->[P](1)=====>[Q](0)
         *   |
         *   |------------------------------------------------------------------>[H](1)----->[I](0)
         */
    PancakeImpl pancakeM = new PancakeImpl("M", 250);
    PancakeImpl pancakeN = new PancakeImpl("N", 250);
    PancakeImpl pancakeK = new PancakeImpl("K", 250);
    PancakeImpl pancakeQ = new PancakeImpl("Q", 250);
    PancakeImpl pancakeI = new PancakeImpl("I", 250);
    PancakeImpl pancakeJ = new PancakeImpl("J", 250);
    pancakeJ.withInstantPancake(pancakeM);
    pancakeJ.withInstantPancake(pancakeN);
    PancakeImpl pancakeP = new PancakeImpl("P", 250);
    pancakeP.withDelayedPancake(pancakeQ);
    PancakeImpl pancakeH = new PancakeImpl("H", 250);
    pancakeH.withInstantPancake(pancakeI);
    PancakeImpl pancakeA = new PancakeImpl("A", 250);
    PancakeImpl pancakeL = new PancakeImpl("L", 250);
    pancakeL.withInstantPancake(pancakeP);
    // Task B wait for 3500 ms then emit error
    PancakeImpl pancakeB = new PancakeImpl("B", 3500, true);
    pancakeB.withInstantPancake(pancakeA);
    PancakeImpl pancakeC = new PancakeImpl("C", 250);
    pancakeC.withInstantPancake(pancakeA);
    PancakeImpl pancakeD = new PancakeImpl("D", 250);
    pancakeD.withInstantPancake(pancakeB);
    // Task G wait for 250 ms then emit error
    PancakeImpl pancakeG = new PancakeImpl("G", 250, true);
    pancakeG.withInstantPancake(pancakeC);
    pancakeG.withDelayedPancake(pancakeL);
    PancakeImpl pancakeE = new PancakeImpl("E", 250);
    pancakeE.withInstantPancake(pancakeB);
    pancakeE.withInstantPancake(pancakeG);
    PancakeImpl pancakeF = new PancakeImpl("F", 250);
    pancakeF.withInstantPancake(pancakeD);
    pancakeF.withInstantPancake(pancakeE);
    pancakeF.withInstantPancake(pancakeH);
    pancakeA.withDelayedPancake(pancakeJ);
    pancakeA.withDelayedPancake(pancakeK);
    final Set<String> expectedToSee = new HashSet<>();
    expectedToSee.add("M");
    expectedToSee.add("N");
    expectedToSee.add("K");
    expectedToSee.add("Q");
    expectedToSee.add("I");
    expectedToSee.add("J");
    expectedToSee.add("P");
    expectedToSee.add("H");
    expectedToSee.add("A");
    expectedToSee.add("L");
    expectedToSee.add("C");
    final Set<String> seen = new HashSet<>();
    final List<Throwable> exceptions = new ArrayList<>();
    IPancake rootPancake = pancakeF.createAsync().map(new Func1<Indexable, IPancake>() {

        @Override
        public IPancake call(Indexable indexable) {
            IPancake pancake = (IPancake) indexable;
            String name = pancake.name();
            System.out.println("map.onNext:" + name);
            seen.add(name);
            return pancake;
        }
    }).onErrorResumeNext(new Func1<Throwable, Observable<IPancake>>() {

        @Override
        public Observable<IPancake> call(Throwable throwable) {
            System.out.println("map.onErrorResumeNext:" + throwable);
            exceptions.add(throwable);
            return Observable.empty();
        }
    }).toBlocking().last();
    Assert.assertTrue(Sets.difference(expectedToSee, seen).isEmpty());
    Assert.assertEquals(exceptions.size(), 1);
    Assert.assertTrue(exceptions.get(0) instanceof CompositeException);
    CompositeException compositeException = (CompositeException) exceptions.get(0);
    Assert.assertEquals(compositeException.getExceptions().size(), 2);
    for (Throwable throwable : compositeException.getExceptions()) {
        String message = throwable.getMessage();
        Assert.assertTrue(message.equalsIgnoreCase("B") || message.equalsIgnoreCase("G"));
    }
}
Also used : CompositeException(rx.exceptions.CompositeException) ArrayList(java.util.ArrayList) Observable(rx.Observable) Indexable(com.microsoft.azure.management.resources.fluentcore.model.Indexable) Func1(rx.functions.Func1) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with CompositeException

use of rx.exceptions.CompositeException in project azure-tools-for-java by Microsoft.

the class SparkBatchJobDebuggerRunner method execute.

@Override
protected void execute(@NotNull ExecutionEnvironment environment, @Nullable Callback callback, @NotNull RunProfileState state) throws ExecutionException {
    SparkBatchJobSubmissionState submissionState = (SparkBatchJobSubmissionState) state;
    SparkSubmitModel submitModel = submissionState.getSubmitModel();
    SparkSubmissionParameter submissionParameter = submitModel.getSubmissionParameter();
    IClusterDetail clusterDetail = submitModel.getSelectedClusterDetail();
    Map<String, String> postEventProperty = new HashMap<>();
    submitModel.buildArtifactObservable(submissionParameter.getArtifactName()).flatMap((artifact) -> submitModel.deployArtifactObservable(artifact, clusterDetail).subscribeOn(Schedulers.io())).map((selectedClusterDetail) -> {
        // Create Batch Spark Debug Job
        try {
            return submitModel.tryToCreateBatchSparkDebugJob(selectedClusterDetail);
        } catch (Exception e) {
            HDInsightUtil.setJobRunningStatus(submitModel.getProject(), false);
            throw Exceptions.propagate(e);
        }
    }).flatMap((remoteDebugJob) -> startDebuggerObservable(environment, callback, submissionState, remoteDebugJob).subscribeOn(Schedulers.computation()).zipWith(submitModel.jobLogObservable(remoteDebugJob.getBatchId(), clusterDetail).subscribeOn(Schedulers.computation()), (session, ignore) -> session).doOnError(err -> {
        try {
            HDInsightUtil.showErrorMessageOnSubmissionMessageWindow(submitModel.getProject(), "Error : Spark batch debugging job is killed, got exception " + err);
            remoteDebugJob.killBatchJob();
            HDInsightUtil.setJobRunningStatus(submitModel.getProject(), false);
        } catch (IOException ignore) {
        }
    })).subscribe(sparkBatchDebugSession -> {
        HDInsightUtil.showInfoOnSubmissionMessageWindow(submitModel.getProject(), "Info : Debugging Spark batch job in cluster is done.");
        sparkBatchDebugSession.close();
        HDInsightUtil.setJobRunningStatus(submitModel.getProject(), false);
        postEventProperty.put("IsSubmitSucceed", "true");
        AppInsightsClient.create(HDInsightBundle.message("SparkRunConfigDebugButtonClick"), null, postEventProperty);
    }, (throwable) -> {
        // set the running flag to false
        HDInsightUtil.setJobRunningStatus(submitModel.getProject(), false);
        String errorMessage;
        if (throwable instanceof CompositeException) {
            CompositeException exceptions = (CompositeException) throwable;
            errorMessage = exceptions.getExceptions().stream().map(Throwable::getMessage).collect(Collectors.joining("; "));
        } else {
            errorMessage = throwable.getMessage();
        }
        HDInsightUtil.showErrorMessageOnSubmissionMessageWindow(submitModel.getProject(), "Error : Spark batch Job remote debug failed, got exception: " + errorMessage);
        postEventProperty.put("IsSubmitSucceed", "false");
        postEventProperty.put("SubmitFailedReason", errorMessage.substring(0, 50));
        AppInsightsClient.create(HDInsightBundle.message("SparkRunConfigDebugButtonClick"), null, postEventProperty);
    });
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Arrays(java.util.Arrays) ExecutionException(com.intellij.execution.ExecutionException) Exceptions(rx.exceptions.Exceptions) URISyntaxException(java.net.URISyntaxException) HDInsightUtil(com.microsoft.azure.hdinsight.common.HDInsightUtil) RemoteDebugRunConfiguration(com.microsoft.azure.hdinsight.spark.run.configuration.RemoteDebugRunConfiguration) HashMap(java.util.HashMap) RunProfileState(com.intellij.execution.configurations.RunProfileState) Single(rx.Single) ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) Map(java.util.Map) HDInsightBundle(com.microsoft.intellij.hdinsight.messages.HDInsightBundle) Schedulers(rx.schedulers.Schedulers) URI(java.net.URI) SimpleEntry(java.util.AbstractMap.SimpleEntry) IClusterDetail(com.microsoft.azure.hdinsight.sdk.cluster.IClusterDetail) ConfigurationInfoProvider(com.intellij.execution.configurations.ConfigurationInfoProvider) DefaultDebugExecutor(com.intellij.execution.executors.DefaultDebugExecutor) RemoteConnection(com.intellij.execution.configurations.RemoteConnection) RunProfile(com.intellij.execution.configurations.RunProfile) IOException(java.io.IOException) Executor(com.intellij.execution.Executor) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) GenericDebuggerRunnerSettings(com.intellij.debugger.impl.GenericDebuggerRunnerSettings) AppInsightsClient(com.microsoft.azuretools.telemetry.AppInsightsClient) CompositeException(rx.exceptions.CompositeException) JSchException(com.jcraft.jsch.JSchException) NotNull(org.jetbrains.annotations.NotNull) com.microsoft.azure.hdinsight.spark.common(com.microsoft.azure.hdinsight.spark.common) GenericDebuggerRunner(com.intellij.debugger.impl.GenericDebuggerRunner) HashMap(java.util.HashMap) CompositeException(rx.exceptions.CompositeException) IOException(java.io.IOException) IClusterDetail(com.microsoft.azure.hdinsight.sdk.cluster.IClusterDetail) ExecutionException(com.intellij.execution.ExecutionException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) CompositeException(rx.exceptions.CompositeException) JSchException(com.jcraft.jsch.JSchException)

Example 4 with CompositeException

use of rx.exceptions.CompositeException in project vertx-examples by vert-x3.

the class Transaction method start.

@Override
public void start() throws Exception {
    JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver");
    String sql = "CREATE TABLE colors (" + "id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) PRIMARY KEY, " + "name VARCHAR(255), " + "datetime TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL)";
    JDBCClient client = JDBCClient.createShared(vertx, config);
    // Connect to the database
    client.rxGetConnection().flatMap(conn -> conn.rxSetAutoCommit(false).flatMap(autoCommit -> conn.rxExecute(sql)).flatMap(executed -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("BLACK"))).flatMap(updateResult -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("WHITE"))).flatMap(updateResult -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("PURPLE"))).flatMap(updateResult -> conn.rxCommit().map(commit -> updateResult)).onErrorResumeNext(ex -> conn.rxRollback().onErrorResumeNext(ex2 -> Single.error(new CompositeException(ex, ex2))).flatMap(ignore -> Single.error(ex))).flatMap(updateResult -> conn.rxQuery("SELECT * FROM colors")).doAfterTerminate(conn::close)).subscribe(resultSet -> {
        // Subscribe to get the final result
        System.out.println("Results : " + resultSet.getRows());
    }, Throwable::printStackTrace);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Single(rx.Single) CompositeException(rx.exceptions.CompositeException) JsonObject(io.vertx.core.json.JsonObject) Runner(io.vertx.example.util.Runner) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient) JsonArray(io.vertx.core.json.JsonArray) CompositeException(rx.exceptions.CompositeException) JsonObject(io.vertx.core.json.JsonObject) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient)

Example 5 with CompositeException

use of rx.exceptions.CompositeException in project async-http-client by AsyncHttpClient.

the class AsyncHttpSingleTest method testErrorInOnThrowablePropagation.

@Test
public void testErrorInOnThrowablePropagation() {
    final RuntimeException processingException = new RuntimeException("processing");
    final RuntimeException thrownException = new RuntimeException("thrown");
    @SuppressWarnings("unchecked") final AsyncHandler<Object> handler = mock(AsyncHandler.class);
    doThrow(thrownException).when(handler).onThrowable(processingException);
    final Single<?> underTest = AsyncHttpSingle.create(bridge -> {
        try {
            bridge.onThrowable(processingException);
            return mock(Future.class);
        } catch (final Throwable t) {
            throw new AssertionError(t);
        }
    }, () -> handler);
    final TestSubscriber<Object> subscriber = new TestSubscriber<>();
    underTest.subscribe(subscriber);
    verify(handler).onThrowable(processingException);
    verifyNoMoreInteractions(handler);
    subscriber.awaitTerminalEvent();
    subscriber.assertTerminalEvent();
    subscriber.assertNoValues();
    final List<Throwable> errorEvents = subscriber.getOnErrorEvents();
    assertEquals(errorEvents.size(), 1);
    assertThat(errorEvents.get(0), is(instanceOf(CompositeException.class)));
    final CompositeException error = (CompositeException) errorEvents.get(0);
    assertEquals(error.getExceptions(), Arrays.asList(processingException, thrownException));
}
Also used : CompositeException(rx.exceptions.CompositeException) TestSubscriber(rx.observers.TestSubscriber) Test(org.testng.annotations.Test)

Aggregations

CompositeException (rx.exceptions.CompositeException)22 Test (org.junit.Test)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 MockResponse (okhttp3.mockwebserver.MockResponse)13 RxJavaErrorHandler (rx.plugins.RxJavaErrorHandler)13 OnErrorFailedException (rx.exceptions.OnErrorFailedException)7 ArrayList (java.util.ArrayList)4 Response (retrofit2.Response)3 TestSubscriber (rx.observers.TestSubscriber)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 Observable (rx.Observable)2 Single (rx.Single)2 Func1 (rx.functions.Func1)2 GenericDebuggerRunner (com.intellij.debugger.impl.GenericDebuggerRunner)1 GenericDebuggerRunnerSettings (com.intellij.debugger.impl.GenericDebuggerRunnerSettings)1 ExecutionException (com.intellij.execution.ExecutionException)1 Executor (com.intellij.execution.Executor)1 ConfigurationInfoProvider (com.intellij.execution.configurations.ConfigurationInfoProvider)1 RemoteConnection (com.intellij.execution.configurations.RemoteConnection)1 RunProfile (com.intellij.execution.configurations.RunProfile)1