1 23 24 28 29 51 package com.sun.jts.CosTransactions; 52 53 import java.util.*; 54 55 import org.omg.CosTransactions.*; 56 import com.sun.jts.jtsxa.XID; 57 58 import com.sun.jts.trace.*; 59 60 import java.util.logging.Logger ; 61 import java.util.logging.Level ; 62 import com.sun.logging.LogDomains; 63 import com.sun.jts.utils.LogFormatter; 64 65 75 76 83 class TimeoutManager { 84 87 static final int CANCEL_TIMEOUT = 0; 88 static final int NO_TIMEOUT = 0; 89 static final int ACTIVE_TIMEOUT = 1; 90 static final int IN_DOUBT_TIMEOUT = 2; 91 92 95 private static boolean initialised = false; 96 97 private static Hashtable pendingTimeouts = new Hashtable(); 98 private static Hashtable indoubtTimeouts = new Hashtable(); 99 private static TimeoutThread timeoutThread = null; 100 private static boolean timeoutActive = false; 101 private static boolean quiescing = false; 102 private static boolean isSetTimeout = false; 103 104 107 static Logger _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER); 108 117 synchronized static void initialise() { 118 119 121 if (initialised) { 122 return; 123 } 124 125 initialised = true; 126 127 129 if (!timeoutActive && timeoutThread == null) { 130 timeoutActive = true; 133 } 134 } 135 136 137 static synchronized void initSetTimeout() { 138 if (isSetTimeout) 139 return; 140 isSetTimeout = true; 141 timeoutThread = new TimeoutThread(); 142 timeoutThread.start(); 143 } 144 145 161 static boolean setTimeout(Long localTID, int timeoutType, 162 int seconds) { 163 164 boolean result = true; 165 166 168 if (timeoutActive) { 169 170 TimeoutInfo timeoutInfo = null; 171 172 switch (timeoutType) { 173 174 177 case TimeoutManager.ACTIVE_TIMEOUT : 178 if (!isSetTimeout) { 179 initSetTimeout(); 180 } 181 timeoutInfo = new TimeoutInfo(); 182 timeoutInfo.expireTime = 183 new Date().getTime() + seconds * 1000; 184 timeoutInfo.localTID = localTID; 185 timeoutInfo.timeoutType = timeoutType; 186 pendingTimeouts.put(localTID,timeoutInfo); 187 break; 188 case TimeoutManager.IN_DOUBT_TIMEOUT : 189 if (!isSetTimeout) { 190 initSetTimeout(); 191 } 193 timeoutInfo = new TimeoutInfo(); 194 timeoutInfo.expireTime = 195 new Date().getTime() + seconds * 1000; 196 timeoutInfo.localTID = localTID; 197 timeoutInfo.timeoutType = timeoutType; 198 indoubtTimeouts.put(localTID,timeoutInfo); 199 break; 200 201 203 default: 204 if (!isSetTimeout) 205 break; 206 result = (pendingTimeouts.remove(localTID) != null); 207 if (!result) 208 result = (indoubtTimeouts.remove(localTID) != null); 209 210 214 if (quiescing && pendingTimeouts.isEmpty() && indoubtTimeouts.isEmpty()) { 215 timeoutThread.stop(); 216 timeoutActive = false; 217 } 219 break; 220 } 221 } else { 222 result = false; 224 } 225 226 return result; 227 } 228 229 245 static void timeoutCoordinator(Long localTID, int timeoutType) { 246 247 251 252 CoordinatorImpl coord = RecoveryManager.getLocalCoordinator(localTID); 253 if (coord == null) { 254 if(_logger.isLoggable(Level.FINER)) 255 { 256 _logger.logp(Level.FINER,"TimeoutManager","timeoutCoordinator()", 257 "RecoveryManager.getLocalCoordinator() returned null,"+ 258 "which means txn is done. Setting timeout type to CANCEL_TIMEOUT"); 259 } 260 TimeoutManager.setTimeout(localTID, 261 TimeoutManager.CANCEL_TIMEOUT, 262 0); 263 } else { 264 synchronized (coord) { 265 boolean[] isRoot = new boolean[1]; 266 267 switch (timeoutType) { 268 269 271 case TimeoutManager.ACTIVE_TIMEOUT : 272 if(_logger.isLoggable(Level.FINER)) 273 { 274 _logger.logp(Level.FINER,"TimeoutManager","timeoutCoordinator()", 275 "TimeoutManager.timeoutCoordinator():case ACTIVE_TIMEOUT"+ 276 "RecoveryManager.getLocalCoordinator() returned non-null,"+ 277 "which means txn is still around. Marking for Rollback the"+ 278 "transaction...: GTID is : " + 279 ((TopCoordinator)coord).superInfo.globalTID.toString()); 280 } 281 try { 282 coord.rollback_only(); 284 } catch (Throwable exc) {} 285 break; 286 287 293 case TimeoutManager.IN_DOUBT_TIMEOUT : 294 if(_logger.isLoggable(Level.FINER)) 295 { 296 _logger.logp(Level.FINER,"TimeoutManager","timeoutCoordinator()", 297 "TimeoutManager.timeoutCoordinator():case IN_DOUBT_TIMEOUT"+ 298 "RecoveryManager.getLocalCoordinator() returned non-null,"+ 299 "which means txn is still around. Invoking recover(boolean)"+ 300 "on TopCoordinator...: GTID is: "+ 301 ((TopCoordinator)coord).superInfo.globalTID.toString()); 302 } 303 Status state = ((TopCoordinator) coord).recover(isRoot); 304 305 if (state == Status.StatusUnknown) { 306 307 311 _logger.log(Level.WARNING, "jts.transaction_resync_from_orginator_failed"); 314 315 } else if (state == Status.StatusCommitted) { 316 317 322 try { 323 ((TopCoordinator)coord).commit(); 324 if (isRoot[0]) { 325 ((TopCoordinator) coord). 326 afterCompletion(state); 327 } 328 } catch (Throwable exc) {} 329 } else { 330 try { 332 ((TopCoordinator) coord).rollback(true); 333 if (isRoot[0]) { 334 ((TopCoordinator) coord). 335 afterCompletion(Status.StatusRolledBack); 336 } 337 } catch (Throwable exc) {} 338 } 339 340 break; 341 342 default: 343 break; 345 } 346 } 347 } 348 } 349 350 367 static Enumeration checkTimeouts() { 368 if (!isSetTimeout) 369 return null; 370 371 Enumeration result = null; 372 373 376 if (timeoutActive && ((pendingTimeouts.size() != 0) || (indoubtTimeouts.size() != 0))) { 377 Vector timedOut = null; 378 379 Enumeration timeouts = null; 380 381 synchronized (pendingTimeouts) { 382 timeouts = pendingTimeouts.elements(); 383 384 while (timeouts.hasMoreElements()) { 385 386 TimeoutInfo timeoutInfo = (TimeoutInfo)timeouts.nextElement(); 387 388 391 if (new Date().getTime() > timeoutInfo.expireTime) { 392 393 396 if (timedOut == null) { 397 timedOut = new Vector(); 398 } 399 400 timedOut.addElement(timeoutInfo); 401 } 402 } 403 } 404 405 synchronized (indoubtTimeouts) { 406 407 timeouts = indoubtTimeouts.elements(); 408 409 while (timeouts.hasMoreElements()) { 410 411 TimeoutInfo timeoutInfo = (TimeoutInfo)timeouts.nextElement(); 412 413 416 if (new Date().getTime() > timeoutInfo.expireTime) { 417 418 421 if (timedOut == null) { 422 timedOut = new Vector(); 423 } 424 425 timedOut.addElement(timeoutInfo); 426 } 427 } 428 429 } 430 432 if (timedOut != null) { 433 result = timedOut.elements(); 434 } 435 } 436 437 443 return result; 444 } 445 446 449 static XID[] getInDoubtXids() { 450 451 synchronized (indoubtTimeouts) { 452 Vector inDoubtList = new Vector(); 453 454 Enumeration timeouts = indoubtTimeouts.elements(); 455 456 while (timeouts.hasMoreElements()) { 457 458 TimeoutInfo timeoutInfo = (TimeoutInfo) timeouts.nextElement(); 459 460 464 CoordinatorImpl coord = 465 RecoveryManager.getLocalCoordinator(timeoutInfo.localTID); 466 467 if (coord != null) { 468 XID xid = new XID(); 469 xid.copy(coord.getGlobalTID()); 470 inDoubtList.addElement(xid); 471 } 472 } 473 474 return (XID[]) inDoubtList.toArray(new XID[] {}); 475 } 476 } 477 478 489 static long timeLeft(Long localTID) { 490 491 TimeoutInfo timeoutInfo = (TimeoutInfo) pendingTimeouts.get(localTID); 492 if (timeoutInfo == null) 493 timeoutInfo = (TimeoutInfo) indoubtTimeouts.get(localTID); 494 long result = -1; 495 if (timeoutInfo != null) { 496 result = timeoutInfo.expireTime - new Date().getTime(); 497 if (result < 0) { 498 result = 0; 499 } 500 } 501 502 return result; 503 } 504 505 519 static void shutdown(boolean immediate) { 520 521 526 if (immediate || 527 pendingTimeouts == null || pendingTimeouts.isEmpty()) { 528 if (timeoutThread != null) { 529 timeoutThread.stop(); 530 } 531 532 if (pendingTimeouts != null) { 533 pendingTimeouts.clear(); 534 } 535 536 pendingTimeouts = null; 537 timeoutThread = null; 538 timeoutActive = false; 539 } else { 540 quiescing = true; 541 } 542 } 543 544 554 586 } 587 588 597 598 605 class TimeoutInfo extends Object { 606 Long localTID = null; 607 long expireTime = 0; 608 int timeoutType = TimeoutManager.NO_TIMEOUT; 609 } 610 611 621 622 629 class TimeoutThread extends Thread { 630 631 private int TIMEOUT_INTERVAL ; 632 633 static Logger _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER); 634 646 TimeoutThread() { 647 setName("JTS Timeout Thread"); 648 setDaemon(true); 649 try{ 650 String timeout_interval = Configuration.getPropertyValue(Configuration.TIMEOUT_INTERVAL); 651 if(timeout_interval!=null){ 652 TIMEOUT_INTERVAL= Integer.parseInt(timeout_interval); 653 TIMEOUT_INTERVAL*=1000; 654 if(TIMEOUT_INTERVAL<10000) 655 TIMEOUT_INTERVAL=10000; 656 } 657 else{ 658 TIMEOUT_INTERVAL=10000; 659 } 660 }catch(Exception e){ 661 TIMEOUT_INTERVAL=10000; 662 } 663 } 664 665 674 public void run() { 675 try { 676 while (true) { 677 678 680 Thread.sleep(TIMEOUT_INTERVAL); 681 682 685 Enumeration timedOut = TimeoutManager.checkTimeouts(); 686 687 690 if (timedOut != null) { 691 while (timedOut.hasMoreElements()) { 692 TimeoutInfo timeoutInfo = 693 (TimeoutInfo) timedOut.nextElement(); 694 695 701 TimeoutManager. 702 timeoutCoordinator(timeoutInfo.localTID, 703 timeoutInfo.timeoutType); 704 } 705 } 706 } 707 } catch (InterruptedException exc) { 708 _logger.log(Level.INFO,"jts.time_out_thread_stopped"); 709 } 710 } 711 } 712 | Popular Tags |