summaryrefslogtreecommitdiff
path: root/storage/tokudb/tokudb_status.h
blob: 07772bdc92a4a5affdcbf194d5eee76389619612 (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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
#ident "$Id$"
/*======
This file is part of TokuDB


Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.

    TokuDBis is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2,
    as published by the Free Software Foundation.

    TokuDB 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 TokuDB.  If not, see <http://www.gnu.org/licenses/>.

======= */

#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."

#ifndef _TOKUDB_STATUS_H
#define _TOKUDB_STATUS_H

// These are keys that will be used for retrieving metadata in status.tokudb
// To get the version, one looks up the value associated with key hatoku_version
// in status.tokudb
typedef ulonglong HA_METADATA_KEY;
#define hatoku_old_version 0
#define hatoku_capabilities 1
#define hatoku_max_ai 2 //maximum auto increment value found so far
#define hatoku_ai_create_value 3
#define hatoku_key_name 4
#define hatoku_frm_data 5
#define hatoku_new_version 6
#define hatoku_cardinality 7

// use a very small pagesize for the status dictionary
#define status_dict_pagesize 1024

namespace tokudb {
namespace metadata {

// get the value for a given key in the status dictionary.
// copy the value to the supplied buffer.
// returns 0 if successful.
int read(
    DB* status_db,
    DB_TXN* txn,
    HA_METADATA_KEY k,
    void* p,
    size_t s,
    size_t* sp) {

    DBT key = {};
    key.data = &k;
    key.size = sizeof(k);
    DBT val = {};
    val.data = p;
    val.ulen = (uint32_t)s;
    val.flags = DB_DBT_USERMEM;
    int error = status_db->get(status_db, txn, &key, &val, 0);
    if (error == 0) {
        *sp = val.size;
    }
    return error;
}

// get the value for a given key in the status dictionary.
// put the value in a realloced buffer.
// returns 0 if successful.
int read_realloc(
    DB* status_db,
    DB_TXN* txn,
    HA_METADATA_KEY k,
    void** pp,
    size_t* sp) {

    DBT key = {};
    key.data = &k;
    key.size = sizeof(k);
    DBT val = {};
    val.data = *pp;
    val.size = (uint32_t)*sp;
    val.flags = DB_DBT_REALLOC;
    int error = status_db->get(status_db, txn, &key, &val, 0);
    if (error == 0) {
        *pp = val.data;
        *sp = val.size;
    }
    return error;
}

// write a key value pair into the status dictionary,
// overwriting the previous value if any.
// auto create a txn if necessary.
// returns 0 if successful.
int write_low(
    DB* status_db,
    void* key_data,
    uint key_size,
    void* val_data,
    uint val_size,
    DB_TXN *txn) {

    DBT key = {};
    key.data = key_data;
    key.size = key_size;
    DBT value = {};
    value.data = val_data;
    value.size = val_size;
    int error = status_db->put(status_db, txn, &key, &value, 0);
    return error;
}

// write a key value pair into the status dictionary,
// overwriting the previous value if any.
// the key must be a HA_METADATA_KEY.
// returns 0 if successful.
int write(
    DB* status_db,
    HA_METADATA_KEY curr_key_data,
    void* val,
    size_t val_size,
    DB_TXN* txn) {

    return
        tokudb::metadata::write_low(
            status_db,
            &curr_key_data,
            sizeof(curr_key_data),
            val,
            val_size,
            txn);
}

// remove a key from the status dictionary.
// auto create a txn if necessary.
// returns 0 if successful.
int remove_low(
    DB* status_db,
    void* key_data,
    uint key_size,
    DB_TXN* txn) {

    DBT key = {};
    key.data = key_data;
    key.size = key_size;
    int error = status_db->del(status_db, txn, &key, DB_DELETE_ANY);
    return error;
}

// remove a key from the status dictionary.
// the key must be a HA_METADATA_KEY
// returns 0 if successful.
int remove(
    DB* status_db,
    HA_METADATA_KEY curr_key_data,
    DB_TXN* txn) {
    return
        tokudb::metadata::remove_low(
            status_db,
            &curr_key_data,
            sizeof(curr_key_data),
            txn);
}

int close(DB** status_db_ptr) {
    int error = 0;
    DB* status_db = *status_db_ptr;
    if (status_db) {
        error = status_db->close(status_db, 0);
        if (error == 0)
            *status_db_ptr = NULL;
    }
    return error;
}

int create(
    DB_ENV* env,
    DB** status_db_ptr,
    const char* name,
    DB_TXN* txn) {

    int error;
    DB *status_db = NULL;

    error = db_create(&status_db, env, 0);
    if (error == 0) {
        error = status_db->set_pagesize(status_db, status_dict_pagesize);
    }
    if (error == 0) {
        error =
            status_db->open(
                status_db,
                txn,
                name,
                NULL,
                DB_BTREE, DB_CREATE | DB_EXCL,
                S_IWUSR);
    }
    if (error == 0) {
        *status_db_ptr = status_db;
    } else {
        int r = tokudb::metadata::close(&status_db);
        assert_always(r == 0);
    }
    return error;
}

int open(
    DB_ENV* env,
    DB** status_db_ptr,
    const char* name,
    DB_TXN* txn) {

    int error = 0;
    DB* status_db = NULL;
    error = db_create(&status_db, env, 0);
    if (error == 0) {
        error =
            status_db->open(
                status_db,
                txn,
                name,
                NULL,
                DB_BTREE,
                DB_THREAD,
                S_IWUSR);
    }
    if (error == 0) {
        uint32_t pagesize = 0;
        error = status_db->get_pagesize(status_db, &pagesize);
        if (error == 0 && pagesize > status_dict_pagesize) {
            error =
                status_db->change_pagesize(status_db, status_dict_pagesize);
        }
    }
    if (error == 0) {
        *status_db_ptr = status_db;
    } else {
        int r = tokudb::metadata::close(&status_db);
        assert_always(r == 0);
    }
    return error;
}

int strip_frm_data(DB_ENV* env) {
    int r;
    DB_TXN* txn = NULL;

    fprintf(stderr, "TokuDB strip_frm_data : Beginning stripping process.\n");

    r = db_env->txn_begin(env, NULL, &txn, 0);
    assert_always(r == 0);

    DBC* c = NULL;
    r = env->get_cursor_for_directory(env, txn, &c);
    assert_always(r == 0);

    DBT key = { };
    key.flags = DB_DBT_REALLOC;

    DBT val = { };
    val.flags = DB_DBT_REALLOC;
    while (1) {
        r = c->c_get(c, &key, &val, DB_NEXT);
        if (r == DB_NOTFOUND)
            break;
        const char* dname = (const char*) key.data;
        const char* iname = (const char*) val.data;
        assert_always(r == 0);

        if (strstr(iname, "_status_")) {
            fprintf(
                stderr,
                "TokuDB strip_frm_data : stripping from dname=%s iname=%s\n",
                dname,
                iname);

            DB* status_db;
            r = tokudb::metadata::open(db_env, &status_db, dname, txn);
            if (r != 0) {
                fprintf(
                    stderr,
                    "TokuDB strip_frm_data : unable to open status file %s, "
                    "error = %d\n",
                    dname,
                    r);
                continue;
            }

            // GOL : this is a godawful hack. The inventors of this did not
            // think it would be a good idea to use some kind of magic
            // identifier k/v pair so that you can in fact tell a proper status
            // file from any other file that might have the string _status_ in
            // it. Out in ha_tokudb::create, when the status file is initially
            // created, it is immediately populated with:
            //    uint hatoku_new_version=HA_TOKU_VERSION=4 and
            //    uint hatoku_capabilities=HA_TOKU_CAP=0
            // Since I can't count on the fact that these values are/were
            // _always_ 4 and 0, I can count on the fact that they _must_ be
            // there and the _must_ be sizeof(uint). That will at least give us
            // a much better idea that these are in fact status files.
            void* p = NULL;
            size_t sz;
            r =
                tokudb::metadata::read_realloc(
                    status_db,
                    txn,
                    hatoku_new_version,
                    &p,
                    &sz);
            if (r != 0) {
                fprintf(
                    stderr,
                    "TokuDB strip_frm_data : does not look like a real TokuDB "
                    "status file, new_verion is missing, leaving alone %s \n",
                    dname);

                r = tokudb::metadata::close(&status_db);
                assert_always(r == 0);
                continue;
            } else if (sz != sizeof(uint)) {
                fprintf(
                    stderr,
                    "TokuDB strip_frm_data : does not look like a real TokuDB "
                    "status file, new_verion is the wrong size, "
                    "leaving alone %s \n",
                    dname);

                tokudb::memory::free(p);
                r = tokudb::metadata::close(&status_db);
                assert_always(r == 0);
                continue;
            }
            tokudb::memory::free(p);
            p = NULL;

            r =
                tokudb::metadata::read_realloc(
                    status_db,
                    txn,
                    hatoku_capabilities,
                    &p,
                    &sz);
            if (r != 0) {
                fprintf(
                    stderr,
                    "TokuDB strip_frm_data : does not look like a real TokuDB "
                    "status file, capabilities is missing, leaving alone %s \n",
                    dname);

                r = tokudb::metadata::close(&status_db);
                assert_always(r == 0);
                continue;
            } else if (sz != sizeof(uint)) {
                fprintf(
                    stderr,
                    "TokuDB strip_frm_data : does not look like a real TokuDB "
                    "status file, capabilities is the wrong size, "
                    "leaving alone %s \n",
                    dname);

                tokudb::memory::free(p);
                r = tokudb::metadata::close(&status_db);
                assert_always(r == 0);
                continue;
            }
            tokudb::memory::free(p);

            // OK, st this point, it is probably a status file, not 100% but
            // it looks like it :(
            r = tokudb::metadata::remove(status_db, hatoku_frm_data, txn);
            if (r != 0) {
                fprintf(
                    stderr,
                    "TokuDB strip_frm_data : unable to find/strip frm data "
                    "from status file %s, error = %d\n",
                    dname,
                    r);
            }

            r = tokudb::metadata::close(&status_db);
            assert_always(r == 0);
        }
    }
    tokudb::memory::free(key.data);
    tokudb::memory::free(val.data);

    fprintf(
        stderr,
        "TokuDB strip_frm_data : Stripping process complete, beginning "
        "commit, this may take some time.\n");

    r = c->c_close(c);
    assert_always(r == 0);

    r = txn->commit(txn, 0);
    assert_always(r == 0);

    fprintf(
        stderr,
        "TokuDB strip_frm_data : Commit complete, resuming server init "
        "process.");

    return 0;
}

} // namespace metadata
} // namespace tokudb
#endif