39
39
class CommandMarker implements Closeable {
40
40
private MongoClient client ;
41
41
private final ProcessBuilder processBuilder ;
42
- private boolean active ;
43
42
44
43
CommandMarker (final Map <String , Object > options ) {
45
44
String connectionString ;
@@ -50,6 +49,13 @@ class CommandMarker implements Closeable {
50
49
connectionString = "mongodb://localhost:27020" ;
51
50
}
52
51
52
+ if (!options .containsKey ("mongocryptdBypassSpawn" ) || !((Boolean ) options .get ("mongocryptdBypassSpawn" ))) {
53
+ processBuilder = new ProcessBuilder (createMongocryptdSpawnArgs (options ));
54
+ startProcess ();
55
+ } else {
56
+ processBuilder = null ;
57
+ }
58
+
53
59
client = MongoClients .create (MongoClientSettings .builder ()
54
60
.applyConnectionString (new ConnectionString (connectionString ))
55
61
.applyToClusterSettings (new Block <ClusterSettings .Builder >() {
@@ -59,13 +65,6 @@ public void apply(final ClusterSettings.Builder builder) {
59
65
}
60
66
})
61
67
.build ());
62
- active = false ;
63
-
64
- if (!options .containsKey ("mongocryptdBypassSpawn" ) || !((Boolean ) options .get ("mongocryptdBypassSpawn" ))) {
65
- processBuilder = new ProcessBuilder (createMongocryptdSpawnArgs (options ));
66
- } else {
67
- processBuilder = null ;
68
- }
69
68
}
70
69
71
70
void mark (final String databaseName , final RawBsonDocument command , final SingleResultCallback <RawBsonDocument > callback ) {
@@ -79,13 +78,22 @@ public void onResult(final RawBsonDocument result, final Throwable t) {
79
78
}
80
79
}
81
80
};
82
- executeCommand (databaseName , command , new SingleResultCallback <RawBsonDocument >() {
81
+ runCommand (databaseName , command , new SingleResultCallback <RawBsonDocument >() {
83
82
@ Override
84
83
public void onResult (final RawBsonDocument result , final Throwable t ) {
85
84
if (t == null ) {
86
85
wrappedCallback .onResult (result , null );
87
86
} else if (t instanceof MongoTimeoutException && processBuilder != null ) {
88
- executeCommand (databaseName , command , wrappedCallback );
87
+ startProcessAndContinue (new SingleResultCallback <Void >() {
88
+ @ Override
89
+ public void onResult (final Void result , final Throwable t ) {
90
+ if (t != null ) {
91
+ callback .onResult (null , t );
92
+ } else {
93
+ runCommand (databaseName , command , wrappedCallback );
94
+ }
95
+ }
96
+ });
89
97
} else {
90
98
wrappedCallback .onResult (null , t );
91
99
}
@@ -98,38 +106,28 @@ public void close() {
98
106
client .close ();
99
107
}
100
108
101
- private void executeCommand (final String databaseName , final RawBsonDocument markableCommand ,
102
- final SingleResultCallback <RawBsonDocument > callback ) {
103
- spawnIfNecessary (new SingleResultCallback <Void >(){
104
- @ Override
105
- public void onResult (final Void result , final Throwable t ) {
106
- if (t != null ) {
107
- callback .onResult (null , t );
108
- } else {
109
- client .getDatabase (databaseName )
110
- .withReadConcern (ReadConcern .DEFAULT )
111
- .withReadPreference (ReadPreference .primary ())
112
- .runCommand (markableCommand , RawBsonDocument .class , callback );
113
- }
114
- }
115
- });
109
+ private void runCommand (final String databaseName , final RawBsonDocument command ,
110
+ final SingleResultCallback <RawBsonDocument > callback ) {
111
+ client .getDatabase (databaseName )
112
+ .withReadConcern (ReadConcern .DEFAULT )
113
+ .withReadPreference (ReadPreference .primary ())
114
+ .runCommand (command , RawBsonDocument .class , callback );
116
115
}
117
116
118
- private synchronized void spawnIfNecessary (final SingleResultCallback <Void > callback ) {
117
+ private void startProcessAndContinue (final SingleResultCallback <Void > callback ) {
119
118
try {
120
- if (processBuilder != null ) {
121
- synchronized (this ) {
122
- if (!active ) {
123
- processBuilder .start ();
124
- active = true ;
125
- }
126
- }
127
- }
119
+ startProcess ();
128
120
callback .onResult (null , null );
129
121
} catch (Throwable t ) {
130
- callback .onResult (null ,
131
- new MongoClientException ("Exception starting mongocryptd process. Is `mongocryptd` on the system path?" , t ));
122
+ callback .onResult (null , t );
132
123
}
133
124
}
134
125
126
+ private void startProcess () {
127
+ try {
128
+ processBuilder .start ();
129
+ } catch (Throwable t ) {
130
+ throw new MongoClientException ("Exception starting mongocryptd process. Is `mongocryptd` on the system path?" , t );
131
+ }
132
+ }
135
133
}
0 commit comments