1 24 25 package org.continuent.sequoia.controller.locks; 26 27 import java.sql.SQLException ; 28 29 import org.continuent.sequoia.common.log.Trace; 30 import org.continuent.sequoia.controller.backend.DatabaseBackend; 31 import org.continuent.sequoia.controller.loadbalancer.BackendTaskQueueEntry; 32 import org.continuent.sequoia.controller.loadbalancer.BackendTaskQueues; 33 import org.continuent.sequoia.controller.virtualdatabase.VirtualDatabase; 34 35 42 public class DeadlockDetectionThread extends Thread 43 { 44 private DatabaseBackend backend; 45 private VirtualDatabase vdb; 46 private Object dbsLock; 47 private long timeout; 48 private boolean killed = false; 49 private BackendTaskQueues queues; 50 private WaitForGraph waitForGraph; 51 protected static Trace logger = Trace 52 .getLogger("org.continuent.sequoia.controller.loadbalancer"); 53 54 64 public DeadlockDetectionThread(DatabaseBackend backend, VirtualDatabase vdb, 65 Object dbsLock, long timeout) 66 { 67 super("Deadlock detection thread"); 68 this.backend = backend; 69 this.vdb = vdb; 70 this.dbsLock = dbsLock; 71 this.timeout = timeout; 72 this.queues = backend.getTaskQueues(); 73 waitForGraph = new WaitForGraph(backend, queues.getStoredProcedureQueue()); 74 } 75 76 79 public synchronized void kill() 80 { 81 killed = true; 82 notify(); 83 } 84 85 88 public void run() 89 { 90 BackendTaskQueueEntry lastEntry = queues 91 .getFirstConflictingRequestQueueOrStoredProcedureQueueEntry(); 92 BackendTaskQueueEntry newEntry; 93 while (!killed) 94 { 95 newEntry = queues 96 .getFirstConflictingRequestQueueOrStoredProcedureQueueEntry(); 97 synchronized (this) 98 { 99 if ((newEntry == null) || (newEntry != lastEntry)) 100 { lastEntry = newEntry; 102 try 103 { 104 wait(timeout); 105 } 106 catch (InterruptedException ignore) 107 { 108 } 109 } 110 else 111 { boolean deadlockDetected; 114 long tid = 0; 115 synchronized (dbsLock) 116 { 117 deadlockDetected = waitForGraph.detectDeadlocks(); 118 if (deadlockDetected) 119 { tid = waitForGraph.getVictimTransactionId(); 121 } 122 lastEntry = null; 125 } if (deadlockDetected) 127 { 128 if (logger.isInfoEnabled()) 129 logger.info("Deadlock detected on backend " + backend.getName() 130 + ", aborting transaction " + tid); 131 132 try 133 { 134 vdb.abort(tid, true, true); 136 } 137 catch (SQLException e) 138 { 139 logger.warn("Failed to abort transaction " + tid, e); 140 } 141 } 142 } } } } 146 147 } 148 | Popular Tags |