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