|
| 1 | +package utils.dbutils; |
| 2 | + |
| 3 | +import com.google.gson.*; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileOutputStream; |
| 7 | +import java.io.PrintStream; |
| 8 | +import java.io.Reader; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Paths; |
| 11 | + |
| 12 | +public class Config { |
| 13 | + public static final int EXTRACT=1; |
| 14 | + public static final int APPLY=2; |
| 15 | + |
| 16 | + public static final int LOG_LEVEL_SUMMARY = 0; |
| 17 | + public static final int LOG_LEVEL_ERRORS = 1; |
| 18 | + public static final int LOG_LEVEL_ALL = 2; |
| 19 | + |
| 20 | + public static int runningMode; |
| 21 | + public static String configDir; |
| 22 | + public static String inputFileName; |
| 23 | + public static String configFileName; |
| 24 | + public static PrintStream logFile; |
| 25 | + public static String logFileName; |
| 26 | + public static int logLevel = 0; |
| 27 | + public static String shutdownFileName; |
| 28 | + public static String outputDir; |
| 29 | + public static JsonArray includeDbs; |
| 30 | + public static JsonArray excludeDbs; |
| 31 | + public static JsonArray includeCmds; |
| 32 | + public static JsonArray excludeCmds; |
| 33 | + public static boolean commandsLogging; |
| 34 | + public static String connectString; |
| 35 | + public static String dbName; |
| 36 | + public static int outputMode = 0; // 0 - mongosh script, default |
| 37 | + // 1 - json |
| 38 | + public static int inputFileFormat = 0; // 0 - Momngod log |
| 39 | + // 1 - db.system.profile collection dump |
| 40 | + // 2 - db.system.profile direct reads |
| 41 | + |
| 42 | + public static int executionTracing = 0; // 0 - not tracing |
| 43 | + // 1 - query planner |
| 44 | + // 2 - execution stats |
| 45 | + // 3 - allPlansExecution |
| 46 | + public static ManagingThread t = new ManagingThread(); |
| 47 | + private static Reader reader; |
| 48 | + private static JsonObject configObject; |
| 49 | + |
| 50 | + public static boolean traceAllDbs() { |
| 51 | + return (includeDbs == null && excludeDbs == null); |
| 52 | + } |
| 53 | + |
| 54 | + public static boolean traceInclDbs() { |
| 55 | + return (includeDbs != null); |
| 56 | + } |
| 57 | + |
| 58 | + public static boolean traceExclDbs() { |
| 59 | + return (excludeDbs != null); |
| 60 | + } |
| 61 | + |
| 62 | + public static boolean traceInclCmds() { |
| 63 | + return (includeCmds != null); |
| 64 | + } |
| 65 | + |
| 66 | + public static boolean traceExclCmds() { |
| 67 | + return (excludeCmds != null); |
| 68 | + } |
| 69 | + |
| 70 | + public static boolean traceAllCmds() { |
| 71 | + return (includeCmds == null && excludeCmds == null); |
| 72 | + } |
| 73 | + |
| 74 | + public static boolean traceDatabase(String dbName) { |
| 75 | + boolean contains = false; |
| 76 | + if ( includeDbs == null && excludeDbs == null ) |
| 77 | + contains = true; |
| 78 | + else if ( excludeDbs == null ) |
| 79 | + contains = includeDbs.contains(new JsonParser().parse(dbName)); |
| 80 | + else contains = !excludeDbs.contains(new JsonParser().parse(dbName)); |
| 81 | + return contains; |
| 82 | + } |
| 83 | + |
| 84 | + public static boolean traceCommand(JsonObject command) { |
| 85 | + boolean contains = false; |
| 86 | + int i = 0; |
| 87 | + if (includeCmds == null && excludeCmds == null) |
| 88 | + contains = true; |
| 89 | + else if (excludeCmds == null) |
| 90 | + while (!contains && i < includeCmds.asList().toArray().length) { |
| 91 | + if (command.has(includeCmds.asList().get(i).getAsString())) |
| 92 | + contains = true; |
| 93 | + i++; |
| 94 | + } |
| 95 | + else if (includeCmds == null) { |
| 96 | + contains = true; |
| 97 | + while (contains && i < excludeCmds.asList().toArray().length) { |
| 98 | + if (command.has(excludeCmds.asList().get(i).getAsString())) |
| 99 | + contains = false; |
| 100 | + i++; |
| 101 | + } |
| 102 | + } |
| 103 | + return contains; |
| 104 | + } |
| 105 | + |
| 106 | + public static void readExtractConfiguration() { |
| 107 | + try { |
| 108 | + |
| 109 | + if (configObject.has("INPUT_FILE")) |
| 110 | + inputFileName = configObject.getAsJsonPrimitive("INPUT_FILE").getAsString(); |
| 111 | + else |
| 112 | + t.start(); |
| 113 | + |
| 114 | + if (configObject.has("OUTPUT_DIR")) |
| 115 | + outputDir = configObject.getAsJsonPrimitive("OUTPUT_DIR").getAsString(); |
| 116 | + |
| 117 | + if (configObject.has("INCLUDE_COMMANDS") && |
| 118 | + configObject.has("EXCLUDE_COMMANDS")) |
| 119 | + throw new Exception("You cannot set INCLUDE_COMMANDS and EXCLUDE_COMMANDS at the same time. Please, review the available documentation."); |
| 120 | + |
| 121 | + if (configObject.has("INCLUDE_DATABASES")) |
| 122 | + includeDbs = configObject.getAsJsonArray("INCLUDE_DATABASES"); |
| 123 | + |
| 124 | + if (configObject.has("EXCLUDE_DATABASES")) |
| 125 | + excludeDbs = configObject.getAsJsonArray("EXCLUDE_DATABASES"); |
| 126 | + |
| 127 | + if (configObject.has("INCLUDE_COMMANDS")) |
| 128 | + includeCmds = configObject.getAsJsonArray("INCLUDE_COMMANDS"); |
| 129 | + |
| 130 | + if (configObject.has("EXCLUDE_COMMANDS")) |
| 131 | + excludeCmds = configObject.getAsJsonArray("EXCLUDE_COMMANDS"); |
| 132 | + |
| 133 | + if (configObject.has("COMMANDS_LOGGING")) |
| 134 | + commandsLogging = configObject.getAsJsonPrimitive("COMMANDS_LOGGING").getAsBoolean(); |
| 135 | + else |
| 136 | + commandsLogging = true; |
| 137 | + |
| 138 | + if (configObject.has("EXECUTION_PLAN_TRACING")) |
| 139 | + executionTracing = configObject.getAsJsonPrimitive("EXECUTION_PLAN_TRACING").getAsInt(); |
| 140 | + |
| 141 | + if (configObject.has("INPUT_FILE_FORMAT")) { |
| 142 | + if ( configObject.getAsJsonPrimitive("INPUT_FILE_FORMAT").getAsString().equals("MONGO_LOG")) |
| 143 | + inputFileFormat = 0; |
| 144 | + else if ( configObject.getAsJsonPrimitive("INPUT_FILE_FORMAT").getAsString().equals("MONGO_PROFILE")) |
| 145 | + inputFileFormat = 1; |
| 146 | + else throw new Exception("INPUT_FILE_FORMAT can be set to MONGO_LOG or MONGO_PROFILE"); |
| 147 | + } |
| 148 | + else inputFileFormat = 0; |
| 149 | + |
| 150 | + if (configObject.has("INPUT_CONNECT_STRING")) { |
| 151 | + if (configObject.has("INPUT_FILE_FORMAT") || configObject.has("INPUT_FILE")) |
| 152 | + throw new Exception("If INPUT_CONNECT_STRING is set then INPUT_FILE and INPUT_FILE_FORMAT cannot be set"); |
| 153 | + |
| 154 | + if (configObject.has("DB_NAME")) |
| 155 | + dbName = configObject.getAsJsonPrimitive("DB_NAME").getAsString(); |
| 156 | + else throw new Exception("DB_NAME parameter is mandatory when INPUT_CONNECT_STRING is set."); |
| 157 | + |
| 158 | + connectString = configObject.get("INPUT_CONNECT_STRING").getAsString(); |
| 159 | + inputFileFormat = 2; |
| 160 | + } |
| 161 | + |
| 162 | + if (configObject.has("OUTPUT_MODE")) { |
| 163 | + if ( configObject.getAsJsonPrimitive("OUTPUT_MODE").getAsString().equals("SCRIPT")) |
| 164 | + outputMode = 0; |
| 165 | + else if (configObject.getAsJsonPrimitive("OUTPUT_MODE").getAsString().equals("JSON")) |
| 166 | + outputMode = 1; |
| 167 | + else throw new Exception("OUTPUT_MODE can be set to SCRIPT or JSON values only"); |
| 168 | + } |
| 169 | + else |
| 170 | + outputMode = 0; |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + } catch (Exception e) { |
| 175 | + e.printStackTrace(); |
| 176 | + System.exit(0); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + public static void readApplyConfiguration() { |
| 181 | + try { |
| 182 | + if (configObject.has("CONNECT_STRING")) |
| 183 | + connectString = configObject.getAsJsonPrimitive("CONNECT_STRING").getAsString(); |
| 184 | + else throw new Exception("CONNECT_STRING parameter is mandatory."); |
| 185 | + |
| 186 | + |
| 187 | + if (configObject.has("DB_NAME")) |
| 188 | + dbName = configObject.getAsJsonPrimitive("DB_NAME").getAsString(); |
| 189 | + else throw new Exception("DB_NAME parameter is mandatory."); |
| 190 | + |
| 191 | + if (configObject.has("INPUT_FILE")) |
| 192 | + inputFileName = configObject.getAsJsonPrimitive("INPUT_FILE").getAsString(); |
| 193 | + else |
| 194 | + t.start(); |
| 195 | + } catch (Exception e) { |
| 196 | + e.printStackTrace(); |
| 197 | + System.exit(0); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + public static void readConfiguration(int mode) { |
| 202 | + try { |
| 203 | + runningMode = mode; |
| 204 | + configDir = System.getenv("MR_CONFIG_DIR"); |
| 205 | + if (configDir == null || configDir.length() == 0) |
| 206 | + throw new Exception("MR_CONFIG_DIR environment variable is mandatory. Please review the available documentation."); |
| 207 | + |
| 208 | + if (mode == EXTRACT) { |
| 209 | + configFileName = configDir + File.separator + "mdbecfg.json"; |
| 210 | + shutdownFileName = configDir + File.separator + "mdbes.label"; |
| 211 | + } |
| 212 | + else { |
| 213 | + configFileName = configDir + File.separator + "mdbacfg.json"; |
| 214 | + shutdownFileName = configDir + File.separator + "mdbas.label"; |
| 215 | + } |
| 216 | + |
| 217 | + reader = Files.newBufferedReader(Paths.get(configFileName)); |
| 218 | + configObject = (new Gson()).fromJson(reader, JsonObject.class); |
| 219 | + |
| 220 | + if (configObject.has("LOG_FILE")) { |
| 221 | + logFileName = configObject.getAsJsonPrimitive("LOG_FILE").getAsString(); |
| 222 | + logFile = new PrintStream(new FileOutputStream(Config.logFileName), true); |
| 223 | + } |
| 224 | + else |
| 225 | + logFile = System.err; |
| 226 | + |
| 227 | + if (configObject.has("LOG_LEVEL")) |
| 228 | + logLevel = configObject.getAsJsonPrimitive("LOG_LEVEL").getAsInt(); |
| 229 | + |
| 230 | + if (mode == EXTRACT) |
| 231 | + readExtractConfiguration(); |
| 232 | + else |
| 233 | + readApplyConfiguration(); |
| 234 | + |
| 235 | + reader.close(); |
| 236 | + } catch (Exception e) { |
| 237 | + e.printStackTrace(); |
| 238 | + System.exit(0); |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + public static void logMessage(String message, int level) { |
| 243 | + if (level <= logLevel) |
| 244 | + logFile.println(message); |
| 245 | + } |
| 246 | +} |
0 commit comments