Retry logs write when it fails.
Report skipped event in statsd.

Test: manual test
BUG: b/77222120
Change-Id: I257f5e76d557893c4eb4a8e8a13396d8b5d1afc0
diff --git a/stats/stats_log_api_gen/Android.bp b/stats/stats_log_api_gen/Android.bp
index 948422c..17819db 100644
--- a/stats/stats_log_api_gen/Android.bp
+++ b/stats/stats_log_api_gen/Android.bp
@@ -101,7 +101,6 @@
     cflags: [
         "-Wall",
         "-Werror",
-        "-fexceptions",
     ],
     export_generated_headers: ["statslog.h"],
     shared_libs: [
diff --git a/stats/stats_log_api_gen/main.cpp b/stats/stats_log_api_gen/main.cpp
index 499f254..057772f 100644
--- a/stats/stats_log_api_gen/main.cpp
+++ b/stats/stats_log_api_gen/main.cpp
@@ -101,7 +101,8 @@
     fprintf(out, "// This file is autogenerated\n");
     fprintf(out, "\n");
 
-    fprintf(out, "#include <exception>\n");
+    fprintf(out, "#include <chrono>\n");
+    fprintf(out, "#include <thread>\n");
     fprintf(out, "#include <log/log_event_list.h>\n");
     fprintf(out, "#include <log/log.h>\n");
     fprintf(out, "#include <statslog.h>\n");
@@ -212,7 +213,7 @@
         int argIndex;
 
         fprintf(out, "int\n");
-        fprintf(out, "stats_write(int32_t code");
+        fprintf(out, "try_stats_write(int32_t code");
         argIndex = 1;
         for (vector<java_type_t>::const_iterator arg = signature->begin();
             arg != signature->end(); arg++) {
@@ -247,10 +248,6 @@
                     if (chainField.javaType == JAVA_TYPE_STRING) {
                         fprintf(out, "    if (%s_length != %s.size()) {\n",
                             attributionDecl.fields.front().name.c_str(), chainField.name.c_str());
-                        fprintf(out, "        throw std::invalid_argument(\"attribution fields with"
-                            " diff length: %s vs %s\");\n",
-                            attributionDecl.fields.front().name.c_str(),
-                            chainField.name.c_str());
                         fprintf(out, "        return -EINVAL;\n");
                         fprintf(out, "    }\n");
                     }
@@ -289,12 +286,74 @@
         fprintf(out, "\n");
     }
 
+   for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
+       signature != atoms.signatures.end(); signature++) {
+       int argIndex;
+
+       fprintf(out, "int \n");
+       fprintf(out, "stats_write(int32_t code");
+       argIndex = 1;
+       for (vector<java_type_t>::const_iterator arg = signature->begin();
+           arg != signature->end(); arg++) {
+           if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
+               for (auto chainField : attributionDecl.fields) {
+                   if (chainField.javaType == JAVA_TYPE_STRING) {
+                           fprintf(out, ", const std::vector<%s>& %s",
+                                cpp_type_name(chainField.javaType),
+                                chainField.name.c_str());
+                   } else {
+                           fprintf(out, ", const %s* %s, size_t %s_length",
+                                cpp_type_name(chainField.javaType),
+                                chainField.name.c_str(), chainField.name.c_str());
+                   }
+               }
+           } else {
+               fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
+           }
+           argIndex++;
+       }
+       fprintf(out, ")\n");
+
+       fprintf(out, "{\n");
+       fprintf(out, "  int ret = 0;\n");
+
+       fprintf(out, "  for(int retry = 0; retry < 3; ++retry) {\n");
+       fprintf(out, "      ret =  try_stats_write(code");
+
+       argIndex = 1;
+       for (vector<java_type_t>::const_iterator arg = signature->begin();
+           arg != signature->end(); arg++) {
+           if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
+               for (auto chainField : attributionDecl.fields) {
+                   if (chainField.javaType == JAVA_TYPE_STRING) {
+                           fprintf(out, ", %s",
+                                chainField.name.c_str());
+                   } else {
+                           fprintf(out, ",  %s,  %s_length",
+                                chainField.name.c_str(), chainField.name.c_str());
+                   }
+               }
+           } else {
+               fprintf(out, ", arg%d", argIndex);
+           }
+           argIndex++;
+       }
+       fprintf(out, ");\n");
+       fprintf(out, "      if (ret >= 0) { return retry; }\n");
+       fprintf(out,
+               "      std::this_thread::sleep_for(std::chrono::milliseconds(10 + 10 * retry));\n");
+       fprintf(out, "  }\n");
+       fprintf(out, "  return ret;\n");
+       fprintf(out, "}\n");
+       fprintf(out, "\n");
+   }
+
     for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin();
         signature != atoms.non_chained_signatures.end(); signature++) {
         int argIndex;
 
         fprintf(out, "int\n");
-        fprintf(out, "stats_write_non_chained(int32_t code");
+        fprintf(out, "try_stats_write_non_chained(int32_t code");
         argIndex = 1;
         for (vector<java_type_t>::const_iterator arg = signature->begin();
             arg != signature->end(); arg++) {
@@ -331,6 +390,45 @@
         fprintf(out, "}\n");
         fprintf(out, "\n");
     }
+
+   for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin();
+       signature != atoms.non_chained_signatures.end(); signature++) {
+       int argIndex;
+
+       fprintf(out, "int\n");
+       fprintf(out, "stats_write_non_chained(int32_t code");
+       argIndex = 1;
+       for (vector<java_type_t>::const_iterator arg = signature->begin();
+           arg != signature->end(); arg++) {
+           fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
+           argIndex++;
+       }
+       fprintf(out, ")\n");
+
+       fprintf(out, "{\n");
+
+       fprintf(out, "  int ret = 0;\n");
+       fprintf(out, "  for(int retry = 0; retry < 3; ++retry) {\n");
+       fprintf(out, "      ret =  try_stats_write_non_chained(code");
+
+       argIndex = 1;
+       for (vector<java_type_t>::const_iterator arg = signature->begin();
+           arg != signature->end(); arg++) {
+           fprintf(out, ", arg%d",   argIndex);
+           argIndex++;
+       }
+       fprintf(out, ");\n");
+       fprintf(out, "      if (ret >= 0) { return retry; }\n");
+       fprintf(out,
+               "      std::this_thread::sleep_for(std::chrono::milliseconds(10 + 10 * retry));\n");
+       fprintf(out, "  }\n");
+       fprintf(out, "  return ret;\n");
+       fprintf(out, "}\n");
+
+       fprintf(out, "\n");
+   }
+
+
     // Print footer
     fprintf(out, "\n");
     fprintf(out, "} // namespace util\n");
@@ -775,10 +873,6 @@
                         fprintf(out, "    if (%s_length != %s_length) {\n",
                             chainField.name.c_str(),
                             attributionDecl.fields.front().name.c_str());
-                        fprintf(out, "        jniThrowException(env, "
-                            "\"java/lang/IllegalArgumentException\", "
-                            "\"invalid attribution field(%s) length.\");\n",
-                            chainField.name.c_str());
                         fprintf(out, "        return -EINVAL;\n");
                         fprintf(out, "    }\n");
                     }