0N/A/*
2362N/A * Copyright (c) 1996, 2005, 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.rmi.Remote;
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.LogStream;
0N/Aimport java.rmi.server.ObjID;
0N/Aimport java.rmi.server.RemoteServer;
0N/Aimport java.rmi.server.ServerNotActiveException;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.HashSet;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.List;
0N/Aimport java.util.Map;
0N/Aimport java.util.Set;
0N/Aimport java.util.concurrent.Future;
0N/Aimport java.util.concurrent.ScheduledExecutorService;
0N/Aimport java.util.concurrent.TimeUnit;
0N/Aimport sun.rmi.runtime.Log;
0N/Aimport sun.rmi.runtime.RuntimeUtil;
0N/Aimport sun.rmi.server.UnicastRef;
0N/Aimport sun.rmi.server.UnicastServerRef;
0N/Aimport sun.rmi.server.Util;
0N/Aimport sun.security.action.GetLongAction;
0N/Aimport sun.security.action.GetPropertyAction;
0N/A
0N/A/**
0N/A * This class implements the guts of the server-side distributed GC
0N/A * algorithm
0N/A *
0N/A * @author Ann Wollrath
0N/A */
0N/Afinal class DGCImpl implements DGC {
0N/A
0N/A /* dgc system log */
0N/A static final Log dgcLog = Log.getLog("sun.rmi.dgc", "dgc",
0N/A LogStream.parseLevel(AccessController.doPrivileged(
0N/A new GetPropertyAction("sun.rmi.dgc.logLevel"))));
0N/A
0N/A /** lease duration to grant to clients */
0N/A private static final long leaseValue = // default 10 minutes
0N/A AccessController.doPrivileged(
0N/A new GetLongAction("java.rmi.dgc.leaseValue", 600000));
0N/A
0N/A /** lease check interval; default is half of lease grant duration */
0N/A private static final long leaseCheckInterval =
0N/A AccessController.doPrivileged(
0N/A new GetLongAction("sun.rmi.dgc.checkInterval", leaseValue / 2));
0N/A
0N/A /** thread pool for scheduling delayed tasks */
0N/A private static final ScheduledExecutorService scheduler =
0N/A AccessController.doPrivileged(
0N/A new RuntimeUtil.GetInstanceAction()).getScheduler();
0N/A
0N/A /** remote implementation of DGC interface for this VM */
0N/A private static DGCImpl dgc;
0N/A /** table that maps VMID to LeaseInfo */
5559N/A private Map<VMID,LeaseInfo> leaseTable = new HashMap<>();
0N/A /** checks for lease expiration */
0N/A private Future<?> checker = null;
0N/A
0N/A /**
0N/A * Return the remote implementation of the DGC interface for
0N/A * this VM.
0N/A */
0N/A static DGCImpl getDGCImpl() {
0N/A return dgc;
0N/A }
0N/A
0N/A /**
0N/A * Construct a new server-side remote object collector at
0N/A * a particular port. Disallow construction from outside.
0N/A */
0N/A private DGCImpl() {}
0N/A
0N/A /**
0N/A * The dirty call adds the VMID "vmid" to the set of clients
0N/A * that hold references to the object associated with the ObjID
0N/A * id. The long "sequenceNum" is used to detect late dirty calls. If
0N/A * the VMID "vmid" is null, a VMID will be generated on the
0N/A * server (for use by the client in subsequent calls) and
0N/A * returned.
0N/A *
0N/A * The client must call the "dirty" method to renew the lease
0N/A * before the "lease" time expires or all references to remote
0N/A * objects in this VM that the client holds are considered
0N/A * "unreferenced".
0N/A */
0N/A public Lease dirty(ObjID[] ids, long sequenceNum, Lease lease) {
0N/A VMID vmid = lease.getVMID();
0N/A /*
0N/A * The server specifies the lease value; the client has
0N/A * no say in the matter.
0N/A */
0N/A long duration = leaseValue;
0N/A
0N/A if (dgcLog.isLoggable(Log.VERBOSE)) {
0N/A dgcLog.log(Log.VERBOSE, "vmid = " + vmid);
0N/A }
0N/A
0N/A // create a VMID if one wasn't supplied
0N/A if (vmid == null) {
0N/A vmid = new VMID();
0N/A
0N/A if (dgcLog.isLoggable(Log.BRIEF)) {
0N/A String clientHost;
0N/A try {
0N/A clientHost = RemoteServer.getClientHost();
0N/A } catch (ServerNotActiveException e) {
0N/A clientHost = "<unknown host>";
0N/A }
0N/A dgcLog.log(Log.BRIEF, " assigning vmid " + vmid +
0N/A " to client " + clientHost);
0N/A }
0N/A }
0N/A
0N/A lease = new Lease(vmid, duration);
0N/A // record lease information
0N/A synchronized (leaseTable) {
0N/A LeaseInfo info = leaseTable.get(vmid);
0N/A if (info == null) {
0N/A leaseTable.put(vmid, new LeaseInfo(vmid, duration));
0N/A if (checker == null) {
0N/A checker = scheduler.scheduleWithFixedDelay(
0N/A new Runnable() {
0N/A public void run() {
0N/A checkLeases();
0N/A }
0N/A },
0N/A leaseCheckInterval,
0N/A leaseCheckInterval, TimeUnit.MILLISECONDS);
0N/A }
0N/A } else {
0N/A info.renew(duration);
0N/A }
0N/A }
0N/A
0N/A for (ObjID id : ids) {
0N/A if (dgcLog.isLoggable(Log.VERBOSE)) {
0N/A dgcLog.log(Log.VERBOSE, "id = " + id +
0N/A ", vmid = " + vmid + ", duration = " + duration);
0N/A }
0N/A
0N/A ObjectTable.referenced(id, sequenceNum, vmid);
0N/A }
0N/A
0N/A // return the VMID used
0N/A return lease;
0N/A }
0N/A
0N/A /**
0N/A * The clean call removes the VMID from the set of clients
0N/A * that hold references to the object associated with the LiveRef
0N/A * ref. The sequence number is used to detect late clean calls. If the
0N/A * argument "strong" is true, then the clean call is a result of a
0N/A * failed "dirty" call, thus the sequence number for the VMID needs
0N/A * to be remembered until the client goes away.
0N/A */
0N/A public void clean(ObjID[] ids, long sequenceNum, VMID vmid, boolean strong)
0N/A {
0N/A for (ObjID id : ids) {
0N/A if (dgcLog.isLoggable(Log.VERBOSE)) {
0N/A dgcLog.log(Log.VERBOSE, "id = " + id +
0N/A ", vmid = " + vmid + ", strong = " + strong);
0N/A }
0N/A
0N/A ObjectTable.unreferenced(id, sequenceNum, vmid, strong);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Register interest in receiving a callback when this VMID
0N/A * becomes inaccessible.
0N/A */
0N/A void registerTarget(VMID vmid, Target target) {
0N/A synchronized (leaseTable) {
0N/A LeaseInfo info = leaseTable.get(vmid);
0N/A if (info == null) {
0N/A target.vmidDead(vmid);
0N/A } else {
0N/A info.notifySet.add(target);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Remove notification request.
0N/A */
0N/A void unregisterTarget(VMID vmid, Target target) {
0N/A synchronized (leaseTable) {
0N/A LeaseInfo info = leaseTable.get(vmid);
0N/A if (info != null) {
0N/A info.notifySet.remove(target);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Check if leases have expired. If a lease has expired, remove
0N/A * it from the table and notify all interested parties that the
0N/A * VMID is essentially "dead".
0N/A *
0N/A * @return if true, there are leases outstanding; otherwise leases
0N/A * no longer need to be checked
0N/A */
0N/A private void checkLeases() {
0N/A long time = System.currentTimeMillis();
0N/A
0N/A /* List of vmids that need to be removed from the leaseTable */
5559N/A List<LeaseInfo> toUnregister = new ArrayList<>();
0N/A
0N/A /* Build a list of leaseInfo objects that need to have
0N/A * targets removed from their notifySet. Remove expired
0N/A * leases from leaseTable.
0N/A */
0N/A synchronized (leaseTable) {
0N/A Iterator<LeaseInfo> iter = leaseTable.values().iterator();
0N/A while (iter.hasNext()) {
0N/A LeaseInfo info = iter.next();
0N/A if (info.expired(time)) {
0N/A toUnregister.add(info);
0N/A iter.remove();
0N/A }
0N/A }
0N/A
0N/A if (leaseTable.isEmpty()) {
0N/A checker.cancel(false);
0N/A checker = null;
0N/A }
0N/A }
0N/A
0N/A /* Notify and unegister targets without holding the lock on
0N/A * the leaseTable so we avoid deadlock.
0N/A */
0N/A for (LeaseInfo info : toUnregister) {
0N/A for (Target target : info.notifySet) {
0N/A target.vmidDead(info.vmid);
0N/A }
0N/A }
0N/A }
0N/A
0N/A static {
0N/A /*
0N/A * "Export" the singleton DGCImpl in a context isolated from
0N/A * the arbitrary current thread context.
0N/A */
0N/A AccessController.doPrivileged(new PrivilegedAction<Void>() {
0N/A public Void run() {
0N/A ClassLoader savedCcl =
0N/A Thread.currentThread().getContextClassLoader();
0N/A try {
0N/A Thread.currentThread().setContextClassLoader(
0N/A ClassLoader.getSystemClassLoader());
0N/A
0N/A /*
0N/A * Put remote collector object in table by hand to prevent
0N/A * listen on port. (UnicastServerRef.exportObject would
0N/A * cause transport to listen.)
0N/A */
0N/A try {
0N/A dgc = new DGCImpl();
0N/A ObjID dgcID = new ObjID(ObjID.DGC_ID);
0N/A LiveRef ref = new LiveRef(dgcID, 0);
0N/A UnicastServerRef disp = new UnicastServerRef(ref);
0N/A Remote stub =
0N/A Util.createProxy(DGCImpl.class,
0N/A new UnicastRef(ref), true);
0N/A disp.setSkeleton(dgc);
0N/A Target target =
0N/A new Target(dgc, disp, stub, dgcID, true);
0N/A ObjectTable.putTarget(target);
0N/A } catch (RemoteException e) {
0N/A throw new Error(
0N/A "exception initializing server-side DGC", e);
0N/A }
0N/A } finally {
0N/A Thread.currentThread().setContextClassLoader(savedCcl);
0N/A }
0N/A return null;
0N/A }
0N/A });
0N/A }
0N/A
0N/A private static class LeaseInfo {
0N/A VMID vmid;
0N/A long expiration;
5559N/A Set<Target> notifySet = new HashSet<>();
0N/A
0N/A LeaseInfo(VMID vmid, long lease) {
0N/A this.vmid = vmid;
0N/A expiration = System.currentTimeMillis() + lease;
0N/A }
0N/A
0N/A synchronized void renew(long lease) {
0N/A long newExpiration = System.currentTimeMillis() + lease;
0N/A if (newExpiration > expiration)
0N/A expiration = newExpiration;
0N/A }
0N/A
0N/A boolean expired(long time) {
0N/A if (expiration < time) {
0N/A if (dgcLog.isLoggable(Log.BRIEF)) {
0N/A dgcLog.log(Log.BRIEF, vmid.toString());
0N/A }
0N/A return true;
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A}