MySQLMetaImpl.cpp 77.4 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

S
starlord 已提交
18
#include "db/meta/MySQLMetaImpl.h"
S
starlord 已提交
19
#include "MetaConsts.h"
S
starlord 已提交
20 21
#include "db/IDGenerator.h"
#include "db/Utils.h"
Z
update  
zhiru 已提交
22
#include "metrics/Metrics.h"
S
starlord 已提交
23 24
#include "utils/Exception.h"
#include "utils/Log.h"
Z
update  
zhiru 已提交
25

S
starlord 已提交
26 27
#include <mysql++/mysql++.h>
#include <string.h>
Z
update  
zhiru 已提交
28
#include <unistd.h>
S
starlord 已提交
29
#include <boost/filesystem.hpp>
Z
update  
zhiru 已提交
30 31
#include <chrono>
#include <fstream>
S
starlord 已提交
32 33 34
#include <iostream>
#include <map>
#include <mutex>
Z
update  
zhiru 已提交
35
#include <regex>
S
starlord 已提交
36 37
#include <set>
#include <sstream>
Z
update  
zhiru 已提交
38
#include <string>
Z
zhiru 已提交
39
#include <thread>
Z
update  
zhiru 已提交
40 41 42 43 44

namespace milvus {
namespace engine {
namespace meta {

45
namespace {
Z
update  
zhiru 已提交
46

S
starlord 已提交
47
Status
S
starlord 已提交
48
HandleException(const std::string& desc, const char* what = nullptr) {
S
starlord 已提交
49
    if (what == nullptr) {
S
starlord 已提交
50 51 52
        ENGINE_LOG_ERROR << desc;
        return Status(DB_META_TRANSACTION_FAILED, desc);
    }
S
starlord 已提交
53 54 55 56

    std::string msg = desc + ":" + what;
    ENGINE_LOG_ERROR << msg;
    return Status(DB_META_TRANSACTION_FAILED, msg);
57
}
Z
update  
zhiru 已提交
58

59
class MetaField {
S
starlord 已提交
60
 public:
S
starlord 已提交
61 62
    MetaField(const std::string& name, const std::string& type, const std::string& setting)
        : name_(name), type_(type), setting_(setting) {
63 64
    }

S
starlord 已提交
65 66
    std::string
    name() const {
67 68 69
        return name_;
    }

S
starlord 已提交
70 71
    std::string
    ToString() const {
72 73 74 75 76
        return name_ + " " + type_ + " " + setting_;
    }

    // mysql field type has additional information. for instance, a filed type is defined as 'BIGINT'
    // we get the type from sql is 'bigint(20)', so we need to ignore the '(20)'
S
starlord 已提交
77 78
    bool
    IsEqual(const MetaField& field) const {
79 80 81
        size_t name_len_min = field.name_.length() > name_.length() ? name_.length() : field.name_.length();
        size_t type_len_min = field.type_.length() > type_.length() ? type_.length() : field.type_.length();
        return strncasecmp(field.name_.c_str(), name_.c_str(), name_len_min) == 0 &&
S
starlord 已提交
82
               strncasecmp(field.type_.c_str(), type_.c_str(), type_len_min) == 0;
83 84
    }

S
starlord 已提交
85
 private:
86 87 88 89 90 91 92
    std::string name_;
    std::string type_;
    std::string setting_;
};

using MetaFields = std::vector<MetaField>;
class MetaSchema {
S
starlord 已提交
93
 public:
S
starlord 已提交
94
    MetaSchema(const std::string& name, const MetaFields& fields) : name_(name), fields_(fields) {
95 96
    }

S
starlord 已提交
97 98
    std::string
    name() const {
99 100 101
        return name_;
    }

S
starlord 已提交
102 103
    std::string
    ToString() const {
104
        std::string result;
S
starlord 已提交
105
        for (auto& field : fields_) {
S
starlord 已提交
106
            if (!result.empty()) {
107 108 109 110 111 112 113
                result += ",";
            }
            result += field.ToString();
        }
        return result;
    }

S
starlord 已提交
114 115 116 117
    // if the outer fields contains all this MetaSchema fields, return true
    // otherwise return false
    bool
    IsEqual(const MetaFields& fields) const {
118
        std::vector<std::string> found_field;
S
starlord 已提交
119 120
        for (const auto& this_field : fields_) {
            for (const auto& outer_field : fields) {
S
starlord 已提交
121
                if (this_field.IsEqual(outer_field)) {
122 123 124 125 126 127 128 129 130
                    found_field.push_back(this_field.name());
                    break;
                }
            }
        }

        return found_field.size() == fields_.size();
    }

S
starlord 已提交
131
 private:
132 133 134 135
    std::string name_;
    MetaFields fields_;
};

S
starlord 已提交
136
// Tables schema
137
static const MetaSchema TABLES_SCHEMA(META_TABLES, {
S
starlord 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150
                                                       MetaField("id", "BIGINT", "PRIMARY KEY AUTO_INCREMENT"),
                                                       MetaField("table_id", "VARCHAR(255)", "UNIQUE NOT NULL"),
                                                       MetaField("state", "INT", "NOT NULL"),
                                                       MetaField("dimension", "SMALLINT", "NOT NULL"),
                                                       MetaField("created_on", "BIGINT", "NOT NULL"),
                                                       MetaField("flag", "BIGINT", "DEFAULT 0 NOT NULL"),
                                                       MetaField("index_file_size", "BIGINT", "DEFAULT 1024 NOT NULL"),
                                                       MetaField("engine_type", "INT", "DEFAULT 1 NOT NULL"),
                                                       MetaField("nlist", "INT", "DEFAULT 16384 NOT NULL"),
                                                       MetaField("metric_type", "INT", "DEFAULT 1 NOT NULL"),
                                                   });

// TableFiles schema
J
JinHai-CN 已提交
151 152 153 154 155 156 157 158 159 160 161 162
static const MetaSchema TABLEFILES_SCHEMA(META_TABLEFILES, {
                                                               MetaField("id", "BIGINT", "PRIMARY KEY AUTO_INCREMENT"),
                                                               MetaField("table_id", "VARCHAR(255)", "NOT NULL"),
                                                               MetaField("engine_type", "INT", "DEFAULT 1 NOT NULL"),
                                                               MetaField("file_id", "VARCHAR(255)", "NOT NULL"),
                                                               MetaField("file_type", "INT", "DEFAULT 0 NOT NULL"),
                                                               MetaField("file_size", "BIGINT", "DEFAULT 0 NOT NULL"),
                                                               MetaField("row_count", "BIGINT", "DEFAULT 0 NOT NULL"),
                                                               MetaField("updated_time", "BIGINT", "NOT NULL"),
                                                               MetaField("created_on", "BIGINT", "NOT NULL"),
                                                               MetaField("date", "INT", "DEFAULT -1 NOT NULL"),
                                                           });
S
starlord 已提交
163 164

}  // namespace
165

166
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
167
MySQLMetaImpl::MySQLMetaImpl(const DBMetaOptions& options, const int& mode) : options_(options), mode_(mode) {
168 169 170 171 172 173
    Initialize();
}

MySQLMetaImpl::~MySQLMetaImpl() {
}

S
starlord 已提交
174
Status
S
starlord 已提交
175
MySQLMetaImpl::NextTableId(std::string& table_id) {
G
groot 已提交
176
    std::lock_guard<std::mutex> lock(genid_mutex_);  // avoid duplicated id
177 178 179 180 181 182 183
    std::stringstream ss;
    SimpleIDGenerator g;
    ss << g.GetNextIDNumber();
    table_id = ss.str();
    return Status::OK();
}

S
starlord 已提交
184
Status
S
starlord 已提交
185
MySQLMetaImpl::NextFileId(std::string& file_id) {
G
groot 已提交
186
    std::lock_guard<std::mutex> lock(genid_mutex_);  // avoid duplicated id
187 188 189 190 191 192 193
    std::stringstream ss;
    SimpleIDGenerator g;
    ss << g.GetNextIDNumber();
    file_id = ss.str();
    return Status::OK();
}

S
starlord 已提交
194 195 196
void
MySQLMetaImpl::ValidateMetaSchema() {
    if (nullptr == mysql_connection_pool_) {
197 198 199
        return;
    }

S
starlord 已提交
200
    mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
201 202 203 204
    if (connectionPtr == nullptr) {
        return;
    }

S
starlord 已提交
205
    auto validate_func = [&](const MetaSchema& schema) {
S
starlord 已提交
206
        mysqlpp::Query query_statement = connectionPtr->query();
207 208 209 210 211
        query_statement << "DESC " << schema.name() << ";";

        MetaFields exist_fields;

        try {
S
starlord 已提交
212
            mysqlpp::StoreQueryResult res = query_statement.store();
213
            for (size_t i = 0; i < res.num_rows(); i++) {
S
starlord 已提交
214
                const mysqlpp::Row& row = res[i];
215 216 217 218 219 220
                std::string name, type;
                row["Field"].to_string(name);
                row["Type"].to_string(type);

                exist_fields.push_back(MetaField(name, type, ""));
            }
S
starlord 已提交
221
        } catch (std::exception& e) {
222 223 224
            ENGINE_LOG_DEBUG << "Meta table '" << schema.name() << "' not exist and will be created";
        }

S
starlord 已提交
225
        if (exist_fields.empty()) {
226 227 228 229 230 231
            return true;
        }

        return schema.IsEqual(exist_fields);
    };

S
starlord 已提交
232
    // verify Tables
233 234 235 236
    if (!validate_func(TABLES_SCHEMA)) {
        throw Exception(DB_INCOMPATIB_META, "Meta Tables schema is created by Milvus old version");
    }

S
starlord 已提交
237
    // verufy TableFiles
238 239 240 241 242
    if (!validate_func(TABLEFILES_SCHEMA)) {
        throw Exception(DB_INCOMPATIB_META, "Meta TableFiles schema is created by Milvus old version");
    }
}

S
starlord 已提交
243 244
Status
MySQLMetaImpl::Initialize() {
S
starlord 已提交
245
    // step 1: create db root path
S
starlord 已提交
246 247
    if (!boost::filesystem::is_directory(options_.path_)) {
        auto ret = boost::filesystem::create_directory(options_.path_);
248
        if (!ret) {
S
starlord 已提交
249
            std::string msg = "Failed to create db directory " + options_.path_;
S
starlord 已提交
250 251
            ENGINE_LOG_ERROR << msg;
            return Status(DB_META_TRANSACTION_FAILED, msg);
Z
update  
zhiru 已提交
252 253 254
        }
    }

S
starlord 已提交
255
    std::string uri = options_.backend_uri_;
256

S
starlord 已提交
257
    // step 2: parse and check meta uri
258 259
    utils::MetaUriInfo uri_info;
    auto status = utils::ParseMetaUri(uri, uri_info);
S
starlord 已提交
260
    if (!status.ok()) {
261 262 263 264 265 266 267 268 269 270 271
        std::string msg = "Wrong URI format: " + uri;
        ENGINE_LOG_ERROR << msg;
        throw Exception(DB_INVALID_META_URI, msg);
    }

    if (strcasecmp(uri_info.dialect_.c_str(), "mysql") != 0) {
        std::string msg = "URI's dialect is not MySQL";
        ENGINE_LOG_ERROR << msg;
        throw Exception(DB_INVALID_META_URI, msg);
    }

S
starlord 已提交
272
    // step 3: connect mysql
273 274 275 276 277 278 279
    int thread_hint = std::thread::hardware_concurrency();
    int max_pool_size = (thread_hint == 0) ? 8 : thread_hint;
    unsigned int port = 0;
    if (!uri_info.port_.empty()) {
        port = std::stoi(uri_info.port_);
    }

S
starlord 已提交
280 281
    mysql_connection_pool_ = std::make_shared<MySQLConnectionPool>(
        uri_info.db_name_, uri_info.username_, uri_info.password_, uri_info.host_, port, max_pool_size);
282 283
    ENGINE_LOG_DEBUG << "MySQL connection pool: maximum pool size = " << std::to_string(max_pool_size);

S
starlord 已提交
284
    // step 4: validate to avoid open old version schema
285 286
    ValidateMetaSchema();

S
starlord 已提交
287
    // step 5: create meta tables
288
    try {
Y
yudong.cai 已提交
289
        if (mode_ != DBOptions::MODE::CLUSTER_READONLY) {
290
            CleanUp();
291 292
        }

293
        {
S
starlord 已提交
294
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
295

296 297 298
            if (connectionPtr == nullptr) {
                return Status(DB_ERROR, "Failed to connect to database server");
            }
299

300 301 302
            if (!connectionPtr->thread_aware()) {
                ENGINE_LOG_ERROR << "MySQL++ wasn't built with thread awareness! Can't run without it.";
                return Status(DB_ERROR, "MySQL++ wasn't built with thread awareness! Can't run without it.");
303
            }
S
starlord 已提交
304
            mysqlpp::Query InitializeQuery = connectionPtr->query();
305

S
starlord 已提交
306 307
            InitializeQuery << "CREATE TABLE IF NOT EXISTS " << TABLES_SCHEMA.name() << " ("
                            << TABLES_SCHEMA.ToString() + ");";
308

309
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::Initialize: " << InitializeQuery.str();
Z
update  
zhiru 已提交
310

311 312 313
            if (!InitializeQuery.exec()) {
                return HandleException("Initialization Error", InitializeQuery.error());
            }
314

S
starlord 已提交
315 316
            InitializeQuery << "CREATE TABLE IF NOT EXISTS " << TABLEFILES_SCHEMA.name() << " ("
                            << TABLEFILES_SCHEMA.ToString() + ");";
Z
update  
zhiru 已提交
317

318
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::Initialize: " << InitializeQuery.str();
Z
update  
zhiru 已提交
319

320 321 322
            if (!InitializeQuery.exec()) {
                return HandleException("Initialization Error", InitializeQuery.error());
            }
S
starlord 已提交
323 324
        }  // Scoped Connection
    } catch (std::exception& e) {
325
        return HandleException("GENERAL ERROR DURING INITIALIZATION", e.what());
Z
update  
zhiru 已提交
326
    }
S
starlord 已提交
327 328

    return Status::OK();
329 330
}

S
starlord 已提交
331
// TODO(myh): Delete single vecotor by id
S
starlord 已提交
332
Status
S
starlord 已提交
333
MySQLMetaImpl::DropPartitionsByDates(const std::string& table_id, const DatesT& dates) {
334
    if (dates.empty()) {
P
peng.xu 已提交
335 336 337
        return Status::OK();
    }

338 339 340 341 342 343
    TableSchema table_schema;
    table_schema.table_id_ = table_id;
    auto status = DescribeTable(table_schema);
    if (!status.ok()) {
        return status;
    }
Z
zhiru 已提交
344

345 346
    try {
        std::stringstream dateListSS;
S
starlord 已提交
347
        for (auto& date : dates) {
348 349 350
            dateListSS << std::to_string(date) << ", ";
        }
        std::string dateListStr = dateListSS.str();
S
starlord 已提交
351
        dateListStr = dateListStr.substr(0, dateListStr.size() - 2);  // remove the last ", "
Z
update  
zhiru 已提交
352

353
        {
S
starlord 已提交
354
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
Z
update  
zhiru 已提交
355

356
            if (connectionPtr == nullptr) {
S
starlord 已提交
357
                return Status(DB_ERROR, "Failed to connect to database server");
358
            }
Z
update  
zhiru 已提交
359

S
starlord 已提交
360
            mysqlpp::Query dropPartitionsByDatesQuery = connectionPtr->query();
361

S
starlord 已提交
362 363 364 365 366
            dropPartitionsByDatesQuery << "UPDATE " << META_TABLEFILES << " "
                                       << "SET file_type = " << std::to_string(TableFileSchema::TO_DELETE) << ","
                                       << "updated_time = " << utils::GetMicroSecTimeStamp() << " "
                                       << "WHERE table_id = " << mysqlpp::quote << table_id << " AND "
                                       << "date in (" << dateListStr << ");";
Z
update  
zhiru 已提交
367

368
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::DropPartitionsByDates: " << dropPartitionsByDatesQuery.str();
Z
update  
zhiru 已提交
369

370
            if (!dropPartitionsByDatesQuery.exec()) {
S
starlord 已提交
371 372
                return HandleException("QUERY ERROR WHEN DROPPING PARTITIONS BY DATES",
                                       dropPartitionsByDatesQuery.error());
Z
update  
zhiru 已提交
373
            }
S
starlord 已提交
374
        }  // Scoped Connection
375 376

        ENGINE_LOG_DEBUG << "Successfully drop partitions, table id = " << table_schema.table_id_;
S
starlord 已提交
377
    } catch (std::exception& e) {
S
starlord 已提交
378
        return HandleException("GENERAL ERROR WHEN DROPPING PARTITIONS BY DATES", e.what());
Z
update  
zhiru 已提交
379
    }
380 381
    return Status::OK();
}
Z
update  
zhiru 已提交
382

S
starlord 已提交
383
Status
S
starlord 已提交
384
MySQLMetaImpl::CreateTable(TableSchema& table_schema) {
385
    try {
Y
Yu Kun 已提交
386
        server::MetricCollector metric;
387
        {
S
starlord 已提交
388
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
389

390
            if (connectionPtr == nullptr) {
S
starlord 已提交
391
                return Status(DB_ERROR, "Failed to connect to database server");
392
            }
Z
update  
zhiru 已提交
393

S
starlord 已提交
394
            mysqlpp::Query createTableQuery = connectionPtr->query();
Z
update  
zhiru 已提交
395

396 397 398
            if (table_schema.table_id_.empty()) {
                NextTableId(table_schema.table_id_);
            } else {
S
starlord 已提交
399 400
                createTableQuery << "SELECT state FROM " << META_TABLES << " "
                                 << "WHERE table_id = " << mysqlpp::quote << table_schema.table_id_ << ";";
Z
zhiru 已提交
401

402
                ENGINE_LOG_DEBUG << "MySQLMetaImpl::CreateTable: " << createTableQuery.str();
Z
update  
zhiru 已提交
403

S
starlord 已提交
404
                mysqlpp::StoreQueryResult res = createTableQuery.store();
405

406 407 408
                if (res.num_rows() == 1) {
                    int state = res[0]["state"];
                    if (TableSchema::TO_DELETE == state) {
S
starlord 已提交
409
                        return Status(DB_ERROR, "Table already exists and it is in delete state, please wait a second");
410
                    } else {
S
starlord 已提交
411
                        return Status(DB_ALREADY_EXIST, "Table already exists");
412 413 414
                    }
                }
            }
415

416 417
            table_schema.id_ = -1;
            table_schema.created_on_ = utils::GetMicroSecTimeStamp();
Z
update  
zhiru 已提交
418

S
starlord 已提交
419
            std::string id = "NULL";  // auto-increment
420 421 422 423
            std::string table_id = table_schema.table_id_;
            std::string state = std::to_string(table_schema.state_);
            std::string dimension = std::to_string(table_schema.dimension_);
            std::string created_on = std::to_string(table_schema.created_on_);
S
starlord 已提交
424 425
            std::string flag = std::to_string(table_schema.flag_);
            std::string index_file_size = std::to_string(table_schema.index_file_size_);
426
            std::string engine_type = std::to_string(table_schema.engine_type_);
S
starlord 已提交
427 428
            std::string nlist = std::to_string(table_schema.nlist_);
            std::string metric_type = std::to_string(table_schema.metric_type_);
Z
update  
zhiru 已提交
429

S
starlord 已提交
430 431 432 433
            createTableQuery << "INSERT INTO " << META_TABLES << " "
                             << "VALUES(" << id << ", " << mysqlpp::quote << table_id << ", " << state << ", "
                             << dimension << ", " << created_on << ", " << flag << ", " << index_file_size << ", "
                             << engine_type << ", " << nlist << ", " << metric_type << ");";
Z
update  
zhiru 已提交
434

435
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::CreateTable: " << createTableQuery.str();
436

S
starlord 已提交
437
            if (mysqlpp::SimpleResult res = createTableQuery.execute()) {
S
starlord 已提交
438
                table_schema.id_ = res.insert_id();  // Might need to use SELECT LAST_INSERT_ID()?
439

S
starlord 已提交
440
                // Consume all results to avoid "Commands out of sync" error
441
            } else {
S
starlord 已提交
442
                return HandleException("Add Table Error", createTableQuery.error());
443
            }
S
starlord 已提交
444
        }  // Scoped Connection
445

446
        ENGINE_LOG_DEBUG << "Successfully create table: " << table_schema.table_id_;
447
        return utils::CreateTablePath(options_, table_schema.table_id_);
S
starlord 已提交
448
    } catch (std::exception& e) {
S
starlord 已提交
449
        return HandleException("GENERAL ERROR WHEN CREATING TABLE", e.what());
450 451
    }
}
452

S
starlord 已提交
453
Status
S
starlord 已提交
454 455
MySQLMetaImpl::FilesByType(const std::string& table_id, const std::vector<int>& file_types,
                           std::vector<std::string>& file_ids) {
S
starlord 已提交
456
    if (file_types.empty()) {
S
starlord 已提交
457
        return Status(DB_ERROR, "file types array is empty");
458
    }
Z
zhiru 已提交
459 460

    try {
461 462
        file_ids.clear();

S
starlord 已提交
463
        mysqlpp::StoreQueryResult res;
Z
zhiru 已提交
464
        {
S
starlord 已提交
465
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
Z
zhiru 已提交
466 467

            if (connectionPtr == nullptr) {
S
starlord 已提交
468
                return Status(DB_ERROR, "Failed to connect to database server");
Z
zhiru 已提交
469 470
            }

471
            std::string types;
S
starlord 已提交
472 473
            for (auto type : file_types) {
                if (!types.empty()) {
474 475 476 477 478
                    types += ",";
                }
                types += std::to_string(type);
            }

S
starlord 已提交
479
            mysqlpp::Query hasNonIndexFilesQuery = connectionPtr->query();
S
starlord 已提交
480 481 482 483
            // since table_id is a unique column we just need to check whether it exists or not
            hasNonIndexFilesQuery << "SELECT file_id, file_type FROM " << META_TABLEFILES << " "
                                  << "WHERE table_id = " << mysqlpp::quote << table_id << " AND "
                                  << "file_type in (" << types << ");";
Z
zhiru 已提交
484

485
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::FilesByType: " << hasNonIndexFilesQuery.str();
Z
zhiru 已提交
486 487

            res = hasNonIndexFilesQuery.store();
S
starlord 已提交
488
        }  // Scoped Connection
Z
zhiru 已提交
489

490 491 492
        if (res.num_rows() > 0) {
            int raw_count = 0, new_count = 0, new_merge_count = 0, new_index_count = 0;
            int to_index_count = 0, index_count = 0, backup_count = 0;
S
starlord 已提交
493
            for (auto& resRow : res) {
494 495 496 497 498 499
                std::string file_id;
                resRow["file_id"].to_string(file_id);
                file_ids.push_back(file_id);

                int32_t file_type = resRow["file_type"];
                switch (file_type) {
S
starlord 已提交
500 501
                    case (int)TableFileSchema::RAW:
                        raw_count++;
502
                        break;
S
starlord 已提交
503 504
                    case (int)TableFileSchema::NEW:
                        new_count++;
505
                        break;
S
starlord 已提交
506 507
                    case (int)TableFileSchema::NEW_MERGE:
                        new_merge_count++;
508
                        break;
S
starlord 已提交
509 510
                    case (int)TableFileSchema::NEW_INDEX:
                        new_index_count++;
511
                        break;
S
starlord 已提交
512 513
                    case (int)TableFileSchema::TO_INDEX:
                        to_index_count++;
514
                        break;
S
starlord 已提交
515 516
                    case (int)TableFileSchema::INDEX:
                        index_count++;
517
                        break;
S
starlord 已提交
518 519 520 521
                    case (int)TableFileSchema::BACKUP:
                        backup_count++;
                        break;
                    default:
522 523 524 525 526 527 528 529 530
                        break;
                }
            }

            ENGINE_LOG_DEBUG << "Table " << table_id << " currently has raw files:" << raw_count
                             << " new files:" << new_count << " new_merge files:" << new_merge_count
                             << " new_index files:" << new_index_count << " to_index files:" << to_index_count
                             << " index files:" << index_count << " backup files:" << backup_count;
        }
S
starlord 已提交
531
    } catch (std::exception& e) {
S
starlord 已提交
532
        return HandleException("GENERAL ERROR WHEN GET FILE BY TYPE", e.what());
Z
zhiru 已提交
533 534
    }

535 536
    return Status::OK();
}
537

S
starlord 已提交
538
Status
S
starlord 已提交
539
MySQLMetaImpl::UpdateTableIndex(const std::string& table_id, const TableIndex& index) {
S
starlord 已提交
540
    try {
Y
Yu Kun 已提交
541
        server::MetricCollector metric;
S
starlord 已提交
542 543

        {
S
starlord 已提交
544
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
S
starlord 已提交
545 546

            if (connectionPtr == nullptr) {
S
starlord 已提交
547
                return Status(DB_ERROR, "Failed to connect to database server");
S
starlord 已提交
548 549
            }

S
starlord 已提交
550
            mysqlpp::Query updateTableIndexParamQuery = connectionPtr->query();
S
starlord 已提交
551 552 553
            updateTableIndexParamQuery << "SELECT id, state, dimension, created_on FROM " << META_TABLES << " "
                                       << "WHERE table_id = " << mysqlpp::quote << table_id << " AND "
                                       << "state <> " << std::to_string(TableSchema::TO_DELETE) << ";";
S
starlord 已提交
554

S
starlord 已提交
555
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::UpdateTableIndex: " << updateTableIndexParamQuery.str();
S
starlord 已提交
556

S
starlord 已提交
557
            mysqlpp::StoreQueryResult res = updateTableIndexParamQuery.store();
S
starlord 已提交
558 559

            if (res.num_rows() == 1) {
S
starlord 已提交
560
                const mysqlpp::Row& resRow = res[0];
S
starlord 已提交
561 562 563 564 565 566

                size_t id = resRow["id"];
                int32_t state = resRow["state"];
                uint16_t dimension = resRow["dimension"];
                int64_t created_on = resRow["created_on"];

S
starlord 已提交
567 568 569 570 571 572 573 574 575
                updateTableIndexParamQuery << "UPDATE " << META_TABLES << " "
                                           << "SET id = " << id << ", "
                                           << "state = " << state << ", "
                                           << "dimension = " << dimension << ", "
                                           << "created_on = " << created_on << ", "
                                           << "engine_type = " << index.engine_type_ << ", "
                                           << "nlist = " << index.nlist_ << ", "
                                           << "metric_type = " << index.metric_type_ << " "
                                           << "WHERE table_id = " << mysqlpp::quote << table_id << ";";
S
starlord 已提交
576

S
starlord 已提交
577
                ENGINE_LOG_DEBUG << "MySQLMetaImpl::UpdateTableIndex: " << updateTableIndexParamQuery.str();
S
starlord 已提交
578 579

                if (!updateTableIndexParamQuery.exec()) {
S
starlord 已提交
580 581
                    return HandleException("QUERY ERROR WHEN UPDATING TABLE INDEX PARAM",
                                           updateTableIndexParamQuery.error());
S
starlord 已提交
582 583
                }
            } else {
S
starlord 已提交
584
                return Status(DB_NOT_FOUND, "Table " + table_id + " not found");
S
starlord 已提交
585
            }
S
starlord 已提交
586
        }  // Scoped Connection
S
starlord 已提交
587

588
        ENGINE_LOG_DEBUG << "Successfully update table index, table id = " << table_id;
S
starlord 已提交
589
    } catch (std::exception& e) {
S
starlord 已提交
590
        return HandleException("GENERAL ERROR WHEN UPDATING TABLE INDEX PARAM", e.what());
S
starlord 已提交
591 592
    }

593 594 595
    return Status::OK();
}

S
starlord 已提交
596
Status
S
starlord 已提交
597
MySQLMetaImpl::UpdateTableFlag(const std::string& table_id, int64_t flag) {
S
starlord 已提交
598
    try {
Y
Yu Kun 已提交
599
        server::MetricCollector metric;
S
starlord 已提交
600 601

        {
S
starlord 已提交
602
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
S
starlord 已提交
603 604

            if (connectionPtr == nullptr) {
S
starlord 已提交
605
                return Status(DB_ERROR, "Failed to connect to database server");
S
starlord 已提交
606 607
            }

S
starlord 已提交
608
            mysqlpp::Query updateTableFlagQuery = connectionPtr->query();
S
starlord 已提交
609 610 611
            updateTableFlagQuery << "UPDATE " << META_TABLES << " "
                                 << "SET flag = " << flag << " "
                                 << "WHERE table_id = " << mysqlpp::quote << table_id << ";";
S
starlord 已提交
612 613 614 615

            ENGINE_LOG_DEBUG << "MySQLMetaImpl::UpdateTableFlag: " << updateTableFlagQuery.str();

            if (!updateTableFlagQuery.exec()) {
S
starlord 已提交
616
                return HandleException("QUERY ERROR WHEN UPDATING TABLE FLAG", updateTableFlagQuery.error());
S
starlord 已提交
617
            }
S
starlord 已提交
618
        }  // Scoped Connection
S
starlord 已提交
619

620
        ENGINE_LOG_DEBUG << "Successfully update table flag, table id = " << table_id;
S
starlord 已提交
621
    } catch (std::exception& e) {
S
starlord 已提交
622
        return HandleException("GENERAL ERROR WHEN UPDATING TABLE FLAG", e.what());
S
starlord 已提交
623 624 625 626 627
    }

    return Status::OK();
}

S
starlord 已提交
628
Status
S
starlord 已提交
629
MySQLMetaImpl::DescribeTableIndex(const std::string& table_id, TableIndex& index) {
S
starlord 已提交
630
    try {
Y
Yu Kun 已提交
631
        server::MetricCollector metric;
632

S
starlord 已提交
633
        {
S
starlord 已提交
634
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
635

S
starlord 已提交
636
            if (connectionPtr == nullptr) {
S
starlord 已提交
637
                return Status(DB_ERROR, "Failed to connect to database server");
S
starlord 已提交
638 639
            }

S
starlord 已提交
640
            mysqlpp::Query describeTableIndexQuery = connectionPtr->query();
S
starlord 已提交
641 642 643 644
            describeTableIndexQuery << "SELECT engine_type, nlist, index_file_size, metric_type FROM " << META_TABLES
                                    << " "
                                    << "WHERE table_id = " << mysqlpp::quote << table_id << " AND "
                                    << "state <> " << std::to_string(TableSchema::TO_DELETE) << ";";
Z
update  
zhiru 已提交
645

S
starlord 已提交
646
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::DescribeTableIndex: " << describeTableIndexQuery.str();
Z
update  
zhiru 已提交
647

S
starlord 已提交
648
            mysqlpp::StoreQueryResult res = describeTableIndexQuery.store();
S
starlord 已提交
649 650

            if (res.num_rows() == 1) {
S
starlord 已提交
651
                const mysqlpp::Row& resRow = res[0];
S
starlord 已提交
652 653 654 655 656

                index.engine_type_ = resRow["engine_type"];
                index.nlist_ = resRow["nlist"];
                index.metric_type_ = resRow["metric_type"];
            } else {
S
starlord 已提交
657
                return Status(DB_NOT_FOUND, "Table " + table_id + " not found");
S
starlord 已提交
658
            }
S
starlord 已提交
659 660
        }  // Scoped Connection
    } catch (std::exception& e) {
S
starlord 已提交
661
        return HandleException("GENERAL ERROR WHEN UPDATING TABLE FLAG", e.what());
S
starlord 已提交
662 663 664 665 666
    }

    return Status::OK();
}

S
starlord 已提交
667
Status
S
starlord 已提交
668
MySQLMetaImpl::DropTableIndex(const std::string& table_id) {
S
starlord 已提交
669
    try {
Y
Yu Kun 已提交
670
        server::MetricCollector metric;
Z
update  
zhiru 已提交
671

672
        {
S
starlord 已提交
673
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
674

675
            if (connectionPtr == nullptr) {
S
starlord 已提交
676
                return Status(DB_ERROR, "Failed to connect to database server");
677
            }
678

S
starlord 已提交
679
            mysqlpp::Query dropTableIndexQuery = connectionPtr->query();
S
starlord 已提交
680

S
starlord 已提交
681 682 683 684 685 686
            // soft delete index files
            dropTableIndexQuery << "UPDATE " << META_TABLEFILES << " "
                                << "SET file_type = " << std::to_string(TableFileSchema::TO_DELETE) << ","
                                << "updated_time = " << utils::GetMicroSecTimeStamp() << " "
                                << "WHERE table_id = " << mysqlpp::quote << table_id << " AND "
                                << "file_type = " << std::to_string(TableFileSchema::INDEX) << ";";
S
starlord 已提交
687 688 689 690

            ENGINE_LOG_DEBUG << "MySQLMetaImpl::DropTableIndex: " << dropTableIndexQuery.str();

            if (!dropTableIndexQuery.exec()) {
S
starlord 已提交
691
                return HandleException("QUERY ERROR WHEN DROPPING TABLE INDEX", dropTableIndexQuery.error());
S
starlord 已提交
692 693
            }

S
starlord 已提交
694 695 696 697 698 699
            // set all backup file to raw
            dropTableIndexQuery << "UPDATE " << META_TABLEFILES << " "
                                << "SET file_type = " << std::to_string(TableFileSchema::RAW) << ","
                                << "updated_time = " << utils::GetMicroSecTimeStamp() << " "
                                << "WHERE table_id = " << mysqlpp::quote << table_id << " AND "
                                << "file_type = " << std::to_string(TableFileSchema::BACKUP) << ";";
S
starlord 已提交
700 701 702 703

            ENGINE_LOG_DEBUG << "MySQLMetaImpl::DropTableIndex: " << dropTableIndexQuery.str();

            if (!dropTableIndexQuery.exec()) {
S
starlord 已提交
704
                return HandleException("QUERY ERROR WHEN DROPPING TABLE INDEX", dropTableIndexQuery.error());
705 706
            }

S
starlord 已提交
707 708 709 710 711 712
            // set table index type to raw
            dropTableIndexQuery << "UPDATE " << META_TABLES << " "
                                << "SET engine_type = " << std::to_string(DEFAULT_ENGINE_TYPE) << ","
                                << "nlist = " << std::to_string(DEFAULT_NLIST) << ", "
                                << "metric_type = " << std::to_string(DEFAULT_METRIC_TYPE) << " "
                                << "WHERE table_id = " << mysqlpp::quote << table_id << ";";
713 714 715 716

            ENGINE_LOG_DEBUG << "MySQLMetaImpl::DropTableIndex: " << dropTableIndexQuery.str();

            if (!dropTableIndexQuery.exec()) {
S
starlord 已提交
717
                return HandleException("QUERY ERROR WHEN DROPPING TABLE INDEX", dropTableIndexQuery.error());
S
starlord 已提交
718
            }
S
starlord 已提交
719
        }  // Scoped Connection
S
starlord 已提交
720

721
        ENGINE_LOG_DEBUG << "Successfully drop table index, table id = " << table_id;
S
starlord 已提交
722
    } catch (std::exception& e) {
S
starlord 已提交
723
        return HandleException("GENERAL ERROR WHEN DROPPING TABLE INDEX", e.what());
S
starlord 已提交
724
    }
725

S
starlord 已提交
726 727
    return Status::OK();
}
Z
update  
zhiru 已提交
728

S
starlord 已提交
729
Status
S
starlord 已提交
730
MySQLMetaImpl::DeleteTable(const std::string& table_id) {
S
starlord 已提交
731
    try {
Y
Yu Kun 已提交
732
        server::MetricCollector metric;
S
starlord 已提交
733
        {
S
starlord 已提交
734
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
Z
zhiru 已提交
735

S
starlord 已提交
736
            if (connectionPtr == nullptr) {
S
starlord 已提交
737
                return Status(DB_ERROR, "Failed to connect to database server");
S
starlord 已提交
738
            }
Z
zhiru 已提交
739

S
starlord 已提交
740
            // soft delete table
S
starlord 已提交
741
            mysqlpp::Query deleteTableQuery = connectionPtr->query();
S
starlord 已提交
742 743 744 745
            //
            deleteTableQuery << "UPDATE " << META_TABLES << " "
                             << "SET state = " << std::to_string(TableSchema::TO_DELETE) << " "
                             << "WHERE table_id = " << mysqlpp::quote << table_id << ";";
Z
update  
zhiru 已提交
746

747
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::DeleteTable: " << deleteTableQuery.str();
748

749
            if (!deleteTableQuery.exec()) {
S
starlord 已提交
750
                return HandleException("QUERY ERROR WHEN DELETING TABLE", deleteTableQuery.error());
751
            }
S
starlord 已提交
752
        }  // Scoped Connection
Z
zhiru 已提交
753

Y
yudong.cai 已提交
754
        if (mode_ == DBOptions::MODE::CLUSTER_WRITABLE) {
755 756
            DeleteTableFiles(table_id);
        }
Z
update  
zhiru 已提交
757

758
        ENGINE_LOG_DEBUG << "Successfully delete table, table id = " << table_id;
S
starlord 已提交
759
    } catch (std::exception& e) {
S
starlord 已提交
760
        return HandleException("GENERAL ERROR WHEN DELETING TABLE", e.what());
761
    }
Z
update  
zhiru 已提交
762

763 764
    return Status::OK();
}
Z
update  
zhiru 已提交
765

S
starlord 已提交
766
Status
S
starlord 已提交
767
MySQLMetaImpl::DeleteTableFiles(const std::string& table_id) {
768
    try {
Y
Yu Kun 已提交
769
        server::MetricCollector metric;
770
        {
S
starlord 已提交
771
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
772 773

            if (connectionPtr == nullptr) {
S
starlord 已提交
774
                return Status(DB_ERROR, "Failed to connect to database server");
775
            }
776

S
starlord 已提交
777
            // soft delete table files
S
starlord 已提交
778
            mysqlpp::Query deleteTableFilesQuery = connectionPtr->query();
779
            //
S
starlord 已提交
780 781 782 783 784
            deleteTableFilesQuery << "UPDATE " << META_TABLEFILES << " "
                                  << "SET file_type = " << std::to_string(TableFileSchema::TO_DELETE) << ", "
                                  << "updated_time = " << std::to_string(utils::GetMicroSecTimeStamp()) << " "
                                  << "WHERE table_id = " << mysqlpp::quote << table_id << " AND "
                                  << "file_type <> " << std::to_string(TableFileSchema::TO_DELETE) << ";";
785

786
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::DeleteTableFiles: " << deleteTableFilesQuery.str();
787

788
            if (!deleteTableFilesQuery.exec()) {
S
starlord 已提交
789
                return HandleException("QUERY ERROR WHEN DELETING TABLE FILES", deleteTableFilesQuery.error());
790
            }
S
starlord 已提交
791
        }  // Scoped Connection
792 793

        ENGINE_LOG_DEBUG << "Successfully delete table files, table id = " << table_id;
S
starlord 已提交
794
    } catch (std::exception& e) {
S
starlord 已提交
795
        return HandleException("GENERAL ERROR WHEN DELETING TABLE FILES", e.what());
Z
update  
zhiru 已提交
796 797
    }

798 799
    return Status::OK();
}
Z
zhiru 已提交
800

S
starlord 已提交
801
Status
S
starlord 已提交
802
MySQLMetaImpl::DescribeTable(TableSchema& table_schema) {
803
    try {
Y
Yu Kun 已提交
804
        server::MetricCollector metric;
S
starlord 已提交
805
        mysqlpp::StoreQueryResult res;
806
        {
S
starlord 已提交
807
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
808

809
            if (connectionPtr == nullptr) {
S
starlord 已提交
810
                return Status(DB_ERROR, "Failed to connect to database server");
811
            }
Z
zhiru 已提交
812

S
starlord 已提交
813 814 815
            mysqlpp::Query describeTableQuery = connectionPtr->query();
            describeTableQuery
                << "SELECT id, state, dimension, created_on, flag, index_file_size, engine_type, nlist, metric_type "
S
starlord 已提交
816 817 818
                << " FROM " << META_TABLES << " "
                << "WHERE table_id = " << mysqlpp::quote << table_schema.table_id_ << " "
                << "AND state <> " << std::to_string(TableSchema::TO_DELETE) << ";";
819 820

            ENGINE_LOG_DEBUG << "MySQLMetaImpl::DescribeTable: " << describeTableQuery.str();
Z
update  
zhiru 已提交
821

822
            res = describeTableQuery.store();
S
starlord 已提交
823
        }  // Scoped Connection
824

825
        if (res.num_rows() == 1) {
S
starlord 已提交
826
            const mysqlpp::Row& resRow = res[0];
827

S
starlord 已提交
828
            table_schema.id_ = resRow["id"];  // implicit conversion
Z
update  
zhiru 已提交
829

S
starlord 已提交
830 831
            table_schema.state_ = resRow["state"];

832
            table_schema.dimension_ = resRow["dimension"];
833

S
starlord 已提交
834 835 836 837
            table_schema.created_on_ = resRow["created_on"];

            table_schema.flag_ = resRow["flag"];

838 839
            table_schema.index_file_size_ = resRow["index_file_size"];

840
            table_schema.engine_type_ = resRow["engine_type"];
S
starlord 已提交
841 842 843 844

            table_schema.nlist_ = resRow["nlist"];

            table_schema.metric_type_ = resRow["metric_type"];
845
        } else {
S
starlord 已提交
846
            return Status(DB_NOT_FOUND, "Table " + table_schema.table_id_ + " not found");
847
        }
S
starlord 已提交
848
    } catch (std::exception& e) {
S
starlord 已提交
849
        return HandleException("GENERAL ERROR WHEN DESCRIBING TABLE", e.what());
Z
update  
zhiru 已提交
850 851
    }

852 853
    return Status::OK();
}
Z
zhiru 已提交
854

S
starlord 已提交
855
Status
S
starlord 已提交
856
MySQLMetaImpl::HasTable(const std::string& table_id, bool& has_or_not) {
857
    try {
Y
Yu Kun 已提交
858
        server::MetricCollector metric;
S
starlord 已提交
859
        mysqlpp::StoreQueryResult res;
860
        {
S
starlord 已提交
861
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
Z
update  
zhiru 已提交
862

863
            if (connectionPtr == nullptr) {
S
starlord 已提交
864
                return Status(DB_ERROR, "Failed to connect to database server");
865
            }
Z
update  
zhiru 已提交
866

S
starlord 已提交
867
            mysqlpp::Query hasTableQuery = connectionPtr->query();
S
starlord 已提交
868 869 870 871 872 873 874
            // since table_id is a unique column we just need to check whether it exists or not
            hasTableQuery << "SELECT EXISTS "
                          << "(SELECT 1 FROM " << META_TABLES << " "
                          << "WHERE table_id = " << mysqlpp::quote << table_id << " "
                          << "AND state <> " << std::to_string(TableSchema::TO_DELETE) << ") "
                          << "AS " << mysqlpp::quote << "check"
                          << ";";
Z
update  
zhiru 已提交
875

876
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::HasTable: " << hasTableQuery.str();
877

878
            res = hasTableQuery.store();
S
starlord 已提交
879
        }  // Scoped Connection
880

881 882
        int check = res[0]["check"];
        has_or_not = (check == 1);
S
starlord 已提交
883
    } catch (std::exception& e) {
S
starlord 已提交
884
        return HandleException("GENERAL ERROR WHEN CHECKING IF TABLE EXISTS", e.what());
885
    }
886

887 888
    return Status::OK();
}
889

S
starlord 已提交
890
Status
S
starlord 已提交
891
MySQLMetaImpl::AllTables(std::vector<TableSchema>& table_schema_array) {
892
    try {
Y
Yu Kun 已提交
893
        server::MetricCollector metric;
S
starlord 已提交
894
        mysqlpp::StoreQueryResult res;
895
        {
S
starlord 已提交
896
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
897

898
            if (connectionPtr == nullptr) {
S
starlord 已提交
899
                return Status(DB_ERROR, "Failed to connect to database server");
900
            }
Z
update  
zhiru 已提交
901

S
starlord 已提交
902 903
            mysqlpp::Query allTablesQuery = connectionPtr->query();
            allTablesQuery << "SELECT id, table_id, dimension, engine_type, nlist, index_file_size, metric_type FROM "
S
starlord 已提交
904 905
                           << META_TABLES << " "
                           << "WHERE state <> " << std::to_string(TableSchema::TO_DELETE) << ";";
Z
zhiru 已提交
906

907
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::AllTables: " << allTablesQuery.str();
Z
zhiru 已提交
908

909
            res = allTablesQuery.store();
S
starlord 已提交
910
        }  // Scoped Connection
911

S
starlord 已提交
912
        for (auto& resRow : res) {
913
            TableSchema table_schema;
Z
update  
zhiru 已提交
914

S
starlord 已提交
915
            table_schema.id_ = resRow["id"];  // implicit conversion
916

917 918 919
            std::string table_id;
            resRow["table_id"].to_string(table_id);
            table_schema.table_id_ = table_id;
920

921
            table_schema.dimension_ = resRow["dimension"];
922

923 924
            table_schema.index_file_size_ = resRow["index_file_size"];

925
            table_schema.engine_type_ = resRow["engine_type"];
926

S
starlord 已提交
927 928 929 930
            table_schema.nlist_ = resRow["nlist"];

            table_schema.metric_type_ = resRow["metric_type"];

931 932
            table_schema_array.emplace_back(table_schema);
        }
S
starlord 已提交
933
    } catch (std::exception& e) {
S
starlord 已提交
934
        return HandleException("GENERAL ERROR WHEN DESCRIBING ALL TABLES", e.what());
935
    }
Z
update  
zhiru 已提交
936

937 938
    return Status::OK();
}
939

S
starlord 已提交
940
Status
S
starlord 已提交
941
MySQLMetaImpl::CreateTableFile(TableFileSchema& file_schema) {
942
    if (file_schema.date_ == EmptyDate) {
943
        file_schema.date_ = utils::GetDate();
944 945 946 947 948 949 950
    }
    TableSchema table_schema;
    table_schema.table_id_ = file_schema.table_id_;
    auto status = DescribeTable(table_schema);
    if (!status.ok()) {
        return status;
    }
951

952
    try {
Y
Yu Kun 已提交
953
        server::MetricCollector metric;
954 955 956

        NextFileId(file_schema.file_id_);
        file_schema.dimension_ = table_schema.dimension_;
957 958
        file_schema.file_size_ = 0;
        file_schema.row_count_ = 0;
959 960
        file_schema.created_on_ = utils::GetMicroSecTimeStamp();
        file_schema.updated_time_ = file_schema.created_on_;
961
        file_schema.index_file_size_ = table_schema.index_file_size_;
962
        file_schema.engine_type_ = table_schema.engine_type_;
S
starlord 已提交
963 964
        file_schema.nlist_ = table_schema.nlist_;
        file_schema.metric_type_ = table_schema.metric_type_;
965

S
starlord 已提交
966
        std::string id = "NULL";  // auto-increment
967 968 969 970
        std::string table_id = file_schema.table_id_;
        std::string engine_type = std::to_string(file_schema.engine_type_);
        std::string file_id = file_schema.file_id_;
        std::string file_type = std::to_string(file_schema.file_type_);
S
starlord 已提交
971
        std::string file_size = std::to_string(file_schema.file_size_);
972
        std::string row_count = std::to_string(file_schema.row_count_);
973 974 975 976 977
        std::string updated_time = std::to_string(file_schema.updated_time_);
        std::string created_on = std::to_string(file_schema.created_on_);
        std::string date = std::to_string(file_schema.date_);

        {
S
starlord 已提交
978
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
Z
update  
zhiru 已提交
979

980
            if (connectionPtr == nullptr) {
S
starlord 已提交
981
                return Status(DB_ERROR, "Failed to connect to database server");
982
            }
Z
update  
zhiru 已提交
983

S
starlord 已提交
984
            mysqlpp::Query createTableFileQuery = connectionPtr->query();
985

S
starlord 已提交
986 987 988 989
            createTableFileQuery << "INSERT INTO " << META_TABLEFILES << " "
                                 << "VALUES(" << id << ", " << mysqlpp::quote << table_id << ", " << engine_type << ", "
                                 << mysqlpp::quote << file_id << ", " << file_type << ", " << file_size << ", "
                                 << row_count << ", " << updated_time << ", " << created_on << ", " << date << ");";
990 991 992

            ENGINE_LOG_DEBUG << "MySQLMetaImpl::CreateTableFile: " << createTableFileQuery.str();

S
starlord 已提交
993
            if (mysqlpp::SimpleResult res = createTableFileQuery.execute()) {
S
starlord 已提交
994
                file_schema.id_ = res.insert_id();  // Might need to use SELECT LAST_INSERT_ID()?
995

S
starlord 已提交
996
                // Consume all results to avoid "Commands out of sync" error
997
            } else {
S
starlord 已提交
998
                return HandleException("QUERY ERROR WHEN CREATING TABLE FILE", createTableFileQuery.error());
999
            }
S
starlord 已提交
1000
        }  // Scoped Connection
1001

1002
        ENGINE_LOG_DEBUG << "Successfully create table file, file id = " << file_schema.file_id_;
1003
        return utils::CreateTableFilePath(options_, file_schema);
S
starlord 已提交
1004
    } catch (std::exception& e) {
S
starlord 已提交
1005
        return HandleException("GENERAL ERROR WHEN CREATING TABLE FILE", e.what());
1006 1007
    }
}
1008

S
starlord 已提交
1009
Status
S
starlord 已提交
1010
MySQLMetaImpl::FilesToIndex(TableFilesSchema& files) {
1011
    files.clear();
1012

1013
    try {
Y
Yu Kun 已提交
1014
        server::MetricCollector metric;
S
starlord 已提交
1015
        mysqlpp::StoreQueryResult res;
1016
        {
S
starlord 已提交
1017
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
1018 1019

            if (connectionPtr == nullptr) {
S
starlord 已提交
1020
                return Status(DB_ERROR, "Failed to connect to database server");
1021
            }
1022

S
starlord 已提交
1023 1024 1025
            mysqlpp::Query filesToIndexQuery = connectionPtr->query();
            filesToIndexQuery
                << "SELECT id, table_id, engine_type, file_id, file_type, file_size, row_count, date, created_on FROM "
S
starlord 已提交
1026 1027
                << META_TABLEFILES << " "
                << "WHERE file_type = " << std::to_string(TableFileSchema::TO_INDEX) << ";";
1028

1029
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::FilesToIndex: " << filesToIndexQuery.str();
1030

1031
            res = filesToIndexQuery.store();
S
starlord 已提交
1032
        }  // Scoped Connection
1033

S
starlord 已提交
1034
        Status ret;
1035 1036
        std::map<std::string, TableSchema> groups;
        TableFileSchema table_file;
S
starlord 已提交
1037 1038
        for (auto& resRow : res) {
            table_file.id_ = resRow["id"];  // implicit conversion
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051

            std::string table_id;
            resRow["table_id"].to_string(table_id);
            table_file.table_id_ = table_id;

            table_file.engine_type_ = resRow["engine_type"];

            std::string file_id;
            resRow["file_id"].to_string(file_id);
            table_file.file_id_ = file_id;

            table_file.file_type_ = resRow["file_type"];

S
starlord 已提交
1052 1053
            table_file.file_size_ = resRow["file_size"];

1054
            table_file.row_count_ = resRow["row_count"];
1055 1056 1057

            table_file.date_ = resRow["date"];

S
starlord 已提交
1058 1059
            table_file.created_on_ = resRow["created_on"];

1060 1061 1062 1063 1064 1065 1066
            auto groupItr = groups.find(table_file.table_id_);
            if (groupItr == groups.end()) {
                TableSchema table_schema;
                table_schema.table_id_ = table_file.table_id_;
                auto status = DescribeTable(table_schema);
                if (!status.ok()) {
                    return status;
1067
                }
1068
                groups[table_file.table_id_] = table_schema;
1069
            }
1070
            table_file.dimension_ = groups[table_file.table_id_].dimension_;
S
starlord 已提交
1071
            table_file.index_file_size_ = groups[table_file.table_id_].index_file_size_;
1072
            table_file.nlist_ = groups[table_file.table_id_].nlist_;
S
starlord 已提交
1073
            table_file.metric_type_ = groups[table_file.table_id_].metric_type_;
Z
update  
zhiru 已提交
1074

S
starlord 已提交
1075
            auto status = utils::GetTableFilePath(options_, table_file);
S
starlord 已提交
1076
            if (!status.ok()) {
S
starlord 已提交
1077
                ret = status;
S
starlord 已提交
1078
            }
1079 1080 1081

            files.push_back(table_file);
        }
S
starlord 已提交
1082

S
starlord 已提交
1083
        if (res.size() > 0) {
1084 1085
            ENGINE_LOG_DEBUG << "Collect " << res.size() << " to-index files";
        }
S
starlord 已提交
1086
        return ret;
S
starlord 已提交
1087
    } catch (std::exception& e) {
S
starlord 已提交
1088
        return HandleException("GENERAL ERROR WHEN FINDING TABLE FILES TO INDEX", e.what());
Z
update  
zhiru 已提交
1089
    }
1090 1091
}

S
starlord 已提交
1092
Status
1093
MySQLMetaImpl::FilesToSearch(const std::string& table_id, const std::vector<size_t>& ids, const DatesT& dates,
S
starlord 已提交
1094
                             DatePartionedTableFilesSchema& files) {
X
xj.lin 已提交
1095 1096 1097
    files.clear();

    try {
Y
Yu Kun 已提交
1098
        server::MetricCollector metric;
S
starlord 已提交
1099
        mysqlpp::StoreQueryResult res;
X
xj.lin 已提交
1100
        {
S
starlord 已提交
1101
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
X
xj.lin 已提交
1102 1103

            if (connectionPtr == nullptr) {
S
starlord 已提交
1104
                return Status(DB_ERROR, "Failed to connect to database server");
X
xj.lin 已提交
1105 1106
            }

S
starlord 已提交
1107 1108
            mysqlpp::Query filesToSearchQuery = connectionPtr->query();
            filesToSearchQuery
S
starlord 已提交
1109 1110 1111
                << "SELECT id, table_id, engine_type, file_id, file_type, file_size, row_count, date FROM "
                << META_TABLEFILES << " "
                << "WHERE table_id = " << mysqlpp::quote << table_id;
X
xj.lin 已提交
1112

1113
            if (!dates.empty()) {
X
xj.lin 已提交
1114
                std::stringstream partitionListSS;
1115
                for (auto& date : dates) {
X
xj.lin 已提交
1116 1117 1118 1119
                    partitionListSS << std::to_string(date) << ", ";
                }
                std::string partitionListStr = partitionListSS.str();

S
starlord 已提交
1120 1121 1122
                partitionListStr = partitionListStr.substr(0, partitionListStr.size() - 2);  // remove the last ", "
                filesToSearchQuery << " AND "
                                   << "date IN (" << partitionListStr << ")";
X
xj.lin 已提交
1123 1124 1125 1126
            }

            if (!ids.empty()) {
                std::stringstream idSS;
S
starlord 已提交
1127
                for (auto& id : ids) {
X
xj.lin 已提交
1128 1129 1130
                    idSS << "id = " << std::to_string(id) << " OR ";
                }
                std::string idStr = idSS.str();
S
starlord 已提交
1131
                idStr = idStr.substr(0, idStr.size() - 4);  // remove the last " OR "
X
xj.lin 已提交
1132

S
starlord 已提交
1133 1134
                filesToSearchQuery << " AND "
                                   << "(" << idStr << ")";
X
xj.lin 已提交
1135 1136
            }
            // End
S
starlord 已提交
1137 1138 1139 1140
            filesToSearchQuery << " AND "
                               << "(file_type = " << std::to_string(TableFileSchema::RAW) << " OR "
                               << "file_type = " << std::to_string(TableFileSchema::TO_INDEX) << " OR "
                               << "file_type = " << std::to_string(TableFileSchema::INDEX) << ");";
X
xj.lin 已提交
1141 1142 1143 1144

            ENGINE_LOG_DEBUG << "MySQLMetaImpl::FilesToSearch: " << filesToSearchQuery.str();

            res = filesToSearchQuery.store();
S
starlord 已提交
1145
        }  // Scoped Connection
X
xj.lin 已提交
1146 1147 1148 1149 1150 1151 1152 1153

        TableSchema table_schema;
        table_schema.table_id_ = table_id;
        auto status = DescribeTable(table_schema);
        if (!status.ok()) {
            return status;
        }

S
starlord 已提交
1154
        Status ret;
X
xj.lin 已提交
1155
        TableFileSchema table_file;
S
starlord 已提交
1156 1157
        for (auto& resRow : res) {
            table_file.id_ = resRow["id"];  // implicit conversion
X
xj.lin 已提交
1158 1159 1160 1161 1162

            std::string table_id_str;
            resRow["table_id"].to_string(table_id_str);
            table_file.table_id_ = table_id_str;

1163 1164
            table_file.index_file_size_ = table_schema.index_file_size_;

X
xj.lin 已提交
1165 1166
            table_file.engine_type_ = resRow["engine_type"];

S
starlord 已提交
1167 1168
            table_file.nlist_ = table_schema.nlist_;

S
starlord 已提交
1169 1170
            table_file.metric_type_ = table_schema.metric_type_;

X
xj.lin 已提交
1171 1172 1173 1174 1175 1176
            std::string file_id;
            resRow["file_id"].to_string(file_id);
            table_file.file_id_ = file_id;

            table_file.file_type_ = resRow["file_type"];

S
starlord 已提交
1177 1178
            table_file.file_size_ = resRow["file_size"];

1179
            table_file.row_count_ = resRow["row_count"];
X
xj.lin 已提交
1180 1181 1182 1183 1184

            table_file.date_ = resRow["date"];

            table_file.dimension_ = table_schema.dimension_;

S
starlord 已提交
1185
            auto status = utils::GetTableFilePath(options_, table_file);
S
starlord 已提交
1186
            if (!status.ok()) {
S
starlord 已提交
1187
                ret = status;
S
starlord 已提交
1188
            }
1189

1190 1191 1192
            auto dateItr = files.find(table_file.date_);
            if (dateItr == files.end()) {
                files[table_file.date_] = TableFilesSchema();
1193 1194
            }

1195
            files[table_file.date_].push_back(table_file);
1196
        }
S
starlord 已提交
1197

S
starlord 已提交
1198
        if (res.size() > 0) {
1199 1200
            ENGINE_LOG_DEBUG << "Collect " << res.size() << " to-search files";
        }
S
starlord 已提交
1201
        return ret;
S
starlord 已提交
1202
    } catch (std::exception& e) {
S
starlord 已提交
1203
        return HandleException("GENERAL ERROR WHEN FINDING TABLE FILES TO SEARCH", e.what());
Z
update  
zhiru 已提交
1204
    }
1205
}
Z
update  
zhiru 已提交
1206

S
starlord 已提交
1207
Status
S
starlord 已提交
1208
MySQLMetaImpl::FilesToMerge(const std::string& table_id, DatePartionedTableFilesSchema& files) {
1209
    files.clear();
Z
update  
zhiru 已提交
1210

1211
    try {
Y
Yu Kun 已提交
1212
        server::MetricCollector metric;
S
starlord 已提交
1213

S
starlord 已提交
1214
        // check table existence
S
starlord 已提交
1215 1216 1217 1218 1219 1220 1221
        TableSchema table_schema;
        table_schema.table_id_ = table_id;
        auto status = DescribeTable(table_schema);
        if (!status.ok()) {
            return status;
        }

S
starlord 已提交
1222
        mysqlpp::StoreQueryResult res;
1223
        {
S
starlord 已提交
1224
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
Z
update  
zhiru 已提交
1225

1226
            if (connectionPtr == nullptr) {
S
starlord 已提交
1227
                return Status(DB_ERROR, "Failed to connect to database server");
1228
            }
Z
update  
zhiru 已提交
1229

S
starlord 已提交
1230 1231 1232
            mysqlpp::Query filesToMergeQuery = connectionPtr->query();
            filesToMergeQuery
                << "SELECT id, table_id, file_id, file_type, file_size, row_count, date, engine_type, created_on FROM "
S
starlord 已提交
1233 1234 1235 1236 1237
                << META_TABLEFILES << " "
                << "WHERE table_id = " << mysqlpp::quote << table_id << " AND "
                << "file_type = " << std::to_string(TableFileSchema::RAW) << " "
                << "ORDER BY row_count DESC"
                << ";";
Z
update  
zhiru 已提交
1238

1239
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::FilesToMerge: " << filesToMergeQuery.str();
1240

1241
            res = filesToMergeQuery.store();
S
starlord 已提交
1242
        }  // Scoped Connection
1243

S
starlord 已提交
1244
        Status ret;
S
starlord 已提交
1245
        for (auto& resRow : res) {
S
starlord 已提交
1246 1247
            TableFileSchema table_file;
            table_file.file_size_ = resRow["file_size"];
S
starlord 已提交
1248
            if (table_file.file_size_ >= table_schema.index_file_size_) {
S
starlord 已提交
1249
                continue;  // skip large file
S
starlord 已提交
1250
            }
Z
update  
zhiru 已提交
1251

S
starlord 已提交
1252
            table_file.id_ = resRow["id"];  // implicit conversion
1253

1254 1255 1256
            std::string table_id_str;
            resRow["table_id"].to_string(table_id_str);
            table_file.table_id_ = table_id_str;
Z
update  
zhiru 已提交
1257

1258 1259 1260
            std::string file_id;
            resRow["file_id"].to_string(file_id);
            table_file.file_id_ = file_id;
1261

1262
            table_file.file_type_ = resRow["file_type"];
1263

S
starlord 已提交
1264 1265
            table_file.row_count_ = resRow["row_count"];

1266
            table_file.date_ = resRow["date"];
Z
update  
zhiru 已提交
1267

1268 1269
            table_file.index_file_size_ = table_schema.index_file_size_;

S
starlord 已提交
1270 1271
            table_file.engine_type_ = resRow["engine_type"];

S
starlord 已提交
1272 1273
            table_file.nlist_ = table_schema.nlist_;

S
starlord 已提交
1274 1275
            table_file.metric_type_ = table_schema.metric_type_;

S
starlord 已提交
1276 1277
            table_file.created_on_ = resRow["created_on"];

1278
            table_file.dimension_ = table_schema.dimension_;
Z
update  
zhiru 已提交
1279

S
starlord 已提交
1280
            auto status = utils::GetTableFilePath(options_, table_file);
S
starlord 已提交
1281
            if (!status.ok()) {
S
starlord 已提交
1282
                ret = status;
S
starlord 已提交
1283
            }
Z
update  
zhiru 已提交
1284

1285 1286 1287
            auto dateItr = files.find(table_file.date_);
            if (dateItr == files.end()) {
                files[table_file.date_] = TableFilesSchema();
1288
            }
1289 1290

            files[table_file.date_].push_back(table_file);
1291
        }
Z
update  
zhiru 已提交
1292

S
starlord 已提交
1293
        if (res.size() > 0) {
1294 1295
            ENGINE_LOG_DEBUG << "Collect " << res.size() << " to-merge files";
        }
S
starlord 已提交
1296
        return ret;
S
starlord 已提交
1297
    } catch (std::exception& e) {
S
starlord 已提交
1298
        return HandleException("GENERAL ERROR WHEN FINDING TABLE FILES TO MERGE", e.what());
1299 1300 1301
    }
}

S
starlord 已提交
1302
Status
S
starlord 已提交
1303 1304
MySQLMetaImpl::GetTableFiles(const std::string& table_id, const std::vector<size_t>& ids,
                             TableFilesSchema& table_files) {
1305
    if (ids.empty()) {
Z
update  
zhiru 已提交
1306 1307 1308
        return Status::OK();
    }

1309
    std::stringstream idSS;
S
starlord 已提交
1310
    for (auto& id : ids) {
1311 1312 1313
        idSS << "id = " << std::to_string(id) << " OR ";
    }
    std::string idStr = idSS.str();
S
starlord 已提交
1314
    idStr = idStr.substr(0, idStr.size() - 4);  // remove the last " OR "
Z
zhiru 已提交
1315

1316
    try {
S
starlord 已提交
1317
        mysqlpp::StoreQueryResult res;
1318
        {
S
starlord 已提交
1319
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
1320 1321

            if (connectionPtr == nullptr) {
S
starlord 已提交
1322
                return Status(DB_ERROR, "Failed to connect to database server");
1323 1324
            }

S
starlord 已提交
1325 1326
            mysqlpp::Query getTableFileQuery = connectionPtr->query();
            getTableFileQuery
S
starlord 已提交
1327 1328 1329 1330 1331
                << "SELECT id, engine_type, file_id, file_type, file_size, row_count, date, created_on FROM "
                << META_TABLEFILES << " "
                << "WHERE table_id = " << mysqlpp::quote << table_id << " AND "
                << "(" << idStr << ") AND "
                << "file_type <> " << std::to_string(TableFileSchema::TO_DELETE) << ";";
1332 1333 1334 1335

            ENGINE_LOG_DEBUG << "MySQLMetaImpl::GetTableFiles: " << getTableFileQuery.str();

            res = getTableFileQuery.store();
S
starlord 已提交
1336
        }  // Scoped Connection
1337 1338 1339

        TableSchema table_schema;
        table_schema.table_id_ = table_id;
S
starlord 已提交
1340
        DescribeTable(table_schema);
1341

S
starlord 已提交
1342
        Status ret;
S
starlord 已提交
1343
        for (auto& resRow : res) {
1344
            TableFileSchema file_schema;
1345

1346
            file_schema.id_ = resRow["id"];
Z
zhiru 已提交
1347

1348
            file_schema.table_id_ = table_id;
Z
update  
zhiru 已提交
1349

1350 1351
            file_schema.index_file_size_ = table_schema.index_file_size_;

1352
            file_schema.engine_type_ = resRow["engine_type"];
Z
update  
zhiru 已提交
1353

S
starlord 已提交
1354 1355
            file_schema.nlist_ = table_schema.nlist_;

S
starlord 已提交
1356 1357
            file_schema.metric_type_ = table_schema.metric_type_;

1358 1359 1360
            std::string file_id;
            resRow["file_id"].to_string(file_id);
            file_schema.file_id_ = file_id;
Z
update  
zhiru 已提交
1361

1362
            file_schema.file_type_ = resRow["file_type"];
Z
update  
zhiru 已提交
1363

1364 1365 1366
            file_schema.file_size_ = resRow["file_size"];

            file_schema.row_count_ = resRow["row_count"];
1367

1368
            file_schema.date_ = resRow["date"];
1369

S
starlord 已提交
1370 1371
            file_schema.created_on_ = resRow["created_on"];

1372
            file_schema.dimension_ = table_schema.dimension_;
Z
update  
zhiru 已提交
1373

S
starlord 已提交
1374
            utils::GetTableFilePath(options_, file_schema);
1375 1376 1377

            table_files.emplace_back(file_schema);
        }
S
starlord 已提交
1378

1379
        ENGINE_LOG_DEBUG << "Get table files by id";
S
starlord 已提交
1380
        return ret;
S
starlord 已提交
1381
    } catch (std::exception& e) {
S
starlord 已提交
1382
        return HandleException("GENERAL ERROR WHEN RETRIEVING TABLE FILES", e.what());
Z
update  
zhiru 已提交
1383
    }
1384
}
Z
zhiru 已提交
1385

S
starlord 已提交
1386
// TODO(myh): Support swap to cloud storage
S
starlord 已提交
1387 1388
Status
MySQLMetaImpl::Archive() {
S
starlord 已提交
1389
    auto& criterias = options_.archive_conf_.GetCriterias();
1390 1391 1392 1393
    if (criterias.empty()) {
        return Status::OK();
    }

S
starlord 已提交
1394 1395 1396
    for (auto& kv : criterias) {
        auto& criteria = kv.first;
        auto& limit = kv.second;
1397
        if (criteria == engine::ARCHIVE_CONF_DAYS) {
1398
            size_t usecs = limit * D_SEC * US_PS;
S
starlord 已提交
1399
            int64_t now = utils::GetMicroSecTimeStamp();
1400 1401

            try {
S
starlord 已提交
1402
                mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
Z
zhiru 已提交
1403

Z
update  
zhiru 已提交
1404
                if (connectionPtr == nullptr) {
S
starlord 已提交
1405
                    return Status(DB_ERROR, "Failed to connect to database server");
Z
update  
zhiru 已提交
1406 1407
                }

S
starlord 已提交
1408
                mysqlpp::Query archiveQuery = connectionPtr->query();
S
starlord 已提交
1409 1410 1411 1412
                archiveQuery << "UPDATE " << META_TABLEFILES << " "
                             << "SET file_type = " << std::to_string(TableFileSchema::TO_DELETE) << " "
                             << "WHERE created_on < " << std::to_string(now - usecs) << " AND "
                             << "file_type <> " << std::to_string(TableFileSchema::TO_DELETE) << ";";
Z
update  
zhiru 已提交
1413

1414
                ENGINE_LOG_DEBUG << "MySQLMetaImpl::Archive: " << archiveQuery.str();
Z
update  
zhiru 已提交
1415

1416
                if (!archiveQuery.exec()) {
S
starlord 已提交
1417
                    return HandleException("QUERY ERROR DURING ARCHIVE", archiveQuery.error());
1418
                }
1419

1420
                ENGINE_LOG_DEBUG << "Archive old files";
S
starlord 已提交
1421
            } catch (std::exception& e) {
S
starlord 已提交
1422
                return HandleException("GENERAL ERROR WHEN DURING ARCHIVE", e.what());
Z
zhiru 已提交
1423
            }
1424
        }
1425
        if (criteria == engine::ARCHIVE_CONF_DISK) {
1426 1427
            uint64_t sum = 0;
            Size(sum);
Z
update  
zhiru 已提交
1428

1429 1430
            auto to_delete = (sum - limit * G);
            DiscardFiles(to_delete);
1431 1432

            ENGINE_LOG_DEBUG << "Archive files to free disk";
1433
        }
Z
update  
zhiru 已提交
1434 1435
    }

1436 1437
    return Status::OK();
}
Z
zhiru 已提交
1438

S
starlord 已提交
1439
Status
S
starlord 已提交
1440
MySQLMetaImpl::Size(uint64_t& result) {
1441
    result = 0;
1442

S
starlord 已提交
1443
    try {
S
starlord 已提交
1444
        mysqlpp::StoreQueryResult res;
1445
        {
S
starlord 已提交
1446
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
1447

1448
            if (connectionPtr == nullptr) {
S
starlord 已提交
1449
                return Status(DB_ERROR, "Failed to connect to database server");
1450
            }
Z
zhiru 已提交
1451

S
starlord 已提交
1452
            mysqlpp::Query getSizeQuery = connectionPtr->query();
S
starlord 已提交
1453 1454
            getSizeQuery << "SELECT IFNULL(SUM(file_size),0) AS sum FROM " << META_TABLEFILES << " "
                         << "WHERE file_type <> " << std::to_string(TableFileSchema::TO_DELETE) << ";";
Z
update  
zhiru 已提交
1455

1456
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::Size: " << getSizeQuery.str();
Z
update  
zhiru 已提交
1457

1458
            res = getSizeQuery.store();
S
starlord 已提交
1459
        }  // Scoped Connection
Z
update  
zhiru 已提交
1460

1461 1462 1463 1464 1465
        if (res.empty()) {
            result = 0;
        } else {
            result = res[0]["sum"];
        }
S
starlord 已提交
1466
    } catch (std::exception& e) {
S
starlord 已提交
1467
        return HandleException("GENERAL ERROR WHEN RETRIEVING SIZE", e.what());
1468
    }
1469

1470 1471
    return Status::OK();
}
1472

S
starlord 已提交
1473 1474
Status
MySQLMetaImpl::DiscardFiles(int64_t to_discard_size) {
1475 1476
    if (to_discard_size <= 0) {
        return Status::OK();
Z
update  
zhiru 已提交
1477
    }
1478
    ENGINE_LOG_DEBUG << "About to discard size=" << to_discard_size;
Z
update  
zhiru 已提交
1479

1480
    try {
Y
Yu Kun 已提交
1481
        server::MetricCollector metric;
1482 1483
        bool status;
        {
S
starlord 已提交
1484
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
1485

1486
            if (connectionPtr == nullptr) {
S
starlord 已提交
1487
                return Status(DB_ERROR, "Failed to connect to database server");
1488
            }
Z
zhiru 已提交
1489

S
starlord 已提交
1490
            mysqlpp::Query discardFilesQuery = connectionPtr->query();
S
starlord 已提交
1491 1492 1493 1494
            discardFilesQuery << "SELECT id, file_size FROM " << META_TABLEFILES << " "
                              << "WHERE file_type <> " << std::to_string(TableFileSchema::TO_DELETE) << " "
                              << "ORDER BY id ASC "
                              << "LIMIT 10;";
Z
update  
zhiru 已提交
1495

1496
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::DiscardFiles: " << discardFilesQuery.str();
1497

S
starlord 已提交
1498
            mysqlpp::StoreQueryResult res = discardFilesQuery.store();
1499 1500 1501
            if (res.num_rows() == 0) {
                return Status::OK();
            }
1502

1503 1504
            TableFileSchema table_file;
            std::stringstream idsToDiscardSS;
S
starlord 已提交
1505
            for (auto& resRow : res) {
1506 1507
                if (to_discard_size <= 0) {
                    break;
Z
update  
zhiru 已提交
1508
                }
1509
                table_file.id_ = resRow["id"];
1510
                table_file.file_size_ = resRow["file_size"];
1511 1512
                idsToDiscardSS << "id = " << std::to_string(table_file.id_) << " OR ";
                ENGINE_LOG_DEBUG << "Discard table_file.id=" << table_file.file_id_
1513 1514
                                 << " table_file.size=" << table_file.file_size_;
                to_discard_size -= table_file.file_size_;
1515
            }
Z
update  
zhiru 已提交
1516

1517
            std::string idsToDiscardStr = idsToDiscardSS.str();
S
starlord 已提交
1518
            idsToDiscardStr = idsToDiscardStr.substr(0, idsToDiscardStr.size() - 4);  // remove the last " OR "
1519

S
starlord 已提交
1520 1521 1522 1523
            discardFilesQuery << "UPDATE " << META_TABLEFILES << " "
                              << "SET file_type = " << std::to_string(TableFileSchema::TO_DELETE) << ", "
                              << "updated_time = " << std::to_string(utils::GetMicroSecTimeStamp()) << " "
                              << "WHERE " << idsToDiscardStr << ";";
1524

1525
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::DiscardFiles: " << discardFilesQuery.str();
Z
update  
zhiru 已提交
1526

1527 1528
            status = discardFilesQuery.exec();
            if (!status) {
S
starlord 已提交
1529
                return HandleException("QUERY ERROR WHEN DISCARDING FILES", discardFilesQuery.error());
1530
            }
S
starlord 已提交
1531
        }  // Scoped Connection
1532 1533

        return DiscardFiles(to_discard_size);
S
starlord 已提交
1534
    } catch (std::exception& e) {
S
starlord 已提交
1535
        return HandleException("GENERAL ERROR WHEN DISCARDING FILES", e.what());
P
peng.xu 已提交
1536
    }
1537
}
P
peng.xu 已提交
1538

S
starlord 已提交
1539
// ZR: this function assumes all fields in file_schema have value
S
starlord 已提交
1540
Status
S
starlord 已提交
1541
MySQLMetaImpl::UpdateTableFile(TableFileSchema& file_schema) {
1542
    file_schema.updated_time_ = utils::GetMicroSecTimeStamp();
1543

S
starlord 已提交
1544
    try {
Y
Yu Kun 已提交
1545
        server::MetricCollector metric;
1546
        {
S
starlord 已提交
1547
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
Z
update  
zhiru 已提交
1548

1549
            if (connectionPtr == nullptr) {
S
starlord 已提交
1550
                return Status(DB_ERROR, "Failed to connect to database server");
1551
            }
Z
update  
zhiru 已提交
1552

S
starlord 已提交
1553
            mysqlpp::Query updateTableFileQuery = connectionPtr->query();
Z
update  
zhiru 已提交
1554

S
starlord 已提交
1555 1556 1557 1558
            // if the table has been deleted, just mark the table file as TO_DELETE
            // clean thread will delete the file later
            updateTableFileQuery << "SELECT state FROM " << META_TABLES << " "
                                 << "WHERE table_id = " << mysqlpp::quote << file_schema.table_id_ << ";";
Z
update  
zhiru 已提交
1559

1560
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::UpdateTableFile: " << updateTableFileQuery.str();
Z
update  
zhiru 已提交
1561

S
starlord 已提交
1562
            mysqlpp::StoreQueryResult res = updateTableFileQuery.store();
1563

1564 1565 1566 1567
            if (res.num_rows() == 1) {
                int state = res[0]["state"];
                if (state == TableSchema::TO_DELETE) {
                    file_schema.file_type_ = TableFileSchema::TO_DELETE;
Z
update  
zhiru 已提交
1568
                }
1569 1570 1571 1572 1573 1574 1575 1576 1577
            } else {
                file_schema.file_type_ = TableFileSchema::TO_DELETE;
            }

            std::string id = std::to_string(file_schema.id_);
            std::string table_id = file_schema.table_id_;
            std::string engine_type = std::to_string(file_schema.engine_type_);
            std::string file_id = file_schema.file_id_;
            std::string file_type = std::to_string(file_schema.file_type_);
1578 1579
            std::string file_size = std::to_string(file_schema.file_size_);
            std::string row_count = std::to_string(file_schema.row_count_);
1580 1581 1582
            std::string updated_time = std::to_string(file_schema.updated_time_);
            std::string created_on = std::to_string(file_schema.created_on_);
            std::string date = std::to_string(file_schema.date_);
Z
update  
zhiru 已提交
1583

S
starlord 已提交
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
            updateTableFileQuery << "UPDATE " << META_TABLEFILES << " "
                                 << "SET table_id = " << mysqlpp::quote << table_id << ", "
                                 << "engine_type = " << engine_type << ", "
                                 << "file_id = " << mysqlpp::quote << file_id << ", "
                                 << "file_type = " << file_type << ", "
                                 << "file_size = " << file_size << ", "
                                 << "row_count = " << row_count << ", "
                                 << "updated_time = " << updated_time << ", "
                                 << "created_on = " << created_on << ", "
                                 << "date = " << date << " "
                                 << "WHERE id = " << id << ";";
1595 1596 1597 1598 1599

            ENGINE_LOG_DEBUG << "MySQLMetaImpl::UpdateTableFile: " << updateTableFileQuery.str();

            if (!updateTableFileQuery.exec()) {
                ENGINE_LOG_DEBUG << "table_id= " << file_schema.table_id_ << " file_id=" << file_schema.file_id_;
S
starlord 已提交
1600
                return HandleException("QUERY ERROR WHEN UPDATING TABLE FILE", updateTableFileQuery.error());
1601
            }
S
starlord 已提交
1602
        }  // Scoped Connection
1603

1604
        ENGINE_LOG_DEBUG << "Update single table file, file id = " << file_schema.file_id_;
S
starlord 已提交
1605
    } catch (std::exception& e) {
S
starlord 已提交
1606
        return HandleException("GENERAL ERROR WHEN UPDATING TABLE FILE", e.what());
1607
    }
S
starlord 已提交
1608

1609 1610
    return Status::OK();
}
1611

S
starlord 已提交
1612
Status
S
starlord 已提交
1613
MySQLMetaImpl::UpdateTableFilesToIndex(const std::string& table_id) {
1614
    try {
S
starlord 已提交
1615
        mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
1616

1617
        if (connectionPtr == nullptr) {
S
starlord 已提交
1618
            return Status(DB_ERROR, "Failed to connect to database server");
1619
        }
Z
update  
zhiru 已提交
1620

S
starlord 已提交
1621
        mysqlpp::Query updateTableFilesToIndexQuery = connectionPtr->query();
Z
zhiru 已提交
1622

S
starlord 已提交
1623 1624 1625 1626
        updateTableFilesToIndexQuery << "UPDATE " << META_TABLEFILES << " "
                                     << "SET file_type = " << std::to_string(TableFileSchema::TO_INDEX) << " "
                                     << "WHERE table_id = " << mysqlpp::quote << table_id << " AND "
                                     << "file_type = " << std::to_string(TableFileSchema::RAW) << ";";
1627

Z
zhiru 已提交
1628
        ENGINE_LOG_DEBUG << "MySQLMetaImpl::UpdateTableFilesToIndex: " << updateTableFilesToIndexQuery.str();
Z
update  
zhiru 已提交
1629

Z
fix  
zhiru 已提交
1630
        if (!updateTableFilesToIndexQuery.exec()) {
S
starlord 已提交
1631 1632
            return HandleException("QUERY ERROR WHEN UPDATING TABLE FILE TO INDEX",
                                   updateTableFilesToIndexQuery.error());
Z
fix  
zhiru 已提交
1633 1634
        }

1635
        ENGINE_LOG_DEBUG << "Update files to to_index, table id = " << table_id;
S
starlord 已提交
1636
    } catch (std::exception& e) {
S
starlord 已提交
1637
        return HandleException("GENERAL ERROR WHEN UPDATING TABLE FILES TO INDEX", e.what());
1638
    }
Z
update  
zhiru 已提交
1639

1640 1641
    return Status::OK();
}
Z
zhiru 已提交
1642

S
starlord 已提交
1643
Status
S
starlord 已提交
1644
MySQLMetaImpl::UpdateTableFiles(TableFilesSchema& files) {
1645
    try {
Y
Yu Kun 已提交
1646
        server::MetricCollector metric;
1647
        {
S
starlord 已提交
1648
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
1649 1650

            if (connectionPtr == nullptr) {
S
starlord 已提交
1651
                return Status(DB_ERROR, "Failed to connect to database server");
1652
            }
Z
update  
zhiru 已提交
1653

S
starlord 已提交
1654
            mysqlpp::Query updateTableFilesQuery = connectionPtr->query();
1655

1656
            std::map<std::string, bool> has_tables;
S
starlord 已提交
1657
            for (auto& file_schema : files) {
1658 1659 1660
                if (has_tables.find(file_schema.table_id_) != has_tables.end()) {
                    continue;
                }
1661

S
starlord 已提交
1662 1663 1664 1665 1666 1667
                updateTableFilesQuery << "SELECT EXISTS "
                                      << "(SELECT 1 FROM " << META_TABLES << " "
                                      << "WHERE table_id = " << mysqlpp::quote << file_schema.table_id_ << " "
                                      << "AND state <> " << std::to_string(TableSchema::TO_DELETE) << ") "
                                      << "AS " << mysqlpp::quote << "check"
                                      << ";";
1668

1669
                ENGINE_LOG_DEBUG << "MySQLMetaImpl::UpdateTableFiles: " << updateTableFilesQuery.str();
1670

S
starlord 已提交
1671
                mysqlpp::StoreQueryResult res = updateTableFilesQuery.store();
1672

1673 1674 1675
                int check = res[0]["check"];
                has_tables[file_schema.table_id_] = (check == 1);
            }
1676

S
starlord 已提交
1677
            for (auto& file_schema : files) {
1678 1679
                if (!has_tables[file_schema.table_id_]) {
                    file_schema.file_type_ = TableFileSchema::TO_DELETE;
1680
                }
1681
                file_schema.updated_time_ = utils::GetMicroSecTimeStamp();
1682

1683 1684 1685 1686 1687
                std::string id = std::to_string(file_schema.id_);
                std::string table_id = file_schema.table_id_;
                std::string engine_type = std::to_string(file_schema.engine_type_);
                std::string file_id = file_schema.file_id_;
                std::string file_type = std::to_string(file_schema.file_type_);
1688 1689
                std::string file_size = std::to_string(file_schema.file_size_);
                std::string row_count = std::to_string(file_schema.row_count_);
1690 1691 1692
                std::string updated_time = std::to_string(file_schema.updated_time_);
                std::string created_on = std::to_string(file_schema.created_on_);
                std::string date = std::to_string(file_schema.date_);
Z
fix  
zhiru 已提交
1693

S
starlord 已提交
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
                updateTableFilesQuery << "UPDATE " << META_TABLEFILES << " "
                                      << "SET table_id = " << mysqlpp::quote << table_id << ", "
                                      << "engine_type = " << engine_type << ", "
                                      << "file_id = " << mysqlpp::quote << file_id << ", "
                                      << "file_type = " << file_type << ", "
                                      << "file_size = " << file_size << ", "
                                      << "row_count = " << row_count << ", "
                                      << "updated_time = " << updated_time << ", "
                                      << "created_on = " << created_on << ", "
                                      << "date = " << date << " "
                                      << "WHERE id = " << id << ";";
1705 1706 1707 1708

                ENGINE_LOG_DEBUG << "MySQLMetaImpl::UpdateTableFiles: " << updateTableFilesQuery.str();

                if (!updateTableFilesQuery.exec()) {
S
starlord 已提交
1709
                    return HandleException("QUERY ERROR WHEN UPDATING TABLE FILES", updateTableFilesQuery.error());
1710 1711
                }
            }
S
starlord 已提交
1712
        }  // Scoped Connection
1713

1714
        ENGINE_LOG_DEBUG << "Update " << files.size() << " table files";
S
starlord 已提交
1715
    } catch (std::exception& e) {
S
starlord 已提交
1716
        return HandleException("GENERAL ERROR WHEN UPDATING TABLE FILES", e.what());
1717
    }
S
starlord 已提交
1718

1719 1720
    return Status::OK();
}
Z
fix  
zhiru 已提交
1721

S
starlord 已提交
1722 1723
Status
MySQLMetaImpl::CleanUpFilesWithTTL(uint16_t seconds) {
1724
    auto now = utils::GetMicroSecTimeStamp();
S
starlord 已提交
1725 1726
    std::set<std::string> table_ids;

S
starlord 已提交
1727
    // remove to_delete files
1728
    try {
Y
Yu Kun 已提交
1729
        server::MetricCollector metric;
1730

1731
        {
S
starlord 已提交
1732
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
Z
update  
zhiru 已提交
1733

1734
            if (connectionPtr == nullptr) {
S
starlord 已提交
1735
                return Status(DB_ERROR, "Failed to connect to database server");
1736
            }
Z
zhiru 已提交
1737

S
starlord 已提交
1738
            mysqlpp::Query cleanUpFilesWithTTLQuery = connectionPtr->query();
S
starlord 已提交
1739 1740 1741
            cleanUpFilesWithTTLQuery << "SELECT id, table_id, file_id, date FROM " << META_TABLEFILES << " "
                                     << "WHERE file_type = " << std::to_string(TableFileSchema::TO_DELETE) << " AND "
                                     << "updated_time < " << std::to_string(now - seconds * US_PS) << ";";
Z
update  
zhiru 已提交
1742

1743
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::CleanUpFilesWithTTL: " << cleanUpFilesWithTTLQuery.str();
Z
update  
zhiru 已提交
1744

S
starlord 已提交
1745
            mysqlpp::StoreQueryResult res = cleanUpFilesWithTTLQuery.store();
Z
update  
zhiru 已提交
1746

1747 1748
            TableFileSchema table_file;
            std::vector<std::string> idsToDelete;
1749

S
starlord 已提交
1750 1751
            for (auto& resRow : res) {
                table_file.id_ = resRow["id"];  // implicit conversion
1752

1753 1754 1755
                std::string table_id;
                resRow["table_id"].to_string(table_id);
                table_file.table_id_ = table_id;
Z
fix  
zhiru 已提交
1756

1757 1758 1759
                std::string file_id;
                resRow["file_id"].to_string(file_id);
                table_file.file_id_ = file_id;
Z
update  
zhiru 已提交
1760

1761
                table_file.date_ = resRow["date"];
Z
update  
zhiru 已提交
1762

1763 1764
                utils::DeleteTableFilePath(options_, table_file);

S
starlord 已提交
1765
                ENGINE_LOG_DEBUG << "Removing file id:" << table_file.id_ << " location:" << table_file.location_;
1766 1767

                idsToDelete.emplace_back(std::to_string(table_file.id_));
S
starlord 已提交
1768 1769

                table_ids.insert(table_file.table_id_);
1770 1771 1772 1773
            }

            if (!idsToDelete.empty()) {
                std::stringstream idsToDeleteSS;
S
starlord 已提交
1774
                for (auto& id : idsToDelete) {
1775
                    idsToDeleteSS << "id = " << id << " OR ";
1776
                }
1777

1778
                std::string idsToDeleteStr = idsToDeleteSS.str();
S
starlord 已提交
1779 1780 1781
                idsToDeleteStr = idsToDeleteStr.substr(0, idsToDeleteStr.size() - 4);  // remove the last " OR "
                cleanUpFilesWithTTLQuery << "DELETE FROM " << META_TABLEFILES << " "
                                         << "WHERE " << idsToDeleteStr << ";";
1782

1783 1784 1785
                ENGINE_LOG_DEBUG << "MySQLMetaImpl::CleanUpFilesWithTTL: " << cleanUpFilesWithTTLQuery.str();

                if (!cleanUpFilesWithTTLQuery.exec()) {
S
starlord 已提交
1786 1787
                    return HandleException("QUERY ERROR WHEN CLEANING UP FILES WITH TTL",
                                           cleanUpFilesWithTTLQuery.error());
1788 1789
                }
            }
1790

S
starlord 已提交
1791
            if (res.size() > 0) {
1792 1793
                ENGINE_LOG_DEBUG << "Clean " << res.size() << " files deleted in " << seconds << " seconds";
            }
S
starlord 已提交
1794 1795
        }  // Scoped Connection
    } catch (std::exception& e) {
S
starlord 已提交
1796
        return HandleException("GENERAL ERROR WHEN CLEANING UP FILES WITH TTL", e.what());
Z
update  
zhiru 已提交
1797
    }
1798

S
starlord 已提交
1799
    // remove to_delete tables
1800
    try {
Y
Yu Kun 已提交
1801
        server::MetricCollector metric;
1802 1803

        {
S
starlord 已提交
1804
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
Z
zhiru 已提交
1805

Z
update  
zhiru 已提交
1806
            if (connectionPtr == nullptr) {
S
starlord 已提交
1807
                return Status(DB_ERROR, "Failed to connect to database server");
Z
update  
zhiru 已提交
1808 1809
            }

S
starlord 已提交
1810
            mysqlpp::Query cleanUpFilesWithTTLQuery = connectionPtr->query();
S
starlord 已提交
1811 1812
            cleanUpFilesWithTTLQuery << "SELECT id, table_id FROM " << META_TABLES << " "
                                     << "WHERE state = " << std::to_string(TableSchema::TO_DELETE) << ";";
1813

1814 1815
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::CleanUpFilesWithTTL: " << cleanUpFilesWithTTLQuery.str();

S
starlord 已提交
1816
            mysqlpp::StoreQueryResult res = cleanUpFilesWithTTLQuery.store();
Z
update  
zhiru 已提交
1817

Z
update  
zhiru 已提交
1818
            if (!res.empty()) {
1819
                std::stringstream idsToDeleteSS;
S
starlord 已提交
1820
                for (auto& resRow : res) {
1821 1822 1823
                    size_t id = resRow["id"];
                    std::string table_id;
                    resRow["table_id"].to_string(table_id);
Z
update  
zhiru 已提交
1824

S
starlord 已提交
1825
                    utils::DeleteTablePath(options_, table_id, false);  // only delete empty folder
1826 1827

                    idsToDeleteSS << "id = " << std::to_string(id) << " OR ";
Z
update  
zhiru 已提交
1828
                }
1829
                std::string idsToDeleteStr = idsToDeleteSS.str();
S
starlord 已提交
1830 1831 1832
                idsToDeleteStr = idsToDeleteStr.substr(0, idsToDeleteStr.size() - 4);  // remove the last " OR "
                cleanUpFilesWithTTLQuery << "DELETE FROM " << META_TABLES << " "
                                         << "WHERE " << idsToDeleteStr << ";";
1833

1834
                ENGINE_LOG_DEBUG << "MySQLMetaImpl::CleanUpFilesWithTTL: " << cleanUpFilesWithTTLQuery.str();
Z
update  
zhiru 已提交
1835

1836
                if (!cleanUpFilesWithTTLQuery.exec()) {
S
starlord 已提交
1837 1838
                    return HandleException("QUERY ERROR WHEN CLEANING UP TABLES WITH TTL",
                                           cleanUpFilesWithTTLQuery.error());
1839 1840
                }
            }
1841

S
starlord 已提交
1842
            if (res.size() > 0) {
1843 1844
                ENGINE_LOG_DEBUG << "Remove " << res.size() << " tables from meta";
            }
S
starlord 已提交
1845 1846
        }  // Scoped Connection
    } catch (std::exception& e) {
S
starlord 已提交
1847
        return HandleException("GENERAL ERROR WHEN CLEANING UP TABLES WITH TTL", e.what());
Z
update  
zhiru 已提交
1848 1849
    }

S
starlord 已提交
1850 1851
    // remove deleted table folder
    // don't remove table folder until all its files has been deleted
S
starlord 已提交
1852
    try {
Y
Yu Kun 已提交
1853
        server::MetricCollector metric;
S
starlord 已提交
1854 1855

        {
S
starlord 已提交
1856
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
S
starlord 已提交
1857 1858

            if (connectionPtr == nullptr) {
S
starlord 已提交
1859
                return Status(DB_ERROR, "Failed to connect to database server");
S
starlord 已提交
1860 1861
            }

S
starlord 已提交
1862
            for (auto& table_id : table_ids) {
S
starlord 已提交
1863
                mysqlpp::Query cleanUpFilesWithTTLQuery = connectionPtr->query();
S
starlord 已提交
1864 1865
                cleanUpFilesWithTTLQuery << "SELECT file_id FROM " << META_TABLEFILES << " "
                                         << "WHERE table_id = " << mysqlpp::quote << table_id << ";";
S
starlord 已提交
1866 1867 1868

                ENGINE_LOG_DEBUG << "MySQLMetaImpl::CleanUpFilesWithTTL: " << cleanUpFilesWithTTLQuery.str();

S
starlord 已提交
1869
                mysqlpp::StoreQueryResult res = cleanUpFilesWithTTLQuery.store();
S
starlord 已提交
1870 1871 1872 1873 1874

                if (res.empty()) {
                    utils::DeleteTablePath(options_, table_id);
                }
            }
1875

S
starlord 已提交
1876
            if (table_ids.size() > 0) {
1877 1878
                ENGINE_LOG_DEBUG << "Remove " << table_ids.size() << " tables folder";
            }
S
starlord 已提交
1879
        }
S
starlord 已提交
1880
    } catch (std::exception& e) {
S
starlord 已提交
1881
        return HandleException("GENERAL ERROR WHEN CLEANING UP TABLES WITH TTL", e.what());
S
starlord 已提交
1882 1883
    }

1884 1885
    return Status::OK();
}
1886

S
starlord 已提交
1887 1888
Status
MySQLMetaImpl::CleanUp() {
1889
    try {
S
starlord 已提交
1890
        mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
1891

1892
        if (connectionPtr == nullptr) {
S
starlord 已提交
1893
            return Status(DB_ERROR, "Failed to connect to database server");
1894
        }
1895

S
starlord 已提交
1896
        mysqlpp::Query cleanUpQuery = connectionPtr->query();
S
starlord 已提交
1897 1898 1899 1900
        cleanUpQuery << "SELECT table_name "
                     << "FROM information_schema.tables "
                     << "WHERE table_schema = " << mysqlpp::quote << mysql_connection_pool_->getDB() << " "
                     << "AND table_name = " << mysqlpp::quote << META_TABLEFILES << ";";
Z
update  
zhiru 已提交
1901

1902
        ENGINE_LOG_DEBUG << "MySQLMetaImpl::CleanUp: " << cleanUpQuery.str();
Z
update  
zhiru 已提交
1903

S
starlord 已提交
1904
        mysqlpp::StoreQueryResult res = cleanUpQuery.store();
Z
update  
zhiru 已提交
1905

1906 1907
        if (!res.empty()) {
            ENGINE_LOG_DEBUG << "Remove table file type as NEW";
1908
            cleanUpQuery << "DELETE FROM " << META_TABLEFILES << " WHERE file_type IN ("
S
starlord 已提交
1909 1910
                         << std::to_string(TableFileSchema::NEW) << "," << std::to_string(TableFileSchema::NEW_MERGE)
                         << "," << std::to_string(TableFileSchema::NEW_INDEX) << ");";
1911

1912
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::CleanUp: " << cleanUpQuery.str();
1913

1914
            if (!cleanUpQuery.exec()) {
S
starlord 已提交
1915
                return HandleException("QUERY ERROR WHEN CLEANING UP FILES", cleanUpQuery.error());
Z
update  
zhiru 已提交
1916
            }
1917
        }
1918

S
starlord 已提交
1919
        if (res.size() > 0) {
1920 1921
            ENGINE_LOG_DEBUG << "Clean " << res.size() << " files";
        }
S
starlord 已提交
1922
    } catch (std::exception& e) {
S
starlord 已提交
1923
        return HandleException("GENERAL ERROR WHEN CLEANING UP FILES", e.what());
Z
update  
zhiru 已提交
1924 1925
    }

1926 1927 1928
    return Status::OK();
}

S
starlord 已提交
1929
Status
S
starlord 已提交
1930
MySQLMetaImpl::Count(const std::string& table_id, uint64_t& result) {
1931
    try {
Y
Yu Kun 已提交
1932
        server::MetricCollector metric;
1933 1934 1935 1936 1937 1938 1939

        TableSchema table_schema;
        table_schema.table_id_ = table_id;
        auto status = DescribeTable(table_schema);

        if (!status.ok()) {
            return status;
1940
        }
Z
zhiru 已提交
1941

S
starlord 已提交
1942
        mysqlpp::StoreQueryResult res;
1943
        {
S
starlord 已提交
1944
            mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
Z
zhiru 已提交
1945

Z
update  
zhiru 已提交
1946
            if (connectionPtr == nullptr) {
S
starlord 已提交
1947
                return Status(DB_ERROR, "Failed to connect to database server");
Z
update  
zhiru 已提交
1948 1949
            }

S
starlord 已提交
1950
            mysqlpp::Query countQuery = connectionPtr->query();
S
starlord 已提交
1951 1952 1953 1954 1955
            countQuery << "SELECT row_count FROM " << META_TABLEFILES << " "
                       << "WHERE table_id = " << mysqlpp::quote << table_id << " AND "
                       << "(file_type = " << std::to_string(TableFileSchema::RAW) << " OR "
                       << "file_type = " << std::to_string(TableFileSchema::TO_INDEX) << " OR "
                       << "file_type = " << std::to_string(TableFileSchema::INDEX) << ");";
Z
update  
zhiru 已提交
1956

1957
            ENGINE_LOG_DEBUG << "MySQLMetaImpl::Count: " << countQuery.str();
Z
update  
zhiru 已提交
1958

1959
            res = countQuery.store();
S
starlord 已提交
1960
        }  // Scoped Connection
1961 1962

        result = 0;
S
starlord 已提交
1963
        for (auto& resRow : res) {
S
starlord 已提交
1964
            size_t size = resRow["row_count"];
1965
            result += size;
1966
        }
S
starlord 已提交
1967
    } catch (std::exception& e) {
S
starlord 已提交
1968
        return HandleException("GENERAL ERROR WHEN RETRIEVING COUNT", e.what());
Z
update  
zhiru 已提交
1969
    }
S
starlord 已提交
1970

1971 1972 1973
    return Status::OK();
}

S
starlord 已提交
1974 1975
Status
MySQLMetaImpl::DropAll() {
1976
    try {
S
starlord 已提交
1977
        ENGINE_LOG_DEBUG << "Drop all mysql meta";
S
starlord 已提交
1978
        mysqlpp::ScopedConnection connectionPtr(*mysql_connection_pool_, safe_grab_);
1979 1980

        if (connectionPtr == nullptr) {
S
starlord 已提交
1981
            return Status(DB_ERROR, "Failed to connect to database server");
Z
zhiru 已提交
1982
        }
1983

S
starlord 已提交
1984
        mysqlpp::Query dropTableQuery = connectionPtr->query();
1985
        dropTableQuery << "DROP TABLE IF EXISTS " << TABLES_SCHEMA.name() << ", " << TABLEFILES_SCHEMA.name() << ";";
1986 1987 1988 1989 1990 1991

        ENGINE_LOG_DEBUG << "MySQLMetaImpl::DropAll: " << dropTableQuery.str();

        if (dropTableQuery.exec()) {
            return Status::OK();
        }
S
starlord 已提交
1992
        return HandleException("QUERY ERROR WHEN DROPPING ALL", dropTableQuery.error());
S
starlord 已提交
1993
    } catch (std::exception& e) {
S
starlord 已提交
1994
        return HandleException("GENERAL ERROR WHEN DROPPING ALL", e.what());
1995 1996 1997
    }
}

S
starlord 已提交
1998 1999 2000
}  // namespace meta
}  // namespace engine
}  // namespace milvus