169N/A/*
5266N/A * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A *
0N/A * @summary The juicer is the classic RMI stress test. The juicer makes
0N/A * a large number of concurrent, long running, remote method invocations
0N/A * between many threads which have exported remote objects. These
0N/A * threads use remote objects that carry on deep "two party"
169N/A * recursion. The juicer relies on Distributed Garbage Collection to
169N/A * unexport these remote objects when no more references are held to them.
0N/A * The two parties in the recursion are OrangeImpl and
0N/A * OrangeEchoImpl. OrangeImpl checks the base case of the recursion
0N/A * so that the program will exit.
0N/A *
0N/A * When the AppleUserImpl.main() method is invoked, the class binds an
0N/A * instance of itself in a registry. A second server process,
169N/A * an ApplicationServer, is started which looks up the recently
0N/A * bound AppleUser object. This server is either started up in
0N/A * the same VM or can optionally be started in a separate VM on the
169N/A * same host or on a different host. When this test is run on the
169N/A * RMI profile, ApplicationServer must be started by AppleUserImpl
0N/A * and the complete juicer runs in a single process.
0N/A *
0N/A * The second server process instructs the AppleUserImpl to "use" some apples.
0N/A * AppleUserImpl creates a new thread for each apple. These threads
0N/A * initiate the two party recursion.
169N/A *
0N/A * Each recursive call nests to a depth determined by this
169N/A * expression: (2 + Math.abs(random.nextInt() % (maxLevel + 1)),
0N/A * where maxLevel is a command line parameter. Thus each recursive
0N/A * call nests a random number of levels between 2 and maxLevel.
169N/A *
0N/A * The test ends when an exception is encountered or the stop time
0N/A * has been reached.
0N/A *
0N/A * @library ../../testlibrary
5266N/A * @build TestLibrary
5551N/A * Apple AppleEvent AppleImpl
5551N/A * Orange OrangeEcho OrangeEchoImpl OrangeImpl
5551N/A * ApplicationServer
0N/A *
0N/A * @run main/othervm/policy=security.policy AppleUserImpl -seconds 30
0N/A *
0N/A * @author Peter Jones, Nigel Daley
0N/A */
0N/A
5266N/Aimport java.rmi.NoSuchObjectException;
0N/Aimport java.rmi.RemoteException;
0N/Aimport java.rmi.registry.LocateRegistry;
5266N/Aimport java.rmi.registry.Registry;
5266N/Aimport java.rmi.server.UnicastRemoteObject;
0N/Aimport java.util.Random;
5266N/Aimport java.util.logging.Level;
0N/Aimport java.util.logging.Logger;
0N/A
0N/A/**
0N/A * The AppleUserImpl class implements the behavior of the remote
0N/A * "apple user" objects exported by the server. The application server
0N/A * passes each of its remote "apple" objects to an apple user, and an
0N/A * AppleUserThread is created for each apple.
0N/A */
0N/Apublic class AppleUserImpl extends UnicastRemoteObject implements AppleUser {
5266N/A private static int registryPort = -1;
169N/A private static final Logger logger =
169N/A Logger.getLogger("reliability.appleuser");
0N/A private static int threadNum = 0;
0N/A private static long testDuration = 0;
0N/A private static int maxLevel = 7;
0N/A private static Exception status = null;
0N/A private static boolean finished = false;
0N/A private static boolean startTestNotified = false;
0N/A private static final Random random = new Random();
0N/A private static final Object lock = new Object();
169N/A
0N/A public AppleUserImpl() throws RemoteException {
0N/A }
0N/A
0N/A /**
0N/A * Allows the other server process to indicate that it is ready
0N/A * to start "juicing".
0N/A */
0N/A public synchronized void startTest() throws RemoteException {
169N/A startTestNotified = true;
0N/A this.notifyAll();
0N/A }
0N/A
0N/A /**
0N/A * Allows the other server process to report an exception to this
0N/A * process and thereby terminate the test.
0N/A */
0N/A public void reportException(Exception status) throws RemoteException {
169N/A synchronized (lock) {
169N/A this.status = status;
169N/A lock.notifyAll();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * "Use" supplied apple object. Create an AppleUserThread to
0N/A * stress it out.
0N/A */
0N/A public synchronized void useApple(Apple apple) throws RemoteException {
169N/A String threadName = Thread.currentThread().getName();
169N/A logger.log(Level.FINEST,
169N/A threadName + ": AppleUserImpl.useApple(): BEGIN");
0N/A
169N/A AppleUserThread t =
169N/A new AppleUserThread("AppleUserThread-" + (++threadNum), apple);
169N/A t.start();
0N/A
169N/A logger.log(Level.FINEST,
169N/A threadName + ": AppleUserImpl.useApple(): END");
0N/A }
169N/A
0N/A /**
0N/A * The AppleUserThread class repeatedly invokes calls on its associated
0N/A * Apple object to stress the RMI system.
0N/A */
0N/A class AppleUserThread extends Thread {
0N/A
169N/A final Apple apple;
0N/A
169N/A public AppleUserThread(String name, Apple apple) {
169N/A super(name);
169N/A this.apple = apple;
169N/A }
0N/A
169N/A public void run() {
169N/A int orangeNum = 0;
0N/A long stopTime = System.currentTimeMillis() + testDuration;
0N/A Logger logger = Logger.getLogger("reliability.appleuserthread");
0N/A
169N/A try {
169N/A do { // loop until stopTime is reached
169N/A
169N/A /*
169N/A * Notify apple with some apple events. This tests
0N/A * serialization of arrays.
169N/A */
169N/A int numEvents = Math.abs(random.nextInt() % 5);
169N/A AppleEvent[] events = new AppleEvent[numEvents];
169N/A for (int i = 0; i < events.length; i++) {
169N/A events[i] = new AppleEvent(orangeNum % 3);
169N/A }
169N/A apple.notify(events);
0N/A
169N/A /*
169N/A * Request a new orange object be created in
0N/A * the application server.
169N/A */
169N/A Orange orange = apple.newOrange(
169N/A "Orange(" + getName() + ")-" + (++orangeNum));
0N/A
169N/A /*
169N/A * Create a large message of random ints to pass to orange.
169N/A */
0N/A int msgLength = 1000 + Math.abs(random.nextInt() % 3000);
169N/A int[] message = new int[msgLength];
169N/A for (int i = 0; i < message.length; i++) {
169N/A message[i] = random.nextInt();
169N/A }
0N/A
169N/A /*
169N/A * Invoke recursive call on the orange. Base case
0N/A * of recursion inverts messgage.
169N/A */
169N/A OrangeEchoImpl echo = new OrangeEchoImpl(
169N/A "OrangeEcho(" + getName() + ")-" + orangeNum);
169N/A int[] response = orange.recurse(echo, message,
169N/A 2 + Math.abs(random.nextInt() % (maxLevel + 1)));
0N/A
169N/A /*
169N/A * Verify message was properly inverted and not corrupted
169N/A * through all the recursive method invocations.
169N/A */
169N/A if (response.length != message.length) {
169N/A throw new RuntimeException(
169N/A "ERROR: CORRUPTED RESPONSE: " +
169N/A "wrong length of returned array " + "(should be " +
169N/A message.length + ", is " + response.length + ")");
169N/A }
169N/A for (int i = 0; i < message.length; i++) {
169N/A if (~message[i] != response[i]) {
169N/A throw new RuntimeException(
169N/A "ERROR: CORRUPTED RESPONSE: " +
169N/A "at element " + i + "/" + message.length +
169N/A " of returned array (should be " +
169N/A Integer.toHexString(~message[i]) + ", is " +
169N/A Integer.toHexString(response[i]) + ")");
169N/A }
169N/A }
0N/A
169N/A try {
169N/A Thread.sleep(Math.abs(random.nextInt() % 10) * 1000);
169N/A } catch (InterruptedException e) {
169N/A }
0N/A
169N/A } while (System.currentTimeMillis() < stopTime);
0N/A
169N/A } catch (Exception e) {
169N/A status = e;
169N/A }
169N/A finished = true;
169N/A synchronized (lock) {
169N/A lock.notifyAll();
169N/A }
169N/A }
0N/A }
0N/A
0N/A private static void usage() {
169N/A System.err.println("Usage: AppleUserImpl [-hours <hours> | " +
169N/A "-seconds <seconds>]");
169N/A System.err.println(" [-maxLevel <maxLevel>]");
169N/A System.err.println(" [-othervm]");
169N/A System.err.println(" [-exit]");
169N/A System.err.println(" hours The number of hours to run the juicer.");
169N/A System.err.println(" The default is 0 hours.");
169N/A System.err.println(" seconds The number of seconds to run the juicer.");
169N/A System.err.println(" The default is 0 seconds.");
169N/A System.err.println(" maxLevel The maximum number of levels to ");
169N/A System.err.println(" recurse on each call.");
169N/A System.err.println(" The default is 7 levels.");
169N/A System.err.println(" othervm If present, the VM will wait for the");
169N/A System.err.println(" ApplicationServer to start in");
169N/A System.err.println(" another process.");
169N/A System.err.println(" The default is to run everything in");
169N/A System.err.println(" a single VM.");
169N/A System.err.println(" exit If present, the VM will call");
169N/A System.err.println(" System.exit() when main() finishes.");
169N/A System.err.println(" The default is to not call");
169N/A System.err.println(" System.exit().");
169N/A System.err.println();
0N/A }
0N/A
0N/A /**
0N/A * Entry point for the "juicer" server process. Create and export
0N/A * an apple user implementation in an rmiregistry running on localhost.
0N/A */
0N/A public static void main(String[] args) {
169N/A String durationString = null;
0N/A boolean othervm = false;
0N/A boolean exit = false;
169N/A try {
169N/A // parse command line args
169N/A for (int i = 0; i < args.length ; i++ ) {
0N/A String arg = args[i];
0N/A if (arg.equals("-hours")) {
169N/A if (durationString != null) {
169N/A usage();
169N/A }
169N/A i++;
169N/A int hours = Integer.parseInt(args[i]);
169N/A durationString = hours + " hours";
169N/A testDuration = hours * 60 * 60 * 1000;
0N/A } else if (arg.equals("-seconds")) {
169N/A if (durationString != null) {
169N/A usage();
169N/A }
169N/A i++;
169N/A long seconds = Long.parseLong(args[i]);
169N/A durationString = seconds + " seconds";
169N/A testDuration = seconds * 1000;
0N/A } else if (arg.equals("-maxLevel")) {
169N/A i++;
169N/A maxLevel = Integer.parseInt(args[i]);
0N/A } else if (arg.equals("-othervm")) {
169N/A othervm = true;
0N/A } else if (arg.equals("-exit")) {
169N/A exit = true;
0N/A } else {
169N/A usage();
0N/A }
0N/A }
0N/A if (durationString == null) {
0N/A durationString = testDuration + " milliseconds";
0N/A }
169N/A } catch (Throwable t) {
169N/A usage();
169N/A throw new RuntimeException("TEST FAILED: Bad argument");
169N/A }
0N/A
169N/A AppleUserImpl user = null;
169N/A long startTime = 0;
169N/A Thread server = null;
169N/A int exitValue = 0;
169N/A try {
169N/A user = new AppleUserImpl();
0N/A
169N/A synchronized (user) {
169N/A // create new registry and bind new AppleUserImpl in registry
5266N/A Registry registry = TestLibrary.createRegistryOnUnusedPort();
5266N/A registryPort = TestLibrary.getRegistryPort(registry);
5266N/A LocateRegistry.getRegistry(registryPort).rebind("AppleUser",
5266N/A user);
169N/A
169N/A // start the other server if applicable
169N/A if (othervm) {
169N/A // the other server must be running in a separate process
169N/A logger.log(Level.INFO, "Application server must be " +
169N/A "started in separate process");
169N/A } else {
169N/A Class app = Class.forName("ApplicationServer");
5266N/A java.lang.reflect.Constructor appConstructor =
5266N/A app.getDeclaredConstructor(new Class[] {Integer.TYPE});
5266N/A server = new Thread((Runnable) appConstructor.newInstance(registryPort));
169N/A logger.log(Level.INFO, "Starting application server " +
0N/A "in same process");
169N/A server.start();
169N/A }
0N/A
169N/A // wait for other server to call startTest method
169N/A logger.log(Level.INFO, "Waiting for application server " +
0N/A "process to start");
169N/A while (!startTestNotified) {
169N/A user.wait();
169N/A }
169N/A }
0N/A
169N/A startTime = System.currentTimeMillis();
169N/A logger.log(Level.INFO, "Test starting");
0N/A
169N/A // wait for exception to be reported or first thread to complete
169N/A logger.log(Level.INFO, "Waiting " + durationString + " for " +
169N/A "test to complete or exception to be thrown");
0N/A
169N/A synchronized (lock) {
169N/A while (status == null && !finished) {
169N/A lock.wait();
169N/A }
169N/A }
0N/A
169N/A if (status != null) {
169N/A throw new RuntimeException("TEST FAILED: "
169N/A + "juicer server reported an exception", status);
169N/A } else {
169N/A logger.log(Level.INFO, "TEST PASSED");
0N/A }
169N/A } catch (Exception e) {
169N/A logger.log(Level.INFO, "TEST FAILED");
169N/A exitValue = 1;
169N/A if (exit) {
169N/A e.printStackTrace();
169N/A }
169N/A throw new RuntimeException("TEST FAILED: "
169N/A + "unexpected exception", e);
169N/A } finally {
169N/A long actualDuration = System.currentTimeMillis() - startTime;
169N/A logger.log(Level.INFO, "Test finished");
169N/A try {
169N/A UnicastRemoteObject.unexportObject(user, true);
0N/A } catch (NoSuchObjectException ignore) {
169N/A }
169N/A logger.log(Level.INFO, "Test duration was " +
169N/A (actualDuration/1000) + " seconds " +
169N/A "(" + (actualDuration/3600000) + " hours)");
169N/A System.gc(); System.gc();
169N/A if (exit) {
169N/A System.exit(exitValue);
169N/A }
0N/A }
0N/A }
0N/A}