service.cpp 10.0 KB
Newer Older
W
wangguibao 已提交
1 2 3 4
#include "common/inner_common.h"
#include "framework/channel.h"
#include "common/constant.h"
#include "framework/service.h"
W
wangguibao 已提交
5
#include <butil/time.h> // butil::Timer
W
wangguibao 已提交
6 7 8 9 10 11 12 13 14 15
#include "framework/server.h"
#include "framework/dag_view.h"
#include "framework/manager.h"
#include "framework/resource.h"
#include "framework/predictor_metric.h" // PredictorMetric

namespace baidu {
namespace paddle_serving {
namespace predictor {

W
wangguibao 已提交
16 17 18 19 20 21 22
int InferService::init(const configure::InferService& conf) {
    _infer_service_format = conf.name();

    std::string merger = conf.merger();
    if (merger == "") {
        merger = "default";
    }
W
wangguibao 已提交
23 24 25 26 27 28 29
    if (!MergerManager::instance().get(merger, _merger)) {
        LOG(ERROR) << "Failed get merger: " << merger;
        return ERR_INTERNAL_FAILURE;
    } else {
        LOG(WARNING) << "Succ get merger: " << merger << 
            " for service: " << _infer_service_format;
    }
W
wangguibao 已提交
30

W
wangguibao 已提交
31 32
    ServerManager& svr_mgr = ServerManager::instance();
    if (svr_mgr.add_service_by_format(_infer_service_format) != 0) {
33
        LOG(ERROR) 
W
wangguibao 已提交
34 35 36 37 38
            << "Not found service by format name:"
            << _infer_service_format << "!";
        return ERR_INTERNAL_FAILURE;
    }
    
W
wangguibao 已提交
39
    _enable_map_request_to_workflow = conf.enable_map_request_to_workflow();
W
wangguibao 已提交
40 41 42 43 44 45 46
    LOG(INFO) << "service[" << _infer_service_format
            << "], enable_map_request_to_workflow["
            << _enable_map_request_to_workflow << "].";

    if (_enable_map_request_to_workflow) {
        if (_request_to_workflow_map.init(
                MAX_WORKFLOW_NUM_IN_ONE_SERVICE/*load_factor=80*/) != 0) {
47
            LOG(ERROR) 
W
wangguibao 已提交
48 49 50 51 52
                << "init request to workflow map failed, bucket_count["
                << MAX_WORKFLOW_NUM_IN_ONE_SERVICE << "].";
            return ERR_INTERNAL_FAILURE;
        }
        int err = 0;
W
wangguibao 已提交
53 54
        _request_field_key = conf.request_field_key().c_str();
        if (_request_field_key == "") {
55
            LOG(ERROR) 
W
wangguibao 已提交
56 57
                << "read request_field_key failed, request_field_key[" 
                << _request_field_key << "].";
W
wangguibao 已提交
58 59
            return ERR_INTERNAL_FAILURE;
        }
W
wangguibao 已提交
60

W
wangguibao 已提交
61 62 63 64
        LOG(INFO) 
            << "service[" << _infer_service_format
            << "], request_field_key[" 
            << _request_field_key << "].";
W
wangguibao 已提交
65 66
        uint32_t value_mapped_workflows_size = conf.value_mapped_workflows_size();
        for (uint32_t fi = 0; fi < value_mapped_workflows_size; fi++) {
W
wangguibao 已提交
67 68
            std::vector<std::string> tokens;
            std::vector<Workflow*> workflows;
W
wangguibao 已提交
69
            std::string list = conf.value_mapped_workflows(fi).workflow();
W
wangguibao 已提交
70 71 72 73 74 75 76
            boost::split(tokens, list, boost::is_any_of(","));
            uint32_t tsize = tokens.size();
            for (uint32_t ti = 0; ti < tsize; ++ti) {
                boost::trim_if(tokens[ti], boost::is_any_of(" "));
                Workflow* workflow = 
                    WorkflowManager::instance().item(tokens[ti]);
                if (workflow == NULL) {
77
                    LOG(ERROR) 
W
wangguibao 已提交
78 79 80 81 82 83 84
                        << "Failed get workflow by name:" 
                        << tokens[ti] << ", ti: " << ti;
                    return ERR_INTERNAL_FAILURE;
                }
                workflow->regist_metric(full_name());
                workflows.push_back(workflow);
            }
W
wangguibao 已提交
85 86

            const std::string& request_field_value = conf.value_mapped_workflows(fi).request_field_value();
W
wangguibao 已提交
87
            if (_request_to_workflow_map.insert(request_field_value, workflows) == NULL) {
88
                LOG(ERROR) 
W
wangguibao 已提交
89 90 91 92 93 94 95 96
                    << "insert [" << request_field_value << "," 
                    << list << "] to _request_to_workflow_map failed.";
                return ERR_INTERNAL_FAILURE;
            }
            LOG(INFO) << "workflow[" << list
                << "], request_field_value[" << request_field_value << "].";
        }
    } else {
W
wangguibao 已提交
97
        uint32_t flow_size = conf.workflows_size();
W
wangguibao 已提交
98
        for (uint32_t fi = 0; fi < flow_size; fi++) {
W
wangguibao 已提交
99
            const std::string& workflow_name = conf.workflows(fi);
W
wangguibao 已提交
100 101 102
            Workflow* workflow = 
                WorkflowManager::instance().item(workflow_name);
            if (workflow == NULL) {
103
                LOG(ERROR) 
W
wangguibao 已提交
104 105 106 107 108 109 110 111 112
                    << "Failed get workflow by name:" 
                    << workflow_name;
                return ERR_INTERNAL_FAILURE;
            }
            workflow->regist_metric(full_name());
            _flows.push_back(workflow);
        } 
    }

W
wangguibao 已提交
113
    LOG(INFO) 
W
wangguibao 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        << "Succ load infer_service: " 
        << _infer_service_format << "!";

    return ERR_OK;
} 

int InferService::reload() {
    return ERR_OK;
}

const std::string& InferService::name() const {
    return _infer_service_format;
}

// ִÿworkflow
int InferService::inference(
        const google::protobuf::Message* request,
        google::protobuf::Message* response,
W
wangguibao 已提交
132
        butil::IOBufBuilder* debug_os) {
W
wangguibao 已提交
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

    TRACEPRINTF("start to inference");
    // when funtion call begins, framework will reset
    // thread local variables&resources automatically.
    if (Resource::instance().thread_clear() != 0) {
        LOG(ERROR) << "Failed thread clear whole resource";
        return ERR_INTERNAL_FAILURE;
    }

    TRACEPRINTF("finish to thread clear");

    if (_enable_map_request_to_workflow) {
        std::vector<Workflow*>* workflows = _map_request_to_workflow(request);
        if (!workflows || workflows->size() == 0) {
            LOG(ERROR) << "Failed to map request to workflow";
            return ERR_INTERNAL_FAILURE;
        }
        size_t fsize = workflows->size();
        for (size_t fi = 0; fi < fsize; ++fi) {
            Workflow* workflow = (*workflows)[fi];
            if (workflow == NULL) {
                LOG(ERROR) << "Failed to get valid workflow at: " << fi;
                return ERR_INTERNAL_FAILURE;
            }
            TRACEPRINTF("start to execute workflow[%s]", workflow->name().c_str());
            int errcode = _execute_workflow(workflow, request, response, debug_os);
            TRACEPRINTF("finish to execute workflow[%s]", workflow->name().c_str());
            if (errcode < 0) {
161
                LOG(ERROR) << "Failed execute workflow[" << workflow->name()
W
wangguibao 已提交
162 163 164 165 166 167 168 169 170 171 172 173
                        << "] in:" << name();
                return errcode;
            }
        }
    } else {
        TRACEPRINTF("start to execute one workflow");
        size_t fsize = _flows.size();
        for (size_t fi = 0; fi < fsize; ++fi) {
            TRACEPRINTF("start to execute one workflow-%lu", fi);
            int errcode = execute_one_workflow(fi, request, response, debug_os);
            TRACEPRINTF("finish to execute one workflow-%lu", fi);
            if (errcode < 0) {
174
                LOG(ERROR) << "Failed execute 0-th workflow in:" << name();
W
wangguibao 已提交
175 176 177 178 179 180 181 182 183 184
                return errcode;
            }
        }
    }
    return ERR_OK;
}

int InferService::debug(
        const google::protobuf::Message* request,
        google::protobuf::Message* response,
W
wangguibao 已提交
185
        butil::IOBufBuilder* debug_os) {
W
wangguibao 已提交
186 187 188 189 190 191 192
    return inference(request, response, debug_os);
}

int InferService::execute_one_workflow(
        uint32_t index, 
        const google::protobuf::Message* request, 
        google::protobuf::Message* response,
W
wangguibao 已提交
193
        butil::IOBufBuilder* debug_os) {
W
wangguibao 已提交
194
    if (index >= _flows.size()) {
195
        LOG(ERROR) << "Faield execute workflow, index: "
W
wangguibao 已提交
196 197 198 199 200 201 202 203 204 205 206
            << index << " >= max:" << _flows.size();
        return ERR_OVERFLOW_FAILURE;
    } 
    Workflow* workflow = _flows[index];
    return _execute_workflow(workflow, request, response, debug_os);
}

int InferService::_execute_workflow(
        Workflow* workflow,
        const google::protobuf::Message* request, 
        google::protobuf::Message* response,
W
wangguibao 已提交
207 208
        butil::IOBufBuilder* debug_os) {
    butil::Timer workflow_time(butil::Timer::STARTED);
W
wangguibao 已提交
209 210 211 212 213 214 215 216 217 218 219
    // create and submit beginer channel
    BuiltinChannel req_channel;
    req_channel.init(0, START_OP_NAME);
    req_channel = request;

    DagView* dv = workflow->fetch_dag_view(full_name());
    dv->set_request_channel(req_channel);

    // call actual inference interface
    int errcode = dv->execute(debug_os);
    if (errcode < 0) {
220
        LOG(ERROR) << "Failed execute dag for workflow:"
W
wangguibao 已提交
221 222 223 224 225 226 227 228
            << workflow->name();
        return errcode;
    }

    TRACEPRINTF("finish to dv execute");
    // create ender channel and copy
    const Channel* res_channel = dv->get_response_channel();
    if (!_merger || !_merger->merge(res_channel->message(), response)) {
229
        LOG(ERROR) << "Failed merge channel res to response"; 
W
wangguibao 已提交
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
        return ERR_INTERNAL_FAILURE;
    }
    TRACEPRINTF("finish to copy from");

    workflow_time.stop();
    PredictorMetric::GetInstance()->update_latency_metric(
            WORKFLOW_METRIC_PREFIX + dv->full_name(), workflow_time.u_elapsed());
    
    // return tls data to object pool
    workflow->return_dag_view(dv);
    TRACEPRINTF("finish to return dag view");
    return ERR_OK;
}

std::vector<Workflow*>* InferService::_map_request_to_workflow(
        const google::protobuf::Message* request) {
    const google::protobuf::Descriptor* desc = request->GetDescriptor();
    const google::protobuf::FieldDescriptor* field = desc->FindFieldByName(_request_field_key);
    if (field == NULL) {
        LOG(ERROR) << "No field[" << _request_field_key << "] in [" << desc->full_name() << "].";
        return NULL;
    }
    if (field->is_repeated()) {
        LOG(ERROR) << "field[" << desc->full_name() << "."
                << _request_field_key << "] is repeated.";
        return NULL;
    }
    if (field->cpp_type() != google::protobuf::FieldDescriptor::CPPTYPE_STRING) {
        LOG(ERROR) << "field[" << desc->full_name() << "."
                << _request_field_key << "] should be string";
        return NULL;
    }
    const std::string& field_value = request->GetReflection()->GetString(*request, field);
    std::vector<Workflow*>* p_workflow = _request_to_workflow_map.seek(field_value);
    if (p_workflow == NULL) {
        LOG(ERROR) << "cannot find key[" << field_value << "] in _request_to_workflow_map";
        return NULL;
    }
    return p_workflow;
}

} // predictor
} // paddle_serving
} // baidu