TensorFlow Serving C++ API Documentation
file_system_storage_path_source.h
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 #ifndef TENSORFLOW_SERVING_SOURCES_STORAGE_PATH_FILE_SYSTEM_STORAGE_PATH_SOURCE_H_
17 #define TENSORFLOW_SERVING_SOURCES_STORAGE_PATH_FILE_SYSTEM_STORAGE_PATH_SOURCE_H_
18 
19 #include <functional>
20 #include <memory>
21 #include <utility>
22 
23 #include "absl/types/variant.h"
24 #include "tensorflow/core/kernels/batching_util/periodic_function.h"
25 #include "tensorflow/core/lib/core/status.h"
26 #include "tensorflow/core/platform/env.h"
27 #include "tensorflow/core/platform/macros.h"
28 #include "tensorflow/core/platform/types.h"
29 #include "tensorflow_serving/config/file_system_storage_path_source.pb.h"
30 #include "tensorflow_serving/core/source.h"
31 #include "tensorflow_serving/core/storage_path.h"
32 
33 namespace tensorflow {
34 namespace serving {
35 namespace internal {
36 class FileSystemStoragePathSourceTestAccess;
37 } // namespace internal
38 } // namespace serving
39 } // namespace tensorflow
40 
41 namespace tensorflow {
42 namespace serving {
43 
62 class FileSystemStoragePathSource : public Source<StoragePath> {
63  public:
64  static Status Create(const FileSystemStoragePathSourceConfig& config,
65  std::unique_ptr<FileSystemStoragePathSource>* result);
66  ~FileSystemStoragePathSource() override;
67 
72  Status UpdateConfig(const FileSystemStoragePathSourceConfig& config);
73 
74  void SetAspiredVersionsCallback(AspiredVersionsCallback callback) override;
75 
76  FileSystemStoragePathSourceConfig config() const {
77  mutex_lock l(mu_);
78  return config_;
79  }
80 
81  private:
83 
84  FileSystemStoragePathSource() = default;
85 
86  // Polls the file system and identify numerical children of the base path.
87  // If zero such children are found, invokes 'aspired_versions_callback_' with
88  // an empty versions list. If one or more such children are found, invokes
89  // 'aspired_versions_callback_' with a singleton list containing the largest
90  // such child.
91  Status PollFileSystemAndInvokeCallback();
92 
93  // Sends empty aspired-versions lists for each servable in 'servable_names'.
94  Status UnaspireServables(const std::set<string>& servable_names)
95  TF_EXCLUSIVE_LOCKS_REQUIRED(mu_);
96 
97  template <typename... Args>
98  void CallAspiredVersionsCallback(Args&&... args) {
99  if (aspired_versions_callback_) {
100  aspired_versions_callback_(std::forward<Args>(args)...);
101  if (aspired_versions_callback_notifier_) {
102  aspired_versions_callback_notifier_();
103  }
104  }
105  }
106 
107  // For testing.
108  void SetAspiredVersionsCallbackNotifier(std::function<void()> fn) {
109  mutex_lock l(mu_);
110  aspired_versions_callback_notifier_ = fn;
111  }
112 
113  mutable mutex mu_;
114 
115  FileSystemStoragePathSourceConfig config_ TF_GUARDED_BY(mu_);
116 
117  AspiredVersionsCallback aspired_versions_callback_ TF_GUARDED_BY(mu_);
118 
119  std::function<void()> aspired_versions_callback_notifier_ TF_GUARDED_BY(mu_);
120 
121  // A thread that calls PollFileSystemAndInvokeCallback() once or periodically.
122  using ThreadType =
123  absl::variant<absl::monostate, PeriodicFunction, std::unique_ptr<Thread>>;
124  std::unique_ptr<ThreadType> fs_polling_thread_ TF_GUARDED_BY(mu_);
125 
126  TF_DISALLOW_COPY_AND_ASSIGN(FileSystemStoragePathSource);
127 };
128 
129 } // namespace serving
130 } // namespace tensorflow
131 
132 #endif // TENSORFLOW_SERVING_SOURCES_STORAGE_PATH_FILE_SYSTEM_STORAGE_PATH_SOURCE_H_
Status UpdateConfig(const FileSystemStoragePathSourceConfig &config)
std::function< void(const StringPiece servable_name, std::vector< ServableData< StoragePath > > versions)> AspiredVersionsCallback
Definition: source.h:88