Skip to content

Instantly share code, notes, and snippets.

@jyemin
Created December 21, 2015 16:53
Show Gist options
  • Save jyemin/b2a172b830ae502786a7 to your computer and use it in GitHub Desktop.
Save jyemin/b2a172b830ae502786a7 to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2015 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package cs;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoException;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
import com.mongodb.WriteConcern;
import java.net.UnknownHostException;
import java.util.List;
import static java.util.Arrays.asList;
public class MongosFailoverTest {
public static void main(final String[] args) throws UnknownHostException, InterruptedException {
MongoClient mongoClient = new MongoClient(asList(new ServerAddress("localhost:27017"), new ServerAddress("localhost:27018")),
MongoClientOptions.builder()
.writeConcern(WriteConcern.ACKNOWLEDGED)
.readPreference(ReadPreference.secondaryPreferred())
.connectionsPerHost(100)
.connectTimeout(60000)
.cursorFinalizerEnabled(true)
.maxWaitTime(120000)
.socketKeepAlive(false)
.socketTimeout(300000)
.threadsAllowedToBlockForConnectionMultiplier(5)
.build());
DBCollection collection = mongoClient.getDB("test").getCollection("MongosFailoverTest");
try {
while (true) {
collection.insert(new BasicDBObject());
Thread.sleep(100);
}
} catch (MongoException e) {
System.out.println(e);
}
long startTime = System.nanoTime();
collection.findOne();
System.out.println("Elapsed time for query after failed insert: " + (System.nanoTime() - startTime) / 1000000 + " ms");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment