Skip to content

Commit 40e2d1f

Browse files
authored
Deprecate currentOp/collStats commands (#1175)
JAVA-4845
1 parent e48cda1 commit 40e2d1f

File tree

7 files changed

+70
-23
lines changed

7 files changed

+70
-23
lines changed

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ import org.bson.BsonDocument
2626
import org.bson.BsonInt32
2727
import org.bson.BsonString
2828
import org.bson.codecs.BsonDocumentCodec
29-
import org.bson.codecs.DocumentCodec
3029
import spock.lang.IgnoreIf
3130

3231
import static com.mongodb.ClusterFixture.getBinding
3332
import static com.mongodb.ClusterFixture.isDiscoverableReplicaSet
3433
import static com.mongodb.ClusterFixture.serverVersionAtLeast
3534
import static com.mongodb.ClusterFixture.serverVersionLessThan
35+
import static java.util.Collections.singletonList
3636

3737
class CreateCollectionOperationSpecification extends OperationFunctionalSpecification {
3838

@@ -160,9 +160,7 @@ class CreateCollectionOperationSpecification extends OperationFunctionalSpecific
160160
collectionNameExists(getCollectionName())
161161

162162
when:
163-
def stats = new CommandReadOperation<>(getDatabaseName(),
164-
new BsonDocument('collStats', new BsonString(getCollectionName())),
165-
new BsonDocumentCodec()).execute(getBinding())
163+
def stats = storageStats()
166164

167165
then:
168166
stats.getBoolean('capped').getValue()
@@ -186,10 +184,7 @@ class CreateCollectionOperationSpecification extends OperationFunctionalSpecific
186184
execute(operation, async)
187185

188186
then:
189-
new CommandReadOperation<>(getDatabaseName(),
190-
new BsonDocument('collStats', new BsonString(getCollectionName())),
191-
new DocumentCodec()).execute(getBinding())
192-
.getInteger('nindexes') == expectedNumberOfIndexes
187+
storageStats().getInt32('nindexes').intValue() == expectedNumberOfIndexes
193188

194189
where:
195190
autoIndex | expectedNumberOfIndexes | async
@@ -291,4 +286,21 @@ class CreateCollectionOperationSpecification extends OperationFunctionalSpecific
291286
def collectionNameExists(String collectionName) {
292287
getCollectionInfo(collectionName) != null
293288
}
289+
290+
BsonDocument storageStats() {
291+
if (serverVersionLessThan(6, 2)) {
292+
return new CommandReadOperation<>(getDatabaseName(),
293+
new BsonDocument('collStats', new BsonString(getCollectionName())),
294+
new BsonDocumentCodec()).execute(getBinding())
295+
}
296+
BatchCursor<BsonDocument> cursor = new AggregateOperation(
297+
getNamespace(),
298+
singletonList(new BsonDocument('$collStats', new BsonDocument('storageStats', new BsonDocument()))),
299+
new BsonDocumentCodec()).execute(getBinding())
300+
try {
301+
return cursor.next().first().getDocument('storageStats')
302+
} finally {
303+
cursor.close()
304+
}
305+
}
294306
}

driver-core/src/test/resources/client-side-encryption/legacy/bypassedCommand.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
]
7979
},
8080
{
81-
"description": "current op is not bypassed",
81+
"description": "kill op is not bypassed",
8282
"clientOptions": {
8383
"autoEncryptOpts": {
8484
"kmsProviders": {
@@ -90,14 +90,15 @@
9090
{
9191
"name": "runCommand",
9292
"object": "database",
93-
"command_name": "currentOp",
93+
"command_name": "killOp",
9494
"arguments": {
9595
"command": {
96-
"currentOp": 1
96+
"killOp": 1,
97+
"op": 1234
9798
}
9899
},
99100
"result": {
100-
"errorContains": "command not supported for auto encryption: currentOp"
101+
"errorContains": "command not supported for auto encryption: killOp"
101102
}
102103
}
103104
]

driver-legacy/src/main/com/mongodb/DBCollection.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1914,7 +1914,12 @@ public void dropIndexes(final String indexName) {
19141914
*
19151915
* @return a CommandResult containing the statistics about this collection
19161916
* @mongodb.driver.manual reference/command/collStats/ collStats Command
1917+
* @mongodb.driver.manual reference/operator/aggregation/collStats/ $collStats
1918+
* @deprecated If you are using server release 3.4 or newer, use the {@code $collStats} aggregation pipeline stage via
1919+
* {@link #aggregate(List, AggregationOptions)} instead.
1920+
* This method uses the {@code collStats} command, which is deprecated since server release 6.2.
19171921
*/
1922+
@Deprecated
19181923
public CommandResult getStats() {
19191924
return getDB().executeCommand(new BsonDocument("collStats", new BsonString(getName())), getReadPreference());
19201925
}
@@ -1923,8 +1928,13 @@ public CommandResult getStats() {
19231928
* Checks whether this collection is capped
19241929
*
19251930
* @return true if this is a capped collection
1926-
* @mongodb.driver.manual /core/capped-collections/#check-if-a-collection-is-capped Capped Collections
1931+
* @mongodb.driver.manual core/capped-collections/#check-if-a-collection-is-capped Capped Collections
1932+
* @mongodb.driver.manual reference/operator/aggregation/collStats/ $collStats
1933+
* @deprecated If you are using server release 3.4 or newer, use the {@code $collStats} aggregation pipeline stage via
1934+
* {@link #aggregate(List, AggregationOptions)} instead, and inspect the {@code storageStats.capped} field.
1935+
* This method uses the {@code collStats} command, which is deprecated since server release 6.2.
19271936
*/
1937+
@Deprecated
19281938
public boolean isCapped() {
19291939
CommandResult commandResult = getStats();
19301940
Object cappedField = commandResult.get("capped");

driver-legacy/src/test/functional/com/mongodb/DBCollectionSpecification.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ import org.bson.UuidRepresentation
5858
import org.bson.codecs.BsonDocumentCodec
5959
import org.bson.codecs.BsonValueCodec
6060
import org.bson.codecs.UuidCodec
61+
import spock.lang.IgnoreIf
6162
import spock.lang.Specification
6263

6364
import java.util.concurrent.TimeUnit
6465

6566
import static Fixture.getMongoClient
67+
import static com.mongodb.ClusterFixture.serverVersionAtLeast
6668
import static com.mongodb.CustomMatchers.isTheSameAs
6769
import static com.mongodb.LegacyMixedBulkWriteOperation.createBulkWriteOperationForDelete
6870
import static com.mongodb.LegacyMixedBulkWriteOperation.createBulkWriteOperationForUpdate
@@ -260,6 +262,7 @@ class DBCollectionSpecification extends Specification {
260262
thrown(IllegalArgumentException)
261263
}
262264

265+
@IgnoreIf({ serverVersionAtLeast(6, 2) })
263266
def 'getStats should execute the expected command with the collection default read preference'() {
264267
given:
265268
def executor = new TestOperationExecutor([new BsonDocument('ok', new BsonInt32(1))])

driver-legacy/src/test/functional/com/mongodb/DBTest.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@
3737
import static com.mongodb.ClusterFixture.isDiscoverableReplicaSet;
3838
import static com.mongodb.ClusterFixture.isSharded;
3939
import static com.mongodb.ClusterFixture.serverVersionAtLeast;
40+
import static com.mongodb.ClusterFixture.serverVersionLessThan;
4041
import static com.mongodb.DBObjectMatchers.hasFields;
4142
import static com.mongodb.DBObjectMatchers.hasSubdocument;
4243
import static com.mongodb.Fixture.getDefaultDatabaseName;
4344
import static com.mongodb.Fixture.getMongoClient;
4445
import static com.mongodb.ReadPreference.secondary;
4546
import static com.mongodb.client.Fixture.getMongoClientSettingsBuilder;
47+
import static java.util.Collections.singletonList;
4648
import static org.hamcrest.CoreMatchers.containsString;
4749
import static org.hamcrest.CoreMatchers.hasItem;
4850
import static org.hamcrest.CoreMatchers.hasItems;
@@ -124,7 +126,7 @@ public void shouldCreateCappedCollection() {
124126
collection.drop();
125127
database.createCollection(collectionName, new BasicDBObject("capped", true)
126128
.append("size", 242880));
127-
assertTrue(database.getCollection(collectionName).isCapped());
129+
assertTrue(isCapped(database.getCollection(collectionName)));
128130
}
129131

130132
@Test
@@ -134,7 +136,7 @@ public void shouldCreateCappedCollectionWithMaxNumberOfDocuments() {
134136
.append("size", 242880)
135137
.append("max", 10));
136138

137-
assertThat(cappedCollectionWithMax.getStats(), hasSubdocument(new BasicDBObject("capped", true).append("max", 10)));
139+
assertThat(storageStats(cappedCollectionWithMax), hasSubdocument(new BasicDBObject("capped", true).append("max", 10)));
138140

139141
for (int i = 0; i < 11; i++) {
140142
cappedCollectionWithMax.insert(new BasicDBObject("x", i));
@@ -148,7 +150,7 @@ public void shouldCreateUncappedCollection() {
148150
BasicDBObject creationOptions = new BasicDBObject("capped", false);
149151
database.createCollection(collectionName, creationOptions);
150152

151-
assertFalse(database.getCollection(collectionName).isCapped());
153+
assertFalse(isCapped(database.getCollection(collectionName)));
152154
}
153155

154156
@Test(expected = MongoCommandException.class)
@@ -347,4 +349,25 @@ BsonDocument getCollectionInfo(final String collectionName) {
347349
return new ListCollectionsOperation<>(getDefaultDatabaseName(), new BsonDocumentCodec())
348350
.filter(new BsonDocument("name", new BsonString(collectionName))).execute(getBinding()).next().get(0);
349351
}
352+
353+
private boolean isCapped(final DBCollection collection) {
354+
if (serverVersionLessThan(6, 2)) {
355+
return collection.isCapped();
356+
} else {
357+
Object capped = storageStats(collection).get("capped");
358+
return Boolean.TRUE.equals(capped) || Integer.valueOf(1).equals(capped);
359+
}
360+
}
361+
362+
private DBObject storageStats(final DBCollection collection) {
363+
if (serverVersionLessThan(6, 2)) {
364+
return collection.getStats();
365+
} else {
366+
try (Cursor cursor = collection.aggregate(singletonList(
367+
new BasicDBObject("$collStats", new BasicDBObject("storageStats", new BasicDBObject()))),
368+
AggregationOptions.builder().build())) {
369+
return (DBObject) cursor.next().get("storageStats");
370+
}
371+
}
372+
}
350373
}

driver-legacy/src/test/unit/com/mongodb/DBSpecification.groovy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ import org.bson.BsonDouble
3535
import spock.lang.Specification
3636

3737
import static Fixture.getMongoClient
38+
import static com.mongodb.ClusterFixture.serverVersionLessThan
3839
import static com.mongodb.CustomMatchers.isTheSameAs
3940
import static com.mongodb.MongoClientSettings.getDefaultCodecRegistry
41+
import static org.junit.Assume.assumeTrue
4042
import static spock.util.matcher.HamcrestSupport.expect
4143

4244
class DBSpecification extends Specification {
@@ -202,6 +204,9 @@ class DBSpecification extends Specification {
202204
}
203205

204206
def 'should use provided read preference for obedient commands'() {
207+
if (cmd.get('collStats') != null) {
208+
assumeTrue(serverVersionLessThan(6, 2))
209+
}
205210
given:
206211
def mongo = Stub(MongoClient)
207212
mongo.mongoClientOptions >> MongoClientOptions.builder().build()

driver-sync/src/examples/documentation/DocumentationSamples.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -721,13 +721,6 @@ public void testRunCommand() {
721721
// Start runCommand Example 1
722722
database.runCommand(new Document("buildInfo", 1));
723723
// End runCommand Example 1
724-
725-
database.getCollection("restaurants").drop();
726-
database.createCollection("restaurants");
727-
728-
// Start runCommand Example 2
729-
database.runCommand(new Document("collStats", "restaurants"));
730-
// End runCommand Example 2
731724
}
732725

733726
@Test

0 commit comments

Comments
 (0)