lasdf1234 commented on code in PR #13841:
URL: https://github.com/apache/gravitino/pull/13841#discussion_r4268711003


##########
plugins/tms/src/main/java/org/apache/gravitino/maintenance/web/rest/feature/TableMaintenanceRESTFeature.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.maintenance.web.rest.feature;
+
+import java.util.concurrent.atomic.AtomicReference;
+import javax.sql.DataSource;
+import javax.ws.rs.core.Feature;
+import javax.ws.rs.core.FeatureContext;
+import javax.ws.rs.ext.Provider;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.Configs;
+import org.apache.gravitino.GravitinoEnv;
+import org.apache.gravitino.maintenance.TableMaintenanceConfig;
+import 
org.apache.gravitino.maintenance.scheduler.TableMaintenanceSchedulerPools;
+import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Jersey feature that starts table maintenance db-scheduler pools when the 
extension package is
+ * enabled (§7.1).
+ *
+ * <p>TMS does not register public REST resources in this change; the feature 
only owns server
+ * lifecycle for in-process scheduling.
+ */
+@Provider
+public class TableMaintenanceRESTFeature implements Feature {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(TableMaintenanceRESTFeature.class);
+
+  /** Package name for {@link Configs#REST_API_EXTENSION_PACKAGES}. */
+  public static final String EXTENSION_PACKAGE = 
TableMaintenanceRESTFeature.class.getPackageName();
+
+  private static final AtomicReference<TableMaintenanceSchedulerPools> 
SCHEDULER_POOLS =
+      new AtomicReference<>();
+
+  @Override
+  public boolean configure(FeatureContext context) {
+    Config config = GravitinoEnv.getInstance().config();
+    if 
(!Configs.RELATIONAL_ENTITY_STORE.equals(config.get(Configs.ENTITY_STORE))) {
+      throw new IllegalStateException(
+          "Table maintenance requires gravitino.entity.store = relational");
+    }
+
+    DataSource dataSource = resolveEntityStoreDataSource();
+    TableMaintenanceSchedulerPools pools = new 
TableMaintenanceSchedulerPools();
+    pools.start(dataSource, TableMaintenanceConfig.from(config));
+    SCHEDULER_POOLS.set(pools);

Review Comment:
   Addressed in ef8ec42ec:
   
   - Registered a Jersey `ApplicationEventListener` that calls 
`stopSchedulers()` on `DESTROY_FINISHED` (runs during Jetty stop, before 
`GravitinoEnv` closes the entity-store DataSource).
   - Guarded repeated `configure()` so we do not overwrite a live pool 
reference / leak the previous pools.



##########
plugins/tms/src/main/java/org/apache/gravitino/maintenance/occupancy/OccupancyGate.java:
##########
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.maintenance.occupancy;
+
+import com.google.common.base.Preconditions;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Set;
+import javax.annotation.Nullable;
+import org.apache.gravitino.exceptions.NoSuchEntityException;
+import org.apache.gravitino.job.JobHandle;
+import org.apache.gravitino.maintenance.TableMaintenanceConfig;
+import org.apache.gravitino.maintenance.storage.po.TableMaintenanceActionPO;
+import 
org.apache.gravitino.maintenance.storage.service.TableMaintenanceActionMetaService;
+import org.apache.gravitino.meta.JobEntity;
+import org.apache.gravitino.storage.relational.mapper.MetalakeMetaMapper;
+import org.apache.gravitino.storage.relational.mapper.TableMetaMapper;
+import org.apache.gravitino.storage.relational.po.MetalakePO;
+import org.apache.gravitino.storage.relational.po.TablePO;
+import org.apache.gravitino.storage.relational.service.JobMetaService;
+import org.apache.gravitino.storage.relational.utils.SessionUtils;
+import org.apache.gravitino.utils.NameIdentifierUtil;
+
+/**
+ * Expand / submit occupancy checks against {@code table_maintenance_action} 
(§5.2).
+ *
+ * <p>Unbound rows ({@code job_id} null, {@code finished_at = 0}) are a short 
claim: occupied while
+ * {@code now - created_at < claimTimeoutMs}. Bound rows consult {@link 
JobMetaService} and do
+ * <strong>not</strong> backfill {@code finished_at}.
+ */
+public final class OccupancyGate {
+
+  private static final Set<JobHandle.Status> ACTIVE =
+      EnumSet.of(JobHandle.Status.QUEUED, JobHandle.Status.STARTED, 
JobHandle.Status.CANCELLING);
+
+  private final TableMaintenanceActionMetaService actionMetaService;
+  private final long claimTimeoutMs;
+
+  /**
+   * @param actionMetaService maintenance action DAO
+   * @param claimTimeoutMs unbound claim TTL
+   */
+  public OccupancyGate(TableMaintenanceActionMetaService actionMetaService, 
long claimTimeoutMs) {
+    Preconditions.checkArgument(claimTimeoutMs > 0, "claimTimeoutMs must be 
positive");
+    this.actionMetaService = Preconditions.checkNotNull(actionMetaService);
+    this.claimTimeoutMs = claimTimeoutMs;
+  }
+
+  /**
+   * @param actionMetaService maintenance action DAO
+   */
+  public OccupancyGate(TableMaintenanceActionMetaService actionMetaService) {
+    this(actionMetaService, TableMaintenanceConfig.DEFAULT_CLAIM_TIMEOUT_MS);
+  }
+
+  /** Uses {@link TableMaintenanceActionMetaService#getInstance()} and default 
claim timeout. */
+  public OccupancyGate() {
+    this(TableMaintenanceActionMetaService.getInstance());
+  }
+
+  /**
+   * @param tableId table entity id
+   * @param policyId policy entity id
+   * @return true when any open row is a live unbound claim or an active 
Gravitino job
+   */
+  public boolean isInFlight(long tableId, long policyId) {
+    return isInFlight(tableId, policyId, System.currentTimeMillis());

Review Comment:
   Addressed in ef8ec42ec:
   
   - Unbound TTL is now evaluated in the DAO via `hasLiveUnboundClaim` / 
`selectLiveUnboundByTableAndPolicy` against the database clock (same timeline 
as acquire/reclaim).
   - `OccupancyGate.isInFlight` no longer subtracts JVM `now` from `created_at`.
   - Added `testExpiredUnboundClaimIsNotInFlightOnDbClock` so gate occupancy 
agrees with DB-clock expiry / reclaim.



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