mchades commented on code in PR #14023:
URL: https://github.com/apache/gravitino/pull/14023#discussion_r4345330403
##########
core/src/main/java/org/apache/gravitino/job/BuiltInJobTemplateEventListener.java:
##########
@@ -246,6 +298,71 @@ private ClassLoader createAuxlibClassLoader() {
}
}
+ private Optional<Long> loadMetalakeId(String metalakeName) {
+ NameIdentifier ident = NameIdentifierUtil.ofMetalake(metalakeName);
+ try {
+ BaseMetalake metalake =
+ TreeLockUtils.doWithTreeLock(
+ ident,
+ LockType.READ,
+ () -> entityStore.get(ident, Entity.EntityType.METALAKE,
BaseMetalake.class));
+ return Optional.of(metalake.id());
+ } catch (Exception e) {
+ LOG.warn(
+ "Skip registering built-in job templates for metalake {} since it
cannot be loaded, "
+ + "they will be registered on the next startup",
+ metalakeName,
+ e);
+ return Optional.empty();
+ }
+ }
+
+ private void registerBuiltInJobTemplates(long metalakeId, String
createdName, String creator) {
+ try {
+ Map<String, JobTemplate> builtInTemplates = loadBuiltInJobTemplates();
+ if (builtInTemplates.isEmpty()) {
+ LOG.debug("No built-in job templates to register for metalake: {}",
createdName);
+ return;
+ }
+
+ // Renaming or dropping a metalake takes the root write lock, so holding
the root read lock
+ // keeps the metalake found by id from being renamed or dropped during
the registration.
+ TreeLockUtils.doWithRootTreeLock(
+ LockType.READ,
+ () -> {
+ Optional<String> currentName = currentMetalakeName(metalakeId);
+ if (!currentName.isPresent()) {
+ LOG.info(
+ "Skip registering built-in job templates for metalake {}
since it was dropped",
+ createdName);
+ return null;
+ }
+
+ // The registration thread has no caller principal. Run as the
user who created the
+ // metalake, so the templates record that user as the creator.
+ PrincipalUtils.doAs(
+ new UserPrincipal(creator),
+ () -> {
+ reconcileBuiltInJobTemplates(currentName.get(),
builtInTemplates);
Review Comment:
[P2] Retry pending registration when a metalake is re-enabled
If a newly created metalake is disabled before the queued registration runs,
reconcileBuiltInJobTemplates fails with MetalakeNotInUseException from
JobManager.listJobTemplates. The exception is logged and the task is discarded;
EnableMetalakeEvent does not trigger reconciliation, so re-enabling leaves the
metalake without built-in templates until restart. Reproduced with the real H2
store by blocking template loading, disabling the metalake, releasing and
draining the task, then re-enabling: expected one template, got zero. Please
retain the pending registration or reconcile it when the metalake is enabled.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]