Skip to content

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

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 3 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 @@ -25,6 +25,7 @@
import jakarta.inject.Inject;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Supplier;
import javax.sql.DataSource;
Expand Down Expand Up @@ -243,6 +244,11 @@ public synchronized EntityCache getOrCreateEntityCache(RealmContext realmContext
return entityCacheMap.get(realmContext.getRealmIdentifier());
}

@Override
public Iterator<Map.Entry<String, PolarisMetaStoreManager>> getMetaStoreManagerMap() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Make it to Iter<Map.Entry> as https://github.com/apache/polaris/pull/1585/files#r2121942046 suggests

return metaStoreManagerMap.entrySet().iterator();
}

/**
* This method bootstraps service for a given realm: i.e. creates all the needed entities in the
* metastore and creates a root service principal.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,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));
PolarisBaseEntity.Builder taskEntityBuilder =
new PolarisBaseEntity.Builder()
.properties(PolarisObjectMapperUtil.serializeProperties(callCtx, properties))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Supplier;
import org.apache.polaris.core.PolarisCallContext;
Expand Down Expand Up @@ -201,6 +202,11 @@ public synchronized EntityCache getOrCreateEntityCache(RealmContext realmContext
return entityCacheMap.get(realmContext.getRealmIdentifier());
}

@Override
public Iterator<Map.Entry<String, PolarisMetaStoreManager>> getMetaStoreManagerMap() {
return metaStoreManagerMap.entrySet().iterator();
}

/**
* 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 @@ -18,6 +18,7 @@
*/
package org.apache.polaris.core.persistence;

import java.util.Iterator;
import java.util.Map;
import java.util.function.Supplier;
import org.apache.polaris.core.context.RealmContext;
Expand Down Expand Up @@ -48,4 +49,8 @@ default Map<String, PrincipalSecretsResult> bootstrapRealms(BootstrapOptions boo

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

default Iterator<Map.Entry<String, PolarisMetaStoreManager>> getMetaStoreManagerMap() {
throw new UnsupportedOperationException("getMetaStoreManagerMap not supported");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,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));
PolarisBaseEntity.Builder taskEntityBuilder =
new PolarisBaseEntity.Builder()
.id(ms.generateNewIdInCurrentTxn(callCtx))
Expand Down
1 change: 1 addition & 0 deletions runtime/service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ dependencies {
implementation("io.quarkus:quarkus-security")
implementation("io.quarkus:quarkus-smallrye-context-propagation")
implementation("io.quarkus:quarkus-smallrye-fault-tolerance")
implementation("io.quarkus:quarkus-scheduler")

implementation(libs.jakarta.enterprise.cdi.api)
implementation(libs.jakarta.inject.api)
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 @@ -243,5 +243,6 @@ public void deleteFile(String location) {
assertThat(handler.handleTask(task, polarisCallContext)).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 @@ -26,7 +26,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.List;
import java.util.Map;
import java.util.Set;
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 Down Expand Up @@ -106,7 +110,7 @@ void setup() {
metaStoreManagerFactory.getOrCreateSessionSupplier(realmContext).get(),
diagServices,
configurationStore,
Clock.systemDefaultZone());
timeSource);
}

@Test
Expand Down Expand Up @@ -147,6 +151,8 @@ public void testTableCleanup() throws IOException {

handler.handleTask(task, callContext);

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

@danielhumanmod danielhumanmod Jul 6, 2025

Choose a reason for hiding this comment

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

@adnanhemani continue the previous comment here:

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?

metaStoreManager.loadTasks fetches available tasks from the metastore — meaning tasks that are either not leased by any executor or whose lease has already timed out (after 5 minutes).

In this test, the new tasks are created and not executed by its parent task, so to ensure these tasks are fetched, we need to simulate a time gap longer than 5 minutes.


assertThat(
metaStoreManagerFactory
.getOrCreateMetaStoreManager(realmContext)
Expand Down Expand Up @@ -227,6 +233,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 @@ -288,6 +295,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 @@ -408,6 +416,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 @@ -582,6 +591,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