Skip to content

Commit 12445f8

Browse files
author
Ryan
committed
started the process of separating out the load balancing logic so it can be tested.
1 parent 36d04c9 commit 12445f8

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

src/main/com/mongodb/ReplicaSetStatus.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class ReplicaSetStatus {
6666
void start() {
6767
_updater.start();
6868
}
69-
69+
7070
boolean ready(){
7171
return _setName != null;
7272
}
@@ -151,30 +151,45 @@ ServerAddress getASecondary(){
151151
/**
152152
* @return a good secondary or null if can't find one
153153
*/
154-
ServerAddress getASecondary( String tagKey, String tagValue ){
154+
ServerAddress getASecondary( String tagKey, String tagValue ) {
155155
_checkClosed();
156+
return getASecondary(tagKey, tagValue, _all, _random);
157+
}
158+
159+
/**
160+
* This was extracted so we can test the logic from a unit test. This can't be
161+
* tested from a standalone unit test until node is more decoupled from this class.
162+
*
163+
* @return a good secondary or null if can't find one
164+
*/
165+
static ServerAddress getASecondary( final String pTagKey,
166+
final String pTagValue,
167+
final List<Node> pNodes,
168+
final Random pRandom)
169+
{
156170
Node best = null;
157171
double badBeforeBest = 0;
158172

159-
if (tagKey == null && tagValue != null || tagValue == null & tagKey != null)
173+
if (pTagKey == null && pTagValue != null || pTagValue == null & pTagKey != null)
160174
throw new IllegalArgumentException( "Tag Key & Value must be consistent: both defined or not defined." );
161175

162-
int start = _random.nextInt( _all.size() );
176+
int start = pRandom.nextInt( pNodes.size() );
177+
178+
final int nodeCount = pNodes.size();
163179

164180
double mybad = 0;
165181

166-
for ( int i=0; i<_all.size(); i++ ){
167-
Node n = _all.get( ( start + i ) % _all.size() );
182+
for ( int i=0; i < nodeCount; i++ ){
183+
Node n = pNodes.get( ( start + i ) % nodeCount );
168184

169185
if ( ! n.secondary() ){
170186
mybad++;
171187
continue;
172-
} else if (tagKey != null && !n.checkTag( tagKey, tagValue )){
188+
} else if (pTagKey != null && !n.checkTag( pTagKey, pTagValue )){
173189
mybad++;
174190
continue;
175191
}
176192

177-
178193
if ( best == null ){
179194
best = n;
180195
badBeforeBest = mybad;
@@ -183,10 +198,9 @@ ServerAddress getASecondary( String tagKey, String tagValue ){
183198
}
184199

185200
float diff = best._pingTime - n._pingTime;
186-
if ( diff > slaveAcceptableLatencyMS ||
187-
// this is a complex way to make sure we get a random distribution of slaves
188-
( ( badBeforeBest - mybad ) / ( _all.size() - 1 ) ) > _random.nextDouble() )
189-
{
201+
202+
// this is a complex way to make sure we get a random distribution of slaves
203+
if ( diff > slaveAcceptableLatencyMS || ( ( badBeforeBest - mybad ) / ( nodeCount - 1 ) ) > pRandom.nextDouble() ) {
190204
best = n;
191205
badBeforeBest = mybad;
192206
mybad = 0;
@@ -196,6 +210,7 @@ ServerAddress getASecondary( String tagKey, String tagValue ){
196210

197211
if ( best == null )
198212
return null;
213+
199214
return best._addr;
200215
}
201216

@@ -209,6 +224,9 @@ boolean hasServerUp() {
209224
return false;
210225
}
211226

227+
/**
228+
* The replica set node object.
229+
*/
212230
class Node {
213231

214232
Node( ServerAddress addr ){
@@ -366,7 +384,7 @@ public void close() {
366384
_port.close();
367385
_port = null;
368386
}
369-
387+
370388
final ServerAddress _addr;
371389
final Set<String> _names = Collections.synchronizedSet( new HashSet<String>() );
372390
DBPort _port; // we have our own port so we can set different socket options and don't have to owrry about the pool

0 commit comments

Comments
 (0)