zhoujinsong commented on code in PR #3997: URL: https://github.com/apache/amoro/pull/3997#discussion_r2638880530
########## amoro-ams/src/main/java/org/apache/amoro/server/ha/DataBaseHighAvailabilityContainer.java: ########## @@ -0,0 +1,311 @@ +/* + * 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.amoro.server.ha; + +import org.apache.amoro.client.AmsServerInfo; +import org.apache.amoro.config.Configurations; +import org.apache.amoro.server.AmoroManagementConf; +import org.apache.amoro.server.persistence.HaLeaseMeta; +import org.apache.amoro.server.persistence.PersistentBase; +import org.apache.amoro.server.persistence.mapper.HaLeaseMapper; +import org.apache.amoro.utils.JacksonUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.UUID; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +/** + * High availability (HA) container backed by a Database lease (RDBMS lock). + * + * <p>Core mechanics: - fixed-rate heartbeat to acquire/renew the lease - optimistic row version + * plus expiration timestamp - leader election: first successful acquire becomes the leader; losing + * the lease demotes the node - persistence via PersistentBase and HaLeaseMetaMapper - concurrency + * guard using AtomicBoolean flags; once the lease is revoked, leadership is not re-gained + * + * <p>Reference to Hudi AnalogousZkClient (for alignment only, no System.exit): - await/signal: + * waitLeaderShip() condition wait and signal on leadership gain - heartbeat: scheduled + * acquire/renew - losing leadership: demote and countDown follower latch, no System.exit + */ +public class DataBaseHighAvailabilityContainer extends PersistentBase + implements HighAvailabilityContainer { + private static final Logger LOG = + LoggerFactory.getLogger(DataBaseHighAvailabilityContainer.class); + private static final String AMS_SERVICE = "AMS"; + private static final String TABLE_SERVICE = "TABLE_SERVICE"; + private static final String OPTIMIZING_SERVICE = "OPTIMIZING_SERVICE"; + + private final Configurations serviceConfig; + private final ScheduledExecutorService executor; + private final AtomicBoolean isLeader = new AtomicBoolean(false); + /** Prevent re-gaining leadership once the lease is lost. */ + private final AtomicBoolean leadershipRevoked = new AtomicBoolean(false); + + private volatile CountDownLatch followerLatch; + + private final Lock leaderLock = new ReentrantLock(); + private final Condition leaderCondition = leaderLock.newCondition(); + + private final String clusterName; + private final long heartbeatIntervalSeconds; // 默认 10s Review Comment: Please use English comment here. -- 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]
