|
| 1 | +/* Copyright 2017 Google Inc. All Rights Reserved. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +#include "contrib/endpoints/src/api_manager/config_manager.h" |
| 16 | +#include "contrib/endpoints/src/api_manager/fetch_metadata.h" |
| 17 | + |
| 18 | +namespace google { |
| 19 | +namespace api_manager { |
| 20 | + |
| 21 | +namespace { |
| 22 | +// Default rollouts refresh interval in ms |
| 23 | +const int kCheckNewRolloutInterval = 60000; |
| 24 | + |
| 25 | +// static configs for error handling |
| 26 | +static std::vector<std::pair<std::string, int>> kEmptyConfigs; |
| 27 | +} // namespace anonymous |
| 28 | + |
| 29 | +ConfigManager::ConfigManager( |
| 30 | + std::shared_ptr<context::GlobalContext> global_context, |
| 31 | + RolloutApplyFunction rollout_apply_function) |
| 32 | + : global_context_(global_context), |
| 33 | + rollout_apply_function_(rollout_apply_function), |
| 34 | + refresh_interval_ms_(kCheckNewRolloutInterval) { |
| 35 | + if (global_context_->server_config()->has_service_management_config()) { |
| 36 | + // update refresh interval in ms |
| 37 | + if (global_context_->server_config() |
| 38 | + ->service_management_config() |
| 39 | + .refresh_interval_ms() > 0) { |
| 40 | + refresh_interval_ms_ = global_context_->server_config() |
| 41 | + ->service_management_config() |
| 42 | + .refresh_interval_ms(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + service_management_fetch_.reset(new ServiceManagementFetch(global_context)); |
| 47 | +} |
| 48 | + |
| 49 | +void ConfigManager::Init() { |
| 50 | + if (global_context_->service_name().empty() || |
| 51 | + global_context_->config_id().empty()) { |
| 52 | + GlobalFetchGceMetadata(global_context_, [this](utils::Status status) { |
| 53 | + OnFetchMetadataDone(status); |
| 54 | + }); |
| 55 | + } else { |
| 56 | + OnFetchMetadataDone(utils::Status::OK); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +void ConfigManager::OnFetchMetadataDone(utils::Status status) { |
| 61 | + if (!status.ok()) { |
| 62 | + // We should not get here |
| 63 | + global_context_->env()->LogError("Unexpected status: " + status.ToString()); |
| 64 | + rollout_apply_function_(utils::Status(Code::ABORTED, status.ToString()), |
| 65 | + kEmptyConfigs); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // Update service_name |
| 70 | + if (global_context_->service_name().empty()) { |
| 71 | + global_context_->set_service_name( |
| 72 | + global_context_->gce_metadata()->endpoints_service_name()); |
| 73 | + } |
| 74 | + |
| 75 | + // Update config_id |
| 76 | + if (global_context_->config_id().empty()) { |
| 77 | + global_context_->config_id( |
| 78 | + global_context_->gce_metadata()->endpoints_service_config_id()); |
| 79 | + } |
| 80 | + |
| 81 | + // Call ApiManager with status Code::ABORTED, ESP will stop moving forward |
| 82 | + if (global_context_->service_name().empty()) { |
| 83 | + std::string msg = "API service name is not specified"; |
| 84 | + global_context_->env()->LogError(msg); |
| 85 | + rollout_apply_function_(utils::Status(Code::ABORTED, msg), kEmptyConfigs); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + // TODO(jaebong) config_id should not be empty for the first version |
| 90 | + // This part will be removed after the rollouts feature added |
| 91 | + if (global_context_->config_id().empty()) { |
| 92 | + std::string msg = "API config_id is not specified"; |
| 93 | + global_context_->env()->LogError(msg); |
| 94 | + rollout_apply_function_(utils::Status(Code::ABORTED, msg), kEmptyConfigs); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // Fetch service account token |
| 99 | + GlobalFetchServiceAccountToken(global_context_, [this](utils::Status status) { |
| 100 | + OnFetchAuthTokenDone(status); |
| 101 | + }); |
| 102 | +} |
| 103 | + |
| 104 | +void ConfigManager::OnFetchAuthTokenDone(utils::Status status) { |
| 105 | + if (!status.ok()) { |
| 106 | + // We should not get here |
| 107 | + global_context_->env()->LogError("Unexpected status: " + status.ToString()); |
| 108 | + rollout_apply_function_(utils::Status(Code::ABORTED, status.ToString()), |
| 109 | + kEmptyConfigs); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + // Fetch configs from the Inception |
| 114 | + // For now, config manager has only one config_id (100% rollout) |
| 115 | + std::shared_ptr<ConfigsFetchInfo> config_fetch_info( |
| 116 | + new ConfigsFetchInfo({{global_context_->config_id(), 100}})); |
| 117 | + |
| 118 | + FetchConfigs(config_fetch_info); |
| 119 | +} |
| 120 | + |
| 121 | +// Fetch configs from rollouts. fetch_info has rollouts and fetched configs |
| 122 | +void ConfigManager::FetchConfigs( |
| 123 | + std::shared_ptr<ConfigsFetchInfo> config_fetch_info) { |
| 124 | + for (auto rollout : config_fetch_info->rollouts) { |
| 125 | + std::string config_id = rollout.first; |
| 126 | + int percentage = rollout.second; |
| 127 | + service_management_fetch_->GetConfig(config_id, [this, config_id, |
| 128 | + percentage, |
| 129 | + config_fetch_info]( |
| 130 | + const utils::Status& |
| 131 | + status, |
| 132 | + std::string&& config) { |
| 133 | + |
| 134 | + if (status.ok()) { |
| 135 | + config_fetch_info->configs.push_back({std::move(config), percentage}); |
| 136 | + } else { |
| 137 | + global_context_->env()->LogError(std::string( |
| 138 | + "Unable to download Service config for the config_id: " + |
| 139 | + config_id)); |
| 140 | + } |
| 141 | + |
| 142 | + config_fetch_info->finished++; |
| 143 | + |
| 144 | + if (config_fetch_info->IsCompleted()) { |
| 145 | + // Failed to fetch all configs or rollouts are empty |
| 146 | + if (config_fetch_info->IsRolloutsEmpty() || |
| 147 | + config_fetch_info->IsConfigsEmpty()) { |
| 148 | + // first time, call the ApiManager callback function with an error |
| 149 | + rollout_apply_function_( |
| 150 | + utils::Status(Code::ABORTED, |
| 151 | + "Failed to download the service config"), |
| 152 | + kEmptyConfigs); |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + // Update ApiManager |
| 157 | + rollout_apply_function_(utils::Status::OK, config_fetch_info->configs); |
| 158 | + } |
| 159 | + }); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +} // namespace api_manager |
| 164 | +} // namespace google |
0 commit comments