From 8600cea29375a74ffac4c651548f2c670a54519e Mon Sep 17 00:00:00 2001 From: Masaori Koshiba Date: Wed, 15 Oct 2025 15:23:59 +0900 Subject: [PATCH 1/2] Cleanup: Remove unused code of HostDB --- include/iocore/hostdb/HostDBProcessor.h | 3 - src/iocore/hostdb/HostDB.cc | 22 +------ src/iocore/hostdb/P_RefCountCache.h | 88 +------------------------ src/iocore/hostdb/RefCountCache.cc | 14 ---- src/iocore/hostdb/test_RefCountCache.cc | 16 ----- 5 files changed, 6 insertions(+), 137 deletions(-) diff --git a/include/iocore/hostdb/HostDBProcessor.h b/include/iocore/hostdb/HostDBProcessor.h index dde0ac95f4f..7d561a041e4 100644 --- a/include/iocore/hostdb/HostDBProcessor.h +++ b/include/iocore/hostdb/HostDBProcessor.h @@ -505,9 +505,6 @@ class HostDBRecord : public RefCountObj */ static self_type *unmarshall(char *buff, unsigned size); - /// Database version. - static constexpr ts::VersionNumber Version{3, 0}; - protected: /// Current active info. /// @note This value may be out of range due to the difficulty of synchronization, therefore diff --git a/src/iocore/hostdb/HostDB.cc b/src/iocore/hostdb/HostDB.cc index ccc15d85d95..0902aebfa26 100644 --- a/src/iocore/hostdb/HostDB.cc +++ b/src/iocore/hostdb/HostDB.cc @@ -279,9 +279,9 @@ HostDBCache::start(int flags) } // Setup the ref-counted cache (this must be done regardless of syncing or not). - this->refcountcache = new RefCountCache(hostdb_partitions, hostdb_max_size, hostdb_max_count, HostDBRecord::Version, - "proxy.process.hostdb.cache."); - this->pending_dns = new Queue[hostdb_partitions]; + this->refcountcache = + new RefCountCache(hostdb_partitions, hostdb_max_size, hostdb_max_count, "proxy.process.hostdb.cache."); + this->pending_dns = new Queue[hostdb_partitions]; this->remoteHostDBQueue = new Queue[hostdb_partitions]; return 0; } @@ -1573,22 +1573,6 @@ HostDBRecord::alloc(TextView query_name, unsigned int rr_count, size_t srv_name_ return self; } -HostDBRecord::self_type * -HostDBRecord::unmarshall(char *buff, unsigned size) -{ - if (size < sizeof(self_type)) { - return nullptr; - } - auto src = reinterpret_cast(buff); - ink_release_assert(size == src->_record_size); - auto ptr = ioBufAllocator[src->_iobuffer_index].alloc_void(); - auto self = static_cast(ptr); - new (self) self_type(); - auto delta = sizeof(RefCountObj); // skip the VFTP and ref count. - memcpy(static_cast(ptr) + delta, buff + delta, size - delta); - return self; -} - bool HostDBRecord::serve_stale_but_revalidate() const { diff --git a/src/iocore/hostdb/P_RefCountCache.h b/src/iocore/hostdb/P_RefCountCache.h index d7e16c87465..be75881cac9 100644 --- a/src/iocore/hostdb/P_RefCountCache.h +++ b/src/iocore/hostdb/P_RefCountCache.h @@ -360,24 +360,10 @@ RefCountCachePartition::get_map() return this->item_map; } -// The header for the cache, this is used to check if the serialized cache is compatible -class RefCountCacheHeader -{ -public: - unsigned int magic = REFCOUNTCACHE_MAGIC_NUMBER; - ts::VersionNumber version{REFCOUNTCACHE_VERSION}; - ts::VersionNumber object_version; // version passed in of whatever it is we are caching - - RefCountCacheHeader(ts::VersionNumber object_version = ts::VersionNumber()); - bool operator==(RefCountCacheHeader const &that) const; - bool compatible(RefCountCacheHeader *that) const; -}; - // RefCountCache is a ref-counted key->value map to store classes that inherit from RefCountObj. // Once an item is `put` into the cache, the cache will maintain a Ptr<> to that object until erase // or clear is called-- which will remove the cache's Ptr<> to the object. // -// This cache may be Persisted (RefCountCacheSync) as well as loaded from disk (LoadRefCountCacheFromPath). // This class will optionally emit metrics at the given `metrics_prefix`. // // Note: although this cache does allow you to set expiry times this cache does not actively GC itself-- meaning @@ -391,8 +377,7 @@ template class RefCountCache { public: // Constructor - RefCountCache(unsigned int num_partitions, int size = -1, int items = -1, ts::VersionNumber object_version = ts::VersionNumber(), - const std::string &metrics_prefix = ".refcountcache"); + RefCountCache(unsigned int num_partitions, int size = -1, int items = -1, const std::string &metrics_prefix = ".refcountcache"); // Destructor ~RefCountCache(); @@ -408,7 +393,6 @@ template class RefCountCache size_t partition_count() const; RefCountCachePartition &get_partition(int pnum); size_t count() const; - RefCountCacheHeader &get_header(); RefCountCacheBlock *get_rsb(); private: @@ -416,15 +400,11 @@ template class RefCountCache int max_items; // Total number of items allowed unsigned int num_partitions; std::vector>> partitions; - // Header - RefCountCacheHeader header; // Our header - RefCountCacheBlock rsb; + RefCountCacheBlock rsb; }; template -RefCountCache::RefCountCache(unsigned int num_partitions, int size, int items, ts::VersionNumber object_version, - const std::string &metrics_prefix) - : header(RefCountCacheHeader(object_version)) +RefCountCache::RefCountCache(unsigned int num_partitions, int size, int items, const std::string &metrics_prefix) { this->max_size = size; this->max_items = items; @@ -476,13 +456,6 @@ RefCountCache::partition_for_key(uint64_t key) return key % this->num_partitions; } -template -RefCountCacheHeader & -RefCountCache::get_header() -{ - return this->header; -} - template ts::shared_mutex & RefCountCache::lock_for_key(uint64_t key) @@ -537,58 +510,3 @@ RefCountCache::clear() this->partitions[i]->clear(); } } - -// Fill `cache` with items in file `filepath` using `load_func` to unmarshall the record. -// Errors are -1 -template -int -LoadRefCountCacheFromPath(RefCountCache &cache, const std::string &filepath, - CacheEntryType *(*load_func)(char *, unsigned int)) -{ - // If we have no load method, then we can't load anything so lets just stop right here - if (load_func == nullptr) { - return -1; // TODO: some specific error code - } - - int fd = open(filepath.c_str(), O_RDONLY); - if (fd < 0) { - Warning("Unable to open file %s; [Error]: %s", filepath.c_str(), strerror(errno)); - return -1; - } - - // read in the header - RefCountCacheHeader tmpHeader = RefCountCacheHeader(); - int read_ret = read(fd, reinterpret_cast(&tmpHeader), sizeof(RefCountCacheHeader)); - if (read_ret != sizeof(RefCountCacheHeader)) { - UnixSocket{fd}.close(); - Warning("Error reading cache header from disk (expected %ld): %d", sizeof(RefCountCacheHeader), read_ret); - return -1; - } - if (!cache.get_header().compatible(&tmpHeader)) { - UnixSocket{fd}.close(); - Warning("Incompatible cache at %s, not loading.", filepath.c_str()); - return -1; // TODO: specific code for incompatible - } - - RefCountCacheItemMeta tmpValue = RefCountCacheItemMeta(0, 0); - while (true) { // TODO: better loop - read_ret = read(fd, reinterpret_cast(&tmpValue), sizeof(tmpValue)); - if (read_ret != sizeof(tmpValue)) { - break; - } - char buf[tmpValue.size]; - read_ret = read(fd, reinterpret_cast(&buf), tmpValue.size); - if (read_ret != static_cast(tmpValue.size)) { - Warning("Encountered error reading item from cache: %d", read_ret); - break; - } - - CacheEntryType *newItem = load_func(reinterpret_cast(&buf), tmpValue.size); - if (newItem != nullptr) { - cache.put(tmpValue.key, newItem, tmpValue.size - sizeof(CacheEntryType)); - } - }; - - UnixSocket{fd}.close(); - return 0; -} diff --git a/src/iocore/hostdb/RefCountCache.cc b/src/iocore/hostdb/RefCountCache.cc index fa0ea6283ee..8e3e590218e 100644 --- a/src/iocore/hostdb/RefCountCache.cc +++ b/src/iocore/hostdb/RefCountCache.cc @@ -37,17 +37,3 @@ RefCountCacheHashEntry::dealloc(RefCountCacheHashEntry *e) { return refCountCacheHashingValueAllocator.free(e); } - -RefCountCacheHeader::RefCountCacheHeader(ts::VersionNumber object_version) : object_version(object_version){}; - -bool -RefCountCacheHeader::operator==(RefCountCacheHeader const &that) const -{ - return this->magic == that.magic && this->version == that.version; -} - -bool -RefCountCacheHeader::compatible(RefCountCacheHeader *that) const -{ - return this->magic == that->magic && this->version == that->version && this->object_version == that->version; -}; diff --git a/src/iocore/hostdb/test_RefCountCache.cc b/src/iocore/hostdb/test_RefCountCache.cc index 5f47759da6a..4be77b6c5a5 100644 --- a/src/iocore/hostdb/test_RefCountCache.cc +++ b/src/iocore/hostdb/test_RefCountCache.cc @@ -64,20 +64,6 @@ class ExampleStruct : public RefCountObj items_freed.insert(this); printf("freeing: %p items_freed.size(): %zu\n", this, items_freed.size()); } - - static ExampleStruct * - unmarshall(char *buf, unsigned int size) - { - if (size < sizeof(ExampleStruct)) { - return nullptr; - } - ExampleStruct *ret = ExampleStruct::alloc(size - sizeof(ExampleStruct)); - memcpy(static_cast(ret), buf, size); - // Reset the refcount back to 0, this is a bit ugly-- but I'm not sure we want to expose a method - // to mess with the refcount, since this is a fairly unique use case - ret = new (ret) ExampleStruct(); - return ret; - } }; std::set ExampleStruct::items_freed; @@ -216,8 +202,6 @@ test() auto cache = std::make_unique>(cachePartitions); printf("Created...\n"); - LoadRefCountCacheFromPath(*cache, "/tmp/hostdb_cache", ExampleStruct::unmarshall); - printf("Cache started...\n"); int numTestEntries = 10000; // See if anything persisted across the restart From b2a2137ec246551f295c5ab75bed5f374491c106 Mon Sep 17 00:00:00 2001 From: Masaori Koshiba Date: Wed, 15 Oct 2025 15:28:17 +0900 Subject: [PATCH 2/2] More unused function --- include/iocore/hostdb/HostDBProcessor.h | 8 -------- 1 file changed, 8 deletions(-) diff --git a/include/iocore/hostdb/HostDBProcessor.h b/include/iocore/hostdb/HostDBProcessor.h index 7d561a041e4..57ea62e72a2 100644 --- a/include/iocore/hostdb/HostDBProcessor.h +++ b/include/iocore/hostdb/HostDBProcessor.h @@ -497,14 +497,6 @@ class HostDBRecord : public RefCountObj /// The index of @a target in this record. int index_of(HostDBInfo const *target) const; - /** Allocation and initialize an instance from a serialized buffer. - * - * @param buff Serialization data. - * @param size Size of @a buff. - * @return An instance initialized from @a buff. - */ - static self_type *unmarshall(char *buff, unsigned size); - protected: /// Current active info. /// @note This value may be out of range due to the difficulty of synchronization, therefore