roryqi commented on code in PR #13841: URL: https://github.com/apache/gravitino/pull/13841#discussion_r4268584995
########## 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: `stopSchedulers()` is not wired into the Jersey/Jetty or `GravitinoServer.stop()` lifecycle. The db-scheduler shutdown hooks only run when the JVM terminates, so stopping a server in the same JVM closes the relational `DataSource` while these pools remain active. A later `configure()` also starts another set and overwrites this reference, leaving the old pools unreachable. Please register a container shutdown callback that stops these pools before the entity store closes, and guard repeated configuration. ########## 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: `created_at` is deliberately written and reclaimed using the database clock, but this default path compares it with the local JVM clock. In a multi-node deployment, clock skew can make a fresh claim appear expired or keep an expired claim live too long, so `shouldSkip()` disagrees with the DAO's DB-clock-based acquire/reclaim decision. Please evaluate unbound-claim TTL in the DAO using the database clock (or otherwise use one shared clock), and cover skew in a test. -- 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]
