0N/A/*
2362N/A * Copyright (c) 1996, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.rmi.transport;
0N/A
0N/Aimport java.lang.ref.PhantomReference;
0N/Aimport java.lang.ref.ReferenceQueue;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.HashSet;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.List;
0N/Aimport java.util.Map;
0N/Aimport java.util.Set;
0N/Aimport java.rmi.ConnectException;
0N/Aimport java.rmi.RemoteException;
0N/Aimport java.rmi.dgc.DGC;
0N/Aimport java.rmi.dgc.Lease;
0N/Aimport java.rmi.dgc.VMID;
0N/Aimport java.rmi.server.ObjID;
0N/Aimport sun.misc.GC;
0N/Aimport sun.rmi.runtime.NewThreadAction;
0N/Aimport sun.rmi.server.UnicastRef;
0N/Aimport sun.rmi.server.Util;
0N/Aimport sun.security.action.GetLongAction;
0N/A
0N/A/**
0N/A * DGCClient implements the client-side of the RMI distributed garbage
0N/A * collection system.
0N/A *
0N/A * The external interface to DGCClient is the "registerRefs" method.
0N/A * When a LiveRef to a remote object enters the VM, it needs to be
0N/A * registered with the DGCClient to participate in distributed garbage
0N/A * collection.
0N/A *
0N/A * When the first LiveRef to a particular remote object is registered,
0N/A * a "dirty" call is made to the server-side distributed garbage
0N/A * collector for the remote object, which returns a lease guaranteeing
0N/A * that the server-side DGC will not collect the remote object for a
0N/A * certain period of time. While LiveRef instances to remote objects
0N/A * on a particular server exist, the DGCClient periodically sends more
0N/A * "dirty" calls to renew its lease.
0N/A *
0N/A * The DGCClient tracks the local reachability of registered LiveRef
0N/A * instances (using phantom references). When the LiveRef instance
0N/A * for a particular remote object becomes garbage collected locally,
0N/A * a "clean" call is made to the server-side distributed garbage
0N/A * collector, indicating that the server no longer needs to keep the
0N/A * remote object alive for this client.
0N/A *
0N/A * @see java.rmi.dgc.DGC, sun.rmi.transport.DGCImpl
0N/A *
0N/A * @author Ann Wollrath
0N/A * @author Peter Jones
0N/A */
0N/Afinal class DGCClient {
0N/A
0N/A /** next sequence number for DGC calls (access synchronized on class) */
0N/A private static long nextSequenceNum = Long.MIN_VALUE;
0N/A
0N/A /** unique identifier for this VM as a client of DGC */
0N/A private static VMID vmid = new VMID();
0N/A
0N/A /** lease duration to request (usually ignored by server) */
0N/A private static final long leaseValue = // default 10 minutes
28N/A AccessController.doPrivileged(
0N/A new GetLongAction("java.rmi.dgc.leaseValue",
28N/A 600000)).longValue();
0N/A
0N/A /** maximum interval between retries of failed clean calls */
0N/A private static final long cleanInterval = // default 3 minutes
28N/A AccessController.doPrivileged(
0N/A new GetLongAction("sun.rmi.dgc.cleanInterval",
28N/A 180000)).longValue();
0N/A
0N/A /** maximum interval between complete garbage collections of local heap */
0N/A private static final long gcInterval = // default 1 hour
28N/A AccessController.doPrivileged(
0N/A new GetLongAction("sun.rmi.dgc.client.gcInterval",
28N/A 3600000)).longValue();
0N/A
0N/A /** minimum retry count for dirty calls that fail */
0N/A private static final int dirtyFailureRetries = 5;
0N/A
0N/A /** retry count for clean calls that fail with ConnectException */
0N/A private static final int cleanFailureRetries = 5;
0N/A
0N/A /** constant empty ObjID array for lease renewal optimization */
0N/A private static final ObjID[] emptyObjIDArray = new ObjID[0];
0N/A
0N/A /** ObjID for server-side DGC object */
0N/A private static final ObjID dgcID = new ObjID(ObjID.DGC_ID);
0N/A
0N/A /*
0N/A * Disallow anyone from creating one of these.
0N/A */
0N/A private DGCClient() {}
0N/A
0N/A /**
0N/A * Register the LiveRef instances in the supplied list to participate
0N/A * in distributed garbage collection.
0N/A *
0N/A * All of the LiveRefs in the list must be for remote objects at the
0N/A * given endpoint.
0N/A */
5559N/A static void registerRefs(Endpoint ep, List<LiveRef> refs) {
0N/A /*
0N/A * Look up the given endpoint and register the refs with it.
0N/A * The retrieved entry may get removed from the global endpoint
0N/A * table before EndpointEntry.registerRefs() is able to acquire
0N/A * its lock; in this event, it returns false, and we loop and
0N/A * try again.
0N/A */
0N/A EndpointEntry epEntry;
0N/A do {
0N/A epEntry = EndpointEntry.lookup(ep);
0N/A } while (!epEntry.registerRefs(refs));
0N/A }
0N/A
0N/A /**
0N/A * Get the next sequence number to be used for a dirty or clean
0N/A * operation from this VM. This method should only be called while
0N/A * synchronized on the EndpointEntry whose data structures the
0N/A * operation affects.
0N/A */
0N/A private static synchronized long getNextSequenceNum() {
0N/A return nextSequenceNum++;
0N/A }
0N/A
0N/A /**
0N/A * Given the length of a lease and the time that it was granted,
0N/A * compute the absolute time at which it should be renewed, giving
0N/A * room for reasonable computational and communication delays.
0N/A */
0N/A private static long computeRenewTime(long grantTime, long duration) {
0N/A /*
0N/A * REMIND: This algorithm should be more sophisticated, waiting
0N/A * a longer fraction of the lease duration for longer leases.
0N/A */
0N/A return grantTime + (duration / 2);
0N/A }
0N/A
0N/A /**
0N/A * EndpointEntry encapsulates the client-side DGC information specific
0N/A * to a particular Endpoint. Of most significance is the table that
0N/A * maps LiveRef value to RefEntry objects and the renew/clean thread
0N/A * that handles asynchronous client-side DGC operations.
0N/A */
0N/A private static class EndpointEntry {
0N/A
0N/A /** the endpoint that this entry is for */
0N/A private Endpoint endpoint;
0N/A /** synthesized reference to the remote server-side DGC */
0N/A private DGC dgc;
0N/A
0N/A /** table of refs held for endpoint: maps LiveRef to RefEntry */
5559N/A private Map<LiveRef, RefEntry> refTable = new HashMap<>(5);
0N/A /** set of RefEntry instances from last (failed) dirty call */
5559N/A private Set<RefEntry> invalidRefs = new HashSet<>(5);
0N/A
0N/A /** true if this entry has been removed from the global table */
0N/A private boolean removed = false;
0N/A
0N/A /** absolute time to renew current lease to this endpoint */
0N/A private long renewTime = Long.MAX_VALUE;
0N/A /** absolute time current lease to this endpoint will expire */
0N/A private long expirationTime = Long.MIN_VALUE;
0N/A /** count of recent dirty calls that have failed */
0N/A private int dirtyFailures = 0;
0N/A /** absolute time of first recent failed dirty call */
0N/A private long dirtyFailureStartTime;
0N/A /** (average) elapsed time for recent failed dirty calls */
0N/A private long dirtyFailureDuration;
0N/A
0N/A /** renew/clean thread for handling lease renewals and clean calls */
0N/A private Thread renewCleanThread;
0N/A /** true if renew/clean thread may be interrupted */
0N/A private boolean interruptible = false;
0N/A
0N/A /** reference queue for phantom references */
5559N/A private ReferenceQueue<LiveRef> refQueue = new ReferenceQueue<>();
0N/A /** set of clean calls that need to be made */
5559N/A private Set<CleanRequest> pendingCleans = new HashSet<>(5);
0N/A
0N/A /** global endpoint table: maps Endpoint to EndpointEntry */
5559N/A private static Map<Endpoint,EndpointEntry> endpointTable = new HashMap<>(5);
0N/A /** handle for GC latency request (for future cancellation) */
0N/A private static GC.LatencyRequest gcLatencyRequest = null;
0N/A
0N/A /**
0N/A * Look up the EndpointEntry for the given Endpoint. An entry is
0N/A * created if one does not already exist.
0N/A */
0N/A public static EndpointEntry lookup(Endpoint ep) {
0N/A synchronized (endpointTable) {
5559N/A EndpointEntry entry = endpointTable.get(ep);
0N/A if (entry == null) {
0N/A entry = new EndpointEntry(ep);
0N/A endpointTable.put(ep, entry);
0N/A /*
0N/A * While we are tracking live remote references registered
0N/A * in this VM, request a maximum latency for inspecting the
0N/A * entire heap from the local garbage collector, to place
0N/A * an upper bound on the time to discover remote references
0N/A * that have become unreachable (see bugid 4171278).
0N/A */
0N/A if (gcLatencyRequest == null) {
0N/A gcLatencyRequest = GC.requestLatency(gcInterval);
0N/A }
0N/A }
0N/A return entry;
0N/A }
0N/A }
0N/A
0N/A private EndpointEntry(final Endpoint endpoint) {
0N/A this.endpoint = endpoint;
0N/A try {
0N/A LiveRef dgcRef = new LiveRef(dgcID, endpoint, false);
0N/A dgc = (DGC) Util.createProxy(DGCImpl.class,
0N/A new UnicastRef(dgcRef), true);
0N/A } catch (RemoteException e) {
0N/A throw new Error("internal error creating DGC stub");
0N/A }
28N/A renewCleanThread = AccessController.doPrivileged(
0N/A new NewThreadAction(new RenewCleanThread(),
0N/A "RenewClean-" + endpoint, true));
0N/A renewCleanThread.start();
0N/A }
0N/A
0N/A /**
0N/A * Register the LiveRef instances in the supplied list to participate
0N/A * in distributed garbage collection.
0N/A *
0N/A * This method returns false if this entry was removed from the
0N/A * global endpoint table (because it was empty) before these refs
0N/A * could be registered. In that case, a new EndpointEntry needs
0N/A * to be looked up.
0N/A *
0N/A * This method must NOT be called while synchronized on this entry.
0N/A */
5559N/A public boolean registerRefs(List<LiveRef> refs) {
0N/A assert !Thread.holdsLock(this);
0N/A
5559N/A Set<RefEntry> refsToDirty = null; // entries for refs needing dirty
0N/A long sequenceNum; // sequence number for dirty call
0N/A
0N/A synchronized (this) {
0N/A if (removed) {
0N/A return false;
0N/A }
0N/A
5559N/A Iterator<LiveRef> iter = refs.iterator();
0N/A while (iter.hasNext()) {
5559N/A LiveRef ref = iter.next();
0N/A assert ref.getEndpoint().equals(endpoint);
0N/A
5559N/A RefEntry refEntry = refTable.get(ref);
0N/A if (refEntry == null) {
0N/A LiveRef refClone = (LiveRef) ref.clone();
0N/A refEntry = new RefEntry(refClone);
0N/A refTable.put(refClone, refEntry);
0N/A if (refsToDirty == null) {
5559N/A refsToDirty = new HashSet<>(5);
0N/A }
0N/A refsToDirty.add(refEntry);
0N/A }
0N/A
0N/A refEntry.addInstanceToRefSet(ref);
0N/A }
0N/A
0N/A if (refsToDirty == null) {
0N/A return true;
0N/A }
0N/A
0N/A refsToDirty.addAll(invalidRefs);
0N/A invalidRefs.clear();
0N/A
0N/A sequenceNum = getNextSequenceNum();
0N/A }
0N/A
0N/A makeDirtyCall(refsToDirty, sequenceNum);
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Remove the given RefEntry from the ref table. If that makes
0N/A * the ref table empty, remove this entry from the global endpoint
0N/A * table.
0N/A *
0N/A * This method must ONLY be called while synchronized on this entry.
0N/A */
0N/A private void removeRefEntry(RefEntry refEntry) {
0N/A assert Thread.holdsLock(this);
0N/A assert !removed;
0N/A assert refTable.containsKey(refEntry.getRef());
0N/A
0N/A refTable.remove(refEntry.getRef());
0N/A invalidRefs.remove(refEntry);
0N/A if (refTable.isEmpty()) {
0N/A synchronized (endpointTable) {
0N/A endpointTable.remove(endpoint);
0N/A Transport transport = endpoint.getOutboundTransport();
0N/A transport.free(endpoint);
0N/A /*
0N/A * If there are no longer any live remote references
0N/A * registered, we are no longer concerned with the
0N/A * latency of local garbage collection here.
0N/A */
0N/A if (endpointTable.isEmpty()) {
0N/A assert gcLatencyRequest != null;
0N/A gcLatencyRequest.cancel();
0N/A gcLatencyRequest = null;
0N/A }
0N/A removed = true;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Make a DGC dirty call to this entry's endpoint, for the ObjIDs
0N/A * corresponding to the given set of refs and with the given
0N/A * sequence number.
0N/A *
0N/A * This method must NOT be called while synchronized on this entry.
0N/A */
5559N/A private void makeDirtyCall(Set<RefEntry> refEntries, long sequenceNum) {
0N/A assert !Thread.holdsLock(this);
0N/A
0N/A ObjID[] ids;
0N/A if (refEntries != null) {
0N/A ids = createObjIDArray(refEntries);
0N/A } else {
0N/A ids = emptyObjIDArray;
0N/A }
0N/A
0N/A long startTime = System.currentTimeMillis();
0N/A try {
0N/A Lease lease =
0N/A dgc.dirty(ids, sequenceNum, new Lease(vmid, leaseValue));
0N/A long duration = lease.getValue();
0N/A
0N/A long newRenewTime = computeRenewTime(startTime, duration);
0N/A long newExpirationTime = startTime + duration;
0N/A
0N/A synchronized (this) {
0N/A dirtyFailures = 0;
0N/A setRenewTime(newRenewTime);
0N/A expirationTime = newExpirationTime;
0N/A }
0N/A
0N/A } catch (Exception e) {
0N/A long endTime = System.currentTimeMillis();
0N/A
0N/A synchronized (this) {
0N/A dirtyFailures++;
0N/A
0N/A if (dirtyFailures == 1) {
0N/A /*
0N/A * If this was the first recent failed dirty call,
0N/A * reschedule another one immediately, in case there
0N/A * was just a transient network problem, and remember
0N/A * the start time and duration of this attempt for
0N/A * future calculations of the delays between retries.
0N/A */
0N/A dirtyFailureStartTime = startTime;
0N/A dirtyFailureDuration = endTime - startTime;
0N/A setRenewTime(endTime);
0N/A } else {
0N/A /*
0N/A * For each successive failed dirty call, wait for a
0N/A * (binary) exponentially increasing delay before
0N/A * retrying, to avoid network congestion.
0N/A */
0N/A int n = dirtyFailures - 2;
0N/A if (n == 0) {
0N/A /*
0N/A * Calculate the initial retry delay from the
0N/A * average time elapsed for each of the first
0N/A * two failed dirty calls. The result must be
0N/A * at least 1000ms, to prevent a tight loop.
0N/A */
0N/A dirtyFailureDuration =
0N/A Math.max((dirtyFailureDuration +
0N/A (endTime - startTime)) >> 1, 1000);
0N/A }
0N/A long newRenewTime =
0N/A endTime + (dirtyFailureDuration << n);
0N/A
0N/A /*
0N/A * Continue if the last known held lease has not
0N/A * expired, or else at least a fixed number of times,
0N/A * or at least until we've tried for a fixed amount
0N/A * of time (the default lease value we request).
0N/A */
0N/A if (newRenewTime < expirationTime ||
0N/A dirtyFailures < dirtyFailureRetries ||
0N/A newRenewTime < dirtyFailureStartTime + leaseValue)
0N/A {
0N/A setRenewTime(newRenewTime);
0N/A } else {
0N/A /*
0N/A * Give up: postpone lease renewals until next
0N/A * ref is registered for this endpoint.
0N/A */
0N/A setRenewTime(Long.MAX_VALUE);
0N/A }
0N/A }
0N/A
0N/A if (refEntries != null) {
0N/A /*
0N/A * Add all of these refs to the set of refs for this
0N/A * endpoint that may be invalid (this VM may not be in
0N/A * the server's referenced set), so that we will
0N/A * attempt to explicitly dirty them again in the
0N/A * future.
0N/A */
0N/A invalidRefs.addAll(refEntries);
0N/A
0N/A /*
0N/A * Record that a dirty call has failed for all of these
0N/A * refs, so that clean calls for them in the future
0N/A * will be strong.
0N/A */
5559N/A Iterator<RefEntry> iter = refEntries.iterator();
0N/A while (iter.hasNext()) {
5559N/A RefEntry refEntry = iter.next();
0N/A refEntry.markDirtyFailed();
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * If the last known held lease will have expired before
0N/A * the next renewal, all refs might be invalid.
0N/A */
0N/A if (renewTime >= expirationTime) {
0N/A invalidRefs.addAll(refTable.values());
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Set the absolute time at which the lease for this entry should
0N/A * be renewed.
0N/A *
0N/A * This method must ONLY be called while synchronized on this entry.
0N/A */
0N/A private void setRenewTime(long newRenewTime) {
0N/A assert Thread.holdsLock(this);
0N/A
0N/A if (newRenewTime < renewTime) {
0N/A renewTime = newRenewTime;
0N/A if (interruptible) {
28N/A AccessController.doPrivileged(
28N/A new PrivilegedAction<Void>() {
28N/A public Void run() {
0N/A renewCleanThread.interrupt();
0N/A return null;
0N/A }
0N/A });
0N/A }
0N/A } else {
0N/A renewTime = newRenewTime;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * RenewCleanThread handles the asynchronous client-side DGC activity
0N/A * for this entry: renewing the leases and making clean calls.
0N/A */
0N/A private class RenewCleanThread implements Runnable {
0N/A
0N/A public void run() {
0N/A do {
0N/A long timeToWait;
0N/A RefEntry.PhantomLiveRef phantom = null;
0N/A boolean needRenewal = false;
5559N/A Set<RefEntry> refsToDirty = null;
0N/A long sequenceNum = Long.MIN_VALUE;
0N/A
0N/A synchronized (EndpointEntry.this) {
0N/A /*
0N/A * Calculate time to block (waiting for phantom
0N/A * reference notifications). It is the time until the
0N/A * lease renewal should be done, bounded on the low
0N/A * end by 1 ms so that the reference queue will always
0N/A * get processed, and if there are pending clean
0N/A * requests (remaining because some clean calls
0N/A * failed), bounded on the high end by the maximum
0N/A * clean call retry interval.
0N/A */
0N/A long timeUntilRenew =
0N/A renewTime - System.currentTimeMillis();
0N/A timeToWait = Math.max(timeUntilRenew, 1);
0N/A if (!pendingCleans.isEmpty()) {
0N/A timeToWait = Math.min(timeToWait, cleanInterval);
0N/A }
0N/A
0N/A /*
0N/A * Set flag indicating that it is OK to interrupt this
0N/A * thread now, such as if a earlier lease renewal time
0N/A * is set, because we are only going to be blocking
0N/A * and can deal with interrupts.
0N/A */
0N/A interruptible = true;
0N/A }
0N/A
0N/A try {
0N/A /*
0N/A * Wait for the duration calculated above for any of
0N/A * our phantom references to be enqueued.
0N/A */
0N/A phantom = (RefEntry.PhantomLiveRef)
0N/A refQueue.remove(timeToWait);
0N/A } catch (InterruptedException e) {
0N/A }
0N/A
0N/A synchronized (EndpointEntry.this) {
0N/A /*
0N/A * Set flag indicating that it is NOT OK to interrupt
0N/A * this thread now, because we may be undertaking I/O
0N/A * operations that should not be interrupted (and we
0N/A * will not be blocking arbitrarily).
0N/A */
0N/A interruptible = false;
0N/A Thread.interrupted(); // clear interrupted state
0N/A
0N/A /*
0N/A * If there was a phantom reference enqueued, process
0N/A * it and all the rest on the queue, generating
0N/A * clean requests as necessary.
0N/A */
0N/A if (phantom != null) {
0N/A processPhantomRefs(phantom);
0N/A }
0N/A
0N/A /*
0N/A * Check if it is time to renew this entry's lease.
0N/A */
0N/A long currentTime = System.currentTimeMillis();
0N/A if (currentTime > renewTime) {
0N/A needRenewal = true;
0N/A if (!invalidRefs.isEmpty()) {
0N/A refsToDirty = invalidRefs;
5559N/A invalidRefs = new HashSet<>(5);
0N/A }
0N/A sequenceNum = getNextSequenceNum();
0N/A }
0N/A }
0N/A
0N/A if (needRenewal) {
0N/A makeDirtyCall(refsToDirty, sequenceNum);
0N/A }
0N/A
0N/A if (!pendingCleans.isEmpty()) {
0N/A makeCleanCalls();
0N/A }
0N/A } while (!removed || !pendingCleans.isEmpty());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Process the notification of the given phantom reference and any
0N/A * others that are on this entry's reference queue. Each phantom
0N/A * reference is removed from its RefEntry's ref set. All ref
0N/A * entries that have no more registered instances are collected
0N/A * into up to two batched clean call requests: one for refs
0N/A * requiring a "strong" clean call, and one for the rest.
0N/A *
0N/A * This method must ONLY be called while synchronized on this entry.
0N/A */
0N/A private void processPhantomRefs(RefEntry.PhantomLiveRef phantom) {
0N/A assert Thread.holdsLock(this);
0N/A
5559N/A Set<RefEntry> strongCleans = null;
5559N/A Set<RefEntry> normalCleans = null;
0N/A
0N/A do {
0N/A RefEntry refEntry = phantom.getRefEntry();
0N/A refEntry.removeInstanceFromRefSet(phantom);
0N/A if (refEntry.isRefSetEmpty()) {
0N/A if (refEntry.hasDirtyFailed()) {
0N/A if (strongCleans == null) {
5559N/A strongCleans = new HashSet<>(5);
0N/A }
0N/A strongCleans.add(refEntry);
0N/A } else {
0N/A if (normalCleans == null) {
5559N/A normalCleans = new HashSet<>(5);
0N/A }
0N/A normalCleans.add(refEntry);
0N/A }
0N/A removeRefEntry(refEntry);
0N/A }
0N/A } while ((phantom =
0N/A (RefEntry.PhantomLiveRef) refQueue.poll()) != null);
0N/A
0N/A if (strongCleans != null) {
0N/A pendingCleans.add(
0N/A new CleanRequest(createObjIDArray(strongCleans),
0N/A getNextSequenceNum(), true));
0N/A }
0N/A if (normalCleans != null) {
0N/A pendingCleans.add(
0N/A new CleanRequest(createObjIDArray(normalCleans),
0N/A getNextSequenceNum(), false));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * CleanRequest holds the data for the parameters of a clean call
0N/A * that needs to be made.
0N/A */
0N/A private static class CleanRequest {
0N/A
0N/A final ObjID[] objIDs;
0N/A final long sequenceNum;
0N/A final boolean strong;
0N/A
0N/A /** how many times this request has failed */
0N/A int failures = 0;
0N/A
0N/A CleanRequest(ObjID[] objIDs, long sequenceNum, boolean strong) {
0N/A this.objIDs = objIDs;
0N/A this.sequenceNum = sequenceNum;
0N/A this.strong = strong;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Make all of the clean calls described by the clean requests in
0N/A * this entry's set of "pending cleans". Clean requests for clean
0N/A * calls that succeed are removed from the "pending cleans" set.
0N/A *
0N/A * This method must NOT be called while synchronized on this entry.
0N/A */
0N/A private void makeCleanCalls() {
0N/A assert !Thread.holdsLock(this);
0N/A
5559N/A Iterator<CleanRequest> iter = pendingCleans.iterator();
0N/A while (iter.hasNext()) {
5559N/A CleanRequest request = iter.next();
0N/A try {
0N/A dgc.clean(request.objIDs, request.sequenceNum, vmid,
0N/A request.strong);
0N/A iter.remove();
0N/A } catch (Exception e) {
0N/A /*
0N/A * Many types of exceptions here could have been
0N/A * caused by a transient failure, so try again a
0N/A * few times, but not forever.
0N/A */
0N/A if (++request.failures >= cleanFailureRetries) {
0N/A iter.remove();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Create an array of ObjIDs (needed for the DGC remote calls)
0N/A * from the ids in the given set of refs.
0N/A */
5559N/A private static ObjID[] createObjIDArray(Set<RefEntry> refEntries) {
0N/A ObjID[] ids = new ObjID[refEntries.size()];
5559N/A Iterator<RefEntry> iter = refEntries.iterator();
0N/A for (int i = 0; i < ids.length; i++) {
5559N/A ids[i] = iter.next().getRef().getObjID();
0N/A }
0N/A return ids;
0N/A }
0N/A
0N/A /**
0N/A * RefEntry encapsulates the client-side DGC information specific
0N/A * to a particular LiveRef value. In particular, it contains a
0N/A * set of phantom references to all of the instances of the LiveRef
0N/A * value registered in the system (but not garbage collected
0N/A * locally).
0N/A */
0N/A private class RefEntry {
0N/A
0N/A /** LiveRef value for this entry (not a registered instance) */
0N/A private LiveRef ref;
0N/A /** set of phantom references to registered instances */
5559N/A private Set<PhantomLiveRef> refSet = new HashSet<>(5);
0N/A /** true if a dirty call containing this ref has failed */
0N/A private boolean dirtyFailed = false;
0N/A
0N/A public RefEntry(LiveRef ref) {
0N/A this.ref = ref;
0N/A }
0N/A
0N/A /**
0N/A * Return the LiveRef value for this entry (not a registered
0N/A * instance).
0N/A */
0N/A public LiveRef getRef() {
0N/A return ref;
0N/A }
0N/A
0N/A /**
0N/A * Add a LiveRef to the set of registered instances for this entry.
0N/A *
0N/A * This method must ONLY be invoked while synchronized on this
0N/A * RefEntry's EndpointEntry.
0N/A */
0N/A public void addInstanceToRefSet(LiveRef ref) {
0N/A assert Thread.holdsLock(EndpointEntry.this);
0N/A assert ref.equals(this.ref);
0N/A
0N/A /*
0N/A * Only keep a phantom reference to the registered instance,
0N/A * so that it can be garbage collected normally (and we can be
0N/A * notified when that happens).
0N/A */
0N/A refSet.add(new PhantomLiveRef(ref));
0N/A }
0N/A
0N/A /**
0N/A * Remove a PhantomLiveRef from the set of registered instances.
0N/A *
0N/A * This method must ONLY be invoked while synchronized on this
0N/A * RefEntry's EndpointEntry.
0N/A */
0N/A public void removeInstanceFromRefSet(PhantomLiveRef phantom) {
0N/A assert Thread.holdsLock(EndpointEntry.this);
0N/A assert refSet.contains(phantom);
0N/A refSet.remove(phantom);
0N/A }
0N/A
0N/A /**
0N/A * Return true if there are no registered LiveRef instances for
0N/A * this entry still reachable in this VM.
0N/A *
0N/A * This method must ONLY be invoked while synchronized on this
0N/A * RefEntry's EndpointEntry.
0N/A */
0N/A public boolean isRefSetEmpty() {
0N/A assert Thread.holdsLock(EndpointEntry.this);
0N/A return refSet.size() == 0;
0N/A }
0N/A
0N/A /**
0N/A * Record that a dirty call that explicitly contained this
0N/A * entry's ref has failed.
0N/A *
0N/A * This method must ONLY be invoked while synchronized on this
0N/A * RefEntry's EndpointEntry.
0N/A */
0N/A public void markDirtyFailed() {
0N/A assert Thread.holdsLock(EndpointEntry.this);
0N/A dirtyFailed = true;
0N/A }
0N/A
0N/A /**
0N/A * Return true if a dirty call that explicitly contained this
0N/A * entry's ref has failed (and therefore a clean call for this
0N/A * ref needs to be marked "strong").
0N/A *
0N/A * This method must ONLY be invoked while synchronized on this
0N/A * RefEntry's EndpointEntry.
0N/A */
0N/A public boolean hasDirtyFailed() {
0N/A assert Thread.holdsLock(EndpointEntry.this);
0N/A return dirtyFailed;
0N/A }
0N/A
0N/A /**
0N/A * PhantomLiveRef is a PhantomReference to a LiveRef instance,
0N/A * used to detect when the LiveRef becomes permanently
0N/A * unreachable in this VM.
0N/A */
5559N/A private class PhantomLiveRef extends PhantomReference<LiveRef> {
0N/A
0N/A public PhantomLiveRef(LiveRef ref) {
0N/A super(ref, EndpointEntry.this.refQueue);
0N/A }
0N/A
0N/A public RefEntry getRefEntry() {
0N/A return RefEntry.this;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}