Statistics
| Branch: | Revision:

grapes / src / PeerSampler / cloudcast.c @ 672eb08e

History | View | Annotate | Download (11.3 KB)

1
/*
2
 *  Copyright (c) 2010 Andrea Zito
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

    
15
#include "net_helper.h"
16
#include "peersampler_iface.h"
17
#include "../Cache/topocache.h"
18
#include "../Cache/cloudcast_proto.h"
19
#include "../Cache/proto.h"
20
#include "config.h"
21
#include "grapes_msg_types.h"
22

    
23
#define DEFAULT_CACHE_SIZE 10
24
#define DEFAULT_CLOUD_CONTACT_TRESHOLD 4000000
25

    
26
struct peersampler_context{
27
  uint64_t currtime;
28
  int cache_size;
29
  int sent_entries;
30
  int keep_cache_full;
31
  struct peer_cache *local_cache;
32
  bool bootstrap;
33
  int bootstrap_period;
34
  int period;
35

    
36
  int cloud_contact_treshold;
37
  struct nodeID *local_node;
38
  struct nodeID **cloud_nodes;
39

    
40
  struct peer_cache *flying_cache;
41
  struct nodeID *dst;
42

    
43
  struct cloudcast_proto_context *proto_context;
44
  struct nodeID **r;
45
};
46

    
47

    
48
static uint64_t gettime(void)
49
{
50
  struct timeval tv;
51

    
52
  gettimeofday(&tv, NULL);
53

    
54
  return tv.tv_usec + tv.tv_sec * 1000000ull;
55
}
56

    
57
static struct peersampler_context* cloudcast_context_init(void){
58
  struct peersampler_context* con;
59
  con = (struct peersampler_context*) calloc(1,sizeof(struct peersampler_context));
60
  memset(con, 0, sizeof(struct peersampler_context));
61

    
62
  //Initialize context with default values
63
  con->bootstrap = true;
64
  con->bootstrap_period = 2000000;
65
  con->period = 10000000;
66
  con->currtime = gettime();
67
  con->r = NULL;
68
  con->cloud_contact_treshold = DEFAULT_CLOUD_CONTACT_TRESHOLD;
69

    
70
  return con;
71
}
72

    
73
static int time_to_send(struct peersampler_context* con)
74
{
75
  int p = con->bootstrap ? con->bootstrap_period : con->period;
76
  if (gettime() - con->currtime > p) {
77
    con->currtime += p;
78

    
79
    return 1;
80
  }
81

    
82
  return 0;
83
}
84

    
85
static void cache_add_cache(struct peer_cache *dst, const struct peer_cache *add)
86
{
87
  int i, meta_size;
88
  const uint8_t *meta;
89

    
90
  meta = get_metadata(add, &meta_size);
91
  for (i = 0; nodeid(add, i); i++) {
92
    cache_add(dst,  nodeid(add, i), meta + (meta_size * i), meta_size);
93
  }
94
}
95

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

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

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

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

    
135
  con->local_cache = cache_init(con->cache_size, metadata_size, 0);
136
  if (con->local_cache == NULL) {
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
};