|
| 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/TraceFileToolRunner.h" |
| 18 | +#include "velox/common/file/FileSystems.h" |
| 19 | + |
| 20 | +DEFINE_string(trace_file_op, "copy", "Operation type of this run"); |
| 21 | +DEFINE_string( |
| 22 | + source_root_dir, |
| 23 | + "", |
| 24 | + "Source root directory of the tracing data, it must be set"); |
| 25 | +DEFINE_string( |
| 26 | + dest_root_dir, |
| 27 | + "", |
| 28 | + "Dest root directory, it must be set if the operation type is copy"); |
| 29 | +DEFINE_string(trace_query_id, "", "Specify the trace query id"); |
| 30 | +DEFINE_string(trace_task_id, "", "Specify the trace task id"); |
| 31 | + |
| 32 | +namespace facebook::velox::tool::trace { |
| 33 | + |
| 34 | +TraceFileToolRunner::TraceFileToolRunner() |
| 35 | + : sourceRootDir_(FLAGS_source_root_dir), destRootDir_(FLAGS_dest_root_dir) { |
| 36 | + VELOX_USER_CHECK(!sourceRootDir_.empty()); |
| 37 | +} |
| 38 | + |
| 39 | +void TraceFileToolRunner::init() { |
| 40 | + filesystems::registerLocalFileSystem(); |
| 41 | + sourceFs_ = filesystems::getFileSystem(sourceRootDir_, nullptr); |
| 42 | + VELOX_CHECK_NOT_NULL(sourceFs_); |
| 43 | + if (FLAGS_trace_file_op == "copy") { |
| 44 | + VELOX_USER_CHECK(!destRootDir_.empty()); |
| 45 | + destFs_ = filesystems::getFileSystem(destRootDir_, nullptr); |
| 46 | + VELOX_CHECK_NOT_NULL(destFs_); |
| 47 | + std::string copyRootDir; |
| 48 | + if (FLAGS_trace_query_id.empty()) { |
| 49 | + VELOX_CHECK( |
| 50 | + FLAGS_trace_task_id.empty(), |
| 51 | + "Trace query ID is empty but trace task ID is not empty"); |
| 52 | + copyRootDir = sourceRootDir_; |
| 53 | + } else if (FLAGS_trace_task_id.empty()) { |
| 54 | + copyRootDir = fmt::format("{}/{}", sourceRootDir_, FLAGS_trace_query_id); |
| 55 | + } else { |
| 56 | + copyRootDir = fmt::format( |
| 57 | + "{}/{}/{}", |
| 58 | + sourceRootDir_, |
| 59 | + FLAGS_trace_query_id, |
| 60 | + FLAGS_trace_task_id); |
| 61 | + } |
| 62 | + listFiles(copyRootDir); |
| 63 | + } else { |
| 64 | + VELOX_UNSUPPORTED( |
| 65 | + "Unsupported trace file operation type {}", FLAGS_trace_file_op); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +void TraceFileToolRunner::run() { |
| 70 | + if (FLAGS_trace_file_op == "copy") { |
| 71 | + copyFiles(); |
| 72 | + } else { |
| 73 | + VELOX_UNSUPPORTED( |
| 74 | + "Unsupported trace file operation type {}", FLAGS_trace_file_op); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +void TraceFileToolRunner::copyFiles() const { |
| 79 | + const auto prefixLen = sourceFs_->extractPath(sourceRootDir_).length(); |
| 80 | + for (const auto& source : sourceFiles_) { |
| 81 | + const auto targetFile = |
| 82 | + fmt::format("{}/{}", destRootDir_, source.substr(prefixLen)); |
| 83 | + const auto readFile = sourceFs_->openFileForRead(source); |
| 84 | + const auto writeFile = destFs_->openFileForWrite( |
| 85 | + targetFile, |
| 86 | + filesystems::FileOptions{ |
| 87 | + .values = {}, |
| 88 | + .fileSize = std::nullopt, |
| 89 | + .shouldCreateParentDirectories = true}); |
| 90 | + const auto fileSize = readFile->size(); |
| 91 | + constexpr auto batchSize = 4 << 10; |
| 92 | + const auto ioBuf = folly::IOBuf::create(batchSize); |
| 93 | + uint64_t offset = 0; |
| 94 | + while (offset < fileSize) { |
| 95 | + const auto curLen = std::min<uint64_t>(fileSize - offset, batchSize); |
| 96 | + const auto dataView = |
| 97 | + readFile->pread(offset, curLen, ioBuf->writableData()); |
| 98 | + writeFile->append(dataView); |
| 99 | + ioBuf->append(curLen); |
| 100 | + offset += curLen; |
| 101 | + ioBuf->clear(); |
| 102 | + } |
| 103 | + writeFile->flush(); |
| 104 | + writeFile->close(); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +void TraceFileToolRunner::listFiles(const std::string& path) { |
| 109 | + VELOX_USER_CHECK(sourceFs_->exists(path), "{} dose not exist", path); |
| 110 | + if (!sourceFs_->isDirectory(path)) { |
| 111 | + sourceFiles_.push_back(path); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + for (const auto& p : sourceFs_->list(sourceFs_->extractPath(path))) { |
| 116 | + listFiles(p); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace facebook::velox::tool::trace |
0 commit comments