1 23 24 28 29 package com.sun.jts.CosTransactions; 30 31 import java.util.*; 32 33 import org.omg.CosTransactions.*; 34 import com.sun.jts.jtsxa.XID; 35 36 import com.sun.jts.trace.*; 37 38 import java.util.logging.Logger ; 39 import java.util.logging.Level ; 40 import com.sun.logging.LogDomains; 41 import com.sun.jts.utils.LogFormatter; 42 43 55 56 class DelegatedTimeoutManager { 57 60 static final int CANCEL_TIMEOUT = 0; 61 static final int NO_TIMEOUT = 0; 62 static final int ACTIVE_TIMEOUT = 1; 63 static final int IN_DOUBT_TIMEOUT = 2; 64 65 68 private static boolean initialised = false; 69 70 private Hashtable pendingTimeouts = new Hashtable(); 71 private Hashtable indoubtTimeouts = new Hashtable(); 72 private DelegatedTimeoutThread timeoutThread = null; 73 private boolean timeoutActive = false; 74 private boolean quiescing = false; 75 private boolean isSetTimeout = false; 76 private String logPath = null; 77 78 81 static Logger _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER); 82 91 111 112 DelegatedTimeoutManager() { 113 } 114 115 DelegatedTimeoutManager(String logPath) { 116 this.logPath = logPath; 117 } 118 119 120 synchronized void initSetTimeout() { 121 if (isSetTimeout) 122 return; 123 isSetTimeout = true; 124 timeoutThread = new DelegatedTimeoutThread(this); 125 timeoutThread.start(); 126 } 127 128 144 boolean setTimeout(Long localTID, int timeoutType, 145 int seconds) { 146 147 boolean result = true; 148 149 151 DelegatedTimeoutInfo timeoutInfo = null; 152 153 switch (timeoutType) { 154 155 158 case DelegatedTimeoutManager.ACTIVE_TIMEOUT : 159 if (!isSetTimeout) { 160 initSetTimeout(); 161 } 162 timeoutInfo = new DelegatedTimeoutInfo(); 163 timeoutInfo.expireTime = 164 new Date().getTime() + seconds * 1000; 165 timeoutInfo.localTID = localTID; 166 timeoutInfo.timeoutType = timeoutType; 167 pendingTimeouts.put(localTID,timeoutInfo); 168 break; 169 case TimeoutManager.IN_DOUBT_TIMEOUT : 170 if (!isSetTimeout) { 171 initSetTimeout(); 172 } 174 timeoutInfo = new DelegatedTimeoutInfo(); 175 timeoutInfo.expireTime = 176 new Date().getTime() + seconds * 1000; 177 timeoutInfo.localTID = localTID; 178 timeoutInfo.timeoutType = timeoutType; 179 indoubtTimeouts.put(localTID,timeoutInfo); 180 break; 181 182 184 default: 185 if (!isSetTimeout) 186 break; 187 result = (pendingTimeouts.remove(localTID) != null); 188 if (!result) 189 result = (indoubtTimeouts.remove(localTID) != null); 190 191 195 if (quiescing && pendingTimeouts.isEmpty() && indoubtTimeouts.isEmpty()) { 196 timeoutThread.stop(); 197 timeoutActive = false; 198 } 200 break; 201 } 202 return result; 203 } 204 205 221 void timeoutCoordinator(Long localTID, int timeoutType) { 222 223 227 228 CoordinatorImpl coord = DelegatedRecoveryManager.getLocalCoordinator(localTID, logPath); 229 if (coord == null) { 230 if(_logger.isLoggable(Level.FINER)) { 231 _logger.logp(Level.FINER,"DelegatedTimeoutManager","timeoutCoordinator()", 232 "DelegatedRecoveryManager.getLocalCoordinator() returned null,"+ 233 "which means txn is done. Setting timeout type to CANCEL_TIMEOUT"); 234 } 235 setTimeout(localTID, TimeoutManager.CANCEL_TIMEOUT, 0); 236 } else { 237 synchronized (coord) { 238 boolean[] isRoot = new boolean[1]; 239 240 switch (timeoutType) { 241 242 244 case DelegatedTimeoutManager.ACTIVE_TIMEOUT : 245 if(_logger.isLoggable(Level.FINER)) { 246 _logger.logp(Level.FINER,"DelegatedTimeoutManager","timeoutCoordinator()", 247 "DelegatedTimeoutManager.timeoutCoordinator():case ACTIVE_TIMEOUT"+ 248 "DelegatedRecoveryManager.getLocalCoordinator() returned non-null,"+ 249 "which means txn is still around. Rolling back the"+ 250 "transaction...: GTID is : " + 251 ((TopCoordinator)coord).superInfo.globalTID.toString()); 252 } 253 try { 254 coord.rollback_only(); 256 } catch (Throwable exc) {} 257 break; 258 259 265 case DelegatedTimeoutManager.IN_DOUBT_TIMEOUT : 266 if(_logger.isLoggable(Level.FINER)) { 267 _logger.logp(Level.FINER,"DelegatedTimeoutManager","timeoutCoordinator()", 268 "DelegatedTimeoutManager.timeoutCoordinator():case IN_DOUBT_TIMEOUT"+ 269 "DelegatedRecoveryManager.getLocalCoordinator() returned non-null,"+ 270 "which means txn is still around. Invoking recover(boolean)"+ 271 "on TopCoordinator...: GTID is: "+ 272 ((TopCoordinator)coord).superInfo.globalTID.toString()); 273 } 274 Status state = ((TopCoordinator) coord).recover(isRoot); 275 276 if (state == Status.StatusUnknown) { 277 278 282 _logger.log(Level.WARNING, "jts.transaction_resync_from_orginator_failed"); 285 286 } else if (state == Status.StatusCommitted) { 287 288 293 try { 294 ((TopCoordinator)coord).commit(); 295 if (isRoot[0]) { 296 ((TopCoordinator) coord). 297 afterCompletion(state); 298 } 299 } catch (Throwable exc) {} 300 } else { 301 try { 303 ((TopCoordinator) coord).rollback(true); 304 if (isRoot[0]) { 305 ((TopCoordinator) coord). 306 afterCompletion(Status.StatusRolledBack); 307 } 308 } catch (Throwable exc) {} 309 } 310 311 break; 312 313 default: 314 break; 316 } 317 } 318 } 319 } 320 321 338 Enumeration checkTimeouts() { 339 if (!isSetTimeout) 340 return null; 341 342 Enumeration result = null; 343 344 347 if (timeoutActive && ((pendingTimeouts.size() != 0) || (indoubtTimeouts.size() != 0))) { 348 Vector timedOut = null; 349 350 Enumeration timeouts = null; 351 352 synchronized (pendingTimeouts) { 353 timeouts = pendingTimeouts.elements(); 354 355 while (timeouts.hasMoreElements()) { 356 357 DelegatedTimeoutInfo timeoutInfo = (DelegatedTimeoutInfo)timeouts.nextElement(); 358 359 362 if (new Date().getTime() > timeoutInfo.expireTime) { 363 364 367 if (timedOut == null) { 368 timedOut = new Vector(); 369 } 370 371 timedOut.addElement(timeoutInfo); 372 } 373 } 374 } 375 376 synchronized (indoubtTimeouts) { 377 378 timeouts = indoubtTimeouts.elements(); 379 380 while (timeouts.hasMoreElements()) { 381 382 DelegatedTimeoutInfo timeoutInfo = (DelegatedTimeoutInfo)timeouts.nextElement(); 383 384 387 if (new Date().getTime() > timeoutInfo.expireTime) { 388 389 392 if (timedOut == null) { 393 timedOut = new Vector(); 394 } 395 396 timedOut.addElement(timeoutInfo); 397 } 398 } 399 400 } 401 403 if (timedOut != null) { 404 result = timedOut.elements(); 405 } 406 } 407 408 414 return result; 415 } 416 417 420 XID[] getInDoubtXids() { 421 422 synchronized (indoubtTimeouts) { 423 Vector inDoubtList = new Vector(); 424 425 Enumeration timeouts = indoubtTimeouts.elements(); 426 427 while (timeouts.hasMoreElements()) { 428 429 DelegatedTimeoutInfo timeoutInfo = (DelegatedTimeoutInfo) timeouts.nextElement(); 430 431 435 CoordinatorImpl coord = 436 DelegatedRecoveryManager.getLocalCoordinator(timeoutInfo.localTID, logPath); 437 438 if (coord != null) { 439 XID xid = new XID(); 440 xid.copy(coord.getGlobalTID()); 441 inDoubtList.addElement(xid); 442 } 443 } 444 445 return (XID[]) inDoubtList.toArray(new XID[] {}); 446 } 447 } 448 449 460 long timeLeft(Long localTID) { 461 462 DelegatedTimeoutInfo timeoutInfo = (DelegatedTimeoutInfo) pendingTimeouts.get(localTID); 463 if (timeoutInfo == null) 464 timeoutInfo = (DelegatedTimeoutInfo) indoubtTimeouts.get(localTID); 465 long result = -1; 466 if (timeoutInfo != null) { 467 result = timeoutInfo.expireTime - new Date().getTime(); 468 if (result < 0) { 469 result = 0; 470 } 471 } 472 473 return result; 474 } 475 476 490 void shutdown(boolean immediate) { 491 492 497 if (immediate || 498 pendingTimeouts == null || pendingTimeouts.isEmpty()) { 499 if (timeoutThread != null) { 500 timeoutThread.stop(); 501 } 502 503 if (pendingTimeouts != null) { 504 pendingTimeouts.clear(); 505 } 506 507 pendingTimeouts = null; 508 timeoutThread = null; 509 timeoutActive = false; 510 } else { 511 quiescing = true; 512 } 513 } 514 515 } 516 517 526 527 534 class DelegatedTimeoutInfo extends Object { 535 Long localTID = null; 536 long expireTime = 0; 537 int timeoutType = TimeoutManager.NO_TIMEOUT; 538 } 539 540 549 550 551 class DelegatedTimeoutThread extends Thread { 552 553 private int TIMEOUT_INTERVAL ; 554 private DelegatedTimeoutManager tmoutMgr = null; 555 556 static Logger _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER); 557 569 DelegatedTimeoutThread(DelegatedTimeoutManager timeoutMgr) { 570 setName("Delegated JTS Timeout Thread"); 571 setDaemon(true); 572 tmoutMgr = timeoutMgr; 573 try{ 574 String timeout_interval = Configuration.getPropertyValue(Configuration.TIMEOUT_INTERVAL); 575 if(timeout_interval!=null){ 576 TIMEOUT_INTERVAL= Integer.parseInt(timeout_interval); 577 TIMEOUT_INTERVAL*=1000; 578 if(TIMEOUT_INTERVAL<10000) 579 TIMEOUT_INTERVAL=10000; 580 } 581 else{ 582 TIMEOUT_INTERVAL=10000; 583 } 584 }catch(Exception e){ 585 TIMEOUT_INTERVAL=10000; 586 } 587 } 588 589 598 public void run() { 599 try { 600 while (true) { 601 602 604 Thread.sleep(TIMEOUT_INTERVAL); 605 606 609 Enumeration timedOut = tmoutMgr.checkTimeouts(); 610 611 614 if (timedOut != null) { 615 while (timedOut.hasMoreElements()) { 616 DelegatedTimeoutInfo timeoutInfo = 617 (DelegatedTimeoutInfo) timedOut.nextElement(); 618 619 625 tmoutMgr. 626 timeoutCoordinator(timeoutInfo.localTID, 627 timeoutInfo.timeoutType); 628 } 629 } 630 } 631 } catch (InterruptedException exc) { 632 _logger.log(Level.INFO,"jts.time_out_thread_stopped"); 633 } 634 } 635 } 636 | Popular Tags |