-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdns_cache_sharded.cpp
More file actions
124 lines (117 loc) · 2.69 KB
/
dns_cache_sharded.cpp
File metadata and controls
124 lines (117 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <memory>
#include <string>
#include "lru_cache.h"
class DNSCache
{
struct impl_t
{
impl_t(size_t max_size)
: cache(max_size)
{}
//
void update(const std::string& name, const std::string& ip)
{
std::lock_guard<std::mutex> lock(mutex);
cache.set(name, ip);
}
std::string resolve(const std::string& name)
{
std::string ip;
{
std::lock_guard<std::mutex> lock(mutex);
cache.get(name, ip);
}
return ip;
}
//
lru_cache_t<std::string, std::string> cache;
std::mutex mutex;
};
// shard whole cache into pieces in order to reduce lock contention
const static int shards_count = 8;
// set local instance under lock
static void instance(size_t max_size, int shard, std::shared_ptr<impl_t> &impl)
{
static std::mutex _mutex;
//
std::lock_guard<std::mutex> lock(_mutex);
//
static std::shared_ptr<impl_t> _impl[shards_count];
if (!_impl[shard])
{
_impl[shard] = std::make_shared<impl_t>(max_size);
}
impl = _impl[shard];
}
// make local copy in order to eliminate frequent calls to instance(...)
std::shared_ptr<impl_t> _impl[shards_count];
// shard uqing FNV32 hash
std::shared_ptr<impl_t> get_shard(const std::string& name)
{
const unsigned FNV_32_PRIME = 0x01000193;
unsigned int hval = 0x811c9dc5; // FNV0 hval = 0
for(size_t i = 0, size = name.size(); i < size; i++)
{
hval *= FNV_32_PRIME;
hval ^= (unsigned int) name[i];
}
//
return _impl[hval % shards_count];
}
public:
explicit DNSCache(size_t max_size)
{
double size = 0, delta = 1. * max_size / shards_count;
for(int i = 0; i < shards_count; i++)
{
int piece = int(size + delta) - int(size);
if (piece < 1) piece = 1;
size += delta;
//
instance(piece, i, _impl[i]);
}
}
void update(const std::string& name, const std::string& ip)
{
get_shard(name)->update(name, ip);
}
std::string resolve(const std::string& name)
{
return get_shard(name)->resolve(name);
}
};
int main(int argc, char* argv[])
{
int num_threads = 1;
if (argc > 1)
num_threads = atoi(argv[1]);
const size_t max_size = 1000;
// generate test strings
const size_t max_strings = 3 * max_size;
std::string strings[max_strings];
for(int i = 0; i < max_strings; i++)
{
strings[i] = std::to_string(i);
}
std::vector<std::thread> threads;
for(int k = 0; k < num_threads; k++)
{
threads.push_back(std::thread([&](){
DNSCache cache(max_size);
for(int i = 0; i < 100000; i++)
{
cache.update(strings[i % max_strings], strings[i % max_strings]);
auto ip = cache.resolve(strings[(i + max_size/3) % max_strings]);
}
});
}
for (auto &th : threads)
{
th.join();
}
return 0;
}