-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathAbstractSubscribePersistentSubscription.java
More file actions
153 lines (127 loc) · 7.14 KB
/
AbstractSubscribePersistentSubscription.java
File metadata and controls
153 lines (127 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package io.kurrent.dbclient;
import io.grpc.Metadata;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.ClientCallStreamObserver;
import io.grpc.stub.ClientResponseObserver;
import io.grpc.stub.StreamObserver;
import io.kurrent.dbclient.proto.persistentsubscriptions.Persistent;
import io.kurrent.dbclient.proto.persistentsubscriptions.PersistentSubscriptionsGrpc;
import io.kurrent.dbclient.proto.shared.Shared;
import io.kurrent.dbclient.serialization.MessageSerializer;
import java.util.concurrent.CompletableFuture;
abstract class AbstractSubscribePersistentSubscription {
protected static final Persistent.ReadReq.Options.Builder defaultReadOptions;
private final GrpcClient client;
private final String group;
private final PersistentSubscriptionListener listener;
private final SubscribePersistentSubscriptionOptions options;
private final MessageSerializer messageSerializer;
static {
defaultReadOptions = Persistent.ReadReq.Options.newBuilder()
.setUuidOption(Persistent.ReadReq.Options.UUIDOption.newBuilder()
.setStructured(Shared.Empty.getDefaultInstance()));
}
public AbstractSubscribePersistentSubscription(
GrpcClient client,
String group,
SubscribePersistentSubscriptionOptions options,
PersistentSubscriptionListener listener,
MessageSerializer messageSerializer
) {
this.client = client;
this.group = group;
this.options = options;
this.listener = listener;
this.messageSerializer = messageSerializer;
}
protected abstract Persistent.ReadReq.Options.Builder createOptions();
public CompletableFuture<PersistentSubscription> execute() {
return this.client.runWithArgs(args -> {
PersistentSubscriptionsGrpc.PersistentSubscriptionsStub persistentSubscriptionsClient =
GrpcUtils.configureStub(PersistentSubscriptionsGrpc.newStub(args.getChannel()), this.client.getSettings(), this.options);
final CompletableFuture<PersistentSubscription> result = new CompletableFuture<>();
int bufferSize = this.options.getBufferSize();
Persistent.ReadReq req = Persistent.ReadReq.newBuilder()
.setOptions(createOptions()
.setBufferSize(bufferSize)
.setGroupName(group))
.build();
if (req.getOptions().hasAll() && !args.supportFeature(FeatureFlags.PERSISTENT_SUBSCRIPTION_TO_ALL)) {
result.completeExceptionally(new UnsupportedFeatureException());
} else {
ClientResponseObserver<Persistent.ReadReq, Persistent.ReadResp> observer = new ClientResponseObserver<Persistent.ReadReq, Persistent.ReadResp>() {
private boolean _confirmed;
private PersistentSubscription _subscription;
private ClientCallStreamObserver<Persistent.ReadReq> _requestStream;
@Override
public void beforeStart(ClientCallStreamObserver<Persistent.ReadReq> requestStream) {
this._requestStream = requestStream;
}
@Override
public void onNext(Persistent.ReadResp readResp) {
if (!_confirmed && readResp.hasSubscriptionConfirmation()) {
this._confirmed = true;
this._subscription = new PersistentSubscription(this._requestStream,
readResp.getSubscriptionConfirmation().getSubscriptionId());
result.complete(this._subscription);
listener.onConfirmation(this._subscription);
return;
}
if (!_confirmed && readResp.hasEvent()) {
onError(new IllegalStateException("Unconfirmed persistent subscription received event"));
return;
}
if (_confirmed && !readResp.hasEvent()) {
onError(new IllegalStateException(
String.format("Confirmed persistent subscription %s received non-{event,checkpoint} variant",
_subscription.getSubscriptionId())));
return;
}
int retryCount = readResp.getEvent().hasNoRetryCount() ? 0 : readResp.getEvent().getRetryCount();
try {
ResolvedEvent resolvedEvent = ResolvedEvent.fromWire(readResp.getEvent(), messageSerializer);
ClientTelemetry.traceSubscribe(
() -> listener.onEvent(this._subscription, retryCount, resolvedEvent),
_subscription.getSubscriptionId(),
args.getChannel(),
client.getSettings(),
options.getCredentials(),
resolvedEvent.getEvent());
} catch (Exception e) {
onError(e);
}
}
@Override
public void onError(Throwable throwable) {
if (!_confirmed) {
result.completeExceptionally(throwable);
}
Throwable error = throwable;
if (error instanceof StatusRuntimeException) {
StatusRuntimeException sre = (StatusRuntimeException) error;
String desc = sre.getStatus().getDescription();
if (sre.getStatus().getCode() == Status.Code.CANCELLED && desc != null && desc.equals("user-initiated")) {
listener.onCancelled(this._subscription, null);
return;
}
String leaderHost = sre.getTrailers().get(Metadata.Key.of("leader-endpoint-host", Metadata.ASCII_STRING_MARSHALLER));
String leaderPort = sre.getTrailers().get(Metadata.Key.of("leader-endpoint-port", Metadata.ASCII_STRING_MARSHALLER));
if (leaderHost != null && leaderPort != null) {
error = new NotLeaderException(leaderHost, Integer.valueOf(leaderPort));
}
}
listener.onCancelled(this._subscription, error);
}
@Override
public void onCompleted() {
// Subscriptions should only complete on error.
}
};
StreamObserver<Persistent.ReadReq> wireStream = persistentSubscriptionsClient.read(observer);
wireStream.onNext(req);
}
return result;
});
}
}