aboutsummaryrefslogtreecommitdiff
path: root/src/rps/rps-sampler_client.c
blob: f6e98ce2976f4973f76d0434764bae641e5aff23 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
     This file is part of GNUnet.
     Copyright (C)

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file rps/gnunet-service-rps_sampler.c
 * @brief sampler implementation
 * @author Julius Bünger
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_statistics_service.h"
#include "rps.h"

#include "rps-sampler_common.h"
#include "gnunet-service-rps_sampler.h"
#include "gnunet-service-rps_sampler_elem.h"

#include <math.h>
#include <inttypes.h>

#include "rps-test_util.h"

#define LOG(kind, ...) GNUNET_log_from (kind, "rps-sampler", __VA_ARGS__)


// multiple 'clients'?

// TODO check for overflows

// TODO align message structs

// hist_size_init, hist_size_max

/***********************************************************************
* WARNING: This section needs to be reviewed regarding the use of
* functions providing (pseudo)randomness!
***********************************************************************/

// TODO care about invalid input of the caller (size 0 or less...)

/**
 * @brief Callback called each time a new peer was put into the sampler
 *
 * @param cls A possibly given closure
 */
typedef void
(*SamplerNotifyUpdateCB) (void *cls);

/**
 * @brief Context for a callback. Contains callback and closure.
 *
 * Meant to be an entry in an DLL.
 */
struct SamplerNotifyUpdateCTX
{
  /**
   * @brief The Callback to call on updates
   */
  SamplerNotifyUpdateCB notify_cb;

  /**
   * @brief The according closure.
   */
  void *cls;

  /**
   * @brief Next element in DLL.
   */
  struct SamplerNotifyUpdateCTX *next;

  /**
   * @brief Previous element in DLL.
   */
  struct SamplerNotifyUpdateCTX *prev;
};


/**
 * Type of function used to differentiate between modified and not modified
 * Sampler.
 */
typedef void
(*RPS_get_peers_type) (void *cls);


/**
 * Get one random peer out of the sampled peers.
 *
 * We might want to reinitialise this sampler after giving the
 * corrsponding peer to the client.
 */
static void
sampler_mod_get_rand_peer (void *cls);


/**
 * Closure to _get_n_rand_peers_ready_cb()
 */
struct RPS_SamplerRequestHandle
{
  /**
   * DLL
   */
  struct RPS_SamplerRequestHandle *next;
  struct RPS_SamplerRequestHandle *prev;

  /**
   * Number of peers we are waiting for.
   */
  uint32_t num_peers;

  /**
   * Number of peers we currently have.
   */
  uint32_t cur_num_peers;

  /**
   * Pointer to the array holding the ids.
   */
  struct GNUNET_PeerIdentity *ids;

  /**
   * Head and tail for the DLL to store the tasks for single requests
   */
  struct GetPeerCls *gpc_head;
  struct GetPeerCls *gpc_tail;

  /**
   * Sampler.
   */
  struct RPS_Sampler *sampler;

  /**
   * Callback to be called when all ids are available.
   */
  RPS_sampler_n_rand_peers_ready_cb callback;

  /**
   * Closure given to the callback
   */
  void *cls;
};


/**
 * Closure to _get_rand_peer_info()
 */
struct RPS_SamplerRequestHandleSingleInfo
{
  /**
   * DLL
   */
  struct RPS_SamplerRequestHandleSingleInfo *next;
  struct RPS_SamplerRequestHandleSingleInfo *prev;

  /**
   * Pointer to the id
   */
  struct GNUNET_PeerIdentity *id;

  /**
   * Head and tail for the DLL to store the tasks for single requests
   */
  struct GetPeerCls *gpc_head;
  struct GetPeerCls *gpc_tail;

  /**
   * Sampler.
   */
  struct RPS_Sampler *sampler;

  /**
   * Callback to be called when all ids are available.
   */
  RPS_sampler_sinlge_info_ready_cb callback;

  /**
   * Closure given to the callback
   */
  void *cls;
};


///**
// * Global sampler variable.
// */
// struct RPS_Sampler *sampler;


/**
 * The minimal size for the extended sampler elements.
 */
static size_t min_size;

/**
 * The maximal size the extended sampler elements should grow to.
 */
static size_t max_size;

/**
 * The size the extended sampler elements currently have.
 */
// static size_t extra_size;

/**
 * Inedex to the sampler element that is the next to be returned
 */
static uint32_t client_get_index;


/**
 * Initialise a modified tuple of sampler elements.
 *
 * @param init_size the size the sampler is initialised with
 * @param max_round_interval maximum time a round takes
 * @return a handle to a sampler that consists of sampler elements.
 */
struct RPS_Sampler *
RPS_sampler_mod_init (size_t init_size,
                      struct GNUNET_TIME_Relative max_round_interval)
{
  struct RPS_Sampler *sampler;

  /* Initialise context around extended sampler */
  min_size = 10; // TODO make input to _samplers_init()
  max_size = 1000; // TODO make input to _samplers_init()

  sampler = GNUNET_new (struct RPS_Sampler);
  sampler->max_round_interval = max_round_interval;
  sampler->get_peers = sampler_mod_get_rand_peer;
  // sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
  // GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);

  client_get_index = 0;

  // GNUNET_assert (init_size == sampler->sampler_size);

  RPS_sampler_resize (sampler, init_size);

  return sampler;
}


// /**
//  * @brief Compute the probability that we already observed all peers from a
//  * biased stream of peer ids.
//  *
//  * Deficiency factor:
//  * As introduced by Brahms: Factor between the number of unique ids in a
//  * truly random stream and number of unique ids in the gossip stream.
//  *
//  * @param num_peers_estim The estimated number of peers in the network
//  * @param num_peers_observed The number of peers the given element has observed
//  * @param deficiency_factor A factor that catches the 'bias' of a random stream
//  * of peer ids
//  *
//  * @return The estimated probability
//  */
// static double
// prob_observed_n_peers (uint32_t num_peers_estim,
//                        uint32_t num_peers_observed,
//                        double deficiency_factor)
// {
//   uint32_t num_peers = num_peers_estim * (1 / deficiency_factor);
//   uint64_t sum = 0;
// 
//   for (uint32_t i = 0; i < num_peers; i++)
//   {
//     uint64_t a = pow (-1, num_peers - i);
//     uint64_t b = binom (num_peers, i);
//     uint64_t c = pow (i, num_peers_observed);
//     sum += a * b * c;
//   }
// 
//   return sum / (double) pow (num_peers, num_peers_observed);
// }


/**
 * Get one random peer out of the sampled peers.
 *
 * This reinitialises the queried sampler element.
 */
static void
sampler_mod_get_rand_peer (void *cls)
{
  struct GetPeerCls *gpc = cls;
  struct RPS_SamplerElement *s_elem;
  struct GNUNET_TIME_Relative last_request_diff;
  struct RPS_Sampler *sampler;
  double prob_observed_n;
  uint32_t num_observed;

  gpc->get_peer_task = NULL;
  gpc->notify_ctx = NULL;
  GNUNET_assert ((NULL != gpc->req_handle) ||
                 (NULL != gpc->req_single_info_handle));
  if (NULL != gpc->req_handle)
    sampler = gpc->req_handle->sampler;
  else
    sampler = gpc->req_single_info_handle->sampler;

  LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");

  /* Cycle the #client_get_index one step further */
  client_get_index = (client_get_index + 1) % sampler->sampler_size;

  s_elem = sampler->sampler_elements[client_get_index];
  *gpc->id = s_elem->peer_id;
  GNUNET_assert (NULL != s_elem);

  if (EMPTY == s_elem->is_empty)
  {
    LOG (GNUNET_ERROR_TYPE_DEBUG,
         "Sampler_mod element empty, rescheduling.\n");
    GNUNET_assert (NULL == gpc->notify_ctx);
    gpc->notify_ctx =
      sampler_notify_on_update (sampler,
                                &sampler_mod_get_rand_peer,
                                gpc);
    return;
  }

  /* Check whether we may use this sampler to give it back to the client */
  if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us !=
      s_elem->last_client_request.abs_value_us)
  {
    // TODO remove this condition at least for the client sampler
    last_request_diff =
      GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
                                           GNUNET_TIME_absolute_get ());
    /* We're not going to give it back now if it was
     * already requested by a client this round */
    if (last_request_diff.rel_value_us <
        sampler->max_round_interval.rel_value_us)
    {
      LOG (GNUNET_ERROR_TYPE_DEBUG,
           "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
      ///* How many time remains untile the next round has started? */
      // inv_last_request_diff =
      //  GNUNET_TIME_absolute_get_difference (last_request_diff,
      //                                       sampler->max_round_interval);
      // add a little delay
      /* Schedule it one round later */
      GNUNET_assert (NULL == gpc->notify_ctx);
      gpc->notify_ctx =
        sampler_notify_on_update (sampler,
                                  &sampler_mod_get_rand_peer,
                                  gpc);
      return;
    }
  }
  if (2 > s_elem->num_peers)
  {
    LOG (GNUNET_ERROR_TYPE_DEBUG,
         "This s_elem saw less than two peers -- scheduling for later\n");
    GNUNET_assert (NULL == gpc->notify_ctx);
    gpc->notify_ctx =
      sampler_notify_on_update (sampler,
                                &sampler_mod_get_rand_peer,
                                gpc);
    return;
  }
  /* compute probability */
  /* FIXME: Currently disabled due to numerical limitations */
  prob_observed_n = 0; // Inititialise to some value
  // prob_observed_n = prob_observed_n_peers (sampler->num_peers_estim,
  //                                         s_elem->num_peers,
  //                                         sampler->deficiency_factor);
  // LOG (GNUNET_ERROR_TYPE_DEBUG,
  //    "Computed sample - prob %f, %" PRIu32 " peers, n: %" PRIu32 ", roh: %f\n",
  //    prob_observed_n,
  //    s_elem->num_peers,
  //    sampler->num_peers_estim,
  //    sampler->deficiency_factor);
  ///* check if probability is above desired */
  // if (prob_observed_n < sampler->desired_probability)
  // {
  //  LOG (GNUNET_ERROR_TYPE_DEBUG,
  //      "Probability of having observed all peers (%f) too small ( < %f).\n",
  //      prob_observed_n,
  //      sampler->desired_probability);
  //  GNUNET_assert (NULL == gpc->notify_ctx);
  //  gpc->notify_ctx =
  //    sampler_notify_on_update (sampler,
  //                              &sampler_mod_get_rand_peer,
  //                              gpc);
  //  return;
  // }
  /* More reasons to wait could be added here */

//  GNUNET_STATISTICS_set (stats,
//                         "# client sampler element input",
//                         s_elem->num_peers,
//                         GNUNET_NO);
//  GNUNET_STATISTICS_set (stats,
//                         "# client sampler element change",
//                         s_elem->num_change,
//                         GNUNET_NO);

  num_observed = s_elem->num_peers;
  RPS_sampler_elem_reinit (s_elem);
  s_elem->last_client_request = GNUNET_TIME_absolute_get ();

  if (NULL != gpc->req_handle)
  {
    GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
                                 gpc->req_handle->gpc_tail,
                                 gpc);
  }
  else
  {
    GNUNET_CONTAINER_DLL_remove (gpc->req_single_info_handle->gpc_head,
                                 gpc->req_single_info_handle->gpc_tail,
                                 gpc);
  }
  gpc->cont (gpc->cont_cls, gpc->id, prob_observed_n, num_observed);
  GNUNET_free (gpc);
}


/* end of gnunet-service-rps.c */