|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 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 | +#include "velox/tool/trace/TraceReplayerConfigPrompt.h" |
| 18 | + |
| 19 | +#include <gflags/gflags.h> |
| 20 | +#include <iostream> |
| 21 | +#include <sstream> |
| 22 | +#include <string> |
| 23 | + |
| 24 | +DEFINE_bool( |
| 25 | + fast, |
| 26 | + false, |
| 27 | + "Skip interactive prompts and use existing gflag values directly. " |
| 28 | + "Use this flag when you already know the config values and want to " |
| 29 | + "bypass the interactive configuration."); |
| 30 | + |
| 31 | +DECLARE_string(root_dir); |
| 32 | +DECLARE_string(query_id); |
| 33 | +DECLARE_string(task_id); |
| 34 | +DECLARE_string(node_id); |
| 35 | +DECLARE_string(driver_ids); |
| 36 | +DECLARE_string(table_writer_output_dir); |
| 37 | + |
| 38 | +namespace facebook::velox::tool::trace { |
| 39 | + |
| 40 | +namespace { |
| 41 | +constexpr const char* kColorReset = "\033[0m"; |
| 42 | +constexpr const char* kColorBold = "\033[1m"; |
| 43 | +constexpr const char* kColorGreen = "\033[32m"; |
| 44 | +constexpr const char* kColorYellow = "\033[33m"; |
| 45 | +constexpr const char* kColorCyan = "\033[36m"; |
| 46 | + |
| 47 | +// Reads a line from stdin, trimming leading/trailing whitespace. |
| 48 | +std::string readLine() { |
| 49 | + std::string input; |
| 50 | + std::getline(std::cin, input); |
| 51 | + |
| 52 | + // Trim leading whitespace |
| 53 | + size_t start = input.find_first_not_of(" \t\n\r"); |
| 54 | + if (start == std::string::npos) { |
| 55 | + return ""; |
| 56 | + } |
| 57 | + |
| 58 | + // Trim trailing whitespace |
| 59 | + size_t end = input.find_last_not_of(" \t\n\r"); |
| 60 | + return input.substr(start, end - start + 1); |
| 61 | +} |
| 62 | + |
| 63 | +// Prints a separator line for visual clarity. |
| 64 | +void printSeparator() { |
| 65 | + std::cout << kColorCyan |
| 66 | + << "============================================================" |
| 67 | + << kColorReset << std::endl; |
| 68 | +} |
| 69 | + |
| 70 | +// Prints a header for the config prompt session. |
| 71 | +void printHeader() { |
| 72 | + std::cout << std::endl; |
| 73 | + printSeparator(); |
| 74 | + std::cout << kColorBold << kColorGreen |
| 75 | + << " Trace Replayer Configuration Prompt" << kColorReset |
| 76 | + << std::endl; |
| 77 | + printSeparator(); |
| 78 | + std::cout << std::endl; |
| 79 | + std::cout << "Enter config values or press " << kColorYellow << "Enter" |
| 80 | + << kColorReset << " to keep current gflag value." << std::endl; |
| 81 | + std::cout << std::endl; |
| 82 | +} |
| 83 | + |
| 84 | +// Prints the equivalent command with --fast flag for future use. |
| 85 | +void printFastCommand() { |
| 86 | + std::ostringstream cmd; |
| 87 | + cmd << "--fast"; |
| 88 | + |
| 89 | + if (!FLAGS_root_dir.empty()) { |
| 90 | + cmd << " \\\n --root_dir=\"" << FLAGS_root_dir << "\""; |
| 91 | + } |
| 92 | + if (!FLAGS_query_id.empty()) { |
| 93 | + cmd << " \\\n --query_id=\"" << FLAGS_query_id << "\""; |
| 94 | + } |
| 95 | + if (!FLAGS_task_id.empty()) { |
| 96 | + cmd << " \\\n --task_id=\"" << FLAGS_task_id << "\""; |
| 97 | + } |
| 98 | + if (!FLAGS_node_id.empty()) { |
| 99 | + cmd << " \\\n --node_id=\"" << FLAGS_node_id << "\""; |
| 100 | + } |
| 101 | + if (!FLAGS_driver_ids.empty()) { |
| 102 | + cmd << " \\\n --driver_ids=\"" << FLAGS_driver_ids << "\""; |
| 103 | + } |
| 104 | + if (!FLAGS_table_writer_output_dir.empty()) { |
| 105 | + cmd << " \\\n --table_writer_output_dir=\"" |
| 106 | + << FLAGS_table_writer_output_dir << "\""; |
| 107 | + } |
| 108 | + |
| 109 | + std::cout << std::endl; |
| 110 | + printSeparator(); |
| 111 | + std::cout << kColorBold << kColorGreen << "Quick Command for Next Run" |
| 112 | + << kColorReset << std::endl; |
| 113 | + printSeparator(); |
| 114 | + std::cout << std::endl; |
| 115 | + std::cout |
| 116 | + << "To skip prompts next time, copy and run with the following flags:\n" |
| 117 | + << std::endl; |
| 118 | + std::cout << kColorYellow << cmd.str() << kColorReset << std::endl; |
| 119 | + std::cout << std::endl; |
| 120 | + printSeparator(); |
| 121 | + std::cout << std::endl; |
| 122 | +} |
| 123 | + |
| 124 | +// Prompts for a single config value. |
| 125 | +// @param configName The name of the config (for display purposes). |
| 126 | +// @param currentValue The current value from gflag. |
| 127 | +// @param description A brief description of what this config is for. |
| 128 | +// @param required Whether this config is required (cannot be empty). |
| 129 | +// @return The final value (either user input or the original gflag value). |
| 130 | +std::string promptForConfig( |
| 131 | + const std::string& configName, |
| 132 | + const std::string& currentValue, |
| 133 | + const std::string& description, |
| 134 | + bool required) { |
| 135 | + std::cout << kColorBold << configName << kColorReset << std::endl; |
| 136 | + std::cout << " " << description << std::endl; |
| 137 | + std::cout << " Current gflag value: " << kColorYellow |
| 138 | + << (currentValue.empty() ? "(empty)" : currentValue) << kColorReset |
| 139 | + << std::endl; |
| 140 | + |
| 141 | + if (required) { |
| 142 | + std::cout << " " << kColorCyan << "[REQUIRED]" << kColorReset << std::endl; |
| 143 | + } |
| 144 | + |
| 145 | + std::cout << " Enter new value: "; |
| 146 | + std::string input = readLine(); |
| 147 | + |
| 148 | + std::string finalValue = input.empty() ? currentValue : input; |
| 149 | + |
| 150 | + if (required && finalValue.empty()) { |
| 151 | + std::cout << kColorYellow << " Warning: This config is required but empty." |
| 152 | + << kColorReset << std::endl; |
| 153 | + } |
| 154 | + |
| 155 | + std::cout << " Using: " << kColorGreen << finalValue << kColorReset |
| 156 | + << std::endl; |
| 157 | + std::cout << std::endl; |
| 158 | + |
| 159 | + return finalValue; |
| 160 | +} |
| 161 | +} // namespace |
| 162 | + |
| 163 | +void TraceReplayerConfigPrompt::run() { |
| 164 | + if (FLAGS_fast) { |
| 165 | + std::cout << kColorCyan << "[--fast mode] " |
| 166 | + << "Skipping interactive prompts, using existing gflag values." |
| 167 | + << kColorReset << std::endl; |
| 168 | + std::cout << std::endl; |
| 169 | + |
| 170 | + // Print summary of current config values |
| 171 | + std::cout << kColorBold << "Current Configuration:" << kColorReset |
| 172 | + << std::endl; |
| 173 | + std::cout << " root_dir: " << FLAGS_root_dir << std::endl; |
| 174 | + std::cout << " query_id: " << FLAGS_query_id << std::endl; |
| 175 | + std::cout << " task_id: " |
| 176 | + << (FLAGS_task_id.empty() ? "(empty - summary mode)" |
| 177 | + : FLAGS_task_id) |
| 178 | + << std::endl; |
| 179 | + std::cout << " node_id: " << FLAGS_node_id << std::endl; |
| 180 | + std::cout << " driver_ids: " |
| 181 | + << (FLAGS_driver_ids.empty() ? "(all drivers)" : FLAGS_driver_ids) |
| 182 | + << std::endl; |
| 183 | + std::cout << " table_writer_output_dir: " |
| 184 | + << (FLAGS_table_writer_output_dir.empty() |
| 185 | + ? "(not set)" |
| 186 | + : FLAGS_table_writer_output_dir) |
| 187 | + << std::endl; |
| 188 | + std::cout << std::endl; |
| 189 | + return; |
| 190 | + } |
| 191 | + |
| 192 | + printHeader(); |
| 193 | + |
| 194 | + // Prompt for critical required configs |
| 195 | + FLAGS_root_dir = promptForConfig( |
| 196 | + "--root_dir", |
| 197 | + FLAGS_root_dir, |
| 198 | + "Root directory where the replayer reads traced data", |
| 199 | + true); |
| 200 | + |
| 201 | + FLAGS_query_id = promptForConfig( |
| 202 | + "--query_id", FLAGS_query_id, "The target query id to replay", true); |
| 203 | + |
| 204 | + FLAGS_task_id = promptForConfig( |
| 205 | + "--task_id", |
| 206 | + FLAGS_task_id, |
| 207 | + "The target task id to replay (leave empty for summary mode)", |
| 208 | + false); |
| 209 | + |
| 210 | + FLAGS_node_id = promptForConfig( |
| 211 | + "--node_id", FLAGS_node_id, "The target node id to replay", true); |
| 212 | + |
| 213 | + // Prompt for optional configs |
| 214 | + std::cout << kColorBold << "Optional Configs:" << kColorReset << std::endl; |
| 215 | + std::cout << std::endl; |
| 216 | + |
| 217 | + FLAGS_driver_ids = promptForConfig( |
| 218 | + "--driver_ids", |
| 219 | + FLAGS_driver_ids, |
| 220 | + "A comma-separated list of target driver ids", |
| 221 | + false); |
| 222 | + |
| 223 | + FLAGS_table_writer_output_dir = promptForConfig( |
| 224 | + "--table_writer_output_dir", |
| 225 | + FLAGS_table_writer_output_dir, |
| 226 | + "Output directory for TableWriter (required for TableWrite replays)", |
| 227 | + false); |
| 228 | + |
| 229 | + printSeparator(); |
| 230 | + std::cout << kColorGreen << "Configuration complete!" << kColorReset |
| 231 | + << std::endl; |
| 232 | + std::cout << std::endl; |
| 233 | + |
| 234 | + // Print summary |
| 235 | + std::cout << kColorBold << "Final Configuration Summary:" << kColorReset |
| 236 | + << std::endl; |
| 237 | + std::cout << " root_dir: " << FLAGS_root_dir << std::endl; |
| 238 | + std::cout << " query_id: " << FLAGS_query_id << std::endl; |
| 239 | + std::cout << " task_id: " |
| 240 | + << (FLAGS_task_id.empty() ? "(empty - summary mode)" |
| 241 | + : FLAGS_task_id) |
| 242 | + << std::endl; |
| 243 | + std::cout << " node_id: " << FLAGS_node_id << std::endl; |
| 244 | + std::cout << " driver_ids: " |
| 245 | + << (FLAGS_driver_ids.empty() ? "(all drivers)" : FLAGS_driver_ids) |
| 246 | + << std::endl; |
| 247 | + std::cout << " table_writer_output_dir: " |
| 248 | + << (FLAGS_table_writer_output_dir.empty() |
| 249 | + ? "(not set)" |
| 250 | + : FLAGS_table_writer_output_dir) |
| 251 | + << std::endl; |
| 252 | + |
| 253 | + // Print the fast command for next time |
| 254 | + printFastCommand(); |
| 255 | +} |
| 256 | + |
| 257 | +} // namespace facebook::velox::tool::trace |
0 commit comments