SammyVimes commented on code in PR #1869: URL: https://github.com/apache/ignite-3/pull/1869#discussion_r1159372882
########## modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/server/time/ClusterTimeImpl.java: ########## @@ -0,0 +1,172 @@ +/* + * 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.ignite.internal.metastorage.server.time; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executors; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import org.apache.ignite.internal.hlc.HybridClock; +import org.apache.ignite.internal.hlc.HybridTimestamp; +import org.apache.ignite.internal.logger.IgniteLogger; +import org.apache.ignite.internal.logger.Loggers; +import org.apache.ignite.internal.metastorage.impl.MetaStorageServiceImpl; +import org.apache.ignite.internal.thread.NamedThreadFactory; +import org.apache.ignite.internal.util.IgniteSpinBusyLock; +import org.apache.ignite.internal.util.IgniteUtils; +import org.apache.ignite.internal.util.PendingComparableValuesTracker; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.TestOnly; + +/** + * Cluster time implementation with additional methods to adjust time and update safe time. + */ +public class ClusterTimeImpl implements ClusterTime { + /** The logger. */ + private static final IgniteLogger LOG = Loggers.forClass(ClusterTimeImpl.class); + + private final IgniteSpinBusyLock busyLock; + + private volatile @Nullable LeaderTimer leaderTimer; + + private final HybridClock clock; + + private final PendingComparableValuesTracker<HybridTimestamp> safeTime; + + /** + * Constructor. + * + * @param busyLock Busy lock. + * @param clock Node's hybrid clock. + */ + public ClusterTimeImpl(IgniteSpinBusyLock busyLock, HybridClock clock) { + this.busyLock = busyLock; + this.clock = clock; + this.safeTime = new PendingComparableValuesTracker<>(clock.now()); + } + + /** + * Starts sync time scheduler. + * + * @param service MetaStorage service that is used by scheduler to sync time. + */ + public void startLeaderTimer(MetaStorageServiceImpl service) { + if (!busyLock.enterBusy()) { + return; + } + + try { + assert leaderTimer == null; + + LeaderTimer newTimer = new LeaderTimer(service); + + leaderTimer = newTimer; + + newTimer.start(); + } finally { + busyLock.leaveBusy(); + } + } + + /** + * Stops sync time scheduler. + */ + public void stopLeaderTimer() { + LeaderTimer timer = leaderTimer; + + assert timer != null; + + timer.stop(); + + leaderTimer = null; + } + + @Override + public HybridTimestamp now() { + return clock.now(); + } + + @Override + public CompletableFuture<Void> waitFor(HybridTimestamp time) { + return safeTime.waitFor(time); + } + + public void updateSafeTime(HybridTimestamp ts) { + this.safeTime.update(ts); + } + + public void adjust(HybridTimestamp ts) { Review Comment: Why? We call adjusting the clock "adjust" -- 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]
