DBImpl.cpp 11.1 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
#include <faiss/AutoTune.h>
X
Xu Peng 已提交
9
#include <wrapper/IndexBuilder.h>
X
xj.lin 已提交
10 11
#include <cstring>
#include <wrapper/Topk.h>
12
#include <easylogging++.h>
X
Xu Peng 已提交
13 14 15
#include "DBImpl.h"
#include "DBMetaImpl.h"
#include "Env.h"
X
Xu Peng 已提交
16

X
Xu Peng 已提交
17 18 19
namespace zilliz {
namespace vecwise {
namespace engine {
X
Xu Peng 已提交
20

X
Xu Peng 已提交
21 22 23
DBImpl::DBImpl(const Options& options)
    : _env(options.env),
      _options(options),
24
      _bg_compaction_scheduled(false),
X
Xu Peng 已提交
25
      _shutting_down(false),
X
Xu Peng 已提交
26
      bg_build_index_started_(false),
X
Xu Peng 已提交
27
      _pMeta(new meta::DBMetaImpl(_options.meta)),
X
Xu Peng 已提交
28
      _pMemMgr(new MemManager(_pMeta, _options)) {
X
Xu Peng 已提交
29
    start_timer_task(_options.memory_sync_interval);
X
Xu Peng 已提交
30 31
}

32 33
Status DBImpl::add_group(meta::GroupSchema& group_info) {
    return _pMeta->add_group(group_info);
34 35
}

36 37
Status DBImpl::get_group(meta::GroupSchema& group_info) {
    return _pMeta->get_group(group_info);
38 39 40 41 42 43
}

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
xj.lin 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
Status DBImpl::search(const std::string &group_id, size_t k, size_t nq,
                      const float *vectors, QueryResults &results) {
    meta::DatePartionedGroupFilesSchema files;
    std::vector<meta::DateT> partition;
    auto status = _pMeta->files_to_search(group_id, partition, files);
    if (!status.ok()) { return status; }

    // TODO: optimized
    meta::GroupFilesSchema index_files;
    meta::GroupFilesSchema raw_files;
    for (auto &day_files : files) {
        for (auto &file : day_files.second) {
            file.file_type == meta::GroupFileSchema::RAW ?
            raw_files.push_back(file) :
            index_files.push_back(file);
        }
    }
    int dim = raw_files[0].dimension;


    // merge raw files
    faiss::Index *index(faiss::index_factory(dim, "IDMap,Flat"));

    for (auto &file : raw_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());
    }
    float *xb = dynamic_cast<faiss::IndexFlat *>(index)->xb.data();
    int64_t *ids = dynamic_cast<faiss::IndexIDMap *>(index)->id_map.data();
    long totoal = index->ntotal;

    std::vector<float> distence;
    std::vector<long> result_ids;
    {
        // allocate memory
        float *output_distence;
        long *output_ids;
        output_distence = (float *) malloc(k * sizeof(float));
        output_ids = (long *) malloc(k * sizeof(long));

        // build and search in raw file
        // TODO: HardCode
        auto opd = std::make_shared<Operand>();
        opd->index_type = "IDMap,Flat";
        IndexBuilderPtr builder = GetIndexBuilder(opd);
        auto index = builder->build_all(totoal, xb, ids);

        index->search(nq, vectors, k, output_distence, output_ids);
        distence.insert(distence.begin(), output_distence, output_distence + k);
        result_ids.insert(result_ids.begin(), output_ids, output_ids + k);
        memset(output_distence, 0, k * sizeof(float));
        memset(output_ids, 0, k * sizeof(long));

        // search in index file
        for (auto &file : index_files) {
            auto index = read_index(file.location.c_str());
            index->search(nq, vectors, k, output_distence, output_ids);
            distence.insert(distence.begin(), output_distence, output_distence + k);
            result_ids.insert(result_ids.begin(), output_ids, output_ids + k);
            memset(output_distence, 0, k * sizeof(float));
            memset(output_ids, 0, k * sizeof(long));
        }

        // TopK
        TopK(distence.data(), distence.size(), k, output_distence, output_ids);
        distence.clear();
        result_ids.clear();
        distence.insert(distence.begin(), output_distence, output_distence + k);
        result_ids.insert(result_ids.begin(), output_ids, output_ids + k);

        // free
        free(output_distence);
        free(output_ids);
    }

X
Xu Peng 已提交
135 136 137
    return Status::OK();
}

X
Xu Peng 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
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();
    }
153 154
}

X
Xu Peng 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
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 已提交
174 175 176

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

179 180 181 182

Status DBImpl::merge_files(const std::string& group_id, const meta::DateT& date,
        const meta::GroupFilesSchema& files) {
    meta::GroupFileSchema group_file;
X
Xu Peng 已提交
183 184 185
    group_file.group_id = group_id;
    group_file.date = date;
    Status status = _pMeta->add_group_file(group_file);
X
Xu Peng 已提交
186

187
    if (!status.ok()) {
188
        LOG(INFO) << status.ToString() << std::endl;
189 190 191
        return status;
    }

X
Xu Peng 已提交
192
    std::shared_ptr<faiss::Index> index(faiss::index_factory(group_file.dimension, "IDMap,Flat"));
193 194 195 196 197

    meta::GroupFilesSchema updated;

    for (auto& file : files) {
        auto file_index = dynamic_cast<faiss::IndexIDMap*>(faiss::read_index(file.location.c_str()));
X
Xu Peng 已提交
198
        index->add_with_ids(file_index->ntotal, dynamic_cast<faiss::IndexFlat*>(file_index->index)->xb.data(),
199 200 201 202
                file_index->id_map.data());
        auto file_schema = file;
        file_schema.file_type = meta::GroupFileSchema::TO_DELETE;
        updated.push_back(file_schema);
203 204
        LOG(DEBUG) << "About to merge file " << file_schema.file_id <<
            " of size=" << file_schema.rows;
205 206
    }

X
Xu Peng 已提交
207
    auto index_size = group_file.dimension * index->ntotal;
X
Xu Peng 已提交
208
    faiss::write_index(index.get(), group_file.location.c_str());
X
Xu Peng 已提交
209 210 211 212 213 214

    if (index_size >= _options.index_trigger_size) {
        group_file.file_type = meta::GroupFileSchema::TO_INDEX;
    } else {
        group_file.file_type = meta::GroupFileSchema::RAW;
    }
X
Xu Peng 已提交
215
    group_file.rows = index_size;
216 217
    updated.push_back(group_file);
    status = _pMeta->update_files(updated);
218 219
    LOG(DEBUG) << "New merged file " << group_file.file_id <<
        " of size=" << group_file.rows;
220 221 222 223 224 225

    return status;
}

Status DBImpl::background_merge_files(const std::string& group_id) {
    meta::DatePartionedGroupFilesSchema raw_files;
X
Xu Peng 已提交
226 227 228 229
    auto status = _pMeta->files_to_merge(group_id, raw_files);
    if (!status.ok()) {
        return status;
    }
230 231 232 233 234

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

X
Xu Peng 已提交
235 236
    bool has_merge = false;

237
    for (auto& kv : raw_files) {
X
Xu Peng 已提交
238
        auto files = kv.second;
X
Xu Peng 已提交
239
        if (files.size() <= _options.merge_trigger_number) {
X
Xu Peng 已提交
240 241
            continue;
        }
X
Xu Peng 已提交
242
        has_merge = true;
243 244
        merge_files(group_id, kv.first, kv.second);
    }
X
Xu Peng 已提交
245 246 247 248 249

    if (has_merge) {
        try_build_index();
    }

X
Xu Peng 已提交
250 251
    _pMeta->cleanup_ttl_files(1);

X
Xu Peng 已提交
252 253 254 255
    return Status::OK();
}

Status DBImpl::build_index(const meta::GroupFileSchema& file) {
X
Xu Peng 已提交
256
    meta::GroupFileSchema group_file;
X
Xu Peng 已提交
257 258 259
    group_file.group_id = file.group_id;
    group_file.date = file.date;
    Status status = _pMeta->add_group_file(group_file);
X
Xu Peng 已提交
260 261 262 263 264
    if (!status.ok()) {
        return status;
    }

    auto opd = std::make_shared<Operand>();
X
Xu Peng 已提交
265
    opd->d = file.dimension;
X
Xu Peng 已提交
266 267 268 269
    opd->index_type = "IDMap,Flat";
    IndexBuilderPtr pBuilder = GetIndexBuilder(opd);

    auto from_index = dynamic_cast<faiss::IndexIDMap*>(faiss::read_index(file.location.c_str()));
270
    LOG(DEBUG) << "Preparing build_index for file_id=" << file.file_id
X
Xu Peng 已提交
271
        << " with new index_file_id=" << group_file.file_id << std::endl;
X
Xu Peng 已提交
272 273 274
    auto index = pBuilder->build_all(from_index->ntotal,
            dynamic_cast<faiss::IndexFlat*>(from_index->index)->xb.data(),
            from_index->id_map.data());
275
    LOG(DEBUG) << "Ending build_index for file_id=" << file.file_id
X
Xu Peng 已提交
276
        << " with new index_file_id=" << group_file.file_id << std::endl;
X
Xu Peng 已提交
277
    /* std::cout << "raw size=" << from_index->ntotal << "   index size=" << index->ntotal << std::endl; */
X
Xu Peng 已提交
278 279
    write_index(index, group_file.location.c_str());
    group_file.file_type = meta::GroupFileSchema::INDEX;
X
Xu Peng 已提交
280
    group_file.rows = file.dimension * index->ntotal;
X
Xu Peng 已提交
281

X
Xu Peng 已提交
282 283
    auto to_remove = file;
    to_remove.file_type = meta::GroupFileSchema::TO_DELETE;
X
Xu Peng 已提交
284

X
Xu Peng 已提交
285 286
    meta::GroupFilesSchema update_files = {to_remove, group_file};
    _pMeta->update_files(update_files);
X
Xu Peng 已提交
287

X
Xu Peng 已提交
288 289 290
    return Status::OK();
}

X
Xu Peng 已提交
291
void DBImpl::background_build_index() {
X
Xu Peng 已提交
292
    std::lock_guard<std::mutex> lock(build_index_mutex_);
X
Xu Peng 已提交
293 294
    assert(bg_build_index_started_);
    meta::GroupFilesSchema to_index_files;
X
Xu Peng 已提交
295
    _pMeta->files_to_index(to_index_files);
X
Xu Peng 已提交
296 297 298 299 300
    Status status;
    for (auto& file : to_index_files) {
        status = build_index(file);
        if (!status.ok()) {
            _bg_error = status;
X
Xu Peng 已提交
301
            return;
X
Xu Peng 已提交
302 303 304 305
        }
    }

    bg_build_index_started_ = false;
X
Xu Peng 已提交
306
    bg_build_index_finish_signal_.notify_all();
X
Xu Peng 已提交
307 308 309 310 311 312 313
}

Status DBImpl::try_build_index() {
    if (bg_build_index_started_) return Status::OK();
    bg_build_index_started_ = true;
    std::thread build_index_task(&DBImpl::background_build_index, this);
    build_index_task.detach();
X
Xu Peng 已提交
314
    return Status::OK();
315 316
}

X
Xu Peng 已提交
317
void DBImpl::background_compaction() {
318 319
    std::vector<std::string> group_ids;
    _pMemMgr->serialize(group_ids);
320

X
Xu Peng 已提交
321 322 323 324 325 326 327
    Status status;
    for (auto group_id : group_ids) {
        status = background_merge_files(group_id);
        if (!status.ok()) {
            _bg_error = status;
            return;
        }
328
    }
X
Xu Peng 已提交
329 330
}

X
Xu Peng 已提交
331
DBImpl::~DBImpl() {
X
Xu Peng 已提交
332 333 334 335 336 337 338 339 340 341 342 343
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _shutting_down.store(true, std::memory_order_release);
        while (_bg_compaction_scheduled) {
            _bg_work_finish_signal.wait(lock);
        }
    }
    {
        std::unique_lock<std::mutex> lock(build_index_mutex_);
        while (bg_build_index_started_) {
            bg_build_index_finish_signal_.wait(lock);
        }
X
Xu Peng 已提交
344
    }
X
Xu Peng 已提交
345 346
    std::vector<std::string> ids;
    _pMemMgr->serialize(ids);
X
Xu Peng 已提交
347 348
}

X
Xu Peng 已提交
349 350 351 352 353 354
/*
 *  DB
 */

DB::~DB() {}

X
Xu Peng 已提交
355 356 357 358
void DB::Open(const Options& options, DB** dbptr) {
    *dbptr = nullptr;
    *dbptr = new DBImpl(options);
    return;
X
Xu Peng 已提交
359 360
}

X
Xu Peng 已提交
361 362 363
} // namespace engine
} // namespace vecwise
} // namespace zilliz