@Override public Optional<QueryStats> handle(Request request, Response response) { if (response.getStatusCode() == HttpStatus.GONE.code()) { return Optional.empty(); } else if (response.getStatusCode() != HttpStatus.OK.code()) { throw new RuntimeException("unexpected error code " + response.getStatusCode() + "; reason=" + response.getStatusMessage()); } try { JsonNode rootNode = objectMapper.readTree(response.getInputStream()); JsonNode queryStatsNode = rootNode.get("queryStats"); if (queryStatsNode == null) { return Optional.empty(); } QueryStats queryStats = objectMapper.treeToValue(queryStatsNode, QueryStats.class); return Optional.of(queryStats); } catch (IOException e) { throw Throwables.propagate(e); } }
@Override public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, ConnectorSession session, TupleDomain<Integer> constraint) { Builder table = InMemoryRecordSet.builder(QUERY_TABLE); for (QueryInfo queryInfo : queryManager.getAllQueryInfo()) { QueryStats queryStats = queryInfo.getQueryStats(); table.addRow( nodeId, queryInfo.getQueryId().toString(), queryInfo.getState().toString(), queryInfo.getSession().getUser(), queryInfo.getSession().getSource().orElse(null), queryInfo.getQuery(), toMillis(queryStats.getQueuedTime()), toMillis(queryStats.getAnalysisTime()), toMillis(queryStats.getDistributedPlanningTime()), toTimeStamp(queryStats.getCreateTime()), toTimeStamp(queryStats.getExecutionStartTime()), toTimeStamp(queryStats.getLastHeartbeat()), toTimeStamp(queryStats.getEndTime())); } return table.build().cursor(); }
private static StatementStats toStatementStats(QueryInfo queryInfo) { QueryStats queryStats = queryInfo.getQueryStats(); return StatementStats.builder() .setState(queryInfo.getState().toString()) .setScheduled(queryInfo.isScheduled()) .setNodes(globalUniqueNodes(queryInfo.getOutputStage()).size()) .setTotalSplits(queryStats.getTotalDrivers()) .setQueuedSplits(queryStats.getQueuedDrivers()) .setRunningSplits(queryStats.getRunningDrivers()) .setCompletedSplits(queryStats.getCompletedDrivers()) .setUserTimeMillis(queryStats.getTotalUserTime().toMillis()) .setCpuTimeMillis(queryStats.getTotalCpuTime().toMillis()) .setWallTimeMillis(queryStats.getTotalScheduledTime().toMillis()) .setProcessedRows(queryStats.getRawInputPositions()) .setProcessedBytes(queryStats.getRawInputDataSize().toBytes()) .setRootStage(toStageStats(queryInfo.getOutputStage())) .build(); }
@JsonCreator public Job(@JsonProperty("user") final String user, @JsonProperty("query") final String query, @JsonProperty("uuid") final UUID uuid, @JsonProperty("output") final PersistentJobOutput output, @JsonProperty("queryStats") final QueryStats queryStats, @JsonProperty("state") final JobState state, @JsonProperty("columns") final List<Column> columns, @JsonProperty("tablesUsed") final Set<Table> tablesUsed, @JsonProperty("queryStarted") final DateTime queryStarted, @JsonProperty("error") final QueryError error, @JsonProperty("queryFinished") final DateTime queryFinished) { this.user = user; this.query = query; this.uuid = uuid; this.output = output; this.queryStats = queryStats; this.state = state; this.columns = columns; this.tablesUsed = tablesUsed; this.queryStarted = queryStarted; this.error = error; this.queryFinished = queryFinished; }
public Job(final String user, final String query, final UUID uuid, final PersistentJobOutput output, final QueryStats stats, final JobState state, final List<Column> columns, final QueryError error, final DateTime queryFinished) { this(user, query, uuid, output, stats, state, columns, Sets.<Table>newConcurrentHashSet(), new DateTime(), error, queryFinished ); }
@Override public Optional<QueryStats> getQueryStats(String queryId) { URI uri = uriBuilderFrom(baseUri).appendPath("/v1/query").appendPath(queryId).build(); Request request = prepareGet().setUri(uri).build(); return httpClient.execute(request, new GetQueryStatsResponseHandler()); }
@Test public void testGetInfoForQuery() throws Exception { mockHttpResponse(SINGLE_QUERY_INFO); Optional<QueryStats> infoForQuery = queryStatsClient.getQueryStats("20150505_160116_00005_sdzex"); assertThat(infoForQuery).isPresent(); assertThat(infoForQuery.get().getTotalCpuTime().getValue()).isEqualTo(1.19); }
@Test public void testGetInfoForUnknownQuery() throws Exception { mockErrorHttpResponse(HttpStatus.GONE); Optional<QueryStats> infoForQuery = queryStatsClient.getQueryStats("20150505_160116_00005_sdzex"); assertThat(infoForQuery).isEmpty(); }
@JsonCreator public BasicQueryInfo( @JsonProperty("queryStats") QueryStats queryStats, @JsonProperty("inputs") Set<Input> inputs) { this.queryStats = queryStats; this.inputs = inputs; }
/** * Rate Limited updateJobInfo */ protected void rlUpdateJobInfo( Set<Table> usedTables, List<Column> columns, QueryStats queryStats, JobState state, QueryError error, List<List<Object>> outputPreview) { if (updateLimiter.tryAcquire(1)) { updateJobInfo(usedTables, columns, queryStats, state, error, outputPreview, true); } else { updateJobInfo(usedTables, columns, queryStats, state, error, outputPreview, false); } }
@Override public Optional<QueryStats> handleException(Request request, Exception exception) { throw propagate(request, exception); }
public void completionEvent(QueryInfo queryInfo) { try { QueryStats queryStats = queryInfo.getQueryStats(); FailureInfo failureInfo = queryInfo.getFailureInfo(); String failureType = failureInfo == null ? null : failureInfo.getType(); String failureMessage = failureInfo == null ? null : failureInfo.getMessage(); ImmutableMap.Builder<String, String> mergedProperties = ImmutableMap.builder(); mergedProperties.putAll(queryInfo.getSession().getSystemProperties()); for (Map.Entry<String, Map<String, String>> catalogEntry : queryInfo.getSession().getCatalogProperties().entrySet()) { for (Map.Entry<String, String> entry : catalogEntry.getValue().entrySet()) { mergedProperties.put(catalogEntry.getKey() + "." + entry.getKey(), entry.getValue()); } } TaskInfo task = null; StageInfo stageInfo = queryInfo.getOutputStage(); if (stageInfo != null) { task = stageInfo.getTasks().stream() .filter(taskInfo -> taskInfo.getState() == TaskState.FAILED) .findFirst().orElse(null); } String failureHost = task == null ? null : task.getSelf().getHost(); String failureTask = task == null ? null : task.getTaskId().toString(); eventClient.post( new QueryCompletionEvent( queryInfo.getQueryId(), queryInfo.getSession().getTransactionId().map(TransactionId::toString).orElse(null), queryInfo.getSession().getUser(), queryInfo.getSession().getPrincipal().orElse(null), queryInfo.getSession().getSource().orElse(null), serverVersion, environment, queryInfo.getSession().getCatalog().orElse(null), queryInfo.getSession().getSchema().orElse(null), queryInfo.getSession().getRemoteUserAddress().orElse(null), queryInfo.getSession().getUserAgent().orElse(null), queryInfo.getState(), queryInfo.getSelf(), queryInfo.getFieldNames(), queryInfo.getQuery(), queryStats.getPeakMemoryReservation().toBytes(), queryStats.getCreateTime(), queryStats.getExecutionStartTime(), queryStats.getEndTime(), queryStats.getQueuedTime(), queryStats.getAnalysisTime(), queryStats.getDistributedPlanningTime(), queryStats.getTotalScheduledTime(), queryStats.getTotalCpuTime(), queryStats.getRawInputDataSize(), queryStats.getRawInputPositions(), queryStats.getTotalDrivers(), queryInfo.getErrorCode(), failureType, failureMessage, failureTask, failureHost, objectMapper.writeValueAsString(queryInfo.getOutputStage()), objectMapper.writeValueAsString(queryInfo.getFailureInfo()), objectMapper.writeValueAsString(queryInfo.getInputs()), objectMapper.writeValueAsString(mergedProperties.build()) ) ); logQueryTimeline(queryInfo); } catch (JsonProcessingException e) { throw Throwables.propagate(e); } }
protected void updateJobInfo( Set<Table> usedTables, List<Column> columns, QueryStats queryStats, JobState state, QueryError error, List<List<Object>> outputPreview, boolean postUpdate) { if ((usedTables != null) && (usedTables.size() > 0)) { job.getTablesUsed().addAll(usedTables); } if ((columns != null) && (columns.size() > 0)) { job.setColumns(columns); } if (queryStats != null) { job.setQueryStats(queryStats); } if ((state != null) && (job.getState() != JobState.FINISHED) && (job.getState() != JobState.FAILED)) { job.setState(state); } if (error != null) { FailureInfo failureInfo = new FailureInfo( error.getFailureInfo().getType(), error.getFailureInfo().getMessage(), null, Collections.<FailureInfo>emptyList(), Collections.<String>emptyList(), error.getFailureInfo().getErrorLocation()); QueryError queryError = new QueryError( error.getMessage(), error.getSqlState(), error.getErrorCode(), error.getErrorName(), error.getErrorType(), error.getErrorLocation(), failureInfo); job.setError(queryError); } if (postUpdate) { eventBus.post(new JobUpdateEvent(job, outputPreview)); } }
public static QueryStats createNoOpQueryStats() { DateTime now = DateTime.now(); io.airlift.units.Duration zeroDuration = new io.airlift.units.Duration(0, TimeUnit.SECONDS); DataSize zeroData = new DataSize(0, DataSize.Unit.BYTE); return new QueryStats( now, null, now, now, zeroDuration, zeroDuration, zeroDuration, zeroDuration, zeroDuration, zeroDuration, 0, 0, 0, 0, 0, 0, 0, 0.0, zeroData, zeroData, zeroDuration, zeroDuration, zeroDuration, zeroDuration, false, ImmutableSet.of(), zeroData, 0, zeroData, 0, zeroData, 0 ); }
/** * Obtains QueryStats for query with given id. If query does not exist or * does not contain queryStats information Optional.empty() is returned. */ Optional<QueryStats> getQueryStats(String queryId);