WalManager.h 2.5 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

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

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

namespace milvus {
namespace engine {

class WalManager {
 public:
G
groot 已提交
33
    WalManager();
G
groot 已提交
34

G
groot 已提交
35 36
    static WalManager&
    GetInstance();
37

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

    Status
    Stop();
43

G
groot 已提交
44 45
    Status
    RecordOperation(const WalOperationPtr& operation, const DBPtr& db);
G
groot 已提交
46

G
groot 已提交
47
    Status
G
groot 已提交
48 49 50 51
    OperationDone(const std::string& collection_name, idx_t op_id);

    Status
    Recovery(const DBPtr& db);
52 53

 private:
G
groot 已提交
54 55 56
    Status
    ReadMaxOpId();

G
groot 已提交
57 58
    Status
    RecordInsertOperation(const InsertEntityOperationPtr& operation, const DBPtr& db);
59

G
groot 已提交
60 61
    Status
    RecordDeleteOperation(const DeleteEntityOperationPtr& operation, const DBPtr& db);
62

G
groot 已提交
63 64
    Status
    SplitChunk(const DataChunkPtr& chunk, std::vector<DataChunkPtr>& chunks);
65

G
groot 已提交
66 67 68 69 70 71 72 73 74 75 76 77
    std::string
    ConstructFilePath(const std::string& collection_name, const std::string& file_name);

    void
    StartCleanupThread(const std::string& collection_name);

    void
    CleanupThread(std::string collection_name);

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

G
groot 已提交
78 79
 private:
    SafeIDGenerator id_gen_;
80

G
groot 已提交
81
    bool enable_ = false;
G
groot 已提交
82 83
    std::string wal_path_;
    int64_t insert_buffer_size_ = 0;
G
groot 已提交
84 85 86 87 88 89 90 91 92 93 94 95

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

    using MaxOpIdMap = std::unordered_map<std::string, id_t>;
    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_;
96 97 98 99
};

}  // namespace engine
}  // namespace milvus