Search in sources :

Example 1 with WorkerWebUILogs

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

the class AlluxioWorkerRestServiceHandler 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, @QueryParam("end") String requestEnd, @DefaultValue("20") @QueryParam("limit") String requestLimit) {
    return RestUtils.call(() -> {
        FilenameFilter filenameFilter = (dir, name) -> name.toLowerCase().endsWith(".log");
        WorkerWebUILogs response = new WorkerWebUILogs();
        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 WorkerStorageTierAssoc().getOrderedStorageAliases()));
                }
            }
            Collections.sort(fileInfos, 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();
                String offsetParam = requestOffset;
                long relativeOffset = 0;
                long offset;
                try {
                    if (offsetParam != null) {
                        relativeOffset = Long.parseLong(offsetParam);
                    }
                } catch (NumberFormatException e) {
                    relativeOffset = 0;
                }
                String endParam = requestEnd;
                // relative to the end of the file.
                if (endParam == null) {
                    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) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) WorkerWebUIMetrics(alluxio.wire.WorkerWebUIMetrics) PropertyKey(alluxio.conf.PropertyKey) LogUtils(alluxio.util.LogUtils) UIFileInfo(alluxio.util.webui.UIFileInfo) ApiOperation(io.swagger.annotations.ApiOperation) MediaType(javax.ws.rs.core.MediaType) FileSystem(alluxio.client.file.FileSystem) QueryParam(javax.ws.rs.QueryParam) WorkerWebUIBlockInfo(alluxio.wire.WorkerWebUIBlockInfo) MetricKey(alluxio.metrics.MetricKey) Map(java.util.Map) Counter(com.codahale.metrics.Counter) DefaultValue(javax.ws.rs.DefaultValue) WebUtils(alluxio.util.webui.WebUtils) Triple(org.apache.commons.lang3.tuple.Triple) WorkerWebUILogs(alluxio.wire.WorkerWebUILogs) Context(javax.ws.rs.core.Context) RestUtils(alluxio.RestUtils) ServerConfiguration(alluxio.conf.ServerConfiguration) WorkerWebUIConfiguration(alluxio.wire.WorkerWebUIConfiguration) Metric(com.codahale.metrics.Metric) Set(java.util.Set) AlluxioException(alluxio.exception.AlluxioException) ConfigProperty(alluxio.grpc.ConfigProperty) Sets(com.google.common.collect.Sets) List(java.util.List) Capacity(alluxio.wire.Capacity) Response(javax.ws.rs.core.Response) UIFileBlockInfo(alluxio.util.webui.UIFileBlockInfo) FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) UIStorageDir(alluxio.util.webui.UIStorageDir) WorkerWebServer(alluxio.web.WorkerWebServer) BlockStoreMeta(alluxio.worker.block.BlockStoreMeta) Gauge(com.codahale.metrics.Gauge) RuntimeConstants(alluxio.RuntimeConstants) SortedMap(java.util.SortedMap) BlockWorker(alluxio.worker.block.BlockWorker) FilenameFilter(java.io.FilenameFilter) GET(javax.ws.rs.GET) GetConfigurationPOptions(alluxio.grpc.GetConfigurationPOptions) WorkerWebUIInit(alluxio.wire.WorkerWebUIInit) BlockId(alluxio.master.block.BlockId) NetworkAddressUtils(alluxio.util.network.NetworkAddressUtils) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Constants(alluxio.Constants) AlluxioURI(alluxio.AlluxioURI) FormatUtils(alluxio.util.FormatUtils) MetricsSystem(alluxio.metrics.MetricsSystem) Api(io.swagger.annotations.Api) ConfigurationValueOptions(alluxio.conf.ConfigurationValueOptions) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) UIUsageOnTier(alluxio.util.webui.UIUsageOnTier) AlluxioWorkerInfo(alluxio.wire.AlluxioWorkerInfo) IOException(java.io.IOException) BlockMeta(alluxio.worker.block.meta.BlockMeta) FileInputStream(java.io.FileInputStream) Pair(alluxio.collections.Pair) ConfigurationUtils(alluxio.util.ConfigurationUtils) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) File(java.io.File) URIStatus(alluxio.client.file.URIStatus) TreeMap(java.util.TreeMap) UIWorkerInfo(alluxio.util.webui.UIWorkerInfo) ServletContext(javax.servlet.ServletContext) WorkerStorageTierAssoc(alluxio.WorkerStorageTierAssoc) WorkerWebUIOverview(alluxio.wire.WorkerWebUIOverview) Comparator(java.util.Comparator) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException) Collections(java.util.Collections) InputStream(java.io.InputStream) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) WorkerStorageTierAssoc(alluxio.WorkerStorageTierAssoc) WorkerWebUILogs(alluxio.wire.WorkerWebUILogs) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FilenameFilter(java.io.FilenameFilter) 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 RestUtils (alluxio.RestUtils)1 RuntimeConstants (alluxio.RuntimeConstants)1 WorkerStorageTierAssoc (alluxio.WorkerStorageTierAssoc)1 FileSystem (alluxio.client.file.FileSystem)1 URIStatus (alluxio.client.file.URIStatus)1 Pair (alluxio.collections.Pair)1 ConfigurationValueOptions (alluxio.conf.ConfigurationValueOptions)1 PropertyKey (alluxio.conf.PropertyKey)1 ServerConfiguration (alluxio.conf.ServerConfiguration)1 AlluxioException (alluxio.exception.AlluxioException)1 BlockDoesNotExistException (alluxio.exception.BlockDoesNotExistException)1 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)1 ConfigProperty (alluxio.grpc.ConfigProperty)1 GetConfigurationPOptions (alluxio.grpc.GetConfigurationPOptions)1 BlockId (alluxio.master.block.BlockId)1 MetricKey (alluxio.metrics.MetricKey)1 MetricsSystem (alluxio.metrics.MetricsSystem)1 ConfigurationUtils (alluxio.util.ConfigurationUtils)1