jdk/src/java.rmi/share/classes/sun/rmi/transport/DGCImpl.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 23333 jdk/src/share/classes/sun/rmi/transport/DGCImpl.java@b0af2c7c8c91
child 33288 ef7d99d1e531
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19211
32a04c562026 8022440: suppress deprecation warnings in sun.rmi
smarks
parents: 14342
diff changeset
     2
 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.rmi.transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.rmi.Remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.rmi.RemoteException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.rmi.dgc.DGC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.rmi.dgc.Lease;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.rmi.dgc.VMID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.rmi.server.LogStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.rmi.server.ObjID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.rmi.server.RemoteServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.rmi.server.ServerNotActiveException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.concurrent.Future;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.concurrent.ScheduledExecutorService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.concurrent.TimeUnit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.rmi.runtime.Log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.rmi.runtime.RuntimeUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.rmi.server.UnicastRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.rmi.server.UnicastServerRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.rmi.server.Util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * This class implements the guts of the server-side distributed GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
19211
32a04c562026 8022440: suppress deprecation warnings in sun.rmi
smarks
parents: 14342
diff changeset
    60
@SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
final class DGCImpl implements DGC {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /* dgc system log */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static final Log dgcLog = Log.getLog("sun.rmi.dgc", "dgc",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        LogStream.parseLevel(AccessController.doPrivileged(
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
    66
            (PrivilegedAction<String>) () -> System.getProperty("sun.rmi.dgc.logLevel"))));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /** lease duration to grant to clients */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final long leaseValue =              // default 10 minutes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        AccessController.doPrivileged(
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
    71
            (PrivilegedAction<Long>) () -> Long.getLong("java.rmi.dgc.leaseValue", 600000));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /** lease check interval; default is half of lease grant duration */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final long leaseCheckInterval =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        AccessController.doPrivileged(
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
    76
            (PrivilegedAction<Long>) () -> Long.getLong("sun.rmi.dgc.checkInterval", leaseValue / 2));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /** thread pool for scheduling delayed tasks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final ScheduledExecutorService scheduler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            new RuntimeUtil.GetInstanceAction()).getScheduler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /** remote implementation of DGC interface for this VM */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static DGCImpl dgc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /** table that maps VMID to LeaseInfo */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
    86
    private Map<VMID,LeaseInfo> leaseTable = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /** checks for lease expiration */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private Future<?> checker = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Return the remote implementation of the DGC interface for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * this VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    static DGCImpl getDGCImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return dgc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Construct a new server-side remote object collector at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * a particular port. Disallow construction from outside.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private DGCImpl() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * The dirty call adds the VMID "vmid" to the set of clients
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * that hold references to the object associated with the ObjID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * id.  The long "sequenceNum" is used to detect late dirty calls.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * the VMID "vmid" is null, a VMID will be generated on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * server (for use by the client in subsequent calls) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * The client must call the "dirty" method to renew the lease
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * before the "lease" time expires or all references to remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * objects in this VM that the client holds are considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * "unreferenced".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public Lease dirty(ObjID[] ids, long sequenceNum, Lease lease) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        VMID vmid = lease.getVMID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         * The server specifies the lease value; the client has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         * no say in the matter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        long duration = leaseValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (dgcLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            dgcLog.log(Log.VERBOSE, "vmid = " + vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // create a VMID if one wasn't supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (vmid == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            vmid = new VMID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (dgcLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                String clientHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    clientHost = RemoteServer.getClientHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                } catch (ServerNotActiveException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    clientHost = "<unknown host>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                dgcLog.log(Log.BRIEF, " assigning vmid " + vmid +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                           " to client " + clientHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        lease = new Lease(vmid, duration);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // record lease information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        synchronized (leaseTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            LeaseInfo info = leaseTable.get(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                leaseTable.put(vmid, new LeaseInfo(vmid, duration));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (checker == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    checker = scheduler.scheduleWithFixedDelay(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                checkLeases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        leaseCheckInterval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        leaseCheckInterval, TimeUnit.MILLISECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                info.renew(duration);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        for (ObjID id : ids) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (dgcLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                dgcLog.log(Log.VERBOSE, "id = " + id +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                           ", vmid = " + vmid + ", duration = " + duration);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            ObjectTable.referenced(id, sequenceNum, vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // return the VMID used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return lease;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * The clean call removes the VMID from the set of clients
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * that hold references to the object associated with the LiveRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * ref.  The sequence number is used to detect late clean calls.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * argument "strong" is true, then the clean call is a result of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * failed "dirty" call, thus the sequence number for the VMID needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * to be remembered until the client goes away.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public void clean(ObjID[] ids, long sequenceNum, VMID vmid, boolean strong)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        for (ObjID id : ids) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (dgcLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                dgcLog.log(Log.VERBOSE, "id = " + id +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    ", vmid = " + vmid + ", strong = " + strong);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            ObjectTable.unreferenced(id, sequenceNum, vmid, strong);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Register interest in receiving a callback when this VMID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * becomes inaccessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    void registerTarget(VMID vmid, Target target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        synchronized (leaseTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            LeaseInfo info = leaseTable.get(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                target.vmidDead(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                info.notifySet.add(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Remove notification request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    void unregisterTarget(VMID vmid, Target target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        synchronized (leaseTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            LeaseInfo info = leaseTable.get(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (info != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                info.notifySet.remove(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Check if leases have expired.  If a lease has expired, remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * it from the table and notify all interested parties that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * VMID is essentially "dead".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @return if true, there are leases outstanding; otherwise leases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * no longer need to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private void checkLeases() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        /* List of vmids that need to be removed from the leaseTable */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   238
        List<LeaseInfo> toUnregister = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        /* Build a list of leaseInfo objects that need to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
         * targets removed from their notifySet.  Remove expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
         * leases from leaseTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        synchronized (leaseTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            Iterator<LeaseInfo> iter = leaseTable.values().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                LeaseInfo info = iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                if (info.expired(time)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    toUnregister.add(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if (leaseTable.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                checker.cancel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                checker = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        /* Notify and unegister targets without holding the lock on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         * the leaseTable so we avoid deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        for (LeaseInfo info : toUnregister) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            for (Target target : info.notifySet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                target.vmidDead(info.vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         * "Export" the singleton DGCImpl in a context isolated from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
         * the arbitrary current thread context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                ClassLoader savedCcl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    Thread.currentThread().setContextClassLoader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        ClassLoader.getSystemClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                     * Put remote collector object in table by hand to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                     * listen on port.  (UnicastServerRef.exportObject would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                     * cause transport to listen.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        dgc = new DGCImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        ObjID dgcID = new ObjID(ObjID.DGC_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        LiveRef ref = new LiveRef(dgcID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        UnicastServerRef disp = new UnicastServerRef(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        Remote stub =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                            Util.createProxy(DGCImpl.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                             new UnicastRef(ref), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        disp.setSkeleton(dgc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        Target target =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                            new Target(dgc, disp, stub, dgcID, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        ObjectTable.putTarget(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        throw new Error(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                            "exception initializing server-side DGC", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    Thread.currentThread().setContextClassLoader(savedCcl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private static class LeaseInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        VMID vmid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        long expiration;
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   315
        Set<Target> notifySet = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        LeaseInfo(VMID vmid, long lease) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            this.vmid = vmid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            expiration = System.currentTimeMillis() + lease;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        synchronized void renew(long lease) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            long newExpiration = System.currentTimeMillis() + lease;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (newExpiration > expiration)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                expiration = newExpiration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        boolean expired(long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (expiration < time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                if (dgcLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    dgcLog.log(Log.BRIEF, vmid.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
}