1 4 package com.tctest.restart.unit; 5 6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 7 import EDU.oswego.cs.dl.util.concurrent.FutureResult; 8 9 import com.tc.exception.TCRuntimeException; 10 import com.tc.object.config.ConfigVisitor; 11 import com.tc.object.config.ConfigLockLevel; 12 import com.tc.object.config.DSOClientConfigHelper; 13 import com.tc.object.config.LockDefinition; 14 import com.tctest.restart.AbstractRestartTestApp; 15 16 import java.lang.reflect.InvocationTargetException ; 17 18 public class DSOServerRestartTestApp extends AbstractRestartTestApp implements RestartUnitTestApp { 19 20 private final FutureResult fallThroughControl = new FutureResult(); 21 private final Object [] sharedLockHolder = new Object [1]; 22 private final Object [] distributedSharedLockHolder = new Object [1]; 23 private CyclicBarrier startBarrier; 24 public DSOServerRestartTestApp(ThreadGroup threadGroup) { 25 super(threadGroup); 26 changeState(INIT); 27 } 28 29 public String toString() { 30 return getClass().getName() + "[" + getID() + ", " + state.peek() + "]"; 31 } 32 33 public void setStartBarrier(CyclicBarrier startBarrier) { 34 this.startBarrier = startBarrier; 35 } 36 37 public void setSharedLock(Object lck) { 38 synchronized (this.sharedLockHolder) { 39 this.sharedLockHolder[0] = lck; 40 } 41 } 42 43 public void setDistributedSharedLock(Object lck) { 44 synchronized (this.distributedSharedLockHolder) { 45 this.distributedSharedLockHolder[0] = lck; 46 } 47 } 48 49 public Object getSharedLock() { 50 Object rv = null; 51 synchronized (this.sharedLockHolder) { 52 if (this.sharedLockHolder[0] != null) rv = this.sharedLockHolder[0]; 53 } 54 if (rv == null) { 55 synchronized (this.distributedSharedLockHolder) { 56 rv = this.distributedSharedLockHolder[0]; 57 } 58 } 59 System.out.println("returning shared lock..."); 60 return rv; 61 } 62 63 public synchronized void reset() { 64 if (!(isEnd() || isInit())) throw new IllegalStateException ("call to reset() when not in STOP state or INIT"); 65 changeState(INIT); 66 } 67 68 public void doWait(long millis) { 69 if (!isInit()) throw new IllegalStateException ("call to doWait() when not in INIT state."); 70 try { 71 changeState(START); 72 this.startBarrier.barrier(); 73 System.out.println("About to call basicDoWait()..."); 74 basicDoWait(millis); 75 changeState(END); 76 } catch (Exception e) { 77 throw new TCRuntimeException(e); 78 } 79 } 80 81 private void basicDoWait(long millis) throws InterruptedException { 82 Object sharedLock = getSharedLock(); 83 synchronized (sharedLock) { 84 changeState(WAITER); 85 sharedLock.wait(millis); 86 changeState(HOLDER); 87 this.state.clear(); 89 } 90 } 91 92 public void doNotify() { 93 Object sharedLock = getSharedLock(); 94 synchronized (sharedLock) { 95 System.out.println("About to call notify()"); 96 sharedLock.notify(); 97 } 98 } 99 100 public void doNotifyAll() { 101 Object sharedLock = getSharedLock(); 102 synchronized (sharedLock) { 103 System.out.println("About to call notifyAll()"); 104 sharedLock.notifyAll(); 105 } 106 } 107 108 public void attemptLock() { 109 if (!isInit()) throw new IllegalStateException ("call to attemptLock() when not in INIT state."); 110 try { 111 changeState(START); 112 this.startBarrier.barrier(); 113 System.out.println("About to call basicAttemptLock()..."); 114 basicAttemptLock(); 115 changeState(END); 116 } catch (Exception e) { 117 throw new TCRuntimeException(e); 118 } 119 } 120 121 private void basicAttemptLock() throws InterruptedException , InvocationTargetException { 122 synchronized (getSharedLock()) { 123 changeState(HOLDER); 126 fallThroughControl.get(); 127 this.state.clear(); 129 } 130 } 131 132 public void blockShutdown(final FutureResult blocker) throws Exception { 133 final FutureResult callback = new FutureResult(); 134 new Thread (this.threadGroup, new Runnable () { 135 public void run() { 136 try { 137 syncBlockShutdown(blocker, callback); 138 } catch (Exception e ) { 139 throw new TCRuntimeException(e); 140 } 141 } 142 }).start(); 143 callback.get(); 144 } 145 146 private void syncBlockShutdown(FutureResult blocker, FutureResult callback) throws InterruptedException , 147 InvocationTargetException { 148 System.out.println("Blocking shutdown..."); 149 callback.set(new Object ()); 150 blocker.get(); 151 } 152 153 public void fallThrough() { 154 if (!isHolder()) throw new IllegalStateException ("Attempt to release a lock without owning it."); 155 try { 156 fallThroughControl.set(new Object ()); 157 synchronized (this) { 158 while (!isEnd()) 159 wait(); 160 } 161 } catch (InterruptedException e) { 162 throw new TCRuntimeException(e); 163 } 164 } 165 166 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper cfg) { 167 String appClassName = DSOServerRestartTestApp.class.getName(); 168 cfg.addIncludePattern(appClassName); 169 cfg.addRoot(appClassName, "distributedSharedLockHolder", appClassName + ".distributedSharedLockHolder", true); 170 cfg.addWriteAutolock("void " + appClassName + ".setDistributedSharedLock(java.lang.Object)"); 171 cfg.addReadAutolock("java.lang.Object " + appClassName + ".getSharedLock()"); 172 173 cfg.addWriteAutolock("void " + appClassName + ".basicDoWait(..)"); 174 cfg.addWriteAutolock("void " + appClassName + ".doNotify()"); 175 cfg.addWriteAutolock("void " + appClassName + ".doNotifyAll()"); 176 LockDefinition lockDefinition = new LockDefinition("basicAttemptLock", ConfigLockLevel.WRITE); 177 lockDefinition.commit(); 178 cfg.addLock("void " + appClassName + ".basicAttemptLock()", lockDefinition); 179 180 lockDefinition = new LockDefinition("blockShutdown", ConfigLockLevel.WRITE); 181 lockDefinition.commit(); 182 cfg.addLock("void " + appClassName + ".syncBlockShutdown(..)", lockDefinition); 183 } 184 185 } 186 | Popular Tags |