@GET
 @Path("data/explore/queries/{id}/status")
 public void getQueryStatus(
     HttpRequest request, HttpResponder responder, @PathParam("id") String id) {
   try {
     QueryHandle handle = QueryHandle.fromId(id);
     QueryStatus status;
     if (!handle.equals(QueryHandle.NO_OP)) {
       status = exploreService.getStatus(handle);
     } else {
       status = QueryStatus.NO_OP;
     }
     responder.sendJson(HttpResponseStatus.OK, status);
   } catch (IllegalArgumentException e) {
     LOG.debug("Got exception:", e);
     responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
   } catch (SQLException e) {
     LOG.debug("Got exception:", e);
     responder.sendString(
         HttpResponseStatus.BAD_REQUEST,
         String.format("[SQLState %s] %s", e.getSQLState(), e.getMessage()));
   } catch (HandleNotFoundException e) {
     responder.sendStatus(HttpResponseStatus.NOT_FOUND);
   } catch (Throwable e) {
     LOG.error("Got exception:", e);
     responder.sendStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
   }
 }
Beispiel #2
0
  @POST
  @Path("/search")
  public void search(
      HttpRequest request,
      HttpResponder responder,
      @QueryParam("target") String target,
      @QueryParam("tag") List<String> tags)
      throws IOException {
    if (target == null) {
      responder.sendJson(HttpResponseStatus.BAD_REQUEST, "Required target param is missing");
      return;
    }

    switch (target) {
      case "tag":
        searchTagAndRespond(responder, tags);
        break;
      case "metric":
        searchMetricAndRespond(responder, tags);
        break;
      default:
        responder.sendJson(HttpResponseStatus.BAD_REQUEST, "Unknown target param value: " + target);
        break;
    }
  }
Beispiel #3
0
  private void doPrev(
      HttpResponder responder,
      LoggingContext loggingContext,
      int maxEvents,
      String fromOffsetStr,
      boolean escape,
      String filterStr,
      @Nullable RunRecord runRecord) {
    try {
      Filter filter = FilterParser.parse(filterStr);

      LogReaderCallback logCallback = new LogReaderCallback(responder, logPattern, escape);
      LogOffset logOffset = FormattedLogEvent.parseLogOffset(fromOffsetStr);
      ReadRange readRange = ReadRange.createToRange(logOffset);
      readRange = adjustReadRange(readRange, runRecord);
      logReader.getLogPrev(loggingContext, readRange, maxEvents, filter, logCallback);
      logCallback.close();
    } catch (SecurityException e) {
      responder.sendStatus(HttpResponseStatus.UNAUTHORIZED);
    } catch (IllegalArgumentException e) {
      responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (Throwable e) {
      LOG.error("Caught exception", e);
      responder.sendStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
    }
  }
 @POST
 @Path("data/explore/queries/{id}/next")
 public void getQueryNextResults(
     HttpRequest request, HttpResponder responder, @PathParam("id") String id) {
   // NOTE: this call is a POST because it is not idempotent: cursor of results is moved
   try {
     QueryHandle handle = QueryHandle.fromId(id);
     List<QueryResult> results;
     if (handle.equals(QueryHandle.NO_OP)) {
       results = Lists.newArrayList();
     } else {
       Map<String, String> args = decodeArguments(request);
       int size = args.containsKey("size") ? Integer.valueOf(args.get("size")) : 100;
       results = exploreService.nextResults(handle, size);
     }
     responder.sendJson(HttpResponseStatus.OK, results);
   } catch (IllegalArgumentException e) {
     LOG.debug("Got exception:", e);
     responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
   } catch (SQLException e) {
     LOG.debug("Got exception:", e);
     responder.sendString(
         HttpResponseStatus.BAD_REQUEST,
         String.format("[SQLState %s] %s", e.getSQLState(), e.getMessage()));
   } catch (HandleNotFoundException e) {
     responder.sendStatus(HttpResponseStatus.NOT_FOUND);
   } catch (Throwable e) {
     LOG.error("Got exception:", e);
     responder.sendStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
   }
 }
Beispiel #5
0
  private void executeBatchQueries(HttpRequest request, HttpResponder responder) {
    if (HttpHeaders.getContentLength(request) > 0) {
      try {
        String json = request.getContent().toString(Charsets.UTF_8);
        Map<String, QueryRequestFormat> queries =
            GSON.fromJson(json, new TypeToken<Map<String, QueryRequestFormat>>() {}.getType());

        LOG.trace("Received Queries {}", queries);

        Map<String, MetricQueryResult> queryFinalResponse = Maps.newHashMap();
        for (Map.Entry<String, QueryRequestFormat> query : queries.entrySet()) {
          MetricQueryRequest queryRequest = getQueryRequestFromFormat(query.getValue());
          queryFinalResponse.put(query.getKey(), executeQuery(queryRequest));
        }
        responder.sendJson(HttpResponseStatus.OK, queryFinalResponse);
      } catch (IllegalArgumentException e) {
        LOG.warn("Invalid request", e);
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
      } catch (Exception e) {
        LOG.error("Exception querying metrics ", e);
        responder.sendString(
            HttpResponseStatus.INTERNAL_SERVER_ERROR, "Internal error while querying for metrics");
      }
    } else {
      responder.sendJson(HttpResponseStatus.BAD_REQUEST, "Batch request with empty content");
    }
  }
Beispiel #6
0
  @GET
  @Path("/system/{component-id}/{service-id}/logs/prev")
  public void sysPrev(
      HttpRequest request,
      HttpResponder responder,
      @PathParam("component-id") String componentId,
      @PathParam("service-id") String serviceId,
      @QueryParam("max") @DefaultValue("50") int maxEvents,
      @QueryParam("fromOffset") @DefaultValue("") String fromOffsetStr,
      @QueryParam("escape") @DefaultValue("true") boolean escape,
      @QueryParam("filter") @DefaultValue("") String filterStr) {
    try {
      Filter filter = FilterParser.parse(filterStr);

      LoggingContext loggingContext =
          LoggingContextHelper.getLoggingContext(
              Constants.SYSTEM_NAMESPACE, componentId, serviceId);
      LogReaderCallback logCallback = new LogReaderCallback(responder, logPattern, escape);
      LogOffset logOffset = FormattedLogEvent.parseLogOffset(fromOffsetStr);
      ReadRange readRange = ReadRange.createToRange(logOffset);
      logReader.getLogPrev(loggingContext, readRange, maxEvents, filter, logCallback);
      logCallback.close();
    } catch (IllegalArgumentException e) {
      responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (Throwable e) {
      LOG.error("Caught exception", e);
      responder.sendStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
    }
  }
Beispiel #7
0
  @GET
  @Path("/system/{component-id}/{service-id}/logs")
  public void sysList(
      HttpRequest request,
      HttpResponder responder,
      @PathParam("component-id") String componentId,
      @PathParam("service-id") String serviceId,
      @QueryParam("start") @DefaultValue("-1") long fromTimeSecsParam,
      @QueryParam("stop") @DefaultValue("-1") long toTimeSecsParam,
      @QueryParam("escape") @DefaultValue("true") boolean escape,
      @QueryParam("filter") @DefaultValue("") String filterStr) {
    try {
      TimeRange timeRange = parseTime(fromTimeSecsParam, toTimeSecsParam, responder);
      if (timeRange == null) {
        return;
      }

      Filter filter = FilterParser.parse(filterStr);
      LoggingContext loggingContext =
          LoggingContextHelper.getLoggingContext(
              Constants.SYSTEM_NAMESPACE, componentId, serviceId);
      ChunkedLogReaderCallback logCallback =
          new ChunkedLogReaderCallback(responder, logPattern, escape);
      logReader.getLog(
          loggingContext, timeRange.getFromMillis(), timeRange.getToMillis(), filter, logCallback);
      logCallback.close();
    } catch (IllegalArgumentException e) {
      responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (Throwable e) {
      LOG.error("Caught exception", e);
      responder.sendStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
    }
  }
Beispiel #8
0
  private void doGetLogs(
      HttpResponder responder,
      LoggingContext loggingContext,
      long fromTimeSecsParam,
      long toTimeSecsParam,
      boolean escape,
      String filterStr,
      @Nullable RunRecord runRecord) {
    try {
      TimeRange timeRange = parseTime(fromTimeSecsParam, toTimeSecsParam, responder);
      if (timeRange == null) {
        return;
      }

      Filter filter = FilterParser.parse(filterStr);

      ReadRange readRange =
          new ReadRange(
              timeRange.getFromMillis(), timeRange.getToMillis(), LogOffset.INVALID_KAFKA_OFFSET);
      readRange = adjustReadRange(readRange, runRecord);

      ChunkedLogReaderCallback logCallback =
          new ChunkedLogReaderCallback(responder, logPattern, escape);
      logReader.getLog(
          loggingContext, readRange.getFromMillis(), readRange.getToMillis(), filter, logCallback);
      logCallback.close();
    } catch (SecurityException e) {
      responder.sendStatus(HttpResponseStatus.UNAUTHORIZED);
    } catch (IllegalArgumentException e) {
      responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (Throwable e) {
      LOG.error("Caught exception", e);
      responder.sendStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
    }
  }
Beispiel #9
0
 @Path("/queue/poll")
 @GET
 public void pollData(HttpRequest request, HttpResponder responder) {
   if (queue.size() == 0) {
     responder.sendStatus(HttpResponseStatus.NO_CONTENT);
     return;
   }
   String dataPacket = queue.poll();
   responder.sendString(HttpResponseStatus.OK, dataPacket);
 }
Beispiel #10
0
 private void searchMetricAndRespond(HttpResponder responder, List<String> tagValues) {
   try {
     responder.sendJson(
         HttpResponseStatus.OK, getMetrics(humanToTagNames(parseTagValues(tagValues))));
   } catch (IllegalArgumentException e) {
     LOG.warn("Invalid request", e);
     responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
   } catch (Exception e) {
     LOG.warn("Exception while retrieving available metrics", e);
     responder.sendStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
   }
 }
Beispiel #11
0
 @Path("/queue")
 @POST
 public void getPing(HttpRequest request, HttpResponder responder) {
   String dataPacket = request.getContent().toString(Charsets.UTF_8);
   LOG.info("/ping Got Data {}", dataPacket);
   queue.add(dataPacket);
   latch.countDown();
   responder.sendStatus(HttpResponseStatus.OK);
 }
Beispiel #12
0
 private void searchTagAndRespond(HttpResponder responder, List<String> tags) {
   try {
     // we want to search the entire range, so startTimestamp is '0' and end Timestamp is
     // Integer.MAX_VALUE and
     // limit is -1 , to include the entire search result.
     MetricSearchQuery searchQuery =
         new MetricSearchQuery(
             0, Integer.MAX_VALUE, -1, toTagValues(humanToTagNames(parseTagValues(tags))));
     Collection<TagValue> nextTags = metricStore.findNextAvailableTags(searchQuery);
     responder.sendJson(HttpResponseStatus.OK, tagValuesToHuman(nextTags));
   } catch (IllegalArgumentException e) {
     LOG.warn("Invalid request", e);
     responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
   } catch (Exception e) {
     LOG.error("Exception querying metrics ", e);
     responder.sendString(
         HttpResponseStatus.INTERNAL_SERVER_ERROR, "Internal error while querying for metrics");
   }
 }
 @DELETE
 @Path("data/explore/queries/{id}")
 public void closeQuery(HttpRequest request, HttpResponder responder, @PathParam("id") String id) {
   try {
     QueryHandle handle = QueryHandle.fromId(id);
     if (!handle.equals(QueryHandle.NO_OP)) {
       exploreService.close(handle);
     }
     responder.sendStatus(HttpResponseStatus.OK);
   } catch (IllegalArgumentException e) {
     LOG.debug("Got exception:", e);
     responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
   } catch (HandleNotFoundException e) {
     responder.sendStatus(HttpResponseStatus.NOT_FOUND);
   } catch (Throwable e) {
     LOG.error("Got exception:", e);
     responder.sendStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
   }
 }
Beispiel #14
0
 private void tagsQuerying(
     HttpRequest request,
     HttpResponder responder,
     List<String> tags,
     List<String> metrics,
     List<String> groupByTags) {
   try {
     responder.sendJson(
         HttpResponseStatus.OK,
         executeQuery(request, parseTagValuesAsMap(tags), groupByTags, metrics));
   } catch (IllegalArgumentException e) {
     LOG.warn("Invalid request", e);
     responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
   } catch (Exception e) {
     LOG.error("Exception querying metrics ", e);
     responder.sendString(
         HttpResponseStatus.INTERNAL_SERVER_ERROR, "Internal error while querying for metrics");
   }
 }
 @POST
 @Path("data/explore/queries/{id}/preview")
 public void getQueryResultPreview(
     HttpRequest request, HttpResponder responder, @PathParam("id") String id) {
   // NOTE: this call is a POST because it is not idempotent: cursor of results is moved
   try {
     QueryHandle handle = QueryHandle.fromId(id);
     List<QueryResult> results;
     if (handle.equals(QueryHandle.NO_OP)) {
       results = Lists.newArrayList();
     } else {
       results = exploreService.previewResults(handle);
     }
     responder.sendJson(HttpResponseStatus.OK, results);
   } catch (IllegalArgumentException e) {
     LOG.debug("Got exception:", e);
     responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
   } catch (SQLException e) {
     LOG.debug("Got exception:", e);
     responder.sendString(
         HttpResponseStatus.BAD_REQUEST,
         String.format("[SQLState %s] %s", e.getSQLState(), e.getMessage()));
   } catch (HandleNotFoundException e) {
     if (e.isInactive()) {
       responder.sendString(
           HttpResponseStatus.CONFLICT, "Preview is unavailable for inactive queries.");
       return;
     }
     responder.sendStatus(HttpResponseStatus.NOT_FOUND);
   } catch (Throwable e) {
     LOG.error("Got exception:", e);
     responder.sendStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
   }
 }
Beispiel #16
0
  private static TimeRange parseTime(
      long fromTimeSecsParam, long toTimeSecsParam, HttpResponder responder) {
    long fromMillis =
        fromTimeSecsParam < 0
            ? System.currentTimeMillis() - TimeUnit.HOURS.toMillis(1)
            : TimeUnit.SECONDS.toMillis(fromTimeSecsParam);
    long toMillis =
        toTimeSecsParam < 0
            ? System.currentTimeMillis()
            : TimeUnit.SECONDS.toMillis(toTimeSecsParam);

    if (toMillis <= fromMillis) {
      responder.sendString(
          HttpResponseStatus.BAD_REQUEST,
          "Invalid time range. " + "'stop' should be greater than 'start'.");
      return null;
    }

    return new TimeRange(fromMillis, toMillis);
  }
  @POST
  @Path("data/explore/queries/{id}/download")
  public void downloadQueryResults(
      HttpRequest request, HttpResponder responder, @PathParam("id") final String id) {
    // NOTE: this call is a POST because it is not idempotent: cursor of results is moved
    boolean responseStarted = false;
    try {
      QueryHandle handle = QueryHandle.fromId(id);
      if (handle.equals(QueryHandle.NO_OP)
          || !exploreService.getStatus(handle).getStatus().equals(QueryStatus.OpStatus.FINISHED)) {
        responder.sendStatus(HttpResponseStatus.CONFLICT);
        return;
      }

      StringBuffer sb = new StringBuffer();
      sb.append(getCSVHeaders(exploreService.getResultSchema(handle)));
      sb.append('\n');

      List<QueryResult> results;
      results = exploreService.previewResults(handle);
      if (results.isEmpty()) {
        results = exploreService.nextResults(handle, DOWNLOAD_FETCH_CHUNK_SIZE);
      }

      ChunkResponder chunkResponder = responder.sendChunkStart(HttpResponseStatus.OK, null);
      responseStarted = true;
      while (!results.isEmpty()) {
        for (QueryResult result : results) {
          appendCSVRow(sb, result);
          sb.append('\n');
        }
        // If failed to send to client, just propagate the IOException and let netty-http to handle
        chunkResponder.sendChunk(ChannelBuffers.wrappedBuffer(sb.toString().getBytes("UTF-8")));
        sb.delete(0, sb.length());
        results = exploreService.nextResults(handle, DOWNLOAD_FETCH_CHUNK_SIZE);
      }
      Closeables.closeQuietly(chunkResponder);

    } catch (IllegalArgumentException e) {
      LOG.debug("Got exception:", e);
      // We can't send another response if sendChunkStart has been called
      if (!responseStarted) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
      }
    } catch (SQLException e) {
      LOG.debug("Got exception:", e);
      if (!responseStarted) {
        responder.sendString(
            HttpResponseStatus.BAD_REQUEST,
            String.format("[SQLState %s] %s", e.getSQLState(), e.getMessage()));
      }
    } catch (HandleNotFoundException e) {
      if (!responseStarted) {
        if (e.isInactive()) {
          responder.sendString(HttpResponseStatus.CONFLICT, "Query is inactive");
        } else {
          responder.sendStatus(HttpResponseStatus.NOT_FOUND);
        }
      }
    } catch (Throwable e) {
      LOG.error("Got exception:", e);
      if (!responseStarted) {
        responder.sendStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
      }
    }
  }