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 a bug in loadTasks which wasn't abiding by compare-and-swap semantics when updating TaskEntities #1134

Open
wants to merge 2 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 @@ -1996,9 +1996,12 @@ private PolarisEntityResolver resolveSecurableToRoleGrant(
},
Function.identity());

List<PolarisBaseEntity> loadedTasks = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

This loadTasks method appears to be called only from test code... Is that expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think @collado-mike had some plans for this. I believe it's what's needed next to have better than "best-effort in-memory attempt at cleanup of tables after purge".

For now I'm just trying to have the tests passing while implementing the NonTransactionalMetaStoreManager that also needs to support loadTasks to pass the base MetaStoreManager test :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On the plus side, task-leasing happens to be the most intensive concurrency set of unittests we have, so this TaskEntity stuff was what helped discover the missing pieces and bugs in my NonTransactionalMetaStoreManager implementation

availableTasks.forEach(
task -> {
PolarisBaseEntity originalTask = new PolarisBaseEntity(task);
// Make a copy to avoid mutating someone else's reference.
// TODO: Refactor into immutable/Builder pattern.
PolarisBaseEntity updatedTask = new PolarisBaseEntity(task);
Map<String, String> properties =
PolarisObjectMapperUtil.deserializeProperties(callCtx, task.getProperties());
properties.put(PolarisTaskConstants.LAST_ATTEMPT_EXECUTOR_ID, executorId);
Expand All @@ -2010,11 +2013,22 @@ private PolarisEntityResolver resolveSecurableToRoleGrant(
String.valueOf(
Integer.parseInt(properties.getOrDefault(PolarisTaskConstants.ATTEMPT_COUNT, "0"))
+ 1));
task.setEntityVersion(task.getEntityVersion() + 1);
task.setProperties(PolarisObjectMapperUtil.serializeProperties(callCtx, properties));
ms.writeEntity(callCtx, task, false, originalTask);
updatedTask.setProperties(
PolarisObjectMapperUtil.serializeProperties(callCtx, properties));
EntityResult result = updateEntityPropertiesIfNotChanged(callCtx, ms, null, updatedTask);
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: why do we need to update task entities in a "load" call?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, loadTasks probably is just a bad name. It is more correctly leaseTasks. Should I rename in this PR?

if (result.getReturnStatus() == BaseResult.ReturnStatus.SUCCESS) {
loadedTasks.add(result.getEntity());
} else {
// TODO: Consider performing incremental leasing of individual tasks one at a time
// instead of requiring all-or-none semantics for all the tasks we think we listed,
// or else contention could be very bad.
ms.rollback();
throw new RetryOnConcurrencyException(
"Failed to lease available task with status %s, info: %s",
result.getReturnStatus(), result.getExtraInformation());
}
});
return new EntitiesResult(availableTasks);
return new EntitiesResult(loadedTasks);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ public List<EntityNameLookupRecord> lookupEntityActiveBatch(
.getSliceEntitiesActive()
.readRange(this.store.buildPrefixKeyComposite(catalogId, parentId, entityType.getCode()))
.stream()
.map(
nameRecord ->
this.lookupEntity(callCtx, catalogId, nameRecord.getId(), entityType.getCode()))
.filter(entityFilter)
.limit(limit)
.map(transformer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,6 @@ void testLoadTasksInParallel() throws Exception {
PolarisMetaStoreManager metaStoreManager = polarisTestMetaStoreManager.polarisMetaStoreManager;
PolarisCallContext callCtx = polarisTestMetaStoreManager.polarisCallContext;
List<Future<Set<String>>> futureList = new ArrayList<>();
List<Set<String>> responses;
ExecutorService executorService = Executors.newCachedThreadPool();
try {
for (int i = 0; i < 3; i++) {
Expand All @@ -399,21 +398,21 @@ void testLoadTasksInParallel() throws Exception {
return taskNames;
}));
}
responses =
futureList.stream()
.map(
f -> {
try {
return f.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toList());
} finally {
executorService.shutdown();
Assertions.assertThat(executorService.awaitTermination(10, TimeUnit.MINUTES)).isTrue();
Assertions.assertThat(executorService.awaitTermination(30, TimeUnit.SECONDS)).isTrue();
}
List<Set<String>> responses =
futureList.stream()
.map(
f -> {
try {
return f.get(30, TimeUnit.SECONDS);
} catch (Exception e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toList());
Assertions.assertThat(responses)
.hasSize(3)
.satisfies(l -> Assertions.assertThat(l.stream().flatMap(Set::stream)).hasSize(100));
Expand Down