TensorFlow Serving C++ API Documentation
availability_preserving_policy.cc
1 /* Copyright 2016 Google Inc. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow_serving/core/availability_preserving_policy.h"
17 
18 #include <algorithm>
19 #include <vector>
20 
21 #include "absl/types/optional.h"
22 #include "tensorflow_serving/core/loader_harness.h"
23 
24 namespace tensorflow {
25 namespace serving {
26 
27 namespace {
28 
29 // Returns the ServableId with the lowest version, if any exists.
30 absl::optional<ServableId> GetLowestServableId(
31  const std::vector<AspiredServableStateSnapshot>& all_versions) {
32  const auto& iterator =
33  std::min_element(all_versions.begin(), all_versions.end(),
34  [](const AspiredServableStateSnapshot& a,
35  const AspiredServableStateSnapshot& b) {
36  return a.id.version < b.id.version;
37  });
38  if (iterator == all_versions.end()) {
39  return absl::nullopt;
40  } else {
41  return iterator->id;
42  }
43 }
44 
45 } // namespace
46 
47 absl::optional<AspiredVersionPolicy::ServableAction>
49  const std::vector<AspiredServableStateSnapshot>& all_versions) const {
50  // We first try to unload non-aspired versions (if any).
51  bool has_aspired = false;
52  bool has_aspired_serving = false;
53  std::vector<AspiredServableStateSnapshot> unaspired_serving_versions;
54  for (const auto& version : all_versions) {
55  if (version.is_aspired) {
56  has_aspired = true;
57  if (version.state == LoaderHarness::State::kReady) {
58  has_aspired_serving = true;
59  }
60  } else if (version.state == LoaderHarness::State::kReady) {
61  unaspired_serving_versions.push_back(version);
62  }
63  }
64 
65  // If there is no aspired version, there is at least one aspired version
66  // that is ready, or there are more than one un-aspired versions that are
67  // ready, unload the lowest non-aspired version.
68  if (!has_aspired || has_aspired_serving ||
69  unaspired_serving_versions.size() > 1) {
70  absl::optional<ServableId> version_to_unload =
71  GetLowestServableId(unaspired_serving_versions);
72  if (version_to_unload) {
73  return {{Action::kUnload, version_to_unload.value()}};
74  }
75  }
76 
77  // If there is at least one new aspired version, load the one with the
78  // highest version number.
79  absl::optional<ServableId> highest_new_aspired_version_id =
80  GetHighestAspiredNewServableId(all_versions);
81  if (highest_new_aspired_version_id) {
82  VLOG(1) << "AvailabilityPreservingPolicy requesting to load servable "
83  << highest_new_aspired_version_id.value();
84  return {{Action::kLoad, highest_new_aspired_version_id.value()}};
85  }
86 
87  return absl::nullopt;
88 }
89 
90 } // namespace serving
91 } // namespace tensorflow
static absl::optional< ServableId > GetHighestAspiredNewServableId(const std::vector< AspiredServableStateSnapshot > &all_versions)
absl::optional< ServableAction > GetNextAction(const std::vector< AspiredServableStateSnapshot > &all_versions) const override
@ kReady
'loader_->Load()' has succeeded.