|
| 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 | +} |
0 commit comments