summaryrefslogtreecommitdiffstats
path: root/storage/mroonga/lib/mrn_database_repairer.cpp
blob: 08ddd84943e9057dcf0ebfe2716668903c103923 (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
/* -*- c-basic-offset: 2 -*- */
/*
  Copyright(C) 2015-2017 Kouhei Sutou <kou@clear-code.com>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA
*/

#include <mrn_mysql.h>
#include <mrn_mysql_compat.h>
#include <mrn_constants.hpp>

#include "mrn_database_repairer.hpp"
#include "mrn_path_mapper.hpp"

// for debug
#define MRN_CLASS_NAME "mrn::DatabaseRepairer"

#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>

#ifndef WIN32
#  include <dirent.h>
#endif

namespace mrn {
  struct CheckResult {
    CheckResult() :
      is_crashed(false),
      is_corrupt(false) {
    }

    bool is_crashed;
    bool is_corrupt;
  };

  DatabaseRepairer::DatabaseRepairer(grn_ctx *ctx, THD *thd)
    : ctx_(ctx),
      thd_(thd),
      base_directory_(NULL),
      base_directory_buffer_(),
      path_prefix_(NULL),
      path_prefix_buffer_(),
      path_prefix_length_(0),
      mrn_db_file_suffix_length_(strlen(MRN_DB_FILE_SUFFIX)) {
  }

  DatabaseRepairer::~DatabaseRepairer() = default;

  bool DatabaseRepairer::is_crashed(void) {
    MRN_DBUG_ENTER_METHOD();

    CheckResult result;
    each_database(&DatabaseRepairer::check_body, &result);

    DBUG_RETURN(result.is_crashed);
  }

  bool DatabaseRepairer::is_corrupt(void) {
    MRN_DBUG_ENTER_METHOD();

    CheckResult result;
    each_database(&DatabaseRepairer::check_body, &result);

    DBUG_RETURN(result.is_corrupt);
  }

  bool DatabaseRepairer::repair(void) {
    MRN_DBUG_ENTER_METHOD();

    bool succeeded = true;
    each_database(&DatabaseRepairer::repair_body, &succeeded);

    DBUG_RETURN(succeeded);
  }

  void DatabaseRepairer::each_database(EachBodyFunc each_body_func,
                                       void *user_data) {
    MRN_DBUG_ENTER_METHOD();

    detect_paths();

#ifdef WIN32
    WIN32_FIND_DATA data;
    HANDLE finder = FindFirstFile(base_directory_, &data);
    if (finder == INVALID_HANDLE_VALUE) {
      DBUG_VOID_RETURN;
    }

    grn_ctx ctx;
    grn_rc rc = grn_ctx_init(&ctx, 0);
    if (rc == GRN_SUCCESS) {
      do {
        each_database_body(data.cFileName, &ctx, each_body_func, user_data);
      } while (FindNextFile(finder, &data) != 0);
      grn_ctx_fin(&ctx);
    } else {
      GRN_LOG(ctx_, GRN_LOG_WARNING,
              "[mroonga][database][repairer][each] "
              "failed to initialize grn_ctx: %d: %s",
              rc, grn_rc_to_string(rc));
    }
    FindClose(finder);
#else
    DIR *dir = opendir(base_directory_);
    if (!dir) {
      DBUG_VOID_RETURN;
    }

    grn_ctx ctx;
    grn_rc rc = grn_ctx_init(&ctx, 0);
    if (rc == GRN_SUCCESS) {
      while (struct dirent *entry = readdir(dir)) {
        each_database_body(entry->d_name, &ctx, each_body_func, user_data);
      }
      grn_ctx_fin(&ctx);
    } else {
      GRN_LOG(ctx_, GRN_LOG_WARNING,
              "[mroonga][database][repairer][each] "
              "failed to initialize grn_ctx: %d: %s",
              rc, grn_rc_to_string(rc));
    }
    closedir(dir);
#endif

    DBUG_VOID_RETURN;
  }

  void DatabaseRepairer::each_database_body(const char *base_path,
                                            grn_ctx *ctx,
                                            EachBodyFunc each_body_func,
                                            void *user_data) {
    MRN_DBUG_ENTER_METHOD();

    if (path_prefix_length_ > 0 &&
        strncmp(base_path, path_prefix_, path_prefix_length_) != 0) {
      DBUG_VOID_RETURN;
    }

    size_t path_length = strlen(base_path);
    if (path_length <= mrn_db_file_suffix_length_) {
      DBUG_VOID_RETURN;
    }

    if (strncmp(base_path + (path_length - mrn_db_file_suffix_length_),
                MRN_DB_FILE_SUFFIX, mrn_db_file_suffix_length_) != 0) {
      DBUG_VOID_RETURN;
    }

    char db_path[MRN_MAX_PATH_SIZE];
    snprintf(db_path, MRN_MAX_PATH_SIZE,
             "%s%c%s", base_directory_, FN_LIBCHAR, base_path);
    grn_obj *db = grn_db_open(ctx, db_path);
    if (!db) {
      DBUG_VOID_RETURN;
    }

    (this->*each_body_func)(ctx, db, db_path, user_data);

    grn_obj_close(ctx, db);

    DBUG_VOID_RETURN;
  }

  void DatabaseRepairer::detect_paths(void) {
    MRN_DBUG_ENTER_METHOD();

    const char *raw_path_prefix = mrn::PathMapper::default_path_prefix;

    if (!raw_path_prefix) {
      base_directory_ = ".";
      path_prefix_ = NULL;
      DBUG_VOID_RETURN;
    }

    strcpy(base_directory_buffer_, raw_path_prefix);
    size_t raw_path_prefix_length = strlen(raw_path_prefix);
    size_t separator_position = raw_path_prefix_length;
    for (; separator_position > 0; separator_position--) {
      if (mrn_is_directory_separator(base_directory_buffer_[separator_position])) {
        break;
      }
    }
    if (separator_position == 0 ||
        separator_position == raw_path_prefix_length) {
      base_directory_ = ".";
    } else {
      base_directory_buffer_[separator_position] = '\0';
      base_directory_ = base_directory_buffer_;
      strcpy(path_prefix_buffer_, raw_path_prefix + separator_position + 1);
      path_prefix_ = path_prefix_buffer_;
      path_prefix_length_ = strlen(path_prefix_);
    }

    DBUG_VOID_RETURN;
  }

  void DatabaseRepairer::check_body(grn_ctx *ctx,
                                    grn_obj *db,
                                    const char *db_path,
                                    void *user_data) {
    MRN_DBUG_ENTER_METHOD();

    CheckResult *result = static_cast<CheckResult *>(user_data);

    if (grn_obj_is_locked(ctx, db)) {
      result->is_crashed = true;
      result->is_corrupt = true;
      DBUG_VOID_RETURN;
    }

    grn_table_cursor *cursor;
    cursor = grn_table_cursor_open(ctx, db,
                                   NULL, 0,
                                   NULL, 0,
                                   0, -1, GRN_CURSOR_BY_ID);
    if (!cursor) {
      result->is_crashed = true;
      result->is_corrupt = true;
      DBUG_VOID_RETURN;
    }

    grn_id id;
    while ((id = grn_table_cursor_next(ctx, cursor)) != GRN_ID_NIL) {
      if (grn_id_is_builtin(ctx, id)) {
        continue;
      }

      grn_obj *object = grn_ctx_at(ctx, id);

      if (!object) {
        if (ctx->rc == GRN_SUCCESS) {
          continue;
        } else {
          result->is_corrupt = true;
          break;
        }
      }

      switch (object->header.type) {
      case GRN_TABLE_HASH_KEY :
      case GRN_TABLE_PAT_KEY:
      case GRN_TABLE_DAT_KEY:
      case GRN_TABLE_NO_KEY:
      case GRN_COLUMN_FIX_SIZE:
      case GRN_COLUMN_VAR_SIZE:
      case GRN_COLUMN_INDEX:
        if (grn_obj_is_locked(ctx_, object)) {
          result->is_crashed = true;
          result->is_corrupt = true;
        }
        break;
      default:
        break;
      }

      grn_obj_unlink(ctx, object);

      if (result->is_crashed || result->is_corrupt) {
        break;
      }
    }
    grn_table_cursor_close(ctx, cursor);

    DBUG_VOID_RETURN;
  }

  void DatabaseRepairer::repair_body(grn_ctx *ctx,
                                     grn_obj *db,
                                     const char *db_path,
                                     void *user_data) {
    MRN_DBUG_ENTER_METHOD();

    bool *succeeded = static_cast<bool *>(user_data);
    if (grn_db_recover(ctx, db) != GRN_SUCCESS) {
      push_warning_printf(thd_,
                          MRN_SEVERITY_WARNING,
                          ER_NOT_KEYFILE,
                          "mroonga: repair: "
                          "Failed to recover database: <%s>: <%s>",
                          db_path, ctx->errbuf);
      *succeeded = false;
    }

    DBUG_VOID_RETURN;
  }
}