xref: /aosp_15_r20/external/federated-compute/fcp/client/fake_event_publisher.h (revision 14675a029014e728ec732f129a32e299b2da0601)
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 
17 #ifndef FCP_CLIENT_FAKE_EVENT_PUBLISHER_H_
18 #define FCP_CLIENT_FAKE_EVENT_PUBLISHER_H_
19 
20 #include "absl/strings/str_split.h"
21 #include "absl/time/time.h"
22 #include "fcp/base/monitoring.h"
23 #include "fcp/client/event_publisher.h"
24 #include "fcp/client/secagg_event_publisher.h"
25 #include "fcp/client/stats.h"
26 
27 namespace fcp {
28 namespace client {
29 
30 class SecAggEventPublisher;
31 
32 // Macro to print log messages prefixed by ClassName::FunctionName, stripping
33 // namespaces before ClassName, if any.
34 #define FCP_CLIENT_LOG_FUNCTION_NAME \
35   FCP_LOG(INFO) << "::" << __func__; \
36   // std::string _demangle_buf(1024, '\0');                          \
37   // size_t _demangle_buf_len = _demangle_buf.length();              \
38   // abi::__cxa_demangle(typeid(*this).name(), _demangle_buf.data(), \
39   //                     &_demangle_buf_len, nullptr);               \
40   // FCP_LOG(INFO) << static_cast<std::vector<std::string>>(         \
41   //                      absl::StrSplit(_demangle_buf, "::"))       \
42   //                      .back()                                    \
43   //                      .c_str()                                   \
44   //               << "::" << __func__
45 
46 // An implementation of the SecAggEventPublisher interface that logs calls to
47 // stderr.
48 class SecAggLoggingEventPublisher : public SecAggEventPublisher {
49  public:
SecAggLoggingEventPublisher(bool quiet)50   explicit SecAggLoggingEventPublisher(bool quiet) : quiet_(quiet) {}
51 
PublishStateTransition(::fcp::secagg::ClientState state,size_t last_sent_message_size,size_t last_received_message_size)52   void PublishStateTransition(::fcp::secagg::ClientState state,
53                               size_t last_sent_message_size,
54                               size_t last_received_message_size) override {
55     if (quiet_) return;
56     FCP_CLIENT_LOG_FUNCTION_NAME;
57   }
PublishError()58   void PublishError() override { FCP_CLIENT_LOG_FUNCTION_NAME; }
PublishAbort(bool client_initiated,const std::string & error_message)59   void PublishAbort(bool client_initiated,
60                     const std::string& error_message) override {
61     FCP_CLIENT_LOG_FUNCTION_NAME;
62   }
set_execution_session_id(int64_t execution_session_id)63   void set_execution_session_id(int64_t execution_session_id) override {
64     FCP_CLIENT_LOG_FUNCTION_NAME;
65   }
66 
67  private:
68   const bool quiet_;
69 };
70 
71 // An implementation of the EventPublisher interface that logs calls to stderr.
72 class FakeEventPublisher : public EventPublisher {
73  public:
74   // Logs all events to stderr.
FakeEventPublisher()75   FakeEventPublisher() : FakeEventPublisher(/*quiet=*/false) {}
76   // Logs only error and "client rejected" events to stderr.
FakeEventPublisher(bool quiet)77   explicit FakeEventPublisher(bool quiet)
78       : quiet_(quiet), secagg_event_publisher_(quiet) {}
79 
PublishEligibilityEvalCheckin()80   void PublishEligibilityEvalCheckin() override {
81     if (quiet_) return;
82     FCP_CLIENT_LOG_FUNCTION_NAME;
83   }
PublishEligibilityEvalPlanUriReceived(const NetworkStats &,absl::Duration)84   void PublishEligibilityEvalPlanUriReceived(const NetworkStats&,
85                                              absl::Duration) override {
86     if (quiet_) return;
87     FCP_CLIENT_LOG_FUNCTION_NAME;
88   }
89 
PublishEligibilityEvalPlanReceived(const NetworkStats &,absl::Duration)90   void PublishEligibilityEvalPlanReceived(const NetworkStats&,
91                                           absl::Duration) override {
92     if (quiet_) return;
93     FCP_CLIENT_LOG_FUNCTION_NAME;
94   }
95 
PublishEligibilityEvalNotConfigured(const NetworkStats &,absl::Duration)96   void PublishEligibilityEvalNotConfigured(const NetworkStats&,
97                                            absl::Duration) override {
98     if (quiet_) return;
99     FCP_CLIENT_LOG_FUNCTION_NAME;
100   }
101 
PublishEligibilityEvalRejected(const NetworkStats &,absl::Duration)102   void PublishEligibilityEvalRejected(const NetworkStats&,
103                                       absl::Duration) override {
104     FCP_CLIENT_LOG_FUNCTION_NAME;
105   }
106 
PublishCheckin()107   void PublishCheckin() override {
108     if (quiet_) return;
109     FCP_CLIENT_LOG_FUNCTION_NAME;
110   }
111 
PublishCheckinFinished(const NetworkStats & network_stats,absl::Duration phase_duration)112   void PublishCheckinFinished(const NetworkStats& network_stats,
113                               absl::Duration phase_duration) override {
114     if (quiet_) return;
115     FCP_CLIENT_LOG_FUNCTION_NAME;
116   }
117 
PublishRejected()118   void PublishRejected() override { FCP_CLIENT_LOG_FUNCTION_NAME; }
119 
PublishReportStarted(int64_t report_size_bytes)120   void PublishReportStarted(int64_t report_size_bytes) override {
121     if (quiet_) return;
122     FCP_CLIENT_LOG_FUNCTION_NAME;
123   }
124 
PublishReportFinished(const NetworkStats & network_stats,absl::Duration report_duration)125   void PublishReportFinished(const NetworkStats& network_stats,
126                              absl::Duration report_duration) override {
127     if (quiet_) return;
128     FCP_CLIENT_LOG_FUNCTION_NAME;
129   }
130 
PublishPlanExecutionStarted()131   void PublishPlanExecutionStarted() override {
132     if (quiet_) return;
133     FCP_CLIENT_LOG_FUNCTION_NAME;
134   }
135 
PublishTensorFlowError(int example_count,absl::string_view error_message)136   void PublishTensorFlowError(int example_count,
137                               absl::string_view error_message) override {
138     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
139   }
140 
PublishIoError(absl::string_view error_message)141   void PublishIoError(absl::string_view error_message) override {
142     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
143   }
144 
PublishExampleSelectorError(int example_count,absl::string_view error_message)145   void PublishExampleSelectorError(int example_count,
146                                    absl::string_view error_message) override {
147     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
148   }
149 
PublishInterruption(const ExampleStats & example_stats,absl::Time start_time)150   void PublishInterruption(const ExampleStats& example_stats,
151                            absl::Time start_time) override {
152     if (quiet_) return;
153     FCP_CLIENT_LOG_FUNCTION_NAME;
154   }
155 
PublishPlanCompleted(const ExampleStats & example_stats,absl::Time start_time)156   void PublishPlanCompleted(const ExampleStats& example_stats,
157                             absl::Time start_time) override {
158     if (quiet_) return;
159     FCP_CLIENT_LOG_FUNCTION_NAME;
160   }
161 
PublishTaskNotStarted(absl::string_view error_message)162   void PublishTaskNotStarted(absl::string_view error_message) override {
163     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
164   }
165 
PublishNonfatalInitializationError(absl::string_view error_message)166   void PublishNonfatalInitializationError(
167       absl::string_view error_message) override {
168     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
169   }
170 
PublishFatalInitializationError(absl::string_view error_message)171   void PublishFatalInitializationError(
172       absl::string_view error_message) override {
173     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
174   }
175 
PublishEligibilityEvalCheckinIoError(absl::string_view error_message,const NetworkStats & network_stats,absl::Duration phase_duration)176   void PublishEligibilityEvalCheckinIoError(
177       absl::string_view error_message, const NetworkStats& network_stats,
178       absl::Duration phase_duration) override {
179     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
180   }
181 
PublishEligibilityEvalCheckinClientInterrupted(absl::string_view error_message,const NetworkStats & network_stats,absl::Duration phase_duration)182   void PublishEligibilityEvalCheckinClientInterrupted(
183       absl::string_view error_message, const NetworkStats& network_stats,
184       absl::Duration phase_duration) override {
185     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
186   }
187 
PublishEligibilityEvalCheckinServerAborted(absl::string_view error_message,const NetworkStats & network_stats,absl::Duration phase_duration)188   void PublishEligibilityEvalCheckinServerAborted(
189       absl::string_view error_message, const NetworkStats& network_stats,
190       absl::Duration phase_duration) override {
191     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
192   }
193 
PublishEligibilityEvalCheckinErrorInvalidPayload(absl::string_view error_message,const NetworkStats & network_stats,absl::Duration phase_duration)194   void PublishEligibilityEvalCheckinErrorInvalidPayload(
195       absl::string_view error_message, const NetworkStats& network_stats,
196       absl::Duration phase_duration) override {
197     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
198   }
199 
PublishEligibilityEvalComputationStarted()200   void PublishEligibilityEvalComputationStarted() override {
201     if (quiet_) return;
202     FCP_CLIENT_LOG_FUNCTION_NAME;
203   }
204 
PublishEligibilityEvalComputationInvalidArgument(absl::string_view error_message,const ExampleStats & example_stats,absl::Duration phase_duration)205   void PublishEligibilityEvalComputationInvalidArgument(
206       absl::string_view error_message, const ExampleStats& example_stats,
207       absl::Duration phase_duration) override {
208     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
209   }
210 
PublishEligibilityEvalComputationExampleIteratorError(absl::string_view error_message,const ExampleStats & example_stats,absl::Duration phase_duration)211   void PublishEligibilityEvalComputationExampleIteratorError(
212       absl::string_view error_message, const ExampleStats& example_stats,
213       absl::Duration phase_duration) override {
214     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
215   }
216 
PublishEligibilityEvalComputationTensorflowError(absl::string_view error_message,const ExampleStats & example_stats,absl::Duration phase_duration)217   void PublishEligibilityEvalComputationTensorflowError(
218       absl::string_view error_message, const ExampleStats& example_stats,
219       absl::Duration phase_duration) override {
220     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
221   }
222 
PublishEligibilityEvalComputationInterrupted(absl::string_view error_message,const ExampleStats & example_stats,absl::Duration phase_duration)223   void PublishEligibilityEvalComputationInterrupted(
224       absl::string_view error_message, const ExampleStats& example_stats,
225       absl::Duration phase_duration) override {
226     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
227   }
228 
PublishEligibilityEvalComputationCompleted(const ExampleStats & example_stats,absl::Duration phase_duration)229   void PublishEligibilityEvalComputationCompleted(
230       const ExampleStats& example_stats,
231       absl::Duration phase_duration) override {
232     if (quiet_) return;
233     FCP_CLIENT_LOG_FUNCTION_NAME;
234   }
235 
PublishCheckinIoError(absl::string_view error_message,const NetworkStats & network_stats,absl::Duration phase_duration)236   void PublishCheckinIoError(absl::string_view error_message,
237                              const NetworkStats& network_stats,
238                              absl::Duration phase_duration) override {
239     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
240   }
241 
PublishCheckinClientInterrupted(absl::string_view error_message,const NetworkStats & network_stats,absl::Duration phase_duration)242   void PublishCheckinClientInterrupted(absl::string_view error_message,
243                                        const NetworkStats& network_stats,
244                                        absl::Duration phase_duration) override {
245     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
246   }
247 
PublishCheckinServerAborted(absl::string_view error_message,const NetworkStats & network_stats,absl::Duration phase_duration)248   void PublishCheckinServerAborted(absl::string_view error_message,
249                                    const NetworkStats& network_stats,
250                                    absl::Duration phase_duration) override {
251     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
252   }
253 
PublishCheckinInvalidPayload(absl::string_view error_message,const NetworkStats & network_stats,absl::Duration phase_duration)254   void PublishCheckinInvalidPayload(absl::string_view error_message,
255                                     const NetworkStats& network_stats,
256                                     absl::Duration phase_duration) override {
257     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
258   }
259 
PublishRejected(const NetworkStats & network_stats,absl::Duration phase_duration)260   void PublishRejected(const NetworkStats& network_stats,
261                        absl::Duration phase_duration) override {
262     FCP_CLIENT_LOG_FUNCTION_NAME;
263   }
264 
PublishCheckinPlanUriReceived(const NetworkStats & network_stats,absl::Duration)265   void PublishCheckinPlanUriReceived(const NetworkStats& network_stats,
266                                      absl::Duration) override {
267     if (quiet_) return;
268     FCP_CLIENT_LOG_FUNCTION_NAME;
269   }
PublishCheckinFinishedV2(const NetworkStats & network_stats,absl::Duration phase_duration)270   void PublishCheckinFinishedV2(const NetworkStats& network_stats,
271                                 absl::Duration phase_duration) override {
272     if (quiet_) return;
273     FCP_CLIENT_LOG_FUNCTION_NAME;
274   }
275 
PublishComputationStarted()276   void PublishComputationStarted() override {
277     if (quiet_) return;
278     FCP_CLIENT_LOG_FUNCTION_NAME;
279   }
280 
PublishComputationInvalidArgument(absl::string_view error_message,const ExampleStats & example_stats,const NetworkStats & network_stats,absl::Duration phase_duration)281   void PublishComputationInvalidArgument(
282       absl::string_view error_message, const ExampleStats& example_stats,
283       const NetworkStats& network_stats,
284       absl::Duration phase_duration) override {
285     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
286   }
287 
PublishComputationIOError(absl::string_view error_message,const ExampleStats & example_stats,const NetworkStats & network_stats,absl::Duration phase_duration)288   void PublishComputationIOError(absl::string_view error_message,
289                                  const ExampleStats& example_stats,
290                                  const NetworkStats& network_stats,
291                                  absl::Duration phase_duration) override {
292     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
293   }
294 
PublishComputationExampleIteratorError(absl::string_view error_message,const ExampleStats & example_stats,const NetworkStats & network_stats,absl::Duration phase_duration)295   void PublishComputationExampleIteratorError(
296       absl::string_view error_message, const ExampleStats& example_stats,
297       const NetworkStats& network_stats,
298       absl::Duration phase_duration) override {
299     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
300   }
301 
PublishComputationTensorflowError(absl::string_view error_message,const ExampleStats & example_stats,const NetworkStats & network_stats,absl::Duration phase_duration)302   void PublishComputationTensorflowError(
303       absl::string_view error_message, const ExampleStats& example_stats,
304       const NetworkStats& network_stats,
305       absl::Duration phase_duration) override {
306     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
307   }
308 
PublishComputationInterrupted(absl::string_view error_message,const ExampleStats & example_stats,const NetworkStats & network_stats,absl::Duration phase_duration)309   void PublishComputationInterrupted(absl::string_view error_message,
310                                      const ExampleStats& example_stats,
311                                      const NetworkStats& network_stats,
312                                      absl::Duration phase_duration) override {
313     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
314   }
315 
PublishComputationCompleted(const ExampleStats & example_stats,const NetworkStats & network_stats,absl::Duration phase_duration)316   void PublishComputationCompleted(const ExampleStats& example_stats,
317                                    const NetworkStats& network_stats,
318                                    absl::Duration phase_duration) override {
319     if (quiet_) return;
320     FCP_CLIENT_LOG_FUNCTION_NAME;
321   }
322 
PublishResultUploadStarted()323   void PublishResultUploadStarted() override {
324     if (quiet_) return;
325     FCP_CLIENT_LOG_FUNCTION_NAME;
326   }
327 
PublishResultUploadIOError(absl::string_view error_message,const NetworkStats & network_stats,absl::Duration phase_duration)328   void PublishResultUploadIOError(absl::string_view error_message,
329                                   const NetworkStats& network_stats,
330                                   absl::Duration phase_duration) override {
331     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
332   }
333 
PublishResultUploadClientInterrupted(absl::string_view error_message,const NetworkStats & network_stats,absl::Duration phase_duration)334   void PublishResultUploadClientInterrupted(
335       absl::string_view error_message, const NetworkStats& network_stats,
336       absl::Duration phase_duration) override {
337     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
338   }
339 
PublishResultUploadServerAborted(absl::string_view error_message,const NetworkStats & network_stats,absl::Duration phase_duration)340   void PublishResultUploadServerAborted(
341       absl::string_view error_message, const NetworkStats& network_stats,
342       absl::Duration phase_duration) override {
343     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
344   }
345 
PublishResultUploadCompleted(const NetworkStats & network_stats,absl::Duration phase_duration)346   void PublishResultUploadCompleted(const NetworkStats& network_stats,
347                                     absl::Duration phase_duration) override {
348     if (quiet_) return;
349     FCP_CLIENT_LOG_FUNCTION_NAME;
350   }
351 
PublishFailureUploadStarted()352   void PublishFailureUploadStarted() override {
353     if (quiet_) return;
354     FCP_CLIENT_LOG_FUNCTION_NAME;
355   }
356 
PublishFailureUploadIOError(absl::string_view error_message,const NetworkStats & network_stats,absl::Duration phase_duration)357   void PublishFailureUploadIOError(absl::string_view error_message,
358                                    const NetworkStats& network_stats,
359                                    absl::Duration phase_duration) override {
360     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
361   }
362 
PublishFailureUploadClientInterrupted(absl::string_view error_message,const NetworkStats & network_stats,absl::Duration phase_duration)363   void PublishFailureUploadClientInterrupted(
364       absl::string_view error_message, const NetworkStats& network_stats,
365       absl::Duration phase_duration) override {
366     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
367   }
368 
PublishFailureUploadServerAborted(absl::string_view error_message,const NetworkStats & network_stats,absl::Duration phase_duration)369   void PublishFailureUploadServerAborted(
370       absl::string_view error_message, const NetworkStats& network_stats,
371       absl::Duration phase_duration) override {
372     // FCP_CLIENT_LOG_FUNCTION_NAME << error_message;
373   }
374 
PublishFailureUploadCompleted(const NetworkStats & network_stats,absl::Duration phase_duration)375   void PublishFailureUploadCompleted(const NetworkStats& network_stats,
376                                      absl::Duration phase_duration) override {
377     if (quiet_) return;
378     FCP_CLIENT_LOG_FUNCTION_NAME;
379   }
380 
SetModelIdentifier(const std::string & model_identifier)381   void SetModelIdentifier(const std::string& model_identifier) override {
382     if (quiet_) return;
383     // FCP_CLIENT_LOG_FUNCTION_NAME << ":\n\t" << model_identifier;
384   }
385 
secagg_event_publisher()386   SecAggEventPublisher* secagg_event_publisher() override {
387     return &secagg_event_publisher_;
388   }
389 
390  private:
391   const bool quiet_;
392   SecAggLoggingEventPublisher secagg_event_publisher_;
393 };
394 
395 }  // namespace client
396 }  // namespace fcp
397 
398 #endif  // FCP_CLIENT_FAKE_EVENT_PUBLISHER_H_
399