summaryrefslogtreecommitdiffstats
path: root/xlators/features/changelog/lib/src/gf-changelog-helpers.h
blob: 17b8862a89bfc0ec2b3290857a177fb413ce1ccd (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
/*
   Copyright (c) 2013 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.
*/

#ifndef _GF_CHANGELOG_HELPERS_H
#define _GF_CHANGELOG_HELPERS_H

#include <unistd.h>
#include <dirent.h>
#include <limits.h>
#include <pthread.h>

#include <xlator.h>

#include "changelog.h"

#include "changelog-rpc-common.h"
#include "gf-changelog-journal.h"

#define GF_CHANGELOG_TRACKER  "tracker"

#define GF_CHANGELOG_CURRENT_DIR    ".current"
#define GF_CHANGELOG_PROCESSED_DIR  ".processed"
#define GF_CHANGELOG_PROCESSING_DIR ".processing"
#define GF_CHANGELOG_HISTORY_DIR    ".history"
#define TIMESTAMP_LENGTH 10

#ifndef MAXLINE
#define MAXLINE 4096
#endif

#define GF_CHANGELOG_FILL_BUFFER(ptr, ascii, off, len) do {     \
                memcpy (ascii + off, ptr, len);                 \
                off += len;                                     \
        } while (0)

typedef struct read_line {
        int rl_cnt;
        char *rl_bufptr;
        char rl_buf[MAXLINE];
} read_line_t;

struct gf_changelog;

/**
 * Event list for ordered event notification
 *
 * ->next_seq holds the next _expected_ sequence number.
 */
struct gf_event_list {
        pthread_mutex_t lock;               /* protects this structure */
        pthread_cond_t  cond;

        pthread_t invoker;

        unsigned long next_seq;             /* next sequence number expected:
                                               zero during bootstrap */

        struct gf_changelog *entry;         /* backpointer to it's brick
                                               encapsulator (entry) */
        struct list_head events;            /* list of events (ordered) */
};

/**
 * include a refcount if it's of use by additional layers
 */
struct gf_event {
        int count;

        unsigned long seq;

        struct list_head list;

        struct iovec iov[0];
};
#define GF_EVENT_CALLOC_SIZE(cnt, len)                                  \
        (sizeof (struct gf_event) + (cnt * sizeof (struct iovec)) + len)

/**
 * assign the base address of the IO vector to the correct memory
 * area and set it's addressable length.
 */
#define GF_EVENT_ASSIGN_IOVEC(vec, event, len, pos)                     \
        do {                                                            \
                vec->iov_base = ((char *)event) +                       \
                        sizeof (struct gf_event) +                      \
                        (event->count * sizeof (struct iovec)) + pos;   \
                vec->iov_len = len;                                     \
                pos += len;                                             \
        } while (0)

/**
 * An instance of this structure is allocated for each brick for which
 * notifications are streamed.
 */
typedef struct gf_changelog {
        xlator_t *this;

        struct list_head list;              /* list of instances */

        char brick[PATH_MAX];               /* brick path for this end-point */

        changelog_rpc_t grpc;               /* rpc{-clnt,svc} for this brick */
#define RPC_PROBER(ent)  ent->grpc.rpc
#define RPC_REBORP(ent)  ent->grpc.svc
#define RPC_SOCK(ent)    ent->grpc.sock

        unsigned int notify;                /* notification flag(s) */

        FINI       *fini;                   /* destructor callback */
        CALLBACK   *callback;               /* event callback dispatcher */
        CONNECT    *connected;              /* connect callback */
        DISCONNECT *disconnected;           /* disconnection callback */

        void *ptr;                          /* owner specific private data */
        xlator_t *invokerxl;                /* consumers _this_, if valid,
                                               assigned to THIS before cbk is
                                               invoked */

        gf_boolean_t ordered;

        struct gf_event_list event;
} gf_changelog_t;

static inline int
gf_changelog_filter_check (gf_changelog_t *entry, changelog_event_t *event)
{
        if (event->ev_type & entry->notify)
                return 1;
        return 0;
}

#define GF_NEED_ORDERED_EVENTS(ent)  (ent->ordered == _gf_true)

/** private structure */
typedef struct gf_private {
        gf_lock_t lock;                  /* protects ->connections */

        void *api;                       /* pointer for API access */

        pthread_t poller;                /* event poller thread */

        struct list_head connections;    /* list of connections */
} gf_private_t;

#define GF_CHANGELOG_GET_API_PTR(this) (((gf_private_t *) this->private)->api)

/**
 * upcall: invoke callback with _correct_ THIS
 */
#define GF_CHANGELOG_INVOKE_CBK(this, cbk, brick, args ...)             \
        do {                                                            \
                xlator_t *old_this = NULL;                              \
                xlator_t *invokerxl = NULL;                             \
                                                                        \
                invokerxl = entry->invokerxl;                           \
                old_this = this;                                        \
                                                                        \
                if (invokerxl) {                                        \
                        THIS = invokerxl;                               \
                }                                                       \
                                                                        \
                cbk (invokerxl, brick, args);                           \
                THIS = old_this;                                        \
                                                                        \
        } while (0)

#define SAVE_THIS(xl)                           \
        do {                                    \
                old_this = xl;                  \
                THIS = master;                  \
        } while (0)

#define RESTORE_THIS()                          \
        do {                                    \
                THIS = old_this;                \
        } while (0)

/** APIs and the rest */

void *
gf_changelog_process (void *data);

ssize_t
gf_changelog_read_path (int fd, char *buffer, size_t bufsize);

void
gf_rfc3986_encode (unsigned char *s, char *enc, char *estr);

size_t
gf_changelog_write (int fd, char *buffer, size_t len);

ssize_t
gf_readline (int fd, void *vptr, size_t maxlen);

int
gf_ftruncate (int fd, off_t length);

off_t
gf_lseek (int fd, off_t offset, int whence);

int
gf_changelog_consume (xlator_t *this,
                      gf_changelog_journal_t *jnl,
                      char *from_path, gf_boolean_t no_publish);
int
gf_changelog_publish (xlator_t *this,
                      gf_changelog_journal_t *jnl, char *from_path);
int
gf_thread_cleanup (xlator_t *this, pthread_t thread);
void *
gf_changelog_callback_invoker (void *arg);

#endif