Statistics
| Branch: | Revision:

streamers / topology.c @ 16ae4927

History | View | Annotate | Download (10.2 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

    
33
double desired_bw = 0;
34
double desired_rtt = 0.2;
35
double alpha_target = 0.5;
36

    
37
int NEIGHBORHOOD_TARGET_SIZE = 30;
38
double NEIGHBORHOOD_ROTATE_RATIO = 1.0;
39
#define TMAN_MAX_IDLE 10
40
#define TMAN_LOG_EVERY 1000
41

    
42
static struct peerset *pset;
43
static struct timeval tout_bmap = {20, 0};
44
static int counter = 0;
45
static int simpleRanker (const void *tin, const void *p1in, const void *p2in);
46
static tmanRankingFunction rankFunct = simpleRanker;
47

    
48
struct metadata {
49
  uint16_t cb_size;
50
  uint16_t cps;
51
  float capacity;
52
  float recv_delay;
53
} __attribute__((packed));
54

    
55
static struct metadata my_metadata;
56
static int cnt = 0;
57
static struct nodeID *me = NULL;
58
static unsigned char mTypes[] = {MSG_TYPE_TOPOLOGY,MSG_TYPE_TMAN};
59
static struct nodeID ** neighbors;
60

    
61
static void update_metadata(void) {
62
        my_metadata.cb_size = am_i_source() ? 0 : get_cb_size();
63
        my_metadata.recv_delay = get_receive_delay();
64
        my_metadata.cps = get_chunks_per_sec();
65
        my_metadata.capacity = get_capacity();
66
}
67

    
68
static int simpleRanker (const void *tin, const void *p1in, const void *p2in) {
69

    
70
        double t,p1,p2;
71
        t = *((const double *)tin);
72
        p1 = *((const double *)p1in);
73
        p2 = *((const double *)p2in);
74

    
75
        if (isnan(t) || (isnan(p1) && isnan(p2))) return 0;
76
        else if (isnan(p1)) return 2;
77
        else if (isnan(p2)) return 1;
78
        else return (fabs(t-p1) == fabs(t-p2))?0:(fabs(t-p1) < fabs(t-p2))?1:2;
79

    
80
}
81

    
82
int topologyInit(struct nodeID *myID, const char *config)
83
{
84
        int i;
85
        for (i=0;i<2;i++)
86
                bind_msg_type(mTypes[i]);
87
        update_metadata();
88
        me = myID;
89
        return (topInit(myID, &my_metadata, sizeof(my_metadata), config) && tmanInit(myID,&my_metadata, sizeof(my_metadata),rankFunct,0));
90
}
91

    
92
void topologyShutdown(void)
93
{
94
}
95

    
96
int topoAddNeighbour(struct nodeID *neighbour, void *metadata, int metadata_size)
97
{
98
        // TODO: check this!! Just to use this function to bootstrap ncast...
99
        struct metadata m = {0};        //TODO: check what metadata option should mean
100

    
101
        if (counter < TMAN_MAX_IDLE)
102
                return topAddNeighbour(neighbour,&m,sizeof(m));
103
        else return tmanAddNeighbour(neighbour,&m,sizeof(m));
104
}
105

    
106
static int topoParseData(const uint8_t *buff, int len)
107
{
108
        int res = -1,ncs = 0,msize;
109
        const struct nodeID **n; const void *m;
110
        if (!buff || buff[0] == MSG_TYPE_TOPOLOGY) {
111
                res = topParseData(buff,len);
112
//                if (counter <= TMAN_MAX_IDLE)
113
//                        counter++;
114
        }
115
        if (counter >= TMAN_MAX_IDLE && (!buff || buff[0] == MSG_TYPE_TMAN))
116
        {
117
                n = topGetNeighbourhood(&ncs);
118
                if (ncs) {
119
                m = topGetMetadata(&msize);
120
                res = tmanParseData(buff,len,n,ncs,m,msize);
121
                }
122
        }
123
  return res;
124
}
125

    
126
static const struct nodeID **topoGetNeighbourhood(int *n)
127
{
128
        int i; double d;
129
        if (counter > TMAN_MAX_IDLE) {
130
                uint8_t *mdata; int msize;
131
                *n = tmanGetNeighbourhoodSize();
132
                if (neighbors) free(neighbors);
133
                neighbors = calloc(*n,sizeof(struct nodeID *));
134
                tmanGetMetadata(&msize);
135
                mdata = calloc(*n,msize);
136
                tmanGivePeers(*n,neighbors,(void *)mdata);
137

    
138
                if (cnt % TMAN_LOG_EVERY == 0) {
139
                        fprintf(stderr,"abouttopublish,%s,%s,,Tman_chunk_delay,%f\n",node_addr(me),node_addr(me),my_metadata.recv_delay);
140
                        for (i=0;i<(*n) && i<NEIGHBORHOOD_TARGET_SIZE;i++) {
141
                                d = *((double *)(mdata+i*msize));
142
                                fprintf(stderr,"abouttopublish,%s,",node_addr(me));
143
                                fprintf(stderr,"%s,,Tman_chunk_delay,%f\n",node_addr(neighbors[i]),d);
144
                        }
145
                        fprintf(stderr,"abouttopublish,%s,%s,,Tman_neighborhood_size,%d\n\n",node_addr(me),node_addr(me),*n);
146
                }
147

    
148
                free(mdata);
149
                return (const struct nodeID **)neighbors;
150
        }
151
        else
152
                return topGetNeighbourhood(n);
153
}
154

    
155
static void topoAddToBL (struct nodeID *id)
156
{
157
        if (counter >= TMAN_MAX_IDLE)
158
                tmanAddToBlackList(id);
159
//        else
160
                topAddToBlackList(id);
161
}
162

    
163
void add_peer(const struct nodeID *id, const struct metadata *m)
164
{
165
      dprintf("Adding %s to neighbourhood! cb_size:%d\n", node_addr(id), m?m->cb_size:-1);
166
      peerset_add_peer(pset, id);
167
      if (m) peerset_get_peer(pset, id)->cb_size = m->cb_size;
168
      if (m) peerset_get_peer(pset, id)->capacity = m->capacity;
169
      /* add measures here */
170
      add_measures(id);
171
      send_bmap(id);
172
}
173

    
174
void remove_peer(const struct nodeID *id)
175
{
176
      dprintf("Removing %s from neighbourhood!\n", node_addr(id));
177
      /* add measures here */
178
      delete_measures(id);
179
      peerset_remove_peer(pset, id);
180
}
181

    
182
//get the rtt. Currenly only MONL version is supported
183
static double get_rtt_of(const struct nodeID* n){
184
#ifdef MONL
185
  return get_rtt(n);
186
#else
187
  return NAN;
188
#endif
189
}
190

    
191
//get the declared capacity of a node
192
static double get_capacity_of(const struct nodeID* n){
193
  struct peer *p = peerset_get_peer(pset, n);
194
  if (p) {
195
    return p->capacity;
196
  }
197

    
198
  return NAN;
199
}
200

    
201
//returns: 1:yes 0:no -1:unknown
202
int desiredness(const struct nodeID* n) {
203
  double rtt = get_rtt_of(n);
204
  double bw =  get_capacity_of(n);
205

    
206
  if ((isnan(rtt) && finite(desired_rtt)) || (isnan(bw) && desired_bw > 0)) {
207
    return -1;
208
  } else if ((isnan(rtt) || rtt <= desired_rtt) && (isnan(bw) || bw >= desired_bw)) {
209
    return 1;
210
  }
211

    
212
  return 0;
213
}
214

    
215
bool is_desired(const struct nodeID* n) {
216
  return (desiredness(n) == 1);
217
}
218

    
219
// currently it just makes the peerset grow
220
void update_peers(struct nodeID *from, const uint8_t *buff, int len)
221
{
222
  int n_ids, metasize, i, newids_size, max_ids;
223
  static const struct nodeID **newids;
224
  static const struct metadata *metas;
225
  struct peer *peers;
226
  struct timeval tnow, told;
227

    
228
  if timerisset(&tout_bmap) {
229
    gettimeofday(&tnow, NULL);
230
    timersub(&tnow, &tout_bmap, &told);
231
    peers = peerset_get_peers(pset);
232
    for (i = 0; i < peerset_size(pset); i++) {
233
      if ( (!timerisset(&peers[i].bmap_timestamp) && timercmp(&peers[i].creation_timestamp, &told, <) ) ||
234
           ( timerisset(&peers[i].bmap_timestamp) && timercmp(&peers[i].bmap_timestamp, &told, <)     )   ) {
235
        ftprintf(stderr,"Topo: dropping inactive %s (peers:%d)\n", node_addr(peers[i].id), peerset_size(pset));
236
        //if (peerset_size(pset) > 1) {        // avoid dropping our last link to the world
237
        topoAddToBL(peers[i].id);
238
        remove_peer(peers[i--].id);
239
        //}
240
      }
241
    }
242
  }
243

    
244
  if (cnt++ % 100 == 0) {
245
        update_metadata();
246
    if (counter > TMAN_MAX_IDLE) {
247
        tmanChangeMetadata(&my_metadata,sizeof(my_metadata));
248
    }
249
  }
250

    
251
  topoParseData(buff, len);
252

    
253
  if (!buff) {
254
    reg_neigh_size(peerset_size(pset));
255
    return;
256
  }
257

    
258
  peers = peerset_get_peers(pset);
259
  n_ids = peerset_size(pset);
260
  newids = topoGetNeighbourhood(&newids_size);        //TODO handle both tman and topo
261
  metas = topGetMetadata(&metasize);        //TODO: check metasize
262
  max_ids = n_ids + newids_size;
263
  ftprintf(stderr,"Topo modify start peers:%d candidates:%d\n", n_ids, newids_size);
264
  {
265
    int desired_part;
266
    const struct nodeID *oldids[max_ids], *nodeids[max_ids], *desireds[max_ids], *selecteds[max_ids], *others[max_ids], *toadds[max_ids], *toremoves[max_ids];
267
    size_t oldids_size, nodeids_size, desireds_size, selecteds_size, others_size, toadds_size, toremoves_size;
268
    nodeids_size = desireds_size = selecteds_size = others_size = toadds_size = toremoves_size = max_ids;
269

    
270
    for (i = 0, oldids_size = 0; i < peerset_size(pset); i++) {
271
      oldids[oldids_size++] = peers[i].id;
272
      fprintf(stderr," %s - RTT: %f\n", node_addr(peers[i].id) , get_rtt_of(peers[i].id));
273
    }
274

    
275

    
276
    //compose list of nodeids
277
    nidset_add(nodeids, &nodeids_size, oldids, oldids_size, newids, newids_size);
278

    
279
    // select the alpha_target portion of desired peers
280
    desired_part = (1 - alpha_target) * NEIGHBORHOOD_TARGET_SIZE;
281
    nidset_filter(desireds, &desireds_size, nodeids, nodeids_size, is_desired);
282
    nidset_shuffle(desireds, desireds_size);
283
    selecteds_size = MIN(desireds_size,desired_part);
284
    memcpy(selecteds, desireds, selecteds_size * sizeof(selecteds[0]));
285

    
286
    // random from the rest
287
    nidset_complement(others, &others_size, nodeids, nodeids_size, selecteds, selecteds_size);
288
    nidset_shuffle(others, others_size);
289
    nidset_add_i(selecteds, &selecteds_size, max_ids, others, NEIGHBORHOOD_TARGET_SIZE ? MIN(others_size, NEIGHBORHOOD_TARGET_SIZE - selecteds_size) : others_size);
290

    
291
    fprintf(stderr,"Topo modify (from:%ld sel:%ld) - desired: %ld of %ld (target:%d sel:%ld); random: from %ld (sel:%ld)\n",
292
            (long)nodeids_size, (long)selecteds_size, (long)desireds_size, (long)nodeids_size, desired_part, (long) MIN(desireds_size,desired_part), (long)others_size, (long)selecteds_size - MIN(desireds_size, desired_part));
293
    // add new ones
294
    nidset_complement(toadds, &toadds_size, selecteds, selecteds_size, oldids, oldids_size);
295
    for (i = 0; i < toadds_size; i++) {
296
      size_t j;
297
      //searching for the metadata
298
      if (nidset_find(&j, newids, newids_size, toadds[i])) {
299
        fprintf(stderr," adding %s\n", node_addr(toadds[i]));
300
        add_peer(newids[j], &metas[j]);
301
      } else {
302
        fprintf(stderr," Error: missing metadata for %s\n", node_addr(toadds[i]));
303
      }
304
    }
305

    
306
    // finally, remove those not needed
307
    fprintf(stderr,"Topo remove start (peers:%d)\n", n_ids);
308
    nidset_complement(toremoves, &toremoves_size, nodeids, nodeids_size, selecteds, selecteds_size);
309
    for (i = 0; i < toremoves_size; i++) {
310
      fprintf(stderr," removing %s\n", node_addr(toremoves[i]));
311
      remove_peer(toremoves[i]);
312
    }
313
    fprintf(stderr,"Topo remove end\n");
314
  }
315

    
316
  reg_neigh_size(peerset_size(pset));
317
}
318

    
319
struct peer *nodeid_to_peer(const struct nodeID* id, int reg)
320
{
321
  struct peer *p = peerset_get_peer(pset, id);
322
  if (!p) {
323
    //fprintf(stderr,"warning: received message from unknown peer: %s!%s\n",node_addr(id), reg ? " Adding it to pset." : "");
324
    if (reg) {
325
      add_peer(id,NULL);
326
      fprintf(stderr,"Topo: ext adding %s (peers:%d)\n", node_addr(id), peerset_size(pset));
327
      p = peerset_get_peer(pset,id);
328
    }
329
  }
330

    
331
  return p;
332
}
333

    
334
int peers_init(void)
335
{
336
  fprintf(stderr,"peers_init\n");
337
  pset = peerset_init(0);
338
  return pset ? 1 : 0;
339
}
340

    
341
struct peerset *get_peers(void)
342
{
343
  return pset;
344
}