grapes / src / PeerSampler / ncast.c @ 10ddaca7
History | View | Annotate | Download (9.58 KB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2010 Luca Abeni
|
3 |
*
|
4 |
* This is free software; see lgpl-2.1.txt
|
5 |
*/
|
6 |
|
7 |
#include <sys/time.h> |
8 |
#include <time.h> |
9 |
#include <stdlib.h> |
10 |
#include <stdint.h> |
11 |
#include <stdio.h> |
12 |
#include <stdbool.h> |
13 |
#include <string.h> |
14 |
#include <limits.h> |
15 |
|
16 |
#include "net_helper.h" |
17 |
#include "peersampler_iface.h" |
18 |
#include "../Cache/topocache.h" |
19 |
#include "../Cache/ncast_proto.h" |
20 |
#include "../Cache/proto.h" |
21 |
#include "grapes_config.h" |
22 |
#include "grapes_msg_types.h" |
23 |
|
24 |
#define DEFAULT_CACHE_SIZE 10 |
25 |
#define DEFAULT_MAX_TIMESTAMP 5 |
26 |
#define DEFAULT_BOOTSTRAP_CYCLES 5 |
27 |
#define DEFAULT_BOOTSTRAP_PERIOD 2*1000*1000 |
28 |
#define DEFAULT_PERIOD 10*1000*1000 |
29 |
|
30 |
struct peersampler_context{
|
31 |
uint64_t currtime; |
32 |
int cache_size;
|
33 |
int cache_size_threshold;
|
34 |
struct peer_cache *local_cache;
|
35 |
bool bootstrap;
|
36 |
struct nodeID *bootstrap_node;
|
37 |
int bootstrap_period;
|
38 |
int bootstrap_cycles;
|
39 |
int period;
|
40 |
int counter;
|
41 |
struct ncast_proto_context *tc;
|
42 |
const struct nodeID **r; |
43 |
int query_tokens;
|
44 |
int reply_tokens;
|
45 |
int first_ts;
|
46 |
int adaptive;
|
47 |
int restart;
|
48 |
int randomize;
|
49 |
int slowstart;
|
50 |
}; |
51 |
|
52 |
static uint64_t gettime(void) |
53 |
{ |
54 |
struct timeval tv;
|
55 |
|
56 |
gettimeofday(&tv, NULL);
|
57 |
|
58 |
return tv.tv_usec + tv.tv_sec * 1000000ull; |
59 |
} |
60 |
|
61 |
static struct peersampler_context* ncast_context_init(void) |
62 |
{ |
63 |
struct peersampler_context* con;
|
64 |
con = (struct peersampler_context*) calloc(1,sizeof(struct peersampler_context)); |
65 |
|
66 |
//Initialize context with default values
|
67 |
con->bootstrap = true;
|
68 |
con->bootstrap_node = NULL;
|
69 |
con->currtime = gettime(); |
70 |
con->r = NULL;
|
71 |
|
72 |
return con;
|
73 |
} |
74 |
|
75 |
static int time_to_send(struct peersampler_context *context) |
76 |
{ |
77 |
int p = context->bootstrap ? context->bootstrap_period : context->period;
|
78 |
if (gettime() - context->currtime > p) {
|
79 |
context->currtime += p; |
80 |
|
81 |
return 1; |
82 |
} |
83 |
|
84 |
return 0; |
85 |
} |
86 |
|
87 |
static void cache_size_threshold_init(struct peersampler_context* context) |
88 |
{ |
89 |
context->cache_size_threshold = (context->cache_size - 1 / 2); |
90 |
} |
91 |
|
92 |
/*
|
93 |
* Exported Functions!
|
94 |
*/
|
95 |
static struct peersampler_context* init(struct nodeID *myID, const void *metadata, int metadata_size, const char *config, int plus_features) |
96 |
{ |
97 |
struct tag *cfg_tags;
|
98 |
struct peersampler_context *context;
|
99 |
int max_timestamp;
|
100 |
|
101 |
context = ncast_context_init(); |
102 |
if (!context) return NULL; |
103 |
|
104 |
cfg_tags = grapes_config_parse(config); |
105 |
grapes_config_value_int_default(cfg_tags, "cache_size", &context->cache_size, DEFAULT_CACHE_SIZE);
|
106 |
grapes_config_value_int_default(cfg_tags, "max_timestamp", &max_timestamp, DEFAULT_MAX_TIMESTAMP);
|
107 |
grapes_config_value_int_default(cfg_tags, "period", &context->period, DEFAULT_PERIOD);
|
108 |
grapes_config_value_int_default(cfg_tags, "bootstrap_period", &context->bootstrap_period, DEFAULT_BOOTSTRAP_PERIOD);
|
109 |
grapes_config_value_int_default(cfg_tags, "bootstrap_cycles", &context->bootstrap_cycles, DEFAULT_BOOTSTRAP_CYCLES);
|
110 |
grapes_config_value_int_default(cfg_tags, "adaptive", &context->adaptive, plus_features);
|
111 |
grapes_config_value_int_default(cfg_tags, "restart", &context->restart, plus_features);
|
112 |
grapes_config_value_int_default(cfg_tags, "randomize", &context->randomize, plus_features);
|
113 |
grapes_config_value_int_default(cfg_tags, "slowstart", &context->slowstart, plus_features);
|
114 |
free(cfg_tags); |
115 |
|
116 |
context->local_cache = cache_init(context->cache_size, metadata_size, max_timestamp); |
117 |
if (context->local_cache == NULL) { |
118 |
free(context); |
119 |
return NULL; |
120 |
} |
121 |
|
122 |
cache_size_threshold_init(context); |
123 |
|
124 |
context->tc = ncast_proto_init(myID, metadata, metadata_size); |
125 |
if (!context->tc){
|
126 |
free(context->local_cache); |
127 |
free(context); |
128 |
return NULL; |
129 |
} |
130 |
|
131 |
context->query_tokens = 0;
|
132 |
context->reply_tokens = 0;
|
133 |
context->first_ts = (max_timestamp + 1) / 2; |
134 |
// increase timestamp for initial message, since that is out of the normal cycle of the bootstrap peer
|
135 |
ncast_proto_myentry_update(context->tc, NULL, context->first_ts, NULL, 0); |
136 |
|
137 |
return context;
|
138 |
} |
139 |
|
140 |
static int ncast_change_metadata(struct peersampler_context *context, const void *metadata, int metadata_size) |
141 |
{ |
142 |
if (ncast_proto_metadata_update(context->tc, metadata, metadata_size) <= 0) { |
143 |
return -1; |
144 |
} |
145 |
|
146 |
return 1; |
147 |
} |
148 |
|
149 |
static struct peersampler_context* ncast_init(struct nodeID *myID, const void *metadata, int metadata_size, const char *config) |
150 |
{ |
151 |
return init(myID, metadata, metadata_size, config, 0); |
152 |
} |
153 |
|
154 |
static struct peersampler_context* ncastplus_init(struct nodeID *myID, const void *metadata, int metadata_size, const char *config) |
155 |
{ |
156 |
return init(myID, metadata, metadata_size, config, 1); |
157 |
} |
158 |
|
159 |
static int ncast_add_neighbour(struct peersampler_context *context, struct nodeID *neighbour, const void *metadata, int metadata_size) |
160 |
{ |
161 |
if (cache_add(context->local_cache, neighbour, metadata, metadata_size) < 0) { |
162 |
return -1; |
163 |
} |
164 |
if (!context->bootstrap_node) { //save the first added nodeid as bootstrap nodeid |
165 |
context->bootstrap_node = nodeid_dup(neighbour); |
166 |
} |
167 |
return ncast_query_peer(context->tc, context->local_cache, neighbour);
|
168 |
} |
169 |
|
170 |
static int ncast_parse_data(struct peersampler_context *context, const uint8_t *buff, int len) |
171 |
{ |
172 |
int dummy;
|
173 |
|
174 |
if (len) {
|
175 |
const struct topo_header *h = (const struct topo_header *)buff; |
176 |
struct peer_cache *new, *remote_cache;
|
177 |
|
178 |
if (h->protocol != MSG_TYPE_TOPOLOGY) {
|
179 |
fprintf(stderr, "NCAST: Wrong protocol!\n");
|
180 |
|
181 |
return -1; |
182 |
} |
183 |
|
184 |
context->counter++; |
185 |
if (context->counter == context->bootstrap_cycles) {
|
186 |
context->bootstrap = false;
|
187 |
ncast_proto_myentry_update(context->tc, NULL , - context->first_ts, NULL, 0); // reset the timestamp of our own ID, we are in normal cycle, we will not disturb the algorithm |
188 |
} |
189 |
|
190 |
remote_cache = entries_undump(buff + sizeof(struct topo_header), len - sizeof(struct topo_header)); |
191 |
if (h->type == NCAST_QUERY) {
|
192 |
context->reply_tokens--; //sending a reply to someone who presumably receives it
|
193 |
cache_randomize(context->local_cache); |
194 |
ncast_reply(context->tc, remote_cache, context->local_cache); |
195 |
} else {
|
196 |
context->query_tokens--; //a query was successful
|
197 |
} |
198 |
cache_randomize(context->local_cache); |
199 |
cache_randomize(remote_cache); |
200 |
new = merge_caches(context->local_cache, remote_cache, context->cache_size, &dummy); |
201 |
cache_free(remote_cache); |
202 |
if (new != NULL) { |
203 |
cache_free(context->local_cache); |
204 |
context->local_cache = new; |
205 |
} |
206 |
} |
207 |
|
208 |
if (time_to_send(context)) {
|
209 |
//fprintf(stderr,"[DEBUG] Time to send a TOPO message\n");
|
210 |
int ret = INT_MIN;
|
211 |
int i;
|
212 |
int entries = cache_entries(context->local_cache);
|
213 |
|
214 |
if (context->bootstrap_node &&
|
215 |
(cache_entries(context->local_cache) <= context->cache_size_threshold) && |
216 |
(cache_pos(context->local_cache, context->bootstrap_node) < 0)) {
|
217 |
cache_add(context->local_cache, context->bootstrap_node, NULL, 0); |
218 |
} |
219 |
context->query_tokens++; |
220 |
if (context->reply_tokens++ > 0) {//on average one reply is sent, if not, do something |
221 |
context->query_tokens += context->reply_tokens; |
222 |
context->reply_tokens = 0;
|
223 |
} |
224 |
if (context->query_tokens > entries) context->query_tokens = entries; //don't be too aggressive |
225 |
|
226 |
cache_update(context->local_cache); |
227 |
for (i = 0; i < context->query_tokens; i++) { |
228 |
int r;
|
229 |
|
230 |
r = ncast_query(context->tc, context->local_cache); |
231 |
r = r > ret ? r : ret; |
232 |
} |
233 |
} |
234 |
return 0; |
235 |
} |
236 |
|
237 |
static const struct nodeID *const*ncast_get_neighbourhood(struct peersampler_context *context, int *n) |
238 |
{ |
239 |
context->r = realloc(context->r, context->cache_size * sizeof(struct nodeID *)); |
240 |
if (context->r == NULL) { |
241 |
return NULL; |
242 |
} |
243 |
|
244 |
for (*n = 0; nodeid(context->local_cache, *n) && (*n < context->cache_size); (*n)++) { |
245 |
context->r[*n] = nodeid(context->local_cache, *n); |
246 |
//fprintf(stderr, "Checking table[%d]\n", *n);
|
247 |
} |
248 |
|
249 |
return context->r;
|
250 |
} |
251 |
|
252 |
static const void *ncast_get_metadata(struct peersampler_context *context, int *metadata_size) |
253 |
{ |
254 |
return get_metadata(context->local_cache, metadata_size);
|
255 |
} |
256 |
|
257 |
static int ncast_grow_neighbourhood(struct peersampler_context *context, int n) |
258 |
{ |
259 |
context->cache_size += n; |
260 |
cache_size_threshold_init(context); |
261 |
|
262 |
return context->cache_size;
|
263 |
} |
264 |
|
265 |
static int ncast_shrink_neighbourhood(struct peersampler_context *context, int n) |
266 |
{ |
267 |
if (context->cache_size < n) {
|
268 |
return -1; |
269 |
} |
270 |
context->cache_size -= n; |
271 |
cache_size_threshold_init(context); |
272 |
|
273 |
return context->cache_size;
|
274 |
} |
275 |
|
276 |
static int ncast_remove_neighbour(struct peersampler_context *context, const struct nodeID *neighbour) |
277 |
{ |
278 |
return cache_del(context->local_cache, neighbour);
|
279 |
} |
280 |
|
281 |
void ncast_destroy(struct peersampler_context **context) |
282 |
{ |
283 |
if (context && *context)
|
284 |
{ |
285 |
if((*context)->r)
|
286 |
free((*context)->r); |
287 |
if((*context)->local_cache)
|
288 |
cache_free((*context)->local_cache); |
289 |
if((*context)->tc)
|
290 |
ncast_proto_destroy(&((*context)->tc)); |
291 |
if((*context)->bootstrap_node)
|
292 |
nodeid_free(((*context)->bootstrap_node)); |
293 |
free(*context); |
294 |
*context = NULL;
|
295 |
} |
296 |
} |
297 |
|
298 |
struct peersampler_iface ncast = {
|
299 |
.init = ncast_init, |
300 |
.destroy = ncast_destroy, |
301 |
.change_metadata = ncast_change_metadata, |
302 |
.add_neighbour = ncast_add_neighbour, |
303 |
.parse_data = ncast_parse_data, |
304 |
.get_neighbourhood = ncast_get_neighbourhood, |
305 |
.get_metadata = ncast_get_metadata, |
306 |
.grow_neighbourhood = ncast_grow_neighbourhood, |
307 |
.shrink_neighbourhood = ncast_shrink_neighbourhood, |
308 |
.remove_neighbour = ncast_remove_neighbour, |
309 |
}; |
310 |
|
311 |
struct peersampler_iface ncastplus = {
|
312 |
.init = ncastplus_init, |
313 |
.destroy = ncast_destroy, |
314 |
.change_metadata = ncast_change_metadata, |
315 |
.add_neighbour = ncast_add_neighbour, |
316 |
.parse_data = ncast_parse_data, |
317 |
.get_neighbourhood = ncast_get_neighbourhood, |
318 |
.get_metadata = ncast_get_metadata, |
319 |
.grow_neighbourhood = ncast_grow_neighbourhood, |
320 |
.shrink_neighbourhood = ncast_shrink_neighbourhood, |
321 |
.remove_neighbour = ncast_remove_neighbour, |
322 |
}; |