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

Map Java driver ReadFailureException on Find command to JsonApiException #1009

Merged
merged 5 commits into from
Apr 1, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public enum ErrorCode {

UNAUTHENTICATED_REQUEST("UNAUTHENTICATED: Invalid token"),
INVALID_QUERY("Invalid query"),
DATABASE_READ_FAILED("Database read failed"),
DRIVER_TIMEOUT("Driver timeout"),
DRIVER_CLOSED_CONNECTION("Driver request connection is closed"),
NO_NODE_AVAILABLE("No node was available to execute the query"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.datastax.oss.driver.api.core.connection.ClosedConnectionException;
import com.datastax.oss.driver.api.core.metadata.Node;
import com.datastax.oss.driver.api.core.servererrors.QueryValidationException;
import com.datastax.oss.driver.api.core.servererrors.ReadFailureException;
import com.datastax.oss.driver.api.core.servererrors.ReadTimeoutException;
import com.datastax.oss.driver.api.core.servererrors.WriteTimeoutException;
import io.quarkus.security.UnauthorizedException;
Expand Down Expand Up @@ -120,6 +121,12 @@ public final class ThrowableToErrorMapper {
.getCommandResultError(message, Response.Status.INTERNAL_SERVER_ERROR);
// Unidentified Driver Exceptions, will not map into JsonApiException
} else if (throwable instanceof DriverException) {
if (throwable instanceof ReadFailureException) {
return ErrorCode.DATABASE_READ_FAILED
.toApiException(
"root cause: (%s) %s", throwable.getClass().getName(), throwable.getMessage())
.getCommandResultError(message, Response.Status.INTERNAL_SERVER_ERROR);
}
return new CommandResult.Error(
message, fieldsForMetricsTag, fields, Response.Status.INTERNAL_SERVER_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import static org.mockito.Mockito.when;

import com.bpodgursky.jbool_expressions.Expression;
import com.datastax.oss.driver.api.core.ConsistencyLevel;
import com.datastax.oss.driver.api.core.cql.AsyncResultSet;
import com.datastax.oss.driver.api.core.cql.ColumnDefinitions;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
import com.datastax.oss.driver.api.core.data.CqlVector;
import com.datastax.oss.driver.api.core.metadata.Node;
import com.datastax.oss.driver.api.core.servererrors.ReadFailureException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
Expand All @@ -25,6 +28,7 @@
import io.stargate.sgv2.jsonapi.api.model.command.CommandResult;
import io.stargate.sgv2.jsonapi.api.model.command.clause.filter.ComparisonExpression;
import io.stargate.sgv2.jsonapi.api.model.command.clause.filter.LogicalExpression;
import io.stargate.sgv2.jsonapi.exception.mappers.ThrowableToErrorMapper;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.CollectionSettings;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.QueryExecutor;
import io.stargate.sgv2.jsonapi.service.operation.model.ReadType;
Expand All @@ -35,19 +39,20 @@
import io.stargate.sgv2.jsonapi.service.testutil.MockRow;
import jakarta.annotation.PostConstruct;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.time.Instant;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.assertj.core.api.AssertionsForClassTypes;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

@QuarkusTest
@TestProfile(NoGlobalResourcesTestProfile.Impl.class)
Expand Down Expand Up @@ -2775,6 +2780,68 @@ public void expressionSort() {
}
}

@Nested
class DriverException {
@Test
public void readFailureException() throws UnknownHostException {

QueryExecutor queryExecutor = mock(QueryExecutor.class);
Node coordinator = mock(Node.class);
ConsistencyLevel consistencyLevel = ConsistencyLevel.ONE;
int received = 1;
int blockFor = 0;
int numFailures = 1;
boolean dataPresent = false;
Map<InetAddress, Integer> reasonMap = new HashMap<>();
reasonMap.put(InetAddress.getByName("127.0.0.1"), 0x0000);
Mockito.when(queryExecutor.executeVectorSearch(any(), any(), anyInt()))
.thenThrow(
new ReadFailureException(
coordinator,
consistencyLevel,
received,
blockFor,
numFailures,
dataPresent,
reasonMap));

LogicalExpression implicitAnd = LogicalExpression.and();
FindOperation operation =
FindOperation.vsearch(
VECTOR_COMMAND_CONTEXT,
implicitAnd,
DocumentProjector.identityProjector(),
null,
2,
2,
ReadType.DOCUMENT,
objectMapper,
new float[] {0.25f, 0.25f, 0.25f, 0.25f});

// Throwable
Throwable failure =
operation
.execute(queryExecutor)
.subscribe()
.withSubscriber(UniAssertSubscriber.create())
.awaitFailure()
.getFailure();
CommandResult.Error error =
ThrowableToErrorMapper.getMapperWithMessageFunction()
.apply(failure, failure.getMessage());
AssertionsForClassTypes.assertThat(error).isNotNull();
AssertionsForClassTypes.assertThat(error.message())
.isEqualTo(
"Cassandra failure during read query at consistency ONE (0 responses were required but only 1 replica responded, 1 failed)");
AssertionsForClassTypes.assertThat(error.fields().get("errorCode"))
.isEqualTo("DATABASE_READ_FAILED");
AssertionsForClassTypes.assertThat(error.fields().get("exceptionClass"))
.isEqualTo("JsonApiException");
AssertionsForClassTypes.assertThat(error.status())
.isEqualTo(Response.Status.INTERNAL_SERVER_ERROR);
}
}

MockRow resultRow(int index, String key, UUID txId, String doc) {
return new MockRow(
KEY_TXID_JSON_COLUMNS,
Expand Down
Loading