Statistics
| Branch: | Revision:

streamers / topology.c @ 0cc44d18

History | View | Annotate | Download (13.4 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
double desired_bw = 1000000;
35
double desired_rtt = 0.2;
36
double alpha_target = 0.4;
37
double topo_mem = 0.7;
38

    
39
bool topo_out = true; //peer selects out-neighbours
40
bool topo_in = false; //peer selects in-neighbours (combined means bidirectional)
41

    
42
int NEIGHBORHOOD_TARGET_SIZE = 30;
43
double NEIGHBORHOOD_ROTATE_RATIO = 1.0;
44
#define TMAN_MAX_IDLE 10
45
#define TMAN_LOG_EVERY 1000
46

    
47
#define STREAMER_TOPOLOGY_MSG_ADD 0
48
#define STREAMER_TOPOLOGY_MSG_REMOVE 1
49

    
50
static struct peerset *pset;
51
static struct timeval tout_bmap = {20, 0};
52
static int counter = 0;
53
static int simpleRanker (const void *tin, const void *p1in, const void *p2in);
54
static tmanRankingFunction rankFunct = simpleRanker;
55

    
56
struct metadata {
57
  uint16_t cb_size;
58
  uint16_t cps;
59
  float capacity;
60
  float recv_delay;
61
} __attribute__((packed));
62

    
63
static struct metadata my_metadata;
64
static int cnt = 0;
65
static struct nodeID *me = NULL;
66
static unsigned char mTypes[] = {MSG_TYPE_TOPOLOGY,MSG_TYPE_TMAN,MSG_TYPE_STREAMER_TOPOLOGY};
67
static struct nodeID ** neighbors;
68

    
69
static void update_metadata(void) {
70
        my_metadata.cb_size = am_i_source() ? 0 : get_cb_size();
71
        my_metadata.recv_delay = get_receive_delay();
72
        my_metadata.cps = get_chunks_per_sec();
73
        my_metadata.capacity = get_capacity();
74
}
75

    
76
static int simpleRanker (const void *tin, const void *p1in, const void *p2in) {
77

    
78
        double t,p1,p2;
79
        t = *((const double *)tin);
80
        p1 = *((const double *)p1in);
81
        p2 = *((const double *)p2in);
82

    
83
        if (isnan(t) || (isnan(p1) && isnan(p2))) return 0;
84
        else if (isnan(p1)) return 2;
85
        else if (isnan(p2)) return 1;
86
        else return (fabs(t-p1) == fabs(t-p2))?0:(fabs(t-p1) < fabs(t-p2))?1:2;
87

    
88
}
89

    
90
int topologyInit(struct nodeID *myID, const char *config)
91
{
92
        int i;
93
        for (i=0;i<sizeof(mTypes)/sizeof(mTypes[0]);i++)
94
                bind_msg_type(mTypes[i]);
95
        update_metadata();
96
        me = myID;
97
        return (topInit(myID, &my_metadata, sizeof(my_metadata), config) && tmanInit(myID,&my_metadata, sizeof(my_metadata),rankFunct,0));
98
}
99

    
100
void topologyShutdown(void)
101
{
102
}
103

    
104
int topoAddNeighbour(struct nodeID *neighbour, void *metadata, int metadata_size)
105
{
106
        // TODO: check this!! Just to use this function to bootstrap ncast...
107
        struct metadata m = {0};        //TODO: check what metadata option should mean
108

    
109
        if (counter < TMAN_MAX_IDLE)
110
                return topAddNeighbour(neighbour,&m,sizeof(m));
111
        else return tmanAddNeighbour(neighbour,&m,sizeof(m));
112
}
113

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

    
134
static const struct nodeID **topoGetNeighbourhood(int *n)
135
{
136
        int i; double d;
137
        if (counter > TMAN_MAX_IDLE) {
138
                uint8_t *mdata; int msize;
139
                *n = tmanGetNeighbourhoodSize();
140
                if (neighbors) free(neighbors);
141
                neighbors = calloc(*n,sizeof(struct nodeID *));
142
                tmanGetMetadata(&msize);
143
                mdata = calloc(*n,msize);
144
                tmanGivePeers(*n,neighbors,(void *)mdata);
145

    
146
                if (cnt % TMAN_LOG_EVERY == 0) {
147
                        fprintf(stderr,"abouttopublish,%s,%s,,Tman_chunk_delay,%f\n",node_addr(me),node_addr(me),my_metadata.recv_delay);
148
                        for (i=0;i<(*n) && i<NEIGHBORHOOD_TARGET_SIZE;i++) {
149
                                d = *((double *)(mdata+i*msize));
150
                                fprintf(stderr,"abouttopublish,%s,",node_addr(me));
151
                                fprintf(stderr,"%s,,Tman_chunk_delay,%f\n",node_addr(neighbors[i]),d);
152
                        }
153
                        fprintf(stderr,"abouttopublish,%s,%s,,Tman_neighborhood_size,%d\n\n",node_addr(me),node_addr(me),*n);
154
                }
155

    
156
                free(mdata);
157
                return (const struct nodeID **)neighbors;
158
        }
159
        else
160
                return topGetNeighbourhood(n);
161
}
162

    
163
static void topoAddToBL (struct nodeID *id)
164
{
165
        if (counter >= TMAN_MAX_IDLE)
166
                tmanAddToBlackList(id);
167
//        else
168
                topAddToBlackList(id);
169
}
170

    
171
//TODO: send metadata as well
172
static int send_topo_msg(struct nodeID *dst, uint8_t type)
173
{
174
  char msg[2];
175
  msg[0] = MSG_TYPE_STREAMER_TOPOLOGY;
176
  msg[1] = type;
177
  return send_to_peer(me, dst, msg, 2);
178
}
179

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

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

    
211
//get the rtt. Currenly only MONL version is supported
212
static double get_rtt_of(const struct nodeID* n){
213
#ifdef MONL
214
  return get_rtt(n);
215
#else
216
  return NAN;
217
#endif
218
}
219

    
220
//get the declared capacity of a node
221
static double get_capacity_of(const struct nodeID* n){
222
  struct peer *p = peerset_get_peer(pset, n);
223
  if (p) {
224
    return p->capacity;
225
  }
226

    
227
  return NAN;
228
}
229

    
230
//returns: 1:yes 0:no -1:unknown
231
int desiredness(const struct nodeID* n) {
232
  double rtt = get_rtt_of(n);
233
  double bw =  get_capacity_of(n);
234

    
235
  if ((isnan(rtt) && finite(desired_rtt)) || (isnan(bw) && desired_bw > 0)) {
236
    return -1;
237
  } else if ((isnan(rtt) || rtt <= desired_rtt) && (isnan(bw) || bw >= desired_bw)) {
238
    return 1;
239
  }
240

    
241
  return 0;
242
}
243

    
244
bool is_desired(const struct nodeID* n) {
245
  return (desiredness(n) == 1);
246
}
247

    
248
// currently it just makes the peerset grow
249
void update_peers(struct nodeID *from, const uint8_t *buff, int len)
250
{
251
  int n_ids, metasize, i, newids_size, max_ids;
252
  static const struct nodeID **newids;
253
  static const struct metadata *metas;
254
  struct peer *peers;
255
  struct timeval tnow, told;
256
  static const struct nodeID **savedids;
257
  static int savedids_size;
258

    
259
  if timerisset(&tout_bmap) {
260
    gettimeofday(&tnow, NULL);
261
    timersub(&tnow, &tout_bmap, &told);
262
    peers = peerset_get_peers(pset);
263
    for (i = 0; i < peerset_size(pset); i++) {
264
      if ( (!timerisset(&peers[i].bmap_timestamp) && timercmp(&peers[i].creation_timestamp, &told, <) ) ||
265
           ( timerisset(&peers[i].bmap_timestamp) && timercmp(&peers[i].bmap_timestamp, &told, <)     )   ) {
266
        ftprintf(stderr,"Topo: dropping inactive %s (peers:%d)\n", node_addr(peers[i].id), peerset_size(pset));
267
        //if (peerset_size(pset) > 1) {        // avoid dropping our last link to the world
268
        topoAddToBL(peers[i].id);
269
        remove_peer(peers[i--].id, true, true);
270
        //}
271
      }
272
    }
273
  }
274

    
275
  if (cnt++ % 100 == 0) {
276
        update_metadata();
277
    if (counter > TMAN_MAX_IDLE) {
278
        tmanChangeMetadata(&my_metadata,sizeof(my_metadata));
279
    }
280
  }
281

    
282
  //handle explicit add/remove messages
283
  if (topo_in && buff && buff[0] == MSG_TYPE_STREAMER_TOPOLOGY) {
284
    dtprintf(stderr,"Topo: explicit topo message received!!!from %s (peers:%d)\n", node_addr(from), peerset_size(pset));
285
    if (len != 2) {
286
      fprintf(stderr, "Bad streamer topo message received, len:%d!\n", len);
287
      return;
288
    }
289
    switch (buff[1]) {
290
      case STREAMER_TOPOLOGY_MSG_ADD:
291
        ftprintf(stderr,"Topo: adding on request %s (peers:%d)\n", node_addr(from), peerset_size(pset));
292
        add_peer(from, NULL, true, false);
293
        break;
294
      case STREAMER_TOPOLOGY_MSG_REMOVE:
295
        ftprintf(stderr,"Topo: removing on request %s (peers:%d)\n", node_addr(from), peerset_size(pset));
296
        remove_peer(from, true, false);
297
        break;
298
      default:
299
        fprintf(stderr, "Bad streamer topo message received!\n");
300
    }
301
    reg_neigh_size(peerset_size(pset));
302
    return;
303
  }
304

    
305
  topoParseData(buff, len);
306

    
307
  if (!buff) {
308
    reg_neigh_size(peerset_size(pset));
309
    return;
310
  }
311

    
312
  peers = peerset_get_peers(pset);
313
  n_ids = peerset_size(pset);
314
  newids = topoGetNeighbourhood(&newids_size);        //TODO handle both tman and topo
315
  metas = topGetMetadata(&metasize);        //TODO: check metasize
316
  max_ids = n_ids + savedids_size + newids_size;
317
  ftprintf(stderr,"Topo modify start peers:%d candidates:%d\n", n_ids, newids_size);
318
  {
319
    int desired_part;
320
    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];
321
    int oldids_size, nodeids_size, candidates_size, desireds_size, selecteds_size, others_size, toadds_size, toremoves_size, keep_size, random_size;
322
    nodeids_size = candidates_size = desireds_size = selecteds_size = others_size = toadds_size = toremoves_size = max_ids;
323

    
324
    if (topo_out) {
325
      for (i = 0, oldids_size = 0; i < peerset_size(pset); i++) {
326
        oldids[oldids_size++] = peers[i].id;
327
        fprintf(stderr," %s - RTT: %f\n", node_addr(peers[i].id) , get_rtt_of(peers[i].id));
328
      }
329
    } else {
330
      for (i = 0, oldids_size = 0; i < savedids_size; i++) {
331
        oldids[oldids_size++] = savedids[i];
332
        fprintf(stderr," %s - RTT: %f\n", node_addr(savedids[i]) , get_rtt_of(savedids[i]));
333
      }
334
      savedids_size = 0;
335
      free(savedids);
336
    }
337

    
338
    // select the topo_mem portion of peers to be kept (uniform random)
339
    nidset_shuffle(oldids, oldids_size);
340
    keep_size = selecteds_size = (int) (topo_mem * oldids_size);
341
    memcpy(selecteds, oldids, selecteds_size * sizeof(selecteds[0]));
342

    
343
    // compose list of known nodeids
344
    nidset_add(nodeids, &nodeids_size, oldids, oldids_size, newids, newids_size);
345

    
346
    // compose list of candidate nodeids
347
    nidset_complement(candidates, &candidates_size, nodeids, nodeids_size, selecteds, selecteds_size);
348

    
349
    // select the alpha_target portion of desired peers
350
    desired_part = (1 - alpha_target) * (NEIGHBORHOOD_TARGET_SIZE ? (MAX (NEIGHBORHOOD_TARGET_SIZE - selecteds_size, 0)) : 0);
351
    nidset_filter(desireds, &desireds_size, candidates, candidates_size, is_desired);
352
    nidset_shuffle(desireds, desireds_size);
353
    nidset_add_i(selecteds, &selecteds_size, max_ids, desireds, MIN(desireds_size,desired_part));
354

    
355
    // random from the rest
356
    nidset_complement(others, &others_size, candidates, candidates_size, selecteds, selecteds_size);
357
    nidset_shuffle(others, others_size);
358
    random_size = NEIGHBORHOOD_TARGET_SIZE ? MIN(others_size, MAX(NEIGHBORHOOD_TARGET_SIZE - selecteds_size, 0)) : others_size;
359
    nidset_add_i(selecteds, &selecteds_size, max_ids, others, random_size);
360

    
361
    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",
362
            selecteds_size, nodeids_size,
363
            keep_size, oldids_size,
364
            MIN(desireds_size,desired_part), desireds_size, candidates_size, desired_part,
365
            random_size, others_size);
366
    // add new ones
367
    nidset_complement(toadds, &toadds_size, selecteds, selecteds_size, oldids, oldids_size);
368
    for (i = 0; i < toadds_size; i++) {
369
      int j;
370
      //searching for the metadata
371
      if (nidset_find(&j, newids, newids_size, toadds[i])) {
372
        fprintf(stderr," adding %s\n", node_addr(toadds[i]));
373
        add_peer(newids[j], &metas[j], topo_out, topo_in);
374
      } else {
375
        fprintf(stderr," Error: missing metadata for %s\n", node_addr(toadds[i]));
376
      }
377
    }
378

    
379
    // finally, remove those not needed
380
    fprintf(stderr,"Topo remove start (peers:%d)\n", n_ids);
381
    nidset_complement(toremoves, &toremoves_size, oldids, oldids_size, selecteds, selecteds_size);
382
    for (i = 0; i < toremoves_size; i++) {
383
      fprintf(stderr," removing %s\n", node_addr(toremoves[i]));
384
      remove_peer(toremoves[i], topo_out, topo_in);
385
    }
386
    fprintf(stderr,"Topo remove end\n");
387

    
388
    if (!topo_out) {
389
      savedids = malloc(selecteds_size * sizeof(savedids[0]));        //TODO: handle errors
390
      for (i = 0, savedids_size = 0; i < selecteds_size; i++) {
391
        savedids[savedids_size++] = nodeid_dup(selecteds[i]);
392
      }
393
      for (i = 0; i < oldids_size; i++) {
394
        nodeid_free(oldids[i]);
395
      }
396
    }
397
  }
398

    
399
  reg_neigh_size(peerset_size(pset));
400
}
401

    
402
struct peer *nodeid_to_peer(const struct nodeID* id, int reg)
403
{
404
  struct peer *p = peerset_get_peer(pset, id);
405
  if (!p) {
406
    //fprintf(stderr,"warning: received message from unknown peer: %s!%s\n",node_addr(id), reg ? " Adding it to pset." : "");
407
    if (reg) {
408
      add_peer(id,NULL, topo_out, false);
409
      fprintf(stderr,"Topo: ext adding %s (peers:%d)\n", node_addr(id), peerset_size(pset));
410
      p = peerset_get_peer(pset,id);
411
    }
412
  }
413

    
414
  return p;
415
}
416

    
417
int peers_init(void)
418
{
419
  fprintf(stderr,"peers_init\n");
420
  pset = peerset_init(0);
421
  return pset ? 1 : 0;
422
}
423

    
424
struct peerset *get_peers(void)
425
{
426
  return pset;
427
}