WalManager.h 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
//
// Licensed 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.

#pragma once

C
Cai Yudong 已提交
14
#include "config/ServerConfig.h"
G
groot 已提交
15 16 17
#include "db/DB.h"
#include "db/IDGenerator.h"
#include "db/Types.h"
G
groot 已提交
18
#include "db/wal/WalFile.h"
G
groot 已提交
19 20
#include "db/wal/WalOperation.h"
#include "utils/Status.h"
G
groot 已提交
21
#include "utils/ThreadPool.h"
G
groot 已提交
22

G
groot 已提交
23 24
#include <list>
#include <mutex>
25
#include <string>
G
groot 已提交
26
#include <unordered_map>
27 28 29 30 31
#include <vector>

namespace milvus {
namespace engine {

G
groot 已提交
32 33 34
extern const char* WAL_MAX_OP_FILE_NAME;
extern const char* WAL_DEL_FILE_NAME;

35 36
class WalManager {
 public:
G
groot 已提交
37 38
    static WalManager&
    GetInstance();
39

G
groot 已提交
40 41 42 43 44
    Status
    Start(const DBOptions& options);

    Status
    Stop();
45

G
groot 已提交
46 47 48
    Status
    DropCollection(const std::string& collection_name);

G
groot 已提交
49 50
    Status
    RecordOperation(const WalOperationPtr& operation, const DBPtr& db);
G
groot 已提交
51

G
groot 已提交
52
    Status
G
groot 已提交
53 54 55 56
    OperationDone(const std::string& collection_name, idx_t op_id);

    Status
    Recovery(const DBPtr& db);
57 58

 private:
G
groot 已提交
59 60
    WalManager();

G
groot 已提交
61
    Status
G
groot 已提交
62
    Init();
G
groot 已提交
63

G
groot 已提交
64 65
    Status
    RecordInsertOperation(const InsertEntityOperationPtr& operation, const DBPtr& db);
66

G
groot 已提交
67 68
    Status
    RecordDeleteOperation(const DeleteEntityOperationPtr& operation, const DBPtr& db);
69

G
groot 已提交
70 71 72 73
    std::string
    ConstructFilePath(const std::string& collection_name, const std::string& file_name);

    void
G
groot 已提交
74 75 76 77
    AddCleanupTask(const std::string& collection_name);

    void
    TakeCleanupTask(std::string& collection_name);
G
groot 已提交
78 79

    void
G
groot 已提交
80 81 82 83 84 85 86
    StartCleanupThread();

    void
    WaitCleanupFinish();

    void
    CleanupThread();
G
groot 已提交
87 88 89 90

    Status
    PerformOperation(const WalOperationPtr& operation, const DBPtr& db);

G
groot 已提交
91 92
 private:
    SafeIDGenerator id_gen_;
93

G
groot 已提交
94
    bool enable_ = false;
G
groot 已提交
95 96
    std::string wal_path_;
    int64_t insert_buffer_size_ = 0;
G
groot 已提交
97 98 99 100 101

    using WalFileMap = std::unordered_map<std::string, WalFilePtr>;
    WalFileMap file_map_;  // mapping collection name to file
    std::mutex file_map_mutex_;

G
groot 已提交
102
    using MaxOpIdMap = std::unordered_map<std::string, idx_t>;
G
groot 已提交
103 104 105 106 107 108
    MaxOpIdMap max_op_id_map_;  // mapping collection name to max operation id
    std::mutex max_op_mutex_;

    ThreadPool cleanup_thread_pool_;
    std::mutex cleanup_thread_mutex_;
    std::list<std::future<void>> cleanup_thread_results_;
G
groot 已提交
109 110 111

    std::list<std::string> cleanup_tasks_;  // cleanup target collections
    std::mutex cleanup_task_mutex_;
112 113 114 115
};

}  // namespace engine
}  // namespace milvus