Statistics
| Branch: | Revision:

streamers / topology.c @ 0877a9f6

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

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

    
21
#include "compatibility/timer.h"
22

    
23
#include "topology.h"
24
#include "streaming.h"
25
#include "dbg.h"
26
#include "measures.h"
27
#include "streamer.h"
28

    
29
double desired_rtt = 0.2;
30
double alpha_target = 0.5;
31

    
32
int NEIGHBORHOOD_TARGET_SIZE = 20;
33
double NEIGHBORHOOD_ROTATE_RATIO = 1.0;
34
#define TMAN_MAX_IDLE 10
35
#define TMAN_LOG_EVERY 1000
36

    
37
static struct peerset *pset;
38
static struct timeval tout_bmap = {10, 0};
39
static int counter = 0;
40
static int simpleRanker (const void *tin, const void *p1in, const void *p2in);
41
static tmanRankingFunction rankFunct = simpleRanker;
42
struct metadata {
43
  uint16_t cb_size;
44
  float recv_delay;
45
};
46
static struct metadata my_metadata;
47
static int cnt = 0;
48
static struct nodeID *me = NULL;
49
static unsigned char mTypes[] = {MSG_TYPE_TOPOLOGY,MSG_TYPE_TMAN};
50
static struct nodeID ** neighbors;
51

    
52
static void update_metadata(void) {
53

    
54
        my_metadata.cb_size = am_i_source() ? 0 : get_cb_size();
55
        my_metadata.recv_delay = get_receive_delay();
56
}
57

    
58
static int simpleRanker (const void *tin, const void *p1in, const void *p2in) {
59

    
60
        double t,p1,p2;
61
        t = *((const double *)tin);
62
        p1 = *((const double *)p1in);
63
        p2 = *((const double *)p2in);
64

    
65
        if (isnan(t) || (isnan(p1) && isnan(p2))) return 0;
66
        else if (isnan(p1)) return 2;
67
        else if (isnan(p2)) return 1;
68
        else return (fabs(t-p1) == fabs(t-p2))?0:(fabs(t-p1) < fabs(t-p2))?1:2;
69

    
70
}
71

    
72
int topologyInit(struct nodeID *myID, const char *config)
73
{
74
        int i;
75
        for (i=0;i<2;i++)
76
                bind_msg_type(mTypes[i]);
77
        update_metadata();
78
        me = myID;
79
        return (topInit(myID, &my_metadata, sizeof(my_metadata), config) && tmanInit(myID,&my_metadata, sizeof(my_metadata),rankFunct,0));
80
}
81

    
82
void topologyShutdown(void)
83
{
84
}
85

    
86
int topoAddNeighbour(struct nodeID *neighbour, void *metadata, int metadata_size)
87
{
88
        // TODO: check this!! Just to use this function to bootstrap ncast...
89
        struct metadata m = {0};        //TODO: check what metadata option should mean
90

    
91
        if (counter < TMAN_MAX_IDLE)
92
                return topAddNeighbour(neighbour,&m,sizeof(m));
93
        else return tmanAddNeighbour(neighbour,&m,sizeof(m));
94
}
95

    
96
static int topoParseData(const uint8_t *buff, int len)
97
{
98
        int res = -1,ncs = 0,msize;
99
        const struct nodeID **n; const void *m;
100
        if (!buff || buff[0] == MSG_TYPE_TOPOLOGY) {
101
                res = topParseData(buff,len);
102
//                if (counter <= TMAN_MAX_IDLE)
103
//                        counter++;
104
        }
105
        if (counter >= TMAN_MAX_IDLE && (!buff || buff[0] == MSG_TYPE_TMAN))
106
        {
107
                n = topGetNeighbourhood(&ncs);
108
                if (ncs) {
109
                m = topGetMetadata(&msize);
110
                res = tmanParseData(buff,len,n,ncs,m,msize);
111
                }
112
        }
113
  return res;
114
}
115

    
116
static const struct nodeID **topoGetNeighbourhood(int *n)
117
{
118
        int i; double d;
119
        if (counter > TMAN_MAX_IDLE) {
120
                uint8_t *mdata; int msize;
121
                *n = tmanGetNeighbourhoodSize();
122
                if (neighbors) free(neighbors);
123
                neighbors = calloc(*n,sizeof(struct nodeID *));
124
                tmanGetMetadata(&msize);
125
                mdata = calloc(*n,msize);
126
                tmanGivePeers(*n,neighbors,(void *)mdata);
127

    
128
                if (cnt % TMAN_LOG_EVERY == 0) {
129
                        fprintf(stderr,"abouttopublish,%s,%s,,Tman_chunk_delay,%f\n",node_addr(me),node_addr(me),my_metadata.recv_delay);
130
                        for (i=0;i<(*n) && i<NEIGHBORHOOD_TARGET_SIZE;i++) {
131
                                d = *((double *)(mdata+i*msize));
132
                                fprintf(stderr,"abouttopublish,%s,",node_addr(me));
133
                                fprintf(stderr,"%s,,Tman_chunk_delay,%f\n",node_addr(neighbors[i]),d);
134
                        }
135
                        fprintf(stderr,"abouttopublish,%s,%s,,Tman_neighborhood_size,%d\n\n",node_addr(me),node_addr(me),*n);
136
                }
137

    
138
                free(mdata);
139
                return (const struct nodeID **)neighbors;
140
        }
141
        else
142
                return topGetNeighbourhood(n);
143
}
144

    
145
static void topoAddToBL (struct nodeID *id)
146
{
147
        if (counter >= TMAN_MAX_IDLE)
148
                tmanAddToBlackList(id);
149
//        else
150
                topAddToBlackList(id);
151
}
152

    
153
void add_peer(const struct nodeID *id, const struct metadata *m)
154
{
155
      dprintf("Adding %s to neighbourhood! cb_size:%d\n", node_addr(id), m?m->cb_size:-1);
156
      peerset_add_peer(pset, id);
157
      if (m) peerset_get_peer(pset, id)->cb_size = m->cb_size;
158
      /* add measures here */
159
      add_measures(id);
160
      send_bmap(id);
161
}
162

    
163
void remove_peer(struct nodeID *id)
164
{
165
      dprintf("Removing %s from neighbourhood!\n", node_addr(id));
166
      /* add measures here */
167
      delete_measures(id);
168
      peerset_remove_peer(pset, id);
169
}
170

    
171
//get the rtt. Currenly only MONL version is supported
172
static double get_rtt_of(struct nodeID* n){
173
#ifdef MONL
174
  double rtt = get_rtt(n);
175
#else
176
  return NAN;
177
#endif
178
}
179

    
180
//returns: 1:yes 0:no -1:unknown
181
int is_desired(struct nodeID* n) {
182
  double rtt = get_rtt_of(n);
183

    
184
  return isnan(rtt) ? -1 : ((rtt <= desired_rtt) ? 1 : 0);
185
}
186

    
187
// currently it just makes the peerset grow
188
void update_peers(struct nodeID *from, const uint8_t *buff, int len)
189
{
190
  int n_ids, metasize, i;
191
  static const struct nodeID **ids;
192
  static const struct metadata *metas;
193
  struct peer *peers;
194
  struct timeval tnow, told;
195

    
196
//  dprintf("Update peers: topo_msg:%d, ",len);
197
//  if (from) {
198
//    dprintf("from:%s, ",node_addr(from));
199
//    if (peerset_check(pset, from) < 0) {
200
//      topAddNeighbour(from, NULL, 0);        //@TODO: this is agressive
201
//      if (!NEIGHBORHOOD_TARGET_SIZE || peerset_size(pset) < NEIGHBORHOOD_TARGET_SIZE) {
202
//        add_peer(from);
203
//      }
204
//    }
205
//  }
206

    
207
  if (cnt++ % 100 == 0) {
208
        update_metadata();
209
    if (counter > TMAN_MAX_IDLE) {
210
        tmanChangeMetadata(&my_metadata,sizeof(my_metadata));
211
    }
212
  }
213

    
214
  topoParseData(buff, len);
215

    
216
  if (!buff) {
217
    reg_neigh_size(peerset_size(pset));
218
    return;
219
  }
220

    
221
  fprintf(stderr,"Topo modify start\n");
222
  peers = peerset_get_peers(pset);
223
  for (i = 0; i < peerset_size(pset); i++) {
224
    fprintf(stderr," %s - RTT: %f\n", node_addr(peers[i].id) , get_rtt_of(peers[i].id));
225
  }
226

    
227
  ids = topoGetNeighbourhood(&n_ids);        //TODO handle both tman and topo
228
  metas = topGetMetadata(&metasize);        //TODO: check metasize
229
  for(i = 0; i < n_ids; i++) {
230
    if(peerset_check(pset, ids[i]) < 0) {
231
      if (!NEIGHBORHOOD_TARGET_SIZE || peerset_size(pset) < NEIGHBORHOOD_TARGET_SIZE) {
232
        add_peer(ids[i],&metas[i]);
233
      } else {  //rotate neighbourhood
234
        if (rand()/((double)RAND_MAX + 1) < NEIGHBORHOOD_ROTATE_RATIO) {
235
          add_peer(ids[i],&metas[i]);
236
        }
237
      }
238
    }
239
  }
240

    
241
  if timerisset(&tout_bmap) {
242
    gettimeofday(&tnow, NULL);
243
    timersub(&tnow, &tout_bmap, &told);
244
    peers = peerset_get_peers(pset);
245
    for (i = 0; i < peerset_size(pset); i++) {
246
      if ( (!timerisset(&peers[i].bmap_timestamp) && timercmp(&peers[i].creation_timestamp, &told, <) ) ||
247
           ( timerisset(&peers[i].bmap_timestamp) && timercmp(&peers[i].bmap_timestamp, &told, <)     )   ) {
248
        //if (peerset_size(pset) > 1) {        // avoid dropping our last link to the world
249
        topoAddToBL(peers[i].id);
250
        remove_peer(peers[i--].id);
251
        //}
252
      }
253
    }
254
  }
255

    
256

    
257
  n_ids = peerset_size(pset);
258
  fprintf(stderr,"Topo remove start (peers:%d)\n", n_ids);
259
  while (NEIGHBORHOOD_TARGET_SIZE && peerset_size(pset) > NEIGHBORHOOD_TARGET_SIZE) { //reduce back neighbourhood to target size
260
    int n, desired, desired_not, desired_unknown;
261
    struct peer *ds[n_ids], *dns[n_ids], *dus[n_ids];
262
    double alpha;
263

    
264
    peers = peerset_get_peers(pset);
265
    n = peerset_size(pset);
266
    desired = desired_not = desired_unknown = 0;
267
    for (i = 0; i < n; i++) {
268
      switch (is_desired(peers[i].id)) {
269
        case 1:
270
          ds[desired++] = &peers[i];
271
          break;
272
        case 0:
273
          dns[desired_not++] = &peers[i];
274
          break;
275
        case -1:
276
          dus[desired_unknown++] = &peers[i];
277
          break;
278
        default:
279
          fprintf(stderr,"error with desiredness!\n");
280
          exit(1);
281
      }
282
    }
283
    alpha = (double) desired / n;
284
    fprintf(stderr," peers:%d desired:%d unknown:%d notdesired:%d alpha: %f (target:%f)\n",n, desired, desired_unknown, desired_not, alpha, alpha_target);
285

    
286
    if (alpha > alpha_target && desired > 0) {
287
      remove_peer(ds[rand() % desired]->id);
288
    } else if (alpha < alpha_target && desired_not > 0) {
289
      remove_peer(dns[rand() % desired_not]->id);
290
    } else if (desired_unknown > 0) {
291
      remove_peer(dus[rand() % desired_unknown]->id);
292
    } else {
293
      remove_peer(peers[rand() % n].id);
294
    }
295
  }
296
  fprintf(stderr,"Topo remove end\n");
297

    
298
  reg_neigh_size(peerset_size(pset));
299
}
300

    
301
struct peer *nodeid_to_peer(const struct nodeID* id, int reg)
302
{
303
  struct peer *p = peerset_get_peer(pset, id);
304
  if (!p) {
305
    //fprintf(stderr,"warning: received message from unknown peer: %s!%s\n",node_addr(id), reg ? " Adding it to pset." : "");
306
    if (reg) {
307
      add_peer(id,NULL);
308
      p = peerset_get_peer(pset,id);
309
    }
310
  }
311

    
312
  return p;
313
}
314

    
315
int peers_init(void)
316
{
317
  fprintf(stderr,"peers_init\n");
318
  pset = peerset_init(0);
319
  return pset ? 1 : 0;
320
}
321

    
322
struct peerset *get_peers(void)
323
{
324
  return pset;
325
}