Skip to content

Commit

Permalink
Create proto file containing server metadata on server startup
Browse files Browse the repository at this point in the history
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
  • Loading branch information
michajlo authored and copybara-github committed Aug 21, 2019
1 parent 447e0f1 commit b7f5605
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand All @@ -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);

This comment has been minimized.

Copy link
@meteorcloudy

meteorcloudy Oct 11, 2019

Member

We probably should close the output stream before we rename the file otherwise it will fail on Windows. Because Windows doesn't allow deleting file while it's still open.

This comment has been minimized.

Copy link
@meteorcloudy

meteorcloudy Oct 11, 2019

Member

Oh, sorry. The try clause should already ensure the out stream is closed.

deleteAtExit(serverInfoFile);
}

private void writeServerFile(String name, String contents) throws IOException {
Path file = serverDirectory.getChild(name);
FileSystemUtils.writeContentAsLatin1(file, contents);
Expand Down
16 changes: 16 additions & 0 deletions src/main/protobuf/command_server.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b7f5605

Please sign in to comment.