Statistics
| Branch: | Revision:

streamers / topology.c @ 14fe383b

History | View | Annotate | Download (14.1 KB)

1
/*
2
 *  Copyright (c) 2010 Csaba Kiraly
3
 *  Copyright (c) 2010 Luca Abeni
4
 *
5
 *  This is free software; see gpl-3.0.txt
6
 */
7
#include <stdint.h>
8
#include <stdio.h>
9
#include <sys/time.h>
10
#include <time.h>
11
#include <stdlib.h>
12
#include <string.h>
13

    
14
#include <math.h>
15
#include <net_helper.h>
16
#include <peerset.h>
17
#include <peer.h>
18
#include <grapes_msg_types.h>
19
#include <topmanager.h>
20
#include <tman.h>
21

    
22
#include "compatibility/timer.h"
23

    
24
#include "topology.h"
25
#include "nodeid_set.h"
26
#include "streaming.h"
27
#include "dbg.h"
28
#include "measures.h"
29
#include "streamer.h"
30

    
31
#define MIN(A,B) (((A) < (B)) ? (A) : (B))
32
#define MAX(A,B) (((A) > (B)) ? (A) : (B))
33

    
34
#ifndef NAN        //NAN is missing in some old math.h versions
35
#define NAN            (0.0/0.0)
36
#endif
37

    
38
double desired_bw = 1000000;
39
double desired_rtt = 0.2;
40
double alpha_target = 0.4;
41
double topo_mem = 0.7;
42

    
43
bool topo_out = true; //peer selects out-neighbours
44
bool topo_in = true; //peer selects in-neighbours (combined means bidirectional)
45

    
46
bool topo_keep_best = false;
47
bool topo_add_best = false;
48

    
49
int NEIGHBORHOOD_TARGET_SIZE = 30;
50
#define TMAN_MAX_IDLE 10
51
#define TMAN_LOG_EVERY 1000
52

    
53
#define STREAMER_TOPOLOGY_MSG_ADD 0
54
#define STREAMER_TOPOLOGY_MSG_REMOVE 1
55

    
56
static struct peerset *pset;
57
static struct timeval tout_bmap = {20, 0};
58
static int counter = 0;
59
static int simpleRanker (const void *tin, const void *p1in, const void *p2in);
60
static tmanRankingFunction rankFunct = simpleRanker;
61

    
62
struct metadata {
63
  uint16_t cb_size;
64
  uint16_t cps;
65
  float capacity;
66
  float recv_delay;
67
} __attribute__((packed));
68

    
69
static struct metadata my_metadata;
70
static int cnt = 0;
71
static struct nodeID *me = NULL;
72
static unsigned char mTypes[] = {MSG_TYPE_TOPOLOGY,MSG_TYPE_TMAN,MSG_TYPE_STREAMER_TOPOLOGY};
73
static struct nodeID ** neighbors;
74

    
75
static void update_metadata(void) {
76
        my_metadata.cb_size = am_i_source() ? 0 : get_cb_size();
77
        my_metadata.recv_delay = get_receive_delay();
78
        my_metadata.cps = get_chunks_per_sec();
79
        my_metadata.capacity = get_capacity();
80
}
81

    
82
static int simpleRanker (const void *tin, const void *p1in, const void *p2in) {
83

    
84
        double t,p1,p2;
85
        t = *((const double *)tin);
86
        p1 = *((const double *)p1in);
87
        p2 = *((const double *)p2in);
88

    
89
        if (isnan(t) || (isnan(p1) && isnan(p2))) return 0;
90
        else if (isnan(p1)) return 2;
91
        else if (isnan(p2)) return 1;
92
        else return (fabs(t-p1) == fabs(t-p2))?0:(fabs(t-p1) < fabs(t-p2))?1:2;
93

    
94
}
95

    
96
int topologyInit(struct nodeID *myID, const char *config)
97
{
98
        int i;
99
        for (i=0;i<sizeof(mTypes)/sizeof(mTypes[0]);i++)
100
                bind_msg_type(mTypes[i]);
101
        update_metadata();
102
        me = myID;
103
        return (topInit(myID, &my_metadata, sizeof(my_metadata), config) && tmanInit(myID,&my_metadata, sizeof(my_metadata),rankFunct,0));
104
}
105

    
106
void topologyShutdown(void)
107
{
108
}
109

    
110
int topoAddNeighbour(struct nodeID *neighbour, void *metadata, int metadata_size)
111
{
112
        // TODO: check this!! Just to use this function to bootstrap ncast...
113
        struct metadata m = {0};        //TODO: check what metadata option should mean
114

    
115
        if (counter < TMAN_MAX_IDLE)
116
                return topAddNeighbour(neighbour,&m,sizeof(m));
117
        else return tmanAddNeighbour(neighbour,&m,sizeof(m));
118
}
119

    
120
static int topoParseData(const uint8_t *buff, int len)
121
{
122
        int res = -1,ncs = 0,msize;
123
        const struct nodeID **n; const void *m;
124
        if (!buff || buff[0] == MSG_TYPE_TOPOLOGY) {
125
                res = topParseData(buff,len);
126
//                if (counter <= TMAN_MAX_IDLE)
127
//                        counter++;
128
        }
129
        if (counter >= TMAN_MAX_IDLE && (!buff || buff[0] == MSG_TYPE_TMAN))
130
        {
131
                n = topGetNeighbourhood(&ncs);
132
                if (ncs) {
133
                m = topGetMetadata(&msize);
134
                res = tmanParseData(buff,len,n,ncs,m,msize);
135
                }
136
        }
137
  return res;
138
}
139

    
140
static const struct nodeID **topoGetNeighbourhood(int *n)
141
{
142
        int i; double d;
143
        if (counter > TMAN_MAX_IDLE) {
144
                uint8_t *mdata; int msize;
145
                *n = tmanGetNeighbourhoodSize();
146
                if (neighbors) free(neighbors);
147
                neighbors = calloc(*n,sizeof(struct nodeID *));
148
                tmanGetMetadata(&msize);
149
                mdata = calloc(*n,msize);
150
                tmanGivePeers(*n,neighbors,(void *)mdata);
151

    
152
                if (cnt % TMAN_LOG_EVERY == 0) {
153
                        fprintf(stderr,"abouttopublish,%s,%s,,Tman_chunk_delay,%f\n",node_addr(me),node_addr(me),my_metadata.recv_delay);
154
                        for (i=0;i<(*n) && i<NEIGHBORHOOD_TARGET_SIZE;i++) {
155
                                d = *((double *)(mdata+i*msize));
156
                                fprintf(stderr,"abouttopublish,%s,",node_addr(me));
157
                                fprintf(stderr,"%s,,Tman_chunk_delay,%f\n",node_addr(neighbors[i]),d);
158
                        }
159
                        fprintf(stderr,"abouttopublish,%s,%s,,Tman_neighborhood_size,%d\n\n",node_addr(me),node_addr(me),*n);
160
                }
161

    
162
                free(mdata);
163
                return (const struct nodeID **)neighbors;
164
        }
165
        else
166
                return topGetNeighbourhood(n);
167
}
168

    
169
static void topoAddToBL (struct nodeID *id)
170
{
171
        if (counter >= TMAN_MAX_IDLE)
172
                tmanAddToBlackList(id);
173
//        else
174
                topAddToBlackList(id);
175
}
176

    
177
//TODO: send metadata as well
178
static int send_topo_msg(struct nodeID *dst, uint8_t type)
179
{
180
  char msg[2];
181
  msg[0] = MSG_TYPE_STREAMER_TOPOLOGY;
182
  msg[1] = type;
183
  return send_to_peer(me, dst, msg, 2);
184
}
185

    
186
static void add_peer(const struct nodeID *id, const struct metadata *m, bool local, bool remote)
187
{
188
  if (local) {
189
      dprintf("Adding %s to neighbourhood! cb_size:%d\n", node_addr(id), m?m->cb_size:-1);
190
      peerset_add_peer(pset, id);
191
      if (m) peerset_get_peer(pset, id)->cb_size = m->cb_size;
192
      if (m) peerset_get_peer(pset, id)->capacity = m->capacity;
193
      /* add measures here */
194
      add_measures(id);
195
      send_bmap(id);
196
  }
197
  if (remote) {
198
      dtprintf("Topo: explicit topo message sent!!! to %s (peers:%d)\n", node_addr(id));
199
      send_topo_msg(id, STREAMER_TOPOLOGY_MSG_ADD);
200
  }
201
}
202

    
203
static void remove_peer(const struct nodeID *id, bool local, bool remote)
204
{
205
  if (local) {
206
      dprintf("Removing %s from neighbourhood!\n", node_addr(id));
207
      /* add measures here */
208
      delete_measures(id);
209
      peerset_remove_peer(pset, id);
210
  }
211
  if (remote) {
212
      dtprintf("Topo: explicit topo message sent!!! to %s (peers:%d)\n", node_addr(id));
213
      send_topo_msg(id, STREAMER_TOPOLOGY_MSG_REMOVE);
214
  }
215
}
216

    
217
//get the rtt. Currenly only MONL version is supported
218
static double get_rtt_of(const struct nodeID* n){
219
#ifdef MONL
220
  return get_rtt(n);
221
#else
222
  return NAN;
223
#endif
224
}
225

    
226
//get the declared capacity of a node
227
static double get_capacity_of(const struct nodeID* n){
228
  struct peer *p = peerset_get_peer(pset, n);
229
  if (p) {
230
    return p->capacity;
231
  }
232

    
233
  return NAN;
234
}
235

    
236
//returns: 1:yes 0:no -1:unknown
237
int desiredness(const struct nodeID* n) {
238
  double rtt = get_rtt_of(n);
239
  double bw =  get_capacity_of(n);
240

    
241
  if ((isnan(rtt) && finite(desired_rtt)) || (isnan(bw) && desired_bw > 0)) {
242
    return -1;
243
  } else if ((isnan(rtt) || rtt <= desired_rtt) && (isnan(bw) || bw >= desired_bw)) {
244
    return 1;
245
  }
246

    
247
  return 0;
248
}
249

    
250
bool is_desired(const struct nodeID* n) {
251
  return (desiredness(n) == 1);
252
}
253

    
254
int cmp_rtt(const struct nodeID* a, const struct nodeID* b) {
255
  double ra, rb;
256
  ra = get_rtt_of(a);
257
  rb = get_rtt_of(a);
258
  if ((isnan(ra) && isnan(rb)) || ra == rb) return 0;
259
  else if (isnan(rb) || ra < rb) return -1;
260
  else return 1;
261
}
262

    
263
int vcmp_rtt(const void* a, const void* b) {
264
  return cmp_rtt((const struct nodeID*)a, (const struct nodeID*)b);
265
}
266

    
267
// currently it just makes the peerset grow
268
void update_peers(struct nodeID *from, const uint8_t *buff, int len)
269
{
270
  int n_ids, metasize, i, newids_size, max_ids;
271
  static const struct nodeID **newids;
272
  static const struct metadata *metas;
273
  struct peer *peers;
274
  struct timeval tnow, told;
275
  static const struct nodeID **savedids;
276
  static int savedids_size;
277

    
278
  if timerisset(&tout_bmap) {
279
    gettimeofday(&tnow, NULL);
280
    timersub(&tnow, &tout_bmap, &told);
281
    peers = peerset_get_peers(pset);
282
    for (i = 0; i < peerset_size(pset); i++) {
283
      if ( (!timerisset(&peers[i].bmap_timestamp) && timercmp(&peers[i].creation_timestamp, &told, <) ) ||
284
           ( timerisset(&peers[i].bmap_timestamp) && timercmp(&peers[i].bmap_timestamp, &told, <)     )   ) {
285
        ftprintf(stderr,"Topo: dropping inactive %s (peers:%d)\n", node_addr(peers[i].id), peerset_size(pset));
286
        //if (peerset_size(pset) > 1) {        // avoid dropping our last link to the world
287
        topoAddToBL(peers[i].id);
288
        remove_peer(peers[i--].id, true, true);
289
        //}
290
      }
291
    }
292
  }
293

    
294
  if (cnt++ % 100 == 0) {
295
        update_metadata();
296
    if (counter > TMAN_MAX_IDLE) {
297
        tmanChangeMetadata(&my_metadata,sizeof(my_metadata));
298
    }
299
  }
300

    
301
  //handle explicit add/remove messages
302
  if (topo_in && buff && buff[0] == MSG_TYPE_STREAMER_TOPOLOGY) {
303
    dtprintf(stderr,"Topo: explicit topo message received!!!from %s (peers:%d)\n", node_addr(from), peerset_size(pset));
304
    if (len != 2) {
305
      fprintf(stderr, "Bad streamer topo message received, len:%d!\n", len);
306
      return;
307
    }
308
    switch (buff[1]) {
309
      case STREAMER_TOPOLOGY_MSG_ADD:
310
        ftprintf(stderr,"Topo: adding on request %s (peers:%d)\n", node_addr(from), peerset_size(pset));
311
        if (!peerset_get_peer(pset, from))
312
                add_peer(from, NULL, true, false);
313
        break;
314
      case STREAMER_TOPOLOGY_MSG_REMOVE:
315
        ftprintf(stderr,"Topo: removing on request %s (peers:%d)\n", node_addr(from), peerset_size(pset));
316
        remove_peer(from, true, false);
317
        break;
318
      default:
319
        fprintf(stderr, "Bad streamer topo message received!\n");
320
    }
321
    reg_neigh_size(peerset_size(pset));
322
    return;
323
  }
324

    
325
  topoParseData(buff, len);
326

    
327
  if (!buff) {
328
    reg_neigh_size(peerset_size(pset));
329
    return;
330
  }
331

    
332
  peers = peerset_get_peers(pset);
333
  n_ids = peerset_size(pset);
334
  newids = topoGetNeighbourhood(&newids_size);        //TODO handle both tman and topo
335
  metas = topGetMetadata(&metasize);        //TODO: check metasize
336
  max_ids = n_ids + savedids_size + newids_size;
337
  ftprintf(stderr,"Topo modify start peers:%d candidates:%d\n", n_ids, newids_size);
338
  {
339
    int desired_part;
340
    const struct nodeID *oldids[max_ids], *nodeids[max_ids], *candidates[max_ids], *desireds[max_ids], *selecteds[max_ids], *others[max_ids], *toadds[max_ids], *toremoves[max_ids];
341
    int oldids_size, nodeids_size, candidates_size, desireds_size, selecteds_size, others_size, toadds_size, toremoves_size, keep_size, random_size;
342
    nodeids_size = candidates_size = desireds_size = selecteds_size = others_size = toadds_size = toremoves_size = max_ids;
343

    
344
    if (topo_out) {
345
      for (i = 0, oldids_size = 0; i < peerset_size(pset); i++) {
346
        oldids[oldids_size++] = peers[i].id;
347
        fprintf(stderr," %s - RTT: %f\n", node_addr(peers[i].id) , get_rtt_of(peers[i].id));
348
      }
349
    } else {
350
      for (i = 0, oldids_size = 0; i < savedids_size; i++) {
351
        oldids[oldids_size++] = savedids[i];
352
        fprintf(stderr," %s - RTT: %f\n", node_addr(savedids[i]) , get_rtt_of(savedids[i]));
353
      }
354
      savedids_size = 0;
355
      free(savedids);
356
    }
357

    
358
    // select the topo_mem portion of peers to be kept (uniform random)
359
    if (topo_keep_best) {
360
      qsort(oldids, oldids_size, sizeof(oldids[0]), vcmp_rtt);
361
    } else {
362
      nidset_shuffle(oldids, oldids_size);
363
    }
364
    keep_size = selecteds_size = (int) (topo_mem * oldids_size);
365
    memcpy(selecteds, oldids, selecteds_size * sizeof(selecteds[0]));
366

    
367
    // compose list of known nodeids
368
    nidset_add(nodeids, &nodeids_size, oldids, oldids_size, newids, newids_size);
369

    
370
    // compose list of candidate nodeids
371
    nidset_complement(candidates, &candidates_size, nodeids, nodeids_size, selecteds, selecteds_size);
372

    
373
    // select the alpha_target portion of desired peers
374
    desired_part = (1 - alpha_target) * (NEIGHBORHOOD_TARGET_SIZE ? (MAX (NEIGHBORHOOD_TARGET_SIZE - selecteds_size, 0)) : 0);
375
    nidset_filter(desireds, &desireds_size, candidates, candidates_size, is_desired);
376
    if (topo_add_best) {
377
      qsort(desireds, desireds_size, sizeof(desireds[0]), vcmp_rtt);
378
    } else {
379
      nidset_shuffle(desireds, desireds_size);
380
    }
381
    nidset_add_i(selecteds, &selecteds_size, max_ids, desireds, MIN(desireds_size,desired_part));
382

    
383
    // random from the rest
384
    nidset_complement(others, &others_size, candidates, candidates_size, selecteds, selecteds_size);
385
    nidset_shuffle(others, others_size);
386
    random_size = NEIGHBORHOOD_TARGET_SIZE ? MIN(others_size, MAX(NEIGHBORHOOD_TARGET_SIZE - selecteds_size, 0)) : others_size;
387
    nidset_add_i(selecteds, &selecteds_size, max_ids, others, random_size);
388

    
389
    fprintf(stderr,"Topo modify sel:%d (from:%d) = keep: %d (of old:%d) + desired: %d (from %d of %d; target:%d) + random: %d (from %d)\n",
390
            selecteds_size, nodeids_size,
391
            keep_size, oldids_size,
392
            MIN(desireds_size,desired_part), desireds_size, candidates_size, desired_part,
393
            random_size, others_size);
394
    // add new ones
395
    nidset_complement(toadds, &toadds_size, selecteds, selecteds_size, oldids, oldids_size);
396
    for (i = 0; i < toadds_size; i++) {
397
      int j;
398
      //searching for the metadata
399
      if (nidset_find(&j, newids, newids_size, toadds[i])) {
400
        fprintf(stderr," adding %s\n", node_addr(toadds[i]));
401
        add_peer(newids[j], &metas[j], topo_out, topo_in);
402
      } else {
403
        fprintf(stderr," Error: missing metadata for %s\n", node_addr(toadds[i]));
404
      }
405
    }
406

    
407
    // finally, remove those not needed
408
    fprintf(stderr,"Topo remove start (peers:%d)\n", n_ids);
409
    nidset_complement(toremoves, &toremoves_size, oldids, oldids_size, selecteds, selecteds_size);
410
    for (i = 0; i < toremoves_size; i++) {
411
      fprintf(stderr," removing %s\n", node_addr(toremoves[i]));
412
      remove_peer(toremoves[i], topo_out, topo_in);
413
    }
414
    fprintf(stderr,"Topo remove end\n");
415

    
416
    if (!topo_out) {
417
      savedids = malloc(selecteds_size * sizeof(savedids[0]));        //TODO: handle errors
418
      for (i = 0, savedids_size = 0; i < selecteds_size; i++) {
419
        savedids[savedids_size++] = nodeid_dup(selecteds[i]);
420
      }
421
      for (i = 0; i < oldids_size; i++) {
422
        nodeid_free(oldids[i]);
423
      }
424
    }
425
  }
426

    
427
  reg_neigh_size(peerset_size(pset));
428
}
429

    
430
struct peer *nodeid_to_peer(const struct nodeID* id, int reg)
431
{
432
  struct peer *p = peerset_get_peer(pset, id);
433
  if (!p) {
434
    //fprintf(stderr,"warning: received message from unknown peer: %s!%s\n",node_addr(id), reg ? " Adding it to pset." : "");
435
    if (reg) {
436
      add_peer(id,NULL, topo_out, false);
437
      fprintf(stderr,"Topo: ext adding %s (peers:%d)\n", node_addr(id), peerset_size(pset));
438
      p = peerset_get_peer(pset,id);
439
    }
440
  }
441

    
442
  return p;
443
}
444

    
445
int peers_init(void)
446
{
447
  fprintf(stderr,"peers_init\n");
448
  pset = peerset_init(0);
449
  return pset ? 1 : 0;
450
}
451

    
452
struct peerset *get_peers(void)
453
{
454
  return pset;
455
}