Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #533 from zalando/bugfix/aruha-485
Browse files Browse the repository at this point in the history
aruha-485: added shutdown hook to free subscription resources
  • Loading branch information
adyach authored Jan 19, 2017
2 parents dca9ce0 + bd5928b commit 02a6fa1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ ADD build/libs/nakadi.jar nakadi.jar

EXPOSE 8080

ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar nakadi.jar
ENTRYPOINT exec java -Djava.security.egd=file:/dev/./urandom -jar nakadi.jar
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,16 @@ public long getKafkaPollTimeout() {

@Override
public void stream() throws InterruptedException {
// bugfix ARUHA-485
Runtime.getRuntime().addShutdownHook(new Thread(() -> onNodeShutdown()));
streamInternal(new StartingState());
}

void onNodeShutdown() {
log.info("Shutdown hook called. Trying to terminate subscription gracefully");
switchState(new CleanupState(null));
}

void streamInternal(final State firstState) throws InterruptedException {
// Add first task - switch to starting state.
switchState(firstState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.zalando.nakadi.service.subscription.model.Session;
import org.zalando.nakadi.service.subscription.state.CleanupState;
import org.zalando.nakadi.service.subscription.state.DummyState;
import org.zalando.nakadi.service.subscription.state.State;

import java.io.IOException;
Expand Down Expand Up @@ -126,4 +129,28 @@ public void onExit() {
// Check that onExit called even if onEnter throws exception.
Assert.assertArrayEquals(new boolean[]{true, true}, onExitCalls);
}

@Test
public void testOnNodeShutdown() throws Exception {
final StreamingContext ctxSpy = Mockito.spy(createTestContext(null));

final Thread t = new Thread(() -> {
try {
ctxSpy.streamInternal(new State() {
@Override
public void onEnter() {
}
});
} catch (final InterruptedException ignore) {
}
});
t.start();
t.join(1000);

ctxSpy.onNodeShutdown();

Mockito.verify(ctxSpy).switchState(Mockito.isA(CleanupState.class));
Mockito.verify(ctxSpy).unregisterSession();
Mockito.verify(ctxSpy).switchState(Mockito.isA(DummyState.class));
}
}

0 comments on commit 02a6fa1

Please sign in to comment.