jdk/src/share/classes/sun/rmi/transport/DGCImpl.java
author smarks
Tue, 06 Aug 2013 14:24:05 -0700
changeset 19211 32a04c562026
parent 14342 8435a30053c1
child 23333 b0af2c7c8c91
permissions -rw-r--r--
8022440: suppress deprecation warnings in sun.rmi Reviewed-by: mduigou
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
import sun.security.action.GetLongAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * This class implements the guts of the server-side distributed GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
19211
32a04c562026 8022440: suppress deprecation warnings in sun.rmi
smarks
parents: 14342
diff changeset
    62
@SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
final class DGCImpl implements DGC {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /* dgc system log */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static final Log dgcLog = Log.getLog("sun.rmi.dgc", "dgc",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        LogStream.parseLevel(AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            new GetPropertyAction("sun.rmi.dgc.logLevel"))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /** lease duration to grant to clients */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final long leaseValue =              // default 10 minutes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            new GetLongAction("java.rmi.dgc.leaseValue", 600000));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /** lease check interval; default is half of lease grant duration */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static final long leaseCheckInterval =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            new GetLongAction("sun.rmi.dgc.checkInterval", leaseValue / 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /** thread pool for scheduling delayed tasks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static final ScheduledExecutorService scheduler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            new RuntimeUtil.GetInstanceAction()).getScheduler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /** remote implementation of DGC interface for this VM */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static DGCImpl dgc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /** table that maps VMID to LeaseInfo */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
    88
    private Map<VMID,LeaseInfo> leaseTable = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /** checks for lease expiration */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private Future<?> checker = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Return the remote implementation of the DGC interface for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * this VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static DGCImpl getDGCImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return dgc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Construct a new server-side remote object collector at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * a particular port. Disallow construction from outside.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private DGCImpl() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * The dirty call adds the VMID "vmid" to the set of clients
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * that hold references to the object associated with the ObjID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * id.  The long "sequenceNum" is used to detect late dirty calls.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * the VMID "vmid" is null, a VMID will be generated on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * server (for use by the client in subsequent calls) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * The client must call the "dirty" method to renew the lease
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * before the "lease" time expires or all references to remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * objects in this VM that the client holds are considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * "unreferenced".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public Lease dirty(ObjID[] ids, long sequenceNum, Lease lease) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        VMID vmid = lease.getVMID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         * The server specifies the lease value; the client has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         * no say in the matter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        long duration = leaseValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (dgcLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            dgcLog.log(Log.VERBOSE, "vmid = " + vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // create a VMID if one wasn't supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (vmid == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            vmid = new VMID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (dgcLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                String clientHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    clientHost = RemoteServer.getClientHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                } catch (ServerNotActiveException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    clientHost = "<unknown host>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                dgcLog.log(Log.BRIEF, " assigning vmid " + vmid +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                           " to client " + clientHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        lease = new Lease(vmid, duration);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // record lease information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        synchronized (leaseTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            LeaseInfo info = leaseTable.get(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                leaseTable.put(vmid, new LeaseInfo(vmid, duration));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                if (checker == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    checker = scheduler.scheduleWithFixedDelay(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                checkLeases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        leaseCheckInterval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        leaseCheckInterval, TimeUnit.MILLISECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                info.renew(duration);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        for (ObjID id : ids) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (dgcLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                dgcLog.log(Log.VERBOSE, "id = " + id +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                           ", vmid = " + vmid + ", duration = " + duration);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            ObjectTable.referenced(id, sequenceNum, vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // return the VMID used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return lease;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * The clean call removes the VMID from the set of clients
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * that hold references to the object associated with the LiveRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * ref.  The sequence number is used to detect late clean calls.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * argument "strong" is true, then the clean call is a result of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * failed "dirty" call, thus the sequence number for the VMID needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * to be remembered until the client goes away.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public void clean(ObjID[] ids, long sequenceNum, VMID vmid, boolean strong)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        for (ObjID id : ids) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (dgcLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                dgcLog.log(Log.VERBOSE, "id = " + id +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    ", vmid = " + vmid + ", strong = " + strong);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            ObjectTable.unreferenced(id, sequenceNum, vmid, strong);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Register interest in receiving a callback when this VMID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * becomes inaccessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    void registerTarget(VMID vmid, Target target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        synchronized (leaseTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            LeaseInfo info = leaseTable.get(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                target.vmidDead(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                info.notifySet.add(target);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Remove notification request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    void unregisterTarget(VMID vmid, Target target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        synchronized (leaseTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            LeaseInfo info = leaseTable.get(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            if (info != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                info.notifySet.remove(target);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Check if leases have expired.  If a lease has expired, remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * it from the table and notify all interested parties that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * VMID is essentially "dead".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @return if true, there are leases outstanding; otherwise leases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * no longer need to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    private void checkLeases() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        /* 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
   240
        List<LeaseInfo> toUnregister = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        /* Build a list of leaseInfo objects that need to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
         * targets removed from their notifySet.  Remove expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         * leases from leaseTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        synchronized (leaseTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            Iterator<LeaseInfo> iter = leaseTable.values().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                LeaseInfo info = iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                if (info.expired(time)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    toUnregister.add(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (leaseTable.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                checker.cancel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                checker = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        /* Notify and unegister targets without holding the lock on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         * the leaseTable so we avoid deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        for (LeaseInfo info : toUnregister) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            for (Target target : info.notifySet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                target.vmidDead(info.vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         * "Export" the singleton DGCImpl in a context isolated from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
         * the arbitrary current thread context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                ClassLoader savedCcl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    Thread.currentThread().setContextClassLoader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        ClassLoader.getSystemClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                     * Put remote collector object in table by hand to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                     * listen on port.  (UnicastServerRef.exportObject would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                     * cause transport to listen.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        dgc = new DGCImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        ObjID dgcID = new ObjID(ObjID.DGC_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        LiveRef ref = new LiveRef(dgcID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        UnicastServerRef disp = new UnicastServerRef(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        Remote stub =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                            Util.createProxy(DGCImpl.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                             new UnicastRef(ref), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        disp.setSkeleton(dgc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        Target target =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                            new Target(dgc, disp, stub, dgcID, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        ObjectTable.putTarget(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                        throw new Error(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                            "exception initializing server-side DGC", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    Thread.currentThread().setContextClassLoader(savedCcl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private static class LeaseInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        VMID vmid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        long expiration;
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   317
        Set<Target> notifySet = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        LeaseInfo(VMID vmid, long lease) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            this.vmid = vmid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            expiration = System.currentTimeMillis() + lease;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        synchronized void renew(long lease) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            long newExpiration = System.currentTimeMillis() + lease;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (newExpiration > expiration)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                expiration = newExpiration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        boolean expired(long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            if (expiration < time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                if (dgcLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    dgcLog.log(Log.BRIEF, vmid.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
}