Skip to content

Commit 418acc0

Browse files
committed
Embedded: Update to the new error status api
JAVA-2856
1 parent d7e0fee commit 418acc0

File tree

13 files changed

+272
-131
lines changed

13 files changed

+272
-131
lines changed

config/checkstyle-exclude.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
<suppress checks="HideUtilityClassConstructor" files="JSON"/>
112112
<suppress checks="HideUtilityClassConstructor" files="QuickTour"/>
113113
<suppress checks="HideUtilityClassConstructor" files="Util"/>
114+
<suppress checks="HideUtilityClassConstructor" files="DatabaseTestCase"/>
114115

115116
<suppress checks="ParameterNumber" files="BulkWriteBatch"/>
116117

config/findbugs-exclude.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@
104104
<Bug pattern="URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD"/>
105105
</Match>
106106

107+
<Match>
108+
<Class name="com.mongodb.embedded.client.MongoDBCAPIHelper$MongoDBCAPIInitParams"/>
109+
<Field name="userData"/>
110+
<Bug pattern="UUF_UNUSED_PUBLIC_OR_PROTECTED_FIELD"/>
111+
</Match>
112+
107113
<!-- Test exclusions -->
108114
<!-- All bugs in test classes, except for JUnit-specific bugs -->
109115
<Match>

driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnection.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ private <T> T receiveCommandMessageResponse(final CommandMessage message, final
288288
ResponseBuffers responseBuffers = receiveMessage(message.getId());
289289
try {
290290
updateSessionContext(sessionContext, responseBuffers);
291-
292291
if (!isCommandOk(responseBuffers)) {
293292
throw getCommandFailureException(responseBuffers.getResponseDocument(message.getId(), new BsonDocumentCodec()),
294293
description.getServerAddress());

driver-embedded/src/main/com/mongodb/embedded/client/EmbeddedInternalConnection.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@
4949

5050
class EmbeddedInternalConnection implements InternalConnection {
5151
private final InternalConnection wrapped;
52+
private volatile Pointer clientStatusPointer;
5253
private volatile Pointer clientPointer;
5354

54-
EmbeddedInternalConnection(final Pointer databasePointer, final CommandListener commandListener,
55+
EmbeddedInternalConnection(final Pointer instancePointer, final CommandListener commandListener,
5556
final BsonDocument clientMetadataDocument) {
56-
this.clientPointer = MongoDBCAPIHelper.db_client_new(databasePointer);
57+
this.clientStatusPointer = MongoDBCAPIHelper.createStatusPointer();
58+
this.clientPointer = MongoDBCAPIHelper.create_client(instancePointer, clientStatusPointer);
5759
this.wrapped = new InternalStreamConnection(new ServerId(new ClusterId(), new ServerAddress()),
5860
new StreamFactory() {
5961
@Override
@@ -84,8 +86,10 @@ public void openAsync(final SingleResultCallback<Void> callback) {
8486
public void close() {
8587
if (!wrapped.isClosed()) {
8688
wrapped.close();
87-
MongoDBCAPIHelper.db_client_destroy(clientPointer);
89+
MongoDBCAPIHelper.client_destroy(clientPointer, clientStatusPointer);
8890
clientPointer = null;
91+
MongoDBCAPIHelper.destroyStatusPointer(clientStatusPointer);
92+
clientStatusPointer = null;
8993
}
9094
}
9195

@@ -156,15 +160,15 @@ public void write(final List<ByteBuf> buffers) {
156160

157161
PointerByReference outputBufferReference = new PointerByReference();
158162
IntByReference outputSize = new IntByReference();
159-
MongoDBCAPIHelper.db_client_wire_protocol_rpc(clientPointer, message, message.length, outputBufferReference, outputSize);
163+
MongoDBCAPIHelper.client_invoke(clientPointer, message, outputBufferReference, outputSize, clientStatusPointer);
160164
curResponse = outputBufferReference.getValue().getByteBuffer(0, outputSize.getValue());
161165
}
162166

163167
@Override
164168
public ByteBuf read(final int numBytes) {
165169
ByteBuffer slice = curResponse.slice();
166170
((Buffer) slice).limit(numBytes);
167-
((Buffer) curResponse).position(((Buffer)curResponse).position() + numBytes);
171+
((Buffer) curResponse).position(((Buffer) curResponse).position() + numBytes);
168172
return new ByteBufNIO(slice);
169173
}
170174

driver-embedded/src/main/com/mongodb/embedded/client/EmbeddedInternalConnectionPool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void close() {
5252
}
5353
}
5454

55-
private final class EmbeddedConnectionItemFactory implements ConcurrentPool.ItemFactory<EmbeddedInternalConnection> {
55+
private static class EmbeddedConnectionItemFactory implements ConcurrentPool.ItemFactory<EmbeddedInternalConnection> {
5656
private final EmbeddedInternalConnectionFactory internalConnectionFactory;
5757

5858
EmbeddedConnectionItemFactory(final EmbeddedInternalConnectionFactory internalConnectionFactory) {

driver-embedded/src/main/com/mongodb/embedded/client/EmbeddedServer.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,20 @@ class EmbeddedServer implements Server, Closeable {
6161
private final ServerDescription serverDescription;
6262
private final EmbeddedInternalConnectionPool connectionPool;
6363
private volatile boolean isClosed;
64-
private volatile Pointer databasePointer;
64+
private volatile Pointer instanceStatusPointer;
65+
private volatile Pointer instancePointer;
66+
6567

6668
EmbeddedServer(final MongoClientSettings mongoClientSettings) {
67-
this.databasePointer = createDatabasePointer(mongoClientSettings);
69+
this.instanceStatusPointer = MongoDBCAPIHelper.createStatusPointer();
70+
this.instancePointer = createInstancePointer(mongoClientSettings);
6871
this.clusterClock = new ClusterClock();
6972
this.commandListener = getCommandListener(mongoClientSettings.getCommandListeners());
7073
this.serverAddress = new ServerAddress();
7174
this.connectionPool = new EmbeddedInternalConnectionPool(new EmbeddedInternalConnectionFactory() {
7275
@Override
7376
public EmbeddedInternalConnection create() {
74-
return new EmbeddedInternalConnection(databasePointer, commandListener,
77+
return new EmbeddedInternalConnection(instancePointer, commandListener,
7578
createClientMetadataDocument(mongoClientSettings.getApplicationName(), MONGO_DRIVER_INFORMATION));
7679
}
7780
});
@@ -96,24 +99,16 @@ public void getConnectionAsync(final SingleResultCallback<AsyncConnection> callb
9699
throw new UnsupportedOperationException("Async not supported");
97100
}
98101

99-
/**
100-
* Pump the message queue.
101-
*/
102-
public void pump() {
103-
isTrue("open", !isClosed);
104-
MongoDBCAPIHelper.db_pump(databasePointer);
105-
}
106-
107102
@Override
108103
public void close() {
109104
if (!isClosed) {
110105
isClosed = true;
111106
connectionPool.close();
112-
destroyDatabasePointer();
107+
destroyInstancePointer();
113108
}
114109
}
115110

116-
private Pointer createDatabasePointer(final MongoClientSettings mongoClientSettings) {
111+
private Pointer createInstancePointer(final MongoClientSettings mongoClientSettings) {
117112
File directory = new File(mongoClientSettings.getDbPath());
118113
try {
119114
if (directory.mkdirs() && LOGGER.isInfoEnabled()) {
@@ -124,12 +119,14 @@ private Pointer createDatabasePointer(final MongoClientSettings mongoClientSetti
124119
}
125120

126121
String yamlConfig = createYamlConfig(mongoClientSettings);
127-
return MongoDBCAPIHelper.db_new(yamlConfig);
122+
return MongoDBCAPIHelper.instance_create(yamlConfig, instanceStatusPointer);
128123
}
129124

130-
private void destroyDatabasePointer() {
131-
MongoDBCAPIHelper.db_destroy(databasePointer);
132-
databasePointer = null;
125+
private void destroyInstancePointer() {
126+
MongoDBCAPIHelper.instance_destroy(instancePointer, instanceStatusPointer);
127+
instancePointer = null;
128+
MongoDBCAPIHelper.destroyStatusPointer(instanceStatusPointer);
129+
instanceStatusPointer = null;
133130
}
134131

135132
private String createYamlConfig(final MongoClientSettings mongoClientSettings) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
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+
17+
package com.mongodb.embedded.client;
18+
19+
import com.mongodb.MongoException;
20+
21+
import static java.lang.String.format;
22+
23+
/**
24+
* Exceptions indicating a failure condition with the embedded MongoClient.
25+
*
26+
* @since 3.8
27+
*/
28+
public final class MongoClientEmbeddedException extends MongoException {
29+
private static final long serialVersionUID = 8314840681404996248L;
30+
31+
/**
32+
* Constructs a new instance
33+
*
34+
* @param errorCode the error code
35+
* @param subErrorCode the sub category error code
36+
* @param reason the reason for the exception
37+
*/
38+
public MongoClientEmbeddedException(final int errorCode, final int subErrorCode, final String reason) {
39+
super(errorCode, format("%s (%s:%s)", reason, errorCode, subErrorCode));
40+
}
41+
42+
/**
43+
* Constructs a new instance.
44+
*
45+
* @param message the message
46+
*/
47+
public MongoClientEmbeddedException(final String message) {
48+
super(message);
49+
}
50+
51+
/**
52+
* Constructs a new instance.
53+
*
54+
* @param message the message
55+
* @param cause the cause
56+
*/
57+
public MongoClientEmbeddedException(final String message, final Throwable cause) {
58+
super(message, cause);
59+
}
60+
61+
}

driver-embedded/src/main/com/mongodb/embedded/client/MongoDBCAPI.java

Lines changed: 76 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,78 +27,117 @@ interface MongoDBCAPI extends Library {
2727

2828
// CHECKSTYLE.OFF: MethodName
2929
/**
30-
* Initializes the mongodbcapi library, required before any other call. Cannot be called again
31-
* without libmongodbcapi_fini() being called first.
30+
* Creates a status pointer
3231
*
33-
* @param initParams the embedded mongod initialization parameters.
32+
* @return the status pointer from which to get an associated status code / error information.
33+
*/
34+
Pointer libmongodbcapi_status_create();
3435

35-
* @note This function is not thread safe.
36-
* @return the error, or 0 if success
36+
/**
37+
* Destroys a valid status pointer object.
38+
*
39+
* @param status the `libmongodbcapi_status` pointer to release.
3740
*/
38-
int libmongodbcapi_init(Structure initParams);
41+
42+
void libmongodbcapi_status_destroy(Pointer status);
3943

4044
/**
41-
* Tears down the state of the library, all databases must be closed before calling this.
45+
* Gets an error code from a status pointer.
46+
*
47+
* @param status the `libmongodbcapi_status` pointer from which to get an associated status code.
48+
* @return The error code associated with the `status` parameter or 0 if successful.
4249
*
43-
* @return the error, or 0 if success
4450
*/
45-
int libmongodbcapi_fini();
51+
int libmongodbcapi_status_get_error(Pointer status);
4652

4753
/**
48-
* Create a new db instance.
54+
* Gets a descriptive error message from a status pointer.
4955
*
50-
* @param yamlConfig null-terminated YAML formatted MongoDB configuration string
51-
* @return the db pointer
56+
* @param status the `libmongodbcapi_status` pointer from which to get an associated error message.
57+
*
58+
* @return A null-terminated string containing an error message.
5259
*/
53-
Pointer libmongodbcapi_db_new(String yamlConfig);
60+
String libmongodbcapi_status_get_explanation(Pointer status);
5461

5562
/**
56-
* Destroy a db instance.
63+
* Gets a status code from a status pointer.
5764
*
58-
* @param db the db pointer
65+
* @param status the `libmongodbcapi_status` pointer from which to get an associated status code.
66+
* @return A numeric status code associated with the `status` parameter which indicates a
67+
* sub-category of failure.
5968
*/
60-
void libmongodbcapi_db_destroy(Pointer db);
69+
int libmongodbcapi_status_get_code(Pointer status);
6170

6271
/**
63-
* Pump the message queue.
72+
* Initializes the mongodbcapi library, required before any other call.
73+
*
74+
* <p>Cannot be called again without {@link #libmongodbcapi_lib_fini(Pointer, Pointer)} being called first.</p>
6475
*
65-
* @param db the db pointer
66-
* @return the error, or 0 if success
76+
* @param initParams the embedded mongod initialization parameters.
77+
* @param status the `libmongodbcapi_status` pointer from which to get an associated status code.
78+
* @return the lib pointer or null if there was a failure. Modifies the status on failure.
6779
*/
68-
int libmongodbcapi_db_pump(Pointer db);
80+
Pointer libmongodbcapi_lib_init(Structure initParams, Pointer status);
6981

7082
/**
71-
* Create a new client instance.
83+
* Tears down the state of the library, all databases must be closed before calling this.
7284
*
73-
* @param db the db pointer
74-
* @return the client pointer
85+
* @param lib the `libmongodbcapi_lib` pointer
86+
* @param status the `libmongodbcapi_status` pointer from which to get an associated status code.
87+
* @return the error code or 0 if successful. Modifies the status on failure.
7588
*/
76-
Pointer libmongodbcapi_db_client_new(Pointer db);
89+
int libmongodbcapi_lib_fini(Pointer lib, Pointer status);
7790

7891
/**
79-
* Destroy a client instance.
92+
* Creates an embedded MongoDB instance and returns a handle with the service context.
8093
*
81-
* @param client the client pointer
94+
* @param lib the `libmongodbcapi_lib` pointer
95+
* @param yamlConfig null-terminated YAML formatted MongoDB configuration string
96+
* @param status the `libmongodbcapi_status` pointer from which to get an associated status code.
97+
* @return the instance pointer or null if there was a failure. Modifies the status on failure.
8298
*/
83-
void libmongodbcapi_db_client_destroy(Pointer client);
99+
Pointer libmongodbcapi_instance_create(Pointer lib, String yamlConfig, Pointer status);
100+
101+
/**
102+
* Shuts down an embedded MongoDB instance.
103+
*
104+
* @param instance the `libmongodbcapi_instance` pointer to be destroyed.
105+
* @param status the `libmongodbcapi_status` pointer from which to get an associated status code.
106+
* @return the error code or 0 if successful. Modifies the status on failure.
107+
*/
108+
int libmongodbcapi_instance_destroy(Pointer instance, Pointer status);
109+
110+
/**
111+
* Create a new embedded client instance.
112+
*
113+
* @param instance the `libmongodbcapi_instance` pointer.
114+
* @param status the `libmongodbcapi_status` pointer from which to get an associated status code.
115+
* @return the client pointer or null if there was a failure. Modifies the status on failure.
116+
*/
117+
Pointer libmongodbcapi_client_create(Pointer instance, Pointer status);
118+
119+
/**
120+
* Destroys an embedded client instance.
121+
*
122+
* @param client the `libmongodbcapi_client` pointer.
123+
* @param status the `libmongodbcapi_status` pointer from which to get an associated status code.
124+
* @return the error code or 0 if successful. Modifies the status on failure.
125+
*/
126+
int libmongodbcapi_client_destroy(Pointer client, Pointer status);
84127

85128
/**
86129
* Make an RPC call. Not clear yet on whether this is the correct signature.
87130
*
88-
* @param client the client pointer
131+
* @param client the `libmongodbcapi_client` pointer
89132
* @param input the RPC input
90133
* @param inputSize the RPC input size
91134
* @param output the RPC output
92135
* @param outputSize the RPC output size
93-
* @return the error, or 0 if success
136+
* @param status status the `libmongodbcapi_status` pointer from which to get an associated status code.
137+
* @return the error code or 0 if successful. Modifies the status on failure.
94138
*/
95-
int libmongodbcapi_db_client_wire_protocol_rpc(Pointer client, byte[] input, int inputSize, PointerByReference output,
96-
IntByReference outputSize);
139+
int libmongodbcapi_client_invoke(Pointer client, byte[] input, int inputSize, PointerByReference output, IntByReference outputSize,
140+
Pointer status);
141+
97142

98-
/**
99-
* Gets the last error.
100-
*
101-
* @return the last error
102-
*/
103-
int libmongodbcapi_get_last_error();
104143
}

0 commit comments

Comments
 (0)