Skip to content

Support retrying non-finished async tasks on startup and periodically #1585

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

Open
wants to merge 8 commits into
base: main
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 @@ -172,6 +172,11 @@ public Map<String, BaseResult> purgeRealms(Iterable<String> realms) {
return Map.copyOf(results);
}

@Override
public Map<String, PolarisMetaStoreManager> getMetaStoreManagerMap() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make this a bit more defensively-coded, I might recommend making this into a iterator of Map.Entry objects, given that this is a public method and we wouldn't want any code path to be able to modify this mapping?

return metaStoreManagerMap;
}

@Override
public synchronized PolarisMetaStoreManager getOrCreateMetaStoreManager(
RealmContext realmContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,16 @@ private void revokeGrantRecord(
PolarisTaskConstants.TASK_TYPE,
String.valueOf(AsyncTaskType.ENTITY_CLEANUP_SCHEDULER.typeCode()));
properties.put("data", PolarisObjectMapperUtil.serialize(callCtx, refreshEntityToDrop));
// Update LAST_ATTEMPT_START_TIME to prevent multiple executors from picking the same task
// simultaneously; protected by TASK_TIMEOUT_MILLIS
properties.put(
PolarisTaskConstants.LAST_ATTEMPT_START_TIME,
String.valueOf(callCtx.getClock().millis()));
properties.put(
PolarisTaskConstants.ATTEMPT_COUNT,
String.valueOf(
Integer.parseInt(properties.getOrDefault(PolarisTaskConstants.ATTEMPT_COUNT, "0"))
+ 1));
taskEntity.setProperties(PolarisObjectMapperUtil.serializeProperties(callCtx, properties));
if (cleanupProperties != null) {
taskEntity.setInternalProperties(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ public synchronized EntityCache getOrCreateEntityCache(RealmContext realmContext
return entityCacheMap.get(realmContext.getRealmIdentifier());
}

@Override
public Map<String, PolarisMetaStoreManager> getMetaStoreManagerMap() {
return metaStoreManagerMap;
}

/**
* This method bootstraps service for a given realm: i.e. creates all the needed entities in the
* metastore and creates a root service principal. After that we rotate the root principal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ Map<String, PrincipalSecretsResult> bootstrapRealms(

/** Purge all metadata for the realms provided */
Map<String, BaseResult> purgeRealms(Iterable<String> realms);

default Map<String, PolarisMetaStoreManager> getMetaStoreManagerMap() {
throw new UnsupportedOperationException("getMetaStoreManagerMap not supported");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,16 @@ private void bootstrapPolarisService(
PolarisTaskConstants.TASK_TYPE,
String.valueOf(AsyncTaskType.ENTITY_CLEANUP_SCHEDULER.typeCode()));
properties.put("data", PolarisObjectMapperUtil.serialize(callCtx, refreshEntityToDrop));
// Update LAST_ATTEMPT_START_TIME to prevent multiple executors from picking the same task
// simultaneously; protected by TASK_TIMEOUT_MILLIS
properties.put(
PolarisTaskConstants.LAST_ATTEMPT_START_TIME,
String.valueOf(callCtx.getClock().millis()));
properties.put(
PolarisTaskConstants.ATTEMPT_COUNT,
String.valueOf(
Integer.parseInt(properties.getOrDefault(PolarisTaskConstants.ATTEMPT_COUNT, "0"))
+ 1));
taskEntity.setProperties(PolarisObjectMapperUtil.serializeProperties(callCtx, properties));
if (cleanupProperties != null) {
taskEntity.setInternalProperties(
Expand Down
2 changes: 1 addition & 1 deletion quarkus/service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ dependencies {
implementation("io.quarkus:quarkus-security")
implementation("io.quarkus:quarkus-smallrye-context-propagation")
implementation("io.quarkus:quarkus-smallrye-fault-tolerance")

implementation(libs.jakarta.enterprise.cdi.api)
implementation(libs.jakarta.inject.api)
implementation(libs.jakarta.validation.api)
Expand All @@ -71,6 +70,7 @@ dependencies {
implementation(libs.auth0.jwt)

implementation(libs.bouncycastle.bcprov)
implementation("io.quarkus:quarkus-scheduler")

compileOnly(libs.jakarta.annotation.api)
compileOnly(libs.spotbugs.annotations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.quarkus.runtime.Startup;
import io.quarkus.scheduler.Scheduled;
import io.smallrye.common.annotation.Identifier;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -60,6 +62,18 @@ public void init() {
super.init();
}

@PostConstruct
@Override
public void postConstruct() {
super.postConstruct();
}

@Scheduled(every = "PT10M")
@Override
public void scheduled() {
super.scheduled();
}

@Override
protected void handleTask(long taskEntityId, CallContext callContext, int attempt) {
Span span =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,12 @@ public void deleteFile(String location) {
.setName(UUID.randomUUID().toString())
.build();
addTaskLocation(task);

assertThatPredicate(handler::canHandleTask).accepts(task);
assertThat(handler.handleTask(task, callCtx)).isTrue();
assertThatPredicate((String f) -> TaskUtils.exists(f, fileIO)).rejects(dataFile1Path);
assertThatPredicate((String f) -> TaskUtils.exists(f, fileIO)).rejects(dataFile2Path);
assertThatPredicate((String f) -> TaskUtils.exists(f, fileIO)).rejects(manifestFile.path());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import jakarta.annotation.Nonnull;
import jakarta.inject.Inject;
import java.io.IOException;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -68,6 +70,7 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.slf4j.LoggerFactory;
import org.threeten.extra.MutableClock;

@QuarkusTest
class TableCleanupTaskHandlerTest {
Expand All @@ -78,6 +81,7 @@ class TableCleanupTaskHandlerTest {
private CallContext callContext;

private final RealmContext realmContext = () -> "realmName";
private final MutableClock timeSource = MutableClock.of(Instant.now(), ZoneOffset.UTC);

private TaskFileIOSupplier buildTaskFileIOSupplier(FileIO fileIO) {
return new TaskFileIOSupplier(
Expand All @@ -103,7 +107,7 @@ void setup() {
metaStoreManagerFactory.getOrCreateSessionSupplier(realmContext).get(),
diagServices,
configurationStore,
Clock.systemDefaultZone());
timeSource);

callContext = CallContext.of(realmContext, polarisCallContext);
}
Expand Down Expand Up @@ -152,6 +156,7 @@ public void testTableCleanup() throws IOException {

handler.handleTask(task, callContext);

timeSource.add(Duration.ofMinutes(10));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, task entity might miss LAST_ATTEMPT_START_TIME prop so loading tasks without time-out can success; After complete each task entity with this property, we need to manipulate time to make loadTasks works

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this further - I'm not sure why the tests need this 10m jump? Is it so that tasks are "recovered" by the Quarkus Scheduled method?

assertThat(
metaStoreManagerFactory
.getOrCreateMetaStoreManager(realmContext)
Expand Down Expand Up @@ -232,6 +237,7 @@ public void close() {
assertThat(results).containsExactly(true, true);

// both tasks successfully executed, but only one should queue subtasks
timeSource.add(Duration.ofMinutes(10));
assertThat(
metaStoreManagerFactory
.getOrCreateMetaStoreManager(realmContext)
Expand Down Expand Up @@ -293,6 +299,7 @@ public void close() {
assertThat(results).containsExactly(true, true);

// both tasks successfully executed, but only one should queue subtasks
timeSource.add(Duration.ofMinutes(10));
assertThat(
metaStoreManagerFactory
.getOrCreateMetaStoreManager(realmContext)
Expand Down Expand Up @@ -413,6 +420,7 @@ public void testTableCleanupMultipleSnapshots() throws IOException {

handler.handleTask(task, callContext);

timeSource.add(Duration.ofMinutes(10));
List<PolarisBaseEntity> entities =
metaStoreManagerFactory
.getOrCreateMetaStoreManager(realmContext)
Expand Down Expand Up @@ -587,6 +595,7 @@ public void testTableCleanupMultipleMetadata() throws IOException {

handler.handleTask(task, callContext);

timeSource.add(Duration.ofMinutes(10));
List<PolarisBaseEntity> entities =
metaStoreManagerFactory
.getOrCreateMetaStoreManager(callContext.getRealmContext())
Expand Down
Loading