Skip to content

Commit f52acd1

Browse files
committed
Remove redundancy.
1 parent 0b777e4 commit f52acd1

File tree

7 files changed

+60
-35
lines changed

7 files changed

+60
-35
lines changed

include/proxy/ParentConsistentHash.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ class ParentConsistentHash : public ParentSelectionStrategy
4848
uint64_t hash_seed0;
4949
uint64_t hash_seed1;
5050

51-
std::unique_ptr<ATSHash64> createHashInstance(ParentHashAlgorithm algo, uint64_t seed0, uint64_t seed1);
52-
5351
public:
5452
static const int PRIMARY = 0;
5553
static const int SECONDARY = 1;

include/proxy/ParentSelection.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@
3535
#include "proxy/ControlMatcher.h"
3636
#include "records/RecProcess.h"
3737
#include "tscore/ConsistentHash.h"
38+
#include "tscore/Hash.h"
3839
#include "tscore/Tokenizer.h"
3940
#include "tscore/ink_apidefs.h"
4041
#include "proxy/HostStatus.h"
4142

4243
#include <algorithm>
44+
#include <memory>
4345
#include <vector>
4446

4547
#define MAX_PARENTS 64
@@ -450,6 +452,10 @@ struct ParentConfig {
450452
// Helper Functions
451453
ParentRecord *createDefaultParent(char *val);
452454

455+
// Hash utility functions
456+
ParentHashAlgorithm parseHashAlgorithm(std::string_view name);
457+
std::unique_ptr<ATSHash64> createHashInstance(ParentHashAlgorithm algo, uint64_t seed0, uint64_t seed1);
458+
453459
// Unit Test Functions
454460
void show_result(ParentResult *aParentResult);
455461
void br(HttpRequestData *h, const char *os_hostname, sockaddr const *dest_ip = nullptr); // short for build request

src/proxy/ParentConsistentHash.cc

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,6 @@ namespace
3030
DbgCtl dbg_ctl_parent_select{"parent_select"};
3131
}
3232

33-
std::unique_ptr<ATSHash64>
34-
ParentConsistentHash::createHashInstance(ParentHashAlgorithm algo, uint64_t seed0, uint64_t seed1)
35-
{
36-
switch (algo) {
37-
case ParentHashAlgorithm::SIPHASH24:
38-
return std::make_unique<ATSHash64Sip24>(seed0, seed1);
39-
case ParentHashAlgorithm::SIPHASH13:
40-
return std::make_unique<ATSHash64Sip13>(seed0, seed1);
41-
default:
42-
Warning("Unknown hash algorithm %d, using SipHash-2-4", static_cast<int>(algo));
43-
return std::make_unique<ATSHash64Sip24>(seed0, seed1);
44-
}
45-
}
46-
4733
ParentConsistentHash::ParentConsistentHash(ParentRecord *parent_record)
4834
{
4935
int i;

src/proxy/ParentSelection.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ parseHashAlgorithm(std::string_view name)
6969
}
7070
}
7171

72+
std::unique_ptr<ATSHash64>
73+
createHashInstance(ParentHashAlgorithm algo, uint64_t seed0, uint64_t seed1)
74+
{
75+
switch (algo) {
76+
case ParentHashAlgorithm::SIPHASH24:
77+
return std::make_unique<ATSHash64Sip24>(seed0, seed1);
78+
case ParentHashAlgorithm::SIPHASH13:
79+
return std::make_unique<ATSHash64Sip13>(seed0, seed1);
80+
default:
81+
Warning("Unknown hash algorithm %d, using SipHash-2-4", static_cast<int>(algo));
82+
return std::make_unique<ATSHash64Sip24>(seed0, seed1);
83+
}
84+
}
85+
7286
ParentSelectionPolicy::ParentSelectionPolicy()
7387
{
7488
int32_t retry_time = 0;

src/proxy/http/remap/NextHopConsistentHash.cc

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "iocore/utils/Machine.h"
2828
#include "tsutil/YamlCfg.h"
2929
#include "proxy/http/remap/NextHopConsistentHash.h"
30+
#include "proxy/ParentSelection.h"
3031

3132
// hash_key strings.
3233
constexpr std::string_view hash_key_url = "url";
@@ -57,16 +58,10 @@ std::shared_ptr<HostRecord>
5758
NextHopConsistentHash::chashLookup(const std::shared_ptr<ATSConsistentHash> &ring, uint32_t cur_ring, ParentResult &result,
5859
HttpRequestData &request_info, bool *wrapped, uint64_t sm_id)
5960
{
60-
uint64_t hash_key = 0;
61-
// Create hash instance based on configured algorithm and seeds
62-
std::unique_ptr<ATSHash64> hash;
63-
if (hash_algorithm == "siphash13") {
64-
hash = std::make_unique<ATSHash64Sip13>(hash_seed0, hash_seed1);
65-
} else {
66-
hash = std::make_unique<ATSHash64Sip24>(hash_seed0, hash_seed1);
67-
}
68-
HostRecord *host_rec = nullptr;
69-
ATSConsistentHashIter *iter = &result.chashIter[cur_ring];
61+
uint64_t hash_key = 0;
62+
std::unique_ptr<ATSHash64> hash = createHashInstance(parseHashAlgorithm(hash_algorithm), hash_seed0, hash_seed1);
63+
HostRecord *host_rec = nullptr;
64+
ATSConsistentHashIter *iter = &result.chashIter[cur_ring];
7065

7166
if (result.chash_init[cur_ring] == false) {
7267
hash_key = getHashKey(sm_id, request_info, hash.get());
@@ -196,13 +191,7 @@ NextHopConsistentHash::NextHopConsistentHash(const std::string_view name, const
196191
"', this strategy will be ignored.");
197192
}
198193

199-
// Create the hash instance based on configured algorithm and seeds
200-
if (hash_algorithm == "siphash13") {
201-
hash = std::make_unique<ATSHash64Sip13>(hash_seed0, hash_seed1);
202-
} else {
203-
// Default to siphash24
204-
hash = std::make_unique<ATSHash64Sip24>(hash_seed0, hash_seed1);
205-
}
194+
hash = createHashInstance(parseHashAlgorithm(hash_algorithm), hash_seed0, hash_seed1);
206195

207196
// load up the hash rings.
208197
for (uint32_t i = 0; i < groups; i++) {

src/proxy/http/remap/unit-tests/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,13 @@ target_compile_definitions(
201201
target_include_directories(test_NextHopStrategyFactory PRIVATE ${PROJECT_SOURCE_DIR}/tests/include)
202202

203203
target_link_libraries(
204-
test_NextHopStrategyFactory PRIVATE Catch2::Catch2WithMain ts::hdrs ts::inkutils tscore libswoc::libswoc
205-
yaml-cpp::yaml-cpp
204+
test_NextHopStrategyFactory
205+
PRIVATE Catch2::Catch2WithMain
206+
ts::hdrs
207+
ts::inkutils
208+
tscore
209+
libswoc::libswoc
210+
yaml-cpp::yaml-cpp
206211
)
207212

208213
add_catch2_test(NAME test_NextHopStrategyFactory COMMAND $<TARGET_FILE:test_NextHopStrategyFactory>)

src/proxy/http/remap/unit-tests/nexthop_test_stubs.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,30 @@ HostStatus::setHostStatus(const std::string_view host, TSHostStatus status, unsi
209209
NH_Dbg(DbgCtl{"next_hop"}, "setting host status for '%.*s' to %s", static_cast<int>(host.size()), host.data(),
210210
HostStatusNames[status]);
211211
}
212+
213+
// Stub implementations for Parent Selection hash utilities
214+
#include "proxy/ParentSelection.h"
215+
#include "tscore/Hash.h"
216+
217+
ParentHashAlgorithm
218+
parseHashAlgorithm(std::string_view name)
219+
{
220+
if (name == "siphash13") {
221+
return ParentHashAlgorithm::SIPHASH13;
222+
} else {
223+
return ParentHashAlgorithm::SIPHASH24;
224+
}
225+
}
226+
227+
std::unique_ptr<ATSHash64>
228+
createHashInstance(ParentHashAlgorithm algo, uint64_t seed0, uint64_t seed1)
229+
{
230+
switch (algo) {
231+
case ParentHashAlgorithm::SIPHASH24:
232+
return std::make_unique<ATSHash64Sip24>(seed0, seed1);
233+
case ParentHashAlgorithm::SIPHASH13:
234+
return std::make_unique<ATSHash64Sip13>(seed0, seed1);
235+
default:
236+
return std::make_unique<ATSHash64Sip24>(seed0, seed1);
237+
}
238+
}

0 commit comments

Comments
 (0)