Skip to content

Commit 515f338

Browse files
author
Ryan
committed
initial load.
1 parent 790f4a9 commit 515f338

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/**
2+
* Copyright (C) 2008 10gen 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+
// Mongo
20+
import com.mongodb.*;
21+
import org.bson.types.*;
22+
import com.mongodb.util.*;
23+
24+
import org.testng.annotations.Test;
25+
26+
27+
// Java
28+
import java.util.*;
29+
import java.util.concurrent.*;
30+
31+
public class SecondaryReadTest extends TestCase {
32+
33+
34+
private static final int INSERT_COUNT = 1000;
35+
36+
private static final int ITERATION_COUNT = 100;
37+
38+
private static final int TOTAL_COUNT = INSERT_COUNT * ITERATION_COUNT;
39+
40+
private static final double MAX_DEVIATION_PERCENT = 1.0;
41+
42+
@Test(groups = {"basic"})
43+
@SuppressWarnings({"unchecked"})
44+
public void testSecondaryReads() throws Exception {
45+
46+
final Mongo mongo = new Mongo(new MongoURI("mongodb://127.0.0.1:27017,127.0.0.1:27018"));
47+
48+
// 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));
50+
51+
final String errorMsg = result.getErrorMessage();
52+
53+
if (errorMsg != null && errorMsg.indexOf("--replSet") != -1) {
54+
System.err.println("---- SecondaryReadTest: This is not a replica set - not testing secondary reads");
55+
return;
56+
}
57+
58+
String primaryHostnameAndPort = null;
59+
60+
// Extract the repl set members.
61+
final List<TestHost> testHosts = new ArrayList<TestHost>();
62+
for (final BasicDBObject member : (List<BasicDBObject>)result.get("members")) {
63+
String hostnameAndPort = member.getString("name");
64+
if (hostnameAndPort.indexOf(":") == -1) hostnameAndPort = hostnameAndPort + ":27017";
65+
66+
final String stateStr = member.getString("stateStr");
67+
68+
if (stateStr.equals("PRIMARY")) primaryHostnameAndPort = hostnameAndPort;
69+
70+
testHosts.add(new TestHost(hostnameAndPort, stateStr));
71+
}
72+
73+
if (primaryHostnameAndPort == null) throw new IllegalStateException("No primary defined");
74+
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");
78+
79+
final ArrayList<ObjectId> insertedIds = new ArrayList<ObjectId>();
80+
81+
// Insert some test data.
82+
for (int idx=0; idx < INSERT_COUNT; idx++) {
83+
final ObjectId id = ObjectId.get();
84+
WriteResult writeResult = col.insert(new BasicDBObject("_id", id), WriteConcern.REPLICAS_SAFE);
85+
writeResult.getLastError().throwOnError();
86+
insertedIds.add(id);
87+
}
88+
89+
// Make sure everything is inserted.
90+
while (true) {
91+
final long count = col.count();
92+
if (count == INSERT_COUNT) break;
93+
Thread.sleep(1000);
94+
}
95+
96+
// Get the opcounter/query data for the hosts.
97+
loadQueryCount(testHosts, true);
98+
99+
int secondaryCount = 0;
100+
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+
*/
121+
122+
// Verify the counts.
123+
final int expectedPerSecondary = TOTAL_COUNT / secondaryCount;
124+
125+
for (final TestHost testHost : testHosts) {
126+
127+
if (!testHost.stateStr.equals("SECONDARY")) continue;
128+
129+
final long queriesExecuted = testHost.getQueriesExecuted();
130+
131+
if (expectedPerSecondary == queriesExecuted) continue;
132+
133+
if (queriesExecuted > expectedPerSecondary) {
134+
final double deviation = (double)100 - (((double)expectedPerSecondary / (double)queriesExecuted) * (double)100);
135+
//System.out.println("------ deviation: " + deviation);
136+
assertEquals(true, (deviation <= MAX_DEVIATION_PERCENT));
137+
} else {
138+
final double deviation = (double)100 - (((double)queriesExecuted / (double)expectedPerSecondary) * (double)100);
139+
//System.out.println("------ deviation: " + deviation);
140+
assertEquals(true, (deviation <= MAX_DEVIATION_PERCENT));
141+
}
142+
}
143+
}
144+
145+
private static void loadQueryCount(final List<TestHost> pHosts, final boolean pBefore) throws Exception {
146+
for (final TestHost testHost : pHosts) {
147+
final Mongo mongoHost = new Mongo(new MongoURI("mongodb://"+testHost.hostnameAndPort));
148+
try {
149+
final CommandResult serverStatusResult
150+
= mongoHost.getDB("com_mongodb_unittest_SecondaryReadTest").command(new BasicDBObject("serverStatus", 1));
151+
152+
final BasicDBObject opcounters = (BasicDBObject)serverStatusResult.get("opcounters");
153+
154+
if (pBefore) testHost.queriesBefore = opcounters.getLong("query");
155+
else testHost.queriesAfter = opcounters.getLong("query");
156+
157+
} finally { mongoHost.close(); }
158+
}
159+
}
160+
161+
private static class TestHost {
162+
private final String hostnameAndPort;
163+
private final String stateStr;
164+
165+
private long queriesBefore;
166+
private long queriesAfter;
167+
168+
public long getQueriesExecuted() { return queriesAfter - queriesBefore; }
169+
170+
private TestHost(final String pHostnameAndPort, final String pStateStr) {
171+
hostnameAndPort = pHostnameAndPort;
172+
stateStr = pStateStr;
173+
}
174+
}
175+
}
176+

0 commit comments

Comments
 (0)