use of com.mongodb.Block in project mongo-java-driver by mongodb.
the class AbstractRetryableReadsTest method setUp.
@Before
public void setUp() {
assumeFalse(skipTest);
assumeTrue("Skipping test: " + definition.getString("skipReason", new BsonString("")).getValue(), !definition.containsKey("skipReason"));
assumeFalse("Skipping count tests", filename.startsWith("count.") || filename.startsWith("count-"));
assumeFalse("Skipping list index names tests", filename.startsWith("listIndexNames"));
collectionHelper = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
final BsonDocument clientOptions = definition.getDocument("clientOptions", new BsonDocument());
ConnectionString connectionString = getConnectionString();
useMultipleMongoses = definition.getBoolean("useMultipleMongoses", BsonBoolean.FALSE).getValue();
if (useMultipleMongoses) {
assumeTrue(isSharded());
connectionString = getMultiMongosConnectionString();
assumeTrue("The system property org.mongodb.test.multi.mongos.uri is not set.", connectionString != null);
}
MongoClientSettings settings = getMongoClientSettingsBuilder().applyConnectionString(connectionString).addCommandListener(commandListener).applyToSocketSettings(new Block<SocketSettings.Builder>() {
@Override
public void apply(final SocketSettings.Builder builder) {
builder.readTimeout(5, TimeUnit.SECONDS);
}
}).applyToServerSettings(new Block<ServerSettings.Builder>() {
@Override
public void apply(final ServerSettings.Builder builder) {
builder.heartbeatFrequency(5, TimeUnit.MILLISECONDS);
}
}).writeConcern(getWriteConcern(clientOptions)).readConcern(getReadConcern(clientOptions)).readPreference(getReadPreference(clientOptions)).retryWrites(clientOptions.getBoolean("retryWrites", BsonBoolean.FALSE).getValue()).retryReads(clientOptions.getBoolean("retryReads", BsonBoolean.TRUE).getValue()).build();
mongoClient = createMongoClient(settings);
if (data != null) {
List<BsonDocument> documents = new ArrayList<BsonDocument>();
for (BsonValue document : data) {
documents.add(document.asDocument());
}
collectionHelper.drop();
if (documents.size() > 0) {
collectionHelper.insertDocuments(documents);
}
}
MongoDatabase database = mongoClient.getDatabase(databaseName);
if (gridFSBucketName != null) {
setupGridFSBuckets(database);
commandListener.reset();
}
collection = database.getCollection(collectionName, BsonDocument.class);
helper = new JsonPoweredCrudTestHelper(description, database, collection, gridFSBucket, mongoClient);
if (definition.containsKey("failPoint")) {
collectionHelper.runAdminCommand(definition.getDocument("failPoint"));
}
}
use of com.mongodb.Block in project repairnator by Spirals-Team.
the class ComputeTestFailingStats method main.
public static void main(String[] args) throws IOException {
if (args.length < 4) {
System.err.println("Usage: java ComputeTestFailingStats <url of mongodb with auth> <db name> <collection name> <path of the file to write>");
System.exit(-1);
}
Map<String, Integer> occurencesByFailure = new HashMap<>();
String dbCollectionUrl = args[0];
String dbName = args[1];
String collectionName = args[2];
String pathOutput = args[3];
File outputFile = new File(pathOutput);
MongoConnection mongoConnection = new MongoConnection(dbCollectionUrl, dbName);
MongoDatabase database = mongoConnection.getMongoDatabase();
MongoCollection<Document> collection = database.getCollection(collectionName);
Calendar limitDateFebruary2017 = Calendar.getInstance();
Calendar limitDateJanuary2018 = Calendar.getInstance();
limitDateFebruary2017.set(2017, Calendar.FEBRUARY, 1);
limitDateJanuary2018.set(2018, Calendar.JANUARY, 1);
Block<Document> block = new Block<Document>() {
@Override
public void apply(Document document) {
totalFailingBuild++;
String typeOfFailures = document.getString("typeOfFailures");
for (String failure : typeOfFailures.split(",")) {
if (failure.endsWith(":")) {
failure = failure.substring(0, failure.length() - 1);
}
if (failure.equals("skip") || failure.equals("skipped")) {
continue;
}
if (!occurencesByFailure.containsKey(failure)) {
occurencesByFailure.put(failure, 0);
}
int nbOcc = occurencesByFailure.get(failure);
nbOcc++;
occurencesByFailure.put(failure, nbOcc);
totalNumberOfFailures++;
}
}
};
collection.find(and(lt("buildFinishedDate", limitDateJanuary2018.getTime()), gt("buildFinishedDate", limitDateFebruary2017.getTime()), in("status", "PATCHED", "test errors", "test failure"))).forEach(block);
BufferedWriter buffer = new BufferedWriter(new FileWriter(outputFile));
buffer.write("failure\tNb Occurences\n");
buffer.flush();
for (Map.Entry<String, Integer> entry : occurencesByFailure.entrySet().stream().sorted(Map.Entry.comparingByValue(Collections.reverseOrder())).collect(Collectors.toList())) {
buffer.write(entry.getKey() + "\t" + entry.getValue() + "\n");
buffer.flush();
}
buffer.close();
System.out.println("Output written to " + pathOutput + " - " + totalFailingBuild + " failing build detected and " + totalNumberOfFailures + " failures counted.");
}
use of com.mongodb.Block in project jackrabbit-oak by apache.
the class MongoDocumentStore method getModStamps.
/**
* Returns the {@link Document#MOD_COUNT} and
* {@link NodeDocument#MODIFIED_IN_SECS} values of the documents with the
* given {@code keys}. The returned map will only contain entries for
* existing documents. The default value is -1 if the document does not have
* a modCount field. The same applies to the modified field.
*
* @param keys the keys of the documents.
* @return map with key to modification stamp mapping.
* @throws MongoException if the call fails
*/
@Nonnull
private Map<String, ModificationStamp> getModStamps(Iterable<String> keys) throws MongoException {
// Fetch only the modCount and id
final BasicDBObject fields = new BasicDBObject(Document.ID, 1);
fields.put(Document.MOD_COUNT, 1);
fields.put(NodeDocument.MODIFIED_IN_SECS, 1);
Map<String, ModificationStamp> modCounts = Maps.newHashMap();
nodes.withReadPreference(ReadPreference.primary()).find(Filters.in(Document.ID, keys)).projection(fields).forEach((Block<BasicDBObject>) obj -> {
String id = (String) obj.get(Document.ID);
Long modCount = Utils.asLong((Number) obj.get(Document.MOD_COUNT));
if (modCount == null) {
modCount = -1L;
}
Long modified = Utils.asLong((Number) obj.get(NodeDocument.MODIFIED_IN_SECS));
if (modified == null) {
modified = -1L;
}
modCounts.put(id, new ModificationStamp(modCount, modified));
});
return modCounts;
}
use of com.mongodb.Block in project jackrabbit-oak by apache.
the class MongoVersionGCSupport method logSplitDocIdsTobeDeleted.
private void logSplitDocIdsTobeDeleted(Bson query) {
// Fetch only the id
final BasicDBObject keys = new BasicDBObject(Document.ID, 1);
List<String> ids = new ArrayList<>();
getNodeCollection().withReadPreference(store.getConfiguredReadPreference(NODES)).find(query).projection(keys).forEach((Block<BasicDBObject>) doc -> ids.add(getID(doc)));
StringBuilder sb = new StringBuilder("Split documents with following ids were deleted as part of GC \n");
Joiner.on(StandardSystemProperty.LINE_SEPARATOR.value()).appendTo(sb, ids);
LOG.debug(sb.toString());
}
use of com.mongodb.Block in project mongo-java-driver by mongodb.
the class ConnectionsSurvivePrimaryStepDownProseTest method setUp.
@Before
public void setUp() {
assumeTrue(isDiscoverableReplicaSet() && serverVersionAtLeast(4, 0));
connectionPoolListener = new TestConnectionPoolListener();
MongoClientSettings settings = MongoClientSettings.builder(getMongoClientSettings()).retryWrites(false).applyToConnectionPoolSettings(new Block<ConnectionPoolSettings.Builder>() {
@Override
public void apply(final ConnectionPoolSettings.Builder builder) {
builder.addConnectionPoolListener(connectionPoolListener);
}
}).build();
collectionHelper = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(getDefaultDatabaseName(), COLLECTION_NAME));
client = MongoClients.create(settings);
MongoDatabase database = client.getDatabase(getDefaultDatabaseName());
collection = client.getDatabase(getDefaultDatabaseName()).getCollection(COLLECTION_NAME);
collection.withWriteConcern(WriteConcern.MAJORITY).drop();
database.withWriteConcern(WriteConcern.MAJORITY).createCollection(COLLECTION_NAME);
}
Aggregations