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());
}
Aggregations