Skip to content

Commit e5e1b98

Browse files
committed
JAVA-2564: Refactor Mongo class to create a separate class for the ServerSession pool
1 parent e27e3db commit e5e1b98

File tree

2 files changed

+93
-58
lines changed

2 files changed

+93
-58
lines changed

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

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import com.mongodb.connection.ServerDescription;
3535
import com.mongodb.connection.SocketStreamFactory;
3636
import com.mongodb.event.ClusterListener;
37-
import com.mongodb.internal.connection.ConcurrentPool;
3837
import com.mongodb.internal.connection.PowerOfTwoBufferPool;
3938
import com.mongodb.internal.thread.DaemonThreadFactory;
4039
import com.mongodb.operation.BatchCursor;
@@ -44,20 +43,14 @@
4443
import com.mongodb.operation.ReadOperation;
4544
import com.mongodb.operation.WriteOperation;
4645
import com.mongodb.selector.LatencyMinimizingServerSelector;
47-
import org.bson.BsonBinary;
4846
import org.bson.BsonBoolean;
4947
import org.bson.BsonDocument;
50-
import org.bson.BsonDocumentWriter;
5148
import org.bson.BsonTimestamp;
52-
import org.bson.UuidRepresentation;
53-
import org.bson.codecs.EncoderContext;
54-
import org.bson.codecs.UuidCodec;
5549

5650
import java.util.ArrayList;
5751
import java.util.Collection;
5852
import java.util.Collections;
5953
import java.util.List;
60-
import java.util.UUID;
6154
import java.util.concurrent.ConcurrentHashMap;
6255
import java.util.concurrent.ConcurrentLinkedQueue;
6356
import java.util.concurrent.ConcurrentMap;
@@ -106,6 +99,7 @@ public class Mongo {
10699

107100
private final ConcurrentLinkedQueue<ServerCursorAndNamespace> orphanedCursors = new ConcurrentLinkedQueue<ServerCursorAndNamespace>();
108101
private final ExecutorService cursorCleaningService;
102+
private final ServerSessionPool serverSessionPool = new ServerSessionPool();
109103

110104
/**
111105
* Creates a Mongo instance based on a (single) mongodb node (localhost, default port)
@@ -538,6 +532,7 @@ public void dropDatabase(final String dbName) {
538532
*/
539533
public void close() {
540534
cluster.close();
535+
serverSessionPool.close();
541536
if (cursorCleaningService != null) {
542537
cursorCleaningService.shutdownNow();
543538
}
@@ -960,28 +955,6 @@ public void close() {
960955
}
961956
}
962957

963-
private final ConcurrentPool<ServerSession> serverSessionPool =
964-
new ConcurrentPool<ServerSession>(Integer.MAX_VALUE, new ServerSessionItemFactory());
965-
966-
private static class ServerSessionImpl implements ServerSession {
967-
private final BsonDocument identifier;
968-
private int transactionNumber;
969-
970-
ServerSessionImpl(final BsonBinary identifier) {
971-
this.identifier = new BsonDocument("id", identifier);
972-
}
973-
974-
@Override
975-
public BsonDocument getIdentifier() {
976-
return identifier;
977-
}
978-
979-
@Override
980-
public long advanceTransactionNumber() {
981-
return transactionNumber++;
982-
}
983-
}
984-
985958
private ExecutorService createCursorCleaningService() {
986959
ScheduledExecutorService newTimer = Executors.newSingleThreadScheduledExecutor(new DaemonThreadFactory("CleanCursors"));
987960
newTimer.scheduleAtFixedRate(new Runnable() {
@@ -1033,35 +1006,6 @@ private static class ServerCursorAndNamespace {
10331006
}
10341007
}
10351008

1036-
1037-
private static final class ServerSessionItemFactory implements ConcurrentPool.ItemFactory<ServerSession> {
1038-
@Override
1039-
public ServerSession create(final boolean initialize) {
1040-
return new ServerSessionImpl(createNewServerSessionIdentifier());
1041-
}
1042-
1043-
@Override
1044-
public void close(final ServerSession serverSession) {
1045-
// TODO: pruning
1046-
}
1047-
1048-
@Override
1049-
public boolean shouldPrune(final ServerSession serverSession) {
1050-
return false;
1051-
}
1052-
1053-
private BsonBinary createNewServerSessionIdentifier() {
1054-
UuidCodec uuidCodec = new UuidCodec(UuidRepresentation.STANDARD);
1055-
BsonDocument holder = new BsonDocument();
1056-
BsonDocumentWriter bsonDocumentWriter = new BsonDocumentWriter(holder);
1057-
bsonDocumentWriter.writeStartDocument();
1058-
bsonDocumentWriter.writeName("id");
1059-
uuidCodec.encode(bsonDocumentWriter, UUID.randomUUID(), EncoderContext.builder().build());
1060-
bsonDocumentWriter.writeEndDocument();
1061-
return holder.getBinary("id");
1062-
}
1063-
}
1064-
10651009
/**
10661010
* Mongo.Holder can be used as a static place to hold several instances of Mongo. Security is not enforced at this level, and needs to
10671011
* be done on the application side.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2017 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;
18+
19+
import com.mongodb.internal.connection.ConcurrentPool;
20+
import org.bson.BsonBinary;
21+
import org.bson.BsonDocument;
22+
import org.bson.BsonDocumentWriter;
23+
import org.bson.UuidRepresentation;
24+
import org.bson.codecs.EncoderContext;
25+
import org.bson.codecs.UuidCodec;
26+
27+
import java.util.UUID;
28+
29+
class ServerSessionPool {
30+
private final ConcurrentPool<ServerSession> serverSessionPool =
31+
new ConcurrentPool<ServerSession>(Integer.MAX_VALUE, new ServerSessionItemFactory());
32+
33+
ServerSession get() {
34+
return serverSessionPool.get();
35+
}
36+
37+
void release(final ServerSession serverSession) {
38+
serverSessionPool.release(serverSession);
39+
}
40+
41+
void close() {
42+
serverSessionPool.close();
43+
}
44+
45+
private static class ServerSessionImpl implements ServerSession {
46+
private final BsonDocument identifier;
47+
private int transactionNumber;
48+
49+
ServerSessionImpl(final BsonBinary identifier) {
50+
this.identifier = new BsonDocument("id", identifier);
51+
}
52+
53+
@Override
54+
public BsonDocument getIdentifier() {
55+
return identifier;
56+
}
57+
58+
@Override
59+
public long advanceTransactionNumber() {
60+
return transactionNumber++;
61+
}
62+
}
63+
64+
private static final class ServerSessionItemFactory implements ConcurrentPool.ItemFactory<ServerSession> {
65+
@Override
66+
public ServerSession create(final boolean initialize) {
67+
return new ServerSessionImpl(createNewServerSessionIdentifier());
68+
}
69+
70+
@Override
71+
public void close(final ServerSession serverSession) {
72+
// TODO: pruning
73+
}
74+
75+
@Override
76+
public boolean shouldPrune(final ServerSession serverSession) {
77+
return false;
78+
}
79+
80+
private BsonBinary createNewServerSessionIdentifier() {
81+
UuidCodec uuidCodec = new UuidCodec(UuidRepresentation.STANDARD);
82+
BsonDocument holder = new BsonDocument();
83+
BsonDocumentWriter bsonDocumentWriter = new BsonDocumentWriter(holder);
84+
bsonDocumentWriter.writeStartDocument();
85+
bsonDocumentWriter.writeName("id");
86+
uuidCodec.encode(bsonDocumentWriter, UUID.randomUUID(), EncoderContext.builder().build());
87+
bsonDocumentWriter.writeEndDocument();
88+
return holder.getBinary("id");
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)