SolidWallOfCode commented on a change in pull request #7023:
URL: https://github.com/apache/trafficserver/pull/7023#discussion_r499090232



##########
File path: 
plugins/experimental/nexthop_strategy_consistenthash/consistenthash.cc
##########
@@ -0,0 +1,483 @@
+/*
+ * 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 "strategy.h"
+#include "consistenthash.h"
+#include "util.h"
+
+#include <cinttypes>
+#include <string>
+#include <algorithm>
+#include <mutex>
+#include <unordered_map>
+#include <unordered_set>
+#include <fstream>
+#include <cstring>
+
+#include <sys/stat.h>
+#include <dirent.h>
+
+#include <yaml-cpp/yaml.h>
+
+#include "tscore/HashSip.h"
+#include "tscore/ConsistentHash.h"
+#include "tscore/ink_assert.h"
+#include "ts/ts.h"
+#include "ts/remap.h"
+#include "tscpp/api/nexthop.h"
+#include "tscpp/api/parentresult.h"
+
+// hash_key strings.
+constexpr std::string_view hash_key_url           = "url";
+constexpr std::string_view hash_key_hostname      = "hostname";
+constexpr std::string_view hash_key_path          = "path";
+constexpr std::string_view hash_key_path_query    = "path+query";
+constexpr std::string_view hash_key_path_fragment = "path+fragment";
+constexpr std::string_view hash_key_cache         = "cache_key";
+
+static HostRecord *
+chash_lookup(std::shared_ptr<ATSConsistentHash> ring, uint64_t hash_key, 
ATSConsistentHashIter *iter, bool *wrapped,
+             ATSHash64Sip24 *hash, bool *hash_init, bool *mapWrapped, uint64_t 
sm_id)
+{
+  HostRecord *host_rec = nullptr;
+
+  if (*hash_init == false) {
+    host_rec   = static_cast<HostRecord *>(ring->lookup_by_hashval(hash_key, 
iter, wrapped));
+    *hash_init = true;
+  } else {
+    host_rec = static_cast<HostRecord *>(ring->lookup(nullptr, iter, wrapped, 
hash));
+  }
+  bool wrap_around = *wrapped;
+  *wrapped         = (*mapWrapped && *wrapped) ? true : false;
+  if (!*mapWrapped && wrap_around) {
+    *mapWrapped = true;
+  }
+
+  return host_rec;
+}
+
+NextHopConsistentHash::NextHopConsistentHash(const std::string_view name) : 
NextHopSelectionStrategy(name) {}
+
+NextHopConsistentHash::~NextHopConsistentHash()
+{
+  NH_Debug(NH_DEBUG_TAG, "destructor called for strategy named: %s", 
strategy_name.c_str());
+}
+
+bool
+NextHopConsistentHash::Init(const YAML::Node &n)
+{
+  ATSHash64Sip24 hash;
+
+  try {
+    if (n["hash_key"]) {
+      auto hash_key_val = n["hash_key"].Scalar();
+      if (hash_key_val == hash_key_url) {
+        hash_key = NH_URL_HASH_KEY;
+      } else if (hash_key_val == hash_key_hostname) {
+        hash_key = NH_HOSTNAME_HASH_KEY;
+      } else if (hash_key_val == hash_key_path) {
+        hash_key = NH_PATH_HASH_KEY;
+      } else if (hash_key_val == hash_key_path_query) {
+        hash_key = NH_PATH_QUERY_HASH_KEY;
+      } else if (hash_key_val == hash_key_path_fragment) {
+        hash_key = NH_PATH_FRAGMENT_HASH_KEY;
+      } else if (hash_key_val == hash_key_cache) {
+        hash_key = NH_CACHE_HASH_KEY;
+      } else {
+        hash_key = NH_PATH_HASH_KEY;
+        NH_Note("Invalid 'hash_key' value, '%s', for the strategy named '%s', 
using default '%s'.", hash_key_val.c_str(),
+                strategy_name.c_str(), hash_key_path.data());
+      }
+    }
+  } catch (std::exception &ex) {
+    NH_Note("Error parsing the strategy named '%s' due to '%s', this strategy 
will be ignored.", strategy_name.c_str(), ex.what());
+    return false;
+  }
+
+  bool result = NextHopSelectionStrategy::Init(n);
+  if (!result) {
+    return false;
+  }
+
+  // load up the hash rings.
+  for (uint32_t i = 0; i < groups; i++) {
+    std::shared_ptr<ATSConsistentHash> hash_ring = 
std::make_shared<ATSConsistentHash>();
+    for (uint32_t j = 0; j < host_groups[i].size(); j++) {
+      // ATSConsistentHash needs the raw pointer.
+      HostRecord *p = host_groups[i][j].get();
+      // need to copy the 'hash_string' or 'hostname' cstring to 'name' for 
insertion into ATSConsistentHash.
+      if (!p->hash_string.empty()) {
+        p->name = const_cast<char *>(p->hash_string.c_str());

Review comment:
       Is `const_cast` really required here? It's a bit risky.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to