-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathconfiguration.cc
284 lines (241 loc) · 6.1 KB
/
configuration.cc
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* ===============================================================
* Description: Weaver cluster configuration implementation.
*
* Created: 2014-02-10 14:08:44
*
* Author: Robert Escriva, [email protected]
* Ayush Dubey, [email protected]
*
* Copyright (C) 2013, Cornell University, see the LICENSE file
* for licensing agreement
* ===============================================================
*/
// Most of the following code has been 'borrowed' from
// Robert Escriva's HyperDex coordinator.
// see https://github.com/rescrv/HyperDex for the original code.
#define __STDC_LIMIT_MACROS
// STL
#include <algorithm>
#include <sstream>
#include <unordered_set>
// e
#include <e/serialization.h>
// HyperDex
#include "common/configuration.h"
configuration :: configuration()
: m_cluster(0)
, m_version(0)
, m_flags(0)
, m_servers()
{
refill_cache();
}
configuration :: configuration(const configuration& other)
: m_cluster(other.m_cluster)
, m_version(other.m_version)
, m_flags(other.m_flags)
, m_servers(other.m_servers)
{
refill_cache();
}
configuration :: ~configuration() throw ()
{
}
uint64_t
configuration :: cluster() const
{
return m_cluster;
}
uint64_t
configuration :: version() const
{
return m_version;
}
void
configuration :: get_all_addresses(std::vector<std::pair<server_id, po6::net::location> >* addrs) const
{
addrs->resize(m_servers.size());
for (size_t i = 0; i < m_servers.size(); ++i)
{
(*addrs)[i].first = m_servers[i].id;
(*addrs)[i].second = m_servers[i].bind_to;
}
}
bool
configuration :: exists(const server_id& id) const
{
for (size_t i = 0; i < m_servers.size(); ++i)
{
if (m_servers[i].id == id)
{
return true;
}
}
return false;
}
po6::net::location
configuration :: get_address(const server_id& id) const
{
for (size_t i = 0; i < m_servers.size(); ++i)
{
if (m_servers[i].id == id)
{
return m_servers[i].bind_to;
}
}
return po6::net::location();
}
server::state_t
configuration :: get_state(const server_id& id) const
{
for (size_t i = 0; i < m_servers.size(); ++i)
{
if (m_servers[i].id == id)
{
return m_servers[i].state;
}
}
return server::KILLED;
}
uint64_t
configuration :: get_weaver_id(const server_id &id) const
{
for (size_t i = 0; i < m_servers.size(); ++i)
{
if (m_servers[i].id == id)
{
return m_servers[i].weaver_id;
}
}
return UINT64_MAX;
}
uint64_t
configuration :: get_virtual_id(const server_id &id) const
{
for (size_t i = 0; i < m_servers.size(); ++i)
{
if (m_servers[i].id == id)
{
return m_servers[i].virtual_id;
}
}
return UINT64_MAX;
}
server::type_t
configuration :: get_type(const server_id &id) const
{
for (size_t i = 0; i < m_servers.size(); ++i)
{
if (m_servers[i].id == id) {
assert(m_servers[i].type != server::UNDEF);
return m_servers[i].type;
}
}
return server::UNDEF;
}
bool
sort_servers(const server &lhs, const server &rhs)
{
if (lhs.weaver_id == rhs.weaver_id) {
return lhs.id < rhs.id;
} else {
return lhs.weaver_id < rhs.weaver_id;
}
}
std::vector<server>
configuration :: get_servers() const
{
auto servers = m_servers;
std::sort(servers.begin(), servers.end(), sort_servers);
return servers;
}
std::string
configuration :: dump() const
{
std::ostringstream out;
out << "cluster " << m_cluster << "\n";
out << "version " << m_version << "\n";
out << "flags " << std::hex << m_flags << std::dec << "\n";
for (size_t i = 0; i < m_servers.size(); ++i)
{
out << "server "
<< m_servers[i].id.get() << " "
<< m_servers[i].bind_to << " "
<< server::to_string(m_servers[i].state) << "\n";
}
return out.str();
}
configuration&
configuration :: operator = (const configuration& rhs)
{
if (this == &rhs)
{
return *this;
}
m_cluster = rhs.m_cluster;
m_version = rhs.m_version;
m_flags = rhs.m_flags;
m_servers = rhs.m_servers;
refill_cache();
return *this;
}
// change to go from this configuration to other configuration
// the change should be returned in the form of a vector of servers whose state
// matches other
// does not include servers which are in this but not in other
// does include servers which are in other but not in this
std::vector<server>
configuration :: delta(const configuration &other)
{
std::vector<server> other_servers = other.get_servers();
size_t search_hint = 0;
std::vector<server> delta;
std::unordered_set<uint64_t> seen;
for (const server &this_srv: m_servers) {
for (size_t i = 0; i < other_servers.size(); i++) {
size_t idx = (search_hint + i) % other_servers.size();
const server &other_srv = other_servers[idx];
if (this_srv.id == other_srv.id) {
// found the server
if (this_srv != other_srv) {
delta.emplace_back(other_srv);
}
if (i == 0) {
search_hint++;
} else {
search_hint = 0;
}
break;
}
}
seen.emplace(this_srv.weaver_id);
}
for (const server &srv: other_servers) {
if (seen.find(srv.weaver_id) == seen.end()) {
delta.emplace_back(srv);
}
}
return delta;
}
void
configuration :: refill_cache()
{
std::sort(m_servers.begin(), m_servers.end());
}
e::unpacker
operator >> (e::unpacker up, configuration& c)
{
uint64_t num_servers;
up = up >> c.m_cluster >> c.m_version >> c.m_flags
>> num_servers;
c.m_servers.clear();
c.m_servers.reserve(num_servers);
for (size_t i = 0; !up.error() && i < num_servers; ++i)
{
server s;
up = up >> s;
c.m_servers.push_back(s);
}
return up;
}