service.h 3.2 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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.

#pragma once
#include <string>
#include <utility>
#include <vector>
G
guru4elephant 已提交
19 20 21
#include "core/predictor/common/inner_common.h"
#include "core/predictor/framework/merger.h"
#include "core/predictor/framework/workflow.h"
W
wangguibao 已提交
22 23 24 25 26 27

namespace baidu {
namespace paddle_serving {
namespace predictor {

class InferService {
W
wangguibao 已提交
28 29
 public:
  typedef OpChannel<google::protobuf::Message> BuiltinChannel;
W
wangguibao 已提交
30

W
wangguibao 已提交
31
  static const char* tag() { return "service"; }
W
wangguibao 已提交
32

W
wangguibao 已提交
33 34 35 36 37 38 39 40
  InferService()
      : _last_change_timestamp(0),
        _enable_map_request_to_workflow(false),
        _request_field_key(""),
        _merger(NULL) {
    _flows.clear();
    _request_to_workflow_map.clear();
  }
W
wangguibao 已提交
41

W
wangguibao 已提交
42
  int init(const configure::InferService& conf);
W
wangguibao 已提交
43

W
wangguibao 已提交
44
  int deinit() { return 0; }
W
wangguibao 已提交
45

W
wangguibao 已提交
46
  int reload();
W
wangguibao 已提交
47

W
wangguibao 已提交
48
  const std::string& name() const;
W
wangguibao 已提交
49

W
wangguibao 已提交
50
  const std::string& full_name() const { return _infer_service_format; }
W
wangguibao 已提交
51

W
wangguibao 已提交
52 53 54
  // Execute each workflow serially
  virtual int inference(const google::protobuf::Message* request,
                        google::protobuf::Message* response,
55
                        const uint64_t log_id,
W
wangguibao 已提交
56
                        butil::IOBufBuilder* debug_os = NULL);
W
wangguibao 已提交
57

W
wangguibao 已提交
58
  int debug(const google::protobuf::Message* request,
W
wangguibao 已提交
59
            google::protobuf::Message* response,
60
            const uint64_t log_id,
W
wangguibao 已提交
61
            butil::IOBufBuilder* debug_os);
W
wangguibao 已提交
62

W
wangguibao 已提交
63 64 65
  int execute_one_workflow(uint32_t index,
                           const google::protobuf::Message* request,
                           google::protobuf::Message* response,
B
barriery 已提交
66
                           const uint64_t log_id,
W
wangguibao 已提交
67 68 69 70 71 72
                           butil::IOBufBuilder* debug_os);

 private:
  int _execute_workflow(Workflow* workflow,
                        const google::protobuf::Message* request,
                        google::protobuf::Message* response,
B
barriery 已提交
73
                        const uint64_t log_id,
W
wangguibao 已提交
74 75 76
                        butil::IOBufBuilder* debug_os);

  std::vector<Workflow*>* _map_request_to_workflow(
B
barriery 已提交
77
      const google::protobuf::Message* request, const uint64_t log_id);
W
wangguibao 已提交
78 79 80 81 82 83 84 85 86 87

 private:
  std::vector<Workflow*> _flows;
  std::string _infer_service_format;
  uint64_t _last_change_timestamp;
  bool _enable_map_request_to_workflow;
  std::string _request_field_key;
  ::butil::FlatMap<std::string, std::vector<Workflow*>>
      _request_to_workflow_map;
  IMerger* _merger;
W
wangguibao 已提交
88 89 90
};

class ParallelInferService : public InferService {
W
wangguibao 已提交
91 92 93 94
 public:
  // Execute workflows in parallel
  int inference(const google::protobuf::Message* request,
                google::protobuf::Message* response,
95
                const uint64_t log_id,
W
wangguibao 已提交
96 97 98
                butil::IOBufBuilder* debug_os) {
    return 0;
  }
W
wangguibao 已提交
99 100
};

W
wangguibao 已提交
101 102 103
}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu