vldpyatkov commented on code in PR #1726: URL: https://github.com/apache/ignite-3/pull/1726#discussion_r1130612963
########## modules/placement-driver/src/main/java/org/apache/ignite/internal/placementdriver/LeaseTracker.java: ########## @@ -0,0 +1,149 @@ +/* + * 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.placementdriver; + +import static org.apache.ignite.internal.placementdriver.PlacementDriverManager.PLACEMENTDRIVER_PREFIX; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.ignite.internal.logger.IgniteLogger; +import org.apache.ignite.internal.logger.Loggers; +import org.apache.ignite.internal.metastorage.Entry; +import org.apache.ignite.internal.metastorage.EntryEvent; +import org.apache.ignite.internal.metastorage.MetaStorageManager; +import org.apache.ignite.internal.metastorage.WatchEvent; +import org.apache.ignite.internal.metastorage.WatchListener; +import org.apache.ignite.internal.replicator.ReplicationGroupId; +import org.apache.ignite.internal.table.distributed.replicator.TablePartitionId; +import org.apache.ignite.internal.util.ByteUtils; +import org.apache.ignite.internal.util.Cursor; +import org.apache.ignite.internal.vault.VaultEntry; +import org.apache.ignite.internal.vault.VaultManager; +import org.apache.ignite.lang.ByteArray; + +/** + * Class tracks cluster leases in memory. + * At first, the class state recoveries from Vault, then updates on watch's listener. + */ +public class LeaseTracker { + /** Ignite logger. */ + private static final IgniteLogger LOG = Loggers.forClass(LeaseTracker.class); + + /** Vault manager. */ + private final VaultManager vaultManager; + + /** Metastorage manager. */ + private final MetaStorageManager msManager; + + /** Leases cache. */ + private final Map<ReplicationGroupId, Lease> leases; + + /** Listener to update a leases cache. */ + private final UpdateListener updateListener = new UpdateListener(); + + /** + * The constructor. + * + * @param vaultManager Vault manager. + * @param msManager Metastorage manager. + */ + public LeaseTracker(VaultManager vaultManager, MetaStorageManager msManager) { + this.vaultManager = vaultManager; + this.msManager = msManager; + + this.leases = new ConcurrentHashMap<>(); + } + + /** + * Recoveries state from Vault and subscribers on further updates. + */ + public void startTrack() { + msManager.registerPrefixWatch(ByteArray.fromString(PLACEMENTDRIVER_PREFIX), updateListener); + + try (Cursor<VaultEntry> cursor = vaultManager.range( + ByteArray.fromString(PLACEMENTDRIVER_PREFIX), + ByteArray.fromString(incrementLastChar(PLACEMENTDRIVER_PREFIX)) + )) { + for (VaultEntry entry : cursor) { + String key = entry.key().toString(); + + key = key.replace(PLACEMENTDRIVER_PREFIX, ""); + + TablePartitionId grpId = TablePartitionId.fromString(key); + Lease lease = ByteUtils.fromBytes(entry.value()); + + leases.put(grpId, lease); + } + } + + LOG.info("Leases cache recovered [leases={}]", leases); + } + + /** + * Stops the tracker. + */ + public void stopTrack() { + msManager.unregisterWatch(updateListener); + } + + /** + * Get a lease for a particular group. + * + * @param grpId Replication group id. + * @return A lease is associated with the group. + */ + public Lease getLease(ReplicationGroupId grpId) { + return leases.getOrDefault(grpId, new Lease()); Review Comment: I can add @NotNull annotation, if it will more expected. -- 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]
