aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java
blob: 7a03c16ebabe94db250a0e0c1b7230fea3be830e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// 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.

package com.google.devtools.build.lib.server;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.runtime.CommandExecutor;
import com.google.devtools.build.lib.util.Clock;
import com.google.devtools.build.lib.util.Preconditions;
import com.google.devtools.build.lib.util.io.OutErr;

import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;

/**
 * gRPC server class.
 *
 * <p>Only this class should depend on gRPC so that we only need to exclude this during
 * bootstrapping.
 */
public class GrpcServerImpl implements CommandServerGrpc.CommandServer, GrpcServer {
  private static final String PORT_FILE = "server/grpc_port";  // relative to the output base
  private final CommandExecutor commandExecutor;
  private final Clock clock;
  private final File portFile;
  private Server server;
  private int port;  // mutable so that we can overwrite it if port 0 is passed in
  boolean serving;

  /**
   * Factory class. Instantiated by reflection.
   */
  public static class Factory implements GrpcServer.Factory {
    @Override
    public GrpcServer create(CommandExecutor commandExecutor, Clock clock, int port,
      String outputBase) {
      return new GrpcServerImpl(commandExecutor, clock, port, outputBase);
    }
  }

  private GrpcServerImpl(CommandExecutor commandExecutor, Clock clock, int port,
      String outputBase) {
    this.commandExecutor = commandExecutor;
    this.clock = clock;
    this.port = port;
    this.portFile = new File(outputBase + "/" + PORT_FILE);
    this.serving = false;
  }

  public void serve() throws IOException {
    Preconditions.checkState(!serving);
    server = ServerBuilder.forPort(port)
        .addService(CommandServerGrpc.bindService(this))
        .build();

    server.start();
    if (port == 0) {
      port = getActualServerPort();
    }

    PrintWriter portWriter = new PrintWriter(portFile);
    portWriter.print(port);
    portWriter.close();
    serving = true;
  }

  /**
   * Gets the server port the kernel bound our server to if port 0 was passed in.
   *
   * <p>The implementation is awful, but gRPC doesn't provide an official way to do this:
   * https://github.com/grpc/grpc-java/issues/72
   */
  private int getActualServerPort() {
    try {
      ServerSocketChannel channel =
          (ServerSocketChannel) getField(server, "transportServer", "channel", "ch");
      InetSocketAddress address = (InetSocketAddress) channel.getLocalAddress();
      return address.getPort();
    } catch (IllegalAccessException | NullPointerException | IOException e) {
      throw new IllegalStateException("Cannot read server socket address from gRPC");
    }
  }

  private static Object getField(Object instance, String... fieldNames)
    throws IllegalAccessException, NullPointerException {
    for (String fieldName : fieldNames) {
      Field field = null;
      for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
        try {
          field = clazz.getDeclaredField(fieldName);
          break;
        } catch (NoSuchFieldException e) {
          // Try again with the superclass
        }
      }
      field.setAccessible(true);
      instance = field.get(instance);
    }

    return instance;
  }

  public void terminate() {
    Preconditions.checkState(serving);
    server.shutdownNow();
    // This is Uninterruptibles#callUninterruptibly. Calling that method properly is about the same
    // amount of code as implementing it ourselves.
    boolean interrupted = false;
    try {
      while (true) {
        try {
          server.awaitTermination();
          serving = false;
          return;
        } catch (InterruptedException e) {
          interrupted = true;
        }
      }
    } finally {
      if (interrupted) {
        Thread.currentThread().interrupt();
      }
    }
  }

  @Override
  public void run(CommandProtos.Request request, StreamObserver<CommandProtos.Response> observer) {
    Preconditions.checkState(serving);
    commandExecutor.exec(
        ImmutableList.of("version"), OutErr.SYSTEM_OUT_ERR, clock.currentTimeMillis());
    CommandProtos.Response response = CommandProtos.Response.newBuilder()
        .setNumber(request.getNumber() + 1)
        .build();
    observer.onNext(response);
    observer.onCompleted();
  }
}