Statistics
| Branch: | Revision:

grapes / src / PeerSampler / cloudcast.c @ c8adcc6c

History | View | Annotate | Download (11.7 KB)

1
/*
2
 *  Copyright (c) 2010 Andrea Zito
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 *
6
 * Implementation of the Cloudcast peer sampling protocol.
7
 *
8
 * If thread are used calls to psample_init which result in calls to
9
 * cloudcast_init must be syncronized with calls to cloud_helper_init
10
 * and get_cloud_helper_for.
11
 */
12

    
13
#include <sys/time.h>
14
#include <time.h>
15
#include <stdlib.h>
16
#include <stdint.h>
17
#include <stdio.h>
18
#include <stdbool.h>
19
#include <string.h>
20

    
21
#include "net_helper.h"
22
#include "peersampler_iface.h"
23
#include "../Cache/topocache.h"
24
#include "../Cache/cloudcast_proto.h"
25
#include "../Cache/proto.h"
26
#include "config.h"
27
#include "grapes_msg_types.h"
28

    
29
#define DEFAULT_CACHE_SIZE 10
30
#define DEFAULT_CLOUD_CONTACT_TRESHOLD 4000000
31

    
32
struct peersampler_context{
33
  uint64_t currtime;
34
  int cache_size;
35
  int sent_entries;
36
  int keep_cache_full;
37
  struct peer_cache *local_cache;
38
  bool bootstrap;
39
  int bootstrap_period;
40
  int period;
41

    
42
  int cloud_contact_treshold;
43
  struct nodeID *local_node;
44
  struct nodeID **cloud_nodes;
45

    
46
  struct peer_cache *flying_cache;
47
  struct nodeID *dst;
48

    
49
  struct cloudcast_proto_context *proto_context;
50
  struct nodeID **r;
51
};
52

    
53

    
54
static uint64_t gettime(void)
55
{
56
  struct timeval tv;
57

    
58
  gettimeofday(&tv, NULL);
59

    
60
  return tv.tv_usec + tv.tv_sec * 1000000ull;
61
}
62

    
63
static struct peersampler_context* cloudcast_context_init(void){
64
  struct peersampler_context* con;
65
  con = (struct peersampler_context*) calloc(1,sizeof(struct peersampler_context));
66
  if (!con) {
67
    fprintf(stderr, "cloudcast: Error! could not create context. ENOMEM\n");
68
    return NULL;
69
  }
70
  memset(con, 0, sizeof(struct peersampler_context));
71

    
72
  //Initialize context with default values
73
  con->bootstrap = true;
74
  con->bootstrap_period = 2000000;
75
  con->period = 10000000;
76
  con->currtime = gettime();
77
  con->r = NULL;
78
  con->cloud_contact_treshold = DEFAULT_CLOUD_CONTACT_TRESHOLD;
79

    
80
  return con;
81
}
82

    
83
static int time_to_send(struct peersampler_context* con)
84
{
85
  int p = con->bootstrap ? con->bootstrap_period : con->period;
86
  if (gettime() - con->currtime > p) {
87
    con->currtime += p;
88

    
89
    return 1;
90
  }
91

    
92
  return 0;
93
}
94

    
95
static void cache_add_cache(struct peer_cache *dst, const struct peer_cache *add)
96
{
97
  int i, meta_size;
98
  const uint8_t *meta;
99

    
100
  meta = get_metadata(add, &meta_size);
101
  for (i = 0; nodeid(add, i); i++) {
102
    cache_add(dst,  nodeid(add, i), meta + (meta_size * i), meta_size);
103
  }
104
}
105

    
106
static int cache_add_resize(struct peer_cache *dst, struct nodeID *n, const int cache_max_size)
107
{
108
  int err;
109
  err = cache_add(dst, n, NULL, 0);
110
  if (err == -2){
111
    // maybe the cache is full... try resizing it to cache_max
112
    cache_resize(dst, cache_max_size);
113
    err = cache_add(dst, n, NULL, 0);
114
  }
115
  return (err > 0)? 0 : 1;
116
}
117

    
118
/*
119
 * Public Functions!
120
 */
121
static struct peersampler_context* cloudcast_init(struct nodeID *myID, const void *metadata, int metadata_size, const char *config)
122
{
123
  struct tag *cfg_tags;
124
  struct peersampler_context *con;
125
  int res;
126

    
127
  con = cloudcast_context_init();
128
  if (!con) return NULL;
129
  con->local_node = myID;
130

    
131
  cfg_tags = config_parse(config);
132
  res = config_value_int(cfg_tags, "cache_size", &(con->cache_size));
133
  if (!res) {
134
    con->cache_size = DEFAULT_CACHE_SIZE;
135
  }
136
  res = config_value_int(cfg_tags, "sent_entries", &(con->sent_entries));
137
  if (!res) {
138
    con->sent_entries = con->cache_size / 2;
139
  }
140
  res = config_value_int(cfg_tags, "keep_cache_full", &(con->keep_cache_full));
141
  if (!res) {
142
    con->keep_cache_full = 1;
143
  }
144

    
145
  con->local_cache = cache_init(con->cache_size, metadata_size, 0);
146
  if (con->local_cache == NULL) {
147
    fprintf(stderr, "cloudcast: Error initializing local cache\n");
148
    free(con);
149
    return NULL;
150
  }
151

    
152
  con->proto_context = cloudcast_proto_init(myID, metadata, metadata_size);
153
  if (!con->proto_context){
154
    free(con->local_cache);
155
    free(con);
156
    return NULL;
157
  }
158
  con->cloud_nodes = cloudcast_get_cloud_nodes(con->proto_context, 2);
159

    
160
  return con;
161
}
162

    
163
static int cloudcast_add_neighbour(struct peersampler_context *context, struct nodeID *neighbour, const void *metadata, int metadata_size)
164
{
165
  if (!context->flying_cache) {
166
    context->flying_cache = rand_cache(context->local_cache, context->sent_entries - 1);
167
  }
168
  if (cache_add(context->local_cache, neighbour, metadata, metadata_size) < 0) {
169
    return -1;
170
  }
171

    
172
  if (cloudcast_is_cloud_node(context->proto_context, neighbour) == 0)
173
    return cloudcast_query_peer(context->proto_context, context->flying_cache, neighbour);
174
  else
175
    return cloudcast_query_cloud(context->proto_context);
176
}
177

    
178
static int cloudcast_parse_data(struct peersampler_context *context, const uint8_t *buff, int len)
179
{
180
  cache_check(context->local_cache);
181
  if (len) {
182
    // Passive thread
183
    const struct topo_header *h = (const struct topo_header *)buff;
184
    struct peer_cache *remote_cache  = NULL;
185
    struct peer_cache *sent_cache = NULL;
186

    
187
    if (h->protocol != MSG_TYPE_TOPOLOGY) {
188
      fprintf(stderr, "Peer Sampler: Wrong protocol!\n");
189

    
190
      return -1;
191
    }
192

    
193
    context->bootstrap = false;
194

    
195
    if (len - sizeof(struct topo_header) > 0)
196
      remote_cache = entries_undump(buff + sizeof(struct topo_header), len - sizeof(struct topo_header));
197

    
198

    
199
    if (h->type == CLOUDCAST_QUERY) {
200
      sent_cache = rand_cache(context->local_cache, context->sent_entries);
201
      cloudcast_reply_peer(context->proto_context, remote_cache, sent_cache);
202
      context->dst = NULL;
203
    } else if (h->type == CLOUDCAST_CLOUD) {
204
      int g = 0;
205
      int i = 0;
206
      struct peer_cache *reply_cache = NULL;
207

    
208
      // Obtain the timestamp reported by the last query_cloud operation
209
      uint64_t cloud_time_stamp = cloudcast_timestamp_cloud(context->proto_context) * 1000000ull;
210
      uint64_t current_time = gettime();
211
      int delta = current_time - cloud_time_stamp;
212

    
213
      // local view with one spot free for the local nodeID
214
      sent_cache = rand_cache(context->local_cache, context->sent_entries - 1);
215
      for (i=0; i<2; i++) cache_del(sent_cache, context->cloud_nodes[i]);
216

    
217
      if (remote_cache == NULL) {
218
        // Cloud is not initialized, just set the cloud cache to send_cache
219
        cloudcast_reply_cloud(context->proto_context, sent_cache);
220
        remote_cache = cloudcast_cloud_default_reply(context->local_cache, context->cloud_nodes[0]);
221
      } else {
222
        // Locally perform exchange of cache entries
223
        int err;
224
        struct peer_cache *cloud_cache = NULL;
225

    
226
        // if the cloud contact frequency *IS NOT* too high save an entry for the cloud
227
        if (delta > (context->period / context->cloud_contact_treshold)) g++;
228

    
229
        // if the cloud contact frequency *IS* too low save another entry for the cloud
230
        if (delta > (context->cloud_contact_treshold * context->period)) g++;
231

    
232
        // Remove mentions of local node from the cache
233
        cache_del(remote_cache, context->local_node);
234

    
235
        // cloud view with g spot free for cloud nodeID
236
        reply_cache = rand_cache(remote_cache, context->sent_entries - g);
237

    
238
        // building new cloud view
239
        cloud_cache = merge_caches(remote_cache, sent_cache, context->cache_size, &err);
240
        free(remote_cache);
241
        cache_add_cache(cloud_cache, sent_cache);
242
        if (context->keep_cache_full){
243
          cache_add_cache(cloud_cache, reply_cache);
244
        }
245

    
246
        err = cloudcast_reply_cloud(context->proto_context, cloud_cache);
247
        free(cloud_cache);
248

    
249
        //Add the actual cloud nodes to the cache
250
        for (i = 0; i<g; i++){
251
          if (cache_add_resize(reply_cache, context->cloud_nodes[i], context->sent_entries) != 0){
252
            fprintf(stderr, "WTF!!! cannot add to cache\n");
253
            return 1;
254
          }
255
        }
256

    
257
        remote_cache = reply_cache;
258
        context->dst = NULL;
259
      }
260
    }
261
    cache_check(context->local_cache);
262

    
263
    if (remote_cache){
264
      // Remote cache may be NULL due to a non initialize cloud
265
      cache_add_cache(context->local_cache, remote_cache);
266
      cache_free(remote_cache);
267
    }
268
    if (sent_cache) {
269
      if (context->keep_cache_full)
270
        cache_add_cache(context->local_cache, sent_cache);
271
      cache_free(sent_cache);
272
    } else {
273
      if (context->flying_cache) {
274
        cache_add_cache(context->local_cache, context->flying_cache);
275
        cache_free(context->flying_cache);
276
        context->flying_cache = NULL;
277
      }
278
    }
279
  }
280

    
281
  // Active thread
282
  if (time_to_send(context)) {
283
    if (context->flying_cache) {
284
      cache_add_cache(context->local_cache, context->flying_cache);
285
      cache_free(context->flying_cache);
286
      context->flying_cache = NULL;
287
    }
288
    cache_update(context->local_cache);
289
    context->dst = last_peer(context->local_cache);
290
    if (context->dst == NULL) {
291
      // If we remain without neighbors, forcely add a cloud entry
292
      context->dst = cloudcast_get_cloud_node(context->proto_context);
293
      cache_add(context->local_cache, context->dst, NULL, 0);
294
    }
295
    context->dst = nodeid_dup(context->dst);
296
    cache_del(context->local_cache, context->dst);
297

    
298
    if (cloudcast_is_cloud_node(context->proto_context, context->dst)) {
299
      int err;
300
      err = cloudcast_query_cloud(context->proto_context);
301
    } else {
302
      context->flying_cache = rand_cache(context->local_cache, context->sent_entries - 1);
303
      cloudcast_query_peer(context->proto_context, context->flying_cache, context->dst);
304
    }
305
  }
306
  cache_check(context->local_cache);
307

    
308
  return 0;
309
  }
310

    
311
static const struct nodeID **cloudcast_get_neighbourhood(struct peersampler_context *context, int *n)
312
{
313
  context->r = realloc(context->r, context->cache_size * sizeof(struct nodeID *));
314
  if (context->r == NULL) {
315
    return NULL;
316
  }
317

    
318
  for (*n = 0; nodeid(context->local_cache, *n) && (*n < context->cache_size); (*n)++) {
319
    context->r[*n] = nodeid(context->local_cache, *n);
320
    //fprintf(stderr, "Checking table[%d]\n", *n);
321
  }
322
  if (context->flying_cache) {
323
    int i,j,dup;
324

    
325
    for (i = 0; nodeid(context->flying_cache, i) && (*n < context->cache_size); (*n)++, i++) {
326
      dup = 0;
327
      for (j = 0; j<*n; j++){
328
        if (nodeid_equal(context->r[j], nodeid(context->flying_cache, i))){
329
          dup = 1;
330
          continue;
331
        }
332
      }
333
      if (dup) (*n)--;
334
      else context->r[*n] = nodeid(context->flying_cache, i);
335
    }
336
  }
337

    
338
  if (context->dst && (*n < context->cache_size)) {
339
    int j,dup;
340
    dup = 0;
341
    for (j = 0; j<*n; j++){
342
      if (nodeid_equal(context->r[j], context->dst)){
343
        dup = 1;
344
        continue;
345
      }
346
    }
347
    if (!dup){
348
      context->r[*n] = context->dst;
349
      (*n)++;
350
    }
351
  }
352

    
353
  return (const struct nodeID **)context->r;
354
}
355

    
356
static const void *cloudcast_get_metadata(struct peersampler_context *context, int *metadata_size)
357
{
358
  return get_metadata(context->local_cache, metadata_size);
359
}
360

    
361
static int cloudcast_grow_neighbourhood(struct peersampler_context *context, int n)
362
{
363
  context->cache_size += n;
364

    
365
  return context->cache_size;
366
}
367

    
368
static int cloudcast_shrink_neighbourhood(struct peersampler_context *context, int n)
369
{
370
  if (context->cache_size < n) {
371
    return -1;
372
  }
373
  context->cache_size -= n;
374

    
375
  return context->cache_size;
376
}
377

    
378
static int cloudcast_remove_neighbour(struct peersampler_context *context, const struct nodeID *neighbour)
379
{
380
  return cache_del(context->local_cache, neighbour);
381
}
382

    
383
static int cloudcast_change_metadata(struct peersampler_context *context, const void *metadata, int metadata_size)
384
{
385
  return cloudcast_proto_change_metadata(context->proto_context, metadata, metadata_size);
386
}
387

    
388
struct peersampler_iface cloudcast = {
389
  .init = cloudcast_init,
390
  .change_metadata = cloudcast_change_metadata,
391
  .add_neighbour = cloudcast_add_neighbour,
392
  .parse_data = cloudcast_parse_data,
393
  .get_neighbourhood = cloudcast_get_neighbourhood,
394
  .get_metadata = cloudcast_get_metadata,
395
  .grow_neighbourhood = cloudcast_grow_neighbourhood,
396
  .shrink_neighbourhood = cloudcast_shrink_neighbourhood,
397
  .remove_neighbour = cloudcast_remove_neighbour,
398
};