Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/transport/dgcDeadLock/TestImpl.java
38828 views
/*1* Copyright (c) 1998, 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/*24*/2526import java.rmi.*;27import sun.rmi.transport.*;28import sun.rmi.*;29import java.io.*;30import java.lang.reflect.*;31import java.rmi.dgc.*;32import java.util.*;33import java.rmi.registry.*;34import java.rmi.server.*;3536public class TestImpl extends UnicastRemoteObject37implements Test {38static Thread locker = null;39static TestImpl foo = null;40static TestImpl bar = null;4142TestImpl() throws RemoteException {43}4445public String echo(String msg) throws RemoteException {4647if (locker == null) {48// hold the target if not already held49locker = lockTargetExpireLeases(foo, DGCDeadLock.HOLD_TARGET_TIME);50}51return "Message received: " + msg;52}5354static public void main(String[] args) {55Registry registry = null;5657try {58int registryPort = Integer.parseInt(System.getProperty("rmi.registry.port"));59registry = java.rmi.registry.LocateRegistry.60createRegistry(registryPort);6162//export "Foo"63foo = new TestImpl();64Naming.rebind("rmi://:" +65registryPort66+ "/Foo", foo);6768try {69//export "Bar" after leases have been expired.70bar = new TestImpl();71Naming.rebind("rmi://localhost:" +72registryPort73+ "/Bar", bar);74} catch (Exception e) {75throw new RemoteException(e.getMessage());76}77Thread.sleep(DGCDeadLock.TEST_FAIL_TIME);78System.err.println("object vm exiting...");79System.exit(0);8081} catch (Exception e) {82System.err.println(e.getMessage());83e.printStackTrace();84} finally {85TestLibrary.unexport(registry);86registry = null;87}88}8990static Thread lockTargetExpireLeases(Remote toLock, int timeOut) {91Thread t = new Thread((Runnable) new TargetLocker(toLock, timeOut));92t.start();93return t;94}9596static class TargetLocker implements Runnable {9798Remote toLock = null;99int timeOut = 0;100101TargetLocker(Remote toLock, int timeOut) {102this.toLock = toLock;103this.timeOut = timeOut;104}105106public void run() {107try {108// give dgc dirty calls time to finish.109Thread.currentThread().sleep(4000);110111java.security.AccessController.112doPrivileged(new LockTargetCheckLeases(toLock,113timeOut));114115} catch (Exception e) {116System.err.println(e.getMessage());117e.printStackTrace();118System.exit(1);119}120}121}122123static class LockTargetCheckLeases124implements java.security.PrivilegedAction {125126Remote toLock = null;127int timeOut = 0;128129LockTargetCheckLeases(Remote toLock, int timeOut) {130this.toLock = toLock;131this.timeOut = timeOut;132}133134public Object run() {135try {136137Class args[] = new Class[1];138139Class objTableClass = Class.forName140("sun.rmi.transport.ObjectTable");141142/* get the Target that corresponds to toLock from the143* ObjectTable144*/145args[0] = Class.forName("java.rmi.Remote");146Method objTableGetTarget =147objTableClass.getDeclaredMethod("getTarget", args );148objTableGetTarget.setAccessible(true);149150Target lockTarget =151((Target) objTableGetTarget.invoke152(null , new Object [] {toLock} ));153154// make sure the lease on this object has expired.155expireLeases(lockTarget);156157// stop other threads from using the target for toLock.158synchronized (lockTarget) {159System.err.println("Locked the relevant target, sleeping " +160timeOut/1000 + " seconds");161Thread.currentThread().sleep(timeOut);162System.err.println("Target unlocked");163}164165} catch (Exception e) {166System.err.println(e.getMessage());167e.printStackTrace();168System.exit(1);169}170return null;171}172}173174/* leases have long values, so no dirty calls which would lock out175* a clean call, but the leases need to expire anyway, so we do it176* explicitly.177*/178static void expireLeases(Target t) throws Exception {179180final Target target = t;181182java.security.AccessController.doPrivileged(183184// put this into another class?185new java.security.PrivilegedAction() {186public Object run() {187try {188189Class DGCClass = Class.forName("sun.rmi.transport.DGCImpl");190Method getDGCImpl =191DGCClass.getDeclaredMethod("getDGCImpl", null );192getDGCImpl.setAccessible(true);193194// make sure the lease on this object has expired.195DGC dgcImpl = ((DGC) getDGCImpl.invoke(null, null));196197/* Get the lease table from the DGCImpl. */198Field reflectedLeaseTable =199dgcImpl.getClass().getDeclaredField("leaseTable");200reflectedLeaseTable.setAccessible(true);201202Map leaseTable = (Map) reflectedLeaseTable.get(dgcImpl);203204// dont really need this synchronization...205synchronized (leaseTable) {206Iterator en = leaseTable.values().iterator();207while (en.hasNext()) {208Object info = en.next();209210/* Get the notifySet in the leaseInfo object. */211Field notifySetField =212info.getClass().getDeclaredField("notifySet");213notifySetField.setAccessible(true);214HashSet notifySet = (HashSet) notifySetField.get(info);215216Iterator iter = notifySet.iterator();217while (iter.hasNext()) {218Target notified = (Target) iter.next();219220if (notified == target) {221222/* Get and set the expiration field from the info object. */223Field expirationField = info.getClass().224getDeclaredField("expiration");225expirationField.setAccessible(true);226expirationField.setLong(info, 0);227}228}229}230}231} catch (Exception e) {232System.err.println(e.getMessage());233e.printStackTrace();234System.exit(1);235}236// no interesting return value for this privileged action237return null;238}239});240}241}242243244