You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by jr...@apache.org on 2018/10/17 18:12:25 UTC
[trafficserver] branch master updated: TCL: Rmove TCL dependency
from HostStatus
This is an automated email from the ASF dual-hosted git repository.
jrushford pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new 168275b TCL: Rmove TCL dependency from HostStatus
168275b is described below
commit 168275b726adc8bff5834e47c44a1ee460a13ee8
Author: Xavier Chi <ch...@gmail.com>
AuthorDate: Tue Oct 16 16:53:53 2018 -0500
TCL: Rmove TCL dependency from HostStatus
---
proxy/HostStatus.h | 10 +++++---
src/traffic_server/HostStatus.cc | 49 ++++++++++++++++------------------------
2 files changed, 27 insertions(+), 32 deletions(-)
diff --git a/proxy/HostStatus.h b/proxy/HostStatus.h
index 40615f5..8257913 100644
--- a/proxy/HostStatus.h
+++ b/proxy/HostStatus.h
@@ -32,10 +32,11 @@
#include <time.h>
#include <string>
-#include "tscore/ink_hash_table.h"
#include "tscore/ink_rwlock.h"
#include "records/P_RecProcess.h"
+#include <unordered_map>
+
enum HostStatus_t {
HOST_STATUS_INIT,
HOST_STATUS_DOWN,
@@ -92,8 +93,11 @@ private:
HostStatus(const HostStatus &obj) = delete;
HostStatus &operator=(HostStatus const &) = delete;
- InkHashTable *hosts_statuses; // next hop status, key is hostname or ip string, data is bool (available).
- InkHashTable *hosts_stats_ids; // next hop stat ids, key is hostname or ip string, data is int stat id.
+ // next hop status, key is hostname or ip string, data is bool (available).
+ std::unordered_map<std::string, HostStatRec_t *> hosts_statuses;
+ // next hop stat ids, key is hostname or ip string, data is int stat id.
+ std::unordered_map<std::string, int> hosts_stats_ids;
+
ink_rwlock host_status_rwlock;
ink_rwlock host_statids_rwlock;
};
diff --git a/src/traffic_server/HostStatus.cc b/src/traffic_server/HostStatus.cc
index ef2fb06..365e2e0 100644
--- a/src/traffic_server/HostStatus.cc
+++ b/src/traffic_server/HostStatus.cc
@@ -94,8 +94,6 @@ mgmt_host_status_down_callback(void *x, char *data, int len)
HostStatus::HostStatus()
{
- hosts_statuses = ink_hash_table_create(InkHashTableKeyType_String);
- hosts_stats_ids = ink_hash_table_create(InkHashTableKeyType_String);
ink_rwlock_init(&host_status_rwlock);
ink_rwlock_init(&host_statids_rwlock);
pmgmt->registerMgmtCallback(MGMT_EVENT_HOST_STATUS_UP, mgmt_host_status_up_callback, nullptr);
@@ -105,20 +103,10 @@ HostStatus::HostStatus()
HostStatus::~HostStatus()
{
- // release host_statues hash table.
- InkHashTableIteratorState ht_iter;
- InkHashTableEntry *ht_entry = nullptr;
- ht_entry = ink_hash_table_iterator_first(hosts_statuses, &ht_iter);
-
- while (ht_entry != nullptr) {
- HostStatRec_t *value = static_cast<HostStatRec_t *>(ink_hash_table_entry_value(hosts_statuses, ht_entry));
- ats_free(value);
- ht_entry = ink_hash_table_iterator_next(hosts_statuses, &ht_iter);
+ for (auto &&it : hosts_statuses) {
+ ats_free(it.second);
}
- ink_hash_table_destroy(hosts_statuses);
-
// release host_stats_ids hash and the read and writer locks.
- ink_hash_table_destroy(hosts_stats_ids);
ink_rwlock_destroy(&host_status_rwlock);
ink_rwlock_destroy(&host_statids_rwlock);
}
@@ -150,9 +138,11 @@ HostStatus::setHostStatus(const char *name, HostStatus_t status, const unsigned
// using the hash table pointer to store the HostStatus_t value.
HostStatRec_t *host_stat = nullptr;
ink_rwlock_wrlock(&host_status_rwlock);
- if (ink_hash_table_lookup(hosts_statuses, name, reinterpret_cast<InkHashTableValue *>(&host_stat)) == 0) {
+ if (auto it = hosts_statuses.find(name); it != hosts_statuses.end()) {
+ host_stat = it->second;
+ } else {
host_stat = static_cast<HostStatRec_t *>(ats_malloc(sizeof(HostStatRec_t)));
- ink_hash_table_insert(hosts_statuses, name, reinterpret_cast<InkHashTableValue *>(host_stat));
+ hosts_statuses.emplace(name, host_stat);
}
host_stat->status = status;
host_stat->down_time = down_time;
@@ -175,16 +165,19 @@ HostStatus_t
HostStatus::getHostStatus(const char *name)
{
HostStatRec_t *_status = 0;
- int lookup = 0;
time_t now = time(0);
// the hash table value pointer has the HostStatus_t value.
ink_rwlock_rdlock(&host_status_rwlock);
- lookup = ink_hash_table_lookup(hosts_statuses, name, reinterpret_cast<void **>(&_status));
+ auto it = hosts_statuses.find(name);
+ bool lookup = it != hosts_statuses.end();
+ if (lookup) {
+ _status = it->second;
+ }
ink_rwlock_unlock(&host_status_rwlock);
// if the host was marked down and it's down_time has elapsed, mark it up.
- if (lookup == 1 && _status->status == HostStatus_t::HOST_STATUS_DOWN && _status->down_time > 0) {
+ if (lookup && _status->status == HostStatus_t::HOST_STATUS_DOWN && _status->down_time > 0) {
if ((_status->down_time + _status->marked_down) < now) {
Debug("host_statuses", "name: %s, now: %ld, down_time: %d, marked_down: %ld", name, now, _status->down_time,
_status->marked_down);
@@ -192,27 +185,24 @@ HostStatus::getHostStatus(const char *name)
return HostStatus_t::HOST_STATUS_UP;
}
}
- return lookup == 1 ? static_cast<HostStatus_t>(_status->status) : HostStatus_t::HOST_STATUS_INIT;
+ return lookup ? static_cast<HostStatus_t>(_status->status) : HostStatus_t::HOST_STATUS_INIT;
}
void
HostStatus::createHostStat(const char *name)
{
- InkHashTableEntry *entry;
-
ink_rwlock_wrlock(&host_statids_rwlock);
{
for (const char *i : Reasons::reasons) {
std::string reason_stat;
getStatName(reason_stat, name, i);
- entry = ink_hash_table_lookup_entry(hosts_stats_ids, reason_stat.c_str());
- if (entry == nullptr) {
+ if (hosts_stats_ids.find(reason_stat) == hosts_stats_ids.end()) {
RecRegisterRawStat(host_status_rsb, RECT_PROCESS, (reason_stat).c_str(), RECD_INT, RECP_NON_PERSISTENT, (int)next_stat_id,
RecRawStatSyncSum);
RecSetRawStatCount(host_status_rsb, next_stat_id, 1);
RecSetRawStatSum(host_status_rsb, next_stat_id, 1);
- ink_hash_table_insert(hosts_stats_ids, reason_stat.c_str(), reinterpret_cast<void *>(next_stat_id));
+ hosts_stats_ids.emplace(reason_stat, next_stat_id);
Debug("host_statuses", "stat name: %s, id: %d", reason_stat.c_str(), next_stat_id);
next_stat_id++;
@@ -226,13 +216,14 @@ HostStatus::createHostStat(const char *name)
int
HostStatus::getHostStatId(const char *stat_name)
{
- int lookup = 0;
- intptr_t _id = -1;
+ int _id = -1;
ink_rwlock_rdlock(&host_statids_rwlock);
- lookup = ink_hash_table_lookup(hosts_stats_ids, stat_name, reinterpret_cast<void **>(&_id));
+ if (auto it = hosts_stats_ids.find(stat_name); it != hosts_stats_ids.end()) {
+ _id = it->second;
+ }
ink_rwlock_unlock(&host_statids_rwlock);
Debug("host_statuses", "name: %s, id: %d", stat_name, static_cast<int>(_id));
- return lookup == 1 ? static_cast<int>(_id) : -1;
+ return _id;
}