hadoop Nfs3HttpServer 源码

  • 2022-10-20
  • 浏览 (184)

haddop Nfs3HttpServer 代码

文件路径:/hadoop-hdfs-project/hadoop-hdfs-nfs/src/main/java/org/apache/hadoop/hdfs/nfs/nfs3/Nfs3HttpServer.java

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.hadoop.hdfs.nfs.nfs3;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.DFSUtil;
import org.apache.hadoop.hdfs.nfs.conf.NfsConfigKeys;
import org.apache.hadoop.hdfs.nfs.conf.NfsConfiguration;
import org.apache.hadoop.http.HttpConfig;
import org.apache.hadoop.http.HttpServer2;
import org.apache.hadoop.net.NetUtils;

/**
 * Encapsulates the HTTP server started by the NFS3 gateway.
 */
class Nfs3HttpServer {
  private int infoPort;
  private int infoSecurePort;

  private HttpServer2 httpServer;

  private final NfsConfiguration conf;

  Nfs3HttpServer(NfsConfiguration conf) {
    this.conf = conf;
  }

  void start() throws IOException {
    final InetSocketAddress httpAddr = getHttpAddress(conf);

    final String httpsAddrString = conf.get(
        NfsConfigKeys.NFS_HTTPS_ADDRESS_KEY,
        NfsConfigKeys.NFS_HTTPS_ADDRESS_DEFAULT);
    InetSocketAddress httpsAddr = NetUtils.createSocketAddr(httpsAddrString);

    HttpServer2.Builder builder = DFSUtil.httpServerTemplateForNNAndJN(conf,
        httpAddr, httpsAddr, "nfs3",
        NfsConfigKeys.DFS_NFS_KERBEROS_PRINCIPAL_KEY,
        NfsConfigKeys.DFS_NFS_KEYTAB_FILE_KEY);

    this.httpServer = builder.build();
    this.httpServer.start();
    
    HttpConfig.Policy policy = DFSUtil.getHttpPolicy(conf);
    int connIdx = 0;
    if (policy.isHttpEnabled()) {
      infoPort = httpServer.getConnectorAddress(connIdx++).getPort();
    }

    if (policy.isHttpsEnabled()) {
      infoSecurePort = httpServer.getConnectorAddress(connIdx).getPort();
    }
  }

  void stop() throws IOException {
    if (httpServer != null) {
      try {
        httpServer.stop();
      } catch (Exception e) {
        throw new IOException(e);
      }
    }
  }

  public int getPort() {
    return this.infoPort;
  }

  public int getSecurePort() {
    return this.infoSecurePort;
  }

  /**
   * Return the URI that locates the HTTP server.
   */
  public URI getServerURI() {
    // getHttpClientScheme() only returns https for HTTPS_ONLY policy. This
    // matches the behavior that the first connector is a HTTPS connector only
    // for HTTPS_ONLY policy.
    InetSocketAddress addr = httpServer.getConnectorAddress(0);
    return URI.create(DFSUtil.getHttpClientScheme(conf) + "://"
        + NetUtils.getHostPortString(addr));
  }

  public InetSocketAddress getHttpAddress(Configuration conf) {
    String addr = conf.get(NfsConfigKeys.NFS_HTTP_ADDRESS_KEY,
        NfsConfigKeys.NFS_HTTP_ADDRESS_DEFAULT);
    return NetUtils.createSocketAddr(addr, NfsConfigKeys.NFS_HTTP_PORT_DEFAULT,
        NfsConfigKeys.NFS_HTTP_ADDRESS_KEY);
  }
}

相关信息

hadoop 源码目录

相关文章

hadoop AsyncDataService 源码

hadoop DFSClientCache 源码

hadoop Nfs3 源码

hadoop Nfs3Metrics 源码

hadoop Nfs3Utils 源码

hadoop OffsetRange 源码

hadoop OpenFileCtx 源码

hadoop OpenFileCtxCache 源码

hadoop PrivilegedNfsGatewayStarter 源码

hadoop RpcProgramNfs3 源码

0  赞