jerryshao commented on code in PR #14023:
URL: https://github.com/apache/gravitino/pull/14023#discussion_r4345426119


##########
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:
   Fixed in 4dde54d27c. The listener now also handles `EnableMetalakeEvent` and 
reconciles the templates through the same id-based path (reconciling is 
idempotent). The queued registration checks that the metalake is in use while 
holding its read lock (disable and enable take its write lock), and skips it 
with an info log if it is disabled. 
`testRegistrationSkipsDisabledMetalakeAndRetriesOnEnable` blocks the load, 
disables the metalake, drains the task, re-enables it, and checks the template 
is registered once; it fails against the previous head.
   
   _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_



-- 
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]

Reply via email to