masaori335 commented on code in PR #13328: URL: https://github.com/apache/trafficserver/pull/13328#discussion_r3687438799
########## src/traffic_ctl/CacheShmCommand.cc: ########## @@ -0,0 +1,266 @@ +/** @file + + traffic_ctl command for inspecting and clearing the cache shared-memory + control segment and its associated stripe segments. + + The status and clear operations work by direct shm_open access rather than + JSONRPC, so they function whether traffic_server is running or not. This + is important for debugging crash-leftover segments when no live process + is available to query. + + @section license License + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#include "CacheShmCommand.h" +#include "CacheShmLayout.h" +#include "CacheShmPurge.h" +#include "TrafficCtlStatus.h" + +#include <fcntl.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <unistd.h> + +#include <cerrno> +#include <cstdio> +#include <cstring> +#include <iostream> + +namespace +{ + +// The middle word of the shm name prefix when --prefix is not given. The +// framing "/<word>-" is supplied by cache_shm::normalize_name_prefix, matching +// the server's proxy.config.cache.shm.name_prefix default. +constexpr const char *DEFAULT_PREFIX = "ats"; + +bool +shm_segment_exists(const std::string &name) +{ + int fd = shm_open(name.c_str(), O_RDONLY, 0); + if (fd < 0) { + return false; + } + close(fd); + return true; +} + +std::string +format_size(uint64_t bytes) +{ + char buf[64]; + if (bytes >= (uint64_t{1} << 30)) { + std::snprintf(buf, sizeof(buf), "%.2f GiB", static_cast<double>(bytes) / (uint64_t{1} << 30)); + } else if (bytes >= (uint64_t{1} << 20)) { + std::snprintf(buf, sizeof(buf), "%.2f MiB", static_cast<double>(bytes) / (uint64_t{1} << 20)); + } else if (bytes >= (uint64_t{1} << 10)) { + std::snprintf(buf, sizeof(buf), "%.2f KiB", static_cast<double>(bytes) / (uint64_t{1} << 10)); + } else { + std::snprintf(buf, sizeof(buf), "%llu B", static_cast<unsigned long long>(bytes)); + } + return buf; +} + +// Shared with the cache subsystem (CacheShmPurge.h): read_shm_name bounds a +// possibly-unterminated name field, process_is_alive backs the owner-liveness gate. +using cache_shm::process_is_alive; +using cache_shm::read_shm_name; + +} // namespace + +CacheShmCommand::CacheShmCommand(ts::Arguments *args) : CtrlCommand(args) +{ + if (get_parsed_arguments()->get(STATUS_STR)) { + _invoked_func = [this]() { status(); }; + } else if (get_parsed_arguments()->get(CLEAR_STR)) { + _invoked_func = [this]() { clear(); }; + } +} + +std::string +CacheShmCommand::get_prefix() +{ + // The operator gives only the middle word (e.g. --prefix ats); frame it the + // same way the server does so the two agree on segment names. + std::string configured = DEFAULT_PREFIX; + if (auto arg = get_parsed_arguments()->get(PREFIX_STR); arg && !arg.empty()) { + configured = arg.value(); + } + return cache_shm::normalize_name_prefix(configured); +} + +void +CacheShmCommand::status() +{ + const std::string prefix = get_prefix(); + const std::string control_name = cache_shm::control_segment_name(prefix); + + int fd = shm_open(control_name.c_str(), O_RDONLY, 0); + if (fd < 0) { + std::cerr << "cache shm: control segment '" << control_name << "' not found: " << std::strerror(errno) << '\n'; Review Comment: Added, in status() and in clear()'s NotPresent. Suppressed when --prefix was actually given, so the hint only appears when the default prefix was in play. Addressed by 80cff77c80. ########## src/iocore/cache/StripeSM.cc: ########## @@ -178,6 +179,26 @@ StripeSM::init(bool clear) return clear_dir_aio(); } + // shm fast restart: a clean shutdown left the in-shm directory authoritative, so + // skip both the disk read and recover_data() (which would re-scan the tail and + // discard the entries the shm copy preserved). After validating the in-shm + // header/footer, jump straight to dir_init_done() in the normal post-recovery + // state. Validation failure falls through to disk read + recover_data(). + if (CacheShm::mode() == CacheShm::Mode::AttachExisting && CacheShm::is_shm_pointer(this->directory.raw_dir)) { + if (this->directory.header->magic == STRIPE_MAGIC && this->directory.footer->magic == STRIPE_MAGIC && Review Comment: Added, with one prerequisite pass in front of it. Directory::check() follows next through dir_from_offset(), which does no bounds checking, and dir_next is a uint16_t. On a stripe with fewer than 16384 buckets — so segment_entries < 65536, which a small stripe is — a torn link can walk check() off the end of the mapping and segfault instead of rejecting. That would turn "reject and rebuild" into a crash loop, so _shm_directory_is_valid() now does one sequential pass bounds-checking every entry's next and prev first, and only then runs the structural walk. prev is included because the runtime writes through it (unlink_from_freelist, delete_entry) even though check() never follows it. Both passes are in-memory and sequential, so they are small next to the disk read + recover_data() they replace, as you say. Also fixed the design doc, which repeated the same "all writers are quiesced" claim you flagged in CacheDir.cc — it now states that this runs before shut_down_event_system(), that the main thread does not join the event threads, and that the structural re-validation is the reason clean_shutdown alone is not trusted. Addressed by 80cff77c80. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
