Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store error on transaction stream #368

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions connection/TypeDBTransactionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import io.grpc.StatusRuntimeException;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import static com.vaticle.typedb.client.common.exception.ErrorMessage.Client.TRANSACTION_CLOSED;
Expand Down Expand Up @@ -124,9 +125,9 @@ public Stream<ResPart> stream(Req.Builder request) {
}

private void throwTransactionClosed() {
List<StatusRuntimeException> errors = bidirectionalStream.getErrors();
if (errors.isEmpty()) throw new TypeDBClientException(TRANSACTION_CLOSED);
else throw new TypeDBClientException(TRANSACTION_CLOSED_WITH_ERRORS, errors);
Optional<StatusRuntimeException> error = bidirectionalStream.getError();
if (error.isPresent()) throw new TypeDBClientException(TRANSACTION_CLOSED_WITH_ERRORS, error.get());
else throw new TypeDBClientException(TRANSACTION_CLOSED);
}

@Override
Expand Down
10 changes: 6 additions & 4 deletions stream/BidirectionalStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;
Expand All @@ -53,13 +54,15 @@ public class BidirectionalStream implements AutoCloseable {
private final ResponseCollector<ResPart> resPartCollector;
private final RequestTransmitter.Dispatcher dispatcher;
private final AtomicBoolean isOpen;
private StatusRuntimeException error;

public BidirectionalStream(TypeDBStub stub, RequestTransmitter transmitter) {
resPartCollector = new ResponseCollector<>();
resCollector = new ResponseCollector<>();
isOpen = new AtomicBoolean(false);
dispatcher = transmitter.dispatcher(stub.transaction(new ResponseObserver()));
isOpen.set(true);
error = null;
}

public Single<Res> single(Req.Builder request, boolean batch) {
Expand Down Expand Up @@ -108,6 +111,7 @@ public void close() {

private void close(@Nullable StatusRuntimeException error) {
if (isOpen.compareAndSet(true, false)) {
this.error = error;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

store the closure error against the bidirectional stream to prevent future transaction operations

resCollector.close(error);
resPartCollector.close(error);
try {
Expand All @@ -126,10 +130,8 @@ void iteratorDone(UUID requestID) {
resPartCollector.remove(requestID);
}

public List<StatusRuntimeException> getErrors() {
List<StatusRuntimeException> errors = new ArrayList<>(resCollector.getErrors());
errors.addAll(resPartCollector.getErrors());
return errors;
public Optional<StatusRuntimeException> getError() {
return Optional.ofNullable(error);
}

RequestTransmitter.Dispatcher dispatcher() {
Expand Down
10 changes: 0 additions & 10 deletions stream/ResponseCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ synchronized void close(@Nullable StatusRuntimeException error) {
collectors.values().forEach(collector -> collector.close(error));
}

List<StatusRuntimeException> getErrors() {
List<StatusRuntimeException> errors = new ArrayList<>();
collectors.values().forEach(collector -> collector.getError().ifPresent(errors::add));
return errors;
}

public static class Queue<R> {

private final BlockingQueue<Either<Response<R>, Done>> responseQueue;
Expand All @@ -93,10 +87,6 @@ public R take() {
}
}

public Optional<StatusRuntimeException> getError() {
return Optional.ofNullable(error);
}

public void put(R response) {
responseQueue.add(Either.first(new Response<>(response)));
}
Expand Down
9 changes: 6 additions & 3 deletions stream/ResponsePartIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ public boolean hasNext() {

@Override
public TransactionProto.Transaction.ResPart next() {
if (!hasNext()) throw new NoSuchElementException();
state = State.EMPTY;
return next;
if (stream.getError().isPresent()) throw TypeDBClientException.of(stream.getError().get());
else if (!hasNext()) throw new NoSuchElementException();
else {
state = State.EMPTY;
return next;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we make the next() idempotently throw the same error if the user repeatedly calls it

}
}