Permalink
Browse files

JAVA-691: MongoAuthority experiment

  • Loading branch information...
1 parent 27b1454 commit e671135376a8b9ae96d7cf91533090d4fa1e51f1 @jyemin jyemin committed Jan 22, 2013
@@ -67,7 +67,7 @@ public static void main(String[] args) throws UnknownHostException, InterruptedE
System.out.println();
MongoClient mongoClient = new MongoClient(
- new MongoAuthority(new ServerAddress(server),
+ MongoAuthority.direct(new ServerAddress(server),
new MongoCredentials(user, MongoCredentials.Protocol.GSSAPI)),
new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
DB testDB = mongoClient.getDB(databaseName);
@@ -42,7 +42,7 @@ public static void main(String[] args) throws UnknownHostException, InterruptedE
MongoCredentials credentials = new MongoCredentials(user, pwd.toCharArray(), MongoCredentials.Protocol.STRONGEST, db);
- MongoClient mongoClient = new MongoClient(new MongoAuthority(new ServerAddress(server), credentials), new MongoClientOptions.Builder().build());
+ MongoClient mongoClient = new MongoClient(MongoAuthority.direct(new ServerAddress(server), credentials), new MongoClientOptions.Builder().build());
DB testDB = mongoClient.getDB(db);
testDB.getCollection("test").insert(new BasicDBObject());
@@ -21,7 +21,6 @@
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
@@ -33,40 +32,23 @@
/**
* @param m
- * @param addr
* @throws MongoException
*/
- public DBTCPConnector( Mongo m , ServerAddress addr ){
+ public DBTCPConnector( Mongo m ) {
_mongo = m;
_portHolder = new DBPortPool.Holder( m._options );
- _checkAddress( addr );
-
- setMasterAddress(addr);
- _allHosts = null;
- }
-
- /**
- * @param m
- * @param all
- * @throws MongoException
- */
- public DBTCPConnector( Mongo m , ServerAddress ... all ){
- this(m, Arrays.asList(all));
- }
-
- /**
- * @param m
- * @param all
- * @throws MongoException
- */
- public DBTCPConnector( Mongo m , List<ServerAddress> all ) {
- _mongo = m;
- _portHolder = new DBPortPool.Holder( m._options );
- _checkAddress( all );
-
- _allHosts = new ArrayList<ServerAddress>( all ); // make a copy so it can't be modified
-
- _connectionStatus = new DynamicConnectionStatus(m, _allHosts);
+ MongoAuthority.Type type = m.getAuthority().getType();
+ if (type == MongoAuthority.Type.DIRECT) {
+ setMasterAddress(m.getAuthority().getServerAddress());
+ } else if (type == MongoAuthority.Type.REPLICA_SET) {
+ _connectionStatus = new ReplicaSetStatus(m, m.getAuthority().getServerAddresses());
+ } else if (type == MongoAuthority.Type.MONGOS_SET) {
+ _connectionStatus = new MongosStatus(m, m.getAuthority().getServerAddresses());
+ } else if (type == MongoAuthority.Type.DYNAMIC_SET) {
+ _connectionStatus = new DynamicConnectionStatus(m, m.getAuthority().getServerAddresses());
+ } else {
+ throw new IllegalArgumentException("Unsupported authority type: " + type);
+ }
}
public void start() {
@@ -75,20 +57,6 @@ public void start() {
}
}
- private static ServerAddress _checkAddress( ServerAddress addr ){
- if ( addr == null )
- throw new NullPointerException( "address can't be null" );
- return addr;
- }
-
- private static ServerAddress _checkAddress( List<ServerAddress> addrs ){
- if ( addrs == null )
- throw new NullPointerException( "addresses can't be null" );
- if ( addrs.size() == 0 )
- throw new IllegalArgumentException( "need to specify at least 1 address" );
- return addrs.get(0);
- }
-
/**
* Start a "request".
*
@@ -325,7 +293,7 @@ public ServerAddress getAddress(){
* @return
*/
public List<ServerAddress> getAllAddress() {
- return _allHosts;
+ return _mongo._authority.getServerAddresses();
}
/**
@@ -350,18 +318,23 @@ public ServerAddress getAddress(){
}
public ReplicaSetStatus getReplicaSetStatus() {
- if (_connectionStatus == null) {
+ if (_connectionStatus instanceof ReplicaSetStatus) {
+ return (ReplicaSetStatus) _connectionStatus;
+ } else if (_connectionStatus instanceof DynamicConnectionStatus) {
+ return ((DynamicConnectionStatus) _connectionStatus).asReplicaSetStatus();
+ } else {
return null;
}
- return _connectionStatus.asReplicaSetStatus();
}
// This call can block if it's not yet known.
// Be careful when modifying this method, as this method is using the fact that _isMongosDirectConnection
// is of type Boolean and is null when uninitialized.
boolean isMongosConnection() {
- if (_connectionStatus != null) {
- return _connectionStatus.asMongosStatus() != null;
+ if (_connectionStatus instanceof MongosStatus) {
+ return true;
+ } else if (_connectionStatus instanceof DynamicConnectionStatus) {
+ return ((DynamicConnectionStatus) _connectionStatus).asMongosStatus() != null;
}
if (_isMongosDirectConnection == null) {
@@ -574,7 +547,7 @@ private synchronized boolean setMasterAddress(ServerAddress addr) {
public String debugString(){
StringBuilder buf = new StringBuilder( "DBTCPConnector: " );
if ( _connectionStatus != null ) {
- buf.append( "set : " ).append( _allHosts );
+ buf.append( "set : " ).append( _mongo._authority.getServerAddresses() );
} else {
ServerAddress master = getAddress();
buf.append( master ).append( " " ).append( master != null ? master.getSocketAddress() : null );
@@ -658,8 +631,7 @@ MyPort getMyPort() {
private volatile DBPortPool _masterPortPool;
private final Mongo _mongo;
private DBPortPool.Holder _portHolder;
- private final List<ServerAddress> _allHosts;
- private DynamicConnectionStatus _connectionStatus;
+ private ConnectionStatus _connectionStatus;
private final AtomicBoolean _closed = new AtomicBoolean(false);
@@ -190,7 +190,7 @@ public Mongo( ServerAddress addr ) {
* @throws MongoException
*/
public Mongo( ServerAddress addr , MongoOptions options ) {
- this(new MongoAuthority(addr), options);
+ this(MongoAuthority.direct(addr), options);
}
/**
@@ -221,7 +221,7 @@ public Mongo( ServerAddress left , ServerAddress right ) {
*/
@Deprecated
public Mongo( ServerAddress left , ServerAddress right , MongoOptions options ) {
- this(new MongoAuthority(Arrays.asList(left, right)), options);
+ this(MongoAuthority.dynamicSet(Arrays.asList(left, right)), options);
}
/**
@@ -260,7 +260,7 @@ public Mongo( List<ServerAddress> seeds ) {
* @throws MongoException
*/
public Mongo( List<ServerAddress> seeds , MongoOptions options ) {
- this(new MongoAuthority(seeds), options);
+ this(MongoAuthority.dynamicSet(seeds), options);
}
/**
@@ -298,10 +298,10 @@ public Mongo( MongoURI uri ) throws UnknownHostException {
_applyMongoOptions();
if ( authority.isDirect() ){
- _connector = new DBTCPConnector( this , authority.getServerAddress() );
+ _connector = new DBTCPConnector( this );
}
else {
- _connector = new DBTCPConnector( this , authority.getServerAddresses() );
+ _connector = new DBTCPConnector( this );
}
_connector.start();
@@ -599,13 +599,13 @@ boolean isMongosConnection() {
private static MongoAuthority getMongoAuthorityFromURI(final MongoURI uri) throws UnknownHostException {
if ( uri.getHosts().size() == 1 ){
- return new MongoAuthority(new ServerAddress(uri.getHosts().get(0)), uri.getCredentials());
+ return MongoAuthority.direct(new ServerAddress(uri.getHosts().get(0)), uri.getCredentials());
}
else {
List<ServerAddress> replicaSetSeeds = new ArrayList<ServerAddress>(uri.getHosts().size());
for ( String host : uri.getHosts() )
replicaSetSeeds.add( new ServerAddress( host ) );
- return new MongoAuthority(replicaSetSeeds, uri.getCredentials());
+ return MongoAuthority.dynamicSet(replicaSetSeeds, uri.getCredentials());
}
}
@@ -770,4 +770,13 @@ public String toString() {
", options=" + _options +
'}';
}
+
+ /**
+ * Gets the authority, which includes the connection type, the server address(es), and the credentials.
+
+ * @return the authority
+ */
+ public MongoAuthority getAuthority() {
+ return _authority;
+ }
}
Oops, something went wrong.

0 comments on commit e671135

Please sign in to comment.