@Description("Decodes json to an exception and throws it") @ScalarFunction @SqlType("unknown") public static void fail(@SqlType(StandardTypes.JSON) Slice failureInfoSlice) { FailureInfo failureInfo = JSON_CODEC.fromJson(failureInfoSlice.getBytes()); // wrap the failure in a new exception to append the current stack trace throw new PrestoException(StandardErrorCode.USER_ERROR, failureInfo.toException()); }
public FailureInfo toFailureInfo() { List<FailureInfo> suppressed = this.suppressed.stream() .map(ExecutionFailureInfo::toFailureInfo) .collect(toImmutableList()); return new FailureInfo(type, message, cause == null ? null : cause.toFailureInfo(), suppressed, stack, errorLocation); }
private static QueryError toQueryError(QueryInfo queryInfo) { FailureInfo failure = queryInfo.getFailureInfo(); if (failure == null) { QueryState state = queryInfo.getState(); if ((!state.isDone()) || (state == QueryState.FINISHED)) { return null; } log.warn("Query %s in state %s has no failure info", queryInfo.getQueryId(), state); failure = toFailure(new RuntimeException(format("Query is %s (reason unknown)", state))).toFailureInfo(); } ErrorCode errorCode; if (queryInfo.getErrorCode() != null) { errorCode = queryInfo.getErrorCode(); } else { errorCode = INTERNAL_ERROR.toErrorCode(); log.warn("Failed query %s has no error code", queryInfo.getQueryId()); } return new QueryError( failure.getMessage(), null, errorCode.getCode(), errorCode.getName(), toErrorType(errorCode.getCode()).toString(), failure.getErrorLocation(), failure); }
@VisibleForTesting @NotNull public static Expression createFailureFunction(RuntimeException exception, Type type) { requireNonNull(exception, "Exception is null"); String failureInfo = JsonCodec.jsonCodec(FailureInfo.class).toJson(Failures.toFailure(exception).toFailureInfo()); FunctionCall jsonParse = new FunctionCall(QualifiedName.of("json_parse"), ImmutableList.of(new StringLiteral(failureInfo))); FunctionCall failureFunction = new FunctionCall(QualifiedName.of("fail"), ImmutableList.of(jsonParse)); return new Cast(failureFunction, type.getTypeSignature().toString()); }
@Override public TaskInfo getTaskInfo() { TaskState state = taskStateMachine.getState(); List<FailureInfo> failures = ImmutableList.of(); if (state == TaskState.FAILED) { failures = toFailures(taskStateMachine.getFailureCauses()); } return new TaskInfo(taskStateMachine.getTaskId(), nextTaskInfoVersion.getAndIncrement(), state, location, DateTime.now(), sharedBuffer.getInfo(), ImmutableSet.<PlanNodeId> of(), taskContext.getTaskStats(), failures, taskContext.getOutputItems()); }
@JsonCreator public QueryInfo( @JsonProperty("queryId") QueryId queryId, @JsonProperty("session") SessionRepresentation session, @JsonProperty("state") QueryState state, @JsonProperty("memoryPool") MemoryPoolId memoryPool, @JsonProperty("scheduled") boolean scheduled, @JsonProperty("self") URI self, @JsonProperty("fieldNames") List<String> fieldNames, @JsonProperty("query") String query, @JsonProperty("queryStats") QueryStats queryStats, @JsonProperty("setSessionProperties") Map<String, String> setSessionProperties, @JsonProperty("resetSessionProperties") Set<String> resetSessionProperties, @JsonProperty("startedTransactionId") Optional<TransactionId> startedTransactionId, @JsonProperty("clearTransactionId") boolean clearTransactionId, @JsonProperty("updateType") String updateType, @JsonProperty("outputStage") StageInfo outputStage, @JsonProperty("failureInfo") FailureInfo failureInfo, @JsonProperty("errorCode") ErrorCode errorCode, @JsonProperty("inputs") Set<Input> inputs) { requireNonNull(queryId, "queryId is null"); requireNonNull(session, "session is null"); requireNonNull(state, "state is null"); requireNonNull(self, "self is null"); requireNonNull(fieldNames, "fieldNames is null"); requireNonNull(queryStats, "queryStats is null"); requireNonNull(setSessionProperties, "setSessionProperties is null"); requireNonNull(resetSessionProperties, "resetSessionProperties is null"); requireNonNull(startedTransactionId, "startedTransactionId is null"); requireNonNull(query, "query is null"); requireNonNull(inputs, "inputs is null"); this.queryId = queryId; this.session = session; this.state = state; this.memoryPool = requireNonNull(memoryPool, "memoryPool is null"); this.scheduled = scheduled; this.self = self; this.fieldNames = ImmutableList.copyOf(fieldNames); this.query = query; this.queryStats = queryStats; this.setSessionProperties = ImmutableMap.copyOf(setSessionProperties); this.resetSessionProperties = ImmutableSet.copyOf(resetSessionProperties); this.startedTransactionId = startedTransactionId; this.clearTransactionId = clearTransactionId; this.updateType = updateType; this.outputStage = outputStage; this.failureInfo = failureInfo; this.errorType = errorCode == null ? null : toErrorType(errorCode.getCode()); this.errorCode = errorCode; this.inputs = ImmutableSet.copyOf(inputs); }
@Nullable @JsonProperty public FailureInfo getFailureInfo() { return failureInfo; }
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); } }
private static void assertState(QueryStateMachine stateMachine, QueryState expectedState) { assertEquals(stateMachine.getQueryId(), QUERY_ID); assertEqualSessions(stateMachine.getSession().withoutTransactionId(), TEST_SESSION); assertSame(stateMachine.getMemoryPool(), MEMORY_POOL); assertEquals(stateMachine.getSetSessionProperties(), SET_SESSION_PROPERTIES); assertEquals(stateMachine.getResetSessionProperties(), RESET_SESSION_PROPERTIES); QueryInfo queryInfo = stateMachine.getQueryInfo(null); assertEquals(queryInfo.getQueryId(), QUERY_ID); assertEquals(queryInfo.getSelf(), LOCATION); assertNull(queryInfo.getOutputStage()); assertEquals(queryInfo.getQuery(), QUERY); assertEquals(queryInfo.getInputs(), INPUTS); assertEquals(queryInfo.getFieldNames(), OUTPUT_FIELD_NAMES); assertEquals(queryInfo.getUpdateType(), UPDATE_TYPE); assertEquals(queryInfo.getMemoryPool(), MEMORY_POOL.getId()); QueryStats queryStats = queryInfo.getQueryStats(); if (queryInfo.getState() == QUEUED) { assertNull(queryStats.getQueuedTime()); assertNull(queryStats.getTotalPlanningTime()); assertNull(queryStats.getExecutionStartTime()); assertNull(queryStats.getFinishingTime()); assertNull(queryStats.getEndTime()); } else if (queryInfo.getState() == PLANNING) { assertNotNull(queryStats.getQueuedTime()); assertNull(queryStats.getTotalPlanningTime()); assertNull(queryStats.getExecutionStartTime()); assertNull(queryStats.getFinishingTime()); assertNull(queryStats.getEndTime()); } else if (queryInfo.getState() == STARTING) { assertNotNull(queryStats.getQueuedTime()); assertNotNull(queryStats.getTotalPlanningTime()); assertNull(queryStats.getExecutionStartTime()); assertNull(queryStats.getFinishingTime()); assertNull(queryStats.getEndTime()); } else if (queryInfo.getState() == RUNNING) { assertNotNull(queryStats.getQueuedTime()); assertNotNull(queryStats.getTotalPlanningTime()); assertNotNull(queryStats.getExecutionStartTime()); assertNull(queryStats.getFinishingTime()); assertNull(queryStats.getEndTime()); } else if (queryInfo.getState() == FINISHING) { assertNotNull(queryStats.getQueuedTime()); assertNotNull(queryStats.getTotalPlanningTime()); assertNotNull(queryStats.getExecutionStartTime()); assertNull(queryStats.getFinishingTime()); assertNull(queryStats.getEndTime()); } else { assertNotNull(queryStats.getQueuedTime()); assertNotNull(queryStats.getTotalPlanningTime()); assertNotNull(queryStats.getExecutionStartTime()); assertNotNull(queryStats.getFinishingTime()); assertNotNull(queryStats.getEndTime()); } assertEquals(stateMachine.getQueryState(), expectedState); assertEquals(queryInfo.getState(), expectedState); assertEquals(stateMachine.isDone(), expectedState.isDone()); if (expectedState == FAILED) { FailureInfo failure = queryInfo.getFailureInfo(); assertNotNull(failure); assertEquals(failure.getMessage(), FAILED_CAUSE.getMessage()); assertEquals(failure.getType(), FAILED_CAUSE.getClass().getName()); } else { assertNull(queryInfo.getFailureInfo()); } }
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)); } }