Skip to content

Commit 7d3a663

Browse files
committed
Updated FsyncUnlockOperation to be a ReadOperation
The command does not require a primary to run. JAVA-2501
1 parent c45dae7 commit 7d3a663

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

driver-core/src/main/com/mongodb/operation/FsyncUnlockOperation.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mongodb.operation;
1818

1919
import com.mongodb.MongoNamespace;
20+
import com.mongodb.binding.ReadBinding;
2021
import com.mongodb.binding.WriteBinding;
2122
import com.mongodb.connection.Connection;
2223
import com.mongodb.operation.OperationHelper.CallableWithConnection;
@@ -35,20 +36,49 @@
3536
* @mongodb.driver.manual reference/command/fsyncUnlock/ fsyncUnlock command
3637
* @since 3.2
3738
*/
38-
public class FsyncUnlockOperation implements WriteOperation<BsonDocument> {
39+
public class FsyncUnlockOperation implements WriteOperation<BsonDocument>, ReadOperation<BsonDocument> {
40+
private static final BsonDocument FSYNC_UNLOCK_COMMAND = new BsonDocument("fsyncUnlock", new BsonInt32(1));
41+
42+
/**
43+
* Unlocks the MongoDB server, allowing write operations to go through.
44+
*
45+
* @param binding the binding to execute in the context of
46+
* @return the result of the operation
47+
* @deprecated use {@link #execute(ReadBinding)} instead.
48+
*/
49+
@Deprecated
3950
@Override
4051
public BsonDocument execute(final WriteBinding binding) {
4152
return withConnection(binding, new CallableWithConnection<BsonDocument>() {
4253
@Override
4354
public BsonDocument call(final Connection connection) {
4455
if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) {
45-
return executeWrappedCommandProtocol(binding, "admin", new BsonDocument("fsyncUnlock", new BsonInt32(1)), connection);
56+
return executeWrappedCommandProtocol(binding, "admin", FSYNC_UNLOCK_COMMAND, connection);
4657
} else {
47-
return connection.query(new MongoNamespace("admin", "$cmd.sys.unlock"), new BsonDocument(), null, 0, 1, 0,
48-
false, false, false, false, false, false,
49-
new BsonDocumentCodec()).getResults().get(0);
58+
return queryUnlock(connection);
5059
}
5160
}
5261
});
5362
}
63+
64+
@Override
65+
public BsonDocument execute(final ReadBinding binding) {
66+
return withConnection(binding, new CallableWithConnection<BsonDocument>() {
67+
@Override
68+
public BsonDocument call(final Connection connection) {
69+
if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) {
70+
return executeWrappedCommandProtocol(binding, "admin", FSYNC_UNLOCK_COMMAND, connection);
71+
} else {
72+
return queryUnlock(connection);
73+
}
74+
}
75+
});
76+
}
77+
78+
private BsonDocument queryUnlock(final Connection connection) {
79+
return connection.query(new MongoNamespace("admin", "$cmd.sys.unlock"), new BsonDocument(), null, 0, 1, 0,
80+
false, false, false, false, false, false,
81+
new BsonDocumentCodec()).getResults().get(0);
82+
}
83+
5484
}

driver-core/src/test/functional/com/mongodb/operation/FsyncUnlockOperationSpecification.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.mongodb.operation
1818

19+
import com.mongodb.binding.ReadBinding
1920
import org.bson.BsonBoolean
2021
import org.bson.BsonDocument
2122
import org.bson.BsonInt32
@@ -36,7 +37,7 @@ class FsyncUnlockOperationSpecification extends Specification {
3637
.execute(getBinding())
3738

3839
when:
39-
def result = new FsyncUnlockOperation().execute(getBinding())
40+
def result = new FsyncUnlockOperation().execute(getBinding() as ReadBinding)
4041

4142
then:
4243
result

driver/src/main/com/mongodb/Mongo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ public CommandResult fsyncAndLock() {
641641
* @mongodb.driver.manual reference/command/fsync/ fsync command
642642
*/
643643
public DBObject unlock() {
644-
return DBObjects.toDBObject(execute(new FsyncUnlockOperation()));
644+
return DBObjects.toDBObject(execute(new FsyncUnlockOperation(), readPreference));
645645
}
646646

647647
/**

driver/src/test/unit/com/mongodb/MongoSpecification.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ class MongoSpecification extends Specification {
102102
}
103103
mongo = Spy(Mongo, constructorArgs: [cluster, MongoClientOptions.builder().build(), []]) {
104104
getDB('admin') >> db
105-
execute(_, _) >> new BsonDocument('fsyncLock', BsonBoolean.TRUE)
106-
execute(_) >> new BsonDocument('ok', new BsonInt32(1))
105+
1 * execute(_, _) >> new BsonDocument('fsyncLock', BsonBoolean.TRUE)
106+
1 * execute(_, _) >> new BsonDocument('ok', new BsonInt32(1))
107107
}
108108

109109
when:

0 commit comments

Comments
 (0)