1 /* 2 * Copyright 2020 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef FCP_CLIENT_SIMPLE_TASK_ENVIRONMENT_H_ 17 #define FCP_CLIENT_SIMPLE_TASK_ENVIRONMENT_H_ 18 19 #include <string> 20 #include <vector> 21 22 #include "absl/status/statusor.h" 23 #include "absl/time/time.h" 24 #include "fcp/base/monitoring.h" 25 #include "fcp/client/http/http_client.h" 26 #include "fcp/client/selector_context.pb.h" 27 #include "fcp/protos/plan.pb.h" 28 29 namespace fcp { 30 namespace client { 31 32 // An interface used by the plan engine to query for serialized examples. Not 33 // required to be thread-safe. 34 class ExampleIterator { 35 public: 36 virtual ~ExampleIterator() = default; 37 38 // Returns a serialized example as std::string on success; or, on error: 39 // - CANCELLED if the call got interrupted. 40 // - INVALID_ARGUMENT if some other error occurred, e.g. I/O. 41 // - OUT_OF_RANGE if the end of the iterator was reached. 42 virtual absl::StatusOr<std::string> Next() = 0; 43 44 // Close the iterator to release associated resources. 45 virtual void Close() = 0; 46 }; 47 48 // A simplified task environment that acts as a standalone task environment for 49 // TFF-based plans and a delegated task environment for earlier plan types. 50 class SimpleTaskEnvironment { 51 public: 52 virtual ~SimpleTaskEnvironment() = default; 53 54 // Returns the path of the directory that will be used to store persistent 55 // files that should not be deleted, such as Opstats. 56 virtual std::string GetBaseDir() = 0; 57 58 // Returns the path of the directory that may be used to store 59 // temporary/cached files. The federated compute runtime will use this 60 // directory to cache data for re-use across multiple invocations, as well as 61 // for creating temporary files that are deleted at the end of each 62 // invocation. Implementers of this interface may also delete files in this 63 // directory (for example, in low storage situations) without adverse effects 64 // to the runtime. GetCacheDir() may return the same path as GetBaseDir(). 65 virtual std::string GetCacheDir() = 0; 66 67 // TODO(team): factor out native implementations of this and delete. 68 virtual absl::StatusOr<std::unique_ptr<ExampleIterator>> 69 CreateExampleIterator( 70 const google::internal::federated::plan::ExampleSelector& 71 example_selector) = 0; 72 73 virtual absl::StatusOr<std::unique_ptr<ExampleIterator>> CreateExampleIterator(const google::internal::federated::plan::ExampleSelector & example_selector,const SelectorContext & selector_context)74 CreateExampleIterator( 75 const google::internal::federated::plan::ExampleSelector& 76 example_selector, 77 const SelectorContext& selector_context) { 78 return CreateExampleIterator(example_selector); 79 } 80 81 // Creates an HttpClient. May return a nullptr if HTTP is not supported 82 // (although support for HTTP will become mandatory in the future). CreateHttpClient()83 virtual std::unique_ptr<fcp::client::http::HttpClient> CreateHttpClient() { 84 return nullptr; 85 } 86 87 // Checks whether the caller should abort computation. If less than 88 // condition_polling_period time has elapsed since the last call this function 89 // made to TrainingConditionsSatisfied, returns false. 90 bool ShouldAbort(absl::Time current_time, 91 absl::Duration condition_polling_period); 92 93 private: 94 virtual bool TrainingConditionsSatisfied() = 0; 95 96 absl::Time last_training_conditions_fetch_timestamp_ = absl::InfinitePast(); 97 }; 98 99 } // namespace client 100 } // namespace fcp 101 102 #endif // FCP_CLIENT_SIMPLE_TASK_ENVIRONMENT_H_ 103