Class ServerImpl

java.lang.Object
io.grpc.Server
io.grpc.internal.ServerImpl
All Implemented Interfaces:
InternalInstrumented<InternalChannelz.ServerStats>, InternalWithLogId

public final class ServerImpl extends Server implements InternalInstrumented<InternalChannelz.ServerStats>
Default implementation of Server, for creation by transports.

Expected usage (by a theoretical TCP transport):

public class TcpTransportServerFactory {
   public static Server newServer(Executor executor, HandlerRegistry registry,
       String configuration) {
     return new ServerImpl(executor, registry, new TcpTransportServer(configuration));
   }
 }

Starting the server starts the underlying transport for servicing requests. Stopping the server stops servicing new requests and waits for all connections to terminate.

  • Field Details

    • log

      private static final Logger log
    • NOOP_LISTENER

      private static final ServerStreamListener NOOP_LISTENER
    • logId

      private final InternalLogId logId
    • executorPool

      private final ObjectPool<? extends Executor> executorPool
    • executor

      private Executor executor
      Executor for application processing. Safe to read after start().
    • registry

      private final HandlerRegistry registry
    • fallbackRegistry

      private final HandlerRegistry fallbackRegistry
    • transportFilters

      private final List<ServerTransportFilter> transportFilters
    • interceptors

      private final ServerInterceptor[] interceptors
    • handshakeTimeoutMillis

      private final long handshakeTimeoutMillis
    • started

      private boolean started
    • shutdown

      private boolean shutdown
    • shutdownNowStatus

      private Status shutdownNowStatus
      non-null if immediate shutdown has been requested.
    • serverShutdownCallbackInvoked

      private boolean serverShutdownCallbackInvoked
      true if ServerListenerImpl.serverShutdown() was called.
    • terminated

      private boolean terminated
    • transportServer

      private final InternalServer transportServer
      Service encapsulating something similar to an accept() socket.
    • lock

      private final Object lock
    • transportServersTerminated

      private boolean transportServersTerminated
    • transports

      private final Set<ServerTransport> transports
      transportServer and services encapsulating something similar to a TCP connection.
    • rootContext

      private final Context rootContext
    • decompressorRegistry

      private final DecompressorRegistry decompressorRegistry
    • compressorRegistry

      private final CompressorRegistry compressorRegistry
    • binlog

      private final BinaryLog binlog
    • channelz

      private final InternalChannelz channelz
    • serverCallTracer

      private final CallTracer serverCallTracer
    • ticker

      private final Deadline.Ticker ticker
    • executorSupplier

      private final ServerCallExecutorSupplier executorSupplier
  • Constructor Details

    • ServerImpl

      ServerImpl(ServerImplBuilder builder, InternalServer transportServer, Context rootContext)
      Construct a server.
      Parameters:
      builder - builder with configuration for server
      transportServer - transport servers that will create new incoming transports
      rootContext - context that callbacks for new RPCs should be derived from
  • Method Details

    • start

      public ServerImpl start() throws IOException
      Bind and start the server.
      Specified by:
      start in class Server
      Returns:
      this object
      Throws:
      IllegalStateException - if already started
      IOException - if unable to bind
    • getPort

      public int getPort()
      Description copied from class: Server
      Returns the port number the server is listening on. This can return -1 if there is no actual port or the result otherwise does not make sense. Result is undefined after the server is terminated. If there are multiple possible ports, this will return one arbitrarily. Implementations are encouraged to return the same port on each call.
      Overrides:
      getPort in class Server
      See Also:
    • getListenSockets

      public List<SocketAddress> getListenSockets()
      Description copied from class: Server
      Returns a list of listening sockets for this server. May be different than the originally requested sockets (e.g. listening on port '0' may end up listening on a different port). The list is unmodifiable.
      Overrides:
      getListenSockets in class Server
    • getListenSocketsIgnoringLifecycle

      private List<SocketAddress> getListenSocketsIgnoringLifecycle()
    • getServices

      public List<ServerServiceDefinition> getServices()
      Description copied from class: Server
      Returns all services registered with the server, or an empty list if not supported by the implementation.
      Overrides:
      getServices in class Server
    • getImmutableServices

      public List<ServerServiceDefinition> getImmutableServices()
      Description copied from class: Server
      Returns immutable services registered with the server, or an empty list if not supported by the implementation.
      Overrides:
      getImmutableServices in class Server
    • getMutableServices

      public List<ServerServiceDefinition> getMutableServices()
      Description copied from class: Server
      Returns mutable services registered with the server, or an empty list if not supported by the implementation.
      Overrides:
      getMutableServices in class Server
    • shutdown

      public ServerImpl shutdown()
      Initiates an orderly shutdown in which preexisting calls continue but new calls are rejected.
      Specified by:
      shutdown in class Server
      Returns:
      this object
    • shutdownNow

      public ServerImpl shutdownNow()
      Description copied from class: Server
      Initiates a forceful shutdown in which preexisting and new calls are rejected. Although forceful, the shutdown process is still not instantaneous; Server.isTerminated() will likely return false immediately after this method returns. After this call returns, this server has released the listening socket(s) and may be reused by another server.

      Calling this method before start() will shut down and terminate the server like normal, but prevents starting the server in the future.

      Specified by:
      shutdownNow in class Server
      Returns:
      this object
    • isShutdown

      public boolean isShutdown()
      Description copied from class: Server
      Returns whether the server is shutdown. Shutdown servers reject any new calls, but may still have some calls being processed.
      Specified by:
      isShutdown in class Server
      See Also:
    • awaitTermination

      public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
      Description copied from class: Server
      Waits for the server to become terminated, giving up if the timeout is reached.

      Calling this method before start() or shutdown() is permitted and does not change its behavior.

      Specified by:
      awaitTermination in class Server
      Returns:
      whether the server is terminated, as would be done by Server.isTerminated().
      Throws:
      InterruptedException
    • awaitTermination

      public void awaitTermination() throws InterruptedException
      Description copied from class: Server
      Waits for the server to become terminated.

      Calling this method before start() or shutdown() is permitted and does not change its behavior.

      Specified by:
      awaitTermination in class Server
      Throws:
      InterruptedException
    • isTerminated

      public boolean isTerminated()
      Description copied from class: Server
      Returns whether the server is terminated. Terminated servers have no running calls and relevant resources released (like TCP connections).
      Specified by:
      isTerminated in class Server
      See Also:
    • transportClosed

      private void transportClosed(ServerTransport transport)
      Remove transport service from accounting collection and notify of complete shutdown if necessary.
      Parameters:
      transport - service to remove
    • checkForTermination

      private void checkForTermination()
      Notify of complete shutdown if necessary.
    • getLogId

      public InternalLogId getLogId()
      Description copied from interface: InternalWithLogId
      Returns an ID that is primarily used in debug logs. It usually contains the class name and a numeric ID that is unique among the instances.

      The subclasses of this interface usually want to include the log ID in their Object.toString() results.

      Specified by:
      getLogId in interface InternalWithLogId
    • getStats

      public com.google.common.util.concurrent.ListenableFuture<InternalChannelz.ServerStats> getStats()
      Description copied from interface: InternalInstrumented
      Returns the stats object.
      Specified by:
      getStats in interface InternalInstrumented<InternalChannelz.ServerStats>
    • toString

      public String toString()
      Overrides:
      toString in class Object