|
| 1 | +package io.kurrent.dbclient; |
| 2 | + |
| 3 | +import com.google.protobuf.Any; |
| 4 | +import com.google.protobuf.ByteString; |
| 5 | +import com.google.protobuf.Value; |
| 6 | +import io.grpc.StatusRuntimeException; |
| 7 | +import io.grpc.protobuf.StatusProto; |
| 8 | +import io.grpc.stub.ClientCallStreamObserver; |
| 9 | +import io.grpc.stub.ClientResponseObserver; |
| 10 | +import io.kurrentdb.protocol.v2.streams.AppendRecordsRequest; |
| 11 | +import io.kurrentdb.protocol.v2.streams.SchemaInfo; |
| 12 | +import io.kurrentdb.protocol.v2.streams.StreamsServiceGrpc; |
| 13 | + |
| 14 | +import io.kurrentdb.protocol.v2.streams.errors.AppendConsistencyViolationErrorDetails; |
| 15 | +import io.kurrentdb.protocol.v2.streams.errors.AppendRecordSizeExceededErrorDetails; |
| 16 | +import io.kurrentdb.protocol.v2.streams.errors.AppendTransactionSizeExceededErrorDetails; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.concurrent.CompletableFuture; |
| 22 | + |
| 23 | +class AppendRecords { |
| 24 | + private final GrpcClient client; |
| 25 | + private final List<AppendRecord> records; |
| 26 | + private final List<ConsistencyCheck> checks; |
| 27 | + |
| 28 | + public AppendRecords(GrpcClient client, List<AppendRecord> records, List<ConsistencyCheck> checks) { |
| 29 | + this.client = client; |
| 30 | + this.records = records; |
| 31 | + this.checks = checks; |
| 32 | + } |
| 33 | + |
| 34 | + public CompletableFuture<AppendRecordsResponse> execute() { |
| 35 | + if (records.isEmpty()) { |
| 36 | + CompletableFuture<AppendRecordsResponse> result = new CompletableFuture<>(); |
| 37 | + result.completeExceptionally(new IllegalArgumentException("At least one record is required.")); |
| 38 | + return result; |
| 39 | + } |
| 40 | + |
| 41 | + return this.client.runWithArgs(args -> ClientTelemetry.traceAppendRecords( |
| 42 | + this::appendRecords, |
| 43 | + args, |
| 44 | + this.records, |
| 45 | + this.client.getSettings())); |
| 46 | + } |
| 47 | + |
| 48 | + private CompletableFuture<AppendRecordsResponse> appendRecords(WorkItemArgs args, List<AppendRecord> records) { |
| 49 | + CompletableFuture<AppendRecordsResponse> result = new CompletableFuture<>(); |
| 50 | + |
| 51 | + if (!args.supportFeature(FeatureFlags.APPEND_RECORDS)) { |
| 52 | + result.completeExceptionally(new UnsupportedOperationException( |
| 53 | + "AppendRecords is not supported by the server. Requires KurrentDB 26.1 or later.")); |
| 54 | + return result; |
| 55 | + } |
| 56 | + |
| 57 | + StreamsServiceGrpc.StreamsServiceStub stub = GrpcUtils.configureStub( |
| 58 | + StreamsServiceGrpc.newStub(args.getChannel()), |
| 59 | + this.client.getSettings(), |
| 60 | + new OptionsBase<>(), |
| 61 | + null, |
| 62 | + false); |
| 63 | + |
| 64 | + try { |
| 65 | + AppendRecordsRequest.Builder requestBuilder = AppendRecordsRequest.newBuilder(); |
| 66 | + |
| 67 | + for (AppendRecord record : records) { |
| 68 | + EventData event = record.getRecord(); |
| 69 | + |
| 70 | + io.kurrentdb.protocol.v2.streams.AppendRecord.Builder recordBuilder = |
| 71 | + io.kurrentdb.protocol.v2.streams.AppendRecord.newBuilder() |
| 72 | + .setStream(record.getStream()) |
| 73 | + .setData(ByteString.copyFrom(event.getEventData())) |
| 74 | + .setRecordId(event.getEventId().toString()) |
| 75 | + .setSchema(SchemaInfo.newBuilder() |
| 76 | + .setFormat(ContentTypeMapper.toSchemaDataFormat(event.getContentType())) |
| 77 | + .setName(event.getEventType())); |
| 78 | + |
| 79 | + if (event.getUserMetadata() != null) { |
| 80 | + Map<String, Value> properties = DynamicValueMapper.mapJsonToValueMap(event.getUserMetadata()); |
| 81 | + recordBuilder.putAllProperties(properties); |
| 82 | + } |
| 83 | + |
| 84 | + requestBuilder.addRecords(recordBuilder.build()); |
| 85 | + } |
| 86 | + |
| 87 | + if (checks != null) { |
| 88 | + for (ConsistencyCheck check : checks) { |
| 89 | + if (check instanceof ConsistencyCheck.StreamStateCheck) { |
| 90 | + ConsistencyCheck.StreamStateCheck stateCheck = (ConsistencyCheck.StreamStateCheck) check; |
| 91 | + requestBuilder.addChecks( |
| 92 | + io.kurrentdb.protocol.v2.streams.ConsistencyCheck.newBuilder() |
| 93 | + .setStreamState( |
| 94 | + io.kurrentdb.protocol.v2.streams.ConsistencyCheck.StreamStateCheck.newBuilder() |
| 95 | + .setStream(stateCheck.getStream()) |
| 96 | + .setExpectedState(stateCheck.getExpectedState().toRawLong()) |
| 97 | + .build()) |
| 98 | + .build()); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + stub.appendRecords(requestBuilder.build(), new AppendRecordsObserver(result)); |
| 104 | + } catch (RuntimeException e) { |
| 105 | + result.completeExceptionally(e); |
| 106 | + } |
| 107 | + |
| 108 | + return result; |
| 109 | + } |
| 110 | + |
| 111 | + private AppendRecordsResponse onResponse(io.kurrentdb.protocol.v2.streams.AppendRecordsResponse response) { |
| 112 | + List<AppendResponse> results = new ArrayList<>(response.getRevisionsCount()); |
| 113 | + |
| 114 | + for (io.kurrentdb.protocol.v2.streams.StreamRevision revision : response.getRevisionsList()) { |
| 115 | + results.add(new AppendResponse(revision.getStream(), revision.getRevision())); |
| 116 | + } |
| 117 | + |
| 118 | + return new AppendRecordsResponse(response.getPosition(), results); |
| 119 | + } |
| 120 | + |
| 121 | + private class AppendRecordsObserver implements ClientResponseObserver<AppendRecordsRequest, io.kurrentdb.protocol.v2.streams.AppendRecordsResponse> { |
| 122 | + private final CompletableFuture<AppendRecordsResponse> result; |
| 123 | + |
| 124 | + public AppendRecordsObserver(CompletableFuture<AppendRecordsResponse> result) { |
| 125 | + this.result = result; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void beforeStart(ClientCallStreamObserver<AppendRecordsRequest> requestStream) { |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void onNext(io.kurrentdb.protocol.v2.streams.AppendRecordsResponse response) { |
| 134 | + try { |
| 135 | + AppendRecordsResponse converted = onResponse(response); |
| 136 | + result.complete(converted); |
| 137 | + } catch (Throwable e) { |
| 138 | + result.completeExceptionally(e); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void onError(Throwable t) { |
| 144 | + if (GrpcUtils.handleNotLeaderError(t, result)) return; |
| 145 | + |
| 146 | + if (t instanceof StatusRuntimeException) { |
| 147 | + StatusRuntimeException e = (StatusRuntimeException) t; |
| 148 | + com.google.rpc.Status status = StatusProto.fromThrowable(e); |
| 149 | + |
| 150 | + if (status != null && status.getDetailsCount() > 0) { |
| 151 | + for (Any d : status.getDetailsList()) { |
| 152 | + try { |
| 153 | + if (d.is(AppendConsistencyViolationErrorDetails.class)) { |
| 154 | + AppendConsistencyViolationErrorDetails details = |
| 155 | + d.unpack(AppendConsistencyViolationErrorDetails.class); |
| 156 | + List<ConsistencyViolation> violations = new ArrayList<>(); |
| 157 | + |
| 158 | + for (io.kurrentdb.protocol.v2.streams.errors.ConsistencyViolation v : details.getViolationsList()) { |
| 159 | + if (v.hasStreamState()) { |
| 160 | + io.kurrentdb.protocol.v2.streams.errors.ConsistencyViolation.StreamStateViolation ss = |
| 161 | + v.getStreamState(); |
| 162 | + violations.add(new ConsistencyViolation( |
| 163 | + v.getCheckIndex(), |
| 164 | + ss.getStream(), |
| 165 | + StreamState.fromRawLong(ss.getExpectedState()), |
| 166 | + StreamState.fromRawLong(ss.getActualState()))); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + result.completeExceptionally(new AppendConsistencyViolationException(violations)); |
| 171 | + return; |
| 172 | + } else if (d.is(AppendRecordSizeExceededErrorDetails.class)) { |
| 173 | + AppendRecordSizeExceededErrorDetails details = |
| 174 | + d.unpack(AppendRecordSizeExceededErrorDetails.class); |
| 175 | + result.completeExceptionally(new RecordSizeExceededException( |
| 176 | + details.getStream(), details.getRecordId(), |
| 177 | + details.getSize(), details.getMaxSize())); |
| 178 | + return; |
| 179 | + } else if (d.is(AppendTransactionSizeExceededErrorDetails.class)) { |
| 180 | + AppendTransactionSizeExceededErrorDetails details = |
| 181 | + d.unpack(AppendTransactionSizeExceededErrorDetails.class); |
| 182 | + result.completeExceptionally(new TransactionMaxSizeExceededException( |
| 183 | + details.getSize(), details.getMaxSize())); |
| 184 | + return; |
| 185 | + } |
| 186 | + } catch (com.google.protobuf.InvalidProtocolBufferException ex) { |
| 187 | + result.completeExceptionally(ex); |
| 188 | + return; |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + result.completeExceptionally(t); |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public void onCompleted() { |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments