use of org.apache.storm.daemon.ui.InvalidRequestException in project storm by apache.
the class LogviewerLogSearchHandler method findNMatches.
/**
* Find the first N matches of target string in files.
*
* @param logs all candidate log files to search
* @param numMatches number of matches expected
* @param fileOffset number of log files to skip initially
* @param startByteOffset number of byte to be ignored in each log file
* @param targetStr searched string
* @return all matched results
*/
@VisibleForTesting
Matched findNMatches(List<Path> logs, int numMatches, int fileOffset, int startByteOffset, String targetStr) {
logs = drop(logs, fileOffset);
LOG.debug("{} files to scan", logs.size());
List<Map<String, Object>> matches = new ArrayList<>();
int matchCount = 0;
int scannedFiles = 0;
while (true) {
if (logs.isEmpty()) {
// fileOffset = one past last scanned file
break;
}
Path firstLog = logs.get(0);
Map<String, Object> matchInLog;
try {
LOG.debug("Looking through {}", firstLog);
matchInLog = substringSearch(firstLog, targetStr, numMatches - matchCount, startByteOffset);
scannedFiles++;
} catch (InvalidRequestException e) {
LOG.error("Can't search past end of file.", e);
matchInLog = new HashMap<>();
}
String fileName = WorkerLogs.getTopologyPortWorkerLog(firstLog);
// This section simply put the formatted log filename and corresponding port in the matching.
final List<Map<String, Object>> newMatches = new ArrayList<>(matches);
Map<String, Object> currentFileMatch = new HashMap<>(matchInLog);
currentFileMatch.put("fileName", fileName);
Path firstLogAbsPath = firstLog.toAbsolutePath().normalize();
currentFileMatch.put("port", truncatePathToLastElements(firstLogAbsPath, 2).getName(0).toString());
newMatches.add(currentFileMatch);
int newCount = matchCount + ((List<?>) matchInLog.getOrDefault("matches", Collections.emptyList())).size();
if (newCount == matchCount) {
// matches and matchCount is not changed
logs = rest(logs);
startByteOffset = 0;
fileOffset = fileOffset + 1;
} else if (newCount >= numMatches) {
matches = newMatches;
// fileOffset = the index of last scanned file
break;
} else {
matches = newMatches;
logs = rest(logs);
startByteOffset = 0;
fileOffset = fileOffset + 1;
matchCount = newCount;
}
}
LOG.debug("scanned {} files", scannedFiles);
return new Matched(fileOffset, targetStr, matches, scannedFiles);
}
use of org.apache.storm.daemon.ui.InvalidRequestException in project storm by apache.
the class LogviewerResource method search.
/**
* Handles '/search' (searching from specific worker or daemon log file) request.
*/
@GET
@Path("/search")
public Response search(@Context HttpServletRequest request) throws IOException {
numSearchLogRequests.mark();
String user = httpCredsHandler.getUserName(request);
boolean isDaemon = StringUtils.equals(request.getParameter("is-daemon"), "yes");
String file = request.getParameter("file");
String decodedFileName = Utils.urlDecodeUtf8(file);
String searchString = request.getParameter("search-string");
String numMatchesStr = request.getParameter("num-matches");
String startByteOffset = request.getParameter("start-byte-offset");
String callback = request.getParameter(StormApiResource.callbackParameterName);
String origin = request.getHeader("Origin");
try (Timer.Context t = searchLogRequestDuration.time()) {
return logSearchHandler.searchLogFile(decodedFileName, user, isDaemon, searchString, numMatchesStr, startByteOffset, callback, origin);
} catch (InvalidRequestException e) {
LOG.error(e.getMessage(), e);
int statusCode = 400;
return new JsonResponseBuilder().setData(UIHelpers.exceptionToJson(e, statusCode)).setCallback(callback).setStatus(statusCode).build();
} catch (IOException e) {
numSearchExceptions.mark();
throw e;
}
}
Aggregations