1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15
16 * \author Dianzhang.Chen
17 * \date Oct 2020
18 * \brief Interface of collection manager
19 */
20
21#pragma once
22
23#include <unordered_map>
24#include <brpc/channel.h>
25#include "collection_creator.h"
26#include "mysql_collection.h"
27
28namespace proxima {
29namespace be {
30namespace repository {
31
32class CollectionManager;
33using CollectionManagerPtr = std::shared_ptr<CollectionManager>;
34
35/*! Collection Manager
36 */
37// class CollectionManager {
38class CollectionManager {
39 public:
40 //! Constructor
41 explicit CollectionManager(CollectionCreatorPtr collection_creator)
42 : collection_creator_(std::move(collection_creator)) {}
43
44 //! Destructor
45 ~CollectionManager() = default;
46
47 public:
48 //! Init Collection Manager
49 int init();
50
51 //! Start repository
52 void start();
53
54 //! Stop repository
55 int stop();
56
57 //!Cleanup repository
58 int cleanup();
59
60 private:
61 //! Create Collection
62 int create_collection(const CollectionInfo &info);
63
64 //! Update Collection
65 int update_collection(const std::string &uuid);
66
67 //! Drop Collection
68 int drop_collection(const std::string &uuid);
69
70 //! Create Collections
71 void create_collections(const std::vector<CollectionInfo> &infos);
72
73 //! Update Collections
74 void update_collections(const std::vector<std::string> &uuids);
75
76 //! Drop Collections
77 void drop_collections(const std::vector<std::string> &uuids);
78
79 //! Classify Collections
80 void classify_collections(
81 const std::vector<CollectionInfo> &infos,
82 std::vector<CollectionInfo> *new_collection_infos,
83 std::vector<std::string> *old_collection_uuids,
84 std::vector<std::string> *expired_collection_uuids) const;
85
86 //! Load configs
87 void load_config();
88
89 //! Clean invalid collections
90 void clean_invalid_collections();
91
92 //! Stop all collections
93 void stop_collections();
94
95 //! Get all collections from index agent
96 int get_all_collections(std::vector<CollectionInfo> *infos);
97
98 //! Get sleep time
99 uint64_t get_sleep_time() const;
100
101 //! Check if collection is old
102 bool is_old_collection(const std::string &uuid,
103 uint32_t new_schema_revision) const;
104
105 //! Filter collections by collection status
106 void filter_collections(const std::vector<CollectionInfo> &infos,
107 std::vector<CollectionInfo> *valid_infos) const;
108
109 //! Check server version
110 int check_server_version();
111
112 private:
113 //! Disable copy constructor
114 CollectionManager(const CollectionManager &) = delete;
115
116 //! Disable assignment operator
117 CollectionManager &operator=(const CollectionManager &) = delete;
118
119 private:
120 //! Members of Collections
121 std::unordered_map<std::string, CollectionPtr> collections_{};
122 std::unordered_map<std::string, std::string> uuid_name_map_{};
123
124 std::atomic<bool> stop_{false};
125 //! Mutex
126 std::mutex mutex_{};
127
128 //! Members of brpc
129 brpc::Channel channel_{};
130 brpc::ChannelOptions options_{};
131 std::string index_server_uri_{};
132 int max_retry_{0};
133 int timeout_ms_{0};
134 std::string load_balance_{};
135 std::string index_agent_addr_{};
136 std::string repository_name_{};
137
138 CollectionCreatorPtr collection_creator_{};
139};
140
141} // end namespace repository
142} // namespace be
143} // end namespace proxima
144