Search in sources :

Example 1 with MasterWebUILogs

use of alluxio.wire.MasterWebUILogs in project alluxio by Alluxio.

the class AlluxioMasterRestServiceHandler method getWebUILogs.

/**
 * Gets Web UI logs page data.
 *
 * @param requestPath the request path
 * @param requestOffset the request offset
 * @param requestEnd the request end
 * @param requestLimit the request limit
 * @return the response object
 */
@GET
@Path(WEBUI_LOGS)
public Response getWebUILogs(@DefaultValue("") @QueryParam("path") String requestPath, @DefaultValue("0") @QueryParam("offset") String requestOffset, @DefaultValue("") @QueryParam("end") String requestEnd, @DefaultValue("20") @QueryParam("limit") String requestLimit) {
    return RestUtils.call(() -> {
        FilenameFilter filenameFilter = (dir, name) -> name.toLowerCase().endsWith(".log");
        MasterWebUILogs response = new MasterWebUILogs();
        if (!ServerConfiguration.getBoolean(PropertyKey.WEB_FILE_INFO_ENABLED)) {
            return response;
        }
        response.setDebug(ServerConfiguration.getBoolean(PropertyKey.DEBUG)).setInvalidPathError("").setViewingOffset(0).setCurrentPath("");
        // response.setDownloadLogFile(1);
        // response.setBaseUrl("./browseLogs");
        // response.setShowPermissions(false);
        String logsPath = ServerConfiguration.getString(PropertyKey.LOGS_DIR);
        File logsDir = new File(logsPath);
        String requestFile = requestPath;
        if (requestFile == null || requestFile.isEmpty()) {
            // List all log files in the log/ directory.
            List<UIFileInfo> fileInfos = new ArrayList<>();
            File[] logFiles = logsDir.listFiles(filenameFilter);
            if (logFiles != null) {
                for (File logFile : logFiles) {
                    String logFileName = logFile.getName();
                    fileInfos.add(new UIFileInfo(new UIFileInfo.LocalFileInfo(logFileName, logFileName, logFile.length(), UIFileInfo.LocalFileInfo.EMPTY_CREATION_TIME, logFile.lastModified(), logFile.isDirectory()), ServerConfiguration.global(), new MasterStorageTierAssoc().getOrderedStorageAliases()));
                }
            }
            fileInfos.sort(UIFileInfo.PATH_STRING_COMPARE);
            response.setNTotalFile(fileInfos.size());
            try {
                int offset = Integer.parseInt(requestOffset);
                int limit = Integer.parseInt(requestLimit);
                limit = offset == 0 && limit > fileInfos.size() ? fileInfos.size() : limit;
                limit = offset + limit > fileInfos.size() ? fileInfos.size() - offset : limit;
                int sum = Math.addExact(offset, limit);
                fileInfos = fileInfos.subList(offset, sum);
                response.setFileInfos(fileInfos);
            } catch (NumberFormatException e) {
                response.setFatalError("Error: offset or limit parse error, " + e.getLocalizedMessage());
                return response;
            } catch (ArithmeticException e) {
                response.setFatalError("Error: offset or offset + limit is out of bound, " + e.getLocalizedMessage());
                return response;
            } catch (IllegalArgumentException e) {
                response.setFatalError(e.getLocalizedMessage());
                return response;
            }
        } else {
            // Request a specific log file.
            // Only allow filenames as the path, to avoid arbitrary local path lookups.
            requestFile = new File(requestFile).getName();
            response.setCurrentPath(requestFile);
            File logFile = new File(logsDir, requestFile);
            try {
                long fileSize = logFile.length();
                long relativeOffset = 0;
                long offset;
                try {
                    if (requestOffset != null) {
                        relativeOffset = Long.parseLong(requestOffset);
                    }
                } catch (NumberFormatException e) {
                // ignore the exception
                }
                // relative to the end of the file.
                if (requestEnd.equals("")) {
                    offset = relativeOffset;
                } else {
                    offset = fileSize - relativeOffset;
                }
                if (offset < 0) {
                    offset = 0;
                } else if (offset > fileSize) {
                    offset = fileSize;
                }
                String fileData;
                try (InputStream is = new FileInputStream(logFile)) {
                    fileSize = logFile.length();
                    int len = (int) Math.min(5L * Constants.KB, fileSize - offset);
                    byte[] data = new byte[len];
                    long skipped = is.skip(offset);
                    if (skipped < 0) {
                        // Nothing was skipped.
                        fileData = "Unable to traverse to offset; is file empty?";
                    } else if (skipped < offset) {
                        // Couldn't skip all the way to offset.
                        fileData = "Unable to traverse to offset; is offset larger than the file?";
                    } else {
                        // Read may not read up to len, so only convert what was read.
                        int read = is.read(data, 0, len);
                        if (read < 0) {
                            // Stream couldn't read anything, skip went to EOF?
                            fileData = "Unable to read file";
                        } else {
                            fileData = WebUtils.convertByteArrayToStringWithoutEscape(data, 0, read);
                        }
                    }
                }
                response.setFileData(fileData).setViewingOffset(offset);
            } catch (IOException e) {
                response.setInvalidPathError("Error: File " + logFile + " is not available " + e.getMessage());
            }
        }
        return response;
    }, ServerConfiguration.global());
}
Also used : Produces(javax.ws.rs.Produces) MasterWebServer(alluxio.web.MasterWebServer) ZonedDateTime(java.time.ZonedDateTime) DefaultFileSystemMaster(alluxio.master.file.DefaultFileSystemMaster) PropertyKey(alluxio.conf.PropertyKey) MediaType(javax.ws.rs.core.MediaType) FileSystem(alluxio.client.file.FileSystem) WorkerInfo(alluxio.wire.WorkerInfo) Map(java.util.Map) AlluxioMasterProcess(alluxio.master.AlluxioMasterProcess) WebUtils(alluxio.util.webui.WebUtils) ZoneOffset(java.time.ZoneOffset) Triple(org.apache.commons.lang3.tuple.Triple) ReadPType(alluxio.grpc.ReadPType) RestUtils(alluxio.RestUtils) Metric(com.codahale.metrics.Metric) Set(java.util.Set) AlluxioException(alluxio.exception.AlluxioException) ConfigProperty(alluxio.grpc.ConfigProperty) UIFileBlockInfo(alluxio.util.webui.UIFileBlockInfo) RuntimeConstants(alluxio.RuntimeConstants) FilenameFilter(java.io.FilenameFilter) ServerUserState(alluxio.security.user.ServerUserState) GET(javax.ws.rs.GET) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) MasterWebUIInit(alluxio.wire.MasterWebUIInit) AlluxioURI(alluxio.AlluxioURI) FileInStream(alluxio.client.file.FileInStream) FormatUtils(alluxio.util.FormatUtils) MetricsSystem(alluxio.metrics.MetricsSystem) Api(io.swagger.annotations.Api) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) MetricRegistry(com.codahale.metrics.MetricRegistry) MasterWebUIBrowse(alluxio.wire.MasterWebUIBrowse) MasterWebUIWorkers(alluxio.wire.MasterWebUIWorkers) IOException(java.io.IOException) OpenFilePOptions(alluxio.grpc.OpenFilePOptions) UFS_OP_PREFIX(alluxio.metrics.MetricInfo.UFS_OP_PREFIX) File(java.io.File) StorageTierInfo(alluxio.util.webui.StorageTierInfo) URIStatus(alluxio.client.file.URIStatus) TreeMap(java.util.TreeMap) ServletContext(javax.servlet.ServletContext) MasterWebUIMountTable(alluxio.wire.MasterWebUIMountTable) MountTable(alluxio.master.file.meta.MountTable) CommonUtils(alluxio.util.CommonUtils) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) URLDecoder(java.net.URLDecoder) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) FileBlockInfo(alluxio.wire.FileBlockInfo) FileSystemMaster(alluxio.master.file.FileSystemMaster) LogUtils(alluxio.util.LogUtils) UIFileInfo(alluxio.util.webui.UIFileInfo) ApiOperation(io.swagger.annotations.ApiOperation) InvalidPathException(alluxio.exception.InvalidPathException) QueryParam(javax.ws.rs.QueryParam) MasterStorageTierAssoc(alluxio.MasterStorageTierAssoc) ListStatusContext(alluxio.master.file.contexts.ListStatusContext) MetricKey(alluxio.metrics.MetricKey) Counter(com.codahale.metrics.Counter) DefaultValue(javax.ws.rs.DefaultValue) AlluxioMasterInfo(alluxio.wire.AlluxioMasterInfo) MasterWebUIConfiguration(alluxio.wire.MasterWebUIConfiguration) MasterWebUIMetrics(alluxio.wire.MasterWebUIMetrics) Context(javax.ws.rs.core.Context) ServerConfiguration(alluxio.conf.ServerConfiguration) StorageTierAssoc(alluxio.StorageTierAssoc) Instant(java.time.Instant) Sets(com.google.common.collect.Sets) AccessControlException(alluxio.exception.AccessControlException) List(java.util.List) Capacity(alluxio.wire.Capacity) Response(javax.ws.rs.core.Response) SecurityUtils(alluxio.util.SecurityUtils) MasterWebUIOverview(alluxio.wire.MasterWebUIOverview) FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) Gauge(com.codahale.metrics.Gauge) UFS_OP_SAVED_PREFIX(alluxio.metrics.MetricInfo.UFS_OP_SAVED_PREFIX) SortedMap(java.util.SortedMap) UnavailableException(alluxio.exception.status.UnavailableException) MountPointInfo(alluxio.wire.MountPointInfo) NodeInfo(alluxio.util.webui.NodeInfo) GetConfigurationPOptions(alluxio.grpc.GetConfigurationPOptions) WorkerNetAddress(alluxio.wire.WorkerNetAddress) HashMap(java.util.HashMap) NetworkAddressUtils(alluxio.util.network.NetworkAddressUtils) BlockMaster(alluxio.master.block.BlockMaster) PathUtils(alluxio.util.io.PathUtils) Constants(alluxio.Constants) ConfigurationValueOptions(alluxio.conf.ConfigurationValueOptions) AuthenticatedClientUser(alluxio.security.authentication.AuthenticatedClientUser) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) RaftProtos(org.apache.ratis.proto.RaftProtos) MasterWebUIData(alluxio.wire.MasterWebUIData) FileInputStream(java.io.FileInputStream) ConfigurationUtils(alluxio.util.ConfigurationUtils) BlockLocation(alluxio.wire.BlockLocation) MasterWebUILogs(alluxio.wire.MasterWebUILogs) FileInfo(alluxio.wire.FileInfo) DateTimeFormatter(java.time.format.DateTimeFormatter) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) Collections(java.util.Collections) ConfigCheckReport(alluxio.wire.ConfigCheckReport) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FilenameFilter(java.io.FilenameFilter) MasterStorageTierAssoc(alluxio.MasterStorageTierAssoc) MasterWebUILogs(alluxio.wire.MasterWebUILogs) UIFileInfo(alluxio.util.webui.UIFileInfo) File(java.io.File) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

AlluxioURI (alluxio.AlluxioURI)1 Constants (alluxio.Constants)1 MasterStorageTierAssoc (alluxio.MasterStorageTierAssoc)1 RestUtils (alluxio.RestUtils)1 RuntimeConstants (alluxio.RuntimeConstants)1 StorageTierAssoc (alluxio.StorageTierAssoc)1 FileInStream (alluxio.client.file.FileInStream)1 FileSystem (alluxio.client.file.FileSystem)1 URIStatus (alluxio.client.file.URIStatus)1 ConfigurationValueOptions (alluxio.conf.ConfigurationValueOptions)1 PropertyKey (alluxio.conf.PropertyKey)1 ServerConfiguration (alluxio.conf.ServerConfiguration)1 AccessControlException (alluxio.exception.AccessControlException)1 AlluxioException (alluxio.exception.AlluxioException)1 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)1 InvalidPathException (alluxio.exception.InvalidPathException)1 UnavailableException (alluxio.exception.status.UnavailableException)1 ConfigProperty (alluxio.grpc.ConfigProperty)1 GetConfigurationPOptions (alluxio.grpc.GetConfigurationPOptions)1 OpenFilePOptions (alluxio.grpc.OpenFilePOptions)1