Statistics
| Branch: | Revision:

grapes / src / PeerSampler / ncast.c @ 176b8de8

History | View | Annotate | Download (9.17 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 res, max_timestamp;
100

    
101
  context = ncast_context_init();
102
  if (!context) return NULL;
103

    
104
  cfg_tags = grapes_config_parse(config);
105
  res = grapes_config_value_int_default(cfg_tags, "cache_size", &context->cache_size, DEFAULT_CACHE_SIZE);
106
  res = grapes_config_value_int_default(cfg_tags, "max_timestamp", &max_timestamp, DEFAULT_MAX_TIMESTAMP);
107
  res = grapes_config_value_int_default(cfg_tags, "period", &context->period, DEFAULT_PERIOD);
108
  res = grapes_config_value_int_default(cfg_tags, "bootstrap_period", &context->bootstrap_period, DEFAULT_BOOTSTRAP_PERIOD);
109
  res = grapes_config_value_int_default(cfg_tags, "bootstrap_cycles", &context->bootstrap_cycles, DEFAULT_BOOTSTRAP_CYCLES);
110
  res = grapes_config_value_int_default(cfg_tags, "adaptive", &context->adaptive, plus_features);
111
  res = grapes_config_value_int_default(cfg_tags, "restart", &context->restart, plus_features);
112
  res = grapes_config_value_int_default(cfg_tags, "randomize", &context->randomize, plus_features);
113
  res = 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
    int ret = INT_MIN;
210
    int i;
211
    int entries = cache_entries(context->local_cache);
212

    
213
    if (context->bootstrap_node &&
214
        (entries <= context->cache_size_threshold) &&
215
        (cache_pos(context->local_cache, context->bootstrap_node) < 0)) {
216
      cache_add(context->local_cache, context->bootstrap_node, NULL, 0);
217
          entries = cache_entries(context->local_cache);
218
    }
219

    
220
    context->query_tokens++;
221
    if (context->reply_tokens++ > 0) {//on average one reply is sent, if not, do something
222
      context->query_tokens += context->reply_tokens;
223
      context->reply_tokens = 0;
224
    }
225
    if (context->query_tokens > entries) context->query_tokens = entries;        //don't be too aggressive
226

    
227
    cache_update(context->local_cache);
228
    for (i = 0; i < context->query_tokens; i++) {
229
      int r;
230

    
231
      r = ncast_query(context->tc, context->local_cache);
232
      r = r > ret ? r : ret;
233
    }
234
  }
235
  return 0;
236
}
237

    
238
static const struct nodeID *const*ncast_get_neighbourhood(struct peersampler_context *context, int *n)
239
{
240
  context->r = realloc(context->r, context->cache_size * sizeof(struct nodeID *));
241
  if (context->r == NULL) {
242
    return NULL;
243
  }
244

    
245
  for (*n = 0; nodeid(context->local_cache, *n) && (*n < context->cache_size); (*n)++) {
246
    context->r[*n] = nodeid(context->local_cache, *n);
247
    //fprintf(stderr, "Checking table[%d]\n", *n);
248
  }
249

    
250
  return context->r;
251
}
252

    
253
static const void *ncast_get_metadata(struct peersampler_context *context, int *metadata_size)
254
{
255
  return get_metadata(context->local_cache, metadata_size);
256
}
257

    
258
static int ncast_grow_neighbourhood(struct peersampler_context *context, int n)
259
{
260
  context->cache_size += n;
261
  cache_size_threshold_init(context);
262

    
263
  return context->cache_size;
264
}
265

    
266
static int ncast_shrink_neighbourhood(struct peersampler_context *context, int n)
267
{
268
  if (context->cache_size < n) {
269
    return -1;
270
  }
271
  context->cache_size -= n;
272
  cache_size_threshold_init(context);
273

    
274
  return context->cache_size;
275
}
276

    
277
static int ncast_remove_neighbour(struct peersampler_context *context, const struct nodeID *neighbour)
278
{
279
  return cache_del(context->local_cache, neighbour);
280
}
281

    
282
struct peersampler_iface ncast = {
283
  .init = ncast_init,
284
  .change_metadata = ncast_change_metadata,
285
  .add_neighbour = ncast_add_neighbour,
286
  .parse_data = ncast_parse_data,
287
  .get_neighbourhood = ncast_get_neighbourhood,
288
  .get_metadata = ncast_get_metadata,
289
  .grow_neighbourhood = ncast_grow_neighbourhood,
290
  .shrink_neighbourhood = ncast_shrink_neighbourhood,
291
  .remove_neighbour = ncast_remove_neighbour,
292
};
293

    
294
struct peersampler_iface ncastplus = {
295
  .init = ncastplus_init,
296
  .change_metadata = ncast_change_metadata,
297
  .add_neighbour = ncast_add_neighbour,
298
  .parse_data = ncast_parse_data,
299
  .get_neighbourhood = ncast_get_neighbourhood,
300
  .get_metadata = ncast_get_metadata,
301
  .grow_neighbourhood = ncast_grow_neighbourhood,
302
  .shrink_neighbourhood = ncast_shrink_neighbourhood,
303
  .remove_neighbour = ncast_remove_neighbour,
304
};