jdk/src/java.rmi/share/classes/sun/rmi/transport/ConnectionInputStream.java
author rriggs
Thu, 16 Mar 2017 16:16:31 -0400
changeset 45984 75fef64e21fa
parent 25859 3317bb8137f4
permissions -rw-r--r--
8163958: Improved garbage collection Reviewed-by: smarks, chegar, skoivu, rhalade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45984
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents: 25859
diff changeset
     2
 * Copyright (c) 1996, 2017, 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.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.rmi.RemoteException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.rmi.server.UID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.rmi.server.MarshalInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.rmi.runtime.Log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Special stream to keep track of refs being unmarshaled so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * refs can be ref-counted locally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
class ConnectionInputStream extends MarshalInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /** indicates whether ack is required for DGC */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private boolean dgcAckNeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /** Hashtable mapping Endpoints to lists of LiveRefs to register */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
    46
    private Map<Endpoint, List<LiveRef>> incomingRefTable = new HashMap<>(5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /** identifier for gc ack*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private UID ackID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Constructs a marshal input stream using the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * stream "in".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    ConnectionInputStream(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    void readID() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        ackID = UID.read((DataInput) this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Save reference in order to send "dirty" call after all args/returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * have been unmarshaled.  Save in hashtable incomingRefTable.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * table is keyed on endpoints, and holds objects of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * IncomingRefTableEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    void saveRef(LiveRef ref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        Endpoint ep = ref.getEndpoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // check whether endpoint is already in the hashtable
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
    73
        List<LiveRef> refList = incomingRefTable.get(ep);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (refList == null) {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
    76
            refList = new ArrayList<LiveRef>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            incomingRefTable.put(ep, refList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // add ref to list of refs for endpoint ep
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        refList.add(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
45984
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents: 25859
diff changeset
    85
     * Discard the saved incoming refs so there is nothing to register
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents: 25859
diff changeset
    86
     * when {@code registerRefs} is called.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents: 25859
diff changeset
    87
     */
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents: 25859
diff changeset
    88
    void discardRefs() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents: 25859
diff changeset
    89
        incomingRefTable.clear();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents: 25859
diff changeset
    90
    }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents: 25859
diff changeset
    91
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents: 25859
diff changeset
    92
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Add references to DGC table (and possibly send dirty call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * RegisterRefs now calls DGCClient.referenced on all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * refs with the same endpoint at once to achieve batching of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * calls to the DGC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    void registerRefs() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (!incomingRefTable.isEmpty()) {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   100
            for (Map.Entry<Endpoint, List<LiveRef>> entry :
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   101
                     incomingRefTable.entrySet()) {
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   102
                DGCClient.registerRefs(entry.getKey(), entry.getValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Indicate that an ack is required to the distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * collector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    void setAckNeeded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        dgcAckNeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Done with input stream for remote call. Send DGC ack if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Allow sending of ack to fail without flagging an error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    void done(Connection c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         * WARNING: The connection c may have already been freed.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         * is only be safe to use c to obtain c's channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (dgcAckNeeded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            Connection conn = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            Channel ch = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            boolean reuse = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            DGCImpl.dgcLog.log(Log.VERBOSE, "send ack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                ch = c.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                conn = ch.newConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                DataOutputStream out =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    new DataOutputStream(conn.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                out.writeByte(TransportConstants.DGCAck);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (ackID == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    ackID = new UID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                ackID.write((DataOutput) out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                conn.releaseOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                 * Fix for 4221173: if this connection is on top of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                 * HttpSendSocket, the DGCAck won't actually get sent until a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                 * read operation is attempted on the socket.  Calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                 * available() is the most innocuous way of triggering the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                 * write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                conn.getInputStream().available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                conn.releaseInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                reuse = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                reuse = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                if (conn != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    ch.free(conn, reuse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } catch (RemoteException e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                // eat exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}