Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/reliability/juicer/AppleUserImpl.java
38828 views
/*1* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24*25* @summary The juicer is the classic RMI stress test. The juicer makes26* a large number of concurrent, long running, remote method invocations27* between many threads which have exported remote objects. These28* threads use remote objects that carry on deep "two party"29* recursion. The juicer relies on Distributed Garbage Collection to30* unexport these remote objects when no more references are held to them.31* The two parties in the recursion are OrangeImpl and32* OrangeEchoImpl. OrangeImpl checks the base case of the recursion33* so that the program will exit.34*35* When the AppleUserImpl.main() method is invoked, the class binds an36* instance of itself in a registry. A second server process,37* an ApplicationServer, is started which looks up the recently38* bound AppleUser object. This server is either started up in39* the same VM or can optionally be started in a separate VM on the40* same host or on a different host. When this test is run on the41* RMI profile, ApplicationServer must be started by AppleUserImpl42* and the complete juicer runs in a single process.43*44* The second server process instructs the AppleUserImpl to "use" some apples.45* AppleUserImpl creates a new thread for each apple. These threads46* initiate the two party recursion.47*48* Each recursive call nests to a depth determined by this49* expression: (2 + Math.abs(random.nextInt() % (maxLevel + 1)),50* where maxLevel is a command line parameter. Thus each recursive51* call nests a random number of levels between 2 and maxLevel.52*53* The test ends when an exception is encountered or the stop time54* has been reached.55*56* @library ../../testlibrary57* @build TestLibrary58* Apple AppleEvent AppleImpl59* Orange OrangeEcho OrangeEchoImpl OrangeImpl60* ApplicationServer61*62* @run main/othervm/policy=security.policy AppleUserImpl -seconds 3063*64* @author Peter Jones, Nigel Daley65*/6667import java.rmi.NoSuchObjectException;68import java.rmi.RemoteException;69import java.rmi.registry.LocateRegistry;70import java.rmi.registry.Registry;71import java.rmi.server.UnicastRemoteObject;72import java.util.Random;73import java.util.logging.Level;74import java.util.logging.Logger;7576/**77* The AppleUserImpl class implements the behavior of the remote78* "apple user" objects exported by the server. The application server79* passes each of its remote "apple" objects to an apple user, and an80* AppleUserThread is created for each apple.81*/82public class AppleUserImpl extends UnicastRemoteObject implements AppleUser {83private static int registryPort = -1;84private static final Logger logger =85Logger.getLogger("reliability.appleuser");86private static int threadNum = 0;87private static long testDuration = 0;88private static int maxLevel = 7;89private static Exception status = null;90private static boolean finished = false;91private static boolean startTestNotified = false;92private static final Random random = new Random();93private static final Object lock = new Object();9495public AppleUserImpl() throws RemoteException {96}9798/**99* Allows the other server process to indicate that it is ready100* to start "juicing".101*/102public synchronized void startTest() throws RemoteException {103startTestNotified = true;104this.notifyAll();105}106107/**108* Allows the other server process to report an exception to this109* process and thereby terminate the test.110*/111public void reportException(Exception status) throws RemoteException {112synchronized (lock) {113this.status = status;114lock.notifyAll();115}116}117118/**119* "Use" supplied apple object. Create an AppleUserThread to120* stress it out.121*/122public synchronized void useApple(Apple apple) throws RemoteException {123String threadName = Thread.currentThread().getName();124logger.log(Level.FINEST,125threadName + ": AppleUserImpl.useApple(): BEGIN");126127AppleUserThread t =128new AppleUserThread("AppleUserThread-" + (++threadNum), apple);129t.start();130131logger.log(Level.FINEST,132threadName + ": AppleUserImpl.useApple(): END");133}134135/**136* The AppleUserThread class repeatedly invokes calls on its associated137* Apple object to stress the RMI system.138*/139class AppleUserThread extends Thread {140141final Apple apple;142143public AppleUserThread(String name, Apple apple) {144super(name);145this.apple = apple;146}147148public void run() {149int orangeNum = 0;150long stopTime = System.currentTimeMillis() + testDuration;151Logger logger = Logger.getLogger("reliability.appleuserthread");152153try {154do { // loop until stopTime is reached155156/*157* Notify apple with some apple events. This tests158* serialization of arrays.159*/160int numEvents = Math.abs(random.nextInt() % 5);161AppleEvent[] events = new AppleEvent[numEvents];162for (int i = 0; i < events.length; i++) {163events[i] = new AppleEvent(orangeNum % 3);164}165apple.notify(events);166167/*168* Request a new orange object be created in169* the application server.170*/171Orange orange = apple.newOrange(172"Orange(" + getName() + ")-" + (++orangeNum));173174/*175* Create a large message of random ints to pass to orange.176*/177int msgLength = 1000 + Math.abs(random.nextInt() % 3000);178int[] message = new int[msgLength];179for (int i = 0; i < message.length; i++) {180message[i] = random.nextInt();181}182183/*184* Invoke recursive call on the orange. Base case185* of recursion inverts messgage.186*/187OrangeEchoImpl echo = new OrangeEchoImpl(188"OrangeEcho(" + getName() + ")-" + orangeNum);189int[] response = orange.recurse(echo, message,1902 + Math.abs(random.nextInt() % (maxLevel + 1)));191192/*193* Verify message was properly inverted and not corrupted194* through all the recursive method invocations.195*/196if (response.length != message.length) {197throw new RuntimeException(198"ERROR: CORRUPTED RESPONSE: " +199"wrong length of returned array " + "(should be " +200message.length + ", is " + response.length + ")");201}202for (int i = 0; i < message.length; i++) {203if (~message[i] != response[i]) {204throw new RuntimeException(205"ERROR: CORRUPTED RESPONSE: " +206"at element " + i + "/" + message.length +207" of returned array (should be " +208Integer.toHexString(~message[i]) + ", is " +209Integer.toHexString(response[i]) + ")");210}211}212213try {214Thread.sleep(Math.abs(random.nextInt() % 10) * 1000);215} catch (InterruptedException e) {216}217218} while (System.currentTimeMillis() < stopTime);219220} catch (Exception e) {221status = e;222}223finished = true;224synchronized (lock) {225lock.notifyAll();226}227}228}229230private static void usage() {231System.err.println("Usage: AppleUserImpl [-hours <hours> | " +232"-seconds <seconds>]");233System.err.println(" [-maxLevel <maxLevel>]");234System.err.println(" [-othervm]");235System.err.println(" [-exit]");236System.err.println(" hours The number of hours to run the juicer.");237System.err.println(" The default is 0 hours.");238System.err.println(" seconds The number of seconds to run the juicer.");239System.err.println(" The default is 0 seconds.");240System.err.println(" maxLevel The maximum number of levels to ");241System.err.println(" recurse on each call.");242System.err.println(" The default is 7 levels.");243System.err.println(" othervm If present, the VM will wait for the");244System.err.println(" ApplicationServer to start in");245System.err.println(" another process.");246System.err.println(" The default is to run everything in");247System.err.println(" a single VM.");248System.err.println(" exit If present, the VM will call");249System.err.println(" System.exit() when main() finishes.");250System.err.println(" The default is to not call");251System.err.println(" System.exit().");252System.err.println();253}254255/**256* Entry point for the "juicer" server process. Create and export257* an apple user implementation in an rmiregistry running on localhost.258*/259public static void main(String[] args) {260String durationString = null;261boolean othervm = false;262boolean exit = false;263try {264// parse command line args265for (int i = 0; i < args.length ; i++ ) {266String arg = args[i];267if (arg.equals("-hours")) {268if (durationString != null) {269usage();270}271i++;272int hours = Integer.parseInt(args[i]);273durationString = hours + " hours";274testDuration = hours * 60 * 60 * 1000;275} else if (arg.equals("-seconds")) {276if (durationString != null) {277usage();278}279i++;280long seconds = Long.parseLong(args[i]);281durationString = seconds + " seconds";282testDuration = seconds * 1000;283} else if (arg.equals("-maxLevel")) {284i++;285maxLevel = Integer.parseInt(args[i]);286} else if (arg.equals("-othervm")) {287othervm = true;288} else if (arg.equals("-exit")) {289exit = true;290} else {291usage();292}293}294if (durationString == null) {295durationString = testDuration + " milliseconds";296}297} catch (Throwable t) {298usage();299throw new RuntimeException("TEST FAILED: Bad argument");300}301302AppleUserImpl user = null;303long startTime = 0;304Thread server = null;305int exitValue = 0;306try {307user = new AppleUserImpl();308309synchronized (user) {310// create new registry and bind new AppleUserImpl in registry311Registry registry = TestLibrary.createRegistryOnUnusedPort();312registryPort = TestLibrary.getRegistryPort(registry);313LocateRegistry.getRegistry(registryPort).rebind("AppleUser",314user);315316// start the other server if applicable317if (othervm) {318// the other server must be running in a separate process319logger.log(Level.INFO, "Application server must be " +320"started in separate process");321} else {322Class app = Class.forName("ApplicationServer");323java.lang.reflect.Constructor appConstructor =324app.getDeclaredConstructor(new Class[] {Integer.TYPE});325server = new Thread((Runnable) appConstructor.newInstance(registryPort));326logger.log(Level.INFO, "Starting application server " +327"in same process");328server.start();329}330331// wait for other server to call startTest method332logger.log(Level.INFO, "Waiting for application server " +333"process to start");334while (!startTestNotified) {335user.wait();336}337}338339startTime = System.currentTimeMillis();340logger.log(Level.INFO, "Test starting");341342// wait for exception to be reported or first thread to complete343logger.log(Level.INFO, "Waiting " + durationString + " for " +344"test to complete or exception to be thrown");345346synchronized (lock) {347while (status == null && !finished) {348lock.wait();349}350}351352if (status != null) {353throw new RuntimeException("TEST FAILED: "354+ "juicer server reported an exception", status);355} else {356logger.log(Level.INFO, "TEST PASSED");357}358} catch (Exception e) {359logger.log(Level.INFO, "TEST FAILED");360exitValue = 1;361if (exit) {362e.printStackTrace();363}364throw new RuntimeException("TEST FAILED: "365+ "unexpected exception", e);366} finally {367long actualDuration = System.currentTimeMillis() - startTime;368logger.log(Level.INFO, "Test finished");369try {370UnicastRemoteObject.unexportObject(user, true);371} catch (NoSuchObjectException ignore) {372}373logger.log(Level.INFO, "Test duration was " +374(actualDuration/1000) + " seconds " +375"(" + (actualDuration/3600000) + " hours)");376System.gc(); System.gc();377if (exit) {378System.exit(exitValue);379}380}381}382}383384385