Github user SolidWallOfCode commented on a diff in the pull request:
https://github.com/apache/trafficserver/pull/843#discussion_r74271671
--- Diff: plugins/experimental/carp/CarpHashAlgorithm.cc ---
@@ -0,0 +1,396 @@
+/** @file
+
+ Implements the CARP hash algorithm
+
+ @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 <ts/ts.h>
+#include <stdio.h>
+#include <memory.h>
+#include <list>
+
+#include <iostream>
+#include <sstream>
+
+#include "Common.h"
+#include "CarpHashAlgorithm.h"
+
+using namespace std;
+
+/*****************************************************************/
+void
+HashNode::dump(string& s)
+{
+ stringstream ss;
+ string sSockaddr;
+ getStringFromSockaddr(reinterpret_cast<const struct sockaddr *>(
&forwardAddr), sSockaddr);
+
+ ss << scheme << "://" << name << ":"<<listenPort<<" ("<<sSockaddr<<")
weight:"<<weight<< (_status ? string(" UP ") : string(" DOWN "));
+ if(_statusTime) {
+ ss << "(" << time(NULL)-_statusTime<< "s ago in "<< _statusLatencyMs
<< "mS)";
+ }
+ ss << " hits:" << _hits;
+ ss << " carp_noforwarded:" << _carp_noforwarded;
+ ss << " carp_forwarded:" << _carp_forwarded;
+ ss << endl;
+ s += ss.str();
+}
+
+/*****************************************************************/
+void
+HashAlgorithm::addHost(std::string name, unsigned int port, std::string
scheme, double weight, bool self, struct sockaddr_storage fwdAddr)
+{
+ HashNode* node = new HashNode(name, port, scheme, weight, self, fwdAddr);
+ TSAssert(NULL != node);
+ addHost(node);
+}
+
+/*****************************************************************/
+void
+HashAlgorithm::addHost(HashNode* node)
+{
+ if (!node) return;
+ _hostList.push_back(node);
+}
+
+/*****************************************************************/
+HashNode*
+HashAlgorithm::findStatusByNameAndPort(const string& name, unsigned int
port,
+ size_t* index) {
+ /*
+ * Todo: This use loop to find the corresponding HashNode
+ * But the HttpClient and the Hash was related, so we
+ * could use more easier method to write the status
+ */
+ for (size_t ptr = 0; ptr < _hostList.size(); ptr++) {
+ if (_hostList[ptr]->listenPort == port
+ && _hostList[ptr]->name.compare(name) == 0) { // found it
+ if (index) {
+ *index = ptr;
+ }
+ return _hostList[ptr];
+ }
+ }
+ return NULL;
+}
+
+size_t
+HashAlgorithm::findHashNodeIndex(HashNode *node) {
+ for (size_t ptr = 0; ptr < _hostList.size(); ptr++) {
+ if ( _hostList[ptr] == node) {
+ return ptr;
+ }
+ }
+ return -1;
+}
+
+/*****************************************************************/
+void
+HashAlgorithm::setStatus(const string& name, unsigned int port, bool
status, time_t time, uint64_t latencyMs)
+{
+ TSDebug(DEBUG_TAG_INIT, "HashAlgorithm::setStatus name=%s status=%d",
name.c_str(), status);
+
+ HashNode* node = findStatusByNameAndPort(name,port);
+ if(node) {
+ node->setStatus(status,time,latencyMs);
+ } else {
+ TSError("Carp internal error setStatus host %s not
found",name.c_str());
+ }
+}
+
+void
+HashAlgorithm::setStatus(HashNode * node, bool status, time_t time,
uint64_t latencyMs)
+{
+ TSDebug(DEBUG_TAG_INIT, "HashAlgorithm::setStatus name=%s status=%d",
node->name.c_str(), status);
+
+// HashNode* node = findStatusByNameAndPort(name,port);
+ if(node) {
+ node->setStatus(status,time,latencyMs);
+ } else {
+ TSError("Carp internal error setStatus host %s not
found",node->name.c_str());
+ }
+}
+
+/*****************************************************************/
+HashAlgorithm::~HashAlgorithm()
+{
+ for (size_t ptr = 0; ptr < _hostList.size(); ptr++) {
+ delete _hostList[ptr];
+ }
+}
+
+/*****************************************************************/
+void
+HashAlgorithm::dump(string& s)
+{
+ _config->dump(s);
+
+ stringstream ss;
+ ss << "Hash Algo stats:" << endl;
+ s += ss.str();
+
+ for (size_t ptr = 0; ptr < _hostList.size(); ptr++) {
+ ss.str("");
+ ss.clear();
+ ss << ptr << "-";
+ s += ss.str();
+ _hostList[ptr]->dump(s);
+ }
+}
+
+/*****************************************************************/
+void
+CarpHashAlgorithm::algoInit()
+{
+ // calculate hash for each host
+ for (size_t ptr = 0; ptr < _hostList.size(); ptr++) {
+ std::string cname;
+ char buf[64];
+ memset(buf, 0, 64);
+ snprintf(buf, 64, "%s:%d", _hostList[ptr]->name.c_str(),
_hostList[ptr]->listenPort);
+ cname = buf;
+
+ _hostList[ptr]->hash = _calculateHash(cname);
+ }
+
+ // calculate loadmultiplier
+ _calculateLoadMultiplier();
+
+ for (size_t ptr = 0; ptr < _hostList.size(); ptr++) {
+ TSDebug(DEBUG_TAG_INIT, "algoInit host=%s port=%d hash=0x%x weight=%g
loadFac=%g loadMult=%g isSelf=%d status=%d",
+ _hostList[ptr]->name.c_str(),
+ _hostList[ptr]->listenPort,
+ _hostList[ptr]->hash,
+ _hostList[ptr]->weight,
+ _hostList[ptr]->loadFactor,
+ _hostList[ptr]->loadMultiplier,
+ _hostList[ptr]->isSelf,
+ _hostList[ptr]->getStatus());
+ }
+}
+
+/*****************************************************************/
+std::vector<HashNode*>
+CarpHashAlgorithm::getRemapProxyList(const std::string& url)
+{
+ std::vector<HashNode *> hn=_selectReplicateNodes(url);
+ return hn;
+}
+
+/*****************************************************************/
+HashNode*
+CarpHashAlgorithm::getRemapProxy(const std::string& url)
+{
+ HashNode *hn=_selectNode(url);
+ if(hn) {
+ hn->hit();
+ }
+ return hn;
+}
+
+
+/*****************************************************************/
+unsigned int
+CarpHashAlgorithm::_calculateHash(const std::string& hostname)
+{
+ // check input data
+ if (hostname.empty())
+ return 0;
+
+ unsigned int hash = 0;
--- End diff --
Does the hash output have a well defined size? That is, is it specifically
a 32 bit or 64 bit (for example) hash?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---