Skip to content
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

Fail task upon function execution error #36

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public InvocationResponse invoke(final byte[] payload) {
try {
final InvokeResult result = futureResult.get(invocationTimeout.toMillis(), TimeUnit.MILLISECONDS);
return new InvocationResponse(result.getStatusCode(), result.getLogResult(),
result.getFunctionError(), start, Instant.now());
result.getFunctionError(), result.getPayload(), start, Instant.now());
} catch (RequestTooLargeException e) {
return checkPayloadSizeForInvocationType(payload, type, start, e);
} catch (final InterruptedException | ExecutionException e) {
LOGGER.error(e.getLocalizedMessage(), e);
throw new InvocationException(e);
} catch (final TimeoutException e) {
return new InvocationResponse(504, e.getLocalizedMessage(), e.getLocalizedMessage(), start,
Instant.now());
return new InvocationResponse(504, e.getLocalizedMessage(), e.getLocalizedMessage(),
null, start, Instant.now());
}
}

Expand Down Expand Up @@ -105,7 +105,7 @@ InvocationResponse checkPayloadSizeForInvocationType(final byte[] payload, final
throw e;
}
// Drop message and continue
return new InvocationResponse(413, e.getLocalizedMessage(), e.getLocalizedMessage(), start, Instant.now());
return new InvocationResponse(413, e.getLocalizedMessage(), e.getLocalizedMessage(), null, start, Instant.now());
}

private class InvocationException extends RuntimeException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.nordstrom.kafka.connect.lambda;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Instant;

public class InvocationResponse {
private final String errorString;
private final ByteBuffer errorDescription;
private final String responseString;
private final Integer statusCode;
private final Instant start;
Expand All @@ -13,12 +17,14 @@ public InvocationResponse(
final Integer statusCode,
final String logResult,
final String functionError,
final ByteBuffer errorDescription,
final Instant start,
final Instant end) {

this.statusCode = statusCode;
this.responseString = logResult;
this.errorString = functionError;
this.errorDescription = errorDescription;
this.start = start;
this.end = end;

Expand All @@ -36,6 +42,11 @@ public String getErrorString() {
return this.errorString;
}

public String getErrorDescription() {
Charset charset = StandardCharsets.UTF_8;
return charset.decode(this.errorDescription).toString();
}

public String getResponseString() {
return this.responseString;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,22 @@ private InvocationResponse invoke(final String payload) {
return response;
}

private void handleResponse(
void handleResponse(
final InvocationResponse response,
final AtomicInteger retryCount,
final Collection<Integer> retriableErrorCodes,
final int maxRetries,
final long backoffTimeMs) {
if (response.getStatusCode() < 300 && response.getStatusCode() >= 200) {

String functionError = response.getErrorString();
if (functionError != null && !functionError.isEmpty()) {
//function-error
throw new FunctionExecutionException(MessageFormat
.format("Lambda function execution failed. Reason: {0}: {1}",
response.getErrorString(),
response.getErrorDescription()
));
} else if (response.getStatusCode() < 300 && response.getStatusCode() >= 200) {
//success
retryCount.set(0);
} else {
Expand Down Expand Up @@ -260,4 +269,10 @@ private class OutOfRetriesException extends RuntimeException {
super(message);
}
}

protected static class FunctionExecutionException extends RuntimeException {
FunctionExecutionException(final String message) {
super(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
import org.apache.kafka.connect.data.SchemaBuilder;
import org.apache.kafka.connect.sink.SinkRecord;
import org.apache.kafka.connect.sink.SinkTaskContext;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;

import static org.apache.kafka.connect.data.Schema.STRING_SCHEMA;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -48,7 +52,7 @@ public void testPutWhenBatchingIsNotEnabled() {
InvocationClient mockedClient = mock(InvocationClient.class);

when(mockedClient.invoke(any()))
.thenReturn(new InvocationResponse(200, "test log", "", Instant.now(), Instant.now()));
.thenReturn(new InvocationResponse(200, "test log", "", null, Instant.now(), Instant.now()));

Schema testSchema = SchemaBuilder.struct().name("com.nordstrom.kafka.connect.lambda.foo").field("bar", STRING_SCHEMA).build();

Expand All @@ -58,4 +62,30 @@ public void testPutWhenBatchingIsNotEnabled() {

task.put(testList);
}

@Test(expected = LambdaSinkTask.FunctionExecutionException.class)
public void testHandleResponseWhenFunctionExecutionFails() {

LambdaSinkTask task = new LambdaSinkTask();

String msg = "{\"errorMessage\": \"foo\", \"errorType\": \"ValueError\", \"stackTrace\": [\" File \\\"/var/task/lambda_function.py\\\", line 5, in lambda_handler\\n raise ValueError(\\\"foo\\\")\\n\"]}";
ByteBuffer errorDescription = ByteBuffer.wrap(msg.getBytes(StandardCharsets.UTF_8));
InvocationResponse invocationResponse = new InvocationResponse(200, "test log", "Unhandled", errorDescription, Instant.now(), Instant.now());

task.handleResponse(invocationResponse, new AtomicInteger(0), Arrays.asList(501,504), 3, 1L);

}

@Test
public void testHandleResponseWhenFunctionInvocationAndExecutionSucceeds() {

LambdaSinkTask task = new LambdaSinkTask();
InvocationResponse invocationResponse = new InvocationResponse(200, "test log", null, null, Instant.now(), Instant.now());
AtomicInteger retryCounter = new AtomicInteger(2);

task.handleResponse(invocationResponse, retryCounter, Arrays.asList(501,504), 3, 1L);

Assert.assertEquals(0, retryCounter.intValue());
}

}