xref: /aosp_15_r20/external/federated-compute/fcp/client/federated_protocol_util.h (revision 14675a029014e728ec732f129a32e299b2da0601)
1*14675a02SAndroid Build Coastguard Worker /*
2*14675a02SAndroid Build Coastguard Worker  * Copyright 2022 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_FEDERATED_PROTOCOL_UTIL_H_
17*14675a02SAndroid Build Coastguard Worker #define FCP_CLIENT_FEDERATED_PROTOCOL_UTIL_H_
18*14675a02SAndroid Build Coastguard Worker 
19*14675a02SAndroid Build Coastguard Worker #include <string>
20*14675a02SAndroid Build Coastguard Worker 
21*14675a02SAndroid Build Coastguard Worker #include "google/protobuf/duration.pb.h"
22*14675a02SAndroid Build Coastguard Worker #include "absl/random/random.h"
23*14675a02SAndroid Build Coastguard Worker #include "absl/status/statusor.h"
24*14675a02SAndroid Build Coastguard Worker #include "absl/time/time.h"
25*14675a02SAndroid Build Coastguard Worker #include "fcp/client/log_manager.h"
26*14675a02SAndroid Build Coastguard Worker #include "fcp/protos/federated_api.pb.h"
27*14675a02SAndroid Build Coastguard Worker 
28*14675a02SAndroid Build Coastguard Worker namespace fcp {
29*14675a02SAndroid Build Coastguard Worker namespace client {
30*14675a02SAndroid Build Coastguard Worker 
31*14675a02SAndroid Build Coastguard Worker // Utility methods likely shared by FederatedProtocol implementations.
32*14675a02SAndroid Build Coastguard Worker 
33*14675a02SAndroid Build Coastguard Worker // Picks an absolute retry time by picking a retry delay from the range
34*14675a02SAndroid Build Coastguard Worker // specified by the RetryWindow, and then adding it to the current timestamp.
35*14675a02SAndroid Build Coastguard Worker absl::Time PickRetryTimeFromRange(const ::google::protobuf::Duration& min_delay,
36*14675a02SAndroid Build Coastguard Worker                                   const ::google::protobuf::Duration& max_delay,
37*14675a02SAndroid Build Coastguard Worker                                   absl::BitGen& bit_gen);
38*14675a02SAndroid Build Coastguard Worker 
39*14675a02SAndroid Build Coastguard Worker // Picks a retry delay and encodes it as a zero-width RetryWindow (where
40*14675a02SAndroid Build Coastguard Worker // delay_min and delay_max are set to the same value), from a given target delay
41*14675a02SAndroid Build Coastguard Worker // and a configured amount of jitter.
42*14675a02SAndroid Build Coastguard Worker ::google::internal::federatedml::v2::RetryWindow
43*14675a02SAndroid Build Coastguard Worker GenerateRetryWindowFromTargetDelay(absl::Duration target_delay,
44*14675a02SAndroid Build Coastguard Worker                                    double jitter_percent,
45*14675a02SAndroid Build Coastguard Worker                                    absl::BitGen& bit_gen);
46*14675a02SAndroid Build Coastguard Worker 
47*14675a02SAndroid Build Coastguard Worker // Converts the given absl::Time to a zero-width RetryWindow (where
48*14675a02SAndroid Build Coastguard Worker // delay_min and delay_max are set to the same value), by converting the target
49*14675a02SAndroid Build Coastguard Worker // retry time to a delay relative to the current timestamp.
50*14675a02SAndroid Build Coastguard Worker ::google::internal::federatedml::v2::RetryWindow
51*14675a02SAndroid Build Coastguard Worker GenerateRetryWindowFromRetryTime(absl::Time retry_time);
52*14675a02SAndroid Build Coastguard Worker 
53*14675a02SAndroid Build Coastguard Worker // Extracts a task name from an aggregation session ID (in the HTTP protocol) or
54*14675a02SAndroid Build Coastguard Worker // a phase ID (in the gRPC protocol), both of which are expected to adhere to
55*14675a02SAndroid Build Coastguard Worker // the following format: "population_name/task_name#round_id.shard_id".
56*14675a02SAndroid Build Coastguard Worker //
57*14675a02SAndroid Build Coastguard Worker // Returns the `session_id` string unmodified if it does not match that format.
58*14675a02SAndroid Build Coastguard Worker // A diag code will be logged to the `LogManager` in this case.
59*14675a02SAndroid Build Coastguard Worker std::string ExtractTaskNameFromAggregationSessionId(
60*14675a02SAndroid Build Coastguard Worker     const std::string& session_id, const std::string& population_name,
61*14675a02SAndroid Build Coastguard Worker     fcp::client::LogManager& log_manager);
62*14675a02SAndroid Build Coastguard Worker 
63*14675a02SAndroid Build Coastguard Worker }  // namespace client
64*14675a02SAndroid Build Coastguard Worker }  // namespace fcp
65*14675a02SAndroid Build Coastguard Worker 
66*14675a02SAndroid Build Coastguard Worker #endif  // FCP_CLIENT_FEDERATED_PROTOCOL_UTIL_H_
67