summaryrefslogtreecommitdiffstats
path: root/wsrep-lib/src/config_service_v1.cpp
blob: ace61427b4f3ac8d0b889ca7a7146ee01dbf9c5c (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
/*
 * Copyright (C) 2022 Codership Oy <info@codership.com>
 *
 * This file is part of wsrep-lib.
 *
 * Wsrep-lib 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, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Wsrep-lib 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 wsrep-lib.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config_service_v1.hpp"
#include "service_helpers.hpp"
#include "v26/wsrep_config_service.h"
#include "wsrep/logger.hpp"
#include "wsrep/provider_options.hpp"

#include <cassert>

namespace wsrep_config_service_v1
{
    wsrep_config_service_v1_t service{ 0 };

    static int map_flags(int flags)
    {
        int option_flags = 0;
        if (flags & WSREP_PARAM_DEPRECATED)
            option_flags |= wsrep::provider_options::flag::deprecated;
        if (flags & WSREP_PARAM_READONLY)
            option_flags |= wsrep::provider_options::flag::readonly;
        if (flags & WSREP_PARAM_TYPE_BOOL)
            option_flags |= wsrep::provider_options::flag::type_bool;
        if (flags & WSREP_PARAM_TYPE_INTEGER)
            option_flags |= wsrep::provider_options::flag::type_integer;
        if (flags & WSREP_PARAM_TYPE_DOUBLE)
            option_flags |= wsrep::provider_options::flag::type_double;
        return option_flags;
    }

    static enum wsrep::provider::status
    make_option(wsrep::provider_options* opt, const char* name, const char* val,
                int flags)
    {
        std::unique_ptr<wsrep::provider_options::option_value> value(
            new wsrep::provider_options::option_value_string(val));
        std::unique_ptr<wsrep::provider_options::option_value> default_value(
            new wsrep::provider_options::option_value_string(val));
        return opt->set_default(name, std::move(value),
                                std::move(default_value), flags);
    }

    static enum wsrep::provider::status
    make_option(wsrep::provider_options* opt, const char* name, int64_t val,
                int flags)
    {
        std::unique_ptr<wsrep::provider_options::option_value> value(
            new wsrep::provider_options::option_value_int(val));
        std::unique_ptr<wsrep::provider_options::option_value> default_value(
            new wsrep::provider_options::option_value_int(val));
        return opt->set_default(name, std::move(value),
                                std::move(default_value), flags);
    }

    static enum wsrep::provider::status
    make_option(wsrep::provider_options* opt, const char* name, bool val,
                int flags)
    {
        std::unique_ptr<wsrep::provider_options::option_value> value(
            new wsrep::provider_options::option_value_bool(val));
        std::unique_ptr<wsrep::provider_options::option_value> default_value(
            new wsrep::provider_options::option_value_bool(val));
        return opt->set_default(name, std::move(value),
                                std::move(default_value), flags);
    }

    static enum wsrep::provider::status
    make_option(wsrep::provider_options* opt, const char* name, double val,
                int flags)
    {
        std::unique_ptr<wsrep::provider_options::option_value> value(
            new wsrep::provider_options::option_value_double(val));
        std::unique_ptr<wsrep::provider_options::option_value> default_value(
            new wsrep::provider_options::option_value_double(val));
        return opt->set_default(name, std::move(value),
                                std::move(default_value), flags);
    }

    wsrep_status_t service_callback(const wsrep_parameter* p, void* context)
    {
        const int flags = map_flags(p->flags);
        enum wsrep::provider::status ret(wsrep::provider::error_unknown);
        wsrep::provider_options* options = (wsrep::provider_options*)context;
        switch (p->flags & WSREP_PARAM_TYPE_MASK)
        {
        case WSREP_PARAM_TYPE_BOOL:
            ret = make_option(options, p->name, p->value.as_bool, flags);
            break;
        case WSREP_PARAM_TYPE_INTEGER:
            ret = make_option(options, p->name, p->value.as_integer, flags);
            break;
        case WSREP_PARAM_TYPE_DOUBLE:
            ret = make_option(options, p->name, p->value.as_double, flags);
            break;
        default:
            assert((p->flags & WSREP_PARAM_TYPE_MASK) == 0);
            ret = make_option(options, p->name, p->value.as_string, flags);
            break;
        }

        if (ret == wsrep::provider::success)
            return WSREP_OK;
        else
            return WSREP_FATAL;
    }
} // namespace wsrep_config_service_v1

static int config_service_v1_probe(void* dlh)
{
    typedef int (*init_fn)(wsrep_config_service_v1_t*);
    typedef void (*deinit_fn)();
    return wsrep_impl::service_probe<init_fn>(
               dlh, WSREP_CONFIG_SERVICE_INIT_FUNC_V1, "config service v1")
           || wsrep_impl::service_probe<deinit_fn>(
               dlh, WSREP_CONFIG_SERVICE_DEINIT_FUNC_V1, "config service v1");
}

static int config_service_v1_init(void* dlh)
{
    typedef int (*init_fn)(wsrep_config_service_v1_t*);
    return wsrep_impl::service_init<init_fn>(
        dlh, WSREP_CONFIG_SERVICE_INIT_FUNC_V1,
        &wsrep_config_service_v1::service, "config service v1");
}

static void config_service_v1_deinit(void* dlh)
{
    typedef int (*deinit_fn)();
    wsrep_impl::service_deinit<deinit_fn>(
        dlh, WSREP_CONFIG_SERVICE_DEINIT_FUNC_V1, "config service v1");
}

int wsrep::config_service_v1_fetch(wsrep::provider& provider,
                                   wsrep::provider_options* options)
{
    struct wsrep_st* wsrep = (struct wsrep_st*)provider.native();
    if (config_service_v1_probe(wsrep->dlh))
    {
        wsrep::log_warning() << "Provider does not support config service v1";
        return 1;
    }
    if (config_service_v1_init(wsrep->dlh))
    {
        wsrep::log_warning() << "Failed to initialize config service v1";
        return 1;
    }

    wsrep_status_t ret = wsrep_config_service_v1::service.get_parameters(
        wsrep, &wsrep_config_service_v1::service_callback, options);

    config_service_v1_deinit(wsrep->dlh);

    if (ret != WSREP_OK)
    {
        return 1;
    }

    return 0;
}