Statistics
| Branch: | Revision:

grapes / som / TopologyManager / tman.c @ 56e6f951

History | View | Annotate | Download (7.05 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 = 0;
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
                return 1;
130
        }
131

    
132
  return 0;
133
}
134

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

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

    
144

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

    
151

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

    
159
  return 1;
160
}
161

    
162

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

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

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

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

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

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

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

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

    
222
                  do_resize = 0;
223
                }
224
        }
225

    
226
  if (time_to_send()) {
227
        uint8_t *meta;
228
        struct nodeID *chosen;
229

    
230
        cache_update(local_cache);
231
        if (active == 0) {
232
                struct peer_cache *ncache;
233
                int j;
234

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

    
264
  return 0;
265
}
266

    
267

    
268

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

    
280

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