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

fix: idempotent terminate that can handle hung streams #4643

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,10 @@ private void unregisterQuery(final ServiceContext serviceContext, final QueryMet
final String applicationId = query.getQueryApplicationId();

if (!query.getState().equalsIgnoreCase("NOT_RUNNING")) {
throw new IllegalStateException("query not stopped."
+ " id " + applicationId + ", state: " + query.getState());
log.warn(
"Unregistering query that has not terminated. "
+ "This may happen when streams threads are hung. State: " + query.getState()
);
}

if (!allLiveQueries.remove(query)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,8 @@ private void terminateQuery(final PreparedStatement<TerminateQuery> terminateQue
return;
}

ksqlEngine.getPersistentQuery(queryId.get())
.orElseThrow(() ->
new KsqlException(String.format("No running query with id %s was found", queryId)))
.close();
final Optional<PersistentQueryMetadata> query = ksqlEngine.getPersistentQuery(queryId.get());
query.ifPresent(PersistentQueryMetadata::close);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.argThat;
Expand Down Expand Up @@ -715,6 +716,37 @@ public void shouldTerminateAll() {
verify(query1).close();
}

@Test
public void shouldDoIdempotentTerminate() {
// Given:
final String queryStatement = "a persistent query";

final TerminateQuery terminate = mock(TerminateQuery.class);
when(terminate.getQueryId()).thenReturn(Optional.of(new QueryId("foo")));

when(mockParser.parseSingleStatement(any()))
.thenReturn(PreparedStatement.of(queryStatement, terminate));

final PersistentQueryMetadata query = mock(PersistentQueryMetadata.class);

when(mockEngine.getPersistentQuery(new QueryId("foo")))
.thenReturn(Optional.of(query))
.thenReturn(Optional.empty());

final QueuedCommand cmd = new QueuedCommand(
new CommandId(Type.TERMINATE, "-", Action.EXECUTE),
new Command("terminate all", emptyMap(), emptyMap(), Optional.empty()),
Optional.empty(),
0L
);

// When:
statementExecutorWithMocks.handleStatement(cmd);
statementExecutorWithMocks.handleStatement(cmd);

// Then should not throw
}

private void createStreamsAndStartTwoPersistentQueries() {
final Command csCommand = new Command(
"CREATE STREAM pageview ("
Expand Down