1 23 24 28 50 package com.sun.jts.CosTransactions; 51 52 54 import java.util.*; 55 import java.io.*; 56 57 import org.omg.CORBA.*; 58 59 import java.io.DataOutputStream ; 60 import java.io.DataInputStream ; 61 import java.util.logging.Logger ; 62 import java.util.logging.Level ; 63 import com.sun.logging.LogDomains; 64 import com.sun.jts.utils.LogFormatter; 65 66 86 93 class CoordinatorLog extends java.lang.Object implements LogUpcallTarget { 94 private static final int LOG_DEF_KEY_TRIGGER = 100; 95 private static final int LOG_THRESHOLD = 10000; 96 private static final int STRING_TO_REF_RETRIES = 20; 97 private static final String defaultstring = "DEFAULT_LOG"; 98 99 113 private static int keypointTrigger = 100; 114 private static Hashtable logStateHoldertable = new Hashtable(); 115 116 private Hashtable sectionMapping = null; 117 private boolean rewriteRequired = false; 118 private boolean writeDone = false; 119 120 private String logPath = null; 121 122 125 static Logger _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER); 126 127 129 Long localTID = null; 130 CoordinatorLogStateHolder logStateHolder = null; 131 private static CoordinatorLogStateHolder defaultLogStateHolder = getStateHolder(defaultstring); 132 133 135 private ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(2000); 136 private DataOutputStream dataOutput = new DataOutputStream (byteOutput); 137 138 139 153 static private CoordinatorLogStateHolder getStateHolder(String str) { 154 synchronized (logStateHoldertable) { 155 CoordinatorLogStateHolder logStateHolder = (CoordinatorLogStateHolder)logStateHoldertable.get(str); 156 if (logStateHolder == null) { 157 logStateHolder = new CoordinatorLogStateHolder(); 158 logStateHolder.logFile = null; 159 logStateHolder.log = null; 160 logStateHolder.activeLogs = new Hashtable(); 161 logStateHolder.keypointLogs = new Hashtable(); 162 logStateHolder.tranCount = 0; 163 logStateHolder.keypointInProgress = false; 164 logStateHolder.keypointLock = new RWLock(); 166 logStateHolder.keypointStateLock = new java.lang.Object (); 167 logStateHoldertable.put(str,logStateHolder); 168 } 169 return logStateHolder; 170 } 171 } 172 173 181 CoordinatorLog() { 182 183 sectionMapping = new Hashtable(); 184 logStateHolder = defaultLogStateHolder; 185 186 187 190 } 191 192 CoordinatorLog(String logPath) { 193 194 sectionMapping = new Hashtable(); 195 logStateHolder = getStateHolder(logPath); 196 this.logPath = logPath; 197 198 199 202 } 203 204 212 synchronized public void finalize() { 213 214 216 if( sectionMapping != null ) { 217 Enumeration sections = sectionMapping.elements(); 218 int sz = sectionMapping.size(); 222 while (sz-- > 0) { 223 CoordinatorLogSection section = (CoordinatorLogSection)sections.nextElement(); 224 section.reUse(); 225 } 226 sectionMapping.clear(); 227 sectionMapping = null; 228 } 229 } 230 231 250 synchronized private void reUse() { 252 254 if( sectionMapping != null ) { 255 Enumeration sections = sectionMapping.elements(); 256 int sz = sectionMapping.size(); 257 while (sz-- > 0) { 258 CoordinatorLogSection section = (CoordinatorLogSection)sections.nextElement(); 259 section.reUse(); 260 } 261 sectionMapping.clear(); 262 } 263 rewriteRequired = false; 264 writeDone = false; 265 localTID = null; 266 267 byteOutput.reset(); 268 269 CoordinatorLogPool.putCoordinatorLog(this); 271 272 } 273 274 synchronized private void reUse(String logPath) { 275 276 278 if( sectionMapping != null ) { 279 Enumeration sections = sectionMapping.elements(); 280 int sz = sectionMapping.size(); 281 while (sz-- > 0) { 282 CoordinatorLogSection section = (CoordinatorLogSection)sections.nextElement(); 283 section.reUse(); 284 } 285 sectionMapping.clear(); 286 } 287 rewriteRequired = false; 288 writeDone = false; 289 localTID = null; 290 291 byteOutput.reset(); 292 293 CoordinatorLogPool.putCoordinatorLog(this, logPath); 295 296 } 297 298 310 CoordinatorLog( Long localTID ) { 311 312 315 this.localTID = localTID; 316 if( localTID.longValue() != 0 ) 317 addLog(localTID,this); 318 319 } 320 321 337 synchronized java.lang.Object createSection( String sectionName ) { 338 339 CoordinatorLogSection result = null; 340 341 343 result = (CoordinatorLogSection) sectionMapping.get(sectionName); 344 if (result == null) { 345 int nameLength = sectionName.length(); 346 347 352 result = SectionPool.getCoordinatorLogSection(sectionName); 354 if( result == null ) { 355 } 356 357 359 else { 360 361 363 sectionMapping.put(sectionName,result); 364 } 365 } 366 367 return result; 368 } 369 370 382 383 synchronized boolean addObject( java.lang.Object sectionObj, 384 org.omg.CORBA.Object obj ) { 385 386 boolean result = true; 387 388 390 if( sectionObj != null ) { 391 CoordinatorLogSection section = (CoordinatorLogSection)sectionObj; 392 393 396 section.unwrittenEmpty = false; 398 if( section.unwrittenObjects == null ) 399 section.unwrittenObjects = new Vector(10,10); 400 401 403 String objRefStr = null; 404 try { 405 objRefStr = Configuration.getORB().object_to_string(obj); 406 407 409 section.unwrittenObjects.addElement(objRefStr); 410 411 } catch( Throwable exc ) { 422 result = false; 423 } 424 } else { 425 result = false; 426 } 427 428 return result; 429 } 430 431 444 445 synchronized boolean addData( java.lang.Object sectionObj, 446 byte[] data ) { 447 448 boolean result = true; 449 byte[] dataCopy; 450 451 453 if( sectionObj != null ) { 454 CoordinatorLogSection section = (CoordinatorLogSection)sectionObj; 455 456 459 section.unwrittenEmpty = false; 461 if( section.unwrittenData == null ) 462 section.unwrittenData = new Vector(4,4); 463 464 466 dataCopy = new byte[data.length]; 467 System.arraycopy(data,0,dataCopy,0,data.length); 468 469 471 section.unwrittenData.addElement(dataCopy); 472 473 475 } else { 484 result = false; 485 } 486 487 return result; 488 } 489 490 516 boolean write( boolean force ) { 517 518 520 boolean result = true; 521 try { 522 result = formatLogRecords(force); 523 } catch( IOException exc ) { 524 result = false; 525 } 526 527 return result; 528 } 529 530 542 private synchronized boolean requireRewrite() { 543 boolean result = true; 544 545 547 if( writeDone ) 548 rewriteRequired = true; 549 550 return result; 551 } 552 553 573 574 private boolean rewrite() { 575 576 boolean result = true; 577 578 581 if( rewriteRequired ) 582 try { 583 result = formatLogRecords(false); 584 } catch( IOException exc ) { 585 result = false; 586 } 587 588 return result; 589 } 590 591 608 private boolean reconstruct( DataInputStream dataInput ) 609 throws IOException { 610 611 boolean result = true; 612 613 615 int numSections = dataInput.readUnsignedShort(); 616 617 618 620 while( --numSections >= 0 ) { 621 622 625 int length = dataInput.readUnsignedShort(); 626 627 629 if( length > 0 ) { 630 631 int numObjects = dataInput.readUnsignedShort(); 632 int numData = dataInput.readUnsignedShort(); 633 634 636 byte[] stringData = new byte[length]; 637 dataInput.read(stringData); 638 String sectionName = new String (stringData); 639 640 642 CoordinatorLogSection section = (CoordinatorLogSection) createSection(sectionName); 643 644 646 if (numObjects > 0 && section.writtenObjects == null) { 649 section.writtenObjects = new Vector(numObjects, 10); 650 } 651 652 for( int i = 0; i < numObjects; i++ ) { 653 654 657 length = dataInput.readUnsignedShort(); 658 stringData = new byte[length]; 659 dataInput.read(stringData); 660 String objRefStr = new String (stringData); 661 662 664 section.writtenObjects.addElement(objRefStr); 665 } 666 667 669 if (numData > 0 && section.writtenData == null) { 672 section.writtenData = new Vector(numData, 4); 673 } 674 675 for( int i = 0; i < numData; i++ ) { 676 677 679 length = dataInput.readUnsignedShort(); 680 byte[] dataItem = new byte[length]; 681 682 685 dataInput.read(dataItem); 686 section.writtenData.addElement(dataItem); 687 } 688 } 689 } 690 691 return result; 692 } 693 694 702 java.lang.Object [] getObjects( java.lang.Object sectionObj ) { 703 java.lang.Object [] result = null; 704 705 710 if( sectionObj != null ) { 711 712 CoordinatorLogSection section = (CoordinatorLogSection)sectionObj; 713 714 int unwrittenSize = 0; 715 if( section.unwrittenObjects != null ) 716 unwrittenSize = section.unwrittenObjects.size(); 717 718 int writtenSize = 0; 719 if( section.writtenObjects != null ) 720 writtenSize = section.writtenObjects.size(); 721 722 result = new java.lang.Object [unwrittenSize + writtenSize]; 723 int currObject = 0; 724 725 727 ORB orb = Configuration.getORB(); 728 729 731 for( int i = 0; i < writtenSize; i++ ) { 732 733 org.omg.CORBA.Object obj = null; 734 String refStr = (String )section.writtenObjects.elementAt(i); 735 736 738 int retries = STRING_TO_REF_RETRIES; 739 boolean discard = false; 740 while( obj == null && retries-- > 0 && !discard ) { 741 try { 742 obj = orb.string_to_object(refStr); 743 } catch( MARSHAL exc ) { 744 747 try { 748 Thread.sleep(2000); 749 } catch( InterruptedException ex2 ) { 750 _logger.log(Level.WARNING, 751 "jts.wait_for_resync_complete_interrupted"); 752 String msg = LogFormatter.getLocalizedMessage(_logger, 753 "jts.wait_for_resync_complete_interrupted"); 754 throw new org.omg.CORBA.INTERNAL (msg); 755 } 756 } catch( Throwable exc ) { 757 759 discard = true; 760 } 761 } 762 763 765 if( !discard ){ 766 if( obj != null ){ 767 result[currObject++] = obj; 768 } 769 else { 770 _logger.log(Level.SEVERE, 771 "jts.unable_to_convert_object_reference_to_string_in_recovery"); 772 773 String msg = LogFormatter.getLocalizedMessage(_logger, 774 "jts.unable_to_convert_object_reference_to_string_in_recovery"); 775 776 throw new org.omg.CORBA.INTERNAL (msg); 777 } 778 } 779 } 780 781 784 for( int i = 0; i < unwrittenSize; i++ ) { 785 try { 786 787 789 org.omg.CORBA.Object obj = orb.string_to_object((String )section.unwrittenObjects.elementAt(i)); 790 result[currObject++] = obj; 791 } catch( Throwable exc ) { 792 } 795 } 796 } 797 798 return result; 799 } 800 801 809 810 byte[][] getData( java.lang.Object sectionObj ) { 811 812 byte[][] result = null; 813 814 818 if( sectionObj != null ) { 819 CoordinatorLogSection section = (CoordinatorLogSection)sectionObj; 820 821 int unwrittenSize = 0; 822 if( section.unwrittenData != null ) 823 unwrittenSize = section.unwrittenData.size(); 824 825 int writtenSize = 0; 826 if( section.writtenData != null ) 827 writtenSize = section.writtenData.size(); 828 829 result = new byte[unwrittenSize+writtenSize][]; 830 831 if( unwrittenSize > 0 ) 832 section.unwrittenData.copyInto(result); 833 834 for( int i = 0; i < writtenSize; i++ ) 835 result[unwrittenSize++] = (byte[])section.writtenData.elementAt(i); 836 } 837 838 return result; 839 } 840 841 842 853 synchronized void setLocalTID( Long localTID ) { 854 855 857 boolean addToMetaclass = (localTID.longValue() != 0 && (this.localTID == null || this.localTID.longValue() == 0)); 858 859 861 this.localTID = localTID; 862 if( addToMetaclass ) 863 addLog(localTID,this); 864 865 } 866 867 synchronized void setLocalTID( Long localTID, String logPath ) { 868 869 871 boolean addToMetaclass = (localTID.longValue() != 0 && (this.localTID == null || this.localTID.longValue() == 0)); 872 873 875 this.localTID = localTID; 876 if( addToMetaclass ) 877 addLog(localTID,this, logPath); 878 879 } 880 881 898 private void formatSection( CoordinatorLogSection section, 899 boolean rewrite, 900 DataOutputStream dataOutput ) 901 throws IOException { 902 907 if( section.unwrittenEmpty && 908 (!rewrite || section.writtenEmpty) ) { 909 dataOutput.writeShort(0); 910 return; 911 } 912 913 915 dataOutput.writeShort(section.sectionName.length()); 916 917 920 int unwrittenObjectsSize = 0; 921 int writtenObjectsSize = 0; 922 if( section.unwrittenObjects != null ) 923 unwrittenObjectsSize = section.unwrittenObjects.size(); 924 if( rewrite && 925 section.writtenObjects != null ) 926 writtenObjectsSize = section.writtenObjects.size(); 927 928 dataOutput.writeShort(unwrittenObjectsSize + writtenObjectsSize); 929 930 933 int unwrittenDataSize = 0; 934 int writtenDataSize = 0; 935 if( section.unwrittenData != null ) 936 unwrittenDataSize = section.unwrittenData.size(); 937 if( rewrite && 938 section.writtenData != null ) 939 writtenDataSize = section.writtenData.size(); 940 941 dataOutput.writeShort(unwrittenDataSize + writtenDataSize); 942 943 945 dataOutput.writeBytes(section.sectionName); 946 947 949 for( int i = 0; i < writtenObjectsSize; i++ ) { 950 String objRefStr = (String )section.writtenObjects.elementAt(i); 951 dataOutput.writeShort(objRefStr.length()); 952 dataOutput.writeBytes(objRefStr); 953 } 954 955 958 for( int i = 0; i < unwrittenObjectsSize; i++ ) { 959 String objRefStr = (String )section.unwrittenObjects.elementAt(i); 960 dataOutput.writeShort(objRefStr.length()); 961 dataOutput.writeBytes(objRefStr); 962 963 if( section.writtenObjects == null ) 964 section.writtenObjects = new Vector(unwrittenObjectsSize,10); 965 966 section.writtenObjects.addElement(objRefStr); 967 } 968 969 if( unwrittenObjectsSize > 0 ) 970 section.unwrittenObjects.removeAllElements(); 971 972 975 for( int i = 0; i < writtenDataSize; i++ ) { 976 byte[] dataItem = (byte[])section.writtenData.elementAt(i); 977 dataOutput.writeShort(dataItem.length); 978 dataOutput.write(dataItem); 979 } 980 981 984 for( int i = 0; i < unwrittenDataSize; i++ ) { 985 byte[] dataItem = (byte[])section.unwrittenData.elementAt(i); 986 dataOutput.writeShort(dataItem.length); 987 dataOutput.write(dataItem); 988 989 if( section.writtenData == null ) 990 section.writtenData = new Vector(unwrittenDataSize,4); 991 992 section.writtenData.addElement(dataItem); 993 } 994 if( unwrittenDataSize > 0 ) 995 section.unwrittenData.removeAllElements(); 996 997 1000 section.unwrittenEmpty = true; 1001 section.writtenEmpty = false; 1002 1003 } 1004 1005 1021 private boolean formatLogRecords( boolean forced ) 1022 throws IOException { 1023 1024 1027 if (logPath == null) 1028 openLog(); 1029 else 1030 openLog(logPath); 1031 if( logStateHolder.logFile == null ) { 1032 return false; 1033 } 1034 1035 1041 boolean result = false; 1042 try { 1043 logStateHolder.keypointLock.acquireReadLock(); 1044 1046 synchronized( this ) { 1047 1048 1050 byteOutput.reset(); 1051 dataOutput.writeLong(localTID.longValue()); 1052 1053 1055 dataOutput.writeShort(sectionMapping.size()); 1056 1057 1060 Enumeration sections = sectionMapping.elements(); 1061 int sz = sectionMapping.size(); while (sz-- > 0) { formatSection((CoordinatorLogSection)sections.nextElement(), 1064 rewriteRequired,dataOutput); 1065 } 1066 1067 1069 result = logStateHolder.logFile.write( forced ? LogFile.FORCED : LogFile.UNFORCED, 1070 byteOutput.toByteArray(), 1071 rewriteRequired ? LogFile.REWRITE : LogFile.NORMAL, 1072 null ); 1073 1074 rewriteRequired = false; 1075 writeDone = true; 1076 } 1077 } finally { 1078 logStateHolder.keypointLock.releaseReadLock(); 1079 } 1080 1081 return result; 1082 } 1083 1084 1092 static { 1093 1094 1096 String keypointCountEnv = Configuration.getPropertyValue(Configuration.KEYPOINT_COUNT); 1097 keypointTrigger = LOG_DEF_KEY_TRIGGER; 1098 if( keypointCountEnv != null ) 1099 try { 1100 keypointTrigger = Integer.parseInt(keypointCountEnv); 1101 } catch( Throwable e ) {} 1102 1103 } 1104 1105 1115 private static boolean openLog() { 1116 boolean result = false; 1117 String logName; 1118 CoordinatorLogStateHolder logStateHolder = defaultLogStateHolder; 1119 1120 1122 if( logStateHolder.log == null ) { 1123 logStateHolder.log = new Log(); 1124 if( !logStateHolder.log.initialise() ) { 1125 logStateHolder.log = null; 1126 _logger.log(Level.SEVERE,"jts.cannot_initialise_log"); 1127 String msg = LogFormatter.getLocalizedMessage(_logger, 1128 "jts.cannot_initialise_log"); 1129 throw new org.omg.CORBA.INTERNAL (msg); 1130 } 1131 } 1132 1133 1134 1137 1138 1142 String serverName = null; 1143 if( logStateHolder.log != null && 1144 logStateHolder.logFile == null && 1145 (serverName = Configuration.getServerName()) != null ) { 1146 1147 logStateHolder.logFile = logStateHolder.log.open(serverName, 1150 CoordinatorLogPool.getCoordinatorLog()); 1151 1152 if( logStateHolder.logFile == null ) { 1153 _logger.log(Level.SEVERE,"jts.cannot_open_log_file",serverName); 1154 String msg = LogFormatter.getLocalizedMessage(_logger, 1155 "jts.cannot_open_log_file"); 1156 throw new org.omg.CORBA.INTERNAL (msg); 1157 } else 1158 Configuration.setLogFile(logStateHolder.logFile); 1159 } 1160 1161 result = (logStateHolder.log != null && logStateHolder.logFile != null); 1162 1163 return result; 1164 } 1165 1166 1176 private static boolean openLog(String logPath) { 1177 boolean result = false; 1178 String logName; 1179 CoordinatorLogStateHolder logStateHolder = getStateHolder(logPath); 1180 1181 1183 if( logStateHolder.log == null ) { 1184 logStateHolder.log = new Log(logPath); 1185 if( !logStateHolder.log.initialise() ) { 1186 logStateHolder.log = null; 1187 _logger.log(Level.SEVERE,"jts.cannot_initialise_log"); 1188 String msg = LogFormatter.getLocalizedMessage(_logger, 1189 "jts.cannot_initialise_log"); 1190 throw new org.omg.CORBA.INTERNAL (msg); 1191 } 1192 } 1193 1194 1195 1198 1199 1203 String serverName = null; 1204 if( logStateHolder.log != null && 1205 logStateHolder.logFile == null && 1206 (serverName = Configuration.getServerName(logPath)) != null ) { 1207 1208 logStateHolder.logFile = logStateHolder.log.open(serverName, 1211 CoordinatorLogPool.getCoordinatorLog(logPath)); 1212 1213 if( logStateHolder.logFile == null ) { 1214 _logger.log(Level.SEVERE,"jts.cannot_open_log_file",serverName); 1215 String msg = LogFormatter.getLocalizedMessage(_logger, 1216 "jts.cannot_open_log_file"); 1217 throw new org.omg.CORBA.INTERNAL (msg); 1218 } else 1219 Configuration.setLogFile(logPath,logStateHolder.logFile); 1220 } 1221 1222 result = (logStateHolder.log != null && logStateHolder.logFile != null); 1223 1224 return result; 1225 } 1226 1227 1236 synchronized static Enumeration getLogged() { 1237 1238 Vector logRecords = null; 1239 Enumeration coordLogs = null; 1240 1241 1244 if( openLog() ) { 1245 CoordinatorLogStateHolder logStateHolder = defaultLogStateHolder; 1246 1247 1252 logRecords = logStateHolder.logFile.getLogRecords(); 1253 for( int i = 0; i < logRecords.size(); i++ ) { 1254 1255 1259 byte[] buffer = (byte[])logRecords.elementAt(i); 1260 ByteArrayInputStream byteInput = new ByteArrayInputStream(buffer); 1261 DataInputStream dataInput = new DataInputStream (byteInput); 1262 1263 try { 1264 Long localTID = new Long (dataInput.readLong()); 1265 CoordinatorLog coordLog = (CoordinatorLog)logStateHolder.activeLogs.get(localTID); 1266 if( coordLog == null ) { 1267 1268 coordLog = CoordinatorLogPool.getCoordinatorLog(); 1271 1272 coordLog.setLocalTID(localTID); 1273 } 1274 1275 1277 coordLog.reconstruct(dataInput); 1278 } catch( IOException exc ) { 1279 } 1280 } 1281 1282 1284 coordLogs = logStateHolder.activeLogs.elements(); 1285 } 1286 1287 1289 else 1290 coordLogs = new Hashtable().elements(); 1291 1292 1293 return coordLogs; 1294 } 1295 1296 1305 synchronized static Enumeration getLogged(String logPath) { 1306 1307 Vector logRecords = null; 1308 Enumeration coordLogs = null; 1309 1310 1313 if( openLog(logPath) ) { 1314 CoordinatorLogStateHolder logStateHolder = getStateHolder(logPath); 1315 1316 1321 logRecords = logStateHolder.logFile.getLogRecords(); 1322 for( int i = 0; i < logRecords.size(); i++ ) { 1323 1324 1328 byte[] buffer = (byte[])logRecords.elementAt(i); 1329 ByteArrayInputStream byteInput = new ByteArrayInputStream(buffer); 1330 DataInputStream dataInput = new DataInputStream (byteInput); 1331 1332 try { 1333 Long localTID = new Long (dataInput.readLong()); 1334 CoordinatorLog coordLog = (CoordinatorLog)logStateHolder.activeLogs.get(localTID); 1335 if( coordLog == null ) { 1336 1337 coordLog = CoordinatorLogPool.getCoordinatorLog(logPath); 1340 1341 coordLog.setLocalTID(localTID, logPath); 1342 } 1343 1344 1346 coordLog.reconstruct(dataInput); 1347 } catch( IOException exc ) { 1348 } 1349 } 1350 1351 1353 coordLogs = logStateHolder.activeLogs.elements(); 1354 } 1355 1356 1358 else 1359 coordLogs = new Hashtable().elements(); 1360 1361 1362 return coordLogs; 1363 } 1364 1365 1366 1376 private static boolean addLog(Long localTID, 1377 CoordinatorLog clog ) { 1378 CoordinatorLogStateHolder logStateHolder = defaultLogStateHolder; 1379 1380 boolean result = true; 1381 1382 logStateHolder.activeLogs.put(localTID,clog); 1383 1384 return result; 1385 } 1386 1387 private static boolean addLog(Long localTID, 1388 CoordinatorLog clog, String logPath ) { 1389 CoordinatorLogStateHolder logStateHolder = getStateHolder(logPath); 1390 1391 boolean result = true; 1392 1393 logStateHolder.activeLogs.put(localTID,clog); 1394 1395 return result; 1396 } 1397 1398 1406 synchronized static boolean removeLog( Long localTID ) { 1407 1408 boolean result = true; 1409 CoordinatorLogStateHolder logStateHolder = defaultLogStateHolder; 1410 1411 1416 CoordinatorLog clog = (CoordinatorLog)logStateHolder.activeLogs.remove(localTID); 1417 if( clog != null ) { 1418 1419 1422 synchronized( logStateHolder.keypointStateLock ) { 1423 1426 if( logStateHolder.keypointInProgress && logStateHolder.keypointLogs != null ) 1427 logStateHolder.keypointLogs.put(localTID,null); 1428 } 1429 1430 1432 if( clog.writeDone ) 1433 logStateHolder.tranCount++; 1434 1435 clog.reUse(); 1438 1439 1440 1443 if( logStateHolder.tranCount >= keypointTrigger ) { 1444 logStateHolder.tranCount = 0; 1445 keypoint(); 1446 } 1447 } 1448 1449 1450 return result; 1451 } 1452 1453 synchronized static boolean removeLog( Long localTID , String logPath) { 1454 1455 boolean result = true; 1456 CoordinatorLogStateHolder logStateHolder = getStateHolder(logPath); 1457 1458 1463 CoordinatorLog clog = (CoordinatorLog)logStateHolder.activeLogs.remove(localTID); 1464 if( clog != null ) { 1465 1466 1469 synchronized( logStateHolder.keypointStateLock ) { 1470 1473 if( logStateHolder.keypointInProgress && logStateHolder.keypointLogs != null ) 1474 logStateHolder.keypointLogs.put(localTID,null); 1475 } 1476 1477 1479 if( clog.writeDone ) 1480 logStateHolder.tranCount++; 1481 1482 clog.reUse(logPath); 1485 1486 1487 1490 if( logStateHolder.tranCount >= keypointTrigger ) { 1491 logStateHolder.tranCount = 0; 1492 keypoint(logPath); 1493 } 1494 } 1495 1496 1497 return result; 1498 } 1499 1500 1513 static void keypoint() { 1514 CoordinatorLogStateHolder logStateHolder = defaultLogStateHolder; 1515 1516 byte[] keypointEndRecord = { 1517 (byte) 'K', 1518 (byte) 'E', 1519 (byte) 'Y', 1520 (byte) 'E', 1521 (byte) 'N', 1522 (byte) 'D'}; 1523 1524 LogLSN previousLSN = new LogLSN(); 1525 LogLSN keypointStartLSN = new LogLSN(); 1526 boolean keypointRequired = false; 1527 1528 1533 try { 1534 logStateHolder.keypointLock.acquireWriteLock(); 1535 keypointRequired = startKeypoint(keypointStartLSN); 1536 } finally { 1537 logStateHolder.keypointLock.releaseWriteLock(); 1538 } 1539 1540 1542 if( keypointStartLSN.isNULL() ) { 1543 return; 1544 } 1545 1546 1552 if( keypointRequired ) { 1553 Enumeration keypointLocalTIDs = logStateHolder.keypointLogs.keys(); 1554 while( keypointLocalTIDs.hasMoreElements() ) 1555 1556 1562 synchronized( logStateHolder.keypointStateLock ) { 1563 CoordinatorLog currentLog = (CoordinatorLog)logStateHolder.keypointLogs.get(keypointLocalTIDs.nextElement()); 1564 1565 1567 if( currentLog != null ) 1568 currentLog.rewrite(); 1569 } 1570 } 1571 1572 1576 logStateHolder.logFile.write(LogFile.UNFORCED, 1577 keypointEndRecord, 1578 LogFile.KEYPOINT_END, 1579 previousLSN); 1580 1581 1586 logStateHolder.logFile.checkpoint(keypointStartLSN); 1587 1588 1591 logStateHolder.keypointInProgress = false; 1592 logStateHolder.keypointLogs.clear(); 1593 1594 } 1595 static void keypoint(String logPath) { 1596 CoordinatorLogStateHolder logStateHolder = getStateHolder(logPath); 1597 1598 byte[] keypointEndRecord = { 1599 (byte) 'K', 1600 (byte) 'E', 1601 (byte) 'Y', 1602 (byte) 'E', 1603 (byte) 'N', 1604 (byte) 'D'}; 1605 1606 LogLSN previousLSN = new LogLSN(); 1607 LogLSN keypointStartLSN = new LogLSN(); 1608 boolean keypointRequired = false; 1609 1610 1615 try { 1616 logStateHolder.keypointLock.acquireWriteLock(); 1617 keypointRequired = startKeypoint(keypointStartLSN, logPath); 1618 } finally { 1619 logStateHolder.keypointLock.releaseWriteLock(); 1620 } 1621 1622 1624 if( keypointStartLSN.isNULL() ) { 1625 return; 1626 } 1627 1628 1634 if( keypointRequired ) { 1635 Enumeration keypointLocalTIDs = logStateHolder.keypointLogs.keys(); 1636 while( keypointLocalTIDs.hasMoreElements() ) 1637 1638 1644 synchronized( logStateHolder.keypointStateLock ) { 1645 CoordinatorLog currentLog = (CoordinatorLog)logStateHolder.keypointLogs.get(keypointLocalTIDs.nextElement()); 1646 1647 1649 if( currentLog != null ) 1650 currentLog.rewrite(); 1651 } 1652 } 1653 1654 1658 logStateHolder.logFile.write(LogFile.UNFORCED, 1659 keypointEndRecord, 1660 LogFile.KEYPOINT_END, 1661 previousLSN); 1662 1663 1668 logStateHolder.logFile.checkpoint(keypointStartLSN); 1669 1670 1673 logStateHolder.keypointInProgress = false; 1674 logStateHolder.keypointLogs.clear(); 1675 1676 } 1677 1678 1688 1689 public void upcall( int reason ){ 1690 1691 if (logPath == null) 1693 CoordinatorLog.keypoint(); 1694 else 1695 CoordinatorLog.keypoint(logPath); 1696 1697 } 1698 1699 1707 1708 synchronized static void finalizeAll(){ 1709 CoordinatorLogStateHolder logStateHolder = defaultLogStateHolder; 1710 1711 boolean deleteFile = false; 1712 1713 1715 synchronized( logStateHolder.keypointStateLock ) { 1716 1717 1719 if( logStateHolder.activeLogs != null ) { 1720 1721 1724 if( logStateHolder.activeLogs.size() == 0 ) 1725 deleteFile = true; 1726 logStateHolder.activeLogs.clear(); 1727 logStateHolder.activeLogs = null; 1728 } 1729 1730 if( logStateHolder.logFile != null ) logStateHolder.logFile.close(deleteFile); 1731 logStateHolder.logFile = null; 1732 1733 1735 if( logStateHolder.keypointLogs != null ) 1736 logStateHolder.keypointLogs.clear(); 1737 logStateHolder.keypointLogs = null; 1738 } 1739 1740 1742 logStateHolder.keypointStateLock = null; 1743 logStateHolder.keypointLock = null; 1744 1745 } 1746 1747 synchronized static void finalizeAll(String logPath){ 1748 CoordinatorLogStateHolder logStateHolder = getStateHolder(logPath); 1749 1750 boolean deleteFile = false; 1751 1752 1754 synchronized( logStateHolder.keypointStateLock ) { 1755 1756 1758 if( logStateHolder.activeLogs != null ) { 1759 1760 1763 if( logStateHolder.activeLogs.size() == 0 ) 1764 deleteFile = true; 1765 logStateHolder.activeLogs.clear(); 1766 logStateHolder.activeLogs = null; 1767 } 1768 1769 if( logStateHolder.logFile != null ) logStateHolder.logFile.close(deleteFile); 1770 logStateHolder.logFile = null; 1771 1772 1774 if( logStateHolder.keypointLogs != null ) 1775 logStateHolder.keypointLogs.clear(); 1776 logStateHolder.keypointLogs = null; 1777 } 1778 1779 1781 logStateHolder.keypointStateLock = null; 1782 logStateHolder.keypointLock = null; 1783 1784 } 1785 1786 1794 1795 synchronized static boolean startKeypoint( LogLSN keypointStartLSN ) { 1796 CoordinatorLogStateHolder logStateHolder = defaultLogStateHolder; 1797 1798 boolean keypointRequired = false; 1799 1800 1802 if( logStateHolder.keypointInProgress ) { 1803 return false; 1804 } 1805 logStateHolder.keypointInProgress = true; 1806 1807 1810 if( !openLog() ) { 1811 logStateHolder.keypointInProgress = false; 1812 return false; 1813 } 1814 1815 1818 if( logStateHolder.activeLogs.size() == 0 ) 1819 keypointRequired = false; 1820 1821 1826 else { 1827 1828 1832 Enumeration clogs = logStateHolder.activeLogs.elements(); 1833 while( clogs.hasMoreElements() ) { 1834 CoordinatorLog currentLog = (CoordinatorLog)clogs.nextElement(); 1835 Long localTID = currentLog.localTID; 1836 1837 currentLog.requireRewrite(); 1838 logStateHolder.keypointLogs.put(localTID,currentLog); 1839 } 1840 keypointRequired = logStateHolder.keypointLogs.size() > 0; 1841 } 1842 1843 1846 byte[] keypointStartRecord = {(byte) 'K', 1847 (byte) 'E', 1848 (byte) 'Y', 1849 (byte) 'S', 1850 (byte) 'T', 1851 (byte) 'A', 1852 (byte) 'R', 1853 (byte) 'T'}; 1854 logStateHolder.logFile.write(LogFile.UNFORCED, 1855 keypointStartRecord, 1856 LogFile.KEYPOINT_START, 1857 keypointStartLSN); 1858 1859 return keypointRequired; 1860 } 1861 1862 synchronized static boolean startKeypoint( LogLSN keypointStartLSN, String logPath ) { 1863 CoordinatorLogStateHolder logStateHolder = getStateHolder(logPath); 1864 1865 boolean keypointRequired = false; 1866 1867 1869 if( logStateHolder.keypointInProgress ) { 1870 return false; 1871 } 1872 logStateHolder.keypointInProgress = true; 1873 1874 1877 if( !openLog(logPath) ) { 1878 logStateHolder.keypointInProgress = false; 1879 return false; 1880 } 1881 1882 1885 if( logStateHolder.activeLogs.size() == 0 ) 1886 keypointRequired = false; 1887 1888 1893 else { 1894 1895 1899 Enumeration clogs = logStateHolder.activeLogs.elements(); 1900 while( clogs.hasMoreElements() ) { 1901 CoordinatorLog currentLog = (CoordinatorLog)clogs.nextElement(); 1902 Long localTID = currentLog.localTID; 1903 1904 currentLog.requireRewrite(); 1905 logStateHolder.keypointLogs.put(localTID,currentLog); 1906 } 1907 keypointRequired = logStateHolder.keypointLogs.size() > 0; 1908 } 1909 1910 1913 byte[] keypointStartRecord = {(byte) 'K', 1914 (byte) 'E', 1915 (byte) 'Y', 1916 (byte) 'S', 1917 (byte) 'T', 1918 (byte) 'A', 1919 (byte) 'R', 1920 (byte) 'T'}; 1921 logStateHolder.logFile.write(LogFile.UNFORCED, 1922 keypointStartRecord, 1923 LogFile.KEYPOINT_START, 1924 keypointStartLSN); 1925 1926 return keypointRequired; 1927 } 1928 1929 1937 1938 static void dumpClass() { 1939 CoordinatorLogStateHolder logStateHolder = defaultLogStateHolder; 1941 1942 logStateHolder.log.dump(); 1943 logStateHolder.logFile.dump(); 1944 } 1945 1946 1954 1955 void dump() { 1956 CoordinatorLogStateHolder logStateHolder = defaultLogStateHolder; 1957 1958 1960 1962 1964 if( sectionMapping != null ) { 1965 1967 Enumeration sections = sectionMapping.elements(); 1968 while( sections.hasMoreElements() ) { 1969 CoordinatorLogSection section = (CoordinatorLogSection)sections.nextElement(); 1970 1971 1973 1975 1977 if( section.writtenObjects != null ) { 1978 1980 Enumeration objects = section.writtenObjects.elements(); 1981 while( objects.hasMoreElements() ) { 1982 String objStr = (String )objects.nextElement(); 1983 1984 } 1986 1987 } 1989 1990 1992 if( section.unwrittenObjects != null ) { 1993 1995 Enumeration objects = section.unwrittenObjects.elements(); 1996 while( objects.hasMoreElements() ) { 1997 String objStr = (String )objects.nextElement(); 1998 1999 } 2001 2002 } 2004 2005 2007 if( section.writtenData != null ) { 2008 2010 Enumeration data = section.writtenData.elements(); 2011 while( data.hasMoreElements() ) { 2012 byte[] dataItem = (byte[])data.nextElement(); 2013 2014 } 2016 2017 } 2019 2020 2022 if( section.unwrittenData != null ) { 2023 2025 Enumeration data = section.unwrittenData.elements(); 2026 while( data.hasMoreElements() ) { 2027 byte[] dataItem = (byte[])data.nextElement(); 2028 2029 } 2031 2032 } 2034 2035 2036 2038 } 2039 2040 } 2042 } 2043 public static void setKeypointTrigger(int keypoint) 2045 { 2046 keypointTrigger = keypoint; 2047 } 2048 2050} 2051 2052 2060 2066class CoordinatorLogSection extends java.lang.Object { 2067 String sectionName = null; 2068 boolean unwrittenEmpty = true; 2069 boolean writtenEmpty = true; 2070 Vector unwrittenObjects = null; 2071 Vector unwrittenData = null; 2072 Vector writtenObjects = null; 2073 Vector writtenData = null; 2074 int headerLength = 0; 2075 2076 2084 CoordinatorLogSection( String sectionName ) { 2085 this.sectionName = sectionName; 2086 } 2087 2088 2096 public void finalize() { 2097 if( unwrittenObjects != null ) 2098 unwrittenObjects.removeAllElements(); 2099 2100 if( unwrittenData != null ) 2101 unwrittenData.removeAllElements(); 2102 2103 if( writtenObjects != null ) 2104 writtenObjects.removeAllElements(); 2105 2106 if( writtenData != null ) 2107 writtenData.removeAllElements(); 2108 2109 sectionName = null; 2110 2111 unwrittenObjects = null; 2112 unwrittenData = null; 2113 writtenObjects = null; 2114 writtenData = null; 2115 } 2116 2117 2134 synchronized void reUse() { 2136 if( unwrittenObjects != null ) 2137 unwrittenObjects.removeAllElements(); 2138 2139 if( unwrittenData != null ) 2140 unwrittenData.removeAllElements(); 2141 2142 if( writtenObjects != null ) 2143 writtenObjects.removeAllElements(); 2144 2145 if( writtenData != null ) 2146 writtenData.removeAllElements(); 2147 2148 sectionName = null; 2149 unwrittenEmpty = true; 2150 writtenEmpty = true; 2151 headerLength = 0; 2152 2153 SectionPool.putCoordinatorLogSection(this); 2154 } 2155} 2156 2157 2170 2171class SectionPool { 2172 2173 private Stack pool; 2174 private static final int MAXSTACKSIZE = 15; 2175 2176 static SectionPool SPool = new SectionPool(); 2177 2178 2179 public SectionPool() { 2180 pool = new Stack(); 2181 } 2182 2183 2194 public static synchronized 2195 CoordinatorLogSection getCoordinatorLogSection(String name) { 2196 2197 CoordinatorLogSection cls; 2198 if (SPool.pool.empty()) { 2199 return new CoordinatorLogSection(name); 2200 } 2201 else { 2202 cls = (CoordinatorLogSection) SPool.pool.pop(); 2203 cls.sectionName = name; 2204 return cls; 2205 } 2206 } 2207 2208 2216 public static void putCoordinatorLogSection(CoordinatorLogSection cls) { 2217 if (SPool.pool.size() <= MAXSTACKSIZE) { 2218 SPool.pool.push(cls); 2219 } 2220 } 2221 2222} 2223 2224class CoordinatorLogStateHolder { 2225 LogFile logFile = null; 2226 Log log = null; 2227 Hashtable activeLogs = null; 2228 Hashtable keypointLogs = null; 2229 int tranCount = 0; 2230 boolean keypointInProgress = false; 2231 RWLock keypointLock = null; 2233 java.lang.Object keypointStateLock = null; 2234} 2235 | Popular Tags |