1 /* Copyright 2020 The TensorFlow Authors. 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_COMPILER_XLA_PJRT_DISTRIBUTED_KEY_VALUE_STORE_H_ 17 #define TENSORFLOW_COMPILER_XLA_PJRT_DISTRIBUTED_KEY_VALUE_STORE_H_ 18 19 #include "grpcpp/grpcpp.h" 20 #include "absl/base/thread_annotations.h" 21 #include "absl/container/flat_hash_map.h" 22 #include "absl/synchronization/mutex.h" 23 #include "absl/time/time.h" 24 25 namespace xla { 26 27 // A simple blocking key-value store class. 28 class KeyValueStore { 29 public: 30 KeyValueStore(); 31 32 KeyValueStore(const KeyValueStore&) = delete; 33 KeyValueStore(KeyValueStore&&) = delete; 34 KeyValueStore& operator=(const KeyValueStore&) = delete; 35 KeyValueStore&& operator=(KeyValueStore&&) = delete; 36 37 // Looks up `key`. If present, returns its value. If the key is not present, 38 // waits until `timeout` expires for the key to arrive. If the key does not 39 // arrive by the expiry of `timeout`, returns NOT_FOUND. 40 ::grpc::Status Get(const std::string& key, absl::Duration timeout, 41 std::string* value); 42 43 // Replaces the value of `key` with `value`. 44 ::grpc::Status Set(const std::string& key, std::string value); 45 46 private: 47 absl::Mutex mu_; 48 absl::flat_hash_map<std::string, std::string> entries_ ABSL_GUARDED_BY(mu_); 49 }; 50 51 } // namespace xla 52 53 #endif // TENSORFLOW_COMPILER_XLA_PJRT_DISTRIBUTED_KEY_VALUE_STORE_H_ 54