Statistics
| Branch: | Revision:

grapes / som / TopologyManager / tman.c @ 9eab8f0b

History | View | Annotate | Download (7.15 KB)

1
/*
2
 *  Copyright (c) 2010 Marco Biazzini
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 <string.h>
13

    
14
#include "net_helper.h"
15
#include "topmanager.h"
16
#include "topocache.h"
17
#include "topo_proto.h"
18
#include "proto.h"
19
#include "msg_types.h"
20
#include "tman.h"
21

    
22
#define TMAN_INIT_PEERS 20 // max # of neighbors in local cache (should be > than the next)
23
#define TMAN_MAX_PREFERRED_PEERS 10 // # of peers to choose a receiver among (should be < than the previous)
24
#define TMAN_MAX_GOSSIPING_PEERS 10 // # size of the view to be sent to receiver peer (should be <= than the previous)
25
#define TMAN_IDLE_TIME 10 // # of iterations to wait before switching to inactive state
26
#define TMAN_STD_PERIOD 3000000
27
#define TMAN_INIT_PERIOD 1000000
28

    
29
//static const int MAX_PEERS = TMAN_MAX_PEERS;
30
static  int MAX_PREFERRED_PEERS = TMAN_MAX_PREFERRED_PEERS;
31
static  int MAX_GOSSIPING_PEERS = TMAN_MAX_GOSSIPING_PEERS;
32
static  int IDLE_TIME = TMAN_IDLE_TIME;
33

    
34
static uint64_t currtime;
35
static int cache_size = TMAN_INIT_PEERS;
36
static struct peer_cache *local_cache = NULL;
37
static int period = TMAN_INIT_PERIOD;
38
static int active = 0;
39
static int do_resize = 0;
40
static void *mymeta;
41

    
42
static tmanRankingFunction rankFunct;
43

    
44

    
45
// TODO: first parameter may be discarded, because it is always called with local_cache...
46
static struct peer_cache *rank_cache (const struct peer_cache *c, const struct nodeID *target, const void *target_meta)
47
{
48
        struct peer_cache *res;
49
        int i, msize;
50
        const uint8_t *mdata;
51

    
52
        mdata = get_metadata(c,&msize);
53
        res = cache_init(cache_size,msize);
54
        if (res == NULL) {
55
          return res;
56
        }
57

    
58
        for (i=0; nodeid(c,i); i++) {
59
                if (!nodeid_equal(nodeid(c,i),target))
60
                        cache_add_ranked(res,nodeid(c,i),mdata+i*msize,msize, rankFunct, target_meta);
61
        }
62

    
63
        return res;
64
}
65

    
66

    
67
static uint64_t gettime(void)
68
{
69
  struct timeval tv;
70

    
71
  gettimeofday(&tv, NULL);
72

    
73
  return tv.tv_usec + tv.tv_sec * 1000000ull;
74
}
75

    
76
int tmanInit(struct nodeID *myID, void *metadata, int metadata_size, ranking_function rfun, int gossip_peers)
77
{
78
  rankFunct = rfun;
79
  topo_proto_init(myID, metadata, metadata_size);
80
  mymeta = metadata;
81
  
82
  local_cache = cache_init(cache_size, metadata_size);
83
  if (local_cache == NULL) {
84
    return -1;
85
  }
86
  IDLE_TIME = TMAN_IDLE_TIME;
87
  if (gossip_peers) {
88
    MAX_GOSSIPING_PEERS = gossip_peers;
89
  }
90
  MAX_PREFERRED_PEERS = TMAN_MAX_PREFERRED_PEERS;
91
  active = IDLE_TIME;
92
  currtime = gettime();
93

    
94
  return 0;
95
}
96

    
97
int tmanGivePeers (int n, struct nodeID **peers, void *metadata)
98
{
99
        int metadata_size;
100
        const uint8_t *mdata;
101
        int i;
102

    
103
        mdata = get_metadata(local_cache, &metadata_size);
104
        for (i=0; nodeid(local_cache, i) && (i < n); i++) {
105
                        peers[i] = nodeid(local_cache,i);
106
                        if (metadata_size)
107
                                memcpy((uint8_t *)metadata + i * metadata_size, mdata + i * metadata_size, metadata_size);
108
        }
109
        if (i != n) {
110
                active = 0;
111
        }
112

    
113
        return i;
114
}
115

    
116
int tmanGetNeighbourhoodSize(void)
117
{
118
  int i;
119

    
120
  for (i = 0; nodeid(local_cache, i); i++);
121

    
122
  return i;
123
}
124

    
125
static int time_to_send(void)
126
{
127
        if (gettime() - currtime > period) {
128
                currtime += period;
129
                if (active > 0)
130
                        return 1;
131
        }
132

    
133
  return 0;
134
}
135

    
136
int tmanAddNeighbour(struct nodeID *neighbour, void *metadata, int metadata_size)
137
{
138
  if (cache_add_ranked(local_cache, neighbour, metadata, metadata_size, rankFunct, mymeta) < 0) {
139
    return -1;
140
  }
141

    
142
  return 1;//topo_query_peer(local_cache, neighbour, TMAN_QUERY);
143
}
144

    
145

    
146
// not self metadata, but neighbors'.
147
const void *tmanGetMetadata(int *metadata_size)
148
{
149
  return get_metadata(local_cache, metadata_size);
150
}
151

    
152

    
153
int tmanChangeMetadata(struct nodeID *peer, void *metadata, int metadata_size)
154
{
155
  if (topo_proto_metadata_update(peer, metadata, metadata_size) <= 0) {
156
    return -1;
157
  }
158
  mymeta = metadata;
159

    
160
  return 1;
161
}
162

    
163

    
164
int tmanParseData(const uint8_t *buff, int len, const struct nodeID **peers, int size, const void *metadata, int metadata_size)
165
{
166
        int msize,s;
167
        const uint8_t *mdata;
168
        struct peer_cache *new;
169
        int source;
170

    
171
        if (len) {
172
                const struct topo_header *h = (const struct topo_header *)buff;
173
                struct peer_cache *remote_cache; int idx;
174

    
175
            if (h->protocol != MSG_TYPE_TOPOLOGY) {
176
              fprintf(stderr, "TMAN: Wrong protocol!\n");
177
              return -1;
178
            }
179

    
180
            if (h->type != TMAN_QUERY && h->type != TMAN_REPLY) {
181
              return -1;
182
            }
183
                remote_cache = entries_undump(buff + sizeof(struct topo_header), len - sizeof(struct topo_header));
184
                mdata = get_metadata(remote_cache,&msize);
185
                get_metadata(local_cache,&s);
186

    
187
                if (msize != s) {
188
                        fprintf(stderr, "TMAN: Metadata size mismatch! -> local (%d) != received (%d)\n",
189
                                s, msize);
190
                        return 1;
191
                }
192

    
193
                if (h->type == TMAN_QUERY) {
194
//                        fprintf(stderr, "\tTman: Parsing received msg to reply...\n");
195
                        new = rank_cache(local_cache, nodeid(remote_cache, 0), get_metadata(remote_cache, &msize));
196
                        if (new) {
197
                                tman_reply(remote_cache, new);
198
                                cache_free(new);
199
                                // TODO: put sender in tabu list (check list size, etc.), if any...
200
                        }
201
                }
202
                idx = cache_add_ranked(local_cache, nodeid(remote_cache,0), mdata, msize, rankFunct, mymeta);
203
//                fprintf(stderr, "\tidx = %d\n",idx);
204
                new = merge_caches_ranked(local_cache, remote_cache, cache_size, &source, rankFunct, mymeta);
205

    
206
//                  newsize = new_cache->current_size>c1->cache_size?new_cache->current_size:c1->cache_size;
207
//                  newsize = cache_shrink(new_cache,(new_cache->cache_size-newsize));
208
//                  // TODO: check newsize??
209
//                  return new_cache;
210

    
211
                cache_free(remote_cache);
212
                if (new!=NULL) {
213
                  cache_free(local_cache);
214
                  local_cache = new;
215
                }
216
                if (source) {
217
                  if (idx>=0 || source!=1) {
218
                          active = IDLE_TIME;
219
                  }
220
                  else {
221
                          period = TMAN_STD_PERIOD;
222
                          if (active>0) active--;
223
                  }
224

    
225
                  do_resize = 0;
226
                }
227
        }
228

    
229
  if (time_to_send()) {
230
        uint8_t *meta;
231
        struct nodeID *chosen;
232

    
233
        cache_update(local_cache);
234
        if (active == 0) {
235
                struct peer_cache *ncache;
236
                int j, res = 0;
237

    
238
                ncache = cache_init(size,metadata_size);
239
                for (j=0;j<size && res!=-3;j++)
240
                        res = cache_add_ranked(ncache, peers[j],(const uint8_t *)metadata + j * metadata_size, metadata_size, rankFunct, mymeta);
241
                if (nodeid(ncache, 0)) {
242
                        new = merge_caches_ranked(local_cache, ncache, cache_size, &source, rankFunct, mymeta);
243
                        if (new) {
244
                                cache_free(local_cache);
245
                                local_cache = new;
246
                        }
247
                        if (source!=1) {
248
                                active = TMAN_IDLE_TIME;
249
                        }
250
                        do_resize = 0;
251
                }
252
                cache_free(ncache);
253
        }
254
                
255
        mdata = get_metadata(local_cache,&msize);
256
        chosen = rand_peer(local_cache, &meta);                //MAX_PREFERRED_PEERS
257
        new = rank_cache(local_cache, chosen, meta);
258
        if (new==NULL) {
259
                fprintf(stderr, "TMAN: No cache could be sent to remote peer!\n");
260
                return 1;
261
        }
262
        tman_query_peer(new, chosen);
263
        cache_free(new);
264
  }
265

    
266
  return 0;
267
}
268

    
269

    
270

    
271
// limit : at most it doubles the current cache size...
272
int tmanGrowNeighbourhood(int n)
273
{
274
        if (n<=0 || do_resize)
275
                return -1;
276
        n = n>cache_size?cache_size:n;
277
        cache_size += n;
278
        do_resize = 1;
279
        return cache_size;
280
}
281

    
282

    
283
int tmanShrinkNeighbourhood(int n)
284
{
285
        if (n<=0 || n>=cache_size || do_resize)
286
                return -1;
287
        cache_size -= n;
288
        do_resize = 1;
289
        return cache_size;
290
}
291