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 Dec 2020
18 * \brief Implementation of MysqlRepository
19 */
20
21#include "mysql_repository.h"
22#include <ailego/utility/process_helper.h>
23#include "repository/collection_creator.h"
24#include "repository/repository_common/config.h"
25#include "repository/repository_common/error_code.h"
26#include "repository_common/logger.h"
27
28namespace proxima {
29namespace be {
30namespace repository {
31
32int MysqlRepository::init(bool daemonized, const std::string &pid_file) {
33 if (!pid_file.empty() && !pid_file_.open(pid_file)) {
34 LOG_ERROR("ProximaSE open the pid file failed, pid_file=[%s].",
35 pid_file.c_str());
36 return ErrorCode_OpenFile;
37 }
38
39 LOG_INFO("Start to init repository");
40 daemonized_ = daemonized;
41
42 // init logger
43 int ret = init_logger();
44 if (ret != 0) {
45 LOG_ERROR("Mysql repository init logger error");
46 return ret;
47 }
48
49 // init meta agent
50 CollectionCreatorPtr collection_creator =
51 std::make_shared<CollectionCreatorImpl>();
52 collection_manager_ = std::make_shared<CollectionManager>(collection_creator);
53 ret = collection_manager_->init();
54 if (ret != 0) {
55 LOG_ERROR("Init collection manager failed.");
56 return ret;
57 }
58
59 return 0;
60}
61
62int MysqlRepository::cleanup() {
63 // add lock protection
64 // cleanup maybe be called in multithread
65 std::lock_guard<std::mutex> lock(mutex_);
66
67 if (collection_manager_ != nullptr) {
68 collection_manager_->cleanup();
69 collection_manager_.reset();
70 }
71
72 LogUtil::Shutdown();
73 Config::Instance().cleanup();
74
75 daemonized_ = false;
76
77 return 0;
78}
79
80int MysqlRepository::start() {
81 if (daemonized_) {
82 daemonize();
83 }
84
85 // start collection manager
86 // todo: will not return, unless finished(because of while loop), so can i use
87 // another thread to start? to review
88 collection_manager_->start();
89
90 is_running_ = true;
91 LOG_INFO("Mysql repository start successfully.");
92 return 0;
93}
94
95int MysqlRepository::stop() {
96 // add lock protection
97 // stop maybe be called in multithread
98 std::lock_guard<std::mutex> lock(mutex_);
99 if (!is_running_) {
100 return 0;
101 }
102
103 if (collection_manager_ != nullptr) {
104 collection_manager_->stop();
105 }
106
107 pid_file_.close();
108 is_running_ = false;
109
110 return 0;
111}
112
113int MysqlRepository::init_logger() {
114 // get logger config
115 std::string log_dir = repository::Config::Instance().get_log_dir();
116 std::string log_file = repository::Config::Instance().get_log_file();
117 uint32_t log_level = repository::Config::Instance().get_log_level();
118 auto logger_name = repository::Config::Instance().get_logger_name();
119
120 // int logger
121 return LogUtil::Init(log_dir, log_file, log_level, logger_name);
122}
123
124void MysqlRepository::daemonize() {
125 std::string log_dir = Config::Instance().get_log_dir();
126 std::string stdout_path = log_dir + "./stdout.log";
127 std::string stderr_path = log_dir + "./stderr.log";
128 ailego::ProcessHelper::Daemon(stdout_path.c_str(), stderr_path.c_str());
129}
130
131} // namespace repository
132} // namespace be
133} // end namespace proxima
134