From b72ecfbc8ee042ce24219489f6a852145a98b831 Mon Sep 17 00:00:00 2001 From: Walter Karas Date: Thu, 6 Jan 2022 17:18:10 -0600 Subject: [PATCH] Clean up of next hop HostRecord class. --- include/tscore/ConsistentHash.h | 4 +- proxy/http/remap/NextHopSelectionStrategy.cc | 6 +- proxy/http/remap/NextHopSelectionStrategy.h | 75 ++++++-------------- 3 files changed, 25 insertions(+), 60 deletions(-) diff --git a/include/tscore/ConsistentHash.h b/include/tscore/ConsistentHash.h index 75d979c0aed..9fe7a237246 100644 --- a/include/tscore/ConsistentHash.h +++ b/include/tscore/ConsistentHash.h @@ -32,8 +32,8 @@ */ struct ATSConsistentHashNode { - std::atomic available; - char *name; + std::atomic available{true}; + char *name{nullptr}; }; std::ostream &operator<<(std::ostream &os, ATSConsistentHashNode &thing); diff --git a/proxy/http/remap/NextHopSelectionStrategy.cc b/proxy/http/remap/NextHopSelectionStrategy.cc index f3b507c3064..6a39028a3ea 100644 --- a/proxy/http/remap/NextHopSelectionStrategy.cc +++ b/proxy/http/remap/NextHopSelectionStrategy.cc @@ -211,7 +211,7 @@ NextHopSelectionStrategy::Init(ts::Yaml::Map &n) std::vector> hosts_inner; for (unsigned int hst = 0; hst < hosts_list.size(); ++hst) { - std::shared_ptr host_rec = std::make_shared(hosts_list[hst].as()); + std::shared_ptr host_rec = std::make_shared(hosts_list[hst].as()); host_rec->group_index = grp; host_rec->host_index = hst; if ((self_host == host_rec->hostname) || mach->is_self(host_rec->hostname.c_str())) { @@ -311,9 +311,9 @@ NextHopSelectionStrategy::responseIsRetryable(int64_t sm_id, HttpTransact::Curre namespace YAML { -template <> struct convert { +template <> struct convert { static bool - decode(const Node &node, HostRecord &nh) + decode(const Node &node, HostRecordCfg &nh) { ts::Yaml::Map map{node}; ts::Yaml::Map *mmap{&map}; diff --git a/proxy/http/remap/NextHopSelectionStrategy.h b/proxy/http/remap/NextHopSelectionStrategy.h index 628c01d55f8..1f931cd275e 100644 --- a/proxy/http/remap/NextHopSelectionStrategy.h +++ b/proxy/http/remap/NextHopSelectionStrategy.h @@ -23,6 +23,8 @@ #pragma once +#include + #include "ts/parentselectdefs.h" #include "ParentSelection.h" #include "HttpTransact.h" @@ -103,64 +105,27 @@ struct NHProtocol { std::string health_check_url; }; -struct HostRecord : ATSConsistentHashNode { - std::mutex _mutex; +struct HostRecordCfg { std::string hostname; - std::atomic failedAt; - std::atomic failCount; - std::atomic upAt; - float weight; - std::string hash_string; - int host_index; - int group_index; - bool self = false; std::vector> protocols; + float weight{0}; + std::string hash_string; +}; - // construct without locking the _mutex. - HostRecord() - { - hostname = ""; - failedAt = 0; - failCount = 0; - upAt = 0; - weight = 0; - hash_string = ""; - host_index = -1; - group_index = -1; - available = true; - } - - // copy constructor to avoid copying the _mutex. - HostRecord(const HostRecord &o) - { - hostname = o.hostname; - failedAt = o.failedAt.load(); - failCount = o.failCount.load(); - upAt = o.upAt.load(); - weight = o.weight; - hash_string = o.hash_string; - host_index = -1; - group_index = -1; - available = true; - protocols = o.protocols; - } - - // assign without copying the _mutex. - HostRecord & - operator=(const HostRecord &o) - { - hostname = o.hostname; - failedAt = o.failedAt.load(); - failCount = o.failCount.load(); - upAt = o.upAt.load(); - weight = o.weight; - hash_string = o.hash_string; - host_index = o.host_index; - group_index = o.group_index; - available = o.available.load(); - protocols = o.protocols; - return *this; - } +struct HostRecord : public ATSConsistentHashNode, public HostRecordCfg { + std::mutex _mutex; + std::atomic failedAt{0}; + std::atomic failCount{0}; + std::atomic upAt{0}; + int host_index{-1}; + int group_index{-1}; + bool self{false}; + + explicit HostRecord(HostRecordCfg &&o) : HostRecordCfg(std::move(o)) {} + + // No copying or moving. + HostRecord(const HostRecord &) = delete; + HostRecord &operator=(const HostRecord &) = delete; // locks the record when marking this host down. void