From b7f5605d0634b8e571665bfb5c407f091e1ac4a4 Mon Sep 17 00:00:00 2001 From: michajlo Date: Wed, 21 Aug 2019 11:16:18 -0700 Subject: [PATCH] Create proto file containing server metadata on server startup This is a combination of all of the other server files (except commandline). Meant to be used with exec-server to determine when the server is online and ready. It might also be useful to remove the individual info files (command_port, request_cookie, response_cookie) in favor of this one. RELNOTES: None PiperOrigin-RevId: 264650984 --- .../build/lib/server/GrpcServerImpl.java | 31 ++++++++++++++++--- src/main/protobuf/command_server.proto | 16 ++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java b/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java index b274e71cefe57e..1791539ce86bf7 100644 --- a/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java +++ b/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java @@ -34,6 +34,7 @@ import com.google.devtools.build.lib.server.CommandProtos.PingResponse; import com.google.devtools.build.lib.server.CommandProtos.RunRequest; import com.google.devtools.build.lib.server.CommandProtos.RunResponse; +import com.google.devtools.build.lib.server.CommandProtos.ServerInfo; import com.google.devtools.build.lib.server.CommandProtos.StartupOption; import com.google.devtools.build.lib.util.ExitCode; import com.google.devtools.build.lib.util.Pair; @@ -314,6 +315,7 @@ public void run() { private static final String PORT_FILE = "command_port"; private static final String REQUEST_COOKIE_FILE = "request_cookie"; private static final String RESPONSE_COOKIE_FILE = "response_cookie"; + private static final String SERVER_INFO_FILE = "server_info.rawproto"; private static final AtomicBoolean runShutdownHooks = new AtomicBoolean(true); @@ -445,10 +447,7 @@ public void serve() throws IOException { } serving = true; - writeServerFile( - PORT_FILE, InetAddresses.toUriString(address.getAddress()) + ":" + server.getPort()); - writeServerFile(REQUEST_COOKIE_FILE, requestCookie); - writeServerFile(RESPONSE_COOKIE_FILE, responseCookie); + writeServerStatusFiles(address); try { server.awaitTermination(); @@ -458,6 +457,30 @@ public void serve() throws IOException { } } + private void writeServerStatusFiles(InetSocketAddress address) throws IOException { + writeServerFile( + PORT_FILE, InetAddresses.toUriString(address.getAddress()) + ":" + server.getPort()); + writeServerFile(REQUEST_COOKIE_FILE, requestCookie); + writeServerFile(RESPONSE_COOKIE_FILE, responseCookie); + + ServerInfo info = + ServerInfo.newBuilder() + .setPid(Integer.parseInt(pidInFile)) + .setAddress(address.getAddress() + ":" + server.getPort()) + .setRequestCookie(requestCookie) + .setResponseCookie(responseCookie) + .build(); + + // Write then mv so the user never sees incomplete contents. + Path serverInfoTmpFile = serverDirectory.getChild(SERVER_INFO_FILE + ".tmp"); + try (OutputStream out = serverInfoTmpFile.getOutputStream()) { + info.writeTo(out); + } + Path serverInfoFile = serverDirectory.getChild(SERVER_INFO_FILE); + serverInfoTmpFile.renameTo(serverInfoFile); + deleteAtExit(serverInfoFile); + } + private void writeServerFile(String name, String contents) throws IOException { Path file = serverDirectory.getChild(name); FileSystemUtils.writeContentAsLatin1(file, contents); diff --git a/src/main/protobuf/command_server.proto b/src/main/protobuf/command_server.proto index bad35abdb676f5..fb484a87520cc2 100644 --- a/src/main/protobuf/command_server.proto +++ b/src/main/protobuf/command_server.proto @@ -142,6 +142,22 @@ message PingResponse { string cookie = 1; } +// Describes metadata necessary for connecting to and managing the server. +message ServerInfo { + // The server process's pid. + int32 pid = 1; + + // Address the CommandServer is listening on. Can be passed directly to grpc + // to create a connection. + string address = 2; + + // Client request cookie. + string request_cookie = 3; + + // Server response cookie. + string response_cookie = 4; +} + service CommandServer { // Run a Bazel command. See documentation of argument/return messages for // details.