summaryrefslogtreecommitdiffstats
path: root/xlators/features/changetimerecorder/src/ctr-helper.c
blob: 263eb58db6f6a4766add053a429a57b918411aa9 (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
/*
   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 "gfdb_sqlite3.h"
#include "ctr-helper.h"
#include "ctr-messages.h"

/*******************************************************************************
 *
 *                      Fill unwind into db record
 *
 ******************************************************************************/
int
fill_db_record_for_unwind(xlator_t              *this,
                          gf_ctr_local_t        *ctr_local,
                          gfdb_fop_type_t       fop_type,
                          gfdb_fop_path_t       fop_path)
{
        int ret                         = -1;
        gfdb_time_t *ctr_uwtime         = NULL;
        gf_ctr_private_t *_priv                 = NULL;

        GF_ASSERT (this);
        _priv = this->private;
        GF_ASSERT (_priv);

        GF_ASSERT(ctr_local);

        /*If not unwind path error*/
        if (!isunwindpath(fop_path)) {
                gf_msg (this->name, GF_LOG_ERROR, 0, CTR_MSG_WRONG_FOP_PATH,
                        "Wrong fop_path. Should be unwind");
                goto out;
        }

        ctr_uwtime = &CTR_DB_REC(ctr_local).gfdb_unwind_change_time;
        CTR_DB_REC(ctr_local).gfdb_fop_path = fop_path;
        CTR_DB_REC(ctr_local).gfdb_fop_type = fop_type;

        ret = gettimeofday (ctr_uwtime, NULL);
        if (ret == -1) {
                        gf_msg (this->name, GF_LOG_ERROR, errno,
                                CTR_MSG_FILL_UNWIND_TIME_REC_ERROR, "Error "
                                "filling unwind time record %s",
                                strerror(errno));
                        goto out;
                }

        /* Special case i.e if its a tier rebalance
         * + cold tier brick
         * + its a create/mknod FOP
         * we record unwind time as zero */
        if (ctr_local->client_pid == GF_CLIENT_PID_TIER_DEFRAG
                && (!_priv->ctr_hot_brick)
                && isdentrycreatefop(fop_type)) {
                memset(ctr_uwtime, 0, sizeof(*ctr_uwtime));
        }
        ret = 0;
out:
        return ret;
}


/*******************************************************************************
 *
 *                      Fill wind into db record
 *
 ******************************************************************************/
int
fill_db_record_for_wind (xlator_t               *this,
                        gf_ctr_local_t          *ctr_local,
                        gf_ctr_inode_context_t  *ctr_inode_cx)
{
        int ret                                 = -1;
        gfdb_time_t *ctr_wtime                  = NULL;
        gf_ctr_private_t *_priv                 = NULL;

        GF_ASSERT (this);
        _priv = this->private;
        GF_ASSERT (_priv);
        GF_ASSERT (ctr_local);
        IS_CTR_INODE_CX_SANE (ctr_inode_cx);

        /*if not wind path error!*/
        if (!iswindpath(ctr_inode_cx->fop_path)) {
                gf_msg (this->name, GF_LOG_ERROR, 0,
                        CTR_MSG_WRONG_FOP_PATH,
                        "Wrong fop_path. Should be wind");
                goto out;
        }

        ctr_wtime = &CTR_DB_REC(ctr_local).gfdb_wind_change_time;
        CTR_DB_REC(ctr_local).gfdb_fop_path = ctr_inode_cx->fop_path;
        CTR_DB_REC(ctr_local).gfdb_fop_type = ctr_inode_cx->fop_type;
        CTR_DB_REC(ctr_local).link_consistency = _priv->ctr_link_consistency;

        ret = gettimeofday (ctr_wtime, NULL);
        if (ret) {
                        gf_msg (this->name, GF_LOG_ERROR, errno,
                                CTR_MSG_FILL_UNWIND_TIME_REC_ERROR,
                                "Error filling wind time record %s",
                                strerror(errno));
                        goto out;
        }

        /* Special case i.e if its a tier rebalance
         * + cold tier brick
         * + its a create/mknod FOP
         * we record wind time as zero */
        if (ctr_local->client_pid == GF_CLIENT_PID_TIER_DEFRAG
                && (!_priv->ctr_hot_brick)
                && isdentrycreatefop(ctr_inode_cx->fop_type)) {
                memset(ctr_wtime, 0, sizeof(*ctr_wtime));
        }

        /* Copy gfid into db record */
        gf_uuid_copy (CTR_DB_REC(ctr_local).gfid, *(ctr_inode_cx->gfid));

        /* Copy older gfid if any */
        if (ctr_inode_cx->old_gfid &&
                (!gf_uuid_is_null (*(ctr_inode_cx->old_gfid)))) {
                gf_uuid_copy (CTR_DB_REC(ctr_local).old_gfid,
                                *(ctr_inode_cx->old_gfid));
        }

        /*Hard Links*/
        if (isdentryfop(ctr_inode_cx->fop_type)) {
                /*new link fop*/
                if (NEW_LINK_CX(ctr_inode_cx)) {
                        gf_uuid_copy (CTR_DB_REC(ctr_local).pargfid,
                                *((NEW_LINK_CX(ctr_inode_cx))->pargfid));
                        strcpy (CTR_DB_REC(ctr_local).file_name,
                                NEW_LINK_CX(ctr_inode_cx)->basename);
                }
                /*rename fop*/
                if (OLD_LINK_CX(ctr_inode_cx)) {
                        gf_uuid_copy (CTR_DB_REC(ctr_local).old_pargfid,
                                *((OLD_LINK_CX(ctr_inode_cx))->pargfid));
                        strcpy (CTR_DB_REC(ctr_local).old_file_name,
                                OLD_LINK_CX(ctr_inode_cx)->basename);
                }
        }

        ret = 0;
out:
        /*On error roll back and clean the record*/
        if (ret == -1) {
                CLEAR_CTR_DB_RECORD (ctr_local);
        }
        return ret;
}


/******************************************************************************
 *
 *                      CTR xlator init related functions
 *
 *
 * ****************************************************************************/
static int
extract_sql_params(xlator_t *this, dict_t *params_dict)
{
        int ret                         = -1;
        char *db_path                   = NULL;
        char *db_name                   = NULL;
        char *db_full_path              = NULL;

        GF_ASSERT (this);
        GF_ASSERT (params_dict);

        /*Extract the path of the db*/
        db_path = NULL;
        GET_DB_PARAM_FROM_DICT_DEFAULT(this->name, this->options, "db-path",
                                        db_path, "/var/run/gluster/");

        /*Extract the name of the db*/
        db_name = NULL;
        GET_DB_PARAM_FROM_DICT_DEFAULT(this->name, this->options, "db-name",
                                        db_name, "gf_ctr_db.db");

        /*Construct full path of the db*/
        ret = gf_asprintf(&db_full_path, "%s/%s", db_path, db_name);
        if (ret < 0) {
                gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, 0,
                        CTR_MSG_CONSTRUCT_DB_PATH_FAILED,
                        "Construction of full db path failed!");
                goto out;
        }

        /*Setting the SQL DB Path*/
        SET_DB_PARAM_TO_DICT(this->name, params_dict, GFDB_SQL_PARAM_DBPATH,
                                db_full_path, ret, out);

        /*Extact rest of the sql params*/
        ret = gfdb_set_sql_params(this->name, this->options, params_dict);
        if (ret) {
                gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, 0,
                        CTR_MSG_SET_VALUE_TO_SQL_PARAM_FAILED,
                        "Failed setting values to sql param dict!");
        }

        ret = 0;

out:
        if (ret)
                GF_FREE (db_full_path);
        return ret;
}



int extract_db_params(xlator_t *this, dict_t *params_dict,
                                                gfdb_db_type_t db_type) {

        int ret = -1;

        GF_ASSERT (this);
        GF_ASSERT (params_dict);

        switch (db_type) {
        case GFDB_SQLITE3:
                ret = extract_sql_params(this, params_dict);
                if (ret)
                        goto out;
                break;
        case GFDB_ROCKS_DB:
        case GFDB_HYPERDEX:
        case GFDB_HASH_FILE_STORE:
        case GFDB_INVALID_DB:
        case GFDB_DB_END:
                ret = -1;
                break;
        }
        ret = 0;
out:
        return ret;
}

int extract_ctr_options (xlator_t *this, gf_ctr_private_t *_priv) {
        int ret         = -1;
        char *_val_str  = NULL;

        GF_ASSERT (this);
        GF_ASSERT (_priv);

        /*Checking if the CTR Translator is enabled. By default its disabled*/
        _priv->enabled = _gf_false;
        GF_OPTION_INIT ("ctr-enabled", _priv->enabled, bool, out);
        if (!_priv->enabled) {
                gf_msg (GFDB_DATA_STORE, GF_LOG_INFO, 0,
                        CTR_MSG_XLATOR_DISABLED,
                        "CTR Xlator is disabled.");
                ret = 0;
                goto out;
        }

        /*Extract db type*/
        GF_OPTION_INIT ("db-type", _val_str, str, out);
        _priv->gfdb_db_type = gf_string2gfdbdbtype(_val_str);

        /*Extract flag for record on wind*/
        GF_OPTION_INIT ("record-entry", _priv->ctr_record_wind, bool, out);

        /*Extract flag for record on unwind*/
        GF_OPTION_INIT ("record-exit", _priv->ctr_record_unwind, bool, out);

        /*Extract flag for record on counters*/
        GF_OPTION_INIT ("record-counters", _priv->ctr_record_counter, bool,
                        out);

        /* Extract flag for record metadata heat */
        GF_OPTION_INIT ("ctr-record-metadata-heat",
                        _priv->ctr_record_metadata_heat, bool,
                        out);

        /*Extract flag for link consistency*/
        GF_OPTION_INIT ("ctr_link_consistency", _priv->ctr_link_consistency,
                        bool, out);

        /*Extract ctr_lookupheal_inode_timeout */
        GF_OPTION_INIT ("ctr_lookupheal_inode_timeout",
                        _priv->ctr_lookupheal_inode_timeout,
                        uint64, out);

        /*Extract ctr_lookupheal_link_timeout*/
        GF_OPTION_INIT ("ctr_lookupheal_link_timeout",
                        _priv->ctr_lookupheal_link_timeout,
                        uint64, out);

        /*Extract flag for hot tier brick*/
        GF_OPTION_INIT ("hot-brick", _priv->ctr_hot_brick, bool, out);

        /*Extract flag for sync mode*/
        GF_OPTION_INIT ("db-sync", _val_str, str, out);
        _priv->gfdb_sync_type = gf_string2gfdbdbsync(_val_str);

        ret = 0;

out:
        return ret;
}