Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HDDS-10899. Refactor Lease callbacks #6715

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@

import org.apache.hadoop.util.Time;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -49,9 +46,9 @@ public class Lease<T> {
private boolean expired;

/**
* Functions to be called in case of timeout.
* Function to be called in case of timeout.
*/
private List<Callable<Void>> callbacks;
private Callable<Void> callback;


/**
Expand All @@ -63,11 +60,7 @@ public class Lease<T> {
* Lease lifetime in milliseconds
*/
public Lease(T resource, long timeout) {
this.resource = resource;
this.leaseTimeout = new AtomicLong(timeout);
this.callbacks = Collections.synchronizedList(new ArrayList<>());
this.creationTime = Time.monotonicNow();
this.expired = false;
this(resource, timeout, null);
}

/**
Expand All @@ -81,8 +74,11 @@ public Lease(T resource, long timeout) {
* Callback registered to be triggered when lease expire
*/
public Lease(T resource, long timeout, Callable<Void> callback) {
this(resource, timeout);
callbacks.add(callback);
this.resource = resource;
this.leaseTimeout = new AtomicLong(timeout);
this.callback = callback;
this.creationTime = Time.monotonicNow();
this.expired = false;
}

/**
Expand Down Expand Up @@ -176,15 +172,15 @@ public String toString() {
*
* @return callbacks to be executed
*/
List<Callable<Void>> getCallbacks() {
return callbacks;
Callable<Void> getCallback() {
return callback;
}

/**
* Expires/Invalidates the lease.
*/
void invalidate() {
callbacks = null;
callback = null;
expired = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.Callable;

/**
Expand All @@ -33,27 +32,27 @@ public class LeaseCallbackExecutor<T> implements Runnable {
LoggerFactory.getLogger(LeaseCallbackExecutor.class);

private final T resource;
private final List<Callable<Void>> callbacks;
private final Callable<Void> callback;

/**
* Constructs LeaseCallbackExecutor instance with list of callbacks.
*
* @param resource
* The resource for which the callbacks are executed
* @param callbacks
* Callbacks to be executed by this executor
* @param callback
* Callback to be executed by this executor
*/
public LeaseCallbackExecutor(T resource, List<Callable<Void>> callbacks) {
public LeaseCallbackExecutor(T resource, Callable<Void> callback) {
this.resource = resource;
this.callbacks = callbacks;
this.callback = callback;
}

@Override
public void run() {
if (LOG.isDebugEnabled()) {
LOG.debug("Executing callbacks for lease on {}", resource);
}
for (Callable<Void> callback : callbacks) {
if (callback != null) {
try {
callback.call();
} catch (Exception e) {
Expand All @@ -62,5 +61,4 @@ public void run() {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.hadoop.ozone.lease;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -268,10 +267,10 @@ public void run() {
long remainingTime = lease.getRemainingTime();
if (remainingTime <= 0) {
//Lease has timed out
List<Callable<Void>> leaseCallbacks = lease.getCallbacks();
Callable<Void> leaseCallback = lease.getCallback();
release(resource);
executorService.execute(
new LeaseCallbackExecutor<>(resource, leaseCallbacks));
new LeaseCallbackExecutor<>(resource, leaseCallback));
} else {
sleepTime = Math.min(remainingTime, sleepTime);
}
Expand Down