summaryrefslogtreecommitdiffstats
path: root/wsrep-lib/wsrep-API/v26/examples/node/sst.c
blob: e93534efa80f25a025b0b19e17a54baab1dd27c0 (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
/* Copyright (c) 2019, Codership Oy. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include "sst.h"

#include "ctx.h"
#include "log.h"
#include "socket.h"

#include <arpa/inet.h> // htonl()
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>  // snprintf()
#include <stdlib.h> // abort()
#include <string.h> // strdup()
#include <unistd.h> // usleep()

/**
 * Helper: creates detached thread */
static int
sst_create_thread(void* (*thread_routine) (void*),
                  void* const thread_arg)
{
    pthread_t thr;
    pthread_attr_t attr;
    int ret = pthread_attr_init(&attr);
    ret = ret ? ret : pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
    ret = ret ? ret : pthread_create(&thr, &attr, thread_routine, thread_arg);
    return ret;
}

/**
 * Helper: creates detached thread and waits for it to call
 *         sst_sync_with_parent() */
static void
sst_create_and_sync(const char*      const role,
                    pthread_mutex_t* const mtx,
                    pthread_cond_t*  const cond,
                    void* (*thread_routine) (void*),
                    void*            const thread_arg)
{
    int ret = pthread_mutex_lock(mtx);
    if (ret)
    {
        NODE_FATAL("Failed to lock %s mutex: %d (%s)", role, ret, strerror(ret));
        abort();
    }

    ret = sst_create_thread(thread_routine, thread_arg);
    if (ret)
    {
        NODE_FATAL("Failed to create detached %s thread: %d (%s)",
                   role, ret, strerror(ret));
        abort();
    }

    ret = pthread_cond_wait(cond, mtx);
    if (ret)
    {
        NODE_FATAL("Failed to synchronize with %s thread: %d (%s)",
                   role, ret, strerror(ret));
        abort();
    }

    pthread_mutex_unlock(mtx);
}

/**
 * Helper: syncs with parent thread and allows it to continue and return
 *         asynchronously */
static void
sst_sync_with_parent(const char*      role,
                     pthread_mutex_t* mtx,
                     pthread_cond_t*  cond)
{
    int ret = pthread_mutex_lock(mtx);
    if (ret)
    {
        NODE_FATAL("Failed to lock %s mutex: %d (%s)", role, ret, strerror(ret));
        abort();
    }

    NODE_INFO("Initialized %s thread", role);

    pthread_cond_signal(cond);
    pthread_mutex_unlock(mtx);
}

static pthread_mutex_t sst_joiner_mtx  = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t  sst_joiner_cond = PTHREAD_COND_INITIALIZER;

struct sst_joiner_ctx
{
    struct node_ctx* node;
    node_socket_t*   socket;
};

/**
 * waits for SST completion and signals the provider to continue */
static void*
sst_joiner_thread(void* ctx)
{
    assert(ctx);

    struct node_ctx* const node   = ((struct sst_joiner_ctx*)ctx)->node;
    node_socket_t* const   listen = ((struct sst_joiner_ctx*)ctx)->socket;
    ctx = NULL; /* may be unusable after next statement */

    /* this allows parent callback to return */
    sst_sync_with_parent("JOINER", &sst_joiner_mtx, &sst_joiner_cond);

    wsrep_gtid_t state_gtid = WSREP_GTID_UNDEFINED;
    int err = -1;

    /* REPLICATION: wait for donor to connect and send the state snapshot */
    node_socket_t* const connected = node_socket_accept(listen);
    if (!connected) goto end;

    uint32_t state_len;
    err = node_socket_recv_bytes(connected, &state_len, sizeof(state_len));
    if (err) goto end;

    state_len = ntohl(state_len);
    if (state_len > 0)
    {
        /* REPLICATION: get the state of state_len size */
        void* state = malloc(state_len);
        if (state)
        {
            err = node_socket_recv_bytes(connected, state, state_len);
            if (err)
            {
                free(state);
                goto end;
            }

            /* REPLICATION: install the newly received state. */
            err = node_store_init_state(node->store, state, state_len);
            free(state);
            if (err) goto end;
        }
        else
        {
            NODE_ERROR("Failed to allocate %zu bytes for state snapshot.",
                       state_len);
            err = -ENOMEM;
            goto end;
        }
    }
    else
    {
        /* REPLICATION: it was a bypass, the node will receive missing data via
         *              IST. It starts with the state it currently has. */
    }

    /* REPLICATION: find gtid of the received state to report to provider */
    node_store_gtid(node->store, &state_gtid);

end:
    assert(err <= 0);
    node_socket_close(connected);
    node_socket_close(listen);

    /* REPLICATION: tell provider that SST is received */
    wsrep_status_t sst_ret;
    wsrep_t* const wsrep = node_wsrep_provider(node->wsrep);
    sst_ret = wsrep->sst_received(wsrep, &state_gtid, NULL, err);

    if (WSREP_OK != sst_ret)
    {
        NODE_FATAL("Failed to report completion of SST: %d", sst_ret);
        abort();
    }

    return NULL;
}

enum wsrep_cb_status
node_sst_request_cb (void*   const app_ctx,
                     void**  const sst_req,
                     size_t* const sst_req_len)
{
    static int const SST_PORT_OFFSET = 2;

    assert(app_ctx);
    struct node_ctx* const node = app_ctx;
    const struct node_options* const opts = node->opts;

    char* sst_str = NULL;

    /* REPLICATION: 1. prepare the node to receive SST */
    uint16_t const sst_port = (uint16_t)(opts->base_port + SST_PORT_OFFSET);
    size_t const sst_len = strlen(opts->base_host)
        + 1 /* ':' */ + 5 /* max port len */ + 1 /* \0 */;
    sst_str = malloc(sst_len);
    if (!sst_str)
    {
        NODE_ERROR("Failed to allocate %zu bytes for SST request", sst_len);
        goto end;
    }

    /* write in request the address at which we listen */
    int ret = snprintf(sst_str, sst_len, "%s:%hu", opts->base_host, sst_port);
    if (ret < 0 || (size_t)ret >= sst_len)
    {
        free(sst_str);
        sst_str = NULL;
        NODE_ERROR("Failed to write a SST request");
        goto end;
    }

    node_socket_t* const socket = node_socket_listen(NULL, sst_port);
    if (!socket)
    {
        free(sst_str);
        sst_str = NULL;
        NODE_ERROR("Failed to listen at %s", sst_str);
        goto end;
    }

    /* REPLICATION 2. start the "joiner" thread that will wait for SST and
     *                report its success to provider, and syncronize with it. */
    struct sst_joiner_ctx ctx =
        {
            .node   = node,
            .socket = socket
        };
    sst_create_and_sync("JOINER", &sst_joiner_mtx, &sst_joiner_cond,
                        sst_joiner_thread, &ctx);

    NODE_INFO("Waiting for SST at %s", sst_str);

end:
    if (sst_str)
    {
        *sst_req     = sst_str;
        *sst_req_len = strlen(sst_str) + 1;
    }
    else
    {
        *sst_req     = NULL;
        *sst_req_len = 0;
        return WSREP_CB_FAILURE;
    }

    /* REPLICATION 3. return SST request to provider */
    return WSREP_CB_SUCCESS;
}

static pthread_mutex_t sst_donor_mtx  = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t  sst_donor_cond = PTHREAD_COND_INITIALIZER;

struct sst_donor_ctx
{
    wsrep_gtid_t     state;
    struct node_ctx* node;
    node_socket_t*   socket;
    wsrep_bool_t     bypass;
};

/**
 * donates SST and signals provider that it is done. */
static void*
sst_donor_thread(void* const args)
{
    struct sst_donor_ctx const ctx = *(struct sst_donor_ctx*)args;

    int err = 0;
    const void* state;
    size_t state_len;

    if (ctx.bypass)
    {
        /* REPLICATION: if bypass is true, there is no need to send snapshot,
         *              just signal the joiner that snapshot is not needed and
         *              it can proceed to apply IST. We'll do it by sending 0
         *              for the size of snapshot */
        state = NULL;
        state_len = 0;
    }
    else
    {
        /* REPLICATION: if bypass is false, we need to send a full state snapshot
         *              Get hold of the state, which is currently just GTID
         *              NOTICE that while parent is waiting, the store is in a
         *              quiescent state, provider blocking any modifications. */
        err = node_store_acquire_state(ctx.node->store, &state, &state_len);
        if (state_len > UINT32_MAX) err = -ERANGE;
    }

    /* REPLICATION: after getting hold of the state we can allow parent callback
     *              to return and the node to resume its normal operation */
    sst_sync_with_parent("DONOR", &sst_donor_mtx, &sst_donor_cond);

    if (err >= 0)
    {
        uint32_t tmp = htonl((uint32_t)state_len);
        err = node_socket_send_bytes(ctx.socket, &tmp, sizeof(tmp));
    }

    if (state_len != 0)
    {
        if (err >= 0)
        {
            assert(state);
            err = node_socket_send_bytes(ctx.socket, state, state_len);
        }

        node_store_release_state(ctx.node->store);
    }

    node_socket_close(ctx.socket);

    /* REPLICATION: signal provider the success of the operation */
    wsrep_t* const wsrep = node_wsrep_provider(ctx.node->wsrep);
    wsrep->sst_sent(wsrep, &ctx.state, err);

    return NULL;
}

enum wsrep_cb_status
node_sst_donate_cb (void*               const app_ctx,
                    void*               const recv_ctx,
                    const wsrep_buf_t*  const str_msg,
                    const wsrep_gtid_t* const state_id,
                    const wsrep_buf_t*  const state,
                    wsrep_bool_t        const bypass)
{
    (void)recv_ctx;
    (void)state;

    struct sst_donor_ctx ctx =
    {
        .node   = app_ctx,
        .state  = *state_id,
        .bypass = bypass
    };

    /* we are expecting a human-readable 0-terminated string  */
    void* p = memchr(str_msg->ptr, '\0', str_msg->len);
    if (!p)
    {
        NODE_ERROR("Received a badly formed State Transfer Request.");
        /* REPLICATION: in case of a failure we return the status to provider, so
         *              that the joining node can be notified of it by cluster */
        return WSREP_CB_FAILURE;
    }

    const char* addr = str_msg->ptr;
    ctx.socket = node_socket_connect(addr);

    if (!ctx.socket) return WSREP_CB_FAILURE;

    sst_create_and_sync("DONOR", &sst_donor_mtx, &sst_donor_cond,
                        sst_donor_thread, &ctx);

    return WSREP_CB_SUCCESS;
}