summaryrefslogtreecommitdiffstats
path: root/xlators/cluster/nsr-server/src/leader.c
blob: 02a2609c86ee05964aecf33d60719deb56b99131 (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
/*
   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.
*/

#include <regex.h>
//#include <stdlib.h>
#include <string.h>

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

#include "call-stub.h"
#include "defaults.h"
#include "xlator.h"
#include "api/src/glfs.h"
#include "api/src/glfs-internal.h"

#ifndef NSR_SIM_ETCD
#include "etcd-api.h"
#endif
#include "nsr-internal.h"
#include "../../nsr-recon/src/recon_driver.h"
#include "../../nsr-recon/src/recon_xlator.h"

#define NSR_TTL         5

static void
nsr_set_leader (xlator_t *this, etcd_session etcd)
{
        long            term = 0;
        etcd_result     res;
        nsr_private_t   *priv   = this->private;
        char             n_t[sizeof(long)+1];
        char            *text   = NULL;

        gf_log (this->name, GF_LOG_INFO, "Just became leader");

        text = etcd_get(etcd, priv->term_key);
        if(text == NULL) {
                term = 0;
        } else {
                term = strtol(text, NULL, 10);
        }
        sprintf(n_t,"%ld",term+1);
        res = etcd_set(etcd, priv->term_key,n_t,text,0);
        if(res != ETCD_OK) {
		gf_log (this->name, GF_LOG_ERROR, "failed to set term");
		return;
	}
        priv->leader = _gf_true;

        priv->current_term = term + 1;

        if (priv->nsr_recon_start == _gf_false) {
                atomic_fetch_and(&(priv->fence_io), 0);
                return;
        }

        // Move this inside recon notify???
        atomic_fetch_or(&(priv->fence_io), 1);

        nsr_recon_notify_event_set_leader(priv);

        return;
}

void *
nsr_leader_thread (void *arg)
{
        xlator_t        *this           = (xlator_t *) arg;
        nsr_private_t   *priv           = this->private;
        etcd_result     res;
        char            *index_in       = NULL;
        char            *index_out      = NULL;

        gf_log (this->name, GF_LOG_INFO,
                "calling etcd_open_str on servers %s", priv->etcd_servers);

        priv->etcd = etcd_open_str(priv->etcd_servers);
        if (!(priv->etcd)) {
                gf_log (this->name, GF_LOG_ERROR,
                        "failed to open etcd session\n");
                return NULL;
        }

        priv->leader_inited = 1;

        for (;;) {
                /* Not leader yet.  Try to become leader. */
                for (;;) {
                        res = etcd_lock (priv->etcd, priv->leader_key, NSR_TTL,
                                         index_in, &index_out);
                        if (res == ETCD_OK) {
                                break;
                        }
                        gf_log (this->name, GF_LOG_WARNING,
                                "etcd_lock failed (%d)", res);
                        sleep(1);
                }
                /* We're there.  Notify other parts of the code. */
                nsr_set_leader(this,priv->etcd);
                /* Try to retain leadership. */
                index_in = index_out;
                index_out = NULL;
                for (;;) {
                        res = etcd_lock (priv->etcd, priv->leader_key, NSR_TTL,
                                         index_in, &index_out);
                        if (index_out && (index_in != index_out)) {
                                if (index_in) {
                                        free(index_in);
                                }
                                index_in = index_out;
                                index_out = NULL;
                        }
                        if (res != ETCD_OK) {
                                gf_log (this->name, GF_LOG_WARNING,
                                        "lost leadership (%d)", res);
                                if (index_out) {
                                        free(index_out);
                                }
                                break;
                        }
                        sleep(1);
                }
        }

        etcd_close_str(priv->etcd);
        return NULL;
}