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;
}
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"));
}
}
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);
});
}
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);
}
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));
}
Aggregations