summaryrefslogtreecommitdiffstats
path: root/xlators/features/changelog/lib/src/gf-changelog-reborp.c
blob: d1b3059a60c0feb8e5b79aa737fa0c096945720f (plain)
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
/*
   Copyright (c) 2015 Red Hat, Inc. <http://www.redhat.com>
   This file is part of GlusterFS.

   This file is licensed to you under your choice of the GNU Lesser
   General Public License, version 3 or any later version (LGPLv3 or
   later), or the GNU General Public License, version 2 (GPLv2), in all
   cases as published by the Free Software Foundation.
*/

#include "changelog-misc.h"
#include "changelog-mem-types.h"

#include "gf-changelog-helpers.h"
#include "changelog-rpc-common.h"
#include "changelog-lib-messages.h"

#include "syscall.h"

/**
 * Reverse socket: actual data transfer handler. Connection
 * initiator is PROBER, data transfer is REBORP.
 */

struct rpcsvc_program *gf_changelog_reborp_programs[];

void *
gf_changelog_connection_janitor (void *arg)
{
        int32_t ret = 0;
        xlator_t *this = NULL;
        gf_private_t *priv = NULL;
        gf_changelog_t *entry = NULL;
        struct gf_event *event = NULL;
        struct gf_event_list *ev = NULL;
        unsigned long drained = 0;

        this = arg;
        THIS = this;

        priv = this->private;

        while (1) {
                pthread_mutex_lock (&priv->lock);
                {
                        while (list_empty (&priv->cleanups))
                                pthread_cond_wait (&priv->cond, &priv->lock);

                        entry = list_first_entry (&priv->cleanups,
                                                  gf_changelog_t, list);
                        list_del_init (&entry->list);
                }
                pthread_mutex_unlock (&priv->lock);

                drained = 0;
                ev = &entry->event;

                gf_smsg (this->name, GF_LOG_INFO, 0,
                         CHANGELOG_LIB_MSG_CLEANING_BRICK_ENTRY_INFO,
                         "Cleaning brick entry for brick",
                         "brick=%s", entry->brick,
                         NULL);

                /* 0x0: disbale rpc-clnt */
                rpc_clnt_disable (RPC_PROBER (entry));

                /* 0x1: cleanup callback invoker thread */
                ret = gf_cleanup_event (this, ev);
                if (ret)
                        continue;

                /* 0x2: drain pending events */
                while (!list_empty (&ev->events)) {
                        event = list_first_entry (&ev->events,
                                                  struct gf_event, list);
                        gf_smsg (this->name, GF_LOG_INFO, 0,
                                 CHANGELOG_LIB_MSG_DRAINING_EVENT_INFO,
                                 "Draining event",
                                 "seq=%lu", event->seq,
                                 "payload=%d", event->count,
                                 NULL);

                        GF_FREE (event);
                        drained++;
                }

                gf_smsg (this->name, GF_LOG_INFO, 0,
                         CHANGELOG_LIB_MSG_DRAINING_EVENT_INFO,
                         "Drained events",
                         "num=%lu", drained,
                         NULL);

                /* 0x3: freeup brick entry */
                gf_smsg (this->name, GF_LOG_INFO, 0,
                         CHANGELOG_LIB_MSG_FREEING_ENTRY_INFO,
                         "freeing entry",
                         "entry=%p", entry,
                         NULL);
                LOCK_DESTROY (&entry->statelock);
                GF_FREE (entry);
        }

        return NULL;
}

int
gf_changelog_reborp_rpcsvc_notify (rpcsvc_t *rpc, void *mydata,
                                   rpcsvc_event_t event, void *data)
{
        int             ret      = 0;
        xlator_t       *this     = NULL;
        gf_changelog_t *entry    = NULL;

        if (!(event == RPCSVC_EVENT_ACCEPT ||
              event == RPCSVC_EVENT_DISCONNECT))
                return 0;

        entry = mydata;
        this = entry->this;

        switch (event) {
        case RPCSVC_EVENT_ACCEPT:
                ret = sys_unlink (RPC_SOCK(entry));
                if (ret != 0)
                        gf_smsg (this->name, GF_LOG_WARNING, errno,
                                 CHANGELOG_LIB_MSG_UNLINK_FAILED,
                                 "failed to unlink "
                                 "reverse socket",
                                 "path=%s", RPC_SOCK (entry),
                                 NULL);
                if (entry->connected)
                        GF_CHANGELOG_INVOKE_CBK (this, entry->connected,
                                                 entry->brick, entry->ptr);
                break;
        case RPCSVC_EVENT_DISCONNECT:
                if (entry->disconnected)
                        GF_CHANGELOG_INVOKE_CBK (this, entry->disconnected,
                                                 entry->brick, entry->ptr);
                /* passthrough */
        default:
                break;
        }

        return 0;
}

rpcsvc_t *
gf_changelog_reborp_init_rpc_listner (xlator_t *this,
                                      char *path, char *sock, void *cbkdata)
{
        CHANGELOG_MAKE_TMP_SOCKET_PATH (path, sock, UNIX_PATH_MAX);
        return changelog_rpc_server_init (this, sock, cbkdata,
                                          gf_changelog_reborp_rpcsvc_notify,
                                          gf_changelog_reborp_programs);
}

/**
 * This is dirty and painful as of now untill there is event filtering in the
 * server. The entire event buffer is scanned and interested events are picked,
 * whereas we _should_ be notified with the events we were interested in
 * (selected at the time of probe). As of now this is complete BS and needs
 * fixture ASAP. I just made it work, it needs to be better.
 *
 * @FIXME: cleanup this bugger once server filters events.
 */
void
gf_changelog_invoke_callback (gf_changelog_t *entry,
                              struct iovec **vec, int payloadcnt)
{
        int i = 0;
        int evsize = 0;
        xlator_t *this = NULL;
        changelog_event_t *event = NULL;

        this = entry->this;

        for (; i < payloadcnt; i++) {
                event = (changelog_event_t *)vec[i]->iov_base;
                evsize = vec[i]->iov_len / CHANGELOG_EV_SIZE;

                for (; evsize > 0; evsize--, event++) {
                        if (gf_changelog_filter_check (entry, event)) {
                                GF_CHANGELOG_INVOKE_CBK (this,
                                                         entry->callback,
                                                         entry->brick,
                                                         entry->ptr, event);
                        }
                }
        }
}

/**
 * Ordered event handler is self-adaptive.. if the event sequence number
 * is what's expected (->next_seq) there is no ordering list that's
 * maintained. On out-of-order event notifications, event buffers are
 * dynamically allocated and ordered.
 */

int
__is_expected_sequence (struct gf_event_list *ev, struct gf_event *event)
{
        return (ev->next_seq == event->seq);
}

int
__can_process_event (struct gf_event_list *ev, struct gf_event **event)
{
        *event = list_first_entry (&ev->events, struct gf_event, list);

        if (__is_expected_sequence (ev, *event)) {
                list_del (&(*event)->list);
                ev->next_seq++;
                return 1;
        }

        return 0;
}

void
pick_event_ordered (struct gf_event_list *ev, struct gf_event **event)
{
        pthread_mutex_lock (&ev->lock);
        {
                while (list_empty (&ev->events)
                       || !__can_process_event (ev, event))
                        pthread_cond_wait (&ev->cond, &ev->lock);
        }
        pthread_mutex_unlock (&ev->lock);
}

void
pick_event_unordered (struct gf_event_list *ev, struct gf_event **event)
{
        pthread_mutex_lock (&ev->lock);
        {
                while (list_empty (&ev->events))
                        pthread_cond_wait (&ev->cond, &ev->lock);
                *event = list_first_entry (&ev->events, struct gf_event, list);
                list_del (&(*event)->list);
        }
        pthread_mutex_unlock (&ev->lock);
}

void *
gf_changelog_callback_invoker (void *arg)
{
        xlator_t             *this   = NULL;
        gf_changelog_t       *entry  = NULL;
        struct iovec         *vec    = NULL;
        struct gf_event      *event  = NULL;
        struct gf_event_list *ev     = NULL;

        ev    = arg;
        entry = ev->entry;
        THIS = this = entry->this;

        while (1) {
                entry->pickevent (ev, &event);

                vec = (struct iovec *) &event->iov;
                gf_changelog_invoke_callback (entry, &vec, event->count);

                GF_FREE (event);
        }

        return NULL;
}

static int
orderfn (struct list_head *pos1, struct list_head *pos2)
{
        struct gf_event *event1 = NULL;
        struct gf_event *event2 = NULL;

        event1 = list_entry (pos1, struct gf_event, list);
        event2 = list_entry (pos2, struct gf_event, list);

        if  (event1->seq > event2->seq)
                return 1;
        return -1;
}

void
queue_ordered_event (struct gf_event_list *ev, struct gf_event *event)
{
        /* add event to the ordered event list and wake up listner(s) */
        pthread_mutex_lock (&ev->lock);
        {
                list_add_order (&event->list, &ev->events, orderfn);
                if (!ev->next_seq)
                        ev->next_seq = event->seq;
                if (ev->next_seq == event->seq)
                        pthread_cond_signal (&ev->cond);
        }
        pthread_mutex_unlock (&ev->lock);
}

void
queue_unordered_event (struct gf_event_list *ev, struct gf_event *event)
{
        /* add event to the tail of the queue and wake up listener(s) */
        pthread_mutex_lock (&ev->lock);
        {
                list_add_tail (&event->list, &ev->events);
                pthread_cond_signal (&ev->cond);
        }
        pthread_mutex_unlock (&ev->lock);
}

int
gf_changelog_event_handler (rpcsvc_request_t *req,
                            xlator_t *this, gf_changelog_t *entry)
{
        int                   i          = 0;
        size_t                payloadlen = 0;
        ssize_t               len        = 0;
        int                   payloadcnt = 0;
        changelog_event_req   rpc_req    = {0,};
        changelog_event_rsp   rpc_rsp    = {0,};
        struct iovec         *vec        = NULL;
        struct gf_event      *event      = NULL;
        struct gf_event_list *ev         = NULL;

        ev = &entry->event;

        len = xdr_to_generic (req->msg[0],
                              &rpc_req, (xdrproc_t)xdr_changelog_event_req);
        if (len < 0) {
                gf_msg (this->name, GF_LOG_ERROR, 0,
                        CHANGELOG_LIB_MSG_XDR_DECODING_FAILED,
                        "xdr decoding failed");
                req->rpc_err = GARBAGE_ARGS;
                goto handle_xdr_error;
        }

        if (len < req->msg[0].iov_len) {
                payloadcnt = 1;
                payloadlen = (req->msg[0].iov_len - len);
        }
        for (i = 1; i < req->count; i++) {
                payloadcnt++;
                payloadlen += req->msg[i].iov_len;
        }

        event = GF_CALLOC (1, GF_EVENT_CALLOC_SIZE (payloadcnt, payloadlen),
                           gf_changelog_mt_libgfchangelog_event_t);
        if (!event)
                goto handle_xdr_error;
        INIT_LIST_HEAD (&event->list);

        payloadlen   = 0;
        event->seq   = rpc_req.seq;
        event->count = payloadcnt;

        /* deep copy IO vectors */
        vec = &event->iov[0];
        GF_EVENT_ASSIGN_IOVEC (vec, event,
                               (req->msg[0].iov_len - len), payloadlen);
        (void) memcpy (vec->iov_base,
                       req->msg[0].iov_base + len, vec->iov_len);

        for (i = 1; i < req->count; i++) {
                vec = &event->iov[i];
                GF_EVENT_ASSIGN_IOVEC (vec, event,
                                       req->msg[i].iov_len, payloadlen);
                (void) memcpy (event->iov[i].iov_base,
                               req->msg[i].iov_base, req->msg[i].iov_len);
        }

        gf_msg_debug (this->name, 0,
                      "seq: %lu [%s] (time: %lu.%lu), (vec: %d, len: %zd)",
                      rpc_req.seq, entry->brick, rpc_req.tv_sec,
                      rpc_req.tv_usec, payloadcnt, payloadlen);

        /* dispatch event */
        entry->queueevent (ev, event);

        /* ack sequence number */
        rpc_rsp.op_ret = 0;
        rpc_rsp.seq    = rpc_req.seq;

        goto submit_rpc;

 handle_xdr_error:
        rpc_rsp.op_ret = -1;
        rpc_rsp.seq    = 0;     /* invalid */
 submit_rpc:
        return changelog_rpc_sumbit_reply (req, &rpc_rsp, NULL, 0, NULL,
                                           (xdrproc_t)xdr_changelog_event_rsp);
}

int
gf_changelog_reborp_handle_event (rpcsvc_request_t *req)
{
        xlator_t       *this  = NULL;
        rpcsvc_t       *svc   = NULL;
        gf_changelog_t *entry = NULL;

        svc = rpcsvc_request_service (req);
        entry = svc->mydata;

        this = THIS = entry->this;

        return gf_changelog_event_handler (req, this, entry);
}

rpcsvc_actor_t gf_changelog_reborp_actors[CHANGELOG_REV_PROC_MAX] = {
        [CHANGELOG_REV_PROC_EVENT] = {
                "CHANGELOG EVENT HANDLER", CHANGELOG_REV_PROC_EVENT,
                gf_changelog_reborp_handle_event, NULL, 0, DRC_NA
        },
};

/**
 * Do not use synctask as the RPC layer dereferences ->mydata as THIS.
 * In gf_changelog_setup_rpc(), @cbkdata is of type @gf_changelog_t,
 * and that's required to invoke the callback with the appropriate
 * brick path and it's private data.
 */
struct rpcsvc_program gf_changelog_reborp_prog = {
        .progname  = "LIBGFCHANGELOG REBORP",
        .prognum   = CHANGELOG_REV_RPC_PROCNUM,
        .progver   = CHANGELOG_REV_RPC_PROCVER,
        .numactors = CHANGELOG_REV_PROC_MAX,
        .actors    = gf_changelog_reborp_actors,
        .synctask  = _gf_false,
};

struct rpcsvc_program *gf_changelog_reborp_programs[] = {
        &gf_changelog_reborp_prog,
        NULL,
};