@@ -40,89 +40,157 @@ public class SecondaryReadTest extends TestCase {
40
40
private static final double MAX_DEVIATION_PERCENT = 1.0 ;
41
41
42
42
@ Test (groups = {"basic" })
43
- @ SuppressWarnings ({"unchecked" })
44
- public void testSecondaryReads () throws Exception {
43
+ public void testSecondaryReads1 () throws Exception {
44
+
45
+ final Mongo mongo = loadMongo ();
46
+
47
+ final CommandResult result = serverStatusCmd (mongo );
48
+
49
+ // If the result is null, this is not a replica set.
50
+ if (result == null ) return ;
51
+
52
+ final List <TestHost > testHosts = new ArrayList <TestHost >();
53
+ final String primaryHostnameAndPort = extractHosts (result , testHosts );
54
+ final DBCollection col = loadCleanDbCollection (mongo );
55
+
56
+ final List <ObjectId > insertedIds = insertTestData (col );
57
+
58
+ // Get the opcounter/query data for the hosts.
59
+ loadQueryCount (testHosts , true );
60
+
61
+ final int secondaryCount = getSecondaryCount (testHosts );
62
+
63
+ // Perform some reads on the secondaries
64
+ col .setReadPreference (ReadPreference .SECONDARY );
65
+
66
+ for (int idx =0 ; idx < ITERATION_COUNT ; idx ++) {
67
+ for (ObjectId id : insertedIds ) {
68
+ final BasicDBObject doc = (BasicDBObject )col .findOne (new BasicDBObject ("_id" , id ));
69
+ if (doc == null ) throw new IllegalStateException ("Doc not found" );
70
+ if (!doc .getObjectId ("_id" ).equals (id )) throw new IllegalStateException ("Ids are off" );
71
+ }
72
+ }
73
+
74
+ loadQueryCount (testHosts , false );
75
+
76
+ verifySecondaryCounts (secondaryCount , testHosts );
77
+ }
78
+
79
+ @ Test (groups = {"basic" })
80
+ public void testSecondaryReads2 () throws Exception {
81
+
82
+ final Mongo mongo = loadMongo ();
83
+
84
+ final CommandResult result = serverStatusCmd (mongo );
85
+
86
+ // If the result is null, this is not a replica set.
87
+ if (result == null ) return ;
88
+
89
+ final List <TestHost > testHosts = new ArrayList <TestHost >();
90
+ final String primaryHostnameAndPort = extractHosts (result , testHosts );
91
+ final DBCollection col = loadCleanDbCollection (mongo );
92
+
93
+ final List <ObjectId > insertedIds = insertTestData (col );
94
+
95
+ // Get the opcounter/query data for the hosts.
96
+ loadQueryCount (testHosts , true );
97
+
98
+ final int secondaryCount = getSecondaryCount (testHosts );
99
+
100
+ // Perform some reads on the secondaries
101
+ mongo .setReadPreference (ReadPreference .SECONDARY );
102
+
103
+ for (int idx =0 ; idx < ITERATION_COUNT ; idx ++) {
104
+ for (ObjectId id : insertedIds ) {
105
+ final BasicDBObject doc = (BasicDBObject )col .findOne (new BasicDBObject ("_id" , id ));
106
+ if (doc == null ) throw new IllegalStateException ("Doc not found" );
107
+ if (!doc .getObjectId ("_id" ).equals (id )) throw new IllegalStateException ("Ids are off" );
108
+ }
109
+ }
110
+
111
+ loadQueryCount (testHosts , false );
45
112
46
- final Mongo mongo = new Mongo (new MongoURI ("mongodb://127.0.0.1:27017,127.0.0.1:27018" ));
113
+ verifySecondaryCounts (secondaryCount , testHosts );
114
+ }
115
+
116
+ private Mongo loadMongo () throws Exception {
117
+ return new Mongo (new MongoURI ("mongodb://127.0.0.1:27017,127.0.0.1:27018" ));
118
+ }
47
119
120
+ private CommandResult serverStatusCmd (final Mongo pMongo ) {
48
121
// Check to see if this is a replica set... if not, get out of here.
49
- final CommandResult result = mongo .getDB ("admin" ).command (new BasicDBObject ("replSetGetStatus" , 1 ));
122
+ final CommandResult result = pMongo .getDB ("admin" ).command (new BasicDBObject ("replSetGetStatus" , 1 ));
50
123
51
124
final String errorMsg = result .getErrorMessage ();
52
125
53
126
if (errorMsg != null && errorMsg .indexOf ("--replSet" ) != -1 ) {
54
127
System .err .println ("---- SecondaryReadTest: This is not a replica set - not testing secondary reads" );
55
- return ;
128
+ return null ;
56
129
}
57
130
58
- String primaryHostnameAndPort = null ;
131
+ return result ;
132
+ }
59
133
134
+ @ SuppressWarnings ({"unchecked" })
135
+ private String extractHosts (final CommandResult pResult , final List <TestHost > pHosts ) {
136
+ String primaryHostnameAndPort = null ;
60
137
// Extract the repl set members.
61
- final List < TestHost > testHosts = new ArrayList < TestHost >();
62
- for (final BasicDBObject member : (List <BasicDBObject >)result .get ("members" )) {
138
+
139
+ for (final BasicDBObject member : (List <BasicDBObject >)pResult .get ("members" )) {
63
140
String hostnameAndPort = member .getString ("name" );
64
141
if (hostnameAndPort .indexOf (":" ) == -1 ) hostnameAndPort = hostnameAndPort + ":27017" ;
65
142
66
143
final String stateStr = member .getString ("stateStr" );
67
144
68
145
if (stateStr .equals ("PRIMARY" )) primaryHostnameAndPort = hostnameAndPort ;
69
146
70
- testHosts .add (new TestHost (hostnameAndPort , stateStr ));
147
+ pHosts .add (new TestHost (hostnameAndPort , stateStr ));
71
148
}
72
149
73
150
if (primaryHostnameAndPort == null ) throw new IllegalStateException ("No primary defined" );
74
151
75
- mongo .getDB ("com_mongodb_unittest_SecondaryReadTest" ).dropDatabase ();
76
- final DB db = mongo .getDB ("com_mongodb_unittest_SecondaryReadTest" );
77
- final DBCollection col = db .getCollection ("testBalance" );
152
+ return primaryHostnameAndPort ;
153
+ }
154
+
155
+ private DBCollection loadCleanDbCollection (final Mongo pMongo ) {
156
+ pMongo .getDB ("com_mongodb_unittest_SecondaryReadTest" ).dropDatabase ();
157
+ final DB db = pMongo .getDB ("com_mongodb_unittest_SecondaryReadTest" );
158
+ return db .getCollection ("testBalance" );
159
+ }
78
160
161
+ private List <ObjectId > insertTestData (final DBCollection pCol ) throws Exception {
79
162
final ArrayList <ObjectId > insertedIds = new ArrayList <ObjectId >();
80
163
81
164
// Insert some test data.
82
165
for (int idx =0 ; idx < INSERT_COUNT ; idx ++) {
83
166
final ObjectId id = ObjectId .get ();
84
- WriteResult writeResult = col .insert (new BasicDBObject ("_id" , id ), WriteConcern .REPLICAS_SAFE );
167
+ WriteResult writeResult = pCol .insert (new BasicDBObject ("_id" , id ), WriteConcern .REPLICAS_SAFE );
85
168
writeResult .getLastError ().throwOnError ();
86
169
insertedIds .add (id );
87
170
}
88
171
89
172
// Make sure everything is inserted.
90
173
while (true ) {
91
- final long count = col .count ();
174
+ final long count = pCol .count ();
92
175
if (count == INSERT_COUNT ) break ;
93
176
Thread .sleep (1000 );
94
177
}
95
178
96
- // Get the opcounter/query data for the hosts.
97
- loadQueryCount ( testHosts , true );
179
+ return insertedIds ;
180
+ }
98
181
182
+ private int getSecondaryCount (final List <TestHost > pHosts ) {
99
183
int secondaryCount = 0 ;
184
+ for (final TestHost testHost : pHosts ) if (testHost .stateStr .equals ("SECONDARY" )) secondaryCount ++;
185
+ return secondaryCount ;
186
+ }
100
187
101
- for (final TestHost testHost : testHosts ) if (testHost .stateStr .equals ("SECONDARY" )) secondaryCount ++;
102
-
103
- // Perform some reads on the secondaries
104
- col .setReadPreference (ReadPreference .SECONDARY );
105
-
106
- for (int idx =0 ; idx < ITERATION_COUNT ; idx ++) {
107
- for (ObjectId id : insertedIds ) {
108
- final BasicDBObject doc = (BasicDBObject )col .findOne (new BasicDBObject ("_id" , id ));
109
- if (doc == null ) throw new IllegalStateException ("Doc not found" );
110
- if (!doc .getObjectId ("_id" ).equals (id )) throw new IllegalStateException ("Ids are off" );
111
- }
112
- }
113
-
114
- loadQueryCount (testHosts , false );
115
-
116
- /*
117
- for (final TestHost testHost : testHosts) {
118
- System.out.println("--- host: " + testHost.hostnameAndPort + " - queries: " + testHost.queriesBefore + " - after: " + testHost.queriesAfter);
119
- }
120
- */
188
+ private void verifySecondaryCounts (final int pSecondaryCount , final List <TestHost > pHosts ) {
121
189
122
190
// Verify the counts.
123
- final int expectedPerSecondary = TOTAL_COUNT / secondaryCount ;
191
+ final int expectedPerSecondary = TOTAL_COUNT / pSecondaryCount ;
124
192
125
- for (final TestHost testHost : testHosts ) {
193
+ for (final TestHost testHost : pHosts ) {
126
194
127
195
if (!testHost .stateStr .equals ("SECONDARY" )) continue ;
128
196
@@ -140,7 +208,14 @@ public void testSecondaryReads() throws Exception {
140
208
assertEquals (true , (deviation <= MAX_DEVIATION_PERCENT ));
141
209
}
142
210
}
143
- }
211
+
212
+ /*
213
+ for (final TestHost testHost : pHosts) {
214
+ System.out.println("--- host: " + testHost.hostnameAndPort + " - queries: " + testHost.queriesBefore + " - after: " + testHost.queriesAfter);
215
+ }
216
+ */
217
+
218
+ }
144
219
145
220
private static void loadQueryCount (final List <TestHost > pHosts , final boolean pBefore ) throws Exception {
146
221
for (final TestHost testHost : pHosts ) {
0 commit comments