Package org.apache.hadoop.dfs

Source Code of org.apache.hadoop.dfs.FileDataServlet

/**
* 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.dfs;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.hadoop.security.UnixUserGroupInformation;

/** Redirect queries about the hosted filesystem to an appropriate datanode.
* @see org.apache.hadoop.dfs.HftpFileSystem
*/
public class FileDataServlet extends DfsServlet {
  private static URI createUri(DFSFileInfo i, UnixUserGroupInformation ugi,
      ClientProtocol nnproxy) throws IOException, URISyntaxException {
    final DatanodeID host = pickSrcDatanode(i, nnproxy);
    final String hostname;
    if (host instanceof DatanodeInfo) {
      hostname = ((DatanodeInfo)host).getHostName();
    } else {
      hostname = host.getHost();
    }
    return new URI("http", null, hostname, host.getInfoPort(),
          "/streamFile", "filename=" + i.getPath() + "&ugi=" + ugi, null);
  }

  private final static JspHelper jspHelper = new JspHelper();

  /** Select a datanode to service this request.
   * Currently, this looks at no more than the first five blocks of a file,
   * selecting a datanode randomly from the most represented.
   */
  private static DatanodeID pickSrcDatanode(DFSFileInfo i,
      ClientProtocol nnproxy) throws IOException {
    final LocatedBlocks blks = nnproxy.getBlockLocations(
        i.getPath().toUri().getPath(), 0, 1);
    if (i.getLen() == 0 || blks.getLocatedBlocks().size() <= 0) {
      // pick a random datanode
      return jspHelper.randomNode();
    }
    return jspHelper.bestNode(blks.get(0));
  }

  /**
   * Service a GET request as described below.
   * Request:
   * {@code
   * GET http://<nn>:<port>/data[/<path>] HTTP/1.1
   * }
   */
  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException {
    final UnixUserGroupInformation ugi = getUGI(request);
    final ClientProtocol nnproxy = createNameNodeProxy(ugi);

    try {
      final String path = request.getPathInfo() != null
        ? request.getPathInfo() : "/";
      DFSFileInfo info = nnproxy.getFileInfo(path);
      if (!info.isDir()) {
        response.sendRedirect(createUri(info, ugi, nnproxy).toURL().toString());
      } else {
        response.sendError(400, "cat: " + path + ": is a directory");
      }
    } catch (URISyntaxException e) {
      response.getWriter().println(e.toString());
    } catch (IOException e) {
      response.sendError(400, e.getMessage());
    }
  }

}
TOP

Related Classes of org.apache.hadoop.dfs.FileDataServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.