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
int EndpointConfigManager::create(const std::string& sdk_desc_str) {
  if (load(sdk_desc_str) != 0) {
    LOG(ERROR) << "Failed reload endpoint config";
    return -1;
  }
B
fix bug  
barrierye 已提交
34
  return 0;
G
guru4elephant 已提交
35 36
}

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

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

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

G
guru4elephant 已提交
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 83
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 已提交
84 85
  VLOG(2) << "Success reload endpoint config file, id: "
          << _current_endpointmap_id;
G
guru4elephant 已提交
86 87 88
  return 0;
}

W
sdk-cpp  
wangguibao 已提交
89
int EndpointConfigManager::load() {
W
wangguibao 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  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 已提交
112
        return -1;
W
wangguibao 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126
      }

      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 已提交
127
    }
W
wangguibao 已提交
128 129 130 131
  } catch (std::exception& e) {
    LOG(ERROR) << "Failed load configure" << e.what();
    return -1;
  }
G
guru4elephant 已提交
132 133
  VLOG(2) << "Success reload endpoint config file, id: "
          << _current_endpointmap_id;
W
wangguibao 已提交
134
  return 0;
W
sdk-cpp  
wangguibao 已提交
135 136
}

W
wangguibao 已提交
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 172
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 已提交
173
        return -1;
W
wangguibao 已提交
174 175 176
      }

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

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

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

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

W
wangguibao 已提交
201 202 203 204 205 206 207 208 209 210 211
    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 已提交
212 213

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

W
wangguibao 已提交
216 217 218 219 220
    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 已提交
221 222

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

W
wangguibao 已提交
225 226 227 228 229
    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 已提交
230
    // Split
W
wangguibao 已提交
231
    const configure::SplitConf& splits = conf.split_conf();
W
sdk-cpp  
wangguibao 已提交
232

W
wangguibao 已提交
233 234
    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 已提交
235
    if (parse_tag_values(var.splitinfo) != 0) {
W
wangguibao 已提交
236 237 238
      LOG(ERROR) << "Failed parse tag_values:"
                 << var.splitinfo.tag_cands_str.value;
      return -1;
W
sdk-cpp  
wangguibao 已提交
239 240 241
    }

    // tag
W
wangguibao 已提交
242 243 244 245 246
    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 已提交
247

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

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

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

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

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

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

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

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