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