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