Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 1 | #include "aos/events/logging/log_backend.h" |
| 2 | |
| 3 | #include <dirent.h> |
| 4 | |
| 5 | #include <filesystem> |
| 6 | |
Alexei Strots | f08b8fb | 2023-04-21 19:46:08 -0700 | [diff] [blame] | 7 | #include "absl/strings/match.h" |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 8 | #include "absl/strings/str_cat.h" |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 9 | #include "glog/logging.h" |
| 10 | |
Alexei Strots | f08b8fb | 2023-04-21 19:46:08 -0700 | [diff] [blame] | 11 | #include "aos/events/logging/file_operations.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 12 | #include "aos/util/file.h" |
| 13 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 14 | DEFINE_bool(direct, false, |
| 15 | "If true, write using O_DIRECT and write 512 byte aligned blocks " |
| 16 | "whenever possible."); |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 17 | DEFINE_bool( |
| 18 | sync, false, |
| 19 | "If true, sync data to disk as we go so we don't get too far ahead. Also " |
| 20 | "fadvise that we are done with the memory once it hits disk."); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 21 | |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 22 | DEFINE_uint32(queue_reserve, 32, "Pre-reserved size of write queue."); |
| 23 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 24 | namespace aos::logger { |
| 25 | namespace { |
| 26 | constexpr const char *kTempExtension = ".tmp"; |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 27 | |
| 28 | // Assuming that kSector is power of 2, it aligns address to the left size. |
| 29 | inline size_t AlignToLeft(size_t value) { |
| 30 | return value & (~(FileHandler::kSector - 1)); |
| 31 | } |
| 32 | |
| 33 | inline bool IsAligned(size_t value) { |
| 34 | return value % FileHandler::kSector == 0; |
| 35 | } |
| 36 | |
| 37 | inline bool IsAlignedStart(const absl::Span<const uint8_t> span) { |
| 38 | return (reinterpret_cast<size_t>(span.data()) % FileHandler::kSector) == 0; |
| 39 | } |
| 40 | |
| 41 | inline bool IsAlignedLength(const absl::Span<const uint8_t> span) { |
| 42 | return (span.size() % FileHandler::kSector) == 0; |
| 43 | } |
| 44 | |
| 45 | } // namespace |
| 46 | |
| 47 | logger::QueueAligner::QueueAligner() { |
| 48 | aligned_queue_.reserve(FLAGS_queue_reserve); |
| 49 | } |
| 50 | |
| 51 | void logger::QueueAligner::FillAlignedQueue( |
| 52 | const absl::Span<const absl::Span<const uint8_t>> &queue) { |
| 53 | aligned_queue_.clear(); |
| 54 | |
Austin Schuh | 790ec9c | 2023-05-03 13:47:15 -0700 | [diff] [blame] | 55 | size_t queue_index = 0; |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 56 | for (const auto &span : queue) { |
Austin Schuh | 790ec9c | 2023-05-03 13:47:15 -0700 | [diff] [blame] | 57 | ++queue_index; |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 58 | // Generally, every span might have 3 optional parts (i.e. 2^3 cases): |
| 59 | // 1. unaligned prefix - from start till first aligned block. |
| 60 | // 2. aligned main - block with aligned start and size |
| 61 | // 3. unaligned suffix - block with aligned start, and size less than one |
| 62 | // sector. If size of the span is less than 1 sector, let's call it prefix. |
| 63 | |
| 64 | auto *data = span.data(); |
| 65 | size_t size = span.size(); |
| 66 | const auto start = reinterpret_cast<size_t>(data); |
| 67 | VLOG(2) << "Consider span starting at " << std::hex << start |
| 68 | << " with size " << size; |
| 69 | |
Austin Schuh | 790ec9c | 2023-05-03 13:47:15 -0700 | [diff] [blame] | 70 | CHECK_GT(size, 0u) |
| 71 | << ": Nobody should be sending empty messages. Queue index " |
| 72 | << (queue_index - 1) << " out of " << queue.size(); |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 73 | |
| 74 | const auto next_aligned = |
| 75 | IsAligned(start) ? start : AlignToLeft(start) + FileHandler::kSector; |
| 76 | const auto prefix_size = next_aligned - start; |
| 77 | VLOG(2) << "Calculated prefix size " << std::hex << prefix_size; |
| 78 | |
| 79 | if (prefix_size >= size) { |
| 80 | // size of prefix >= size of span - alignment is not possible, accept the |
| 81 | // whole span |
| 82 | VLOG(2) << "Only prefix found"; |
| 83 | CHECK_GT(size, 0u); |
| 84 | aligned_queue_.emplace_back(data, size, false); |
| 85 | continue; |
| 86 | } |
| 87 | CHECK_LT(prefix_size, FileHandler::kSector) |
| 88 | << ": Wrong calculation of 'next' aligned position"; |
| 89 | if (prefix_size > 0) { |
| 90 | // Cut the prefix and move to the main part. |
| 91 | VLOG(2) << "Cutting prefix at " << std::hex << start << " of size " |
| 92 | << prefix_size; |
| 93 | aligned_queue_.emplace_back(data, prefix_size, false); |
| 94 | data += prefix_size; |
| 95 | size -= prefix_size; |
| 96 | CHECK(data <= span.data() + span.size()) << " :Boundaries after prefix"; |
| 97 | } |
| 98 | |
| 99 | if (IsAligned(size)) { |
| 100 | // the rest is aligned. |
| 101 | VLOG(2) << "Returning aligned main part"; |
| 102 | CHECK_GT(size, 0u); |
| 103 | aligned_queue_.emplace_back(data, size, true); |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | const auto aligned_size = AlignToLeft(size); |
| 108 | CHECK(aligned_size < size) << ": Wrong calculation of 'main' size"; |
| 109 | if (aligned_size > 0) { |
| 110 | VLOG(2) << "Cutting main part starting " << std::hex |
| 111 | << reinterpret_cast<size_t>(data) << " of size " << aligned_size; |
| 112 | aligned_queue_.emplace_back(data, aligned_size, true); |
| 113 | |
| 114 | data += aligned_size; |
| 115 | size -= aligned_size; |
| 116 | CHECK(data <= span.data() + span.size()) << " :Boundaries after main"; |
| 117 | } |
| 118 | |
| 119 | VLOG(2) << "Cutting suffix part starting " << std::hex |
| 120 | << reinterpret_cast<size_t>(data) << " of size " << size; |
| 121 | CHECK_GT(size, 0u); |
| 122 | aligned_queue_.emplace_back(data, size, false); |
| 123 | } |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | FileHandler::FileHandler(std::string filename) |
| 127 | : filename_(std::move(filename)), supports_odirect_(FLAGS_direct) {} |
| 128 | |
| 129 | FileHandler::~FileHandler() { Close(); } |
| 130 | |
| 131 | WriteCode FileHandler::OpenForWrite() { |
| 132 | iovec_.reserve(10); |
| 133 | if (!aos::util::MkdirPIfSpace(filename_, 0777)) { |
| 134 | return WriteCode::kOutOfSpace; |
| 135 | } else { |
| 136 | fd_ = open(filename_.c_str(), O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 0774); |
| 137 | if (fd_ == -1 && errno == ENOSPC) { |
| 138 | return WriteCode::kOutOfSpace; |
| 139 | } else { |
| 140 | PCHECK(fd_ != -1) << ": Failed to open " << filename_ << " for writing"; |
| 141 | VLOG(1) << "Opened " << filename_ << " for writing"; |
| 142 | } |
| 143 | |
| 144 | flags_ = fcntl(fd_, F_GETFL, 0); |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 145 | PCHECK(flags_ >= 0) << ": Failed to get flags for " << filename_; |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 146 | |
| 147 | EnableDirect(); |
| 148 | |
| 149 | CHECK(std::filesystem::exists(filename_)); |
| 150 | |
| 151 | return WriteCode::kOk; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void FileHandler::EnableDirect() { |
| 156 | if (supports_odirect_ && !ODirectEnabled()) { |
| 157 | const int new_flags = flags_ | O_DIRECT; |
| 158 | // Track if we failed to set O_DIRECT. Note: Austin hasn't seen this call |
| 159 | // fail. The write call tends to fail instead. |
| 160 | if (fcntl(fd_, F_SETFL, new_flags) == -1) { |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 161 | PLOG(WARNING) << "Failed to set O_DIRECT on " << filename_; |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 162 | supports_odirect_ = false; |
| 163 | } else { |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 164 | VLOG(1) << "Enabled O_DIRECT on " << filename_; |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 165 | flags_ = new_flags; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void FileHandler::DisableDirect() { |
| 171 | if (supports_odirect_ && ODirectEnabled()) { |
| 172 | flags_ = flags_ & (~O_DIRECT); |
| 173 | PCHECK(fcntl(fd_, F_SETFL, flags_) != -1) << ": Failed to disable O_DIRECT"; |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 174 | VLOG(1) << "Disabled O_DIRECT on " << filename_; |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | WriteResult FileHandler::Write( |
| 179 | const absl::Span<const absl::Span<const uint8_t>> &queue) { |
| 180 | iovec_.clear(); |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 181 | CHECK_LE(queue.size(), static_cast<size_t>(IOV_MAX)); |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 182 | |
| 183 | queue_aligner_.FillAlignedQueue(queue); |
| 184 | CHECK_LE(queue_aligner_.aligned_queue().size(), static_cast<size_t>(IOV_MAX)); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 185 | |
| 186 | // Ok, we now need to figure out if we were aligned, and if we were, how much |
| 187 | // of the data we are being asked to write is aligned. |
| 188 | // |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 189 | // When writing with O_DIRECT, the kernel only will accept writes where the |
| 190 | // offset into the file is a multiple of kSector, the data is aligned to |
| 191 | // kSector in memory, and the length being written is a multiple of kSector. |
| 192 | // Some of the callers use an aligned ResizeableBuffer to generate 512 byte |
| 193 | // aligned buffers for this code to find and use. |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 194 | bool was_aligned = IsAligned(total_write_bytes_); |
| 195 | VLOG(1) << "Started " << (was_aligned ? "aligned" : "unaligned") |
| 196 | << " at offset " << total_write_bytes_ << " on " << filename(); |
Austin Schuh | 9e8df9e | 2023-05-03 08:28:29 -0700 | [diff] [blame] | 197 | |
Alexei Strots | caf17d3 | 2023-04-03 22:31:11 -0700 | [diff] [blame] | 198 | // Walk through aligned queue and batch writes based on aligned flag |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 199 | for (const auto &item : queue_aligner_.aligned_queue()) { |
| 200 | if (was_aligned != item.aligned) { |
| 201 | // Switching aligned context. Let's flush current batch. |
| 202 | if (!iovec_.empty()) { |
| 203 | // Flush current queue if we need. |
| 204 | const auto code = WriteV(iovec_, was_aligned); |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 205 | if (code == WriteCode::kOutOfSpace) { |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 206 | // We cannot say anything about what number of messages was written |
| 207 | // for sure. |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 208 | return { |
| 209 | .code = code, |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 210 | .messages_written = queue.size(), |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 211 | }; |
| 212 | } |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 213 | iovec_.clear(); |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 214 | } |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 215 | // Write queue is flushed. WriteV updates the total_write_bytes_. |
| 216 | was_aligned = IsAligned(total_write_bytes_) && item.aligned; |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 217 | } |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 218 | iovec_.push_back( |
| 219 | {.iov_base = const_cast<uint8_t *>(item.data), .iov_len = item.size}); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 222 | WriteCode result_code = WriteCode::kOk; |
| 223 | if (!iovec_.empty()) { |
| 224 | // Flush current queue if we need. |
| 225 | result_code = WriteV(iovec_, was_aligned); |
| 226 | } |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 227 | return { |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 228 | .code = result_code, |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 229 | .messages_written = queue.size(), |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 230 | }; |
| 231 | } |
| 232 | |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 233 | WriteCode FileHandler::WriteV(const std::vector<struct iovec> &iovec, |
| 234 | bool aligned) { |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 235 | // Configure the file descriptor to match the mode we should be in. This is |
| 236 | // safe to over-call since it only does the syscall if needed. |
| 237 | if (aligned) { |
| 238 | EnableDirect(); |
| 239 | } else { |
| 240 | DisableDirect(); |
| 241 | } |
| 242 | |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 243 | VLOG(2) << "Flushing queue of " << iovec.size() << " elements, " |
| 244 | << (aligned ? "aligned" : "unaligned"); |
| 245 | |
| 246 | CHECK_GT(iovec.size(), 0u); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 247 | const auto start = aos::monotonic_clock::now(); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 248 | |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 249 | // Validation of alignment assumptions. |
Austin Schuh | 9e8df9e | 2023-05-03 08:28:29 -0700 | [diff] [blame] | 250 | if (aligned) { |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 251 | CHECK(IsAligned(total_write_bytes_)) |
Austin Schuh | 9e8df9e | 2023-05-03 08:28:29 -0700 | [diff] [blame] | 252 | << ": Failed after writing " << total_write_bytes_ |
| 253 | << " to the file, attempting aligned write with unaligned start."; |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 254 | |
| 255 | for (const auto &iovec_item : iovec) { |
Austin Schuh | 9e8df9e | 2023-05-03 08:28:29 -0700 | [diff] [blame] | 256 | absl::Span<const uint8_t> data( |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 257 | reinterpret_cast<const uint8_t *>(iovec_item.iov_base), |
| 258 | iovec_item.iov_len); |
| 259 | VLOG(2) << " iov_base " << static_cast<void *>(iovec_item.iov_base) |
| 260 | << ", iov_len " << iovec_item.iov_len; |
Austin Schuh | 9e8df9e | 2023-05-03 08:28:29 -0700 | [diff] [blame] | 261 | CHECK(IsAlignedStart(data) && IsAlignedLength(data)); |
Austin Schuh | 9e8df9e | 2023-05-03 08:28:29 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 265 | // Calculation of expected written size. |
| 266 | size_t counted_size = 0; |
| 267 | for (const auto &iovec_item : iovec) { |
| 268 | CHECK_GT(iovec_item.iov_len, 0u); |
| 269 | counted_size += iovec_item.iov_len; |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 272 | VLOG(2) << "Going to write " << counted_size; |
| 273 | CHECK_GT(counted_size, 0u); |
| 274 | |
| 275 | const ssize_t written = writev(fd_, iovec.data(), iovec.size()); |
| 276 | VLOG(2) << "Wrote " << written << ", for iovec size " << iovec.size(); |
| 277 | |
| 278 | const auto end = aos::monotonic_clock::now(); |
| 279 | if (written == -1 && errno == ENOSPC) { |
| 280 | return WriteCode::kOutOfSpace; |
| 281 | } |
| 282 | PCHECK(written >= 0) << ": write failed, got " << written; |
| 283 | if (written < static_cast<ssize_t>(counted_size)) { |
| 284 | // Sometimes this happens instead of ENOSPC. On a real filesystem, this |
| 285 | // never seems to happen in any other case. If we ever want to log to a |
| 286 | // socket, this will happen more often. However, until we get there, we'll |
| 287 | // just assume it means we ran out of space. |
| 288 | return WriteCode::kOutOfSpace; |
| 289 | } |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 290 | |
Austin Schuh | 9e8df9e | 2023-05-03 08:28:29 -0700 | [diff] [blame] | 291 | if (FLAGS_sync) { |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 292 | // Flush asynchronously and force the data out of the cache. |
| 293 | sync_file_range(fd_, total_write_bytes_, written, SYNC_FILE_RANGE_WRITE); |
| 294 | if (last_synced_bytes_ != 0) { |
| 295 | // Per Linus' recommendation online on how to do fast file IO, do a |
| 296 | // blocking flush of the previous write chunk, and then tell the kernel to |
| 297 | // drop the pages from the cache. This makes sure we can't get too far |
| 298 | // ahead. |
| 299 | sync_file_range(fd_, last_synced_bytes_, |
| 300 | total_write_bytes_ - last_synced_bytes_, |
| 301 | SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE | |
| 302 | SYNC_FILE_RANGE_WAIT_AFTER); |
| 303 | posix_fadvise(fd_, last_synced_bytes_, |
| 304 | total_write_bytes_ - last_synced_bytes_, |
| 305 | POSIX_FADV_DONTNEED); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 306 | } |
Austin Schuh | 3c4029f | 2023-04-13 12:09:55 -0700 | [diff] [blame] | 307 | last_synced_bytes_ = total_write_bytes_; |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 310 | total_write_bytes_ += written; |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 311 | if (aligned) { |
| 312 | written_aligned_ += written; |
| 313 | } |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 314 | WriteStatistics()->UpdateStats(end - start, written, iovec.size()); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 315 | return WriteCode::kOk; |
| 316 | } |
| 317 | |
| 318 | WriteCode FileHandler::Close() { |
| 319 | if (!is_open()) { |
| 320 | return WriteCode::kOk; |
| 321 | } |
| 322 | bool ran_out_of_space = false; |
| 323 | if (close(fd_) == -1) { |
| 324 | if (errno == ENOSPC) { |
| 325 | ran_out_of_space = true; |
| 326 | } else { |
| 327 | PLOG(ERROR) << "Closing log file failed"; |
| 328 | } |
| 329 | } |
| 330 | fd_ = -1; |
| 331 | VLOG(1) << "Closed " << filename_; |
| 332 | return ran_out_of_space ? WriteCode::kOutOfSpace : WriteCode::kOk; |
| 333 | } |
| 334 | |
| 335 | FileBackend::FileBackend(std::string_view base_name) |
| 336 | : base_name_(base_name), separator_(base_name_.back() == '/' ? "" : "_") {} |
| 337 | |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 338 | std::unique_ptr<LogSink> FileBackend::RequestFile(std::string_view id) { |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 339 | const std::string filename = absl::StrCat(base_name_, separator_, id); |
| 340 | return std::make_unique<FileHandler>(filename); |
| 341 | } |
| 342 | |
Alexei Strots | f08b8fb | 2023-04-21 19:46:08 -0700 | [diff] [blame] | 343 | std::vector<std::string> FileBackend::ListFiles() const { |
| 344 | std::filesystem::path directory(base_name_); |
| 345 | if (!is_directory(directory)) { |
| 346 | directory = directory.parent_path(); |
| 347 | } |
| 348 | internal::LocalFileOperations operations(directory.string()); |
| 349 | std::vector<std::string> files; |
| 350 | operations.FindLogs(&files); |
| 351 | |
| 352 | std::vector<std::string> names; |
| 353 | const std::string prefix = absl::StrCat(base_name_, separator_); |
| 354 | for (const auto &file : files) { |
| 355 | CHECK(absl::StartsWith(file, prefix)); |
| 356 | names.push_back(file.substr(prefix.size())); |
| 357 | } |
| 358 | return names; |
| 359 | } |
| 360 | |
| 361 | std::unique_ptr<DataDecoder> FileBackend::GetDecoder( |
| 362 | std::string_view id) const { |
| 363 | const std::string filename = absl::StrCat(base_name_, separator_, id); |
| 364 | CHECK(std::filesystem::exists(filename)); |
| 365 | return std::make_unique<DummyDecoder>(filename); |
| 366 | } |
| 367 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 368 | RenamableFileBackend::RenamableFileBackend(std::string_view base_name) |
| 369 | : base_name_(base_name), separator_(base_name_.back() == '/' ? "" : "_") {} |
| 370 | |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 371 | std::unique_ptr<LogSink> RenamableFileBackend::RequestFile( |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 372 | std::string_view id) { |
| 373 | const std::string filename = |
| 374 | absl::StrCat(base_name_, separator_, id, temp_suffix_); |
| 375 | return std::make_unique<RenamableFileHandler>(this, filename); |
| 376 | } |
| 377 | |
| 378 | void RenamableFileBackend::EnableTempFiles() { |
| 379 | use_temp_files_ = true; |
| 380 | temp_suffix_ = kTempExtension; |
| 381 | } |
| 382 | |
| 383 | bool RenamableFileBackend::RenameLogBase(std::string_view new_base_name) { |
| 384 | if (new_base_name == base_name_) { |
| 385 | return true; |
| 386 | } |
| 387 | CHECK(old_base_name_.empty()) |
| 388 | << "Only one change of base_name is supported. Was: " << old_base_name_; |
| 389 | |
| 390 | std::string current_directory = base_name_; |
| 391 | std::string new_directory(new_base_name); |
| 392 | |
| 393 | auto current_path_split = current_directory.rfind("/"); |
| 394 | CHECK(current_path_split != std::string::npos) |
| 395 | << "Could not find / in the current directory path"; |
| 396 | auto new_path_split = new_directory.rfind("/"); |
| 397 | CHECK(new_path_split != std::string::npos) |
| 398 | << "Could not find / in the new directory path"; |
| 399 | |
| 400 | CHECK(new_base_name.substr(new_path_split) == |
| 401 | current_directory.substr(current_path_split)) |
| 402 | << "Rename of file base from " << current_directory << " to " |
| 403 | << new_directory << " is not supported."; |
| 404 | |
| 405 | current_directory.resize(current_path_split); |
| 406 | new_directory.resize(new_path_split); |
| 407 | DIR *dir = opendir(current_directory.c_str()); |
| 408 | if (dir) { |
| 409 | closedir(dir); |
| 410 | const int result = rename(current_directory.c_str(), new_directory.c_str()); |
| 411 | if (result != 0) { |
| 412 | PLOG(ERROR) << "Unable to rename " << current_directory << " to " |
| 413 | << new_directory; |
| 414 | return false; |
| 415 | } |
| 416 | } else { |
| 417 | // Handle if directory was already renamed. |
| 418 | dir = opendir(new_directory.c_str()); |
| 419 | if (!dir) { |
| 420 | LOG(ERROR) << "Old directory " << current_directory |
| 421 | << " missing and new directory " << new_directory |
| 422 | << " not present."; |
| 423 | return false; |
| 424 | } |
| 425 | closedir(dir); |
| 426 | } |
| 427 | old_base_name_ = base_name_; |
| 428 | base_name_ = std::string(new_base_name); |
| 429 | separator_ = base_name_.back() == '/' ? "" : "_"; |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | WriteCode RenamableFileBackend::RenameFileAfterClose( |
| 434 | std::string_view filename) { |
| 435 | // Fast check that we can skip rename. |
| 436 | if (!use_temp_files_ && old_base_name_.empty()) { |
| 437 | return WriteCode::kOk; |
| 438 | } |
| 439 | |
| 440 | std::string current_filename(filename); |
| 441 | |
| 442 | // When changing the base name, we rename the log folder while there active |
| 443 | // buffer writers. Therefore, the name of that active buffer may still refer |
| 444 | // to the old file location rather than the new one. |
| 445 | if (!old_base_name_.empty()) { |
| 446 | auto offset = current_filename.find(old_base_name_); |
| 447 | if (offset != std::string::npos) { |
| 448 | current_filename.replace(offset, old_base_name_.length(), base_name_); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | std::string final_filename = current_filename; |
| 453 | if (use_temp_files_) { |
| 454 | CHECK(current_filename.size() > temp_suffix_.size()); |
| 455 | final_filename = current_filename.substr( |
| 456 | 0, current_filename.size() - temp_suffix_.size()); |
| 457 | } |
| 458 | |
| 459 | int result = rename(current_filename.c_str(), final_filename.c_str()); |
| 460 | |
| 461 | bool ran_out_of_space = false; |
| 462 | if (result != 0) { |
| 463 | if (errno == ENOSPC) { |
| 464 | ran_out_of_space = true; |
| 465 | } else { |
| 466 | PLOG(FATAL) << "Renaming " << current_filename << " to " << final_filename |
| 467 | << " failed"; |
| 468 | } |
| 469 | } else { |
| 470 | VLOG(1) << "Renamed " << current_filename << " -> " << final_filename; |
| 471 | } |
| 472 | return ran_out_of_space ? WriteCode::kOutOfSpace : WriteCode::kOk; |
| 473 | } |
| 474 | |
| 475 | WriteCode RenamableFileBackend::RenamableFileHandler::Close() { |
| 476 | if (!is_open()) { |
| 477 | return WriteCode::kOk; |
| 478 | } |
| 479 | if (FileHandler::Close() == WriteCode::kOutOfSpace) { |
| 480 | return WriteCode::kOutOfSpace; |
| 481 | } |
| 482 | if (owner_->RenameFileAfterClose(filename()) == WriteCode::kOutOfSpace) { |
| 483 | return WriteCode::kOutOfSpace; |
| 484 | } |
| 485 | return WriteCode::kOk; |
| 486 | } |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 487 | |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 488 | } // namespace aos::logger |