manager.h 7.2 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12
#ifndef BAIDU_PADDLE_SERVING_PREDICTOR_MANAGER_H
#define BAIDU_PADDLE_SERVING_PREDICTOR_MANAGER_H

#include "common/inner_common.h"
#include "framework/workflow.h"
#include "common/constant.h"
#include "framework/service.h"

namespace baidu {
namespace paddle_serving {
namespace predictor {

W
wangguibao 已提交
13
using configure::WorkflowConf;
W
wangguibao 已提交
14
using configure::InferServiceConf;
W
wangguibao 已提交
15

W
wangguibao 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
class Workflow;
//class InferService;
//class ParallelInferService;

template<typename I>
I* create_item_impl() {
    return new (std::nothrow) I();
}

template<>
inline InferService* create_item_impl<InferService>() {
    if (FLAGS_use_parallel_infer_service) {
        return new (std::nothrow) ParallelInferService();
    } else {
        return new (std::nothrow) InferService();
    } 
}

W
wangguibao 已提交
34
class WorkflowManager {
W
wangguibao 已提交
35
public:
W
wangguibao 已提交
36 37
    static WorkflowManager& instance() {
        static WorkflowManager mgr; 
W
wangguibao 已提交
38 39 40 41
        return mgr;
    }

    int initialize(const std::string path, const std::string file) {
W
wangguibao 已提交
42 43
        WorkflowConf workflow_conf;
        if (configure::read_proto_conf(path, file, &workflow_conf) != 0) {
W
wangguibao 已提交
44
            LOG(FATAL) << "Failed load manager<" << Workflow::tag() << "> configure from " << path << "/" << file;
W
wangguibao 已提交
45 46 47 48 49
            return -1;
        }

        try {

W
wangguibao 已提交
50
        uint32_t item_size = workflow_conf.workflows_size();
W
wangguibao 已提交
51
        for (uint32_t ii = 0; ii < item_size; ii++) {
W
wangguibao 已提交
52 53
            std::string name = workflow_conf.workflows(ii).name();
            Workflow* item = new (std::nothrow) Workflow();
W
wangguibao 已提交
54
            if (item == NULL) {
W
wangguibao 已提交
55
                LOG(FATAL) << "Failed create " << Workflow::tag() << " for: " << name;
W
wangguibao 已提交
56 57
                return -1;
            }
W
wangguibao 已提交
58
            if (item->init(workflow_conf.workflows(ii)) != 0) {
W
wangguibao 已提交
59 60 61 62 63 64 65
                LOG(FATAL) 
                    << "Failed init item: " << name << " at:"
                    << ii << "!";
                return -1;
            }

            std::pair<
W
wangguibao 已提交
66
                typename boost::unordered_map<std::string, Workflow*>::iterator, bool>
W
wangguibao 已提交
67 68 69 70 71 72 73 74
                r = _item_map.insert(std::make_pair(name, item));
            if (!r.second) {
                LOG(FATAL) 
                    << "Failed insert item:" << name << " at:"
                    << ii << "!";
                return -1;
            }

W
wangguibao 已提交
75
            LOG(INFO) 
W
wangguibao 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88
                << "Succ init item:" << name << " from conf:"
                << path << "/" << file << ", at:" << ii << "!";
        } 
        
        } catch (...) {
            LOG(FATAL) 
                << "Config[" << path << "/" << file << "] format "
                << "invalid, load failed";
            return -1;
        }
        return 0;
    }

W
wangguibao 已提交
89 90
    Workflow* create_item() {
        return create_item_impl<Workflow>();
W
wangguibao 已提交
91 92
    }

W
wangguibao 已提交
93 94
    Workflow* item(const std::string& name) {
        typename boost::unordered_map<std::string, Workflow*>::iterator it;
W
wangguibao 已提交
95 96 97 98 99 100 101 102 103
        it = _item_map.find(name);
        if (it == _item_map.end()) {
            LOG(WARNING) << "Not found item: " << name << "!";
            return NULL;
        }

        return it->second;
    }

W
wangguibao 已提交
104 105
    Workflow& operator[](const std::string& name) {
        Workflow* i = item(name);
W
wangguibao 已提交
106 107 108 109 110 111 112 113 114 115
        if (i == NULL) {
            std::string err = "Not found item in manager for:";
            err += name;
            throw std::overflow_error(err);
        }
        return *i;
    }

    int reload() {
        int ret = 0;
W
wangguibao 已提交
116
        typename boost::unordered_map<std::string, Workflow*>::iterator it
W
wangguibao 已提交
117 118 119 120 121 122 123 124
            = _item_map.begin();
        for (; it != _item_map.end(); ++it) {
            if (it->second->reload() != 0) {
                LOG(WARNING) << "failed reload item: " << it->first << "!";
                ret = -1;
            }
        }

W
wangguibao 已提交
125
        LOG(INFO) << "Finish reload " 
W
wangguibao 已提交
126
            << _item_map.size() 
W
wangguibao 已提交
127
            << " " << Workflow::tag() << "(s)";
W
wangguibao 已提交
128 129 130 131 132 133 134 135
        return ret;
    }

    int finalize() {
        return 0;
    }

private:
W
wangguibao 已提交
136
    WorkflowManager() {}
W
wangguibao 已提交
137 138

private:
W
wangguibao 已提交
139
    boost::unordered_map<std::string, Workflow*> _item_map;
W
wangguibao 已提交
140 141
};

W
wangguibao 已提交
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
class InferServiceManager {
public:
    static InferServiceManager& instance() {
        static InferServiceManager mgr; 
        return mgr;
    }

    int initialize(const std::string path, const std::string file) {
        InferServiceConf infer_service_conf;
        if (configure::read_proto_conf(path, file, &infer_service_conf) != 0) {
            LOG(FATAL) << "Failed load manager<" << InferService::tag() << "> configure!";
            return -1;
        }

        try {

        uint32_t item_size = infer_service_conf.services_size();
        for (uint32_t ii = 0; ii < item_size; ii++) {
            std::string name = infer_service_conf.services(ii).name();
            InferService* item = new (std::nothrow) InferService();
            if (item == NULL) {
                LOG(FATAL) << "Failed create " << InferService::tag() << " for: " << name;
                return -1;
            }
            if (item->init(infer_service_conf.services(ii)) != 0) {
                LOG(FATAL) 
                    << "Failed init item: " << name << " at:"
                    << ii << "!";
                return -1;
            }

            std::pair<
                typename boost::unordered_map<std::string, InferService*>::iterator, bool>
                r = _item_map.insert(std::make_pair(name, item));
            if (!r.second) {
                LOG(FATAL) 
                    << "Failed insert item:" << name << " at:"
                    << ii << "!";
                return -1;
            }

            LOG(INFO) 
                << "Succ init item:" << name << " from conf:"
                << path << "/" << file << ", at:" << ii << "!";
        } 
        
        } catch (...) {
            LOG(FATAL) 
                << "Config[" << path << "/" << file << "] format "
                << "invalid, load failed";
            return -1;
        }
        return 0;
    }

    InferService* create_item() {
        return create_item_impl<InferService>();
    }

    InferService* item(const std::string& name) {
        typename boost::unordered_map<std::string, InferService*>::iterator it;
        it = _item_map.find(name);
        if (it == _item_map.end()) {
            LOG(WARNING) << "Not found item: " << name << "!";
            return NULL;
        }

        return it->second;
    }

    InferService& operator[](const std::string& name) {
        InferService* i = item(name);
        if (i == NULL) {
            std::string err = "Not found item in manager for:";
            err += name;
            throw std::overflow_error(err);
        }
        return *i;
    }

    int reload() {
        int ret = 0;
        typename boost::unordered_map<std::string, InferService*>::iterator it
            = _item_map.begin();
        for (; it != _item_map.end(); ++it) {
            if (it->second->reload() != 0) {
                LOG(WARNING) << "failed reload item: " << it->first << "!";
                ret = -1;
            }
        }

        LOG(INFO) << "Finish reload " 
            << _item_map.size() 
            << " " << InferService::tag() << "(s)";
        return ret;
    }

    int finalize() {
        return 0;
    }

private:
    InferServiceManager() {}

private:
    boost::unordered_map<std::string, InferService*> _item_map;
};
W
wangguibao 已提交
249 250 251 252 253 254

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

#endif