jdk/test/java/rmi/dgc/retryDirtyCalls/RetryDirtyCalls.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 309 bda219d843f6
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/* 
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4268258
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary When a DGC dirty call fails, RMI's client-side DGC implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * should attempt to retry the same dirty call a few times, at least until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * known lease for that endpoint has expired, instead of just giving up
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * renewing that lease at all after the first failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author Peter Jones (inspired by Adrian Colley's test case in 4268258)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @build RetryDirtyCalls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @build RetryDirtyCalls_Stub
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @run main/othervm RetryDirtyCalls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.rmi.server.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
interface Self extends Remote {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    Self getSelf() throws RemoteException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class RetryDirtyCalls implements Self, Unreferenced {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /** how long we wait before declaring that this test has passed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final static long TIMEOUT = 20000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /** true if this object's unreferenced method has been called */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private boolean unreferenced = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Return this object.  The need for this method is explained below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public Self getSelf() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
	return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public void unreferenced() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
	synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
	    unreferenced = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
	    notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
	System.err.println("\nRegression test for bug 4268258\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
	/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
	 * Set properties to tweak DGC behavior so that this test will execute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
	 * quickly: set the granted lease duration to 10 seconds, the interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
	 * that leases are checked to 3 seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
	 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
	System.setProperty("java.rmi.dgc.leaseValue", "10000");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
	System.setProperty("sun.rmi.dgc.checkInterval", "3000");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
	/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
	 * Make idle connections time out almost instantly (0.1 seconds) so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
	 * that the DGC implementation will have to make a new connection for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
	 * each dirty call, thus going through the socket factory, where we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
	 * can easily cause the operation to fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
	 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
	System.setProperty("sun.rmi.transport.connectionTimeout", "100");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
	RetryDirtyCalls impl = new RetryDirtyCalls();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
	try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
	    TestSF sf = new TestSF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
	    RMISocketFactory.setSocketFactory(sf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
	    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
	     * The stub returned by UnicastRemoteObject.exportObject() does
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
	     * not participate in DGC, but it does allow us to invoke a method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
	     * on the remote object through RMI.  Therefore, we invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
	     * getSelf() method through RMI, which returns an equivalent stub
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
	     * that does participate in DGC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
	     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
	    Self stub = (Self) UnicastRemoteObject.exportObject(impl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
	    Self dgcStub = stub.getSelf();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
	    stub = null;		// in case 4114579 has been fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
	    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
	     * Set the socket factory to cause 3 connections attempts in a row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
	     * to fail before allowing a connection to succeed, expecting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
	     * client-side DGC implementation to make at least four attempts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
	     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
	    final int FLAKE_FACTOR = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
	    sf.setFlakeFactor(FLAKE_FACTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
	    long deadline = System.currentTimeMillis() + TIMEOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
	    boolean unreferenced;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
	    synchronized (impl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
		while (!(unreferenced = impl.unreferenced)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
		    long timeToWait = deadline - System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
		    if (timeToWait > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
			impl.wait(timeToWait);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
		    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
			break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
		    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
		}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
	    if (unreferenced) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
		throw new RuntimeException("remote object unreferenced");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
	    int createCount = sf.getCreateCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
	    if (createCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
		throw new RuntimeException("test socket factory never used");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
	    } else if (createCount < (FLAKE_FACTOR + 3)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
		/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
		 * The unreferenced method was not invoked for some reason,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
		 * but the dirty calls were clearly not retried well enough.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
		 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
		throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
		    "test failed because dirty calls not retried enough, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
		    "but remote object not unreferenced");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
	    System.err.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
		"TEST PASSED: remote object not unreferenced");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
	} catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
	    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
	    throw new RuntimeException("TEST FAILED: " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
	} finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
	    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
	     * When all is said and done, try to unexport the remote object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
	     * so that the VM has a chance to exit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
	     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
	    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
		UnicastRemoteObject.unexportObject(impl, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
	    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
class TestSF extends RMISocketFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private int flakeFactor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private int flakeState = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private int createCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public synchronized void setFlakeFactor(int newFlakeFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
	flakeFactor = newFlakeFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public synchronized int getCreateCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
	return createCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public synchronized Socket createSocket(String host, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
	throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
	createCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
	if (++flakeState > flakeFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
	    flakeState = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
	if (flakeState == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
	    return new Socket(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
	} else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
	    throw new IOException("random network failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public ServerSocket createServerSocket(int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
	return new ServerSocket(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}