config_manager.cpp 8.6 KB
Newer Older
W
sdk-cpp  
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 83 84 85 86 87 88 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 125 126 127 128 129 130 131 132 133 134 135 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
/***************************************************************************
 *
 * Copyright (c) 2018 Baidu.com, Inc. All Rights Reserved
 *
 **************************************************************************/

/**
 * @file endpoint_config.cpp
 * @author wanlijin01(com@baidu.com)
 * @date 2018/07/09 15:30:09
 * @brief
 *
 **/
#include "abtest.h"
#include "config_manager.h"
#include <brpc/server.h>

namespace baidu {
namespace paddle_serving {
namespace sdk_cpp {

int EndpointConfigManager::create(const char* path, const char* file) {
    _endpoint_config_path = path;
    _endpoint_config_file = file;

    if (load() != 0) {
        LOG(FATAL) << "Failed reload endpoint config";
        return -1;
    }

    return 0;
}

int EndpointConfigManager::load() {
    try {
        comcfg::Configure conf;
        if (conf.load(
                    _endpoint_config_path.c_str(),
                    _endpoint_config_file.c_str()) != 0) {
            LOG(FATAL)
                << "Failed initialize endpoint list"
                << ", config: " << _endpoint_config_path
                << "/" << _endpoint_config_file;
            return -1;
        }

        VariantInfo default_var;
        if (init_one_variant(conf["DefaultVariantInfo"],
                    default_var) != 0) {
            LOG(FATAL) << "Failed read default var conf";
            return -1;
        }

        uint32_t ep_size = conf["Predictor"].size();
        for (uint32_t ei = 0; ei < ep_size; ++ei) {
            EndpointInfo ep;
            if (init_one_endpoint(conf["Predictor"][ei], ep,
                        default_var) != 0) {
                LOG(FATAL) << "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(FATAL) << "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(FATAL) << "Failed insert endpoint, name"
                    << ep.endpoint_name;
                return -1;
            }
        }

    } catch (bsl::Exception& e) {
        LOG(FATAL) << "Failed load configure, err: " << e.what();
        return -1;
    }
    LOG(INFO)
        << "Success reload endpoint config file, id: "
        << _current_endpointmap_id;
    return 0;
}

int EndpointConfigManager::init_one_endpoint(
        const comcfg::ConfigUnit& conf, EndpointInfo& ep,
        const VariantInfo& dft_var) {
    try {
        // name
        ep.endpoint_name = conf["name"].to_cstr();
        // stub
        ep.stub_service = conf["service_name"].to_cstr();
        // abtest
        ConfigItem<std::string> ep_router;
        PARSE_CONF_ITEM(conf, ep_router, "endpoint_router", -1);
        if (ep_router.init) {
            std::string endpoint_router_info
                = conf["endpoint_router"].to_cstr();
            EndpointRouterBase* router
                = EndpointRouterFactory::instance().generate_object(
                        ep_router.value);
            if (!router || router->initialize(
                        conf[endpoint_router_info.c_str()]) != 0) {
                LOG(FATAL) << "Failed fetch valid ab test strategy"
                    << ", name:" << endpoint_router_info;
                return -1;
            }
            ep.ab_test = router;
        }

        // varlist
        uint32_t var_size = conf["VariantInfo"].size();
        for (uint32_t vi = 0; vi < var_size; ++vi) {
            VariantInfo var;
            if (merge_variant(dft_var, conf["VariantInfo"][vi],
                        var) != 0) {
                LOG(FATAL) << "Failed merge variant info at: "
                    << vi;
                return -1;
            }

            ep.vars.push_back(var);
        }

        if (ep.vars.size() > 1 && ep.ab_test == NULL) {
            LOG(FATAL) << "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 (bsl::Exception& e) {
        LOG(FATAL) << "Exception acccurs when load endpoint conf"
            << ", message: " << e.what();
        return -1;
    }
    return 0;
}

int EndpointConfigManager::init_one_variant(
        const comcfg::ConfigUnit& conf, VariantInfo& var) {
    try {
    // Connect
    const comcfg::ConfigUnit& conn = conf["Connection"];

    PARSE_CONF_ITEM(conn, var.connection.tmo_conn,
            "ConnectTimeoutMilliSec", -1);
    PARSE_CONF_ITEM(conn, var.connection.tmo_rpc,
            "RpcTimeoutMilliSec", -1);
    PARSE_CONF_ITEM(conn, var.connection.tmo_hedge,
            "HedgeRequestTimeoutMilliSec", -1);
    PARSE_CONF_ITEM(conn, var.connection.cnt_retry_conn,
            "ConnectRetryCount", -1);
    PARSE_CONF_ITEM(conn, var.connection.cnt_retry_hedge,
            "HedgeFetchRetryCount", -1);
    PARSE_CONF_ITEM(conn, var.connection.cnt_maxconn_per_host,
            "MaxConnectionPerHost", -1);
    PARSE_CONF_ITEM(conn, var.connection.type_conn,
            "ConnectionType", -1);

    // Naming
    const comcfg::ConfigUnit& name = conf["NamingInfo"];

    PARSE_CONF_ITEM(name, var.naminginfo.cluster_naming,
            "Cluster", -1);
    PARSE_CONF_ITEM(name, var.naminginfo.load_balancer,
            "LoadBalanceStrategy", -1);
    PARSE_CONF_ITEM(name, var.naminginfo.cluster_filter,
            "ClusterFilterStrategy", -1);

    // Rpc
    const comcfg::ConfigUnit& params = conf["RpcParameter"];

    PARSE_CONF_ITEM(params, var.parameters.protocol,
            "Protocol", -1);
    PARSE_CONF_ITEM(params, var.parameters.compress_type,
            "CompressType", -1);
    PARSE_CONF_ITEM(params, var.parameters.package_size,
            "PackageSize", -1);
    PARSE_CONF_ITEM(params, var.parameters.max_channel,
            "MaxChannelPerRequest", -1);
    // Split
    const comcfg::ConfigUnit& splits = conf["SplitInfo"];

    PARSE_CONF_ITEM(splits, var.splitinfo.split_tag,
            "split_tag_name", -1);
    PARSE_CONF_ITEM(splits, var.splitinfo.tag_cands_str,
            "tag_candidates", -1);
    if (parse_tag_values(var.splitinfo) != 0) {
        LOG(FATAL) << "Failed parse tag_values:" <<
            var.splitinfo.tag_cands_str.value;
        return -1;
    }

    // tag
    PARSE_CONF_ITEM(conf, var.parameters.route_tag,
            "Tag", -1);

    // router
    ConfigItem<std::string> var_router;
    PARSE_CONF_ITEM(conf, var_router, "variant_router", -1);
    if (var_router.init) {
        VariantRouterBase* router
            = VariantRouterFactory::instance().generate_object(
                    var_router.value);
        if (!router || router->initialize(
                    conf[var_router.value.c_str()]) != 0) {
            LOG(FATAL) << "Failed fetch valid variant router"
                << ", name:" << var_router.value;
            return -1;
        }
        var.ab_test = router;
    }

    } catch (...) {
        LOG(FATAL) << "Failed load variant from configure unit";
        return -1;
    }

    return 0;
}

int EndpointConfigManager::merge_variant(
        const VariantInfo& default_var,
        const comcfg::ConfigUnit& conf,
        VariantInfo& merged_var) {
    merged_var = default_var;

    // VariantRouter cannot be merged!
    merged_var.ab_test = NULL;
    return init_one_variant(conf, merged_var);
}

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;
        }

        split.tag_values.push_back(tag_value_str);
    } while (end_pos != std::string::npos);

    return 0;
}

} // sdk_cpp
} // paddle_serving
} // baidu

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