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_CORE_DATA_SERVICE_SPLIT_PROVIDER_H_
17 #define TENSORFLOW_CORE_DATA_SERVICE_SPLIT_PROVIDER_H_
18 
19 #include <queue>
20 
21 #include "tensorflow/core/data/service/data_service.h"
22 #include "tensorflow/core/framework/dataset.h"
23 
24 namespace tensorflow {
25 namespace data {
26 
27 // SplitProvider which reads splits from a tf.data service dispatcher over RPC.
28 class DataServiceSplitProvider : public SplitProvider {
29  public:
DataServiceSplitProvider(const std::string & address,const std::string & protocol,int64 job_id,int64 timeout_ms)30   DataServiceSplitProvider(const std::string& address,
31                            const std::string& protocol, int64 job_id,
32                            int64 timeout_ms)
33       : address_(address),
34         protocol_(protocol),
35         job_id_(job_id),
36         timeout_ms_(timeout_ms) {}
37 
38   Status GetNext(Tensor* split, bool* end_of_splits) override;
39   Status Reset() override;
40   Status Save(std::function<std::string(std::string)> full_name,
41               IteratorStateWriter* writer) override;
42   Status Restore(std::function<std::string(std::string)> full_name,
43                  IteratorStateReader* reader) override;
44 
45  private:
46   const std::string address_;
47   const std::string protocol_;
48   const int64 job_id_;
49   const int64 timeout_ms_;
50 
51   mutex mu_;
52   int64 repetition_ = 0;
53   std::unique_ptr<DataServiceDispatcherClient> dispatcher_;
54 };
55 
56 }  // namespace data
57 }  // namespace tensorflow
58 
59 #endif  // TENSORFLOW_CORE_DATA_SERVICE_SPLIT_PROVIDER_H_
60