denis-chudov commented on code in PR #1871: URL: https://github.com/apache/ignite-3/pull/1871#discussion_r1165770862
########## modules/placement-driver/src/main/java/org/apache/ignite/internal/placementdriver/negotiation/LeaseAgreement.java: ########## @@ -0,0 +1,100 @@ +/* + * 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.negotiation; + +import static java.util.concurrent.CompletableFuture.completedFuture; + +import java.util.concurrent.CompletableFuture; +import org.apache.ignite.internal.placementdriver.leases.Lease; +import org.apache.ignite.internal.placementdriver.message.LeaseGrantedMessageResponse; + +/** + * The agreement is formed from {@link LeaseGrantedMessageResponse}. + */ +public class LeaseAgreement { + /** The agreement, which has not try negotiating yet. */ + public static final LeaseAgreement UNDEFINED_AGREEMENT = new LeaseAgreement(null, completedFuture(null)); + + /** Lease. */ + private final Lease lease; + + /** Future to {@link LeaseGrantedMessageResponse} response. */ + private final CompletableFuture<LeaseGrantedMessageResponse> responseFut; + + /** + * The constructor. + * + * @param lease Lease. + * @param remoteNodeResponseFuture The future of response from the remote node which is negotiating the agreement. + */ + public LeaseAgreement(Lease lease, CompletableFuture<LeaseGrantedMessageResponse> remoteNodeResponseFuture) { + this.lease = lease; + this.responseFut = remoteNodeResponseFuture; + } + + /** + * Gets a lease about which the leaseholder was notified. + * + * @return Lease. + */ + public Lease getLease() { + return lease; + } + + /** + * Gets a accepted flag. The flag is true, when the lease is accepted by leaseholder. + * + * @return Accepted flag. + */ + public boolean isAccepted() { + if (!responseFut.isDone()) { + return false; + } + + LeaseGrantedMessageResponse resp = responseFut.join(); + + if (resp != null) { + return resp.accepted(); + } + + return false; + } + + /** + * The property matches to {@link LeaseGrantedMessageResponse#redirectProposal()}. + * This property is available only when the agreement is ready (look at {@link #ready()}). + * + * @return Node id to propose a lease. + */ + public String getRedirectTo() { + assert responseFut.isDone() : "The method should invoke only the agreement is ready"; Review Comment: ```suggestion assert responseFut.isDone() : "The method should be invoked only after the agreement is ready"; ``` ########## modules/placement-driver/src/main/java/org/apache/ignite/internal/placementdriver/LeaseUpdater.java: ########## @@ -174,29 +203,27 @@ private class Updater implements Runnable { @Override public void run() { while (updaterTread != null && !updaterTread.isInterrupted()) { + HybridTimestamp now = clock.now(); + for (Map.Entry<ReplicationGroupId, Set<Assignment>> entry : assignmentsTracker.assignments().entrySet()) { ReplicationGroupId grpId = entry.getKey(); Lease lease = leaseTracker.getLease(grpId); - HybridTimestamp now = clock.now(); - - // Nothing holds the lease. - if (lease == EMPTY_LEASE - // The lease is near to expiration. - || now.getPhysical() > (lease.getLeaseExpirationTime().getPhysical() - LEASE_PERIOD / 2)) { + // The lease is expired or close to this. + if (now.getPhysical() > (lease.getExpirationTime().getPhysical() - LEASE_INTERVAL / 2)) { ClusterNode candidate = nextLeaseHolder(entry.getValue()); if (candidate == null) { continue; } - if (isReplicationGroupUpdateLeaseholder(lease, candidate)) { - updateLeaseInMetaStorage(grpId, lease, candidate); + if (isLeaseOutdated(lease)) { Review Comment: ```suggestion // We can't prolong the expired lease because we already have an interval of time when the lease was not active, // so we must start ne negotiation round from the beginning; the same we do for the groups that don't have // leaseholders at all. if (isLeaseOutdated(lease)) { ``` ########## modules/placement-driver/src/main/java/org/apache/ignite/internal/placementdriver/LeaseUpdater.java: ########## @@ -174,29 +203,27 @@ private class Updater implements Runnable { @Override public void run() { while (updaterTread != null && !updaterTread.isInterrupted()) { + HybridTimestamp now = clock.now(); + for (Map.Entry<ReplicationGroupId, Set<Assignment>> entry : assignmentsTracker.assignments().entrySet()) { ReplicationGroupId grpId = entry.getKey(); Lease lease = leaseTracker.getLease(grpId); - HybridTimestamp now = clock.now(); - - // Nothing holds the lease. - if (lease == EMPTY_LEASE - // The lease is near to expiration. - || now.getPhysical() > (lease.getLeaseExpirationTime().getPhysical() - LEASE_PERIOD / 2)) { + // The lease is expired or close to this. + if (now.getPhysical() > (lease.getExpirationTime().getPhysical() - LEASE_INTERVAL / 2)) { Review Comment: ```suggestion if (lease.getExpirationTime().getPhysical() < outdatedLeaseThreshold) { ``` ########## modules/placement-driver/src/main/java/org/apache/ignite/internal/placementdriver/LeaseUpdater.java: ########## @@ -174,29 +203,27 @@ private class Updater implements Runnable { @Override public void run() { while (updaterTread != null && !updaterTread.isInterrupted()) { + HybridTimestamp now = clock.now(); Review Comment: ```suggestion HybridTimestamp now = clock.now(); long outdatedLeaseThreshold = now.getPhysical() + LEASE_INTERVAL / 2; ``` -- 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]
