TensorFlow Serving C++ API Documentation
resource_preserving_policy.cc
1 /* Copyright 2017 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/resource_preserving_policy.h"
17 
18 #include <algorithm>
19 #include <vector>
20 
21 namespace tensorflow {
22 namespace serving {
23 
24 absl::optional<AspiredVersionPolicy::ServableAction>
26  const std::vector<AspiredServableStateSnapshot>& all_versions) const {
27  // First iterate over all_versions and find any in kReady that are no longer
28  // aspired. Unload the first if any.
29  for (const auto& version : all_versions) {
30  if (version.state == LoaderHarness::State::kReady && !version.is_aspired) {
31  VLOG(1) << "ResourcePreservingPolicy requesting to unload servable "
32  << version.id;
33  return {{Action::kUnload, version.id}};
34  }
35  }
36 
37  // Second, see if there are any not-aspired versions that aren't in an end
38  // state (kDisabled or kError). If so, do nothing for now.
39  const bool not_aspired_not_finished =
40  std::any_of(all_versions.begin(), all_versions.end(),
41  [](const AspiredServableStateSnapshot& version) {
42  return !version.is_aspired &&
43  version.state != LoaderHarness::State::kDisabled &&
44  version.state != LoaderHarness::State::kError;
45  });
46  if (not_aspired_not_finished) {
47  return absl::nullopt;
48  }
49 
50  // Third and only if no action was found earlier, load the aspired new
51  // servable with the highest version if any.
52  absl::optional<ServableId> highest_aspired_new_version_id =
53  GetHighestAspiredNewServableId(all_versions);
54  if (highest_aspired_new_version_id) {
55  VLOG(1) << "ResourcePreservingPolicy requesting to load servable "
56  << highest_aspired_new_version_id.value();
57  return {{Action::kLoad, highest_aspired_new_version_id.value()}};
58  }
59 
60  return absl::nullopt;
61 }
62 
63 } // namespace serving
64 } // namespace tensorflow
static absl::optional< ServableId > GetHighestAspiredNewServableId(const std::vector< AspiredServableStateSnapshot > &all_versions)
@ kReady
'loader_->Load()' has succeeded.
absl::optional< ServableAction > GetNextAction(const std::vector< AspiredServableStateSnapshot > &all_versions) const override
A snapshot of a servable's state and aspiredness.