Search in sources :

Example 6 with InvalidRequestException

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);
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InvalidRequestException(org.apache.storm.daemon.ui.InvalidRequestException) Map(java.util.Map) HashMap(java.util.HashMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with InvalidRequestException

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;
    }
}
Also used : JsonResponseBuilder(org.apache.storm.daemon.common.JsonResponseBuilder) Timer(com.codahale.metrics.Timer) InvalidRequestException(org.apache.storm.daemon.ui.InvalidRequestException) IOException(java.io.IOException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

InvalidRequestException (org.apache.storm.daemon.ui.InvalidRequestException)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)5 InputStream (java.io.InputStream)4 Path (java.nio.file.Path)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 UncheckedIOException (java.io.UncheckedIOException)3 Response (javax.ws.rs.core.Response)3 Meter (com.codahale.metrics.Meter)2 TagCreator.a (j2html.TagCreator.a)2 TagCreator.body (j2html.TagCreator.body)2 TagCreator.div (j2html.TagCreator.div)2 TagCreator.form (j2html.TagCreator.form)2 TagCreator.h3 (j2html.TagCreator.h3)2 TagCreator.head (j2html.TagCreator.head)2