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 guonix |
17 | * \date Dec 2020 |
18 | * \brief |
19 | */ |
20 | |
21 | #include "meta_agent.h" |
22 | #include "common/config.h" |
23 | #include "common/error_code.h" |
24 | #include "common/logger.h" |
25 | #include "meta_service_builder.h" |
26 | |
27 | namespace proxima { |
28 | namespace be { |
29 | namespace meta { |
30 | |
31 | /*! MetaAgent implementation |
32 | */ |
33 | class MetaAgentImpl : public MetaAgent { |
34 | public: |
35 | //! Constructor |
36 | explicit MetaAgentImpl(MetaServicePtr meta_service) |
37 | : meta_service_(std::move(meta_service)) {} |
38 | |
39 | //! Destructor |
40 | ~MetaAgentImpl() override = default; |
41 | |
42 | public: |
43 | //! Get Meta Service Instance |
44 | MetaServicePtr get_service() const override { |
45 | return meta_service_; |
46 | } |
47 | |
48 | public: |
49 | //! Init Meta Agent |
50 | int init() override { |
51 | int error_code = meta_service_->init(); |
52 | if (error_code == 0) { |
53 | LOG_INFO("MetaAgent initialize complete." ); |
54 | } else { |
55 | LOG_ERROR("MetaAgent initialize failed. code[%d] what[%s]" , error_code, |
56 | ErrorCode::What(error_code)); |
57 | } |
58 | return error_code; |
59 | } |
60 | |
61 | //! Clean up object |
62 | int cleanup() override { |
63 | int error_code = meta_service_->cleanup(); |
64 | if (error_code == 0) { |
65 | LOG_INFO("MetaAgent cleanup complete. " ); |
66 | } else { |
67 | LOG_ERROR("MetaAgent cleanup failed. code[%d] what[%s]" , error_code, |
68 | ErrorCode::What(error_code)); |
69 | } |
70 | return error_code; |
71 | } |
72 | |
73 | //! Start background service |
74 | int start() override { |
75 | int error_code = meta_service_->start(); |
76 | if (error_code == 0) { |
77 | LOG_INFO("MetaAgent start complete." ); |
78 | } else { |
79 | LOG_ERROR("MetaAgent start failed. code[%d] what[%s]" , error_code, |
80 | ErrorCode::What(error_code)); |
81 | } |
82 | return error_code; |
83 | } |
84 | |
85 | //! Stop background service |
86 | int stop() override { |
87 | int error_code = meta_service_->stop(); |
88 | if (error_code == 0) { |
89 | LOG_INFO("MetaAgent stopped." ); |
90 | } else { |
91 | LOG_ERROR("MetaAgent stop failed. code[%d] what[%s]" , error_code, |
92 | ErrorCode::What(error_code)); |
93 | } |
94 | return error_code; |
95 | } |
96 | |
97 | public: |
98 | //! Reload meta service |
99 | int reload() override { |
100 | int error_code = meta_service_->reload(); |
101 | if (error_code == 0) { |
102 | LOG_INFO("MetaAgent reloaded." ); |
103 | } else { |
104 | LOG_ERROR("MetaAgent reload failed. code[%d] what[%s]" , error_code, |
105 | ErrorCode::What(error_code)); |
106 | } |
107 | return error_code; |
108 | } |
109 | |
110 | //! Create collection |
111 | int create_collection(const CollectionBase ¶m, |
112 | CollectionMetaPtr *meta) override { |
113 | int code = meta_service_->create_collection(param, meta); |
114 | if (code != 0) { |
115 | LOG_ERROR("Failed to create collection: code[%d], what[%s]," , code, |
116 | ErrorCode::What(code)); |
117 | } |
118 | return code; |
119 | } |
120 | |
121 | //! Update collection |
122 | int update_collection(const CollectionBase ¶m, |
123 | CollectionMetaPtr *meta) override { |
124 | int code = meta_service_->update_collection(param, meta); |
125 | if (code != 0) { |
126 | LOG_ERROR("Failed to update collection: code[%d], what[%s]," , code, |
127 | ErrorCode::What(code)); |
128 | } |
129 | return code; |
130 | } |
131 | |
132 | //! Update the status of current used collection |
133 | int update_status(const std::string &collection_name, |
134 | CollectionStatus status) override { |
135 | int code = meta_service_->update_status(collection_name, status); |
136 | if (code != 0) { |
137 | LOG_ERROR("Failed to update collection: code[%d], what[%s]," , code, |
138 | ErrorCode::What(code)); |
139 | } |
140 | return code; |
141 | } |
142 | |
143 | //! Enable collection |
144 | int enable_collection(const std::string &collection, |
145 | uint32_t revision) override { |
146 | int code = meta_service_->enable_collection(collection, revision, true); |
147 | if (code != 0) { |
148 | LOG_ERROR("Failed to enable collection. code[%d] what[%s]" , code, |
149 | ErrorCode::What(code)); |
150 | } |
151 | return code; |
152 | } |
153 | |
154 | //! Suspend reading requests of collection |
155 | int suspend_collection_read(const std::string &collection_name) override { |
156 | return meta_service_->suspend_collection_read(collection_name); |
157 | } |
158 | |
159 | //! Resume reading requests of collection |
160 | int resume_collection_read(const std::string &collection_name) override { |
161 | return meta_service_->resume_collection_read(collection_name); |
162 | } |
163 | |
164 | //! Suspend writing requests of collection |
165 | int suspend_collection_write(const std::string &collection_name) override { |
166 | return meta_service_->suspend_collection_write(collection_name); |
167 | } |
168 | |
169 | //! Resume writing requests of collection |
170 | int resume_collection_write(const std::string &collection_name) override { |
171 | return meta_service_->resume_collection_write(collection_name); |
172 | } |
173 | |
174 | //! Delete collection |
175 | int delete_collection(const std::string &collection) override { |
176 | if (collection.empty()) { |
177 | LOG_ERROR("Collection name can't be empty" ); |
178 | return PROXIMA_BE_ERROR_CODE(InvalidArgument); |
179 | } |
180 | int code = meta_service_->drop_collection(collection); |
181 | if (code != 0) { |
182 | LOG_ERROR("Drop collection failed: code[%d], what[%s]," , code, |
183 | ErrorCode::What(code)); |
184 | } |
185 | return code; |
186 | } |
187 | |
188 | //! Retrieve collections |
189 | int list_collections(CollectionMetaPtrList *collections) const override { |
190 | if (!collections) { |
191 | return PROXIMA_BE_ERROR_CODE(InvalidArgument); |
192 | } |
193 | int code = meta_service_->get_latest_collections(collections); |
194 | if (code != 0) { |
195 | LOG_ERROR("Failed to list collections, code[%d], what[%s]." , code, |
196 | ErrorCode::What(code)); |
197 | } |
198 | return code; |
199 | } |
200 | |
201 | //! Retrieve collections |
202 | int get_collection_history( |
203 | const std::string &name, |
204 | CollectionMetaPtrList *collections) const override { |
205 | if (name.empty()) { |
206 | LOG_ERROR("Collection name can't be empty" ); |
207 | return PROXIMA_BE_ERROR_CODE(InvalidArgument); |
208 | } |
209 | int code = meta_service_->get_collections(name, collections); |
210 | if (code != 0) { |
211 | LOG_ERROR("Failed to list collections, code[%d], what[%s]." , code, |
212 | ErrorCode::What(code)); |
213 | } |
214 | return code; |
215 | } |
216 | |
217 | //! Check collection exists |
218 | CollectionMetaPtr get_collection(const std::string &name) const override { |
219 | if (name.empty()) { |
220 | LOG_ERROR("Collection name can't be empty" ); |
221 | return nullptr; |
222 | } |
223 | return meta_service_->get_current_collection(name); |
224 | } |
225 | |
226 | //! Check collection exists |
227 | bool exist_collection(const std::string &name) const override { |
228 | if (name.empty()) { |
229 | LOG_ERROR("Collection name can't be empty" ); |
230 | return false; |
231 | } |
232 | return meta_service_->exist_collection(name); |
233 | } |
234 | |
235 | private: |
236 | MetaServicePtr meta_service_{nullptr}; |
237 | }; |
238 | |
239 | //! Create one MetaAgent instance |
240 | MetaAgentPtr MetaAgent::Create(const std::string &uri) { |
241 | return MetaAgent::Create(MetaServiceBuilder::Create(uri)); |
242 | } |
243 | |
244 | MetaAgentPtr MetaAgent::Create(const MetaServicePtr &meta_service) { |
245 | if (meta_service) { |
246 | return std::make_shared<MetaAgentImpl>(meta_service); |
247 | } |
248 | LOG_ERROR("Failed to create MetaService, invalid arguments of meta_service" ); |
249 | return nullptr; |
250 | } |
251 | |
252 | } // namespace meta |
253 | } // namespace be |
254 | } // namespace proxima |
255 | |