DBImpl.cpp 5.3 KB
Newer Older
X
Xu Peng 已提交
1
#include <assert.h>
X
Xu Peng 已提交
2
#include <chrono>
X
Xu Peng 已提交
3
#include <thread>
4
#include <iostream>
5 6 7
#include <faiss/IndexFlat.h>
#include <faiss/MetaIndexes.h>
#include <faiss/index_io.h>
X
Xu Peng 已提交
8 9 10
#include "DBImpl.h"
#include "DBMetaImpl.h"
#include "Env.h"
X
Xu Peng 已提交
11

X
Xu Peng 已提交
12 13 14
namespace zilliz {
namespace vecwise {
namespace engine {
X
Xu Peng 已提交
15

X
Xu Peng 已提交
16
DBImpl::DBImpl(const Options& options_, const std::string& name_)
X
Xu Peng 已提交
17 18
    : _dbname(name_),
      _env(options_.env),
X
Xu Peng 已提交
19
      _options(options_),
20
      _bg_compaction_scheduled(false),
X
Xu Peng 已提交
21
      _shutting_down(false),
22
      _pMeta(new meta::DBMetaImpl(*(_options.pMetaOptions))),
23
      _pMemMgr(new MemManager(_pMeta)) {
X
Xu Peng 已提交
24
    start_timer_task(options_.memory_sync_interval);
X
Xu Peng 已提交
25 26
}

X
Xu Peng 已提交
27 28
Status DBImpl::add_group(const GroupOptions& options,
        const std::string& group_id,
29
        meta::GroupSchema& group_info) {
X
Xu Peng 已提交
30 31
    assert((!options.has_id) ||
            (options.has_id && ("" != group_id)));
X
Xu Peng 已提交
32

X
Xu Peng 已提交
33
    return _pMeta->add_group(options, group_id, group_info);
34 35
}

36
Status DBImpl::get_group(const std::string& group_id_, meta::GroupSchema& group_info_) {
37 38 39 40 41 42 43
    return _pMeta->get_group(group_id_, group_info_);
}

Status DBImpl::has_group(const std::string& group_id_, bool& has_or_not_) {
    return _pMeta->has_group(group_id_, has_or_not_);
}

X
Xu Peng 已提交
44 45
Status DBImpl::get_group_files(const std::string& group_id,
                               const int date_delta,
46
                               meta::GroupFilesSchema& group_files_info) {
X
Xu Peng 已提交
47
    return _pMeta->get_group_files(group_id, date_delta, group_files_info);
48

X
Xu Peng 已提交
49 50
}

51 52
Status DBImpl::add_vectors(const std::string& group_id_,
        size_t n, const float* vectors, IDNumbers& vector_ids_) {
X
Xu Peng 已提交
53 54 55 56 57 58
    Status status = _pMemMgr->add_vectors(group_id_, n, vectors, vector_ids_);
    if (!status.ok()) {
        return status;
    }
}

X
Xu Peng 已提交
59 60 61 62 63 64
Status DBImpl::search(const std::string& group_id, size_t k, size_t nq,
        const float* vectors, QueryResults& results) {
    // PXU TODO
    return Status::OK();
}

X
Xu Peng 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
void DBImpl::start_timer_task(int interval_) {
    std::thread bg_task(&DBImpl::background_timer_task, this, interval_);
    bg_task.detach();
}

void DBImpl::background_timer_task(int interval_) {
    Status status;
    while (true) {
        if (!_bg_error.ok()) break;
        if (_shutting_down.load(std::memory_order_acquire)) break;

        std::this_thread::sleep_for(std::chrono::seconds(interval_));

        try_schedule_compaction();
    }
80 81
}

X
Xu Peng 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
void DBImpl::try_schedule_compaction() {
    if (_bg_compaction_scheduled) return;
    if (!_bg_error.ok()) return;

    _bg_compaction_scheduled = true;
    _env->schedule(&DBImpl::BGWork, this);
}

void DBImpl::BGWork(void* db_) {
    reinterpret_cast<DBImpl*>(db_)->background_call();
}

void DBImpl::background_call() {
    std::lock_guard<std::mutex> lock(_mutex);
    assert(_bg_compaction_scheduled);

    if (!_bg_error.ok()) return;

    background_compaction();
X
Xu Peng 已提交
101 102 103

    _bg_compaction_scheduled = false;
    _bg_work_finish_signal.notify_all();
X
Xu Peng 已提交
104 105
}

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

Status DBImpl::merge_files(const std::string& group_id, const meta::DateT& date,
        const meta::GroupFilesSchema& files) {
    meta::GroupFileSchema group_file;
    Status status = _pMeta->add_group_file(group_id, date, group_file);
    if (!status.ok()) {
        return status;
    }

    faiss::IndexFlat innerIndex(group_file.dimension);
    faiss::IndexIDMap index(&innerIndex);

    meta::GroupFilesSchema updated;

    for (auto& file : files) {
        auto file_index = dynamic_cast<faiss::IndexIDMap*>(faiss::read_index(file.location.c_str()));
        index.add_with_ids(file_index->ntotal, dynamic_cast<faiss::IndexFlat*>(file_index->index)->xb.data(),
                file_index->id_map.data());
        auto file_schema = file;
        file_schema.file_type = meta::GroupFileSchema::TO_DELETE;
        updated.push_back(file_schema);
    }

    faiss::write_index(&index, group_file.location.c_str());
    group_file.file_type = meta::GroupFileSchema::RAW;
    updated.push_back(group_file);
    status = _pMeta->update_files(updated);

    return status;
}

Status DBImpl::background_merge_files(const std::string& group_id) {
    meta::DatePartionedGroupFilesSchema raw_files;
X
Xu Peng 已提交
139 140 141 142
    auto status = _pMeta->files_to_merge(group_id, raw_files);
    if (!status.ok()) {
        return status;
    }
143 144 145 146 147 148

    if (raw_files.size() == 0) {
        return Status::OK();
    }

    for (auto& kv : raw_files) {
X
Xu Peng 已提交
149 150 151 152
        auto files = kv.second;
        if (files.size() <= _options.raw_file_merge_trigger_number) {
            continue;
        }
153 154
        merge_files(group_id, kv.first, kv.second);
    }
X
Xu Peng 已提交
155
    return Status::OK();
156 157
}

X
Xu Peng 已提交
158
void DBImpl::background_compaction() {
159 160
    std::vector<std::string> group_ids;
    _pMemMgr->serialize(group_ids);
161

X
Xu Peng 已提交
162 163 164 165 166 167 168 169
    Status status;
    for (auto group_id : group_ids) {
        /* std::cout << __func__ << " group_id=" << group_id << std::endl; */
        status = background_merge_files(group_id);
        if (!status.ok()) {
            _bg_error = status;
            return;
        }
170
    }
X
Xu Peng 已提交
171 172
}

X
Xu Peng 已提交
173
DBImpl::~DBImpl() {
X
Xu Peng 已提交
174
    std::unique_lock<std::mutex> lock(_mutex);
X
Xu Peng 已提交
175 176
    _shutting_down.store(true, std::memory_order_release);
    while (_bg_compaction_scheduled) {
X
Xu Peng 已提交
177
        _bg_work_finish_signal.wait(lock);
X
Xu Peng 已提交
178
    }
X
Xu Peng 已提交
179 180
}

X
Xu Peng 已提交
181 182 183 184 185 186 187 188 189 190 191
/*
 *  DB
 */

DB::~DB() {}

DB* DB::Open(const Options& options_, const std::string& name_) {
    DBImpl* impl = new DBImpl(options_, name_);
    return impl;
}

X
Xu Peng 已提交
192 193 194
} // namespace engine
} // namespace vecwise
} // namespace zilliz