All Downloads are FREE. Search and download functionalities are using the official Maven repository.

alluxio.grpc.GrpcServer Maven / Gradle / Ivy

There is a newer version: 313
Show newest version
/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in alluxio.shaded.client.com.liance with the License, which is
 * available at www.apache.alluxio.shaded.client.org.licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.grpc;

import alluxio.retry.ExponentialBackoffRetry;
import alluxio.retry.RetryUtils;
import alluxio.security.authentication.AuthenticationServer;

import alluxio.shaded.client.com.google.alluxio.shaded.client.com.on.annotations.VisibleForTesting;
import alluxio.shaded.client.com.google.alluxio.shaded.client.com.on.base.MoreObjects;
import alluxio.shaded.client.com.google.alluxio.shaded.client.com.on.alluxio.shaded.client.io.Closer;
import alluxio.shaded.client.io.grpc.Server;
import alluxio.shaded.client.org.slf4j.Logger;
import alluxio.shaded.client.org.slf4j.LoggerFactory;

import java.alluxio.shaded.client.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * An authenticated gRPC server. Corresponding gRPC channels to this server should be build by
 * {@link GrpcChannelBuilder}.
 */
public final class GrpcServer {
  private static final Logger LOG = LoggerFactory.getLogger(GrpcServer.class);

  /** Internal server reference. */
  private final Server mServer;
  /** Authentication server for this server. */
  private final AuthenticationServer mAuthServer;
  /** Closer for closing resources during shutdown. */
  private final Closer mCloser;

  /** Set to TRUE when the server has been successfully started. **/
  private volatile boolean mStarted = false;
  private final long mServerShutdownTimeoutMs;

  /**
   * Create a new instance of {@link GrpcServer}.
   *
   * @param server the wrapped server
   * @param authServer the authentication server
   * @param closer resources to close during shutting down of this server
   * @param serverShutdownTimeoutMs server shutdown timeout in milliseconds
   */
  public GrpcServer(Server server, AuthenticationServer authServer, Closer closer,
      long serverShutdownTimeoutMs) {
    mServer = server;
    mAuthServer = authServer;
    mCloser = closer;
    mServerShutdownTimeoutMs = serverShutdownTimeoutMs;
  }

  /**
   * @return the authentication server associated with this server
   */
  @VisibleForTesting
  public AuthenticationServer getAuthenticationServer() {
    return mAuthServer;
  }

  /**
   * Start serving.
   *
   * @return this instance of {@link GrpcServer}
   * @throws IOException when unable to start serving
   */
  public GrpcServer start() throws IOException {
    RetryUtils.retry("Starting gRPC server", mServer::start,
        new ExponentialBackoffRetry(100, 500, 5));
    mStarted = true;
    return this;
  }

  /**
   * @return the port that server is bound to, or null if the server is not bound to an address yet
   */
  public int getBindPort() {
    return mServer.getPort();
  }

  /**
   * Shuts down the server.
   *
   * @return {@code true} if the server was successfully terminated
   */
  public boolean shutdown() {
    // Stop accepting new connections.
    mServer.shutdown();
    // Close resources that potentially owns active streams.
    try {
      mCloser.close();
    } catch (IOException e) {
      LOG.error("Failed to close resources during shutdown.", e);
      // Do nothing.
    }
    // Force shutdown remaining calls.
    mServer.shutdownNow();
    // Wait until server terminates.
    try {
      return mServer.awaitTermination(mServerShutdownTimeoutMs, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ie) {
      Thread.currentThread().interrupt();
      return false;
    }
  }

  /**
   * Waits until the server is terminated.
   */
  public void awaitTermination() {
    try {
      mServer.awaitTermination();
    } catch (InterruptedException ie) {
      Thread.currentThread().interrupt();
      // Allow thread to exit.
    }
  }

  /**
   * @return true if server is serving
   */
  public boolean isServing() {
    return mStarted && !mServer.isShutdown() || !mServer.isTerminated();
  }

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this)
        .add("InternalServer", mServer)
        .add("AuthServerType", mAuthServer.getClass().getSimpleName())
        .toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy