Skip to content

Commit 3a6cfae

Browse files
committed
fix: [grpc] InsertStream surfaces commit-time constraint violations as a stream-level INTERNAL error instead of populating InsertSummary.errors
Fixed issue #4198
1 parent 0744ff7 commit 3a6cfae

2 files changed

Lines changed: 288 additions & 1 deletion

File tree

‎grpcw/src/main/java/com/arcadedb/server/grpc/ArcadeDbGrpcService.java‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,15 @@ public void onCompleted() {
16401640
return;
16411641
}
16421642

1643-
ctx.flushCommit(true); // commit if not validate-only
1643+
// Issue #4198: PER_STREAM / PER_REQUEST defer the commit to here. A commit-time constraint
1644+
// violation (e.g. DuplicatedKeyException) used to bubble up to the outer catch and become
1645+
// Status.INTERNAL, leaving the client without an InsertSummary. Instead, surface those as
1646+
// structured errors in totals.errors and still deliver the summary.
1647+
try {
1648+
ctx.flushCommit(true); // commit if not validate-only
1649+
} catch (Exception commitEx) {
1650+
recordCommitException(totals, commitEx);
1651+
}
16441652

16451653
if (!cancelled.get()) {
16461654
resp.onNext(ctx.summary(totals, startedAt));
@@ -1657,6 +1665,30 @@ public void onCompleted() {
16571665
};
16581666
}
16591667

1668+
/**
1669+
* Issue #4198: maps a commit-time exception (DuplicatedKeyException, ValidationException, etc.) into
1670+
* the running {@link Counts} so the client receives a structured {@link InsertSummary} instead of a
1671+
* stream-level {@code Status.INTERNAL}. The engine auto-rolls back on commit failure, so any rows
1672+
* that were optimistically counted as inserted/updated did not persist - reclassify them as failed.
1673+
*/
1674+
private static void recordCommitException(final Counts totals, final Exception e) {
1675+
final long rolledBack = totals.inserted + totals.updated;
1676+
totals.inserted = 0;
1677+
totals.updated = 0;
1678+
totals.failed += rolledBack;
1679+
if (rolledBack == 0)
1680+
totals.failed++;
1681+
1682+
final String code = e instanceof DuplicatedKeyException ? "CONFLICT" : "COMMIT_FAILED";
1683+
final String message = e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName();
1684+
totals.errors.add(InsertError.newBuilder()
1685+
.setRowIndex(-1)
1686+
.setCode(code)
1687+
.setMessage(message)
1688+
.setField("")
1689+
.build());
1690+
}
1691+
16601692
// --- 3) Client-streaming graph batch load ---
16611693
@Override
16621694
public StreamObserver<GraphBatchChunk> graphBatchLoad(final StreamObserver<GraphBatchResult> resp) {
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
/*
2+
* Copyright © 2021-present Arcade Data Ltd ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-FileCopyrightText: 2021-present Arcade Data Ltd ([email protected])
17+
* SPDX-License-Identifier: Apache-2.0
18+
*/
19+
package com.arcadedb.server.grpc;
20+
21+
import com.arcadedb.GlobalConfiguration;
22+
import com.arcadedb.test.BaseGraphServerTest;
23+
import io.grpc.CallOptions;
24+
import io.grpc.Channel;
25+
import io.grpc.ClientCall;
26+
import io.grpc.ClientInterceptor;
27+
import io.grpc.ClientInterceptors;
28+
import io.grpc.ForwardingClientCall;
29+
import io.grpc.ManagedChannel;
30+
import io.grpc.ManagedChannelBuilder;
31+
import io.grpc.Metadata;
32+
import io.grpc.MethodDescriptor;
33+
import io.grpc.stub.StreamObserver;
34+
import org.junit.jupiter.api.AfterEach;
35+
import org.junit.jupiter.api.BeforeEach;
36+
import org.junit.jupiter.api.Test;
37+
38+
import java.util.concurrent.CountDownLatch;
39+
import java.util.concurrent.TimeUnit;
40+
import java.util.concurrent.atomic.AtomicReference;
41+
42+
import static org.assertj.core.api.Assertions.assertThat;
43+
44+
/**
45+
* Regression test for issue #4198: gRPC {@code InsertStream} surfaces commit-time constraint
46+
* violations as a stream-level {@code Status.INTERNAL} error instead of populating
47+
* {@link InsertSummary#getErrorsList()}.
48+
*
49+
* <p>In {@code PER_STREAM} (and {@code PER_REQUEST}) transaction mode, the commit happens in
50+
* {@code onCompleted()} after {@code insertRows(...)} has already returned cleanly. If commit
51+
* raises a {@link com.arcadedb.exception.DuplicatedKeyException} (deferred unique-index check),
52+
* the original code routed the exception through {@code resp.onError(Status.INTERNAL...)} which
53+
* aborted the entire RPC and left the client with no {@code InsertSummary}.
54+
*
55+
* <p>The fix catches recoverable per-row errors thrown by {@code ctx.flushCommit(true)} and
56+
* registers them in the totals so the client receives a well-formed {@code InsertSummary}.
57+
*
58+
* @author Luca Garulli ([email protected])
59+
*/
60+
public class Issue4198InsertStreamCommitErrorIT extends BaseGraphServerTest {
61+
62+
private static final int GRPC_PORT = 50051;
63+
64+
private static final Metadata.Key<String> USER_HEADER = Metadata.Key.of("x-arcade-user", Metadata.ASCII_STRING_MARSHALLER);
65+
private static final Metadata.Key<String> PASSWORD_HEADER = Metadata.Key.of("x-arcade-password", Metadata.ASCII_STRING_MARSHALLER);
66+
private static final Metadata.Key<String> DATABASE_HEADER = Metadata.Key.of("x-arcade-database", Metadata.ASCII_STRING_MARSHALLER);
67+
68+
private ManagedChannel channel;
69+
private ArcadeDbServiceGrpc.ArcadeDbServiceBlockingStub authenticatedStub;
70+
private ArcadeDbServiceGrpc.ArcadeDbServiceStub asyncAuthenticatedStub;
71+
72+
@Override
73+
public void setTestConfiguration() {
74+
super.setTestConfiguration();
75+
GlobalConfiguration.SERVER_PLUGINS.setValue("GrpcServer:com.arcadedb.server.grpc.GrpcServerPlugin");
76+
}
77+
78+
@BeforeEach
79+
void setupGrpcClient() {
80+
channel = ManagedChannelBuilder.forAddress("localhost", GRPC_PORT).usePlaintext().build();
81+
final Channel authenticatedChannel = ClientInterceptors.intercept(channel, new AuthClientInterceptor());
82+
authenticatedStub = ArcadeDbServiceGrpc.newBlockingStub(authenticatedChannel);
83+
asyncAuthenticatedStub = ArcadeDbServiceGrpc.newStub(authenticatedChannel);
84+
}
85+
86+
@AfterEach
87+
void shutdownGrpcClient() throws InterruptedException {
88+
if (channel != null) {
89+
channel.shutdown();
90+
channel.awaitTermination(5, TimeUnit.SECONDS);
91+
}
92+
}
93+
94+
private class AuthClientInterceptor implements ClientInterceptor {
95+
@Override
96+
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
97+
final MethodDescriptor<ReqT, RespT> method, final CallOptions callOptions, final Channel next) {
98+
return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
99+
@Override
100+
public void start(final Listener<RespT> responseListener, final Metadata headers) {
101+
headers.put(USER_HEADER, "root");
102+
headers.put(PASSWORD_HEADER, DEFAULT_PASSWORD_FOR_TESTS);
103+
headers.put(DATABASE_HEADER, getDatabaseName());
104+
super.start(responseListener, headers);
105+
}
106+
};
107+
}
108+
}
109+
110+
private DatabaseCredentials credentials() {
111+
return DatabaseCredentials.newBuilder().setUsername("root").setPassword(DEFAULT_PASSWORD_FOR_TESTS).build();
112+
}
113+
114+
private GrpcValue stringValue(final String s) {
115+
return GrpcValue.newBuilder().setStringValue(s).build();
116+
}
117+
118+
@Test
119+
void insertStreamUniqueViolationUnderPerStreamShouldPopulateErrors() throws Exception {
120+
final String typeName = "Issue4198PerStreamType_" + System.currentTimeMillis();
121+
122+
// Schema + unique index + one pre-existing row that the streamed row will collide with.
123+
authenticatedStub.executeCommand(ExecuteCommandRequest.newBuilder()
124+
.setDatabase(getDatabaseName()).setCredentials(credentials())
125+
.setCommand("CREATE DOCUMENT TYPE " + typeName).build());
126+
authenticatedStub.executeCommand(ExecuteCommandRequest.newBuilder()
127+
.setDatabase(getDatabaseName()).setCredentials(credentials())
128+
.setCommand("CREATE PROPERTY " + typeName + ".name STRING").build());
129+
authenticatedStub.executeCommand(ExecuteCommandRequest.newBuilder()
130+
.setDatabase(getDatabaseName()).setCredentials(credentials())
131+
.setCommand("CREATE INDEX ON " + typeName + "(name) UNIQUE").build());
132+
authenticatedStub.executeCommand(ExecuteCommandRequest.newBuilder()
133+
.setDatabase(getDatabaseName()).setCredentials(credentials())
134+
.setCommand("INSERT INTO " + typeName + " SET name = 'Alice'").build());
135+
136+
try {
137+
final CountDownLatch done = new CountDownLatch(1);
138+
final AtomicReference<InsertSummary> summaryRef = new AtomicReference<>();
139+
final AtomicReference<Throwable> errorRef = new AtomicReference<>();
140+
141+
final StreamObserver<InsertChunk> req = asyncAuthenticatedStub.insertStream(new StreamObserver<>() {
142+
@Override public void onNext(final InsertSummary s) { summaryRef.set(s); }
143+
@Override public void onError(final Throwable t) { errorRef.set(t); done.countDown(); }
144+
@Override public void onCompleted() { done.countDown(); }
145+
});
146+
147+
req.onNext(InsertChunk.newBuilder()
148+
.setSessionId("issue-4198-per-stream")
149+
.setChunkSeq(0)
150+
.setLast(true)
151+
.setOptions(InsertOptions.newBuilder()
152+
.setDatabase(getDatabaseName())
153+
.setCredentials(credentials())
154+
.setTargetClass(typeName)
155+
.setConflictMode(InsertOptions.ConflictMode.CONFLICT_ERROR)
156+
.setTransactionMode(InsertOptions.TransactionMode.PER_STREAM)
157+
.build())
158+
.addRows(GrpcRecord.newBuilder()
159+
.setType(typeName)
160+
.putProperties("name", stringValue("Alice")) // duplicate of the pre-existing row
161+
.build())
162+
.build());
163+
req.onCompleted();
164+
165+
assertThat(done.await(30, TimeUnit.SECONDS)).as("InsertStream should terminate within 30s").isTrue();
166+
167+
// The RPC must terminate cleanly with an InsertSummary, not as Status.INTERNAL.
168+
assertThat(errorRef.get())
169+
.as("InsertStream should not abort with Status.INTERNAL on a per-row constraint violation")
170+
.isNull();
171+
assertThat(summaryRef.get()).as("client must receive an InsertSummary").isNotNull();
172+
173+
final InsertSummary summary = summaryRef.get();
174+
assertThat(summary.getReceived()).isEqualTo(1);
175+
assertThat(summary.getFailed()).isEqualTo(1);
176+
assertThat(summary.getInserted()).isEqualTo(0);
177+
assertThat(summary.getErrorsList())
178+
.as("the violating row should appear in InsertSummary.errors")
179+
.hasSize(1);
180+
assertThat(summary.getErrors(0).getCode()).isEqualTo("CONFLICT");
181+
assertThat(summary.getErrors(0).getMessage()).contains("Alice");
182+
} finally {
183+
authenticatedStub.executeCommand(ExecuteCommandRequest.newBuilder()
184+
.setDatabase(getDatabaseName()).setCredentials(credentials())
185+
.setCommand("DROP TYPE " + typeName + " IF EXISTS UNSAFE").build());
186+
}
187+
}
188+
189+
@Test
190+
void insertStreamUniqueViolationUnderPerRequestShouldPopulateErrors() throws Exception {
191+
final String typeName = "Issue4198PerRequestType_" + System.currentTimeMillis();
192+
193+
authenticatedStub.executeCommand(ExecuteCommandRequest.newBuilder()
194+
.setDatabase(getDatabaseName()).setCredentials(credentials())
195+
.setCommand("CREATE DOCUMENT TYPE " + typeName).build());
196+
authenticatedStub.executeCommand(ExecuteCommandRequest.newBuilder()
197+
.setDatabase(getDatabaseName()).setCredentials(credentials())
198+
.setCommand("CREATE PROPERTY " + typeName + ".name STRING").build());
199+
authenticatedStub.executeCommand(ExecuteCommandRequest.newBuilder()
200+
.setDatabase(getDatabaseName()).setCredentials(credentials())
201+
.setCommand("CREATE INDEX ON " + typeName + "(name) UNIQUE").build());
202+
authenticatedStub.executeCommand(ExecuteCommandRequest.newBuilder()
203+
.setDatabase(getDatabaseName()).setCredentials(credentials())
204+
.setCommand("INSERT INTO " + typeName + " SET name = 'Bob'").build());
205+
206+
try {
207+
final CountDownLatch done = new CountDownLatch(1);
208+
final AtomicReference<InsertSummary> summaryRef = new AtomicReference<>();
209+
final AtomicReference<Throwable> errorRef = new AtomicReference<>();
210+
211+
final StreamObserver<InsertChunk> req = asyncAuthenticatedStub.insertStream(new StreamObserver<>() {
212+
@Override public void onNext(final InsertSummary s) { summaryRef.set(s); }
213+
@Override public void onError(final Throwable t) { errorRef.set(t); done.countDown(); }
214+
@Override public void onCompleted() { done.countDown(); }
215+
});
216+
217+
req.onNext(InsertChunk.newBuilder()
218+
.setSessionId("issue-4198-per-request")
219+
.setChunkSeq(0)
220+
.setLast(true)
221+
.setOptions(InsertOptions.newBuilder()
222+
.setDatabase(getDatabaseName())
223+
.setCredentials(credentials())
224+
.setTargetClass(typeName)
225+
.setConflictMode(InsertOptions.ConflictMode.CONFLICT_ERROR)
226+
.setTransactionMode(InsertOptions.TransactionMode.PER_REQUEST)
227+
.build())
228+
.addRows(GrpcRecord.newBuilder()
229+
.setType(typeName)
230+
.putProperties("name", stringValue("Bob"))
231+
.build())
232+
.build());
233+
req.onCompleted();
234+
235+
assertThat(done.await(30, TimeUnit.SECONDS)).as("InsertStream should terminate within 30s").isTrue();
236+
237+
assertThat(errorRef.get())
238+
.as("InsertStream should not abort with Status.INTERNAL on a per-row constraint violation")
239+
.isNull();
240+
assertThat(summaryRef.get()).as("client must receive an InsertSummary").isNotNull();
241+
242+
final InsertSummary summary = summaryRef.get();
243+
assertThat(summary.getReceived()).isEqualTo(1);
244+
assertThat(summary.getFailed()).isEqualTo(1);
245+
assertThat(summary.getInserted()).isEqualTo(0);
246+
assertThat(summary.getErrorsList()).hasSize(1);
247+
assertThat(summary.getErrors(0).getCode()).isEqualTo("CONFLICT");
248+
assertThat(summary.getErrors(0).getMessage()).contains("Bob");
249+
} finally {
250+
authenticatedStub.executeCommand(ExecuteCommandRequest.newBuilder()
251+
.setDatabase(getDatabaseName()).setCredentials(credentials())
252+
.setCommand("DROP TYPE " + typeName + " IF EXISTS UNSAFE").build());
253+
}
254+
}
255+
}

0 commit comments

Comments
 (0)