-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathserver.C
496 lines (427 loc) · 12 KB
/
server.C
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
*
* Copyright (C) 2001 Frank Dabek ([email protected]),
* Frans Kaashoek ([email protected]),
* Massachusetts Institute of Technology
*
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <arpc.h>
#include <rpctypes.h>
#include <chord.h>
#include "dhash_common.h"
#include "dhash_impl.h"
#include "dhashcli.h"
#include <dhash_prot.h>
#include <chord_types.h>
#include <misc_utils.h>
#include <id_utils.h>
#include <location.h>
#include <locationtable.h>
#include <misc_utils.h>
#include "dhblock_srv.h"
#include "dhblock_chash_srv.h"
#include "dhblock_keyhash_srv.h"
#include "dhblock_noauth_srv.h"
#ifdef DMALLOC
#include <dmalloc.h>
#endif
#include <modlogger.h>
#define warning modlogger ("dhash", modlogger::WARNING)
#define info modlogger ("dhash", modlogger::INFO)
#define trace modlogger ("dhash", modlogger::TRACE)
#include <configurator.h>
struct dhash_config_init {
dhash_config_init ();
} dci;
dhash_config_init::dhash_config_init ()
{
bool ok = true;
#define set_int Configurator::only ().set_int
/** Whether or not to drop writes */
ok = ok && set_int ("dhash.drop_writes", 0);
/** Should replication run initially? */
ok = ok && set_int ("dhash.start_maintenance", 1);
ok = ok && set_int ("dhash.repair_timer", 300);
//plab hacks
ok = ok && set_int ("dhash.disable_db_env", 0);
assert (ok);
#undef set_int
}
// Things that read from Configurator
#define DECL_CONFIG_METHOD(name,key) \
u_long \
dhash::name () \
{ \
static bool initialized = false; \
static int v = 0; \
if (!initialized) { \
initialized = Configurator::only ().get_int (key, v); \
assert (initialized); \
} \
return v; \
}
DECL_CONFIG_METHOD(reptm, "dhash.repair_timer")
DECL_CONFIG_METHOD(dhash_disable_db_env, "dhash.disable_db_env")
#undef DECL_CONFIG_METHOD
// Pure virtual destructors still need definitions
dhash::~dhash () {}
ref<dhash>
dhash::produce_dhash (ptr<vnode> v, str dbsock, str msock, ptr<chord_trigger_t> t)
{
return New refcounted<dhash_impl> (v, dbsock, msock, t);
}
dhash_impl::~dhash_impl ()
{
}
dhash_impl::dhash_impl (ptr<vnode> node, str dbsock, str msock,
ptr<chord_trigger_t> t) :
host_node (node),
cli (NULL),
bytes_stored (0),
bytes_served (0),
objects_stored (0),
objects_served (0)
{
ptr<dhblock_srv> srv;
cli = New refcounted<dhashcli> (host_node, mkref(this));
ptr<chord_trigger_t> dhashtrigger = chord_trigger_t::alloc (
wrap (this, &dhash_impl::srv_ready, t));
srv = New refcounted<dhblock_chash_srv> (node, cli, msock,
dbsock, "chash.c", dhashtrigger);
blocksrv.insert (DHASH_CONTENTHASH, srv);
srv = New refcounted<dhblock_keyhash_srv> (node, cli, msock,
dbsock, "keyhash.k",
dhashtrigger);
blocksrv.insert (DHASH_KEYHASH, srv);
srv = New refcounted<dhblock_noauth_srv> (node, cli, msock,
dbsock, "noauth.n",
dhashtrigger);
blocksrv.insert (DHASH_NOAUTH, srv);
}
void
dhash_impl::srv_ready (ptr<chord_trigger_t> t)
{
// RPC demux
trace << host_node->my_ID () << " registered dhash_program_1\n";
host_node->addHandler (dhash_program_1, wrap(this, &dhash_impl::dispatch));
start_maint ();
}
void
dhash_impl::start_maint ()
{
int v;
bool ok = Configurator::only ().get_int ("dhash.start_maintenance", v);
if (!ok || v)
start (true);
}
void
dhash_impl::fetchcomplete_done (int nonce, chord_node sender,
dhash_stat err, bool present, u_int32_t sz)
{
// warn << host_node->my_ID () << ": dhash_impl::fetchcomplete_done: "
// << nonce << " " << sender << " " << err << "\n";
if (err == DHASH_NOENT) { // block wasn't in db. notify sender.
ref<s_dhash_insertarg> arg = New refcounted<s_dhash_insertarg> ();
//fill in just enough fields so that it marshalls
// only nonce and type will be examined on the other end
arg->key = chordID (0);
arg->data.setsize (0);
arg->nonce = nonce;
arg->type = DHASH_NOENT_NOTIFY;
doRPC (sender, dhash_program_1, DHASHPROC_FETCHCOMPLETE,
arg, NULL, aclnt_cb_null);
} else {
objects_served++;
bytes_served += sz;
}
}
long
dhash_impl::register_fetch_callback (cbfetch cb)
{
fcb_state *fcb = New fcb_state (cb);
fetch_cbs.insert (fcb);
return fcb->nonce;
}
void
dhash_impl::unregister_fetch_callback (long nonce)
{
fcb_state *fcb = fetch_cbs[nonce];
fetch_cbs.remove (fcb);
delete fcb;
}
void
dhash_impl::dispatch (user_args *sbp)
{
switch (sbp->procno) {
case DHASHPROC_FETCHREC:
{
assert (0);
// dhash_fetchrec_arg *arg = sbp->template getarg<dhash_fetchrec_arg> ();
//dofetchrec (sbp, arg);
}
break;
case DHASHPROC_FETCHITER:
{
s_dhash_fetch_arg *farg = sbp->Xtmpl getarg<s_dhash_fetch_arg> ();
blockID id (farg->key, farg->ctype);
dhash_fetchiter_res res (DHASH_INPROGRESS);
chord_node from;
sbp->fill_from (&from);
long nonce = farg->nonce;
sbp->reply (&res);
//farg is garbage below this line -----
ptr<location> requestor = host_node->locations->lookup_or_create (from);
ptr<dhblock_srv> srv = blocksrv[id.ctype];
cli->sendblock (requestor, id, srv,
wrap (this, &dhash_impl::fetchcomplete_done,
nonce, from),
nonce);
}
break;
case DHASHPROC_FETCHCOMPLETE:
{
s_dhash_insertarg *sarg = sbp->Xtmpl getarg<s_dhash_insertarg> ();
fcb_state *fcb = fetch_cbs[sarg->nonce];
if (fcb) {
if (sarg->type == DHASH_NOENT_NOTIFY) {
(fcb->cb) (str (""), -1, sarg->attr);
} else {
str data (sarg->data.base (), sarg->data.size ());
(fcb->cb) (data, sarg->offset, sarg->attr);
}
} else
warn << host_node->my_ID () << ": unexpected FCB for " << sarg->key
<< " nonce: " << sarg->nonce
<< " type: " << sarg->type
<< " offset: " << sarg->offset
<< " totsz: " << sarg->attr.size
<< "\n";
sbp->reply (NULL);
}
break;
case DHASHPROC_STORE:
{
s_dhash_insertarg *sarg = sbp->Xtmpl getarg<s_dhash_insertarg> ();
// What to do about retries??
// e.g. checking if we're responsible (or continuing a partial)
store (sarg,
wrap (this, &dhash_impl::storesvc_cb, sbp, sarg));
}
break;
default:
sbp->replyref (PROC_UNAVAIL);
break;
}
}
void store_after_store (cbstore cb, dhash_stat status);
void
dhash_impl::storesvc_cb (user_args *sbp,
s_dhash_insertarg *arg,
bool already_present,
dhash_stat err)
{
dhash_storeres res (DHASH_OK);
if ((err != DHASH_OK) && (err != DHASH_STORE_PARTIAL))
res.set_status (err);
else {
res.resok->already_present = already_present;
res.resok->source = host_node->my_ID ();
res.resok->done = (err == DHASH_OK);
}
sbp->reply (&res);
}
void
dhash_impl::store (s_dhash_insertarg *arg, cbstore cb)
{
ptr<dhblock_srv> srv = blocksrv[arg->ctype];
if (!srv) {
cb (false, DHASH_ERR);
return;
}
store_state *ss = pst[arg->key];
if (ss == NULL) {
ss = New store_state (arg->key, arg->attr.size);
pst.insert(ss);
}
if (!ss->addchunk(arg->offset, arg->offset+arg->data.size (),
arg->data.base ())) {
cb (false, DHASH_ERR);
return;
}
if (ss->iscomplete()) {
str d (ss->buf, ss->size);
// this will start the store, but it won't be on disk until the cb
// is called.
srv->store (arg->key, d, arg->attr.expiration, wrap (&store_after_store, cb));
bytes_stored += ss->size;
objects_stored++;
pst.remove (ss);
delete ss;
// Wait until store returns to decide what error code to return.
// XXX this needs a "callback" RPC.
} else {
cb (false, DHASH_STORE_PARTIAL);
}
// XXX should throw out very old store states.
}
void
store_after_store (cbstore cb, dhash_stat status)
{
// XXX eliminate already_present flag to simplify this.
cb (false, status);
return;
}
// --------- utility
void
dhash_impl::doRPC (const chord_node &n, const rpc_program &prog, int procno,
ptr<void> in,void *out, aclnt_cb cb,
cbtmo_t cb_tmo)
{
host_node->doRPC (n, prog, procno, in, out, cb, cb_tmo);
}
void
dhash_impl::doRPC (const chord_node_wire &n, const rpc_program &prog,
int procno, ptr<void> in,void *out, aclnt_cb cb,
cbtmo_t cb_tmo)
{
host_node->doRPC (make_chord_node (n), prog, procno, in, out, cb, cb_tmo);
}
void
dhash_impl::doRPC (ptr<location> ID, const rpc_program &prog, int procno,
ptr<void> in,void *out, aclnt_cb cb,
cbtmo_t cb_tmo)
{
host_node->doRPC (ID, prog, procno, in, out, cb, cb_tmo);
}
// ---------- debug ----
void
dhash_impl::print_stats ()
{
warnx << "ID: " << host_node->my_ID () << "\n";
warnx << "Stats:\n";
vec<dstat> ds = stats ();
for (size_t i = 0; i < ds.size (); i++)
warnx << " " << ds[i].value << " " << ds[i].desc << "\n";
}
static void
statscb (vec<dstat> *s, const dhash_ctype &c, ptr<dhblock_srv> srv)
{
srv->stats (*s);
}
vec<dstat>
dhash_impl::stats ()
{
vec<dstat> s;
s.push_back(dstat ("all.objects.stored", objects_stored));
s.push_back(dstat ("all.bytes.stored", bytes_stored));
s.push_back(dstat ("all.objects.served", objects_served));
s.push_back(dstat ("all.bytes.served", bytes_served));
blocksrv.traverse (wrap (&statscb, &s));
return s;
}
static void
startcb (bool randomize, const dhash_ctype &c, ptr<dhblock_srv> srv)
{
srv->start (randomize);
}
void
dhash_impl::start (bool randomize)
{
blocksrv.traverse (wrap (&startcb, randomize));
}
static void
stopcb (const dhash_ctype &c, ptr<dhblock_srv> srv)
{
srv->stop ();
}
void
dhash_impl::stop ()
{
blocksrv.traverse (wrap (&stopcb));
}
// ----------------------------------------------------------------------------
// store state
static void
join (store_chunk *c)
{
store_chunk *cnext;
while (c->next && c->end >= c->next->start) {
cnext = c->next;
if (c->end < cnext->end)
c->end = cnext->end;
c->next = cnext->next;
delete cnext;
}
}
bool
store_state::gap ()
{
if (!have)
return true;
store_chunk *c = have;
store_chunk *p = 0;
if (c->start != 0)
return true;
while (c) {
if (p && p->end != c->start)
return true;
p = c;
c = c->next;
}
if (p->end != size)
return true;
return false;
}
bool
store_state::iscomplete ()
{
return have && have->start == 0 && have->end == (unsigned)size && !gap ();
}
bool
store_state::addchunk (unsigned int start, unsigned int end, void *base)
{
store_chunk **l, *c;
if (start >= end || end > size)
return false;
l = &have;
for (l=&have; *l; l=&(*l)->next) {
c = *l;
// our start touches this block
if (c->start <= start && start <= c->end) {
// we have new data
if (end > c->end) {
memmove (buf+start, base, end-start);
c->end = end;
join(c);
}
return true;
}
// our start comes before this block; break to insert
if (start < c->start)
break;
}
*l = New store_chunk(start, end, *l);
memmove(buf+start, base, end-start);
join(*l);
return true;
}