endpoint.cpp 3.4 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
// 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.
W
sdk-cpp  
wangguibao 已提交
14

G
guru4elephant 已提交
15 16
#include "core/sdk-cpp/include/endpoint.h"
#include "core/sdk-cpp/include/factory.h"
W
sdk-cpp  
wangguibao 已提交
17 18 19 20 21 22

namespace baidu {
namespace paddle_serving {
namespace sdk_cpp {

int Endpoint::initialize(const EndpointInfo& ep_info) {
W
wangguibao 已提交
23 24
  _variant_list.clear();
  _endpoint_name = ep_info.endpoint_name;
W
wangguibao 已提交
25
  _abtest_router = static_cast<EndpointRouterBase*>(ep_info.ab_test);
W
wangguibao 已提交
26 27 28 29 30 31 32 33 34 35
  uint32_t var_size = ep_info.vars.size();
  for (uint32_t vi = 0; vi < var_size; ++vi) {
    const VariantInfo& var_info = ep_info.vars[vi];
    Variant* var = new (std::nothrow) Variant;
    if (!var || var->initialize(ep_info, var_info) != 0) {
      LOG(ERROR) << "Failed initialize variant, tag:"
                 << var_info.parameters.route_tag.value
                 << ", endpoint: " << ep_info.endpoint_name
                 << ", var index: " << vi;
      return -1;
W
sdk-cpp  
wangguibao 已提交
36
    }
W
wangguibao 已提交
37
    _variant_list.push_back(var);
G
guru4elephant 已提交
38 39
    VLOG(2) << "Succ create variant: " << vi
            << ", endpoint:" << _endpoint_name;
W
wangguibao 已提交
40
  }
W
sdk-cpp  
wangguibao 已提交
41

W
wangguibao 已提交
42
  return 0;
W
sdk-cpp  
wangguibao 已提交
43 44 45
}

int Endpoint::thrd_initialize() {
W
wangguibao 已提交
46 47 48 49 50 51
  uint32_t var_size = _variant_list.size();
  for (uint32_t vi = 0; vi < var_size; ++vi) {
    Variant* var = _variant_list[vi];
    if (!var || var->thrd_initialize()) {
      LOG(ERROR) << "Failed thrd initialize var: " << vi;
      return -1;
W
sdk-cpp  
wangguibao 已提交
52
    }
W
wangguibao 已提交
53
  }
G
guru4elephant 已提交
54
  VLOG(2) << "Succ thrd initialize all vars: " << var_size;
W
wangguibao 已提交
55
  return 0;
W
sdk-cpp  
wangguibao 已提交
56 57 58
}

int Endpoint::thrd_clear() {
W
wangguibao 已提交
59 60 61 62 63 64
  uint32_t var_size = _variant_list.size();
  for (uint32_t vi = 0; vi < var_size; ++vi) {
    Variant* var = _variant_list[vi];
    if (!var || var->thrd_clear()) {
      LOG(ERROR) << "Failed thrd clear var: " << vi;
      return -1;
W
sdk-cpp  
wangguibao 已提交
65
    }
W
wangguibao 已提交
66 67
  }
  return 0;
W
sdk-cpp  
wangguibao 已提交
68 69 70
}

int Endpoint::thrd_finalize() {
W
wangguibao 已提交
71 72 73 74 75 76
  uint32_t var_size = _variant_list.size();
  for (uint32_t vi = 0; vi < var_size; ++vi) {
    Variant* var = _variant_list[vi];
    if (!var || var->thrd_finalize()) {
      LOG(ERROR) << "Failed thrd finalize var: " << vi;
      return -1;
W
sdk-cpp  
wangguibao 已提交
77
    }
W
wangguibao 已提交
78 79 80
  }
  LOG(INFO) << "Succ thrd finalize all vars: " << var_size;
  return 0;
W
sdk-cpp  
wangguibao 已提交
81 82 83
}

Predictor* Endpoint::get_predictor() {
W
wangguibao 已提交
84 85 86 87
  if (_variant_list.size() == 1) {
    if (_variant_list[0] == NULL) {
      LOG(ERROR) << "Not valid variant info";
      return NULL;
W
sdk-cpp  
wangguibao 已提交
88
    }
W
wangguibao 已提交
89 90 91
    return _variant_list[0]->get_predictor();
  }

W
wangguibao 已提交
92 93 94 95 96 97 98 99 100 101 102 103
  if (_abtest_router == NULL) {
    LOG(FATAL) << "Not valid abtest_router!";
    return NULL;
  }

  Variant* var = _abtest_router->route(_variant_list);
  if (!var) {
    LOG(FATAL) << "get null var from endpoint";
    return NULL;
  }

  return var->get_predictor();
W
sdk-cpp  
wangguibao 已提交
104 105 106
}

int Endpoint::ret_predictor(Predictor* predictor) {
W
wangguibao 已提交
107 108 109 110 111
  const Stub* stub = predictor->stub();
  if (!stub || stub->return_predictor(predictor) != 0) {
    LOG(ERROR) << "Failed return predictor to pool";
    return -1;
  }
W
sdk-cpp  
wangguibao 已提交
112

W
wangguibao 已提交
113
  return 0;
W
sdk-cpp  
wangguibao 已提交
114 115
}

W
wangguibao 已提交
116 117 118
}  // namespace sdk_cpp
}  // namespace paddle_serving
}  // namespace baidu