DBImpl.cpp 91.8 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// 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
J
jinhai 已提交
5
//
6 7 8 9 10
// 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.
J
jinhai 已提交
11

S
starlord 已提交
12
#include "db/DBImpl.h"
Z
Zhiru Zhu 已提交
13 14

#include <assert.h>
15
#include <fiu-local.h>
Z
Zhiru Zhu 已提交
16 17 18 19 20

#include <algorithm>
#include <boost/filesystem.hpp>
#include <chrono>
#include <cstring>
21
#include <functional>
Z
Zhiru Zhu 已提交
22
#include <iostream>
23
#include <limits>
24
#include <queue>
Z
Zhiru Zhu 已提交
25 26
#include <set>
#include <thread>
27
#include <unordered_map>
Z
Zhiru Zhu 已提交
28 29
#include <utility>

S
starlord 已提交
30
#include "Utils.h"
S
starlord 已提交
31 32
#include "cache/CpuCacheMgr.h"
#include "cache/GpuCacheMgr.h"
33
#include "db/IDGenerator.h"
S
starlord 已提交
34
#include "engine/EngineFactory.h"
35
#include "index/thirdparty/faiss/utils/distances.h"
S
starlord 已提交
36
#include "insert/MemMenagerFactory.h"
S
starlord 已提交
37
#include "meta/MetaConsts.h"
S
starlord 已提交
38 39
#include "meta/MetaFactory.h"
#include "meta/SqliteMetaImpl.h"
G
groot 已提交
40
#include "metrics/Metrics.h"
G
groot 已提交
41
#include "scheduler/Definition.h"
S
starlord 已提交
42
#include "scheduler/SchedInst.h"
Y
Yu Kun 已提交
43
#include "scheduler/job/BuildIndexJob.h"
S
starlord 已提交
44 45
#include "scheduler/job/DeleteJob.h"
#include "scheduler/job/SearchJob.h"
46 47 48
#include "segment/SegmentReader.h"
#include "segment/SegmentWriter.h"
#include "utils/Exception.h"
S
starlord 已提交
49
#include "utils/Log.h"
G
groot 已提交
50
#include "utils/StringHelpFunctions.h"
S
starlord 已提交
51
#include "utils/TimeRecorder.h"
52 53
#include "utils/ValidationUtil.h"
#include "wal/WalDefinations.h"
X
Xu Peng 已提交
54

55 56
#include "search/TaskInst.h"

J
jinhai 已提交
57
namespace milvus {
X
Xu Peng 已提交
58
namespace engine {
X
Xu Peng 已提交
59

G
groot 已提交
60
namespace {
G
groot 已提交
61 62
constexpr uint64_t BACKGROUND_METRIC_INTERVAL = 1;
constexpr uint64_t BACKGROUND_INDEX_INTERVAL = 1;
G
groot 已提交
63
constexpr uint64_t WAIT_BUILD_INDEX_INTERVAL = 5;
G
groot 已提交
64

G
groot 已提交
65
static const Status SHUTDOWN_ERROR = Status(DB_ERROR, "Milvus server is shutdown!");
G
groot 已提交
66

S
starlord 已提交
67
}  // namespace
G
groot 已提交
68

Y
Yu Kun 已提交
69
DBImpl::DBImpl(const DBOptions& options)
70
    : options_(options), initialized_(false), merge_thread_pool_(1, 1), index_thread_pool_(1, 1) {
S
starlord 已提交
71
    meta_ptr_ = MetaFactory::Build(options.meta_, options.mode_);
Z
zhiru 已提交
72
    mem_mgr_ = MemManagerFactory::Build(meta_ptr_, options_);
73 74 75 76 77 78 79 80 81 82

    if (options_.wal_enable_) {
        wal::MXLogConfiguration mxlog_config;
        mxlog_config.recovery_error_ignore = options_.recovery_error_ignore_;
        // 2 buffers in the WAL
        mxlog_config.buffer_size = options_.buffer_size_ / 2;
        mxlog_config.mxlog_path = options_.mxlog_path_;
        wal_mgr_ = std::make_shared<wal::WalManager>(mxlog_config);
    }

83 84
    SetIdentity("DBImpl");
    AddCacheInsertDataListener();
85
    AddUseBlasThresholdListener();
86

S
starlord 已提交
87 88 89 90 91 92 93
    Start();
}

DBImpl::~DBImpl() {
    Stop();
}

S
starlord 已提交
94
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
95
// external api
S
starlord 已提交
96
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
97 98
Status
DBImpl::Start() {
99
    if (initialized_.load(std::memory_order_acquire)) {
S
starlord 已提交
100 101 102
        return Status::OK();
    }

103
    // LOG_ENGINE_TRACE_ << "DB service start";
104
    initialized_.store(true, std::memory_order_release);
S
starlord 已提交
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
    // wal
    if (options_.wal_enable_) {
        auto error_code = DB_ERROR;
        if (wal_mgr_ != nullptr) {
            error_code = wal_mgr_->Init(meta_ptr_);
        }
        if (error_code != WAL_SUCCESS) {
            throw Exception(error_code, "Wal init error!");
        }

        // recovery
        while (1) {
            wal::MXLogRecord record;
            auto error_code = wal_mgr_->GetNextRecovery(record);
            if (error_code != WAL_SUCCESS) {
                throw Exception(error_code, "Wal recovery error!");
            }
            if (record.type == wal::MXLogType::None) {
                break;
            }

            ExecWalRecord(record);
        }

        // for distribute version, some nodes are read only
        if (options_.mode_ != DBOptions::MODE::CLUSTER_READONLY) {
G
groot 已提交
132 133
            // background wal thread
            bg_wal_thread_ = std::thread(&DBImpl::BackgroundWalThread, this);
134 135 136 137
        }
    } else {
        // for distribute version, some nodes are read only
        if (options_.mode_ != DBOptions::MODE::CLUSTER_READONLY) {
G
groot 已提交
138 139
            // background flush thread
            bg_flush_thread_ = std::thread(&DBImpl::BackgroundFlushThread, this);
140
        }
Z
update  
zhiru 已提交
141
    }
S
starlord 已提交
142

G
groot 已提交
143 144 145 146 147 148 149 150 151
    // for distribute version, some nodes are read only
    if (options_.mode_ != DBOptions::MODE::CLUSTER_READONLY) {
        // background build index thread
        bg_index_thread_ = std::thread(&DBImpl::BackgroundIndexThread, this);
    }

    // background metric thread
    bg_metric_thread_ = std::thread(&DBImpl::BackgroundMetricThread, this);

S
starlord 已提交
152 153 154
    return Status::OK();
}

S
starlord 已提交
155 156
Status
DBImpl::Stop() {
157
    if (!initialized_.load(std::memory_order_acquire)) {
S
starlord 已提交
158 159
        return Status::OK();
    }
160

161
    initialized_.store(false, std::memory_order_release);
S
starlord 已提交
162

163 164
    if (options_.mode_ != DBOptions::MODE::CLUSTER_READONLY) {
        if (options_.wal_enable_) {
G
groot 已提交
165 166
            // wait wal thread finish
            swn_wal_.Notify();
167 168
            bg_wal_thread_.join();
        } else {
G
groot 已提交
169
            // flush all without merge
170 171 172 173
            wal::MXLogRecord record;
            record.type = wal::MXLogType::Flush;
            ExecWalRecord(record);

G
groot 已提交
174 175 176
            // wait flush thread finish
            swn_flush_.Notify();
            bg_flush_thread_.join();
177
        }
S
starlord 已提交
178

179 180
        WaitMergeFileFinish();

G
groot 已提交
181 182 183
        swn_index_.Notify();
        bg_index_thread_.join();

184
        meta_ptr_->CleanUpShadowFiles();
S
starlord 已提交
185 186
    }

G
groot 已提交
187 188 189 190
    // wait metric thread exit
    swn_metric_.Notify();
    bg_metric_thread_.join();

191
    // LOG_ENGINE_TRACE_ << "DB service stop";
S
starlord 已提交
192
    return Status::OK();
X
Xu Peng 已提交
193 194
}

S
starlord 已提交
195 196
Status
DBImpl::DropAll() {
S
starlord 已提交
197 198 199
    return meta_ptr_->DropAll();
}

S
starlord 已提交
200
Status
201
DBImpl::CreateCollection(meta::CollectionSchema& collection_schema) {
202
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
203
        return SHUTDOWN_ERROR;
S
starlord 已提交
204 205
    }

206
    meta::CollectionSchema temp_schema = collection_schema;
S
starlord 已提交
207
    temp_schema.index_file_size_ *= ONE_MB;  // store as MB
208
    if (options_.wal_enable_) {
209
        temp_schema.flush_lsn_ = wal_mgr_->CreateCollection(collection_schema.collection_id_);
210 211
    }

212
    return meta_ptr_->CreateCollection(temp_schema);
213 214
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
Status
DBImpl::CreateHybridCollection(meta::CollectionSchema& collection_schema, meta::hybrid::FieldsSchema& fields_schema) {
    if (!initialized_.load(std::memory_order_acquire)) {
        return SHUTDOWN_ERROR;
    }

    meta::CollectionSchema temp_schema = collection_schema;
    if (options_.wal_enable_) {
        // TODO(yukun): wal_mgr_->CreateHybridCollection()
    }

    return meta_ptr_->CreateHybridCollection(temp_schema, fields_schema);
}

Status
DBImpl::DescribeHybridCollection(meta::CollectionSchema& collection_schema,
                                 milvus::engine::meta::hybrid::FieldsSchema& fields_schema) {
    if (!initialized_.load(std::memory_order_acquire)) {
        return SHUTDOWN_ERROR;
    }

    auto stat = meta_ptr_->DescribeHybridCollection(collection_schema, fields_schema);
    return stat;
}

S
starlord 已提交
240
Status
241
DBImpl::DropCollection(const std::string& collection_id) {
242
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
243
        return SHUTDOWN_ERROR;
S
starlord 已提交
244 245
    }

246
    if (options_.wal_enable_) {
247
        wal_mgr_->DropCollection(collection_id);
248 249
    }

250
    return DropCollectionRecursively(collection_id);
G
groot 已提交
251 252
}

S
starlord 已提交
253
Status
254
DBImpl::DescribeCollection(meta::CollectionSchema& collection_schema) {
255
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
256
        return SHUTDOWN_ERROR;
S
starlord 已提交
257 258
    }

259 260
    auto stat = meta_ptr_->DescribeCollection(collection_schema);
    collection_schema.index_file_size_ /= ONE_MB;  // return as MB
S
starlord 已提交
261
    return stat;
262 263
}

S
starlord 已提交
264
Status
265
DBImpl::HasCollection(const std::string& collection_id, bool& has_or_not) {
266
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
267
        return SHUTDOWN_ERROR;
S
starlord 已提交
268 269
    }

270
    return meta_ptr_->HasCollection(collection_id, has_or_not);
271 272
}

273
Status
274
DBImpl::HasNativeCollection(const std::string& collection_id, bool& has_or_not_) {
275 276 277 278
    if (!initialized_.load(std::memory_order_acquire)) {
        return SHUTDOWN_ERROR;
    }

279 280 281
    engine::meta::CollectionSchema collection_schema;
    collection_schema.collection_id_ = collection_id;
    auto status = DescribeCollection(collection_schema);
282 283 284 285
    if (!status.ok()) {
        has_or_not_ = false;
        return status;
    } else {
286
        if (!collection_schema.owner_collection_.empty()) {
287 288 289 290 291 292 293 294 295
            has_or_not_ = false;
            return Status(DB_NOT_FOUND, "");
        }

        has_or_not_ = true;
        return Status::OK();
    }
}

S
starlord 已提交
296
Status
297
DBImpl::AllCollections(std::vector<meta::CollectionSchema>& collection_schema_array) {
298
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
299
        return SHUTDOWN_ERROR;
S
starlord 已提交
300 301
    }

302 303
    std::vector<meta::CollectionSchema> all_collections;
    auto status = meta_ptr_->AllCollections(all_collections);
304

305 306 307 308 309
    // only return real collections, dont return partition collections
    collection_schema_array.clear();
    for (auto& schema : all_collections) {
        if (schema.owner_collection_.empty()) {
            collection_schema_array.push_back(schema);
310 311 312 313
        }
    }

    return status;
G
groot 已提交
314 315
}

316
Status
317
DBImpl::GetCollectionInfo(const std::string& collection_id, CollectionInfo& collection_info) {
318 319 320 321 322
    if (!initialized_.load(std::memory_order_acquire)) {
        return SHUTDOWN_ERROR;
    }

    // step1: get all partition ids
J
Jin Hai 已提交
323 324 325
    std::vector<std::pair<std::string, std::string>> name2tag = {{collection_id, milvus::engine::DEFAULT_PARTITON_TAG}};
    std::vector<meta::CollectionSchema> partition_array;
    auto status = meta_ptr_->ShowPartitions(collection_id, partition_array);
326
    for (auto& schema : partition_array) {
J
Jin Hai 已提交
327
        name2tag.push_back(std::make_pair(schema.collection_id_, schema.partition_tag_));
328 329
    }

J
Jin Hai 已提交
330 331 332
    // step2: get native collection info
    std::vector<int> file_types{meta::SegmentSchema::FILE_TYPE::RAW, meta::SegmentSchema::FILE_TYPE::TO_INDEX,
                                meta::SegmentSchema::FILE_TYPE::INDEX};
333 334 335 336 337 338

    static std::map<int32_t, std::string> index_type_name = {
        {(int32_t)engine::EngineType::FAISS_IDMAP, "IDMAP"},
        {(int32_t)engine::EngineType::FAISS_IVFFLAT, "IVFFLAT"},
        {(int32_t)engine::EngineType::FAISS_IVFSQ8, "IVFSQ8"},
        {(int32_t)engine::EngineType::NSG_MIX, "NSG"},
O
op-hunter 已提交
339
        {(int32_t)engine::EngineType::ANNOY, "ANNOY"},
340 341 342 343 344 345 346 347 348
        {(int32_t)engine::EngineType::FAISS_IVFSQ8H, "IVFSQ8H"},
        {(int32_t)engine::EngineType::FAISS_PQ, "PQ"},
        {(int32_t)engine::EngineType::SPTAG_KDT, "KDT"},
        {(int32_t)engine::EngineType::SPTAG_BKT, "BKT"},
        {(int32_t)engine::EngineType::FAISS_BIN_IDMAP, "IDMAP"},
        {(int32_t)engine::EngineType::FAISS_BIN_IVFFLAT, "IVFFLAT"},
    };

    for (auto& name_tag : name2tag) {
349 350
        meta::SegmentsSchema collection_files;
        status = meta_ptr_->FilesByType(name_tag.first, file_types, collection_files);
351
        if (!status.ok()) {
J
Jin Hai 已提交
352
            std::string err_msg = "Failed to get collection info: " + status.ToString();
353
            LOG_ENGINE_ERROR_ << err_msg;
354 355 356 357
            return Status(DB_ERROR, err_msg);
        }

        std::vector<SegmentStat> segments_stat;
358
        for (auto& file : collection_files) {
359 360 361 362 363 364 365 366 367
            SegmentStat seg_stat;
            seg_stat.name_ = file.segment_id_;
            seg_stat.row_count_ = (int64_t)file.row_count_;
            seg_stat.index_name_ = index_type_name[file.engine_type_];
            seg_stat.data_size_ = (int64_t)file.file_size_;
            segments_stat.emplace_back(seg_stat);
        }

        PartitionStat partition_stat;
J
Jin Hai 已提交
368
        if (name_tag.first == collection_id) {
369 370 371 372 373 374
            partition_stat.tag_ = milvus::engine::DEFAULT_PARTITON_TAG;
        } else {
            partition_stat.tag_ = name_tag.second;
        }

        partition_stat.segments_stat_.swap(segments_stat);
375
        collection_info.partitions_stat_.emplace_back(partition_stat);
376 377 378 379 380
    }

    return Status::OK();
}

S
starlord 已提交
381
Status
382
DBImpl::PreloadCollection(const std::string& collection_id) {
383
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
384
        return SHUTDOWN_ERROR;
S
starlord 已提交
385 386
    }

J
Jin Hai 已提交
387 388 389
    // step 1: get all collection files from parent collection
    meta::SegmentsSchema files_array;
    auto status = GetFilesToSearch(collection_id, files_array);
Y
Yu Kun 已提交
390 391 392
    if (!status.ok()) {
        return status;
    }
Y
Yu Kun 已提交
393

394
    // step 2: get files from partition collections
J
Jin Hai 已提交
395 396
    std::vector<meta::CollectionSchema> partition_array;
    status = meta_ptr_->ShowPartitions(collection_id, partition_array);
G
groot 已提交
397
    for (auto& schema : partition_array) {
J
Jin Hai 已提交
398
        status = GetFilesToSearch(schema.collection_id_, files_array);
G
groot 已提交
399 400
    }

Y
Yu Kun 已提交
401 402
    int64_t size = 0;
    int64_t cache_total = cache::CpuCacheMgr::GetInstance()->CacheCapacity();
Y
Yu Kun 已提交
403 404
    int64_t cache_usage = cache::CpuCacheMgr::GetInstance()->CacheUsage();
    int64_t available_size = cache_total - cache_usage;
Y
Yu Kun 已提交
405

406
    // step 3: load file one by one
407 408
    LOG_ENGINE_DEBUG_ << "Begin pre-load collection:" + collection_id + ", totally " << files_array.size()
                      << " files need to be pre-loaded";
J
Jin Hai 已提交
409
    TimeRecorderAuto rc("Pre-load collection:" + collection_id);
G
groot 已提交
410
    for (auto& file : files_array) {
411
        EngineType engine_type;
J
Jin Hai 已提交
412 413 414
        if (file.file_type_ == meta::SegmentSchema::FILE_TYPE::RAW ||
            file.file_type_ == meta::SegmentSchema::FILE_TYPE::TO_INDEX ||
            file.file_type_ == meta::SegmentSchema::FILE_TYPE::BACKUP) {
415 416
            engine_type =
                utils::IsBinaryMetricType(file.metric_type_) ? EngineType::FAISS_BIN_IDMAP : EngineType::FAISS_IDMAP;
417 418 419
        } else {
            engine_type = (EngineType)file.engine_type_;
        }
420 421 422 423

        auto json = milvus::json::parse(file.index_params_);
        ExecutionEnginePtr engine =
            EngineFactory::Build(file.dimension_, file.location_, engine_type, (MetricType)file.metric_type_, json);
424
        fiu_do_on("DBImpl.PreloadCollection.null_engine", engine = nullptr);
G
groot 已提交
425
        if (engine == nullptr) {
426
            LOG_ENGINE_ERROR_ << "Invalid engine type";
G
groot 已提交
427 428
            return Status(DB_ERROR, "Invalid engine type");
        }
Y
Yu Kun 已提交
429

430
        fiu_do_on("DBImpl.PreloadCollection.exceed_cache", size = available_size + 1);
431 432

        try {
433
            fiu_do_on("DBImpl.PreloadCollection.engine_throw_exception", throw std::exception());
434 435 436 437 438 439
            std::string msg = "Pre-loaded file: " + file.file_id_ + " size: " + std::to_string(file.file_size_);
            TimeRecorderAuto rc_1(msg);
            engine->Load(true);

            size += engine->Size();
            if (size > available_size) {
440
                LOG_ENGINE_DEBUG_ << "Pre-load cancelled since cache is almost full";
441
                return Status(SERVER_CACHE_FULL, "Cache is full");
Y
Yu Kun 已提交
442
            }
443
        } catch (std::exception& ex) {
J
Jin Hai 已提交
444
            std::string msg = "Pre-load collection encounter exception: " + std::string(ex.what());
445
            LOG_ENGINE_ERROR_ << msg;
446
            return Status(DB_ERROR, msg);
Y
Yu Kun 已提交
447 448
        }
    }
G
groot 已提交
449

Y
Yu Kun 已提交
450
    return Status::OK();
Y
Yu Kun 已提交
451 452
}

S
starlord 已提交
453
Status
454
DBImpl::UpdateCollectionFlag(const std::string& collection_id, int64_t flag) {
455
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
456
        return SHUTDOWN_ERROR;
S
starlord 已提交
457 458
    }

459
    return meta_ptr_->UpdateCollectionFlag(collection_id, flag);
S
starlord 已提交
460 461
}

S
starlord 已提交
462
Status
463
DBImpl::GetCollectionRowCount(const std::string& collection_id, uint64_t& row_count) {
464
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
465 466 467
        return SHUTDOWN_ERROR;
    }

468
    return GetCollectionRowCountRecursively(collection_id, row_count);
G
groot 已提交
469 470 471
}

Status
J
Jin Hai 已提交
472
DBImpl::CreatePartition(const std::string& collection_id, const std::string& partition_name,
G
groot 已提交
473
                        const std::string& partition_tag) {
474
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
475 476 477
        return SHUTDOWN_ERROR;
    }

478
    uint64_t lsn = 0;
479
    meta_ptr_->GetCollectionFlushLSN(collection_id, lsn);
J
Jin Hai 已提交
480
    return meta_ptr_->CreatePartition(collection_id, partition_name, partition_tag, lsn);
G
groot 已提交
481 482 483 484
}

Status
DBImpl::DropPartition(const std::string& partition_name) {
485
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
486
        return SHUTDOWN_ERROR;
S
starlord 已提交
487 488
    }

489
    mem_mgr_->EraseMemVector(partition_name);                // not allow insert
J
Jin Hai 已提交
490
    auto status = meta_ptr_->DropPartition(partition_name);  // soft delete collection
491
    if (!status.ok()) {
492
        LOG_ENGINE_ERROR_ << status.message();
493 494
        return status;
    }
G
groot 已提交
495

J
Jin Hai 已提交
496
    // scheduler will determine when to delete collection files
G
groot 已提交
497 498 499 500 501 502
    auto nres = scheduler::ResMgrInst::GetInstance()->GetNumOfComputeResource();
    scheduler::DeleteJobPtr job = std::make_shared<scheduler::DeleteJob>(partition_name, meta_ptr_, nres);
    scheduler::JobMgrInst::GetInstance()->Put(job);
    job->WaitAndDelete();

    return Status::OK();
G
groot 已提交
503 504
}

S
starlord 已提交
505
Status
J
Jin Hai 已提交
506
DBImpl::DropPartitionByTag(const std::string& collection_id, const std::string& partition_tag) {
507
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
508 509 510 511
        return SHUTDOWN_ERROR;
    }

    std::string partition_name;
J
Jin Hai 已提交
512
    auto status = meta_ptr_->GetPartitionName(collection_id, partition_tag, partition_name);
513
    if (!status.ok()) {
514
        LOG_ENGINE_ERROR_ << status.message();
515 516 517
        return status;
    }

G
groot 已提交
518 519 520 521
    return DropPartition(partition_name);
}

Status
J
Jin Hai 已提交
522
DBImpl::ShowPartitions(const std::string& collection_id, std::vector<meta::CollectionSchema>& partition_schema_array) {
523
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
524 525 526
        return SHUTDOWN_ERROR;
    }

J
Jin Hai 已提交
527
    return meta_ptr_->ShowPartitions(collection_id, partition_schema_array);
G
groot 已提交
528 529 530
}

Status
J
Jin Hai 已提交
531
DBImpl::InsertVectors(const std::string& collection_id, const std::string& partition_tag, VectorsData& vectors) {
532
    //    LOG_ENGINE_DEBUG_ << "Insert " << n << " vectors to cache";
533
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
534
        return SHUTDOWN_ERROR;
S
starlord 已提交
535
    }
Y
yu yunfeng 已提交
536

J
Jin Hai 已提交
537
    // insert vectors into target collection
538 539
    // (zhiru): generate ids
    if (vectors.id_array_.empty()) {
J
Jin Hai 已提交
540 541 542
        SafeIDGenerator& id_generator = SafeIDGenerator::GetInstance();
        Status status = id_generator.GetNextIDNumbers(vectors.vector_count_, vectors.id_array_);
        if (!status.ok()) {
543
            LOG_ENGINE_ERROR_ << LogOut("[%s][%ld] Get next id number fail: %s", "insert", 0, status.message().c_str());
J
Jin Hai 已提交
544 545
            return status;
        }
546 547
    }

548
    Status status;
549
    if (options_.wal_enable_) {
550 551
        std::string target_collection_name;
        status = GetPartitionByTag(collection_id, partition_tag, target_collection_name);
G
groot 已提交
552
        if (!status.ok()) {
553
            LOG_ENGINE_ERROR_ << LogOut("[%s][%ld] Get partition fail: %s", "insert", 0, status.message().c_str());
G
groot 已提交
554 555
            return status;
        }
556 557

        if (!vectors.float_data_.empty()) {
J
Jin Hai 已提交
558
            wal_mgr_->Insert(collection_id, partition_tag, vectors.id_array_, vectors.float_data_);
559
        } else if (!vectors.binary_data_.empty()) {
J
Jin Hai 已提交
560
            wal_mgr_->Insert(collection_id, partition_tag, vectors.id_array_, vectors.binary_data_);
561
        }
G
groot 已提交
562
        swn_wal_.Notify();
563 564 565
    } else {
        wal::MXLogRecord record;
        record.lsn = 0;  // need to get from meta ?
J
Jin Hai 已提交
566
        record.collection_id = collection_id;
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
        record.partition_tag = partition_tag;
        record.ids = vectors.id_array_.data();
        record.length = vectors.vector_count_;
        if (vectors.binary_data_.empty()) {
            record.type = wal::MXLogType::InsertVector;
            record.data = vectors.float_data_.data();
            record.data_size = vectors.float_data_.size() * sizeof(float);
        } else {
            record.type = wal::MXLogType::InsertBinary;
            record.ids = vectors.id_array_.data();
            record.length = vectors.vector_count_;
            record.data = vectors.binary_data_.data();
            record.data_size = vectors.binary_data_.size() * sizeof(uint8_t);
        }

        status = ExecWalRecord(record);
G
groot 已提交
583 584
    }

585 586 587
    return status;
}

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
Status
DBImpl::InsertEntities(const std::string& collection_id, const std::string& partition_tag, Entity& entity,
                       std::unordered_map<std::string, meta::hybrid::DataType>& attr_types) {
    if (!initialized_.load(std::memory_order_acquire)) {
        return SHUTDOWN_ERROR;
    }

    // Generate id
    if (entity.id_array_.empty()) {
        SafeIDGenerator& id_generator = SafeIDGenerator::GetInstance();
        Status status = id_generator.GetNextIDNumbers(entity.entity_count_, entity.id_array_);
        if (!status.ok()) {
            return status;
        }
    }

    Status status;
    // insert entities: collection_name is field id
    wal::MXLogRecord record;
    record.lsn = 0;
    record.collection_id = collection_id;
    record.partition_tag = partition_tag;
    record.ids = entity.id_array_.data();
    record.length = entity.entity_count_;

    auto vector_it = entity.vector_data_.begin();
    if (vector_it->second.binary_data_.empty()) {
        record.type = wal::MXLogType::Entity;
        record.data = vector_it->second.float_data_.data();
        record.data_size = vector_it->second.float_data_.size() * sizeof(float);
    } else {
        //        record.type = wal::MXLogType::InsertBinary;
        //        record.data = entities.vector_data_[0].binary_data_.data();
        //        record.length = entities.vector_data_[0].binary_data_.size() * sizeof(uint8_t);
    }

    auto attr_data_it = entity.attr_data_.begin();
    for (; attr_data_it != entity.attr_data_.end(); ++attr_data_it) {
        switch (attr_types.at(attr_data_it->first)) {
            case meta::hybrid::DataType::INT8: {
                std::vector<int8_t> entity_data;
                entity_data.resize(entity.entity_count_);
                for (uint64_t j = 0; j < entity.entity_count_; ++j) {
                    entity_data[j] = atoi(attr_data_it->second[j].c_str());
                }
                std::vector<uint8_t> data;
                data.resize(entity.entity_count_ * sizeof(int8_t));
                memcpy(data.data(), entity_data.data(), entity.entity_count_ * sizeof(int8_t));
                record.attr_data.insert(std::make_pair(attr_data_it->first, data));

                record.attr_nbytes.insert(std::make_pair(attr_data_it->first, sizeof(int8_t)));
                record.attr_data_size.insert(
                    std::make_pair(attr_data_it->first, entity.entity_count_ * sizeof(int8_t)));
                break;
            }
            case meta::hybrid::DataType::INT16: {
                std::vector<int16_t> entity_data;
                entity_data.resize(entity.entity_count_);
                for (uint64_t j = 0; j < entity.entity_count_; ++j) {
                    entity_data[j] = atoi(attr_data_it->second[j].c_str());
                }
                std::vector<uint8_t> data;
                data.resize(entity.entity_count_ * sizeof(int16_t));
                memcpy(data.data(), entity_data.data(), entity.entity_count_ * sizeof(int16_t));
                record.attr_data.insert(std::make_pair(attr_data_it->first, data));

                record.attr_nbytes.insert(std::make_pair(attr_data_it->first, sizeof(int16_t)));
                record.attr_data_size.insert(
                    std::make_pair(attr_data_it->first, entity.entity_count_ * sizeof(int16_t)));
                break;
            }
            case meta::hybrid::DataType::INT32: {
                std::vector<int32_t> entity_data;
                entity_data.resize(entity.entity_count_);
                for (uint64_t j = 0; j < entity.entity_count_; ++j) {
                    entity_data[j] = atoi(attr_data_it->second[j].c_str());
                }
                std::vector<uint8_t> data;
                data.resize(entity.entity_count_ * sizeof(int32_t));
                memcpy(data.data(), entity_data.data(), entity.entity_count_ * sizeof(int32_t));
                record.attr_data.insert(std::make_pair(attr_data_it->first, data));

                record.attr_nbytes.insert(std::make_pair(attr_data_it->first, sizeof(int32_t)));
                record.attr_data_size.insert(
                    std::make_pair(attr_data_it->first, entity.entity_count_ * sizeof(int32_t)));
                break;
            }
            case meta::hybrid::DataType::INT64: {
                std::vector<int64_t> entity_data;
                entity_data.resize(entity.entity_count_);
                for (uint64_t j = 0; j < entity.entity_count_; ++j) {
                    entity_data[j] = atoi(attr_data_it->second[j].c_str());
                }
                std::vector<uint8_t> data;
                data.resize(entity.entity_count_ * sizeof(int64_t));
                memcpy(data.data(), entity_data.data(), entity.entity_count_ * sizeof(int64_t));
                record.attr_data.insert(std::make_pair(attr_data_it->first, data));

                record.attr_nbytes.insert(std::make_pair(attr_data_it->first, sizeof(int64_t)));
                record.attr_data_size.insert(
                    std::make_pair(attr_data_it->first, entity.entity_count_ * sizeof(int64_t)));

                break;
            }
            case meta::hybrid::DataType::FLOAT: {
                std::vector<float> entity_data;
                entity_data.resize(entity.entity_count_);
                for (uint64_t j = 0; j < entity.entity_count_; ++j) {
                    entity_data[j] = atof(attr_data_it->second[j].c_str());
                }
                std::vector<uint8_t> data;
                data.resize(entity.entity_count_ * sizeof(float));
                memcpy(data.data(), entity_data.data(), entity.entity_count_ * sizeof(float));
                record.attr_data.insert(std::make_pair(attr_data_it->first, data));

                record.attr_nbytes.insert(std::make_pair(attr_data_it->first, sizeof(float)));
                record.attr_data_size.insert(std::make_pair(attr_data_it->first, entity.entity_count_ * sizeof(float)));

                break;
            }
            case meta::hybrid::DataType::DOUBLE: {
                std::vector<double> entity_data;
                entity_data.resize(entity.entity_count_);
                for (uint64_t j = 0; j < entity.entity_count_; ++j) {
                    entity_data[j] = atof(attr_data_it->second[j].c_str());
                }
                std::vector<uint8_t> data;
                data.resize(entity.entity_count_ * sizeof(double));
                memcpy(data.data(), entity_data.data(), entity.entity_count_ * sizeof(double));
                record.attr_data.insert(std::make_pair(attr_data_it->first, data));

                record.attr_nbytes.insert(std::make_pair(attr_data_it->first, sizeof(double)));
                record.attr_data_size.insert(
                    std::make_pair(attr_data_it->first, entity.entity_count_ * sizeof(double)));
                break;
            }
        }
    }

    status = ExecWalRecord(record);
    return status;
}

731
Status
J
Jin Hai 已提交
732
DBImpl::DeleteVector(const std::string& collection_id, IDNumber vector_id) {
733 734
    IDNumbers ids;
    ids.push_back(vector_id);
J
Jin Hai 已提交
735
    return DeleteVectors(collection_id, ids);
736 737 738
}

Status
J
Jin Hai 已提交
739
DBImpl::DeleteVectors(const std::string& collection_id, IDNumbers vector_ids) {
740 741 742 743 744 745
    if (!initialized_.load(std::memory_order_acquire)) {
        return SHUTDOWN_ERROR;
    }

    Status status;
    if (options_.wal_enable_) {
J
Jin Hai 已提交
746
        wal_mgr_->DeleteById(collection_id, vector_ids);
G
groot 已提交
747
        swn_wal_.Notify();
748 749 750 751
    } else {
        wal::MXLogRecord record;
        record.lsn = 0;  // need to get from meta ?
        record.type = wal::MXLogType::Delete;
J
Jin Hai 已提交
752
        record.collection_id = collection_id;
753 754 755 756 757 758 759 760 761 762
        record.ids = vector_ids.data();
        record.length = vector_ids.size();

        status = ExecWalRecord(record);
    }

    return status;
}

Status
J
Jin Hai 已提交
763
DBImpl::Flush(const std::string& collection_id) {
764 765 766 767 768
    if (!initialized_.load(std::memory_order_acquire)) {
        return SHUTDOWN_ERROR;
    }

    Status status;
769 770
    bool has_collection;
    status = HasCollection(collection_id, has_collection);
771 772 773
    if (!status.ok()) {
        return status;
    }
774
    if (!has_collection) {
775
        LOG_ENGINE_ERROR_ << "Collection to flush does not exist: " << collection_id;
J
Jin Hai 已提交
776
        return Status(DB_NOT_FOUND, "Collection to flush does not exist");
777 778
    }

779
    LOG_ENGINE_DEBUG_ << "Begin flush collection: " << collection_id;
780 781

    if (options_.wal_enable_) {
782
        LOG_ENGINE_DEBUG_ << "WAL flush";
J
Jin Hai 已提交
783
        auto lsn = wal_mgr_->Flush(collection_id);
784
        if (lsn != 0) {
G
groot 已提交
785 786
            swn_wal_.Notify();
            flush_req_swn_.Wait();
787 788 789
        }

    } else {
790
        LOG_ENGINE_DEBUG_ << "MemTable flush";
G
groot 已提交
791
        InternalFlush(collection_id);
792 793
    }

794
    LOG_ENGINE_DEBUG_ << "End flush collection: " << collection_id;
795 796 797 798 799 800 801 802 803 804

    return status;
}

Status
DBImpl::Flush() {
    if (!initialized_.load(std::memory_order_acquire)) {
        return SHUTDOWN_ERROR;
    }

805
    LOG_ENGINE_DEBUG_ << "Begin flush all collections";
806 807 808

    Status status;
    if (options_.wal_enable_) {
809
        LOG_ENGINE_DEBUG_ << "WAL flush";
810 811
        auto lsn = wal_mgr_->Flush();
        if (lsn != 0) {
G
groot 已提交
812 813
            swn_wal_.Notify();
            flush_req_swn_.Wait();
814 815
        }
    } else {
816
        LOG_ENGINE_DEBUG_ << "MemTable flush";
G
groot 已提交
817
        InternalFlush();
818 819
    }

820
    LOG_ENGINE_DEBUG_ << "End flush all collections";
821 822 823 824 825

    return status;
}

Status
J
Jin Hai 已提交
826
DBImpl::Compact(const std::string& collection_id) {
827 828 829 830
    if (!initialized_.load(std::memory_order_acquire)) {
        return SHUTDOWN_ERROR;
    }

831 832 833
    engine::meta::CollectionSchema collection_schema;
    collection_schema.collection_id_ = collection_id;
    auto status = DescribeCollection(collection_schema);
834 835
    if (!status.ok()) {
        if (status.code() == DB_NOT_FOUND) {
836
            LOG_ENGINE_ERROR_ << "Collection to compact does not exist: " << collection_id;
J
Jin Hai 已提交
837
            return Status(DB_NOT_FOUND, "Collection to compact does not exist");
838 839 840 841
        } else {
            return status;
        }
    } else {
842
        if (!collection_schema.owner_collection_.empty()) {
843
            LOG_ENGINE_ERROR_ << "Collection to compact does not exist: " << collection_id;
J
Jin Hai 已提交
844
            return Status(DB_NOT_FOUND, "Collection to compact does not exist");
845 846 847
        }
    }

848
    LOG_ENGINE_DEBUG_ << "Before compacting, wait for build index thread to finish...";
849

Z
update  
Zhiru Zhu 已提交
850
    // WaitBuildIndexFinish();
851

Z
update  
Zhiru Zhu 已提交
852
    const std::lock_guard<std::mutex> index_lock(build_index_mutex_);
Z
Zhiru Zhu 已提交
853
    const std::lock_guard<std::mutex> merge_lock(flush_merge_compact_mutex_);
Z
Zhiru Zhu 已提交
854

855
    LOG_ENGINE_DEBUG_ << "Compacting collection: " << collection_id;
Z
Zhiru Zhu 已提交
856

857
    // Get files to compact from meta.
J
Jin Hai 已提交
858 859 860 861
    std::vector<int> file_types{meta::SegmentSchema::FILE_TYPE::RAW, meta::SegmentSchema::FILE_TYPE::TO_INDEX,
                                meta::SegmentSchema::FILE_TYPE::BACKUP};
    meta::SegmentsSchema files_to_compact;
    status = meta_ptr_->FilesByType(collection_id, file_types, files_to_compact);
862 863
    if (!status.ok()) {
        std::string err_msg = "Failed to get files to compact: " + status.message();
864
        LOG_ENGINE_ERROR_ << err_msg;
865 866 867
        return Status(DB_ERROR, err_msg);
    }

868
    LOG_ENGINE_DEBUG_ << "Found " << files_to_compact.size() << " segment to compact";
869 870

    OngoingFileChecker::GetInstance().MarkOngoingFiles(files_to_compact);
Z
Zhiru Zhu 已提交
871 872

    Status compact_status;
Z
Zhiru Zhu 已提交
873
    for (auto iter = files_to_compact.begin(); iter != files_to_compact.end();) {
J
Jin Hai 已提交
874
        meta::SegmentSchema file = *iter;
G
groot 已提交
875 876
        iter = files_to_compact.erase(iter);

Z
Zhiru Zhu 已提交
877 878 879
        // Check if the segment needs compacting
        std::string segment_dir;
        utils::GetParentPath(file.location_, segment_dir);
880

Z
Zhiru Zhu 已提交
881
        segment::SegmentReader segment_reader(segment_dir);
Z
Zhiru Zhu 已提交
882 883
        size_t deleted_docs_size;
        status = segment_reader.ReadDeletedDocsSize(deleted_docs_size);
Z
Zhiru Zhu 已提交
884
        if (!status.ok()) {
G
groot 已提交
885 886
            OngoingFileChecker::GetInstance().UnmarkOngoingFile(file);
            continue;  // skip this file and try compact next one
Z
Zhiru Zhu 已提交
887 888
        }

J
Jin Hai 已提交
889
        meta::SegmentsSchema files_to_update;
Z
Zhiru Zhu 已提交
890
        if (deleted_docs_size != 0) {
J
Jin Hai 已提交
891
            compact_status = CompactFile(collection_id, file, files_to_update);
Z
Zhiru Zhu 已提交
892 893

            if (!compact_status.ok()) {
894 895
                LOG_ENGINE_ERROR_ << "Compact failed for segment " << file.segment_id_ << ": "
                                  << compact_status.message();
G
groot 已提交
896 897
                OngoingFileChecker::GetInstance().UnmarkOngoingFile(file);
                continue;  // skip this file and try compact next one
Z
Zhiru Zhu 已提交
898 899
            }
        } else {
G
groot 已提交
900
            OngoingFileChecker::GetInstance().UnmarkOngoingFile(file);
901
            LOG_ENGINE_DEBUG_ << "Segment " << file.segment_id_ << " has no deleted data. No need to compact";
G
groot 已提交
902
            continue;  // skip this file and try compact next one
903
        }
Z
Zhiru Zhu 已提交
904

905
        LOG_ENGINE_DEBUG_ << "Updating meta after compaction...";
906
        status = meta_ptr_->UpdateCollectionFiles(files_to_update);
G
groot 已提交
907
        OngoingFileChecker::GetInstance().UnmarkOngoingFile(file);
G
groot 已提交
908 909 910 911
        if (!status.ok()) {
            compact_status = status;
            break;  // meta error, could not go on
        }
Z
Zhiru Zhu 已提交
912 913
    }

914 915
    OngoingFileChecker::GetInstance().UnmarkOngoingFiles(files_to_compact);

G
groot 已提交
916
    if (compact_status.ok()) {
917
        LOG_ENGINE_DEBUG_ << "Finished compacting collection: " << collection_id;
G
groot 已提交
918
    }
919

G
groot 已提交
920
    return compact_status;
921 922 923
}

Status
J
Jin Hai 已提交
924 925
DBImpl::CompactFile(const std::string& collection_id, const meta::SegmentSchema& file,
                    meta::SegmentsSchema& files_to_update) {
926
    LOG_ENGINE_DEBUG_ << "Compacting segment " << file.segment_id_ << " for collection: " << collection_id;
927

J
Jin Hai 已提交
928 929 930
    // Create new collection file
    meta::SegmentSchema compacted_file;
    compacted_file.collection_id_ = collection_id;
931
    // compacted_file.date_ = date;
J
Jin Hai 已提交
932
    compacted_file.file_type_ = meta::SegmentSchema::NEW_MERGE;  // TODO: use NEW_MERGE for now
933
    Status status = meta_ptr_->CreateCollectionFile(compacted_file);
934 935

    if (!status.ok()) {
936
        LOG_ENGINE_ERROR_ << "Failed to create collection file: " << status.message();
937 938 939
        return status;
    }

J
Jin Hai 已提交
940
    // Compact (merge) file to the newly created collection file
941 942 943 944 945 946 947 948

    std::string new_segment_dir;
    utils::GetParentPath(compacted_file.location_, new_segment_dir);
    auto segment_writer_ptr = std::make_shared<segment::SegmentWriter>(new_segment_dir);

    std::string segment_dir_to_merge;
    utils::GetParentPath(file.location_, segment_dir_to_merge);

949
    LOG_ENGINE_DEBUG_ << "Compacting begin...";
950 951 952
    segment_writer_ptr->Merge(segment_dir_to_merge, compacted_file.file_id_);

    // Serialize
953
    LOG_ENGINE_DEBUG_ << "Serializing compacted segment...";
954 955
    status = segment_writer_ptr->Serialize();
    if (!status.ok()) {
956
        LOG_ENGINE_ERROR_ << "Failed to serialize compacted segment: " << status.message();
J
Jin Hai 已提交
957
        compacted_file.file_type_ = meta::SegmentSchema::TO_DELETE;
958
        auto mark_status = meta_ptr_->UpdateCollectionFile(compacted_file);
959
        if (mark_status.ok()) {
960
            LOG_ENGINE_DEBUG_ << "Mark file: " << compacted_file.file_id_ << " to to_delete";
961 962 963 964
        }
        return status;
    }

J
Jin Hai 已提交
965
    // Update collection files state
966 967
    // if index type isn't IDMAP, set file type to TO_INDEX if file size exceed index_file_size
    // else set file type to RAW, no need to build index
968
    if (!utils::IsRawIndexType(compacted_file.engine_type_)) {
969
        compacted_file.file_type_ = (segment_writer_ptr->Size() >= compacted_file.index_file_size_)
J
Jin Hai 已提交
970 971
                                        ? meta::SegmentSchema::TO_INDEX
                                        : meta::SegmentSchema::RAW;
972
    } else {
J
Jin Hai 已提交
973
        compacted_file.file_type_ = meta::SegmentSchema::RAW;
974 975 976 977 978
    }
    compacted_file.file_size_ = segment_writer_ptr->Size();
    compacted_file.row_count_ = segment_writer_ptr->VectorCount();

    if (compacted_file.row_count_ == 0) {
979
        LOG_ENGINE_DEBUG_ << "Compacted segment is empty. Mark it as TO_DELETE";
J
Jin Hai 已提交
980
        compacted_file.file_type_ = meta::SegmentSchema::TO_DELETE;
981 982
    }

Z
Zhiru Zhu 已提交
983
    files_to_update.emplace_back(compacted_file);
Z
Zhiru Zhu 已提交
984

Z
Zhiru Zhu 已提交
985 986
    // Set all files in segment to TO_DELETE
    auto& segment_id = file.segment_id_;
J
Jin Hai 已提交
987
    meta::SegmentsSchema segment_files;
988
    status = meta_ptr_->GetCollectionFilesBySegmentId(segment_id, segment_files);
Z
Zhiru Zhu 已提交
989 990 991 992
    if (!status.ok()) {
        return status;
    }
    for (auto& f : segment_files) {
J
Jin Hai 已提交
993
        f.file_type_ = meta::SegmentSchema::FILE_TYPE::TO_DELETE;
Z
Zhiru Zhu 已提交
994 995
        files_to_update.emplace_back(f);
    }
996

997 998 999
    LOG_ENGINE_DEBUG_ << "Compacted segment " << compacted_file.segment_id_ << " from "
                      << std::to_string(file.file_size_) << " bytes to " << std::to_string(compacted_file.file_size_)
                      << " bytes";
1000 1001 1002 1003 1004 1005 1006 1007 1008

    if (options_.insert_cache_immediately_) {
        segment_writer_ptr->Cache();
    }

    return status;
}

Status
J
Jin Hai 已提交
1009
DBImpl::GetVectorByID(const std::string& collection_id, const IDNumber& vector_id, VectorsData& vector) {
1010 1011 1012 1013
    if (!initialized_.load(std::memory_order_acquire)) {
        return SHUTDOWN_ERROR;
    }

1014 1015 1016
    bool has_collection;
    auto status = HasCollection(collection_id, has_collection);
    if (!has_collection) {
1017
        LOG_ENGINE_ERROR_ << "Collection " << collection_id << " does not exist: ";
J
Jin Hai 已提交
1018
        return Status(DB_NOT_FOUND, "Collection does not exist");
1019 1020 1021 1022 1023
    }
    if (!status.ok()) {
        return status;
    }

J
Jin Hai 已提交
1024
    meta::SegmentsSchema files_to_query;
1025

J
Jin Hai 已提交
1026 1027
    std::vector<int> file_types{meta::SegmentSchema::FILE_TYPE::RAW, meta::SegmentSchema::FILE_TYPE::TO_INDEX,
                                meta::SegmentSchema::FILE_TYPE::BACKUP};
1028
    meta::SegmentsSchema collection_files;
J
Jin Hai 已提交
1029
    status = meta_ptr_->FilesByType(collection_id, file_types, files_to_query);
1030 1031
    if (!status.ok()) {
        std::string err_msg = "Failed to get files for GetVectorByID: " + status.message();
1032
        LOG_ENGINE_ERROR_ << err_msg;
1033 1034 1035
        return status;
    }

J
Jin Hai 已提交
1036 1037
    OngoingFileChecker::GetInstance().MarkOngoingFiles(files_to_query);

J
Jin Hai 已提交
1038 1039
    std::vector<meta::CollectionSchema> partition_array;
    status = meta_ptr_->ShowPartitions(collection_id, partition_array);
1040
    for (auto& schema : partition_array) {
J
Jin Hai 已提交
1041 1042
        meta::SegmentsSchema files;
        status = meta_ptr_->FilesByType(schema.collection_id_, file_types, files);
1043 1044
        if (!status.ok()) {
            std::string err_msg = "Failed to get files for GetVectorByID: " + status.message();
1045
            LOG_ENGINE_ERROR_ << err_msg;
1046 1047
            return status;
        }
J
Jin Hai 已提交
1048 1049

        OngoingFileChecker::GetInstance().MarkOngoingFiles(files);
1050 1051 1052 1053 1054
        files_to_query.insert(files_to_query.end(), std::make_move_iterator(files.begin()),
                              std::make_move_iterator(files.end()));
    }

    if (files_to_query.empty()) {
1055
        LOG_ENGINE_DEBUG_ << "No files to get vector by id from";
J
Jin Hai 已提交
1056
        return Status(DB_NOT_FOUND, "Collection is empty");
1057 1058 1059 1060
    }

    cache::CpuCacheMgr::GetInstance()->PrintInfo();

J
Jin Hai 已提交
1061
    status = GetVectorByIdHelper(collection_id, vector_id, vector, files_to_query);
1062 1063 1064 1065 1066 1067 1068 1069

    OngoingFileChecker::GetInstance().UnmarkOngoingFiles(files_to_query);
    cache::CpuCacheMgr::GetInstance()->PrintInfo();

    return status;
}

Status
J
Jin Hai 已提交
1070
DBImpl::GetVectorIDs(const std::string& collection_id, const std::string& segment_id, IDNumbers& vector_ids) {
1071 1072 1073 1074
    if (!initialized_.load(std::memory_order_acquire)) {
        return SHUTDOWN_ERROR;
    }

J
Jin Hai 已提交
1075
    // step 1: check collection existence
1076 1077 1078
    bool has_collection;
    auto status = HasCollection(collection_id, has_collection);
    if (!has_collection) {
1079
        LOG_ENGINE_ERROR_ << "Collection " << collection_id << " does not exist: ";
J
Jin Hai 已提交
1080
        return Status(DB_NOT_FOUND, "Collection does not exist");
1081 1082 1083 1084 1085 1086
    }
    if (!status.ok()) {
        return status;
    }

    //  step 2: find segment
1087 1088
    meta::SegmentsSchema collection_files;
    status = meta_ptr_->GetCollectionFilesBySegmentId(segment_id, collection_files);
1089 1090 1091 1092
    if (!status.ok()) {
        return status;
    }

1093
    if (collection_files.empty()) {
1094 1095 1096
        return Status(DB_NOT_FOUND, "Segment does not exist");
    }

J
Jin Hai 已提交
1097
    // check the segment is belong to this collection
1098
    if (collection_files[0].collection_id_ != collection_id) {
J
Jin Hai 已提交
1099
        // the segment could be in a partition under this collection
1100 1101 1102 1103
        meta::CollectionSchema collection_schema;
        collection_schema.collection_id_ = collection_files[0].collection_id_;
        status = DescribeCollection(collection_schema);
        if (collection_schema.owner_collection_ != collection_id) {
J
Jin Hai 已提交
1104
            return Status(DB_NOT_FOUND, "Segment does not belong to this collection");
1105 1106 1107 1108 1109
        }
    }

    // step 3: load segment ids and delete offset
    std::string segment_dir;
1110
    engine::utils::GetParentPath(collection_files[0].location_, segment_dir);
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
    segment::SegmentReader segment_reader(segment_dir);

    std::vector<segment::doc_id_t> uids;
    status = segment_reader.LoadUids(uids);
    if (!status.ok()) {
        return status;
    }

    segment::DeletedDocsPtr deleted_docs_ptr;
    status = segment_reader.LoadDeletedDocs(deleted_docs_ptr);
    if (!status.ok()) {
        return status;
    }

    // step 4: construct id array
    // avoid duplicate offset and erase from max offset to min offset
    auto& deleted_offset = deleted_docs_ptr->GetDeletedDocs();
    std::set<segment::offset_t, std::greater<segment::offset_t>> ordered_offset;
    for (segment::offset_t offset : deleted_offset) {
        ordered_offset.insert(offset);
    }
    for (segment::offset_t offset : ordered_offset) {
        uids.erase(uids.begin() + offset);
    }
    vector_ids.swap(uids);
S
starlord 已提交
1136

G
groot 已提交
1137
    return status;
X
Xu Peng 已提交
1138 1139
}

1140
Status
J
Jin Hai 已提交
1141 1142
DBImpl::GetVectorByIdHelper(const std::string& collection_id, IDNumber vector_id, VectorsData& vector,
                            const meta::SegmentsSchema& files) {
J
Jin Hai 已提交
1143 1144 1145 1146 1147
    LOG_ENGINE_DEBUG_ << "Getting vector by id in " << files.size() << " files, id = " << vector_id;

    vector.vector_count_ = 0;
    vector.float_data_.clear();
    vector.binary_data_.clear();
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174

    for (auto& file : files) {
        // Load bloom filter
        std::string segment_dir;
        engine::utils::GetParentPath(file.location_, segment_dir);
        segment::SegmentReader segment_reader(segment_dir);
        segment::IdBloomFilterPtr id_bloom_filter_ptr;
        segment_reader.LoadBloomFilter(id_bloom_filter_ptr);

        // Check if the id is present in bloom filter.
        if (id_bloom_filter_ptr->Check(vector_id)) {
            // Load uids and check if the id is indeed present. If yes, find its offset.
            std::vector<int64_t> offsets;
            std::vector<segment::doc_id_t> uids;
            auto status = segment_reader.LoadUids(uids);
            if (!status.ok()) {
                return status;
            }

            auto found = std::find(uids.begin(), uids.end(), vector_id);
            if (found != uids.end()) {
                auto offset = std::distance(uids.begin(), found);

                // Check whether the id has been deleted
                segment::DeletedDocsPtr deleted_docs_ptr;
                status = segment_reader.LoadDeletedDocs(deleted_docs_ptr);
                if (!status.ok()) {
J
Jin Hai 已提交
1175
                    LOG_ENGINE_ERROR_ << status.message();
1176 1177 1178 1179 1180 1181 1182
                    return status;
                }
                auto& deleted_docs = deleted_docs_ptr->GetDeletedDocs();

                auto deleted = std::find(deleted_docs.begin(), deleted_docs.end(), offset);
                if (deleted == deleted_docs.end()) {
                    // Load raw vector
1183
                    bool is_binary = utils::IsBinaryMetricType(file.metric_type_);
1184 1185 1186 1187
                    size_t single_vector_bytes = is_binary ? file.dimension_ / 8 : file.dimension_ * sizeof(float);
                    std::vector<uint8_t> raw_vector;
                    status = segment_reader.LoadVectors(offset * single_vector_bytes, single_vector_bytes, raw_vector);
                    if (!status.ok()) {
J
Jin Hai 已提交
1188
                        LOG_ENGINE_ERROR_ << status.message();
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
                        return status;
                    }

                    vector.vector_count_ = 1;
                    if (is_binary) {
                        vector.binary_data_ = std::move(raw_vector);
                    } else {
                        std::vector<float> float_vector;
                        float_vector.resize(file.dimension_);
                        memcpy(float_vector.data(), raw_vector.data(), single_vector_bytes);
                        vector.float_data_ = std::move(float_vector);
                    }
                    return Status::OK();
                }
            }
        } else {
            continue;
        }
    }

J
Jin Hai 已提交
1209 1210 1211 1212 1213
    if (vector.binary_data_.empty() && vector.float_data_.empty()) {
        std::string msg = "Vector with id " + std::to_string(vector_id) + " not found in collection " + collection_id;
        LOG_ENGINE_DEBUG_ << msg;
    }

1214 1215 1216
    return Status::OK();
}

S
starlord 已提交
1217
Status
1218
DBImpl::CreateIndex(const std::string& collection_id, const CollectionIndex& index) {
1219
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
1220 1221 1222
        return SHUTDOWN_ERROR;
    }

G
groot 已提交
1223
    // serialize memory data
1224 1225
    //    std::set<std::string> sync_collection_ids;
    //    auto status = SyncMemData(sync_collection_ids);
1226
    auto status = Flush();
G
groot 已提交
1227

S
starlord 已提交
1228 1229 1230
    {
        std::unique_lock<std::mutex> lock(build_index_mutex_);

S
starlord 已提交
1231
        // step 1: check index difference
1232
        CollectionIndex old_index;
J
Jin Hai 已提交
1233
        status = DescribeIndex(collection_id, old_index);
S
starlord 已提交
1234
        if (!status.ok()) {
1235
            LOG_ENGINE_ERROR_ << "Failed to get collection index info for collection: " << collection_id;
S
starlord 已提交
1236 1237 1238
            return status;
        }

S
starlord 已提交
1239
        // step 2: update index info
1240 1241
        CollectionIndex new_index = index;
        new_index.metric_type_ = old_index.metric_type_;  // dont change metric type, it was defined by CreateCollection
S
starlord 已提交
1242
        if (!utils::IsSameIndex(old_index, new_index)) {
1243
            status = UpdateCollectionIndexRecursively(collection_id, new_index);
S
starlord 已提交
1244 1245 1246 1247 1248 1249
            if (!status.ok()) {
                return status;
            }
        }
    }

S
starlord 已提交
1250 1251
    // step 3: let merge file thread finish
    // to avoid duplicate data bug
1252 1253
    WaitMergeFileFinish();

S
starlord 已提交
1254
    // step 4: wait and build index
1255 1256
    status = index_failed_checker_.CleanFailedIndexFileOfCollection(collection_id);
    status = WaitCollectionIndexRecursively(collection_id, index);
S
starlord 已提交
1257

G
groot 已提交
1258
    return status;
S
starlord 已提交
1259 1260
}

S
starlord 已提交
1261
Status
1262
DBImpl::DescribeIndex(const std::string& collection_id, CollectionIndex& index) {
1263
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
1264 1265 1266
        return SHUTDOWN_ERROR;
    }

1267
    return meta_ptr_->DescribeCollectionIndex(collection_id, index);
S
starlord 已提交
1268 1269
}

S
starlord 已提交
1270
Status
J
Jin Hai 已提交
1271
DBImpl::DropIndex(const std::string& collection_id) {
1272
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
1273 1274 1275
        return SHUTDOWN_ERROR;
    }

1276
    LOG_ENGINE_DEBUG_ << "Drop index for collection: " << collection_id;
1277
    return DropCollectionIndexRecursively(collection_id);
S
starlord 已提交
1278 1279
}

S
starlord 已提交
1280
Status
J
Jin Hai 已提交
1281
DBImpl::QueryByID(const std::shared_ptr<server::Context>& context, const std::string& collection_id,
1282 1283
                  const std::vector<std::string>& partition_tags, uint64_t k, const milvus::json& extra_params,
                  IDNumber vector_id, ResultIds& result_ids, ResultDistances& result_distances) {
1284
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
1285
        return SHUTDOWN_ERROR;
S
starlord 已提交
1286 1287
    }

1288 1289 1290
    VectorsData vectors_data = VectorsData();
    vectors_data.id_array_.emplace_back(vector_id);
    vectors_data.vector_count_ = 1;
1291
    Status result =
J
Jin Hai 已提交
1292
        Query(context, collection_id, partition_tags, k, extra_params, vectors_data, result_ids, result_distances);
Y
yu yunfeng 已提交
1293
    return result;
X
Xu Peng 已提交
1294 1295
}

1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
Status
DBImpl::HybridQuery(const std::shared_ptr<server::Context>& context, const std::string& collection_id,
                    const std::vector<std::string>& partition_tags,
                    context::HybridSearchContextPtr hybrid_search_context, query::GeneralQueryPtr general_query,
                    std::unordered_map<std::string, engine::meta::hybrid::DataType>& attr_type, uint64_t& nq,
                    ResultIds& result_ids, ResultDistances& result_distances) {
    auto query_ctx = context->Child("Query");

    if (!initialized_.load(std::memory_order_acquire)) {
        return SHUTDOWN_ERROR;
    }

    Status status;
    std::vector<size_t> ids;
    meta::SegmentsSchema files_array;

    if (partition_tags.empty()) {
        // no partition tag specified, means search in whole table
        // get all table files from parent table
        status = GetFilesToSearch(collection_id, files_array);
        if (!status.ok()) {
            return status;
        }

        std::vector<meta::CollectionSchema> partition_array;
        status = meta_ptr_->ShowPartitions(collection_id, partition_array);
        if (!status.ok()) {
            return status;
        }
        for (auto& schema : partition_array) {
            status = GetFilesToSearch(schema.collection_id_, files_array);
            if (!status.ok()) {
                return Status(DB_ERROR, "GetFilesToSearch failed in HybridQuery");
            }
        }

        if (files_array.empty()) {
            return Status::OK();
        }
    } else {
        // get files from specified partitions
        std::set<std::string> partition_name_array;
        GetPartitionsByTags(collection_id, partition_tags, partition_name_array);

        for (auto& partition_name : partition_name_array) {
            status = GetFilesToSearch(partition_name, files_array);
            if (!status.ok()) {
                return Status(DB_ERROR, "GetFilesToSearch failed in HybridQuery");
            }
        }

        if (files_array.empty()) {
            return Status::OK();
        }
    }

    cache::CpuCacheMgr::GetInstance()->PrintInfo();  // print cache info before query
    status = HybridQueryAsync(query_ctx, collection_id, files_array, hybrid_search_context, general_query, attr_type,
                              nq, result_ids, result_distances);
    if (!status.ok()) {
        return status;
    }
    cache::CpuCacheMgr::GetInstance()->PrintInfo();  // print cache info after query

    query_ctx->GetTraceContext()->GetSpan()->Finish();

    return status;
}

S
starlord 已提交
1365
Status
J
Jin Hai 已提交
1366
DBImpl::Query(const std::shared_ptr<server::Context>& context, const std::string& collection_id,
1367 1368
              const std::vector<std::string>& partition_tags, uint64_t k, const milvus::json& extra_params,
              const VectorsData& vectors, ResultIds& result_ids, ResultDistances& result_distances) {
1369
    milvus::server::ContextChild tracer(context, "Query");
Z
Zhiru Zhu 已提交
1370

1371
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
1372
        return SHUTDOWN_ERROR;
S
starlord 已提交
1373 1374
    }

G
groot 已提交
1375
    Status status;
J
Jin Hai 已提交
1376
    meta::SegmentsSchema files_array;
1377

G
groot 已提交
1378
    if (partition_tags.empty()) {
J
Jin Hai 已提交
1379 1380 1381
        // no partition tag specified, means search in whole collection
        // get all collection files from parent collection
        status = GetFilesToSearch(collection_id, files_array);
G
groot 已提交
1382 1383 1384 1385
        if (!status.ok()) {
            return status;
        }

J
Jin Hai 已提交
1386 1387
        std::vector<meta::CollectionSchema> partition_array;
        status = meta_ptr_->ShowPartitions(collection_id, partition_array);
G
groot 已提交
1388
        for (auto& schema : partition_array) {
J
Jin Hai 已提交
1389
            status = GetFilesToSearch(schema.collection_id_, files_array);
1390 1391 1392 1393
        }

        if (files_array.empty()) {
            return Status::OK();
G
groot 已提交
1394 1395 1396 1397
        }
    } else {
        // get files from specified partitions
        std::set<std::string> partition_name_array;
J
Jin Hai 已提交
1398
        status = GetPartitionsByTags(collection_id, partition_tags, partition_name_array);
T
Tinkerrr 已提交
1399 1400 1401
        if (!status.ok()) {
            return status;  // didn't match any partition.
        }
G
groot 已提交
1402 1403

        for (auto& partition_name : partition_name_array) {
1404
            status = GetFilesToSearch(partition_name, files_array);
1405 1406 1407 1408
        }

        if (files_array.empty()) {
            return Status::OK();
1409 1410 1411
        }
    }

S
starlord 已提交
1412
    cache::CpuCacheMgr::GetInstance()->PrintInfo();  // print cache info before query
1413
    status = QueryAsync(tracer.Context(), files_array, k, extra_params, vectors, result_ids, result_distances);
S
starlord 已提交
1414
    cache::CpuCacheMgr::GetInstance()->PrintInfo();  // print cache info after query
Z
Zhiru Zhu 已提交
1415

S
starlord 已提交
1416
    return status;
G
groot 已提交
1417
}
X
Xu Peng 已提交
1418

S
starlord 已提交
1419
Status
1420 1421 1422
DBImpl::QueryByFileID(const std::shared_ptr<server::Context>& context, const std::vector<std::string>& file_ids,
                      uint64_t k, const milvus::json& extra_params, const VectorsData& vectors, ResultIds& result_ids,
                      ResultDistances& result_distances) {
1423
    milvus::server::ContextChild tracer(context, "Query by file id");
Z
Zhiru Zhu 已提交
1424

1425
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
1426
        return SHUTDOWN_ERROR;
S
starlord 已提交
1427 1428
    }

S
starlord 已提交
1429
    // get specified files
1430
    std::vector<size_t> ids;
Y
Yu Kun 已提交
1431
    for (auto& id : file_ids) {
1432
        std::string::size_type sz;
J
jinhai 已提交
1433
        ids.push_back(std::stoul(id, &sz));
1434 1435
    }

J
Jin Hai 已提交
1436
    meta::SegmentsSchema search_files;
1437
    auto status = meta_ptr_->FilesByID(ids, search_files);
1438 1439
    if (!status.ok()) {
        return status;
1440 1441
    }

1442 1443
    fiu_do_on("DBImpl.QueryByFileID.empty_files_array", search_files.clear());
    if (search_files.empty()) {
S
starlord 已提交
1444
        return Status(DB_ERROR, "Invalid file id");
G
groot 已提交
1445 1446
    }

S
starlord 已提交
1447
    cache::CpuCacheMgr::GetInstance()->PrintInfo();  // print cache info before query
1448
    status = QueryAsync(tracer.Context(), search_files, k, extra_params, vectors, result_ids, result_distances);
S
starlord 已提交
1449
    cache::CpuCacheMgr::GetInstance()->PrintInfo();  // print cache info after query
Z
Zhiru Zhu 已提交
1450

S
starlord 已提交
1451
    return status;
1452 1453
}

S
starlord 已提交
1454
Status
Y
Yu Kun 已提交
1455
DBImpl::Size(uint64_t& result) {
1456
    if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
1457
        return SHUTDOWN_ERROR;
S
starlord 已提交
1458 1459
    }

S
starlord 已提交
1460
    return meta_ptr_->Size(result);
S
starlord 已提交
1461 1462 1463
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
1464
// internal methods
S
starlord 已提交
1465
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
1466
Status
J
Jin Hai 已提交
1467
DBImpl::QueryAsync(const std::shared_ptr<server::Context>& context, const meta::SegmentsSchema& files, uint64_t k,
1468 1469
                   const milvus::json& extra_params, const VectorsData& vectors, ResultIds& result_ids,
                   ResultDistances& result_distances) {
1470
    milvus::server::ContextChild tracer(context, "Query Async");
G
groot 已提交
1471
    server::CollectQueryMetrics metrics(vectors.vector_count_);
Y
Yu Kun 已提交
1472

G
groot 已提交
1473 1474 1475
    if (files.size() > milvus::scheduler::TASK_TABLE_MAX_COUNT) {
        std::string msg =
            "Search files count exceed scheduler limit: " + std::to_string(milvus::scheduler::TASK_TABLE_MAX_COUNT);
1476
        LOG_ENGINE_ERROR_ << msg;
G
groot 已提交
1477 1478 1479
        return Status(DB_ERROR, msg);
    }

S
starlord 已提交
1480
    TimeRecorder rc("");
G
groot 已提交
1481

1482
    // step 1: construct search job
1483
    auto status = OngoingFileChecker::GetInstance().MarkOngoingFiles(files);
1484

1485
    LOG_ENGINE_DEBUG_ << LogOut("Engine query begin, index file count: %ld", files.size());
1486
    scheduler::SearchJobPtr job = std::make_shared<scheduler::SearchJob>(tracer.Context(), k, extra_params, vectors);
Y
Yu Kun 已提交
1487
    for (auto& file : files) {
J
Jin Hai 已提交
1488
        scheduler::SegmentSchemaPtr file_ptr = std::make_shared<meta::SegmentSchema>(file);
W
wxyu 已提交
1489
        job->AddIndexFile(file_ptr);
G
groot 已提交
1490 1491
    }

1492
    // step 2: put search job to scheduler and wait result
S
starlord 已提交
1493
    scheduler::JobMgrInst::GetInstance()->Put(job);
W
wxyu 已提交
1494
    job->WaitResult();
1495

1496
    status = OngoingFileChecker::GetInstance().UnmarkOngoingFiles(files);
W
wxyu 已提交
1497 1498
    if (!job->GetStatus().ok()) {
        return job->GetStatus();
1499
    }
G
groot 已提交
1500

1501
    // step 3: construct results
G
groot 已提交
1502 1503
    result_ids = job->GetResultIds();
    result_distances = job->GetResultDistances();
S
starlord 已提交
1504
    rc.ElapseFromBegin("Engine query totally cost");
G
groot 已提交
1505 1506 1507 1508

    return Status::OK();
}

1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
Status
DBImpl::HybridQueryAsync(const std::shared_ptr<server::Context>& context, const std::string& table_id,
                         const meta::SegmentsSchema& files, context::HybridSearchContextPtr hybrid_search_context,
                         query::GeneralQueryPtr general_query,
                         std::unordered_map<std::string, engine::meta::hybrid::DataType>& attr_type, uint64_t& nq,
                         ResultIds& result_ids, ResultDistances& result_distances) {
    auto query_async_ctx = context->Child("Query Async");

#if 0
    // Construct tasks
    for (auto file : files) {
        std::unordered_map<std::string, engine::DataType> types;
        auto it = attr_type.begin();
        for (; it != attr_type.end(); it++) {
            types.insert(std::make_pair(it->first, (engine::DataType)it->second));
        }

        auto file_ptr = std::make_shared<meta::TableFileSchema>(file);
        search::TaskPtr
            task = std::make_shared<search::Task>(context, file_ptr, general_query, types, hybrid_search_context);
        search::TaskInst::GetInstance().load_queue().push(task);
        search::TaskInst::GetInstance().load_cv().notify_one();
        hybrid_search_context->tasks_.emplace_back(task);
    }

#endif

    //#if 0
    TimeRecorder rc("");

    // step 1: construct search job
    auto status = OngoingFileChecker::GetInstance().MarkOngoingFiles(files);

    VectorsData vectors;

    LOG_ENGINE_DEBUG_ << LogOut("Engine query begin, index file count: %ld", files.size());
    scheduler::SearchJobPtr job =
        std::make_shared<scheduler::SearchJob>(query_async_ctx, general_query, attr_type, vectors);
    for (auto& file : files) {
        scheduler::SegmentSchemaPtr file_ptr = std::make_shared<meta::SegmentSchema>(file);
        job->AddIndexFile(file_ptr);
    }

    // step 2: put search job to scheduler and wait result
    scheduler::JobMgrInst::GetInstance()->Put(job);
    job->WaitResult();

    status = OngoingFileChecker::GetInstance().UnmarkOngoingFiles(files);
    if (!job->GetStatus().ok()) {
        return job->GetStatus();
    }

    // step 3: construct results
    nq = job->vector_count();
    result_ids = job->GetResultIds();
    result_distances = job->GetResultDistances();
    rc.ElapseFromBegin("Engine query totally cost");

    query_async_ctx->GetTraceContext()->GetSpan()->Finish();
    //#endif

    return Status::OK();
}

S
starlord 已提交
1573
void
G
groot 已提交
1574
DBImpl::BackgroundIndexThread() {
Y
yu yunfeng 已提交
1575
    server::SystemInfo::GetInstance().Init();
X
Xu Peng 已提交
1576
    while (true) {
1577
        if (!initialized_.load(std::memory_order_acquire)) {
1578 1579
            WaitMergeFileFinish();
            WaitBuildIndexFinish();
S
starlord 已提交
1580

1581
            LOG_ENGINE_DEBUG_ << "DB background thread exit";
G
groot 已提交
1582 1583
            break;
        }
X
Xu Peng 已提交
1584

G
groot 已提交
1585
        swn_index_.Wait_For(std::chrono::seconds(BACKGROUND_INDEX_INTERVAL));
X
Xu Peng 已提交
1586

G
groot 已提交
1587
        WaitMergeFileFinish();
G
groot 已提交
1588 1589
        StartBuildIndexTask();
    }
X
Xu Peng 已提交
1590 1591
}

S
starlord 已提交
1592 1593
void
DBImpl::WaitMergeFileFinish() {
1594
    //    LOG_ENGINE_DEBUG_ << "Begin WaitMergeFileFinish";
1595 1596
    std::lock_guard<std::mutex> lck(merge_result_mutex_);
    for (auto& iter : merge_thread_results_) {
1597 1598
        iter.wait();
    }
1599
    //    LOG_ENGINE_DEBUG_ << "End WaitMergeFileFinish";
1600 1601
}

S
starlord 已提交
1602 1603
void
DBImpl::WaitBuildIndexFinish() {
1604
    //    LOG_ENGINE_DEBUG_ << "Begin WaitBuildIndexFinish";
1605
    std::lock_guard<std::mutex> lck(index_result_mutex_);
Y
Yu Kun 已提交
1606
    for (auto& iter : index_thread_results_) {
1607 1608
        iter.wait();
    }
1609
    //    LOG_ENGINE_DEBUG_ << "End WaitBuildIndexFinish";
1610 1611
}

S
starlord 已提交
1612 1613
void
DBImpl::StartMetricTask() {
G
groot 已提交
1614
    server::Metrics::GetInstance().KeepingAliveCounterIncrement(BACKGROUND_METRIC_INTERVAL);
G
groot 已提交
1615 1616
    int64_t cache_usage = cache::CpuCacheMgr::GetInstance()->CacheUsage();
    int64_t cache_total = cache::CpuCacheMgr::GetInstance()->CacheCapacity();
S
shengjh 已提交
1617 1618
    fiu_do_on("DBImpl.StartMetricTask.InvalidTotalCache", cache_total = 0);

J
JinHai-CN 已提交
1619 1620 1621 1622 1623 1624 1625
    if (cache_total > 0) {
        double cache_usage_double = cache_usage;
        server::Metrics::GetInstance().CpuCacheUsageGaugeSet(cache_usage_double * 100 / cache_total);
    } else {
        server::Metrics::GetInstance().CpuCacheUsageGaugeSet(0);
    }

Y
Yu Kun 已提交
1626
    server::Metrics::GetInstance().GpuCacheUsageGaugeSet();
G
groot 已提交
1627 1628 1629 1630 1631 1632 1633 1634
    uint64_t size;
    Size(size);
    server::Metrics::GetInstance().DataFileSizeGaugeSet(size);
    server::Metrics::GetInstance().CPUUsagePercentSet();
    server::Metrics::GetInstance().RAMUsagePercentSet();
    server::Metrics::GetInstance().GPUPercentGaugeSet();
    server::Metrics::GetInstance().GPUMemoryUsageGaugeSet();
    server::Metrics::GetInstance().OctetsSet();
S
starlord 已提交
1635

K
kun yu 已提交
1636
    server::Metrics::GetInstance().CPUCoreUsagePercentSet();
K
kun yu 已提交
1637 1638
    server::Metrics::GetInstance().GPUTemperature();
    server::Metrics::GetInstance().CPUTemperature();
1639
    server::Metrics::GetInstance().PushToGateway();
G
groot 已提交
1640 1641
}

S
starlord 已提交
1642
void
1643
DBImpl::StartMergeTask() {
1644
    // LOG_ENGINE_DEBUG_ << "Begin StartMergeTask";
1645
    // merge task has been finished?
1646
    {
1647 1648
        std::lock_guard<std::mutex> lck(merge_result_mutex_);
        if (!merge_thread_results_.empty()) {
1649
            std::chrono::milliseconds span(10);
1650 1651
            if (merge_thread_results_.back().wait_for(span) == std::future_status::ready) {
                merge_thread_results_.pop_back();
1652
            }
G
groot 已提交
1653 1654
        }
    }
X
Xu Peng 已提交
1655

1656
    // add new merge task
1657
    {
1658 1659
        std::lock_guard<std::mutex> lck(merge_result_mutex_);
        if (merge_thread_results_.empty()) {
1660 1661
            // collect merge files for all collections(if merge_collection_ids_ is empty) for two reasons:
            // 1. other collections may still has un-merged files
1662
            // 2. server may be closed unexpected, these un-merge files need to be merged when server restart
1663 1664 1665 1666 1667
            if (merge_collection_ids_.empty()) {
                std::vector<meta::CollectionSchema> collection_schema_array;
                meta_ptr_->AllCollections(collection_schema_array);
                for (auto& schema : collection_schema_array) {
                    merge_collection_ids_.insert(schema.collection_id_);
1668 1669 1670 1671
                }
            }

            // start merge file thread
1672
            merge_thread_results_.push_back(
1673 1674
                merge_thread_pool_.enqueue(&DBImpl::BackgroundMerge, this, merge_collection_ids_));
            merge_collection_ids_.clear();
1675
        }
G
groot 已提交
1676
    }
1677

1678
    // LOG_ENGINE_DEBUG_ << "End StartMergeTask";
X
Xu Peng 已提交
1679 1680
}

S
starlord 已提交
1681
Status
J
Jin Hai 已提交
1682
DBImpl::MergeFiles(const std::string& collection_id, const meta::SegmentsSchema& files) {
Z
Zhiru Zhu 已提交
1683
    // const std::lock_guard<std::mutex> lock(flush_merge_compact_mutex_);
1684

1685
    LOG_ENGINE_DEBUG_ << "Merge files for collection: " << collection_id;
S
starlord 已提交
1686

J
Jin Hai 已提交
1687
    // step 1: create collection file
1688 1689 1690 1691
    meta::SegmentSchema collection_file;
    collection_file.collection_id_ = collection_id;
    collection_file.file_type_ = meta::SegmentSchema::NEW_MERGE;
    Status status = meta_ptr_->CreateCollectionFile(collection_file);
X
Xu Peng 已提交
1692

1693
    if (!status.ok()) {
1694
        LOG_ENGINE_ERROR_ << "Failed to create collection: " << status.ToString();
1695 1696 1697
        return status;
    }

S
starlord 已提交
1698
    // step 2: merge files
1699
    /*
G
groot 已提交
1700
    ExecutionEnginePtr index =
1701 1702
        EngineFactory::Build(collection_file.dimension_, collection_file.location_,
    (EngineType)collection_file.engine_type_, (MetricType)collection_file.metric_type_, collection_file.nlist_);
1703
*/
J
Jin Hai 已提交
1704
    meta::SegmentsSchema updated;
1705 1706

    std::string new_segment_dir;
1707
    utils::GetParentPath(collection_file.location_, new_segment_dir);
1708
    auto segment_writer_ptr = std::make_shared<segment::SegmentWriter>(new_segment_dir);
1709

Y
Yu Kun 已提交
1710
    for (auto& file : files) {
Y
Yu Kun 已提交
1711
        server::CollectMergeFilesMetrics metrics;
1712 1713
        std::string segment_dir_to_merge;
        utils::GetParentPath(file.location_, segment_dir_to_merge);
1714
        segment_writer_ptr->Merge(segment_dir_to_merge, collection_file.file_id_);
1715
        auto file_schema = file;
J
Jin Hai 已提交
1716
        file_schema.file_type_ = meta::SegmentSchema::TO_DELETE;
1717
        updated.push_back(file_schema);
1718 1719
        auto size = segment_writer_ptr->Size();
        if (size >= file_schema.index_file_size_) {
S
starlord 已提交
1720
            break;
S
starlord 已提交
1721
        }
1722 1723
    }

S
starlord 已提交
1724
    // step 3: serialize to disk
S
starlord 已提交
1725
    try {
1726
        status = segment_writer_ptr->Serialize();
S
shengjh 已提交
1727 1728
        fiu_do_on("DBImpl.MergeFiles.Serialize_ThrowException", throw std::exception());
        fiu_do_on("DBImpl.MergeFiles.Serialize_ErrorStatus", status = Status(DB_ERROR, ""));
Y
Yu Kun 已提交
1729
    } catch (std::exception& ex) {
S
starlord 已提交
1730
        std::string msg = "Serialize merged index encounter exception: " + std::string(ex.what());
1731
        LOG_ENGINE_ERROR_ << msg;
G
groot 已提交
1732 1733
        status = Status(DB_ERROR, msg);
    }
Y
yu yunfeng 已提交
1734

G
groot 已提交
1735
    if (!status.ok()) {
1736
        LOG_ENGINE_ERROR_ << "Failed to persist merged segment: " << new_segment_dir << ". Error: " << status.message();
1737

G
groot 已提交
1738
        // if failed to serialize merge file to disk
1739
        // typical error: out of disk space, out of memory or permission denied
1740 1741
        collection_file.file_type_ = meta::SegmentSchema::TO_DELETE;
        status = meta_ptr_->UpdateCollectionFile(collection_file);
1742 1743
        LOG_ENGINE_DEBUG_ << "Failed to update file to index, mark file: " << collection_file.file_id_
                          << " to to_delete";
X
Xu Peng 已提交
1744

G
groot 已提交
1745
        return status;
S
starlord 已提交
1746 1747
    }

J
Jin Hai 已提交
1748
    // step 4: update collection files state
1749
    // if index type isn't IDMAP, set file type to TO_INDEX if file size exceed index_file_size
S
starlord 已提交
1750
    // else set file type to RAW, no need to build index
1751 1752 1753 1754
    if (!utils::IsRawIndexType(collection_file.engine_type_)) {
        collection_file.file_type_ = (segment_writer_ptr->Size() >= collection_file.index_file_size_)
                                         ? meta::SegmentSchema::TO_INDEX
                                         : meta::SegmentSchema::RAW;
1755
    } else {
1756
        collection_file.file_type_ = meta::SegmentSchema::RAW;
1757
    }
1758 1759 1760 1761
    collection_file.file_size_ = segment_writer_ptr->Size();
    collection_file.row_count_ = segment_writer_ptr->VectorCount();
    updated.push_back(collection_file);
    status = meta_ptr_->UpdateCollectionFiles(updated);
1762 1763
    LOG_ENGINE_DEBUG_ << "New merged segment " << collection_file.segment_id_ << " of size "
                      << segment_writer_ptr->Size() << " bytes";
1764

S
starlord 已提交
1765
    if (options_.insert_cache_immediately_) {
1766
        segment_writer_ptr->Cache();
S
starlord 已提交
1767
    }
X
Xu Peng 已提交
1768

1769 1770 1771
    return status;
}

1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
Status
DBImpl::MergeHybridFiles(const std::string& collection_id, const milvus::engine::meta::SegmentsSchema& files) {
    // const std::lock_guard<std::mutex> lock(flush_merge_compact_mutex_);

    LOG_ENGINE_DEBUG_ << "Merge files for collection: " << collection_id;

    // step 1: create table file
    meta::SegmentSchema table_file;
    table_file.collection_id_ = collection_id;
    table_file.file_type_ = meta::SegmentSchema::NEW_MERGE;
    Status status = meta_ptr_->CreateHybridCollectionFile(table_file);

    if (!status.ok()) {
        LOG_ENGINE_ERROR_ << "Failed to create collection: " << status.ToString();
        return status;
    }

    // step 2: merge files
    /*
    ExecutionEnginePtr index =
        EngineFactory::Build(table_file.dimension_, table_file.location_, (EngineType)table_file.engine_type_,
                             (MetricType)table_file.metric_type_, table_file.nlist_);
*/
    meta::SegmentsSchema updated;

    std::string new_segment_dir;
    utils::GetParentPath(table_file.location_, new_segment_dir);
    auto segment_writer_ptr = std::make_shared<segment::SegmentWriter>(new_segment_dir);

    for (auto& file : files) {
        server::CollectMergeFilesMetrics metrics;
        std::string segment_dir_to_merge;
        utils::GetParentPath(file.location_, segment_dir_to_merge);
        segment_writer_ptr->Merge(segment_dir_to_merge, table_file.file_id_);
        auto file_schema = file;
        file_schema.file_type_ = meta::SegmentSchema::TO_DELETE;
        updated.push_back(file_schema);
        auto size = segment_writer_ptr->Size();
        if (size >= file_schema.index_file_size_) {
            break;
        }
    }

    // step 3: serialize to disk
    try {
        status = segment_writer_ptr->Serialize();
        fiu_do_on("DBImpl.MergeFiles.Serialize_ThrowException", throw std::exception());
        fiu_do_on("DBImpl.MergeFiles.Serialize_ErrorStatus", status = Status(DB_ERROR, ""));
    } catch (std::exception& ex) {
        std::string msg = "Serialize merged index encounter exception: " + std::string(ex.what());
        LOG_ENGINE_ERROR_ << msg;
        status = Status(DB_ERROR, msg);
    }

    if (!status.ok()) {
        LOG_ENGINE_ERROR_ << "Failed to persist merged segment: " << new_segment_dir << ". Error: " << status.message();

        // if failed to serialize merge file to disk
        // typical error: out of disk space, out of memory or permission denied
        table_file.file_type_ = meta::SegmentSchema::TO_DELETE;
        status = meta_ptr_->UpdateCollectionFile(table_file);
        LOG_ENGINE_DEBUG_ << "Failed to update file to index, mark file: " << table_file.file_id_ << " to to_delete";

        return status;
    }

    // step 4: update table files state
    // if index type isn't IDMAP, set file type to TO_INDEX if file size exceed index_file_size
    // else set file type to RAW, no need to build index
    if (!utils::IsRawIndexType(table_file.engine_type_)) {
        table_file.file_type_ = (segment_writer_ptr->Size() >= table_file.index_file_size_)
                                    ? meta::SegmentSchema::TO_INDEX
                                    : meta::SegmentSchema::RAW;
    } else {
        table_file.file_type_ = meta::SegmentSchema::RAW;
    }
    table_file.file_size_ = segment_writer_ptr->Size();
    table_file.row_count_ = segment_writer_ptr->VectorCount();
    updated.push_back(table_file);
    status = meta_ptr_->UpdateCollectionFiles(updated);
    LOG_ENGINE_DEBUG_ << "New merged segment " << table_file.segment_id_ << " of size " << segment_writer_ptr->Size()
                      << " bytes";

    if (options_.insert_cache_immediately_) {
        segment_writer_ptr->Cache();
    }

    return status;
}

S
starlord 已提交
1862
Status
J
Jin Hai 已提交
1863
DBImpl::BackgroundMergeFiles(const std::string& collection_id) {
Z
Zhiru Zhu 已提交
1864
    const std::lock_guard<std::mutex> lock(flush_merge_compact_mutex_);
1865

J
Jin Hai 已提交
1866 1867
    meta::SegmentsSchema raw_files;
    auto status = meta_ptr_->FilesToMerge(collection_id, raw_files);
X
Xu Peng 已提交
1868
    if (!status.ok()) {
1869
        LOG_ENGINE_ERROR_ << "Failed to get merge files for collection: " << collection_id;
X
Xu Peng 已提交
1870 1871
        return status;
    }
1872

1873
    if (raw_files.size() < options_.merge_trigger_number_) {
1874
        LOG_ENGINE_TRACE_ << "Files number not greater equal than merge trigger number, skip merge action";
1875 1876
        return Status::OK();
    }
1877

1878
    status = OngoingFileChecker::GetInstance().MarkOngoingFiles(raw_files);
J
Jin Hai 已提交
1879
    MergeFiles(collection_id, raw_files);
1880
    status = OngoingFileChecker::GetInstance().UnmarkOngoingFiles(raw_files);
G
groot 已提交
1881

1882
    if (!initialized_.load(std::memory_order_acquire)) {
1883
        LOG_ENGINE_DEBUG_ << "Server will shutdown, skip merge action for collection: " << collection_id;
1884
    }
X
Xu Peng 已提交
1885

G
groot 已提交
1886 1887
    return Status::OK();
}
1888

S
starlord 已提交
1889
void
1890
DBImpl::BackgroundMerge(std::set<std::string> collection_ids) {
1891
    // LOG_ENGINE_TRACE_ << " Background merge thread start";
S
starlord 已提交
1892

G
groot 已提交
1893
    Status status;
1894
    for (auto& collection_id : collection_ids) {
J
Jin Hai 已提交
1895
        status = BackgroundMergeFiles(collection_id);
G
groot 已提交
1896
        if (!status.ok()) {
1897
            LOG_ENGINE_ERROR_ << "Merge files for collection " << collection_id << " failed: " << status.ToString();
G
groot 已提交
1898
        }
S
starlord 已提交
1899

1900
        if (!initialized_.load(std::memory_order_acquire)) {
1901
            LOG_ENGINE_DEBUG_ << "Server will shutdown, skip merge action";
S
starlord 已提交
1902 1903
            break;
        }
G
groot 已提交
1904
    }
X
Xu Peng 已提交
1905

G
groot 已提交
1906
    meta_ptr_->Archive();
Z
update  
zhiru 已提交
1907

1908
    {
G
groot 已提交
1909
        uint64_t ttl = 10 * meta::SECOND;  // default: file will be hard-deleted few seconds after soft-deleted
1910
        if (options_.mode_ == DBOptions::MODE::CLUSTER_WRITABLE) {
1911
            ttl = meta::HOUR;
1912
        }
G
groot 已提交
1913

1914
        meta_ptr_->CleanUpFilesWithTTL(ttl);
Z
update  
zhiru 已提交
1915
    }
S
starlord 已提交
1916

1917
    // LOG_ENGINE_TRACE_ << " Background merge thread exit";
G
groot 已提交
1918
}
X
Xu Peng 已提交
1919

S
starlord 已提交
1920
void
G
groot 已提交
1921
DBImpl::StartBuildIndexTask() {
S
starlord 已提交
1922
    // build index has been finished?
1923 1924 1925 1926 1927 1928 1929
    {
        std::lock_guard<std::mutex> lck(index_result_mutex_);
        if (!index_thread_results_.empty()) {
            std::chrono::milliseconds span(10);
            if (index_thread_results_.back().wait_for(span) == std::future_status::ready) {
                index_thread_results_.pop_back();
            }
G
groot 已提交
1930 1931 1932
        }
    }

S
starlord 已提交
1933
    // add new build index task
1934 1935 1936
    {
        std::lock_guard<std::mutex> lck(index_result_mutex_);
        if (index_thread_results_.empty()) {
S
starlord 已提交
1937
            index_thread_results_.push_back(index_thread_pool_.enqueue(&DBImpl::BackgroundBuildIndex, this));
1938
        }
G
groot 已提交
1939
    }
X
Xu Peng 已提交
1940 1941
}

S
starlord 已提交
1942 1943
void
DBImpl::BackgroundBuildIndex() {
P
peng.xu 已提交
1944
    std::unique_lock<std::mutex> lock(build_index_mutex_);
J
Jin Hai 已提交
1945
    meta::SegmentsSchema to_index_files;
G
groot 已提交
1946
    meta_ptr_->FilesToIndex(to_index_files);
1947
    Status status = index_failed_checker_.IgnoreFailedIndexFiles(to_index_files);
1948

1949
    if (!to_index_files.empty()) {
1950
        LOG_ENGINE_DEBUG_ << "Background build index thread begin";
1951
        status = OngoingFileChecker::GetInstance().MarkOngoingFiles(to_index_files);
1952

1953
        // step 2: put build index task to scheduler
J
Jin Hai 已提交
1954
        std::vector<std::pair<scheduler::BuildIndexJobPtr, scheduler::SegmentSchemaPtr>> job2file_map;
1955
        for (auto& file : to_index_files) {
G
groot 已提交
1956
            scheduler::BuildIndexJobPtr job = std::make_shared<scheduler::BuildIndexJob>(meta_ptr_, options_);
J
Jin Hai 已提交
1957
            scheduler::SegmentSchemaPtr file_ptr = std::make_shared<meta::SegmentSchema>(file);
1958
            job->AddToIndexFiles(file_ptr);
G
groot 已提交
1959
            scheduler::JobMgrInst::GetInstance()->Put(job);
G
groot 已提交
1960
            job2file_map.push_back(std::make_pair(job, file_ptr));
1961
        }
G
groot 已提交
1962

G
groot 已提交
1963
        // step 3: wait build index finished and mark failed files
G
groot 已提交
1964 1965
        for (auto iter = job2file_map.begin(); iter != job2file_map.end(); ++iter) {
            scheduler::BuildIndexJobPtr job = iter->first;
J
Jin Hai 已提交
1966
            meta::SegmentSchema& file_schema = *(iter->second.get());
G
groot 已提交
1967 1968 1969
            job->WaitBuildIndexFinish();
            if (!job->GetStatus().ok()) {
                Status status = job->GetStatus();
1970
                LOG_ENGINE_ERROR_ << "Building index job " << job->id() << " failed: " << status.ToString();
G
groot 已提交
1971

1972
                index_failed_checker_.MarkFailedIndexFile(file_schema, status.message());
G
groot 已提交
1973
            } else {
1974
                LOG_ENGINE_DEBUG_ << "Building index job " << job->id() << " succeed.";
G
groot 已提交
1975 1976

                index_failed_checker_.MarkSucceedIndexFile(file_schema);
G
groot 已提交
1977
            }
1978
            status = OngoingFileChecker::GetInstance().UnmarkOngoingFile(file_schema);
1979
        }
G
groot 已提交
1980

1981
        LOG_ENGINE_DEBUG_ << "Background build index thread finished";
Y
Yu Kun 已提交
1982
    }
X
Xu Peng 已提交
1983 1984
}

G
groot 已提交
1985
Status
J
Jin Hai 已提交
1986 1987
DBImpl::GetFilesToBuildIndex(const std::string& collection_id, const std::vector<int>& file_types,
                             meta::SegmentsSchema& files) {
G
groot 已提交
1988
    files.clear();
J
Jin Hai 已提交
1989
    auto status = meta_ptr_->FilesByType(collection_id, file_types, files);
G
groot 已提交
1990 1991 1992

    // only build index for files that row count greater than certain threshold
    for (auto it = files.begin(); it != files.end();) {
J
Jin Hai 已提交
1993
        if ((*it).file_type_ == static_cast<int>(meta::SegmentSchema::RAW) &&
G
groot 已提交
1994 1995 1996
            (*it).row_count_ < meta::BUILD_INDEX_THRESHOLD) {
            it = files.erase(it);
        } else {
1997
            ++it;
G
groot 已提交
1998 1999 2000 2001 2002 2003
        }
    }

    return Status::OK();
}

G
groot 已提交
2004
Status
J
Jin Hai 已提交
2005
DBImpl::GetFilesToSearch(const std::string& collection_id, meta::SegmentsSchema& files) {
2006
    LOG_ENGINE_DEBUG_ << "Collect files from collection: " << collection_id;
2007

J
Jin Hai 已提交
2008 2009
    meta::SegmentsSchema search_files;
    auto status = meta_ptr_->FilesToSearch(collection_id, search_files);
G
groot 已提交
2010 2011 2012 2013
    if (!status.ok()) {
        return status;
    }

2014 2015 2016
    for (auto& file : search_files) {
        files.push_back(file);
    }
G
groot 已提交
2017 2018 2019
    return Status::OK();
}

2020
Status
J
Jin Hai 已提交
2021 2022
DBImpl::GetPartitionByTag(const std::string& collection_id, const std::string& partition_tag,
                          std::string& partition_name) {
2023 2024 2025
    Status status;

    if (partition_tag.empty()) {
J
Jin Hai 已提交
2026
        partition_name = collection_id;
2027 2028 2029 2030 2031 2032 2033 2034

    } else {
        // trim side-blank of tag, only compare valid characters
        // for example: " ab cd " is treated as "ab cd"
        std::string valid_tag = partition_tag;
        server::StringHelpFunctions::TrimStringBlank(valid_tag);

        if (valid_tag == milvus::engine::DEFAULT_PARTITON_TAG) {
J
Jin Hai 已提交
2035
            partition_name = collection_id;
2036 2037 2038
            return status;
        }

J
Jin Hai 已提交
2039
        status = meta_ptr_->GetPartitionName(collection_id, partition_tag, partition_name);
2040
        if (!status.ok()) {
2041
            LOG_ENGINE_ERROR_ << status.message();
2042 2043 2044 2045 2046 2047
        }
    }

    return status;
}

G
groot 已提交
2048
Status
J
Jin Hai 已提交
2049
DBImpl::GetPartitionsByTags(const std::string& collection_id, const std::vector<std::string>& partition_tags,
G
groot 已提交
2050
                            std::set<std::string>& partition_name_array) {
J
Jin Hai 已提交
2051 2052
    std::vector<meta::CollectionSchema> partition_array;
    auto status = meta_ptr_->ShowPartitions(collection_id, partition_array);
G
groot 已提交
2053 2054

    for (auto& tag : partition_tags) {
2055 2056 2057 2058
        // trim side-blank of tag, only compare valid characters
        // for example: " ab cd " is treated as "ab cd"
        std::string valid_tag = tag;
        server::StringHelpFunctions::TrimStringBlank(valid_tag);
2059 2060

        if (valid_tag == milvus::engine::DEFAULT_PARTITON_TAG) {
J
Jin Hai 已提交
2061
            partition_name_array.insert(collection_id);
2062 2063 2064
            return status;
        }

G
groot 已提交
2065
        for (auto& schema : partition_array) {
2066
            if (server::StringHelpFunctions::IsRegexMatch(schema.partition_tag_, valid_tag)) {
J
Jin Hai 已提交
2067
                partition_name_array.insert(schema.collection_id_);
G
groot 已提交
2068 2069 2070 2071
            }
        }
    }

T
Tinkerrr 已提交
2072 2073 2074 2075
    if (partition_name_array.empty()) {
        return Status(PARTITION_NOT_FOUND, "Cannot find the specified partitions");
    }

G
groot 已提交
2076 2077 2078 2079
    return Status::OK();
}

Status
2080
DBImpl::DropCollectionRecursively(const std::string& collection_id) {
J
Jin Hai 已提交
2081
    // dates partly delete files of the collection but currently we don't support
2082
    LOG_ENGINE_DEBUG_ << "Prepare to delete collection " << collection_id;
G
groot 已提交
2083 2084

    Status status;
2085
    if (options_.wal_enable_) {
2086
        wal_mgr_->DropCollection(collection_id);
G
groot 已提交
2087 2088
    }

2089 2090 2091
    status = mem_mgr_->EraseMemVector(collection_id);   // not allow insert
    status = meta_ptr_->DropCollection(collection_id);  // soft delete collection
    index_failed_checker_.CleanFailedIndexFileOfCollection(collection_id);
2092

J
Jin Hai 已提交
2093
    // scheduler will determine when to delete collection files
2094
    auto nres = scheduler::ResMgrInst::GetInstance()->GetNumOfComputeResource();
J
Jin Hai 已提交
2095
    scheduler::DeleteJobPtr job = std::make_shared<scheduler::DeleteJob>(collection_id, meta_ptr_, nres);
2096 2097 2098
    scheduler::JobMgrInst::GetInstance()->Put(job);
    job->WaitAndDelete();

J
Jin Hai 已提交
2099 2100
    std::vector<meta::CollectionSchema> partition_array;
    status = meta_ptr_->ShowPartitions(collection_id, partition_array);
G
groot 已提交
2101
    for (auto& schema : partition_array) {
2102 2103
        status = DropCollectionRecursively(schema.collection_id_);
        fiu_do_on("DBImpl.DropCollectionRecursively.failed", status = Status(DB_ERROR, ""));
G
groot 已提交
2104 2105 2106 2107 2108 2109 2110 2111 2112
        if (!status.ok()) {
            return status;
        }
    }

    return Status::OK();
}

Status
2113
DBImpl::UpdateCollectionIndexRecursively(const std::string& collection_id, const CollectionIndex& index) {
J
Jin Hai 已提交
2114
    DropIndex(collection_id);
G
groot 已提交
2115

2116 2117
    auto status = meta_ptr_->UpdateCollectionIndex(collection_id, index);
    fiu_do_on("DBImpl.UpdateCollectionIndexRecursively.fail_update_collection_index",
S
shengjh 已提交
2118
              status = Status(DB_META_TRANSACTION_FAILED, ""));
G
groot 已提交
2119
    if (!status.ok()) {
2120
        LOG_ENGINE_ERROR_ << "Failed to update collection index info for collection: " << collection_id;
G
groot 已提交
2121 2122 2123
        return status;
    }

J
Jin Hai 已提交
2124 2125
    std::vector<meta::CollectionSchema> partition_array;
    status = meta_ptr_->ShowPartitions(collection_id, partition_array);
G
groot 已提交
2126
    for (auto& schema : partition_array) {
2127
        status = UpdateCollectionIndexRecursively(schema.collection_id_, index);
G
groot 已提交
2128 2129 2130 2131 2132 2133 2134 2135 2136
        if (!status.ok()) {
            return status;
        }
    }

    return Status::OK();
}

Status
2137
DBImpl::WaitCollectionIndexRecursively(const std::string& collection_id, const CollectionIndex& index) {
G
groot 已提交
2138 2139 2140
    // for IDMAP type, only wait all NEW file converted to RAW file
    // for other type, wait NEW/RAW/NEW_MERGE/NEW_INDEX/TO_INDEX files converted to INDEX files
    std::vector<int> file_types;
2141
    if (utils::IsRawIndexType(index.engine_type_)) {
G
groot 已提交
2142
        file_types = {
J
Jin Hai 已提交
2143 2144
            static_cast<int32_t>(meta::SegmentSchema::NEW),
            static_cast<int32_t>(meta::SegmentSchema::NEW_MERGE),
G
groot 已提交
2145 2146 2147
        };
    } else {
        file_types = {
J
Jin Hai 已提交
2148 2149 2150
            static_cast<int32_t>(meta::SegmentSchema::RAW),       static_cast<int32_t>(meta::SegmentSchema::NEW),
            static_cast<int32_t>(meta::SegmentSchema::NEW_MERGE), static_cast<int32_t>(meta::SegmentSchema::NEW_INDEX),
            static_cast<int32_t>(meta::SegmentSchema::TO_INDEX),
G
groot 已提交
2151 2152 2153 2154
        };
    }

    // get files to build index
2155 2156
    meta::SegmentsSchema collection_files;
    auto status = GetFilesToBuildIndex(collection_id, file_types, collection_files);
G
groot 已提交
2157 2158
    int times = 1;

2159
    while (!collection_files.empty()) {
2160
        LOG_ENGINE_DEBUG_ << "Non index files detected! Will build index " << times;
2161
        if (!utils::IsRawIndexType(index.engine_type_)) {
2162
            status = meta_ptr_->UpdateCollectionFilesToIndex(collection_id);
G
groot 已提交
2163 2164
        }

G
groot 已提交
2165
        std::this_thread::sleep_for(std::chrono::seconds(WAIT_BUILD_INDEX_INTERVAL));
2166
        GetFilesToBuildIndex(collection_id, file_types, collection_files);
2167
        ++times;
G
groot 已提交
2168

2169
        index_failed_checker_.IgnoreFailedIndexFiles(collection_files);
G
groot 已提交
2170 2171 2172
    }

    // build index for partition
J
Jin Hai 已提交
2173 2174
    std::vector<meta::CollectionSchema> partition_array;
    status = meta_ptr_->ShowPartitions(collection_id, partition_array);
G
groot 已提交
2175
    for (auto& schema : partition_array) {
2176 2177
        status = WaitCollectionIndexRecursively(schema.collection_id_, index);
        fiu_do_on("DBImpl.WaitCollectionIndexRecursively.fail_build_collection_Index_for_partition",
S
shengjh 已提交
2178
                  status = Status(DB_ERROR, ""));
G
groot 已提交
2179 2180 2181 2182 2183
        if (!status.ok()) {
            return status;
        }
    }

G
groot 已提交
2184
    // failed to build index for some files, return error
2185
    std::string err_msg;
2186 2187
    index_failed_checker_.GetErrMsgForCollection(collection_id, err_msg);
    fiu_do_on("DBImpl.WaitCollectionIndexRecursively.not_empty_err_msg", err_msg.append("fiu"));
2188 2189
    if (!err_msg.empty()) {
        return Status(DB_ERROR, err_msg);
G
groot 已提交
2190 2191
    }

G
groot 已提交
2192 2193 2194 2195
    return Status::OK();
}

Status
2196
DBImpl::DropCollectionIndexRecursively(const std::string& collection_id) {
2197
    LOG_ENGINE_DEBUG_ << "Drop index for collection: " << collection_id;
2198 2199
    index_failed_checker_.CleanFailedIndexFileOfCollection(collection_id);
    auto status = meta_ptr_->DropCollectionIndex(collection_id);
G
groot 已提交
2200 2201 2202 2203 2204
    if (!status.ok()) {
        return status;
    }

    // drop partition index
J
Jin Hai 已提交
2205 2206
    std::vector<meta::CollectionSchema> partition_array;
    status = meta_ptr_->ShowPartitions(collection_id, partition_array);
G
groot 已提交
2207
    for (auto& schema : partition_array) {
2208 2209
        status = DropCollectionIndexRecursively(schema.collection_id_);
        fiu_do_on("DBImpl.DropCollectionIndexRecursively.fail_drop_collection_Index_for_partition",
S
shengjh 已提交
2210
                  status = Status(DB_ERROR, ""));
G
groot 已提交
2211 2212 2213 2214 2215 2216 2217 2218 2219
        if (!status.ok()) {
            return status;
        }
    }

    return Status::OK();
}

Status
2220
DBImpl::GetCollectionRowCountRecursively(const std::string& collection_id, uint64_t& row_count) {
G
groot 已提交
2221
    row_count = 0;
J
Jin Hai 已提交
2222
    auto status = meta_ptr_->Count(collection_id, row_count);
G
groot 已提交
2223 2224 2225 2226 2227
    if (!status.ok()) {
        return status;
    }

    // get partition row count
J
Jin Hai 已提交
2228 2229
    std::vector<meta::CollectionSchema> partition_array;
    status = meta_ptr_->ShowPartitions(collection_id, partition_array);
G
groot 已提交
2230
    for (auto& schema : partition_array) {
G
groot 已提交
2231
        uint64_t partition_row_count = 0;
2232 2233
        status = GetCollectionRowCountRecursively(schema.collection_id_, partition_row_count);
        fiu_do_on("DBImpl.GetCollectionRowCountRecursively.fail_get_collection_rowcount_for_partition",
S
shengjh 已提交
2234
                  status = Status(DB_ERROR, ""));
G
groot 已提交
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244
        if (!status.ok()) {
            return status;
        }

        row_count += partition_row_count;
    }

    return Status::OK();
}

2245 2246 2247 2248
Status
DBImpl::ExecWalRecord(const wal::MXLogRecord& record) {
    fiu_return_on("DBImpl.ExexWalRecord.return", Status(););

2249 2250
    auto collections_flushed = [&](const std::set<std::string>& collection_ids) -> uint64_t {
        if (collection_ids.empty()) {
2251 2252 2253 2254 2255
            return 0;
        }

        uint64_t max_lsn = 0;
        if (options_.wal_enable_) {
2256
            for (auto& collection : collection_ids) {
2257
                uint64_t lsn = 0;
2258 2259
                meta_ptr_->GetCollectionFlushLSN(collection, lsn);
                wal_mgr_->CollectionFlushed(collection, lsn);
2260 2261 2262 2263 2264 2265 2266
                if (lsn > max_lsn) {
                    max_lsn = lsn;
                }
            }
        }

        std::lock_guard<std::mutex> lck(merge_result_mutex_);
2267 2268
        for (auto& collection : collection_ids) {
            merge_collection_ids_.insert(collection);
2269 2270 2271 2272 2273 2274 2275
        }
        return max_lsn;
    };

    Status status;

    switch (record.type) {
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
        case wal::MXLogType::Entity: {
            std::string target_collection_name;
            status = GetPartitionByTag(record.collection_id, record.partition_tag, target_collection_name);
            if (!status.ok()) {
                return status;
            }

            std::set<std::string> flushed_tables;
            status = mem_mgr_->InsertEntities(target_collection_name, record.length, record.ids,
                                              (record.data_size / record.length / sizeof(float)),
                                              (const float*)record.data, record.attr_nbytes, record.attr_data_size,
                                              record.attr_data, record.lsn, flushed_tables);
            collections_flushed(flushed_tables);

            milvus::server::CollectInsertMetrics metrics(record.length, status);
            break;
        }
2293
        case wal::MXLogType::InsertBinary: {
2294 2295
            std::string target_collection_name;
            status = GetPartitionByTag(record.collection_id, record.partition_tag, target_collection_name);
2296
            if (!status.ok()) {
2297
                LOG_WAL_ERROR_ << LogOut("[%s][%ld] ", "insert", 0) << "Get partition fail: " << status.message();
2298 2299 2300
                return status;
            }

2301 2302
            std::set<std::string> flushed_collections;
            status = mem_mgr_->InsertVectors(target_collection_name, record.length, record.ids,
2303
                                             (record.data_size / record.length / sizeof(uint8_t)),
2304
                                             (const u_int8_t*)record.data, record.lsn, flushed_collections);
2305
            // even though !status.ok, run
2306
            collections_flushed(flushed_collections);
2307 2308 2309 2310 2311 2312 2313

            // metrics
            milvus::server::CollectInsertMetrics metrics(record.length, status);
            break;
        }

        case wal::MXLogType::InsertVector: {
2314 2315
            std::string target_collection_name;
            status = GetPartitionByTag(record.collection_id, record.partition_tag, target_collection_name);
2316
            if (!status.ok()) {
2317
                LOG_WAL_ERROR_ << LogOut("[%s][%ld] ", "insert", 0) << "Get partition fail: " << status.message();
2318 2319 2320
                return status;
            }

2321 2322
            std::set<std::string> flushed_collections;
            status = mem_mgr_->InsertVectors(target_collection_name, record.length, record.ids,
2323
                                             (record.data_size / record.length / sizeof(float)),
2324
                                             (const float*)record.data, record.lsn, flushed_collections);
2325
            // even though !status.ok, run
2326
            collections_flushed(flushed_collections);
2327 2328 2329 2330 2331 2332 2333

            // metrics
            milvus::server::CollectInsertMetrics metrics(record.length, status);
            break;
        }

        case wal::MXLogType::Delete: {
J
Jin Hai 已提交
2334 2335
            std::vector<meta::CollectionSchema> partition_array;
            status = meta_ptr_->ShowPartitions(record.collection_id, partition_array);
2336 2337 2338 2339
            if (!status.ok()) {
                return status;
            }

2340
            std::vector<std::string> collection_ids{record.collection_id};
2341
            for (auto& partition : partition_array) {
2342 2343
                auto& partition_collection_id = partition.collection_id_;
                collection_ids.emplace_back(partition_collection_id);
2344 2345 2346
            }

            if (record.length == 1) {
2347
                for (auto& collection_id : collection_ids) {
J
Jin Hai 已提交
2348
                    status = mem_mgr_->DeleteVector(collection_id, *record.ids, record.lsn);
2349 2350 2351 2352 2353
                    if (!status.ok()) {
                        return status;
                    }
                }
            } else {
2354
                for (auto& collection_id : collection_ids) {
J
Jin Hai 已提交
2355
                    status = mem_mgr_->DeleteVectors(collection_id, record.length, record.ids, record.lsn);
2356 2357 2358 2359 2360 2361 2362 2363 2364
                    if (!status.ok()) {
                        return status;
                    }
                }
            }
            break;
        }

        case wal::MXLogType::Flush: {
J
Jin Hai 已提交
2365 2366 2367 2368
            if (!record.collection_id.empty()) {
                // flush one collection
                std::vector<meta::CollectionSchema> partition_array;
                status = meta_ptr_->ShowPartitions(record.collection_id, partition_array);
2369 2370 2371 2372
                if (!status.ok()) {
                    return status;
                }

2373
                std::vector<std::string> collection_ids{record.collection_id};
2374
                for (auto& partition : partition_array) {
2375 2376
                    auto& partition_collection_id = partition.collection_id_;
                    collection_ids.emplace_back(partition_collection_id);
2377 2378
                }

2379 2380
                std::set<std::string> flushed_collections;
                for (auto& collection_id : collection_ids) {
2381
                    const std::lock_guard<std::mutex> lock(flush_merge_compact_mutex_);
J
Jin Hai 已提交
2382
                    status = mem_mgr_->Flush(collection_id);
2383 2384 2385
                    if (!status.ok()) {
                        break;
                    }
2386
                    flushed_collections.insert(collection_id);
2387 2388
                }

2389
                collections_flushed(flushed_collections);
2390 2391

            } else {
2392 2393
                // flush all collections
                std::set<std::string> collection_ids;
2394 2395
                {
                    const std::lock_guard<std::mutex> lock(flush_merge_compact_mutex_);
2396
                    status = mem_mgr_->Flush(collection_ids);
2397 2398
                }

2399
                uint64_t lsn = collections_flushed(collection_ids);
2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
                if (options_.wal_enable_) {
                    wal_mgr_->RemoveOldFiles(lsn);
                }
            }
            break;
        }
    }

    return status;
}

void
G
groot 已提交
2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422
DBImpl::InternalFlush(const std::string& collection_id) {
    wal::MXLogRecord record;
    record.type = wal::MXLogType::Flush;
    record.collection_id = collection_id;
    ExecWalRecord(record);

    StartMergeTask();
}

void
DBImpl::BackgroundWalThread() {
2423
    SetThreadName("wal_thread");
2424 2425
    server::SystemInfo::GetInstance().Init();

2426
    std::chrono::system_clock::time_point next_auto_flush_time;
2427
    auto get_next_auto_flush_time = [&]() {
2428
        return std::chrono::system_clock::now() + std::chrono::seconds(options_.auto_flush_interval_);
2429
    };
2430 2431 2432
    if (options_.auto_flush_interval_ > 0) {
        next_auto_flush_time = get_next_auto_flush_time();
    }
2433 2434

    while (true) {
2435 2436
        if (options_.auto_flush_interval_ > 0) {
            if (std::chrono::system_clock::now() >= next_auto_flush_time) {
G
groot 已提交
2437
                InternalFlush();
2438 2439
                next_auto_flush_time = get_next_auto_flush_time();
            }
2440 2441
        }

G
groot 已提交
2442
        wal::MXLogRecord record;
2443 2444
        auto error_code = wal_mgr_->GetNextRecord(record);
        if (error_code != WAL_SUCCESS) {
2445
            LOG_ENGINE_ERROR_ << "WAL background GetNextRecord error";
2446 2447 2448 2449 2450 2451
            break;
        }

        if (record.type != wal::MXLogType::None) {
            ExecWalRecord(record);
            if (record.type == wal::MXLogType::Flush) {
G
groot 已提交
2452 2453
                // notify flush request to return
                flush_req_swn_.Notify();
2454 2455

                // if user flush all manually, update auto flush also
J
Jin Hai 已提交
2456
                if (record.collection_id.empty() && options_.auto_flush_interval_ > 0) {
2457 2458 2459 2460 2461 2462
                    next_auto_flush_time = get_next_auto_flush_time();
                }
            }

        } else {
            if (!initialized_.load(std::memory_order_acquire)) {
G
groot 已提交
2463 2464
                InternalFlush();
                flush_req_swn_.Notify();
2465 2466
                WaitMergeFileFinish();
                WaitBuildIndexFinish();
2467
                LOG_ENGINE_DEBUG_ << "WAL background thread exit";
2468 2469 2470
                break;
            }

2471
            if (options_.auto_flush_interval_ > 0) {
G
groot 已提交
2472
                swn_wal_.Wait_Until(next_auto_flush_time);
2473
            } else {
G
groot 已提交
2474
                swn_wal_.Wait();
2475
            }
2476 2477 2478 2479
        }
    }
}

G
groot 已提交
2480 2481
void
DBImpl::BackgroundFlushThread() {
2482
    SetThreadName("flush_thread");
G
groot 已提交
2483 2484 2485
    server::SystemInfo::GetInstance().Init();
    while (true) {
        if (!initialized_.load(std::memory_order_acquire)) {
2486
            LOG_ENGINE_DEBUG_ << "DB background flush thread exit";
G
groot 已提交
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503
            break;
        }

        InternalFlush();
        if (options_.auto_flush_interval_ > 0) {
            swn_flush_.Wait_For(std::chrono::seconds(options_.auto_flush_interval_));
        } else {
            swn_flush_.Wait();
        }
    }
}

void
DBImpl::BackgroundMetricThread() {
    server::SystemInfo::GetInstance().Init();
    while (true) {
        if (!initialized_.load(std::memory_order_acquire)) {
2504
            LOG_ENGINE_DEBUG_ << "DB background metric thread exit";
G
groot 已提交
2505 2506 2507 2508 2509 2510 2511 2512
            break;
        }

        swn_metric_.Wait_For(std::chrono::seconds(BACKGROUND_METRIC_INTERVAL));
        StartMetricTask();
    }
}

2513 2514 2515 2516 2517
void
DBImpl::OnCacheInsertDataChanged(bool value) {
    options_.insert_cache_immediately_ = value;
}

2518 2519 2520 2521 2522
void
DBImpl::OnUseBlasThresholdChanged(int64_t threshold) {
    faiss::distance_compute_blas_threshold = threshold;
}

S
starlord 已提交
2523 2524
}  // namespace engine
}  // namespace milvus