Skip to content

Commit dd55d3d

Browse files
ApiManager uses ConfigManager to download the service config (istio#343)
* ApiManager uses ConfigManager to download the service config * Removed uncessary header file inclusion * Moved all initialization into Init() * Reverted copyright year to 2016 * Reverted copyright year to 2016 * Added a local auto variable for ServiceContext creation * Fixed backward compatibility * Added the rollout_strategy check * Added service_config argument to ConfigManager * Moved service_config_ variable to GlobalContext class * Fixed code formatting * Reverted storing service_config in GlobalContext * Renamed and changed data type of const * Moved callback function argument from constructor to Init
1 parent 0f25dc8 commit dd55d3d

File tree

7 files changed

+329
-106
lines changed

7 files changed

+329
-106
lines changed

contrib/endpoints/src/api_manager/api_manager_impl.cc

Lines changed: 82 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,68 @@
2121
namespace google {
2222
namespace api_manager {
2323

24+
namespace {
25+
26+
const std::string kConfigRolloutManaged("managed");
27+
28+
} // namespace unknown
29+
2430
ApiManagerImpl::ApiManagerImpl(std::unique_ptr<ApiManagerEnvInterface> env,
2531
const std::string &service_config,
2632
const std::string &server_config)
2733
: global_context_(
28-
new context::GlobalContext(std::move(env), server_config)) {
34+
new context::GlobalContext(std::move(env), server_config)),
35+
config_loading_status_(
36+
utils::Status(Code::UNAVAILABLE, "Not initialized yet")) {
2937
if (!service_config.empty()) {
30-
AddConfig(service_config, true);
38+
std::string config_id;
39+
if (AddConfig(service_config, false, &config_id).ok()) {
40+
DeployConfigs({{config_id, 100}});
41+
config_loading_status_ = utils::Status::OK;
42+
} else {
43+
config_loading_status_ =
44+
utils::Status(Code::ABORTED, "Invalid service config");
45+
}
3146
}
3247

3348
check_workflow_ = std::unique_ptr<CheckWorkflow>(new CheckWorkflow);
3449
check_workflow_->RegisterAll();
3550
}
3651

37-
void ApiManagerImpl::AddConfig(const std::string &service_config,
38-
bool deploy_it) {
52+
utils::Status ApiManagerImpl::AddConfig(const std::string &service_config,
53+
bool initialize,
54+
std::string *config_id) {
3955
std::unique_ptr<Config> config =
4056
Config::Create(global_context_->env(), service_config);
41-
if (config != nullptr) {
42-
std::string service_name = config->service().name();
43-
if (global_context_->service_name().empty()) {
44-
global_context_->set_service_name(service_name);
45-
} else {
46-
if (service_name != global_context_->service_name()) {
47-
auto err_msg = std::string("Mismatched service name; existing: ") +
48-
global_context_->service_name() + ", new: " +
49-
service_name;
50-
global_context_->env()->LogError(err_msg);
51-
return;
52-
}
53-
}
54-
std::string config_id = config->service().id();
55-
service_context_map_[config_id] = std::make_shared<context::ServiceContext>(
56-
global_context_, std::move(config));
57-
// TODO: if this function is called at worker process, need to call
58-
// service_context->service_control()->Init().
59-
// ApiManagerImpl constructor is called at master process, not at worker
60-
// process.
61-
if (deploy_it) {
62-
DeployConfigs({{config_id, 0}});
57+
if (config == nullptr) {
58+
std::string err_msg =
59+
std::string("Invalid service config: ") + service_config;
60+
global_context_->env()->LogError(err_msg);
61+
return utils::Status(Code::INVALID_ARGUMENT, err_msg);
62+
}
63+
64+
std::string service_name = config->service().name();
65+
if (global_context_->service_name().empty()) {
66+
global_context_->set_service_name(service_name);
67+
} else {
68+
if (service_name != global_context_->service_name()) {
69+
auto err_msg = std::string("Mismatched service name; existing: ") +
70+
global_context_->service_name() + ", new: " + service_name;
71+
global_context_->env()->LogError(err_msg);
72+
return utils::Status(Code::INVALID_ARGUMENT, err_msg);
6373
}
6474
}
75+
76+
*config_id = config->service().id();
77+
78+
auto context_service = std::make_shared<context::ServiceContext>(
79+
global_context_, std::move(config));
80+
if (initialize == true && context_service->service_control()) {
81+
context_service->service_control()->Init();
82+
}
83+
service_context_map_[*config_id] = context_service;
84+
85+
return utils::Status::OK;
6586
}
6687

6788
// Deploy these configs according to the traffic percentage.
@@ -75,11 +96,43 @@ utils::Status ApiManagerImpl::Init() {
7596
global_context_->cloud_trace_aggregator()->Init();
7697
}
7798

78-
for (auto it : service_context_map_) {
79-
if (it.second->service_control()) {
80-
it.second->service_control()->Init();
99+
if (!service_context_map_.empty()) {
100+
for (auto it : service_context_map_) {
101+
if (it.second->service_control()) {
102+
it.second->service_control()->Init();
103+
}
104+
}
105+
106+
if (global_context_->rollout_strategy() != kConfigRolloutManaged) {
107+
return config_loading_status_;
81108
}
82109
}
110+
111+
config_manager_.reset(new ConfigManager(global_context_));
112+
config_manager_->Init(
113+
[this](const utils::Status &status,
114+
const std::vector<std::pair<std::string, int>> &configs) {
115+
if (status.ok()) {
116+
std::vector<std::pair<std::string, int>> rollouts;
117+
118+
for (auto item : configs) {
119+
std::string config_id;
120+
if (AddConfig(item.first, true, &config_id).ok()) {
121+
rollouts.push_back({config_id, item.second});
122+
}
123+
}
124+
125+
if (rollouts.size() == 0) {
126+
config_loading_status_ =
127+
utils::Status(Code::ABORTED, "Invalid service config");
128+
return;
129+
}
130+
131+
DeployConfigs(std::move(rollouts));
132+
}
133+
config_loading_status_ = status;
134+
});
135+
83136
return utils::Status::OK;
84137
}
85138

contrib/endpoints/src/api_manager/api_manager_impl.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define API_MANAGER_API_MANAGER_IMPL_H_
1717

1818
#include "contrib/endpoints/include/api_manager/api_manager.h"
19+
#include "contrib/endpoints/src/api_manager/config_manager.h"
1920
#include "contrib/endpoints/src/api_manager/context/global_context.h"
2021
#include "contrib/endpoints/src/api_manager/context/service_context.h"
2122
#include "contrib/endpoints/src/api_manager/service_control/interface.h"
@@ -52,11 +53,17 @@ class ApiManagerImpl : public ApiManager {
5253
utils::Status GetStatistics(ApiManagerStatistics *statistics) const override;
5354

5455
// Add a new service config.
55-
void AddConfig(const std::string &service_config, bool deploy_it);
56+
// Return true if service_config is valid, otherwise return false.
57+
// config_id will be updated when the deployment was successful
58+
utils::Status AddConfig(const std::string &service_config, bool initialize,
59+
std::string *config_id);
5660

5761
// Use these configs according to the traffic percentage.
5862
void DeployConfigs(std::vector<std::pair<std::string, int>> &&list);
5963

64+
// Return the initialization status
65+
inline utils::Status ConfigLoadingStatus() { return config_loading_status_; }
66+
6067
private:
6168
// The check work flow.
6269
std::shared_ptr<CheckWorkflow> check_workflow_;
@@ -73,6 +80,14 @@ class ApiManagerImpl : public ApiManager {
7380

7481
// A weighted service selector.
7582
std::unique_ptr<WeightedSelector> service_selector_;
83+
84+
// A config manager
85+
std::unique_ptr<ConfigManager> config_manager_;
86+
87+
// - Code::UNAVAILABLE Not initialized yet. The default value.
88+
// - Code::OK Successfully initialized
89+
// - Code::ABORTED Initialization was failed
90+
utils::Status config_loading_status_;
7691
};
7792

7893
} // namespace api_manager

0 commit comments

Comments
 (0)