Statistics
| Branch: | Revision:

grapes / src / PeerSampler / cloudcast.c @ 43b793e3

History | View | Annotate | Download (11.4 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 int cache_add_resize(struct peer_cache *dst, struct nodeID *n, const int cache_max_size)
96
{
97
  int err;
98
  err = cache_add(dst, n, NULL, 0);
99
  if (err == -2){
100
    // maybe the cache is full... try resizing it to cache_max
101
    cache_resize(dst, cache_max_size);
102
    err = cache_add(dst, n, NULL, 0);
103
  }
104
  return (err > 0)? 0 : 1;
105
}
106

    
107
/*
108
 * Public Functions!
109
 */
110
static struct peersampler_context* cloudcast_init(struct nodeID *myID, const void *metadata, int metadata_size, const char *config)
111
{
112
  struct tag *cfg_tags;
113
  struct peersampler_context *con;
114
  int res;
115

    
116
  con = cloudcast_context_init();
117
  if (!con) return NULL;
118
  con->local_node = myID;
119

    
120
  cfg_tags = config_parse(config);
121
  res = config_value_int(cfg_tags, "cache_size", &(con->cache_size));
122
  if (!res) {
123
    con->cache_size = DEFAULT_CACHE_SIZE;
124
  }
125
  res = config_value_int(cfg_tags, "sent_entries", &(con->sent_entries));
126
  if (!res) {
127
    con->sent_entries = con->cache_size / 2;
128
  }
129
  res = config_value_int(cfg_tags, "keep_cache_full", &(con->keep_cache_full));
130
  if (!res) {
131
    con->keep_cache_full = 1;
132
  }
133

    
134
  con->local_cache = cache_init(con->cache_size, metadata_size, 0);
135
  if (con->local_cache == NULL) {
136
    fprintf(stderr, "cloudcast: Error initializing local cache\n");
137
    free(con);
138
    return NULL;
139
  }
140

    
141
  con->proto_context = cloudcast_proto_init(myID, metadata, metadata_size);
142
  if (!con->proto_context){
143
    free(con->local_cache);
144
    free(con);
145
    return NULL;
146
  }
147
  con->cloud_nodes = cloudcast_get_cloud_nodes(con->proto_context, 2);
148

    
149
  return con;
150
}
151

    
152
static int cloudcast_add_neighbour(struct peersampler_context *context, struct nodeID *neighbour, const void *metadata, int metadata_size)
153
{
154
  if (!context->flying_cache) {
155
    context->flying_cache = rand_cache(context->local_cache, context->sent_entries - 1);
156
  }
157
  if (cache_add(context->local_cache, neighbour, metadata, metadata_size) < 0) {
158
    return -1;
159
  }
160

    
161
  if (cloudcast_is_cloud_node(context->proto_context, neighbour) == 0)
162
    return cloudcast_query_peer(context->proto_context, context->flying_cache, neighbour);
163
  else
164
    return cloudcast_query_cloud(context->proto_context);
165
}
166

    
167
static int cloudcast_parse_data(struct peersampler_context *context, const uint8_t *buff, int len)
168
{
169
  cache_check(context->local_cache);
170
  if (len) {
171
    // Passive thread
172
    const struct topo_header *h = (const struct topo_header *)buff;
173
    struct peer_cache *remote_cache  = NULL;
174
    struct peer_cache *sent_cache = NULL;
175

    
176
    if (h->protocol != MSG_TYPE_TOPOLOGY) {
177
      fprintf(stderr, "Peer Sampler: Wrong protocol!\n");
178

    
179
      return -1;
180
    }
181

    
182
    context->bootstrap = false;
183

    
184
    if (len - sizeof(struct topo_header) > 0)
185
      remote_cache = entries_undump(buff + sizeof(struct topo_header), len - sizeof(struct topo_header));
186

    
187

    
188
    if (h->type == CLOUDCAST_QUERY) {
189
      sent_cache = rand_cache(context->local_cache, context->sent_entries);
190
      cloudcast_reply_peer(context->proto_context, remote_cache, sent_cache);
191
      context->dst = NULL;
192
    } else if (h->type == CLOUDCAST_CLOUD) {
193
      int g = 0;
194
      int i = 0;
195
      struct peer_cache *reply_cache = NULL;
196

    
197
      // Obtain the timestamp reported by the last query_cloud operation
198
      uint64_t cloud_time_stamp = cloudcast_timestamp_cloud(context->proto_context) * 1000000ull;
199
      uint64_t current_time = gettime();
200
      int delta = current_time - cloud_time_stamp;
201

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

    
206
      if (remote_cache == NULL) {
207
        // Cloud is not initialized, just set the cloud cache to send_cache
208
        cloudcast_reply_cloud(context->proto_context, sent_cache);
209
        remote_cache = cloudcast_cloud_default_reply(context->local_cache, context->cloud_nodes[0]);
210
      } else {
211
        // Locally perform exchange of cache entries
212
        int err;
213
        struct peer_cache *cloud_cache = NULL;
214

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

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

    
221
        // Remove mentions of local node from the cache
222
        cache_del(remote_cache, context->local_node);
223

    
224
        // cloud view with g spot free for cloud nodeID
225
        reply_cache = rand_cache(remote_cache, context->sent_entries - g);
226

    
227
        // building new cloud view
228
        cloud_cache = merge_caches(remote_cache, sent_cache, context->cache_size, &err);
229
        free(remote_cache);
230
        cache_add_cache(cloud_cache, sent_cache);
231
        if (context->keep_cache_full){
232
          cache_add_cache(cloud_cache, reply_cache);
233
        }
234

    
235
        err = cloudcast_reply_cloud(context->proto_context, cloud_cache);
236
        free(cloud_cache);
237

    
238
        //Add the actual cloud nodes to the cache
239
        for (i = 0; i<g; i++){
240
          if (cache_add_resize(reply_cache, context->cloud_nodes[i], context->sent_entries) != 0){
241
            fprintf(stderr, "WTF!!! cannot add to cache\n");
242
            return 1;
243
          }
244
        }
245

    
246
        remote_cache = reply_cache;
247
        context->dst = NULL;
248
      }
249
    }
250
    cache_check(context->local_cache);
251

    
252
    if (remote_cache){
253
      // Remote cache may be NULL due to a non initialize cloud
254
      cache_add_cache(context->local_cache, remote_cache);
255
      cache_free(remote_cache);
256
    }
257
    if (sent_cache) {
258
      if (context->keep_cache_full)
259
        cache_add_cache(context->local_cache, sent_cache);
260
      cache_free(sent_cache);
261
    } else {
262
      if (context->flying_cache) {
263
        cache_add_cache(context->local_cache, context->flying_cache);
264
        cache_free(context->flying_cache);
265
        context->flying_cache = NULL;
266
      }
267
    }
268
  }
269

    
270
  // Active thread
271
  if (time_to_send(context)) {
272
    if (context->flying_cache) {
273
      cache_add_cache(context->local_cache, context->flying_cache);
274
      cache_free(context->flying_cache);
275
      context->flying_cache = NULL;
276
    }
277
    cache_update(context->local_cache);
278
    context->dst = last_peer(context->local_cache);
279
    if (context->dst == NULL) {
280
      // If we remain without neighbors, forcely add a cloud entry
281
      context->dst = cloudcast_get_cloud_node(context->proto_context);
282
      cache_add(context->local_cache, context->dst, NULL, 0);
283
    }
284
    context->dst = nodeid_dup(context->dst);
285
    cache_del(context->local_cache, context->dst);
286

    
287
    if (cloudcast_is_cloud_node(context->proto_context, context->dst)) {
288
      int err;
289
      err = cloudcast_query_cloud(context->proto_context);
290
    } else {
291
      context->flying_cache = rand_cache(context->local_cache, context->sent_entries - 1);
292
      cloudcast_query_peer(context->proto_context, context->flying_cache, context->dst);
293
    }
294
  }
295
  cache_check(context->local_cache);
296

    
297
  return 0;
298
  }
299

    
300
static const struct nodeID **cloudcast_get_neighbourhood(struct peersampler_context *context, int *n)
301
{
302
  context->r = realloc(context->r, context->cache_size * sizeof(struct nodeID *));
303
  if (context->r == NULL) {
304
    return NULL;
305
  }
306

    
307
  for (*n = 0; nodeid(context->local_cache, *n) && (*n < context->cache_size); (*n)++) {
308
    context->r[*n] = nodeid(context->local_cache, *n);
309
    //fprintf(stderr, "Checking table[%d]\n", *n);
310
  }
311
  if (context->flying_cache) {
312
    int i,j,dup;
313

    
314
    for (i = 0; nodeid(context->flying_cache, i) && (*n < context->cache_size); (*n)++, i++) {
315
      dup = 0;
316
      for (j = 0; j<*n; j++){
317
        if (nodeid_equal(context->r[j], nodeid(context->flying_cache, i))){
318
          dup = 1;
319
          continue;
320
        }
321
      }
322
      if (dup) (*n)--;
323
      else context->r[*n] = nodeid(context->flying_cache, i);
324
    }
325
  }
326

    
327
  if (context->dst && (*n < context->cache_size)) {
328
    int j,dup;
329
    dup = 0;
330
    for (j = 0; j<*n; j++){
331
      if (nodeid_equal(context->r[j], context->dst)){
332
        dup = 1;
333
        continue;
334
      }
335
    }
336
    if (!dup){
337
      context->r[*n] = context->dst;
338
      (*n)++;
339
    }
340
  }
341

    
342
  return (const struct nodeID **)context->r;
343
}
344

    
345
static const void *cloudcast_get_metadata(struct peersampler_context *context, int *metadata_size)
346
{
347
  return get_metadata(context->local_cache, metadata_size);
348
}
349

    
350
static int cloudcast_grow_neighbourhood(struct peersampler_context *context, int n)
351
{
352
  context->cache_size += n;
353

    
354
  return context->cache_size;
355
}
356

    
357
static int cloudcast_shrink_neighbourhood(struct peersampler_context *context, int n)
358
{
359
  if (context->cache_size < n) {
360
    return -1;
361
  }
362
  context->cache_size -= n;
363

    
364
  return context->cache_size;
365
}
366

    
367
static int cloudcast_remove_neighbour(struct peersampler_context *context, const struct nodeID *neighbour)
368
{
369
  return cache_del(context->local_cache, neighbour);
370
}
371

    
372
static int cloudcast_change_metadata(struct peersampler_context *context, const void *metadata, int metadata_size)
373
{
374
  return cloudcast_proto_change_metadata(context->proto_context, metadata, metadata_size);
375
}
376

    
377
struct peersampler_iface cloudcast = {
378
  .init = cloudcast_init,
379
  .change_metadata = cloudcast_change_metadata,
380
  .add_neighbour = cloudcast_add_neighbour,
381
  .parse_data = cloudcast_parse_data,
382
  .get_neighbourhood = cloudcast_get_neighbourhood,
383
  .get_metadata = cloudcast_get_metadata,
384
  .grow_neighbourhood = cloudcast_grow_neighbourhood,
385
  .shrink_neighbourhood = cloudcast_shrink_neighbourhood,
386
  .remove_neighbour = cloudcast_remove_neighbour,
387
};