Skip to content

Commit 9573f64

Browse files
JAVA-404: slaveOk support for inline mapreduce (routes to secondaries); changed CommandResult to include serverUsed, made readPref-Secondary set slaveOk query flag, toString/toJSON on Node/ReplicaSetStatus.
1 parent a1692e0 commit 9573f64

12 files changed

+222
-82
lines changed

src/main/com/mongodb/CommandResult.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@
1818

1919
package com.mongodb;
2020

21+
2122
/**
2223
* A simple wrapper for the result of getLastError() calls and other commands
2324
*/
2425
public class CommandResult extends BasicDBObject {
2526

26-
CommandResult() { }
27+
CommandResult(ServerAddress srv) {
28+
super();
29+
_host = srv;
30+
//so it is shown in toString/debug
31+
put("serverUsed", srv.toString());
32+
}
2733

2834
/**
2935
* gets the "ok" field which is the result of the command
@@ -116,8 +122,12 @@ public void throwOnError() throws MongoException {
116122
}
117123
}
118124

125+
public ServerAddress getServerUsed() {
126+
return _host;
127+
}
119128

120129
DBObject _cmd;
130+
ServerAddress _host = null;
121131
private static final long serialVersionUID = 1L;
122132

123133
static class CommandFailure extends MongoException {

src/main/com/mongodb/DB.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.List;
2929
import java.util.Set;
3030

31+
import com.mongodb.DBApiLayer.Result;
3132
import com.mongodb.util.Util;
3233

3334
/**
@@ -139,33 +140,48 @@ public DBCollection getCollectionFromString( String s ){
139140
* @throws MongoException
140141
* @dochub commands
141142
*/
142-
public CommandResult command( DBObject cmd )
143-
throws MongoException {
144-
return command( cmd , 0 );
143+
public CommandResult command( DBObject cmd ) throws MongoException{
144+
return command( cmd, 0 );
145145
}
146146

147147
/**
148148
* Executes a database command.
149149
* @see <a href="http://mongodb.onconfluence.com/display/DOCS/List+of+Database+Commands">List of Commands</a>
150150
* @param cmd dbobject representing the command to execute
151151
* @param options query options to use
152+
* @param readPrefs ReadPreferences for this command (nodes selection is the biggest part of this)
152153
* @return result of command from the database
153154
* @dochub commands
154155
* @throws MongoException
155156
*/
156-
public CommandResult command( DBObject cmd , int options )
157+
public CommandResult command( DBObject cmd , int options, ReadPreference readPrefs )
157158
throws MongoException {
158159

159-
// TODO - Is ReadPreference OK for commands?
160-
Iterator<DBObject> i = getCollection("$cmd").__find(cmd, new BasicDBObject(), 0, -1, 0, options, null, DefaultDBDecoder.FACTORY.create() );
160+
Iterator<DBObject> i = getCollection("$cmd").__find(cmd, new BasicDBObject(), 0, -1, 0, options, readPrefs , DefaultDBDecoder.FACTORY.create());
161161
if ( i == null || ! i.hasNext() )
162162
return null;
163163

164-
CommandResult res = (CommandResult)i.next();
165-
res._cmd = cmd;
166-
return res;
164+
DBObject res = i.next();
165+
ServerAddress sa = (i instanceof Result) ? ((Result) i).getServerAddress() : null;
166+
CommandResult cr = new CommandResult(sa);
167+
cr.putAll( res );
168+
cr._cmd = cmd;
169+
return cr;
167170
}
168171

172+
/**
173+
* Executes a database command.
174+
* @see <a href="http://mongodb.onconfluence.com/display/DOCS/List+of+Database+Commands">List of Commands</a>
175+
* @param cmd dbobject representing the command to execute
176+
* @param options query options to use
177+
* @return result of command from the database
178+
* @dochub commands
179+
* @throws MongoException
180+
*/
181+
public CommandResult command( DBObject cmd , int options )
182+
throws MongoException {
183+
return command(cmd, options, null);
184+
}
169185
/**
170186
* Executes a database command.
171187
* This method constructs a simple dbobject and calls {@link DB#command(com.mongodb.DBObject) }

src/main/com/mongodb/DBCollection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ public MapReduceOutput mapReduce( MapReduceCommand command ) throws MongoExcepti
10491049
// if type in inline, then query options like slaveOk is fine
10501050
CommandResult res = null;
10511051
if (command.getOutputType() == MapReduceCommand.OutputType.INLINE)
1052-
res = _db.command( cmd, getOptions() );
1052+
res = _db.command( cmd, getOptions(), command.getReadPreference() != null ? command.getReadPreference() : getReadPreference() );
10531053
else
10541054
res = _db.command( cmd );
10551055
res.throwOnError();

src/main/com/mongodb/DBPort.java

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ public DBPort( ServerAddress addr ){
6767
_decoder = _options.dbDecoderFactory.create();
6868
}
6969

70-
Response call( OutMessage msg , DBCollection coll ) throws IOException {
71-
return go( msg , coll );
70+
Response call( OutMessage msg , DBCollection coll ) throws IOException{
71+
return go( msg, coll );
7272
}
7373

74-
Response call( OutMessage msg , DBCollection coll , DBDecoder decoder ) throws IOException {
75-
return go( msg , coll , false , null , decoder );
74+
Response call( OutMessage msg , DBCollection coll , DBDecoder decoder) throws IOException{
75+
return go( msg, coll, false, null, decoder);
7676
}
7777

78-
Response call( OutMessage msg , DBCollection coll , ReadPreference readPref, DBDecoder decoder ) throws IOException {
79-
return go( msg , coll , false , readPref , decoder );
78+
Response call( OutMessage msg , DBCollection coll , ReadPreference readPref , DBDecoder decoder) throws IOException{
79+
return go( msg, coll, false, readPref, decoder);
8080
}
8181

8282
void say( OutMessage msg )
@@ -86,15 +86,14 @@ void say( OutMessage msg )
8686

8787
private synchronized Response go( OutMessage msg , DBCollection coll )
8888
throws IOException {
89-
return go( msg , coll , false , null , null );
89+
return go( msg , coll , false, null, null );
9090
}
9191

92-
private synchronized Response go( OutMessage msg , DBCollection coll , DBDecoder decoder )
93-
throws IOException {
94-
return go( msg , coll , false , null , decoder );
92+
private synchronized Response go( OutMessage msg , DBCollection coll , DBDecoder decoder ) throws IOException{
93+
return go( msg, coll, false, null, decoder );
9594
}
9695

97-
private synchronized Response go( OutMessage msg , DBCollection coll , boolean forceReponse , ReadPreference readPref , DBDecoder decoder )
96+
private synchronized Response go( OutMessage msg , DBCollection coll , boolean forceReponse , ReadPreference readPref, DBDecoder decoder)
9897
throws IOException {
9998

10099
if ( _processingResponse ){
@@ -137,49 +136,48 @@ private synchronized Response go( OutMessage msg , DBCollection coll , boolean f
137136
}
138137
}
139138

140-
synchronized CommandResult getLastError( DB db , WriteConcern concern) throws IOException {
141-
DBApiLayer dbAL = (DBApiLayer) db;
142-
return runCommand( dbAL , concern.getCommand() );
139+
synchronized CommandResult getLastError( DB db , WriteConcern concern ) throws IOException{
140+
DBApiLayer dbAL = (DBApiLayer) db;
141+
return runCommand( dbAL, concern.getCommand() );
143142
}
144143

145-
synchronized DBObject findOne( DB db , String coll , DBObject q ) throws IOException {
144+
synchronized private Response findOne( DB db , String coll , DBObject q ) throws IOException {
146145
OutMessage msg = OutMessage.query( db._mongo , 0 , db.getName() + "." + coll , 0 , -1 , q , null );
147-
148146
Response res = go( msg , db.getCollection( coll ) , DefaultDBDecoder.FACTORY.create() );
149-
if ( res.size() == 0 )
150-
return null;
151-
if ( res.size() > 1 )
152-
throw new MongoInternalException( "something is wrong. size:" + res.size() );
153-
return res.get(0);
147+
return res;
148+
}
149+
150+
synchronized private Response findOne( String ns , DBObject q ) throws IOException{
151+
OutMessage msg = OutMessage.query( null , 0 , ns , 0 , -1 , q , null );
152+
Response res = go( msg , null , true, null, DefaultDBDecoder.FACTORY.create() );
153+
return res;
154154
}
155155

156156
synchronized CommandResult runCommand( DB db , DBObject cmd ) throws IOException {
157-
DBObject res = findOne( db , "$cmd" , cmd );
158-
if ( res == null )
159-
throw new MongoInternalException( "something is wrong, no command result" );
160-
return (CommandResult)res;
157+
Response res = findOne( db , "$cmd" , cmd );
158+
return convertToCR( res );
161159
}
162160

163-
synchronized DBObject findOne( String ns , DBObject q ) throws IOException{
164-
OutMessage msg = OutMessage.query( null , 0 , ns , 0 , -1 , q , null );
165-
Response res = go( msg , null , true , null, DefaultDBDecoder.FACTORY.create() );
161+
synchronized CommandResult runCommand( String db , DBObject cmd ) throws IOException {
162+
Response res = findOne( db + ".$cmd" , cmd );
163+
return convertToCR( res );
164+
}
165+
166+
private CommandResult convertToCR(Response res) {
166167
if ( res.size() == 0 )
167168
return null;
168169
if ( res.size() > 1 )
169170
throw new MongoInternalException( "something is wrong. size:" + res.size() );
170-
return res.get(0);
171-
}
172171

173-
synchronized CommandResult runCommand( String db , DBObject cmd ) throws IOException {
174-
DBObject res = findOne( db + ".$cmd" , cmd );
175-
if ( res == null )
172+
DBObject data = res.get(0);
173+
if ( data == null )
176174
throw new MongoInternalException( "something is wrong, no command result" );
177-
CommandResult cr = new CommandResult();
178-
cr.putAll( res );
175+
176+
CommandResult cr = new CommandResult(res.serverUsed());
177+
cr.putAll( data );
179178
return cr;
180179
}
181180

182-
183181
synchronized CommandResult tryGetLastError( DB db , long last, WriteConcern concern) throws IOException {
184182
if ( last != _calls )
185183
return null;
@@ -263,6 +261,13 @@ public String host(){
263261
return _addr.toString();
264262
}
265263

264+
/**
265+
* @return the server address for this port
266+
*/
267+
public ServerAddress serverAddress() {
268+
return _sa;
269+
}
270+
266271
@Override
267272
public String toString(){
268273
return "{DBPort " + host() + "}";

src/main/com/mongodb/DBTCPConnector.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
package com.mongodb;
2020

21-
import com.mongodb.ReadPreference.*;
22-
2321
import java.io.IOException;
2422
import java.net.SocketTimeoutException;
2523
import java.util.ArrayList;
@@ -28,6 +26,8 @@
2826
import java.util.logging.Level;
2927
import java.util.logging.Logger;
3028

29+
import com.mongodb.ReadPreference.TaggedReadPreference;
30+
3131
public class DBTCPConnector implements DBConnector {
3232

3333
static Logger _logger = Logger.getLogger( Bytes.LOGGER.getName() + ".tcp" );
@@ -165,7 +165,7 @@ public WriteResult say( DB db , OutMessage m , WriteConcern concern , ServerAddr
165165
if ( concern.raiseNetworkErrors() )
166166
throw new MongoException.Network( "can't say something" , ioe );
167167

168-
CommandResult res = new CommandResult();
168+
CommandResult res = new CommandResult(port.serverAddress());
169169
res.put( "ok" , false );
170170
res.put( "$err" , "NETWORK ERROR" );
171171
return new WriteResult( res , concern );

src/main/com/mongodb/DefaultDBCallback.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ private DBObject _create( List<String> path ){
121121
}
122122
}
123123

124-
if ( _collection != null && _collection._name.equals( "$cmd" ) )
125-
return new CommandResult();
126124
return new BasicDBObject();
127125
}
128126

src/main/com/mongodb/MapReduceOutput.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
public class MapReduceOutput {
2626

2727
@SuppressWarnings("unchecked")
28-
public MapReduceOutput( DBCollection from , DBObject cmd, BasicDBObject raw ){
29-
_raw = raw;
28+
public MapReduceOutput( DBCollection from , DBObject cmd, CommandResult raw ){
29+
_commandResult = raw;
3030
_cmd = cmd;
3131

3232
if ( raw.containsField( "results" ) ) {
@@ -80,19 +80,28 @@ public DBCollection getOutputCollection(){
8080
return _coll;
8181
}
8282

83+
@Deprecated
8384
public BasicDBObject getRaw(){
84-
return _raw;
85+
return _commandResult;
86+
}
87+
88+
public CommandResult getCommandResult(){
89+
return _commandResult;
8590
}
8691

8792
public DBObject getCommand() {
8893
return _cmd;
8994
}
9095

96+
public ServerAddress getServerUsed() {
97+
return _commandResult.getServerUsed();
98+
}
99+
91100
public String toString(){
92-
return _raw.toString();
101+
return _commandResult.toString();
93102
}
94103

95-
final BasicDBObject _raw;
104+
final CommandResult _commandResult;
96105

97106
final String _collname;
98107
String _dbname = null;

src/main/com/mongodb/OutMessage.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,13 @@
1818

1919
package com.mongodb;
2020

21-
import static org.bson.BSON.EOO;
22-
import static org.bson.BSON.OBJECT;
23-
import static org.bson.BSON.REF;
24-
2521
import java.io.IOException;
2622
import java.io.OutputStream;
2723
import java.util.concurrent.atomic.AtomicInteger;
2824

29-
import org.bson.*;
25+
import org.bson.BSONObject;
26+
import org.bson.BasicBSONEncoder;
3027
import org.bson.io.PoolOutputBuffer;
31-
import org.bson.types.ObjectId;
3228

3329
class OutMessage extends BasicBSONEncoder {
3430

@@ -44,8 +40,7 @@ static OutMessage query( Mongo m , int options , String ns , int numToSkip , int
4440

4541
static OutMessage query( Mongo m , int options , String ns , int numToSkip , int batchSize , DBObject query , DBObject fields, ReadPreference readPref, DBEncoder enc ){
4642
OutMessage out = new OutMessage( m , 2004, enc );
47-
out._appendQuery( options , ns , numToSkip , batchSize , query , fields );
48-
out.setReadPreference( readPref );
43+
out._appendQuery( options , ns , numToSkip , batchSize , query , fields, readPref);
4944
return out;
5045
}
5146

@@ -69,9 +64,15 @@ static OutMessage query( Mongo m , int options , String ns , int numToSkip , int
6964
this( m , enc );
7065
reset( op );
7166
}
72-
private void _appendQuery( int options , String ns , int numToSkip , int batchSize , DBObject query , DBObject fields ){
67+
private void _appendQuery( int options , String ns , int numToSkip , int batchSize , DBObject query , DBObject fields, ReadPreference readPref){
7368
_queryOptions = options;
74-
writeInt( options );
69+
_readPref = readPref;
70+
71+
//If the readPrefs are non-null and non-primary, set slaveOk query option
72+
if (!(_readPref instanceof ReadPreference.PrimaryReadPreference))
73+
_queryOptions |= Bytes.QUERYOPTION_SLAVEOK;
74+
75+
writeInt( _queryOptions );
7576
writeCString( ns );
7677

7778
writeInt( numToSkip );
@@ -149,10 +150,6 @@ public ReadPreference getReadPreference(){
149150
return _readPref;
150151
}
151152

152-
public void setReadPreference( ReadPreference readPref ){
153-
_readPref = readPref;
154-
}
155-
156153
private Mongo _mongo;
157154
private PoolOutputBuffer _buffer;
158155
private int _id;

src/main/com/mongodb/ReadPreference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
package com.mongodb;
1515

16-
import java.util.*;
16+
import java.util.Map;
1717

1818
public class ReadPreference {
1919
public static class PrimaryReadPreference extends ReadPreference {

0 commit comments

Comments
 (0)