Skip to content

runtime-v2: fix for exception stack trace logging #1062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions cli/src/main/java/com/walmartlabs/concord/cli/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
import com.walmartlabs.concord.runtime.v2.runner.Runner;
import com.walmartlabs.concord.runtime.v2.runner.guice.ProcessDependenciesModule;
import com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders;
import com.walmartlabs.concord.runtime.v2.runner.vm.WrappedException;
import com.walmartlabs.concord.runtime.v2.sdk.*;
import com.walmartlabs.concord.runtime.v2.runner.vm.LoggedException;
import com.walmartlabs.concord.sdk.Constants;
import com.walmartlabs.concord.sdk.MapUtils;
import com.walmartlabs.concord.svm.ParallelExecutionException;
Expand Down Expand Up @@ -285,18 +285,10 @@ public Integer call() throws Exception {

try {
runner.start(cfg, processDefinition, args);
} catch (LoggedException e) {
return -1;
} catch (ParallelExecutionException e) {
System.err.println(e.getMessage());
} catch (ParallelExecutionException | UserDefinedException e) {
return -1;
} catch (Exception e) {
if (verbosity.verbose()) {
System.err.print("Error: ");
e.printStackTrace(System.err);
} else {
System.err.println("Error: " + e.getMessage());
}
logException(verbosity, e);
return 1;
}

Expand Down Expand Up @@ -424,6 +416,15 @@ private static void dumpArguments(Map<String, Object> args) {
}
}

private static void logException(Verbosity verbosity, Exception e) {
if (verbosity.verbose()) {
System.err.print("Error: ");
e.printStackTrace(System.err);
} else {
System.err.println("Error: " + e.getMessage());
}
}

private static class CopyNotifier implements FileVisitor {

private final long notifyOnCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static ConcordRule configure() {
.useLocalMavenRepository(true)
.extraConfigurationSupplier(() -> """
concord-agent {
dependencyResolveTimeout = "20 seconds"
dependencyResolveTimeout = "30 seconds"
logMaxDelay = "250 milliseconds"
pollInterval = "250 milliseconds"
prefork {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ public void testThrowParallelWithPayload() throws Exception {

// ---
Map<String, Object> data = proc.getOutVariables();
List<Map<String, Object>> exceptions = (List<Map<String, Object>>) ConfigurationUtils.get(data, "exceptions");
List<Map<String, Object>> exceptions = MapUtils.getList(data, "exceptions", List.of());

assertNotNull(exceptions);
assertEquals(List.of("BOOM1", "BOOM2"), exceptions.stream().map(e -> e.get("message")).toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ flows:
parallelism: 2
error:
- set:
exceptions: ${lastError.cause.exceptions.stream().map(e -> e.cause).toList()}
exceptions: ${lastError.exceptions.stream().toList()}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
configuration:
runtime: concord-v2

flows:
default:
- task: http
in:
method: GET
url: ${baseUrl.replace('://', '://' += ${processInfo.sessionToken})}/api/v1/process/${txId}
out: result

- log: "${result}"
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.walmartlabs.concord.client2.*;
import com.walmartlabs.concord.common.IOUtils;
import com.walmartlabs.concord.runtime.v2.sdk.TaskResult;
import com.walmartlabs.concord.runtime.v2.sdk.UserDefinedException;
import com.walmartlabs.concord.sdk.Constants;
import com.walmartlabs.concord.sdk.LogTags;
import com.walmartlabs.concord.sdk.MapUtils;
Expand Down Expand Up @@ -415,7 +416,7 @@ private static void handleResults(Map<String, ProcessEntry> m, boolean ignoreFai
}

if (hasErrors) {
throw new IllegalStateException(errors.toString());
throw new UserDefinedException(errors.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.walmartlabs.concord.runtime.v2.runner.tasks.TaskV2Provider;
import com.walmartlabs.concord.runtime.v2.runner.vm.BlockCommand;
import com.walmartlabs.concord.runtime.v2.runner.vm.ParallelCommand;
import com.walmartlabs.concord.runtime.v2.runner.vm.WrappedException;
import com.walmartlabs.concord.runtime.v2.sdk.*;
import com.walmartlabs.concord.sdk.Constants;
import com.walmartlabs.concord.svm.Runtime;
Expand All @@ -70,8 +71,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;

Expand Down Expand Up @@ -150,7 +150,10 @@ public void setWorkDir(Path newWorkDir) {
}

public void deploy(String resource) throws URISyntaxException, IOException {
Path src = Paths.get(testClass.getResource(resource).toURI());
var res = testClass.getResource(resource);
assertNotNull(res, "Resource not found: " + resource);

Path src = Paths.get(res.toURI());
IOUtils.copy(src, workDir);
}

Expand Down Expand Up @@ -234,6 +237,11 @@ protected void configure() {
runtimeModule)
.create();
injector.getInstance(Main.class).execute();
} catch (UserDefinedException | ParallelExecutionException e) { // see {@link com.walmartlabs.concord.runtime.v2.runner.Main#main}
throw e;
} catch (Throwable t) {
t.printStackTrace(out);
throw t;
} finally {
out.flush();
System.setOut(oldOut);
Expand Down Expand Up @@ -267,6 +275,17 @@ public static void assertLogAtLeast(byte[] ab, int n, String pattern) throws IOE
}
}

public static void assertLogExactMatch(byte[] ab, int n, String pattern) throws IOException {
if (ab == null) {
fail("Log is empty");
}

int count = grep(ab, pattern);
if (count != n) {
fail("Expected exactly " + n + " log line(s): " + pattern + ", but found: " + count + "\nLog content:\n" + new String(ab));
}
}

public static void assertLog(byte[] ab, String pattern) throws IOException {
if (ab == null) {
fail("Log is empty");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package com.walmartlabs.concord.runtime.v2.runner;

/*-
* *****
* Concord
* -----
* Copyright (C) 2017 - 2025 Walmart Inc.
* -----
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =====
*/

import com.walmartlabs.concord.runtime.v2.sdk.ProcessConfiguration;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static com.walmartlabs.concord.runtime.v2.runner.TestRuntimeV2.*;
import static java.util.regex.Pattern.quote;
import static org.junit.jupiter.api.Assertions.fail;

public class LogExceptionsTest {

@RegisterExtension
private static final TestRuntimeV2 runtime = new TestRuntimeV2();

@Test
public void shouldLogExceptionStackTraceWhenTaskThrowsException() throws Exception {
runtime.deploy("logExceptionTests/fromTask");

runtime.save(ProcessConfiguration.builder()
.build());

try {
runtime.run();
fail("Exception expected");
} catch (Exception e) {
// ignore
}

// error
assertLog(runtime.lastLog(), ".*" + quote("(concord.yaml): Error @ line: 3, col: 7. boom!") + ".*");
// stacktrace
assertLog(runtime.lastLog(), ".*at com.walmartlabs.concord.runtime.v2.runner.LogExceptionsTest.shouldLogExceptionStackTraceWhenTaskThrowsException.*");
}

@Test
public void shouldLogExceptionStackTraceWhenExpressionThrowsException() throws Exception {
runtime.deploy("logExceptionTests/fromExpression");

runtime.save(ProcessConfiguration.builder()
.build());

try {
runtime.run();
fail("Exception expected");
} catch (Exception e) {
// ignore
}

// error
// TODO: "javax.el.ELException: java.lang.Exception: ..." remove javax.el.ELException?
assertLog(runtime.lastLog(), ".*" + quote("Error @ line: 3, col: 7. while evaluating expression '${faultyTask.exception('BOOM')}': javax.el.ELException: java.lang.Exception: BOOM") + ".*");
// stacktrace
assertLog(runtime.lastLog(), ".*at com.walmartlabs.concord.runtime.v2.runner.LogExceptionsTest.shouldLogExceptionStackTraceWhenExpressionThrowsException.*");
}

@Test
public void shouldLogExceptionStackTraceWhenTaskThrowsExceptionFromParallel() throws Exception {
runtime.deploy("logExceptionTests/fromParallel");

runtime.save(ProcessConfiguration.builder()
.build());

try {
runtime.run();
fail("Exception expected");
} catch (Exception e) {
// ignore
}

// error
assertLog(runtime.lastLog(), ".*" + quote("(concord.yaml): Error @ line: 4, col: 11. boom!") + ".*");

// stacktrace
assertLog(runtime.lastLog(), ".*" + quote("at com.walmartlabs.concord.runtime.v2.runner.tasks.Tasks$FaultyTask3.execute") + ".*");
}

@Test
public void noStacktraceForUserDefinedExceptionFromTask() throws Exception {
runtime.deploy("logExceptionTests/userDefinedExceptionFromTask");

runtime.save(ProcessConfiguration.builder()
.build());

try {
runtime.run();
fail("Exception expected");
} catch (Exception e) {
// ignore
}

// error
assertLog(runtime.lastLog(), ".*" + quote("(concord.yaml): Error @ line: 3, col: 9. boom!") + ".*");

// no single exception message
assertNoLog(runtime.lastLog(), quote("boom!"));

// no stacktrace
assertNoLog(runtime.lastLog(), ".*noStacktraceForUserDefinedExceptionFromTask.*");
}

@Test
public void noStacktraceForUserDefinedExceptionFromExpression() throws Exception {
runtime.deploy("logExceptionTests/userDefinedExceptionFromExpression");

runtime.save(ProcessConfiguration.builder()
.build());

try {
runtime.run();
fail("Exception expected");
} catch (Exception e) {
// ignore
}

// error
assertLog(runtime.lastLog(), ".*" + quote("(concord.yaml): Error @ line: 3, col: 7. BOOM") + ".*");

// no single exception message
assertNoLog(runtime.lastLog(), quote("BOOM"));

// no stacktrace
assertNoLog(runtime.lastLog(), ".*noStacktraceForUserDefinedExceptionFromExpression.*");
}

@Test
public void noStacktraceForUserDefinedExceptionFromTaskParallel() throws Exception {
runtime.deploy("logExceptionTests/userDefinedExceptionFromTaskParallel");

runtime.save(ProcessConfiguration.builder()
.build());

try {
runtime.run();
fail("Exception expected");
} catch (Exception e) {
// ignore
}

// error
assertLog(runtime.lastLog(), ".*" + quote("(concord.yaml): Error @ line: 4, col: 11. boom!") + ".*");
assertMultiLineLog(runtime.lastLog(), ".*" + quote("(concord.yaml): Error @ line: 3, col: 7. Parallel execution errors: ") + "\n" + quote("boom!"));

assertLogExactMatch(runtime.lastLog(), 1, ".*" + quote("Parallel execution errors") + ".*");

// no stacktrace
assertNoLog(runtime.lastLog(), ".*" + quote("com.walmartlabs.concord.runtime.v2.runner.tasks.Tasks$UserDefinedExceptionTask.execute") + ".*");
}

@Test
public void noStacktraceForTaskFailReturn() throws Exception {
runtime.deploy("logExceptionTests/failResultFromTask");

runtime.save(ProcessConfiguration.builder()
.build());

try {
runtime.run();
fail("Exception expected");
} catch (Exception e) {
// ignore
}

// error
assertLog(runtime.lastLog(), ".*" + quote("(concord.yaml): Error @ line: 3, col: 9. boom!") + ".*");

assertLogExactMatch(runtime.lastLog(), 1, ".*" + quote("boom!") + ".*");

// no stacktrace
assertNoLog(runtime.lastLog(), ".*noStacktraceForTaskFailReturn.*");
assertNoLog(runtime.lastLog(), ".*" + quote("at com.walmartlabs.concord.runtime.v2.runner.tasks.Tasks$FaultyTask.execute") + ".*");
}
}
Loading
Loading