config_manager.cpp 9.3 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

G
guru4elephant 已提交
15
#include "core/sdk-cpp/include/config_manager.h"
W
wangguibao 已提交
16 17 18
#ifdef BCLOUD
#include "baidu/rpc/server.h"
#else
W
wangguibao 已提交
19
#include "brpc/server.h"
W
wangguibao 已提交
20
#endif
G
guru4elephant 已提交
21
#include "core/sdk-cpp/include/abtest.h"
W
sdk-cpp  
wangguibao 已提交
22 23 24 25 26

namespace baidu {
namespace paddle_serving {
namespace sdk_cpp {

W
wangguibao 已提交
27 28
using configure::SDKConf;

G
guru4elephant 已提交
29 30 31 32 33 34 35
int EndpointConfigManager::create(const std::string& sdk_desc_str) {
  if (load(sdk_desc_str) != 0) {
    LOG(ERROR) << "Failed reload endpoint config";
    return -1;
  }
}

W
sdk-cpp  
wangguibao 已提交
36
int EndpointConfigManager::create(const char* path, const char* file) {
W
wangguibao 已提交
37 38
  _endpoint_config_path = path;
  _endpoint_config_file = file;
W
sdk-cpp  
wangguibao 已提交
39

W
wangguibao 已提交
40 41 42 43
  if (load() != 0) {
    LOG(ERROR) << "Failed reload endpoint config";
    return -1;
  }
W
sdk-cpp  
wangguibao 已提交
44

W
wangguibao 已提交
45
  return 0;
W
sdk-cpp  
wangguibao 已提交
46 47
}

G
guru4elephant 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
int EndpointConfigManager::load(const std::string& sdk_desc_str) {
  try {
    SDKConf sdk_conf;
    sdk_conf.ParseFromString(sdk_desc_str);
    VariantInfo default_var;
    if (init_one_variant(sdk_conf.default_variant_conf(), default_var) != 0) {
      LOG(ERROR) << "Failed read default var conf";
      return -1;
    }

    uint32_t ep_size = sdk_conf.predictors_size();
    for (uint32_t ei = 0; ei < ep_size; ++ei) {
      EndpointInfo ep;
      if (init_one_endpoint(sdk_conf.predictors(ei), ep, default_var) != 0) {
        LOG(ERROR) << "Failed read endpoint info at: " << ei;
        return -1;
      }

      std::map<std::string, EndpointInfo>::iterator it;
      if (_ep_map.find(ep.endpoint_name) != _ep_map.end()) {
        LOG(ERROR) << "Cannot insert duplicated endpoint"
                   << ", ep name: " << ep.endpoint_name;
      }

      std::pair<std::map<std::string, EndpointInfo>::iterator, bool> r =
          _ep_map.insert(std::make_pair(ep.endpoint_name, ep));
      if (!r.second) {
        LOG(ERROR) << "Failed insert endpoint, name" << ep.endpoint_name;
        return -1;
      }
    }
  } catch (std::exception& e) {
    LOG(ERROR) << "Failed load configure" << e.what();
    return -1;
  }
G
guru4elephant 已提交
83 84
  VLOG(2) << "Success reload endpoint config file, id: "
          << _current_endpointmap_id;
G
guru4elephant 已提交
85 86 87
  return 0;
}

W
sdk-cpp  
wangguibao 已提交
88
int EndpointConfigManager::load() {
W
wangguibao 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  try {
    SDKConf sdk_conf;
    if (configure::read_proto_conf(_endpoint_config_path.c_str(),
                                   _endpoint_config_file.c_str(),
                                   &sdk_conf) != 0) {
      LOG(ERROR) << "Failed initialize endpoint list"
                 << ", config: " << _endpoint_config_path << "/"
                 << _endpoint_config_file;
      return -1;
    }

    VariantInfo default_var;
    if (init_one_variant(sdk_conf.default_variant_conf(), default_var) != 0) {
      LOG(ERROR) << "Failed read default var conf";
      return -1;
    }

    uint32_t ep_size = sdk_conf.predictors_size();
    for (uint32_t ei = 0; ei < ep_size; ++ei) {
      EndpointInfo ep;
      if (init_one_endpoint(sdk_conf.predictors(ei), ep, default_var) != 0) {
        LOG(ERROR) << "Failed read endpoint info at: " << ei;
W
sdk-cpp  
wangguibao 已提交
111
        return -1;
W
wangguibao 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125
      }

      std::map<std::string, EndpointInfo>::iterator it;
      if (_ep_map.find(ep.endpoint_name) != _ep_map.end()) {
        LOG(ERROR) << "Cannot insert duplicated endpoint"
                   << ", ep name: " << ep.endpoint_name;
      }

      std::pair<std::map<std::string, EndpointInfo>::iterator, bool> r =
          _ep_map.insert(std::make_pair(ep.endpoint_name, ep));
      if (!r.second) {
        LOG(ERROR) << "Failed insert endpoint, name" << ep.endpoint_name;
        return -1;
      }
W
sdk-cpp  
wangguibao 已提交
126
    }
W
wangguibao 已提交
127 128 129 130
  } catch (std::exception& e) {
    LOG(ERROR) << "Failed load configure" << e.what();
    return -1;
  }
G
guru4elephant 已提交
131 132
  VLOG(2) << "Success reload endpoint config file, id: "
          << _current_endpointmap_id;
W
wangguibao 已提交
133
  return 0;
W
sdk-cpp  
wangguibao 已提交
134 135
}

W
wangguibao 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
int EndpointConfigManager::init_one_endpoint(const configure::Predictor& conf,
                                             EndpointInfo& ep,
                                             const VariantInfo& dft_var) {
  try {
    // name
    ep.endpoint_name = conf.name();
    // stub
    ep.stub_service = conf.service_name();
    // abtest
    ConfigItem<std::string> ep_router;
    PARSE_CONF_ITEM(conf, ep_router, endpoint_router, -1);
    if (ep_router.init) {
      if (ep_router.value != "WeightedRandomRender") {
        LOG(ERROR) << "endpointer_router unrecognized " << ep_router.value;
        return -1;
      }

      EndpointRouterBase* router =
          EndpointRouterFactory::instance().generate_object(ep_router.value);

      const configure::WeightedRandomRenderConf& router_conf =
          conf.weighted_random_render_conf();
      if (!router || router->initialize(router_conf) != 0) {
        LOG(ERROR) << "Failed fetch valid ab test strategy"
                   << ", name:" << ep_router.value;
        return -1;
      }
      ep.ab_test = router;
    }

    // varlist
    uint32_t var_size = conf.variants_size();
    for (uint32_t vi = 0; vi < var_size; ++vi) {
      VariantInfo var;
      if (merge_variant(dft_var, conf.variants(vi), var) != 0) {
        LOG(ERROR) << "Failed merge variant info at: " << vi;
W
sdk-cpp  
wangguibao 已提交
172
        return -1;
W
wangguibao 已提交
173 174 175
      }

      ep.vars.push_back(var);
W
sdk-cpp  
wangguibao 已提交
176
    }
W
wangguibao 已提交
177 178 179 180 181 182 183

    if (ep.vars.size() > 1 && ep.ab_test == NULL) {
      LOG(ERROR) << "EndpointRouter must be configured, when"
                 << " #Variants > 1.";
      return -1;
    }

G
guru4elephant 已提交
184 185
    VLOG(2) << "Succ load one endpoint, name: " << ep.endpoint_name
            << ", count of variants: " << ep.vars.size() << ".";
W
wangguibao 已提交
186 187 188 189 190 191
  } catch (std::exception& e) {
    LOG(ERROR) << "Exception acccurs when load endpoint conf"
               << ", message: " << e.what();
    return -1;
  }
  return 0;
W
sdk-cpp  
wangguibao 已提交
192 193
}

W
wangguibao 已提交
194 195 196
int EndpointConfigManager::init_one_variant(const configure::VariantConf& conf,
                                            VariantInfo& var) {
  try {
W
sdk-cpp  
wangguibao 已提交
197
    // Connect
W
wangguibao 已提交
198
    const configure::ConnectionConf& conn = conf.connection_conf();
W
sdk-cpp  
wangguibao 已提交
199

W
wangguibao 已提交
200 201 202 203 204 205 206 207 208 209 210
    PARSE_CONF_ITEM(conn, var.connection.tmo_conn, connect_timeout_ms, -1);
    PARSE_CONF_ITEM(conn, var.connection.tmo_rpc, rpc_timeout_ms, -1);
    PARSE_CONF_ITEM(
        conn, var.connection.tmo_hedge, hedge_request_timeout_ms, -1);
    PARSE_CONF_ITEM(
        conn, var.connection.cnt_retry_conn, connect_retry_count, -1);
    PARSE_CONF_ITEM(
        conn, var.connection.cnt_retry_hedge, hedge_fetch_retry_count, -1);
    PARSE_CONF_ITEM(
        conn, var.connection.cnt_maxconn_per_host, max_connection_per_host, -1);
    PARSE_CONF_ITEM(conn, var.connection.type_conn, connection_type, -1);
W
sdk-cpp  
wangguibao 已提交
211 212

    // Naming
W
wangguibao 已提交
213
    const configure::NamingConf& name = conf.naming_conf();
W
sdk-cpp  
wangguibao 已提交
214

W
wangguibao 已提交
215 216 217 218 219
    PARSE_CONF_ITEM(name, var.naminginfo.cluster_naming, cluster, -1);
    PARSE_CONF_ITEM(
        name, var.naminginfo.load_balancer, load_balance_strategy, -1);
    PARSE_CONF_ITEM(
        name, var.naminginfo.cluster_filter, cluster_filter_strategy, -1);
W
sdk-cpp  
wangguibao 已提交
220 221

    // Rpc
W
wangguibao 已提交
222
    const configure::RpcParameter& params = conf.rpc_parameter();
W
sdk-cpp  
wangguibao 已提交
223

W
wangguibao 已提交
224 225 226 227 228
    PARSE_CONF_ITEM(params, var.parameters.protocol, protocol, -1);
    PARSE_CONF_ITEM(params, var.parameters.compress_type, compress_type, -1);
    PARSE_CONF_ITEM(params, var.parameters.package_size, package_size, -1);
    PARSE_CONF_ITEM(
        params, var.parameters.max_channel, max_channel_per_request, -1);
W
sdk-cpp  
wangguibao 已提交
229
    // Split
W
wangguibao 已提交
230
    const configure::SplitConf& splits = conf.split_conf();
W
sdk-cpp  
wangguibao 已提交
231

W
wangguibao 已提交
232 233
    PARSE_CONF_ITEM(splits, var.splitinfo.split_tag, split_tag_name, -1);
    PARSE_CONF_ITEM(splits, var.splitinfo.tag_cands_str, tag_candidates, -1);
W
sdk-cpp  
wangguibao 已提交
234
    if (parse_tag_values(var.splitinfo) != 0) {
W
wangguibao 已提交
235 236 237
      LOG(ERROR) << "Failed parse tag_values:"
                 << var.splitinfo.tag_cands_str.value;
      return -1;
W
sdk-cpp  
wangguibao 已提交
238 239 240
    }

    // tag
W
wangguibao 已提交
241 242 243 244 245
    PARSE_CONF_ITEM(conf, var.parameters.route_tag, tag, -1);
  } catch (...) {
    LOG(ERROR) << "Failed load variant from configure unit";
    return -1;
  }
W
sdk-cpp  
wangguibao 已提交
246

W
wangguibao 已提交
247
  return 0;
W
sdk-cpp  
wangguibao 已提交
248 249
}

W
wangguibao 已提交
250 251 252 253
int EndpointConfigManager::merge_variant(const VariantInfo& default_var,
                                         const configure::VariantConf& conf,
                                         VariantInfo& merged_var) {
  merged_var = default_var;
W
sdk-cpp  
wangguibao 已提交
254

W
wangguibao 已提交
255
  return init_one_variant(conf, merged_var);
W
sdk-cpp  
wangguibao 已提交
256 257
}

W
wangguibao 已提交
258 259 260
int EndpointConfigManager::parse_tag_values(SplitParameters& split) {
  split.tag_values.clear();
  if (!split.split_tag.init || !split.tag_cands_str.init) {
G
guru4elephant 已提交
261
    VLOG(2) << "split info not set, skip...";
W
wangguibao 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    return 0;
  }

  static const char SPLIT_DELIM = ',';
  const std::string& tag_str = split.tag_cands_str.value;
  std::string::size_type start_pos = 0;
  std::string::size_type end_pos;

  do {
    end_pos = tag_str.find(SPLIT_DELIM, start_pos);
    std::string tag_value_str;
    if (end_pos == std::string::npos) {
      tag_value_str = tag_str.substr(start_pos);
    } else {
      tag_value_str = tag_str.substr(start_pos, end_pos - start_pos);
      start_pos = end_pos + 1;
W
sdk-cpp  
wangguibao 已提交
278 279
    }

W
wangguibao 已提交
280 281
    split.tag_values.push_back(tag_value_str);
  } while (end_pos != std::string::npos);
W
sdk-cpp  
wangguibao 已提交
282

W
wangguibao 已提交
283
  return 0;
W
sdk-cpp  
wangguibao 已提交
284 285
}

W
wangguibao 已提交
286 287 288
}  // namespace sdk_cpp
}  // namespace paddle_serving
}  // namespace baidu
W
sdk-cpp  
wangguibao 已提交
289 290

/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */