Merge "bigtrace: Implement an initial service for Orchestrator and Worker" into main
diff --git a/protos/perfetto/bigtrace/BUILD.gn b/protos/perfetto/bigtrace/BUILD.gn
new file mode 100644
index 0000000..3784358
--- /dev/null
+++ b/protos/perfetto/bigtrace/BUILD.gn
@@ -0,0 +1,46 @@
+# Copyright (C) 2024 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import("../../../gn/proto_library.gni")
+
+perfetto_proto_library("worker_@TYPE@") {
+  proto_generators = [
+    "lite",
+    "zero",
+    "source_set",
+  ]
+  deps = [ "../trace_processor:@TYPE@" ]  # needed for descriptor.proto.
+  sources = [ "worker.proto" ]
+}
+
+perfetto_proto_library("orchestrator_@TYPE@") {
+  proto_generators = [
+    "lite",
+    "zero",
+    "source_set",
+  ]
+  sources = [ "orchestrator.proto" ]
+}
+
+if (enable_perfetto_grpc) {
+  perfetto_grpc_library("orchestrator_grpc") {
+    deps = [ ":orchestrator_lite" ]
+    sources = [ "orchestrator.proto" ]
+  }
+
+  perfetto_grpc_library("worker_grpc") {
+    deps = [ ":worker_lite" ]
+    sources = [ "worker.proto" ]
+  }
+}
diff --git a/protos/perfetto/bigtrace/orchestrator.proto b/protos/perfetto/bigtrace/orchestrator.proto
new file mode 100644
index 0000000..06557ef
--- /dev/null
+++ b/protos/perfetto/bigtrace/orchestrator.proto
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+syntax = "proto3";
+
+package perfetto.protos;
+
+import "protos/perfetto/trace_processor/trace_processor.proto";
+
+// gRPC Interface for a Bigtrace Orchestrator
+
+// Each Bigtrace instance has an orchestrator which is responsible for receiving
+// requests from the client and loading and querying traces by sharding them
+// across a set of "Workers"
+service BigtraceOrchestrator {
+  // Executes a SQL query on the specified list of traces and returns a stream
+  // of the result of the query for a given trace
+  rpc Query(BigtraceQueryArgs) returns (stream BigtraceQueryResponse) {}
+}
+
+// Request/Response for TraceListQuery
+message BigtraceQueryArgs {
+  repeated string traces = 1;
+  optional string sql_query = 2;
+}
+
+message BigtraceQueryResponse {
+  optional string trace = 1;
+  repeated QueryResult result = 2;
+}
diff --git a/protos/perfetto/bigtrace/worker.proto b/protos/perfetto/bigtrace/worker.proto
new file mode 100644
index 0000000..b0e658c
--- /dev/null
+++ b/protos/perfetto/bigtrace/worker.proto
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+syntax = "proto3";
+
+package perfetto.protos;
+
+import "protos/perfetto/trace_processor/trace_processor.proto";
+
+// gRPC Interface for a Bigtrace Worker
+//
+// Workers are owned by an "Orchestrator" which forward traces from requests by
+// end users. Workers are responsible for loading the traces with
+// TraceProcessor and executing the requests.
+service BigtraceWorker {
+  // Executes a SQL query on the specified trace and returns a stream of
+  // execution responses. Note that this method returns a stream because each
+  // trace can return >1 result due to chunking of protos at the
+  // TraceProcessor::QueryResult level.
+  rpc QueryTrace(BigtraceQueryTraceArgs) returns (BigtraceQueryTraceResponse);
+}
+
+// Request/Response for QueryTrace.
+message BigtraceQueryTraceArgs {
+  optional string trace = 1;
+  optional string sql_query = 2;
+}
+message BigtraceQueryTraceResponse {
+  optional string trace = 1;
+  repeated QueryResult result = 2;
+}
diff --git a/src/bigtrace/BUILD.gn b/src/bigtrace/BUILD.gn
index 734435c..f1072ab 100644
--- a/src/bigtrace/BUILD.gn
+++ b/src/bigtrace/BUILD.gn
@@ -24,6 +24,8 @@
     deps = [
       "../../gn:default_deps",
       "../../gn:grpc",
+      "../../protos/perfetto/bigtrace:orchestrator_grpc",
+      "../../protos/perfetto/bigtrace:worker_grpc",
       "../base",
     ]
   }
@@ -33,6 +35,7 @@
     deps = [
       "../../gn:default_deps",
       "../../gn:grpc",
+      "../../protos/perfetto/bigtrace:worker_grpc",
       "../base",
     ]
   }
diff --git a/src/bigtrace/orchestrator_main.cc b/src/bigtrace/orchestrator_main.cc
index 0a0a4b7..2f4aa57 100644
--- a/src/bigtrace/orchestrator_main.cc
+++ b/src/bigtrace/orchestrator_main.cc
@@ -15,28 +15,45 @@
  */
 
 #include <chrono>
+#include <memory>
 
 #include <grpcpp/grpcpp.h>
 
 #include "perfetto/base/status.h"
+#include "protos/perfetto/bigtrace/orchestrator.grpc.pb.h"
+#include "protos/perfetto/bigtrace/orchestrator.pb.h"
 
 namespace perfetto {
 namespace bigtrace {
 namespace {
 
+class OrchestratorImpl final : public protos::BigtraceOrchestrator::Service {
+  grpc::Status Query(
+      grpc::ServerContext*,
+      const protos::BigtraceQueryArgs*,
+      grpc::ServerWriter<protos::BigtraceQueryResponse>*) override {
+    return grpc::Status::OK;
+  }
+};
+
 base::Status OrchestratorMain(int, char**) {
-  std::string server_address("127.0.0.1:5051");
+  // Setup the Orchestrator Server
+  std::string server_address("localhost:5051");
+  auto service = std::make_unique<OrchestratorImpl>();
   grpc::ServerBuilder builder;
-  auto cq = builder.AddCompletionQueue();
+  builder.RegisterService(service.get());
   builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
 
+  // Setup the Orchestrator Client
+  std::string target_address("localhost:5052");
   auto channel =
-      grpc::CreateChannel("localhost:5052", grpc::InsecureChannelCredentials());
+      grpc::CreateChannel(target_address, grpc::InsecureChannelCredentials());
   bool connected = channel->WaitForConnected(std::chrono::system_clock::now() +
                                              std::chrono::milliseconds(5000));
 
   PERFETTO_CHECK(connected);
 
+  // Build and start the Orchestrator server
   std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
   PERFETTO_LOG("Orchestrator server listening on %s", server_address.c_str());
 
diff --git a/src/bigtrace/worker_main.cc b/src/bigtrace/worker_main.cc
index 2b877d7..05da8f2 100644
--- a/src/bigtrace/worker_main.cc
+++ b/src/bigtrace/worker_main.cc
@@ -17,18 +17,30 @@
 #include <grpcpp/grpcpp.h>
 
 #include "perfetto/base/status.h"
+#include "protos/perfetto/bigtrace/worker.grpc.pb.h"
+#include "protos/perfetto/bigtrace/worker.pb.h"
 
 namespace perfetto {
 namespace bigtrace {
 namespace {
 
+class WorkerImpl final : public protos::BigtraceWorker::Service {
+  grpc::Status QueryTrace(grpc::ServerContext*,
+                          const protos::BigtraceQueryTraceArgs*,
+                          protos::BigtraceQueryTraceResponse*) override {
+    return grpc::Status::OK;
+  }
+};
+
 base::Status WorkerMain(int, char**) {
-  std::string server_address("127.0.0.1:5052");
+  // Setup the Worker Server
+  std::string server_address("localhost:5052");
+  auto service = std::make_unique<WorkerImpl>();
   grpc::ServerBuilder builder;
-  auto cq = builder.AddCompletionQueue();
+  builder.RegisterService(service.get());
   builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
   std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
-  PERFETTO_LOG("Orchestrator server listening on %s", server_address.c_str());
+  PERFETTO_LOG("Worker server listening on %s", server_address.c_str());
 
   server->Wait();