config_manager.cpp 7.9 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;

W
sdk-cpp  
wangguibao 已提交
29
int EndpointConfigManager::create(const char* path, const char* file) {
W
wangguibao 已提交
30 31
  _endpoint_config_path = path;
  _endpoint_config_file = file;
W
sdk-cpp  
wangguibao 已提交
32

W
wangguibao 已提交
33 34 35 36
  if (load() != 0) {
    LOG(ERROR) << "Failed reload endpoint config";
    return -1;
  }
W
sdk-cpp  
wangguibao 已提交
37

W
wangguibao 已提交
38
  return 0;
W
sdk-cpp  
wangguibao 已提交
39 40 41
}

int EndpointConfigManager::load() {
W
wangguibao 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  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 已提交
64
        return -1;
W
wangguibao 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78
      }

      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 已提交
79
    }
W
wangguibao 已提交
80 81 82 83 84 85 86
  } catch (std::exception& e) {
    LOG(ERROR) << "Failed load configure" << e.what();
    return -1;
  }
  LOG(INFO) << "Success reload endpoint config file, id: "
            << _current_endpointmap_id;
  return 0;
W
sdk-cpp  
wangguibao 已提交
87 88
}

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

      ep.vars.push_back(var);
W
sdk-cpp  
wangguibao 已提交
129
    }
W
wangguibao 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

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

    LOG(INFO) << "Succ load one endpoint, name: " << ep.endpoint_name
              << ", count of variants: " << ep.vars.size() << ".";
  } catch (std::exception& e) {
    LOG(ERROR) << "Exception acccurs when load endpoint conf"
               << ", message: " << e.what();
    return -1;
  }
  return 0;
W
sdk-cpp  
wangguibao 已提交
145 146
}

W
wangguibao 已提交
147 148 149
int EndpointConfigManager::init_one_variant(const configure::VariantConf& conf,
                                            VariantInfo& var) {
  try {
W
sdk-cpp  
wangguibao 已提交
150
    // Connect
W
wangguibao 已提交
151
    const configure::ConnectionConf& conn = conf.connection_conf();
W
sdk-cpp  
wangguibao 已提交
152

W
wangguibao 已提交
153 154 155 156 157 158 159 160 161 162 163
    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 已提交
164 165

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

W
wangguibao 已提交
168 169 170 171 172
    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 已提交
173 174

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

W
wangguibao 已提交
177 178 179 180 181
    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 已提交
182
    // Split
W
wangguibao 已提交
183
    const configure::SplitConf& splits = conf.split_conf();
W
sdk-cpp  
wangguibao 已提交
184

W
wangguibao 已提交
185 186
    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 已提交
187
    if (parse_tag_values(var.splitinfo) != 0) {
W
wangguibao 已提交
188 189 190
      LOG(ERROR) << "Failed parse tag_values:"
                 << var.splitinfo.tag_cands_str.value;
      return -1;
W
sdk-cpp  
wangguibao 已提交
191 192 193
    }

    // tag
W
wangguibao 已提交
194 195 196 197 198
    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 已提交
199

W
wangguibao 已提交
200
  return 0;
W
sdk-cpp  
wangguibao 已提交
201 202
}

W
wangguibao 已提交
203 204 205 206
int EndpointConfigManager::merge_variant(const VariantInfo& default_var,
                                         const configure::VariantConf& conf,
                                         VariantInfo& merged_var) {
  merged_var = default_var;
W
sdk-cpp  
wangguibao 已提交
207

W
wangguibao 已提交
208
  return init_one_variant(conf, merged_var);
W
sdk-cpp  
wangguibao 已提交
209 210
}

W
wangguibao 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
int EndpointConfigManager::parse_tag_values(SplitParameters& split) {
  split.tag_values.clear();
  if (!split.split_tag.init || !split.tag_cands_str.init) {
    LOG(WARNING) << "split info not set, skip...";
    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 已提交
231 232
    }

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

W
wangguibao 已提交
236
  return 0;
W
sdk-cpp  
wangguibao 已提交
237 238
}

W
wangguibao 已提交
239 240 241
}  // namespace sdk_cpp
}  // namespace paddle_serving
}  // namespace baidu
W
sdk-cpp  
wangguibao 已提交
242 243

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