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

W
wangguibao 已提交
15 16
#include "sdk-cpp/include/endpoint.h"
#include "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 25 26 27 28 29 30 31 32 33 34
  _variant_list.clear();
  _endpoint_name = ep_info.endpoint_name;
  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 已提交
35
    }
W
wangguibao 已提交
36 37 38 39
    _variant_list.push_back(var);
    LOG(INFO) << "Succ create variant: " << vi
              << ", endpoint:" << _endpoint_name;
  }
W
sdk-cpp  
wangguibao 已提交
40

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

int Endpoint::thrd_initialize() {
W
wangguibao 已提交
45 46 47 48 49 50
  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 已提交
51
    }
W
wangguibao 已提交
52 53 54
  }
  LOG(WARNING) << "Succ thrd initialize all vars: " << var_size;
  return 0;
W
sdk-cpp  
wangguibao 已提交
55 56 57
}

int Endpoint::thrd_clear() {
W
wangguibao 已提交
58 59 60 61 62 63
  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 已提交
64
    }
W
wangguibao 已提交
65 66 67
  }
  LOG(INFO) << "Succ thrd clear all vars: " << var_size;
  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
}

// 带全流量分层实验路由信息
W
wangguibao 已提交
84 85 86 87 88
Predictor* Endpoint::get_predictor(const void* params) {
  Variant* var = NULL;
  if (_variant_list.size() == 1) {
    var = _variant_list[0];
  }
W
sdk-cpp  
wangguibao 已提交
89

W
wangguibao 已提交
90 91 92 93
  if (!var) {
    LOG(ERROR) << "get null var from endpoint.";
    return NULL;
  }
W
sdk-cpp  
wangguibao 已提交
94

W
wangguibao 已提交
95
  return var->get_predictor(params);
W
sdk-cpp  
wangguibao 已提交
96 97 98
}

Predictor* Endpoint::get_predictor() {
99
#if 1
W
wangguibao 已提交
100
  LOG(INFO) << "Endpoint::get_predictor";
101
#endif
W
wangguibao 已提交
102 103 104 105
  if (_variant_list.size() == 1) {
    if (_variant_list[0] == NULL) {
      LOG(ERROR) << "Not valid variant info";
      return NULL;
W
sdk-cpp  
wangguibao 已提交
106
    }
W
wangguibao 已提交
107 108 109 110
    return _variant_list[0]->get_predictor();
  }

  return NULL;
W
sdk-cpp  
wangguibao 已提交
111 112 113
}

int Endpoint::ret_predictor(Predictor* predictor) {
W
wangguibao 已提交
114 115 116 117 118
  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 已提交
119

W
wangguibao 已提交
120
  return 0;
W
sdk-cpp  
wangguibao 已提交
121 122
}

W
wangguibao 已提交
123 124 125
}  // namespace sdk_cpp
}  // namespace paddle_serving
}  // namespace baidu