3939class CommandMarker implements Closeable {
4040 private MongoClient client ;
4141 private final ProcessBuilder processBuilder ;
42- private boolean active ;
4342
4443 CommandMarker (final Map <String , Object > options ) {
4544 String connectionString ;
@@ -50,6 +49,13 @@ class CommandMarker implements Closeable {
5049 connectionString = "mongodb://localhost:27020" ;
5150 }
5251
52+ if (!options .containsKey ("mongocryptdBypassSpawn" ) || !((Boolean ) options .get ("mongocryptdBypassSpawn" ))) {
53+ processBuilder = new ProcessBuilder (createMongocryptdSpawnArgs (options ));
54+ startProcess ();
55+ } else {
56+ processBuilder = null ;
57+ }
58+
5359 client = MongoClients .create (MongoClientSettings .builder ()
5460 .applyConnectionString (new ConnectionString (connectionString ))
5561 .applyToClusterSettings (new Block <ClusterSettings .Builder >() {
@@ -59,13 +65,6 @@ public void apply(final ClusterSettings.Builder builder) {
5965 }
6066 })
6167 .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- }
6968 }
7069
7170 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) {
7978 }
8079 }
8180 };
82- executeCommand (databaseName , command , new SingleResultCallback <RawBsonDocument >() {
81+ runCommand (databaseName , command , new SingleResultCallback <RawBsonDocument >() {
8382 @ Override
8483 public void onResult (final RawBsonDocument result , final Throwable t ) {
8584 if (t == null ) {
8685 wrappedCallback .onResult (result , null );
8786 } 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+ });
8997 } else {
9098 wrappedCallback .onResult (null , t );
9199 }
@@ -98,38 +106,28 @@ public void close() {
98106 client .close ();
99107 }
100108
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 );
116115 }
117116
118- private synchronized void spawnIfNecessary (final SingleResultCallback <Void > callback ) {
117+ private void startProcessAndContinue (final SingleResultCallback <Void > callback ) {
119118 try {
120- if (processBuilder != null ) {
121- synchronized (this ) {
122- if (!active ) {
123- processBuilder .start ();
124- active = true ;
125- }
126- }
127- }
119+ startProcess ();
128120 callback .onResult (null , null );
129121 } 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 );
132123 }
133124 }
134125
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+ }
135133}
0 commit comments