PreloadCollectionRequest.cpp 3.3 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
G
groot 已提交
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
G
groot 已提交
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.
G
groot 已提交
11

12
#include "server/delivery/request/PreloadCollectionRequest.h"
G
groot 已提交
13
#include "server/DBWrapper.h"
C
Cai Yudong 已提交
14
#include "server/ValidationUtil.h"
G
groot 已提交
15 16 17
#include "utils/Log.h"
#include "utils/TimeRecorder.h"

S
shengjh 已提交
18
#include <fiu-local.h>
G
groot 已提交
19
#include <memory>
20 21
#include <unordered_map>
#include <vector>
G
groot 已提交
22 23 24 25

namespace milvus {
namespace server {

26 27 28
PreloadCollectionRequest::PreloadCollectionRequest(const std::shared_ptr<milvus::server::Context>& context,
                                                   const std::string& collection_name)
    : BaseRequest(context, BaseRequest::kPreloadCollection), collection_name_(collection_name) {
G
groot 已提交
29 30 31
}

BaseRequestPtr
32 33 34
PreloadCollectionRequest::Create(const std::shared_ptr<milvus::server::Context>& context,
                                 const std::string& collection_name) {
    return std::shared_ptr<BaseRequest>(new PreloadCollectionRequest(context, collection_name));
G
groot 已提交
35 36 37
}

Status
38
PreloadCollectionRequest::OnExecute() {
G
groot 已提交
39
    try {
40
        std::string hdr = "PreloadCollectionRequest(collection=" + collection_name_ + ")";
G
groot 已提交
41
        TimeRecorderAuto rc(hdr);
G
groot 已提交
42 43

        // step 1: check arguments
C
Cai Yudong 已提交
44
        auto status = ValidateCollectionName(collection_name_);
G
groot 已提交
45 46 47 48
        if (!status.ok()) {
            return status;
        }

J
Jin Hai 已提交
49
        // only process root collection, ignore partition collection
50 51 52
        engine::snapshot::CollectionPtr collection;
        std::unordered_map<engine::snapshot::FieldPtr, std::vector<engine::snapshot::FieldElementPtr>> fields_schema;
        status = DBWrapper::SSDB()->DescribeCollection(collection_name_, collection, fields_schema);
53 54
        if (!status.ok()) {
            if (status.code() == DB_NOT_FOUND) {
G
groot 已提交
55
                return Status(SERVER_COLLECTION_NOT_EXIST, CollectionNotExistMsg(collection_name_));
56 57 58
            } else {
                return status;
            }
59 60 61 62 63 64
        }

        // TODO(yukun): if PreloadCollection interface needs to add field names as params
        std::vector<std::string> field_names;
        for (auto field_it : fields_schema) {
            field_names.emplace_back(field_it.first->GetName());
65 66
        }

G
groot 已提交
67 68
        // step 2: force load collection data into cache
        // load each segment and insert into cache even cache capacity is not enough
69
        status = DBWrapper::SSDB()->LoadCollection(context_, collection_name_, field_names, true);
G
groot 已提交
70
        fiu_do_on("PreloadCollectionRequest.OnExecute.preload_collection_fail",
S
shengjh 已提交
71
                  status = Status(milvus::SERVER_UNEXPECTED_ERROR, ""));
72
        fiu_do_on("PreloadCollectionRequest.OnExecute.throw_std_exception", throw std::exception());
G
groot 已提交
73 74 75 76 77 78 79 80 81 82 83 84
        if (!status.ok()) {
            return status;
        }
    } catch (std::exception& ex) {
        return Status(SERVER_UNEXPECTED_ERROR, ex.what());
    }

    return Status::OK();
}

}  // namespace server
}  // namespace milvus