Skip to content

Commit bd0643e

Browse files
experiment restoring driver-workload-executor
1 parent 5d6192c commit bd0643e

File tree

6 files changed

+264
-0
lines changed

6 files changed

+264
-0
lines changed

driver-sync/src/test/functional/com/mongodb/client/unified/Entities.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public final class Entities {
112112
private final Map<String, Long> iterationCounts = new HashMap<>();
113113
private final Map<String, BsonArray> errorDocumentsMap = new HashMap<>();
114114
private final Map<String, BsonArray> failureDocumentsMap = new HashMap<>();
115+
private final Map<String, List<BsonDocument>> eventsMap = new HashMap<>();
115116

116117
public boolean hasSuccessCount(final String id) {
117118
return successCounts.containsKey(id);
@@ -161,6 +162,14 @@ public BsonArray getFailureDocuments(final String id) {
161162
return getEntity(id, failureDocumentsMap, "failureDocuments");
162163
}
163164

165+
public boolean hasEvents(final String id) {
166+
return eventsMap.containsKey(id);
167+
}
168+
169+
public List<BsonDocument> getEvents(final String id) {
170+
return getEntity(id, eventsMap, "events");
171+
}
172+
164173
public void addResult(final String id, final BsonValue result) {
165174
putEntity(id, result, results);
166175
}

driver-sync/src/test/functional/com/mongodb/client/unified/UnifiedTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,10 @@ private OperationResult executeOperation(final UnifiedTestContext context, final
728728
}
729729
}
730730

731+
protected boolean terminateLoop() {
732+
return true;
733+
}
734+
731735
private OperationResult executeCreateEntities(final BsonDocument operation) {
732736
entities.init(operation.getDocument("arguments").getArray("entities"),
733737
startingClusterTime,
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2008-present MongoDB, 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+
plugins {
18+
id("application")
19+
id("java-library")
20+
id("project.base")
21+
id("conventions.test-artifacts")
22+
}
23+
24+
application {
25+
mainClass = "com.mongodb.workload.WorkloadExecutor"
26+
}
27+
28+
sourceSets {
29+
main {
30+
java { setSrcDirs(listOf("src/main")) }
31+
resources { setSrcDirs(listOf("src/resources")) }
32+
}
33+
}
34+
35+
dependencies {
36+
implementation(project(":driver-sync"))
37+
implementation(project(path = ":driver-core", configuration = "testArtifacts"))
38+
implementation(project(path = ":driver-sync", configuration = "testArtifacts"))
39+
implementation(platform(libs.junit.bom))
40+
implementation(libs.bundles.junit.vintage)
41+
}
42+
43+
tasks.withType<Javadoc>().configureEach {
44+
enabled = false
45+
}
46+
47+
java {
48+
sourceCompatibility = JavaVersion.VERSION_11
49+
targetCompatibility = JavaVersion.VERSION_11
50+
}
51+
52+
53+
tasks.withType<Jar> {
54+
manifest {
55+
attributes["Main-Class"] = "com.mongodb.workload.WorkloadExecutor"
56+
}
57+
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* Copyright 2008-present MongoDB, 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.workload;
18+
19+
import com.mongodb.client.unified.Entities;
20+
import com.mongodb.client.unified.UnifiedSyncTest;
21+
import com.mongodb.client.unified.UnifiedTest;
22+
import com.mongodb.internal.diagnostics.logging.Logger;
23+
import com.mongodb.internal.diagnostics.logging.Loggers;
24+
import org.bson.BsonArray;
25+
import org.bson.BsonDocument;
26+
import org.bson.BsonInt64;
27+
import org.bson.codecs.BsonDocumentCodec;
28+
import org.bson.codecs.DecoderContext;
29+
import org.bson.json.JsonReader;
30+
import org.bson.json.JsonWriterSettings;
31+
32+
import java.io.FileReader;
33+
import java.io.IOException;
34+
import java.nio.charset.StandardCharsets;
35+
import java.nio.file.Files;
36+
import java.nio.file.Path;
37+
import java.nio.file.Paths;
38+
import java.util.concurrent.CountDownLatch;
39+
import java.util.concurrent.TimeUnit;
40+
41+
public class WorkloadExecutor {
42+
private static final Logger LOGGER = Loggers.getLogger("workload-executor");
43+
private static final CountDownLatch terminationLatch = new CountDownLatch(1);
44+
private static volatile boolean terminateLoop;
45+
46+
public static void main(String[] args) throws IOException {
47+
if (args.length != 2) {
48+
System.out.println("Usage: AstrolabeTestRunner <path to workload spec JSON file> <path to results directory>");
49+
System.exit(1);
50+
}
51+
52+
String pathToWorkloadFile = args[0];
53+
String pathToResultsDirectory = args[1];
54+
55+
LOGGER.info("Max memory (GB): " + (Runtime.getRuntime().maxMemory() / 1_073_741_824.0));
56+
LOGGER.info("Path to workload file: '" + pathToWorkloadFile + "'");
57+
LOGGER.info("Path to results directory: '" + pathToResultsDirectory + "'");
58+
59+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
60+
LOGGER.info("Running shutdown hook");
61+
terminateLoop = true;
62+
try {
63+
if (!terminationLatch.await(1, TimeUnit.MINUTES)) {
64+
LOGGER.warn("Terminating after waiting for 1 minute for results to be written");
65+
} else {
66+
LOGGER.info("Terminating.");
67+
}
68+
} catch (InterruptedException e) {
69+
e.printStackTrace();
70+
}
71+
}));
72+
73+
74+
BsonDocument fileDocument;
75+
76+
try (FileReader reader = new FileReader(pathToWorkloadFile)) {
77+
fileDocument = new BsonDocumentCodec().decode(new JsonReader(reader), DecoderContext.builder().build());
78+
}
79+
80+
LOGGER.info("Executing workload: " + fileDocument.toJson(JsonWriterSettings.builder().indent(true).build()));
81+
82+
BsonArray testArray = fileDocument.getArray("tests");
83+
if (testArray.size() != 1) {
84+
throw new IllegalArgumentException("Expected exactly one test");
85+
}
86+
BsonDocument testDocument = testArray.get(0).asDocument();
87+
88+
UnifiedTest unifiedTest = new UnifiedSyncTest() {
89+
@Override
90+
protected boolean terminateLoop() {
91+
return terminateLoop;
92+
}
93+
};
94+
95+
try {
96+
String schemaVersion = fileDocument.getString("schemaVersion").getValue();
97+
BsonArray runOnRequirements = fileDocument.getArray("runOnRequirements", null);
98+
BsonArray createEntities = fileDocument.getArray("createEntities", new BsonArray());
99+
BsonArray initialData = fileDocument.getArray("initialData", new BsonArray());
100+
unifiedTest.setUp(
101+
"",
102+
null,
103+
null,
104+
null,
105+
1,
106+
1,
107+
schemaVersion,
108+
runOnRequirements,
109+
createEntities,
110+
initialData,
111+
testDocument);
112+
unifiedTest.shouldPassAllOutcomes(
113+
"",
114+
null,
115+
null,
116+
null,
117+
1,
118+
1,
119+
schemaVersion,
120+
runOnRequirements,
121+
createEntities,
122+
initialData,
123+
testDocument);
124+
Entities entities = unifiedTest.getEntities();
125+
126+
long iterationCount = -1;
127+
if (entities.hasIterationCount("iterations")) {
128+
iterationCount = entities.getIterationCount("iterations");
129+
}
130+
131+
long successCount = -1;
132+
if (entities.hasSuccessCount("successes")) {
133+
successCount = entities.getSuccessCount("successes");
134+
}
135+
136+
BsonArray errorDocuments = null;
137+
long errorCount = 0;
138+
if (entities.hasErrorDocuments("errors")) {
139+
errorDocuments = entities.getErrorDocuments("errors");
140+
errorCount = errorDocuments.size();
141+
}
142+
143+
BsonArray failureDocuments = null;
144+
long failureCount = 0;
145+
if (entities.hasFailureDocuments("failures")) {
146+
failureDocuments = entities.getFailureDocuments("failures");
147+
failureCount = failureDocuments.size();
148+
}
149+
150+
BsonArray eventDocuments = new BsonArray();
151+
if (entities.hasEvents("events")) {
152+
eventDocuments = new BsonArray(entities.getEvents("events"));
153+
}
154+
155+
BsonDocument eventsDocument = new BsonDocument()
156+
.append("errors", errorDocuments == null ? new BsonArray() : errorDocuments)
157+
.append("failures", failureDocuments == null ? new BsonArray() : failureDocuments)
158+
.append("events", eventDocuments);
159+
160+
BsonDocument resultsDocument = new BsonDocument()
161+
.append("numErrors", new BsonInt64(errorCount))
162+
.append("numFailures", new BsonInt64(failureCount))
163+
.append("numSuccesses", new BsonInt64(successCount))
164+
.append("numIterations", new BsonInt64(iterationCount));
165+
166+
writeFile(eventsDocument, Paths.get(pathToResultsDirectory, "events.json"));
167+
writeFile(resultsDocument, Paths.get(pathToResultsDirectory, "results.json"));
168+
} finally {
169+
unifiedTest.cleanUp();
170+
terminationLatch.countDown();
171+
}
172+
}
173+
174+
private static void writeFile(final BsonDocument document, final Path path) throws IOException {
175+
LOGGER.info("Writing file: '" + path.toFile().getAbsolutePath());
176+
Files.deleteIfExists(path);
177+
String json = document.toJson(JsonWriterSettings.builder().indent(true).build());
178+
LOGGER.debug("File contents: " + json);
179+
Files.write(path, (json + "\n").getBytes(StandardCharsets.UTF_8));
180+
}
181+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<configuration>
2+
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
6+
</encoder>
7+
</appender>
8+
9+
<root level="INFO">
10+
<appender-ref ref="STDOUT"/>
11+
</root>
12+
</configuration>

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ include(":driver-scala")
4343

4444
include(":driver-benchmarks")
4545
include(":driver-lambda")
46+
include(":driver-workload-executor")
4647
if (providers.gradleProperty("includeGraalvm").isPresent) {
4748
include(":graalvm-native-image-app")
4849
}

0 commit comments

Comments
 (0)