2121namespace google {
2222namespace api_manager {
2323
24+ namespace {
25+
26+ const std::string kConfigRolloutManaged (" managed" );
27+
28+ } // namespace unknown
29+
2430ApiManagerImpl::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
0 commit comments