Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/reliability/juicer/AppleUserImpl.java
38828 views
1
/*
2
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/* @test
25
*
26
* @summary The juicer is the classic RMI stress test. The juicer makes
27
* a large number of concurrent, long running, remote method invocations
28
* between many threads which have exported remote objects. These
29
* threads use remote objects that carry on deep "two party"
30
* recursion. The juicer relies on Distributed Garbage Collection to
31
* unexport these remote objects when no more references are held to them.
32
* The two parties in the recursion are OrangeImpl and
33
* OrangeEchoImpl. OrangeImpl checks the base case of the recursion
34
* so that the program will exit.
35
*
36
* When the AppleUserImpl.main() method is invoked, the class binds an
37
* instance of itself in a registry. A second server process,
38
* an ApplicationServer, is started which looks up the recently
39
* bound AppleUser object. This server is either started up in
40
* the same VM or can optionally be started in a separate VM on the
41
* same host or on a different host. When this test is run on the
42
* RMI profile, ApplicationServer must be started by AppleUserImpl
43
* and the complete juicer runs in a single process.
44
*
45
* The second server process instructs the AppleUserImpl to "use" some apples.
46
* AppleUserImpl creates a new thread for each apple. These threads
47
* initiate the two party recursion.
48
*
49
* Each recursive call nests to a depth determined by this
50
* expression: (2 + Math.abs(random.nextInt() % (maxLevel + 1)),
51
* where maxLevel is a command line parameter. Thus each recursive
52
* call nests a random number of levels between 2 and maxLevel.
53
*
54
* The test ends when an exception is encountered or the stop time
55
* has been reached.
56
*
57
* @library ../../testlibrary
58
* @build TestLibrary
59
* Apple AppleEvent AppleImpl
60
* Orange OrangeEcho OrangeEchoImpl OrangeImpl
61
* ApplicationServer
62
*
63
* @run main/othervm/policy=security.policy AppleUserImpl -seconds 30
64
*
65
* @author Peter Jones, Nigel Daley
66
*/
67
68
import java.rmi.NoSuchObjectException;
69
import java.rmi.RemoteException;
70
import java.rmi.registry.LocateRegistry;
71
import java.rmi.registry.Registry;
72
import java.rmi.server.UnicastRemoteObject;
73
import java.util.Random;
74
import java.util.logging.Level;
75
import java.util.logging.Logger;
76
77
/**
78
* The AppleUserImpl class implements the behavior of the remote
79
* "apple user" objects exported by the server. The application server
80
* passes each of its remote "apple" objects to an apple user, and an
81
* AppleUserThread is created for each apple.
82
*/
83
public class AppleUserImpl extends UnicastRemoteObject implements AppleUser {
84
private static int registryPort = -1;
85
private static final Logger logger =
86
Logger.getLogger("reliability.appleuser");
87
private static int threadNum = 0;
88
private static long testDuration = 0;
89
private static int maxLevel = 7;
90
private static Exception status = null;
91
private static boolean finished = false;
92
private static boolean startTestNotified = false;
93
private static final Random random = new Random();
94
private static final Object lock = new Object();
95
96
public AppleUserImpl() throws RemoteException {
97
}
98
99
/**
100
* Allows the other server process to indicate that it is ready
101
* to start "juicing".
102
*/
103
public synchronized void startTest() throws RemoteException {
104
startTestNotified = true;
105
this.notifyAll();
106
}
107
108
/**
109
* Allows the other server process to report an exception to this
110
* process and thereby terminate the test.
111
*/
112
public void reportException(Exception status) throws RemoteException {
113
synchronized (lock) {
114
this.status = status;
115
lock.notifyAll();
116
}
117
}
118
119
/**
120
* "Use" supplied apple object. Create an AppleUserThread to
121
* stress it out.
122
*/
123
public synchronized void useApple(Apple apple) throws RemoteException {
124
String threadName = Thread.currentThread().getName();
125
logger.log(Level.FINEST,
126
threadName + ": AppleUserImpl.useApple(): BEGIN");
127
128
AppleUserThread t =
129
new AppleUserThread("AppleUserThread-" + (++threadNum), apple);
130
t.start();
131
132
logger.log(Level.FINEST,
133
threadName + ": AppleUserImpl.useApple(): END");
134
}
135
136
/**
137
* The AppleUserThread class repeatedly invokes calls on its associated
138
* Apple object to stress the RMI system.
139
*/
140
class AppleUserThread extends Thread {
141
142
final Apple apple;
143
144
public AppleUserThread(String name, Apple apple) {
145
super(name);
146
this.apple = apple;
147
}
148
149
public void run() {
150
int orangeNum = 0;
151
long stopTime = System.currentTimeMillis() + testDuration;
152
Logger logger = Logger.getLogger("reliability.appleuserthread");
153
154
try {
155
do { // loop until stopTime is reached
156
157
/*
158
* Notify apple with some apple events. This tests
159
* serialization of arrays.
160
*/
161
int numEvents = Math.abs(random.nextInt() % 5);
162
AppleEvent[] events = new AppleEvent[numEvents];
163
for (int i = 0; i < events.length; i++) {
164
events[i] = new AppleEvent(orangeNum % 3);
165
}
166
apple.notify(events);
167
168
/*
169
* Request a new orange object be created in
170
* the application server.
171
*/
172
Orange orange = apple.newOrange(
173
"Orange(" + getName() + ")-" + (++orangeNum));
174
175
/*
176
* Create a large message of random ints to pass to orange.
177
*/
178
int msgLength = 1000 + Math.abs(random.nextInt() % 3000);
179
int[] message = new int[msgLength];
180
for (int i = 0; i < message.length; i++) {
181
message[i] = random.nextInt();
182
}
183
184
/*
185
* Invoke recursive call on the orange. Base case
186
* of recursion inverts messgage.
187
*/
188
OrangeEchoImpl echo = new OrangeEchoImpl(
189
"OrangeEcho(" + getName() + ")-" + orangeNum);
190
int[] response = orange.recurse(echo, message,
191
2 + Math.abs(random.nextInt() % (maxLevel + 1)));
192
193
/*
194
* Verify message was properly inverted and not corrupted
195
* through all the recursive method invocations.
196
*/
197
if (response.length != message.length) {
198
throw new RuntimeException(
199
"ERROR: CORRUPTED RESPONSE: " +
200
"wrong length of returned array " + "(should be " +
201
message.length + ", is " + response.length + ")");
202
}
203
for (int i = 0; i < message.length; i++) {
204
if (~message[i] != response[i]) {
205
throw new RuntimeException(
206
"ERROR: CORRUPTED RESPONSE: " +
207
"at element " + i + "/" + message.length +
208
" of returned array (should be " +
209
Integer.toHexString(~message[i]) + ", is " +
210
Integer.toHexString(response[i]) + ")");
211
}
212
}
213
214
try {
215
Thread.sleep(Math.abs(random.nextInt() % 10) * 1000);
216
} catch (InterruptedException e) {
217
}
218
219
} while (System.currentTimeMillis() < stopTime);
220
221
} catch (Exception e) {
222
status = e;
223
}
224
finished = true;
225
synchronized (lock) {
226
lock.notifyAll();
227
}
228
}
229
}
230
231
private static void usage() {
232
System.err.println("Usage: AppleUserImpl [-hours <hours> | " +
233
"-seconds <seconds>]");
234
System.err.println(" [-maxLevel <maxLevel>]");
235
System.err.println(" [-othervm]");
236
System.err.println(" [-exit]");
237
System.err.println(" hours The number of hours to run the juicer.");
238
System.err.println(" The default is 0 hours.");
239
System.err.println(" seconds The number of seconds to run the juicer.");
240
System.err.println(" The default is 0 seconds.");
241
System.err.println(" maxLevel The maximum number of levels to ");
242
System.err.println(" recurse on each call.");
243
System.err.println(" The default is 7 levels.");
244
System.err.println(" othervm If present, the VM will wait for the");
245
System.err.println(" ApplicationServer to start in");
246
System.err.println(" another process.");
247
System.err.println(" The default is to run everything in");
248
System.err.println(" a single VM.");
249
System.err.println(" exit If present, the VM will call");
250
System.err.println(" System.exit() when main() finishes.");
251
System.err.println(" The default is to not call");
252
System.err.println(" System.exit().");
253
System.err.println();
254
}
255
256
/**
257
* Entry point for the "juicer" server process. Create and export
258
* an apple user implementation in an rmiregistry running on localhost.
259
*/
260
public static void main(String[] args) {
261
String durationString = null;
262
boolean othervm = false;
263
boolean exit = false;
264
try {
265
// parse command line args
266
for (int i = 0; i < args.length ; i++ ) {
267
String arg = args[i];
268
if (arg.equals("-hours")) {
269
if (durationString != null) {
270
usage();
271
}
272
i++;
273
int hours = Integer.parseInt(args[i]);
274
durationString = hours + " hours";
275
testDuration = hours * 60 * 60 * 1000;
276
} else if (arg.equals("-seconds")) {
277
if (durationString != null) {
278
usage();
279
}
280
i++;
281
long seconds = Long.parseLong(args[i]);
282
durationString = seconds + " seconds";
283
testDuration = seconds * 1000;
284
} else if (arg.equals("-maxLevel")) {
285
i++;
286
maxLevel = Integer.parseInt(args[i]);
287
} else if (arg.equals("-othervm")) {
288
othervm = true;
289
} else if (arg.equals("-exit")) {
290
exit = true;
291
} else {
292
usage();
293
}
294
}
295
if (durationString == null) {
296
durationString = testDuration + " milliseconds";
297
}
298
} catch (Throwable t) {
299
usage();
300
throw new RuntimeException("TEST FAILED: Bad argument");
301
}
302
303
AppleUserImpl user = null;
304
long startTime = 0;
305
Thread server = null;
306
int exitValue = 0;
307
try {
308
user = new AppleUserImpl();
309
310
synchronized (user) {
311
// create new registry and bind new AppleUserImpl in registry
312
Registry registry = TestLibrary.createRegistryOnUnusedPort();
313
registryPort = TestLibrary.getRegistryPort(registry);
314
LocateRegistry.getRegistry(registryPort).rebind("AppleUser",
315
user);
316
317
// start the other server if applicable
318
if (othervm) {
319
// the other server must be running in a separate process
320
logger.log(Level.INFO, "Application server must be " +
321
"started in separate process");
322
} else {
323
Class app = Class.forName("ApplicationServer");
324
java.lang.reflect.Constructor appConstructor =
325
app.getDeclaredConstructor(new Class[] {Integer.TYPE});
326
server = new Thread((Runnable) appConstructor.newInstance(registryPort));
327
logger.log(Level.INFO, "Starting application server " +
328
"in same process");
329
server.start();
330
}
331
332
// wait for other server to call startTest method
333
logger.log(Level.INFO, "Waiting for application server " +
334
"process to start");
335
while (!startTestNotified) {
336
user.wait();
337
}
338
}
339
340
startTime = System.currentTimeMillis();
341
logger.log(Level.INFO, "Test starting");
342
343
// wait for exception to be reported or first thread to complete
344
logger.log(Level.INFO, "Waiting " + durationString + " for " +
345
"test to complete or exception to be thrown");
346
347
synchronized (lock) {
348
while (status == null && !finished) {
349
lock.wait();
350
}
351
}
352
353
if (status != null) {
354
throw new RuntimeException("TEST FAILED: "
355
+ "juicer server reported an exception", status);
356
} else {
357
logger.log(Level.INFO, "TEST PASSED");
358
}
359
} catch (Exception e) {
360
logger.log(Level.INFO, "TEST FAILED");
361
exitValue = 1;
362
if (exit) {
363
e.printStackTrace();
364
}
365
throw new RuntimeException("TEST FAILED: "
366
+ "unexpected exception", e);
367
} finally {
368
long actualDuration = System.currentTimeMillis() - startTime;
369
logger.log(Level.INFO, "Test finished");
370
try {
371
UnicastRemoteObject.unexportObject(user, true);
372
} catch (NoSuchObjectException ignore) {
373
}
374
logger.log(Level.INFO, "Test duration was " +
375
(actualDuration/1000) + " seconds " +
376
"(" + (actualDuration/3600000) + " hours)");
377
System.gc(); System.gc();
378
if (exit) {
379
System.exit(exitValue);
380
}
381
}
382
}
383
}
384
385