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

B
fix bug  
barrierye 已提交
35
  return 0;
G
guru4elephant 已提交
36 37
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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