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