HBASE-461 Simplify leases.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/hbase/trunk@630389 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jim Kellerman 2008-02-23 04:44:11 +00:00
parent f876238940
commit c180c47e3f
2 changed files with 18 additions and 24 deletions

View File

@ -59,7 +59,8 @@ Hbase Change Log
HBASE-414 Move client classes into client package
HBASE-79 When HBase needs to be migrated, it should display a message on
stdout, not just in the logs
HBASE-461 Simplify leases.
Branch 0.1
INCOMPATIBLE CHANGES

View File

@ -50,8 +50,6 @@ public class Leases extends Thread {
private volatile DelayQueue<Lease> leaseQueue = new DelayQueue<Lease>();
protected final Map<String, Lease> leases = new HashMap<String, Lease>();
protected final Map<String, LeaseListener> listeners =
new HashMap<String, LeaseListener>();
private volatile boolean stopRequested = false;
/**
@ -84,17 +82,14 @@ public class Leases extends Thread {
continue;
}
// A lease expired
LeaseListener listener = null;
synchronized (leaseQueue) {
String leaseName = lease.getLeaseName();
leases.remove(leaseName);
listener = listeners.remove(leaseName);
if (listener == null) {
LOG.error("lease listener is null for lease " + leaseName);
leases.remove(lease.getLeaseName());
if (lease.getListener() == null) {
LOG.error("lease listener is null for lease " + lease.getLeaseName());
continue;
}
}
listener.leaseExpired();
lease.getListener().leaseExpired();
}
close();
}
@ -120,7 +115,6 @@ public class Leases extends Thread {
synchronized (leaseQueue) {
leaseQueue.clear();
leases.clear();
listeners.clear();
leaseQueue.notifyAll();
}
LOG.info(Thread.currentThread().getName() + " closed leases");
@ -136,14 +130,14 @@ public class Leases extends Thread {
if (stopRequested) {
return;
}
Lease lease = new Lease(leaseName, System.currentTimeMillis() + leasePeriod);
Lease lease = new Lease(leaseName, listener,
System.currentTimeMillis() + leasePeriod);
synchronized (leaseQueue) {
if (leases.containsKey(leaseName)) {
throw new IllegalStateException("lease '" + leaseName +
"' already exists");
}
leases.put(leaseName, lease);
listeners.put(leaseName, listener);
leaseQueue.add(lease);
}
}
@ -179,17 +173,18 @@ public class Leases extends Thread {
"' does not exist");
}
leaseQueue.remove(lease);
listeners.remove(leaseName);
}
}
/** This class tracks a single Lease. */
private static class Lease implements Delayed {
private final String leaseName;
private final LeaseListener listener;
private long expirationTime;
Lease(final String leaseName, long expirationTime) {
Lease(final String leaseName, LeaseListener listener, long expirationTime) {
this.leaseName = leaseName;
this.listener = listener;
this.expirationTime = expirationTime;
}
@ -197,6 +192,11 @@ public class Leases extends Thread {
public String getLeaseName() {
return leaseName;
}
/** @return listener */
public LeaseListener getListener() {
return this.listener;
}
/** {@inheritDoc} */
@Override
@ -219,16 +219,9 @@ public class Leases extends Thread {
/** {@inheritDoc} */
public int compareTo(Delayed o) {
long delta = this.getDelay(TimeUnit.MILLISECONDS) -
o.getDelay(TimeUnit.MILLISECONDS);
o.getDelay(TimeUnit.MILLISECONDS);
int value = 0;
if (delta > 0) {
value = 1;
} else if (delta < 0) {
value = -1;
}
return value;
return this.equals(o) ? 0 : (delta > 0 ? 1 : -1);
}
/** @param expirationTime the expirationTime to set */