1 24 25 package org.continuent.sequoia.controller.core.shutdown; 26 27 import java.util.ArrayList ; 28 29 import org.continuent.sequoia.common.i18n.Translate; 30 import org.continuent.sequoia.controller.cache.result.AbstractResultCache; 31 import org.continuent.sequoia.controller.recoverylog.RecoveryLog; 32 import org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase; 33 import org.continuent.sequoia.controller.virtualdatabase.VirtualDatabase; 34 import org.continuent.sequoia.controller.virtualdatabase.VirtualDatabaseWorkerThread; 35 36 44 public abstract class VirtualDatabaseShutdownThread extends ShutdownThread 45 { 46 protected VirtualDatabase virtualDatabase; 47 48 55 public VirtualDatabaseShutdownThread(VirtualDatabase vdb, int level) 56 { 57 super(level); 58 this.virtualDatabase = vdb; 59 } 60 61 66 protected void shutdownCacheRecoveryLogAndGroupCommunication() 67 { 68 AbstractResultCache resultCache = virtualDatabase.getRequestManager() 70 .getResultCache(); 71 if (resultCache != null) 72 resultCache.shutdown(); 73 74 if (virtualDatabase.isDistributed()) 75 { 76 logger.info("Shutting down group communication"); 77 try 78 { 79 ((DistributedVirtualDatabase) virtualDatabase).quitChannel(shutdownLevel); 80 } 81 catch (Exception e) 82 { 83 logger 84 .warn( 85 "An error occured while shutting down the group communication channel", 86 e); 87 } 88 } 89 90 RecoveryLog recoveryLog = virtualDatabase.getRequestManager() 92 .getRecoveryLog(); 93 if (recoveryLog != null) 94 { 95 virtualDatabase.getRequestManager().setRecoveryLog(null); 98 recoveryLog.shutdown(); 100 } 101 } 102 103 107 protected void disableAllBackendsWithCheckpoint() 108 { 109 if (virtualDatabase.getRequestManager().getRecoveryLog() != null) 110 { 111 try 112 { virtualDatabase.disableAllBackendsWithCheckpoint(virtualDatabase 114 .buildCheckpointName("disable all backends")); 115 } 116 catch (Exception ve) 117 { 118 logger.error(Translate 119 .get("controller.shutdown.backends.exception", ve)); 120 } 121 finally 122 { 123 virtualDatabase.storeBackendsInfo(); 124 } 125 } 126 else 127 { disableAllBackendsWithoutCheckpoint(); 129 } 130 } 131 132 135 protected void disableAllBackendsWithoutCheckpoint() 136 { 137 try 138 { 139 virtualDatabase.disableAllBackends(true); 140 } 141 catch (Exception vde) 142 { 143 logger 144 .error(Translate.get("controller.shutdown.backends.exception", vde)); 145 } 146 } 147 148 152 protected void terminateVirtualDatabaseWorkerThreads() 153 { 154 ArrayList idleThreads = virtualDatabase.getPendingConnections(); 155 156 synchronized (idleThreads) 159 { 160 virtualDatabase.setPoolConnectionThreads(false); 161 idleThreads.notifyAll(); 162 } 163 164 ArrayList threads = virtualDatabase.getActiveThreads(); 165 logger.info(Translate.get("controller.shutdown.active.threads", threads 166 .size())); 167 168 boolean retry; 169 do 170 { 171 synchronized (threads) 172 { 173 for (int i = 0; i < threads.size(); i++) 175 { 176 if (logger.isDebugEnabled()) 177 logger.debug(Translate.get("controller.shutdown.database.thread", 178 new String []{virtualDatabase.getVirtualDatabaseName(), 179 String.valueOf(i)})); 180 ((VirtualDatabaseWorkerThread) threads.get(i)).shutdown(); 181 } 182 } 183 synchronized (threads) 184 { 185 retry = threads.size() > 0; 186 if (retry) 187 { 188 try 189 { 190 threads.wait(100); 191 } 192 catch (InterruptedException e) 193 { 194 e.printStackTrace(); 195 } 196 } 197 } 198 } 199 while (retry); 200 } 201 202 206 protected void waitForClientsToDisconnect() 207 { 208 boolean wait = true; 209 ArrayList threads = virtualDatabase.getActiveThreads(); 210 while (wait) 211 { 212 synchronized (threads) 213 { 214 int nbThreads = threads.size(); 215 if (logger.isDebugEnabled()) 216 logger.debug(Translate.get("controller.shutdown.active.threads", 217 nbThreads)); 218 if (nbThreads == 0) 219 wait = false; 220 } 221 if (wait) 222 { 223 synchronized (this) 224 { 225 try 226 { 227 wait(1000); 228 } 229 catch (InterruptedException e) 230 { 231 } 233 } 234 } 235 } 236 } 237 238 } | Popular Tags |