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