1 21 22 package org.apache.derby.impl.store.raw.data; 23 24 25 import org.apache.derby.iapi.reference.SQLState; 26 import org.apache.derby.iapi.reference.MessageId; 27 28 import org.apache.derby.impl.store.raw.data.AllocationActions; 29 import org.apache.derby.impl.store.raw.data.BaseContainerHandle; 30 import org.apache.derby.impl.store.raw.data.BasePage; 31 import org.apache.derby.impl.store.raw.data.DirectActions; 32 import org.apache.derby.impl.store.raw.data.LoggableActions; 33 import org.apache.derby.impl.store.raw.data.PageActions; 34 import org.apache.derby.impl.store.raw.data.RecordId; 35 import org.apache.derby.impl.store.raw.data.ReclaimSpace; 36 37 import org.apache.derby.iapi.services.info.ProductVersionHolder; 38 import org.apache.derby.iapi.services.info.ProductGenusNames; 39 40 import org.apache.derby.iapi.services.cache.CacheFactory; 41 import org.apache.derby.iapi.services.cache.CacheManager; 42 import org.apache.derby.iapi.services.cache.Cacheable; 43 import org.apache.derby.iapi.services.cache.CacheableFactory; 44 import org.apache.derby.iapi.services.context.ContextManager; 45 import org.apache.derby.iapi.services.daemon.DaemonService; 46 import org.apache.derby.iapi.services.daemon.Serviceable; 47 import org.apache.derby.iapi.services.monitor.ModuleControl; 48 import org.apache.derby.iapi.services.monitor.ModuleSupportable; 49 import org.apache.derby.iapi.services.monitor.Monitor; 50 import org.apache.derby.iapi.services.monitor.PersistentService; 51 import org.apache.derby.iapi.services.diag.Performance; 52 import org.apache.derby.iapi.services.sanity.SanityManager; 53 import org.apache.derby.iapi.services.io.FormatIdUtil; 54 import org.apache.derby.iapi.services.stream.HeaderPrintWriter; 55 56 import org.apache.derby.iapi.error.StandardException; 57 import org.apache.derby.iapi.services.i18n.MessageService; 58 import org.apache.derby.iapi.store.access.AccessFactoryGlobals; 59 import org.apache.derby.iapi.store.access.FileResource; 60 import org.apache.derby.iapi.store.access.TransactionController; 61 import org.apache.derby.iapi.store.raw.data.DataFactory; 62 import org.apache.derby.iapi.store.raw.data.RawContainerHandle; 63 import org.apache.derby.iapi.store.raw.log.LogFactory; 64 import org.apache.derby.iapi.store.raw.log.LogInstant; 65 import org.apache.derby.iapi.store.raw.ContainerHandle; 66 import org.apache.derby.iapi.store.raw.ContainerKey; 67 import org.apache.derby.iapi.store.raw.LockingPolicy; 68 import org.apache.derby.iapi.store.raw.Page; 69 import org.apache.derby.iapi.store.raw.RawStoreFactory; 70 import org.apache.derby.iapi.store.raw.RecordHandle; 71 import org.apache.derby.iapi.store.raw.StreamContainerHandle; 72 import org.apache.derby.iapi.store.raw.Transaction; 73 import org.apache.derby.iapi.store.raw.xact.RawTransaction; 74 75 import org.apache.derby.iapi.store.access.RowSource; 76 77 import org.apache.derby.io.StorageFactory; 78 import org.apache.derby.io.WritableStorageFactory; 79 import org.apache.derby.io.StorageFile; 80 import org.apache.derby.io.StorageRandomAccessFile; 81 import org.apache.derby.iapi.services.uuid.UUIDFactory; 82 import org.apache.derby.catalog.UUID; 83 import org.apache.derby.iapi.reference.Attribute; 84 import org.apache.derby.iapi.reference.Property; 85 import org.apache.derby.iapi.reference.SQLState; 86 import org.apache.derby.iapi.util.ByteArray; 87 import org.apache.derby.iapi.services.io.FileUtil; 88 import org.apache.derby.iapi.util.CheapDateFormatter; 89 import org.apache.derby.iapi.util.ReuseFactory; 90 import org.apache.derby.iapi.services.property.PropertyUtil; 91 92 import java.util.Properties ; 93 import java.util.Hashtable ; 94 import java.util.Enumeration ; 95 96 import java.io.File ; 97 import java.io.FilePermission ; 98 import java.io.OutputStream ; 99 import java.io.IOException ; 100 101 import java.security.AccessController ; 102 import java.security.PrivilegedAction ; 103 import java.security.PrivilegedExceptionAction ; 104 import java.security.PrivilegedActionException ; 105 106 124 125 public final class BaseDataFileFactory 126 implements DataFactory, CacheableFactory, ModuleControl, ModuleSupportable, PrivilegedExceptionAction 127 { 128 129 private String subSubProtocol; 130 StorageFactory storageFactory; 131 132 136 WritableStorageFactory writableStorageFactory; 137 138 private long nextContainerId = System.currentTimeMillis(); 139 private boolean databaseEncrypted; 140 141 private CacheManager pageCache; 142 private CacheManager containerCache; 143 144 private LogFactory logFactory; 145 146 private ProductVersionHolder jbmsVersion; 147 148 private RawStoreFactory rawStoreFactory; 150 private String dataDirectory; 152 private boolean throwDBlckException; 157 private UUID identifier; 159 private Object freezeSemaphore; 160 161 162 private boolean isFrozen; 164 165 166 private int writersInProgress; 169 170 171 private boolean removeStubsOK; 172 private boolean isCorrupt; 173 174 private boolean inCreateNoLog; 176 177 private StorageRandomAccessFile fileLockOnDB; 179 private StorageFile exFileLock; private HeaderPrintWriter istream; 181 private static final String LINE = 182 "----------------------------------------------------------------"; 183 184 boolean dataNotSyncedAtAllocation = true; 187 188 boolean dataNotSyncedAtCheckpoint = false; 190 191 private PageActions loggablePageActions; 194 private AllocationActions loggableAllocActions; 195 196 private boolean readOnly; private boolean supportsRandomAccess; 198 private FileResource fileHandler; 201 202 private Hashtable droppedTableStubInfo; 204 205 private Hashtable postRecoveryRemovedFiles; 206 207 private EncryptData containerEncrypter; 208 209 210 private int actionCode; 212 private static final int GET_TEMP_DIRECTORY_ACTION = 1; 213 private static final int REMOVE_TEMP_DIRECTORY_ACTION = 2; 214 private static final int GET_CONTAINER_PATH_ACTION = 3; 215 private static final int GET_ALTERNATE_CONTAINER_PATH_ACTION = 4; 216 private static final int FIND_MAX_CONTAINER_ID_ACTION = 5; 217 private static final int DELETE_IF_EXISTS_ACTION = 6; 218 private static final int GET_PATH_ACTION = 7; 219 private static final int POST_RECOVERY_REMOVE_ACTION = 8; 220 private static final int REMOVE_STUBS_ACTION = 9; 221 private static final int BOOT_ACTION = 10; 222 private static final int GET_LOCK_ON_DB_ACTION = 11; 223 private static final int RELEASE_LOCK_ON_DB_ACTION = 12; 224 private static final int RESTORE_DATA_DIRECTORY_ACTION = 13; 225 private static final int GET_CONTAINER_NAMES_ACTION = 14; 226 227 private ContainerKey containerId; 228 private boolean stub; 229 private StorageFile actionFile; 230 private UUID myUUID; 231 private UUIDFactory uuidFactory; 232 private String databaseDirectory; 233 234 private String backupPath; 235 private File backupRoot; 236 private String [] bfilelist; 237 238 241 242 public BaseDataFileFactory() 243 { 244 } 245 246 249 250 public boolean canSupport(Properties startParams) 251 { 252 253 String serviceType = startParams.getProperty(PersistentService.TYPE); 254 if (serviceType == null) 255 return false; 256 257 if (!handleServiceType(serviceType)) 258 return false; 259 260 if (startParams.getProperty(PersistentService.ROOT) == null) 261 return false; 262 263 return true; 264 } 265 266 public void boot(boolean create, Properties startParams) 267 throws StandardException 268 { 269 270 jbmsVersion = Monitor.getMonitor().getEngineVersion(); 271 272 dataDirectory = startParams.getProperty(PersistentService.ROOT); 273 274 UUIDFactory uf = Monitor.getMonitor().getUUIDFactory(); 275 276 identifier = uf.createUUID(); 277 278 PersistentService ps = Monitor.getMonitor().getServiceType(this); 279 280 try 281 { 282 storageFactory = 283 ps.getStorageFactoryInstance( 284 true, 285 dataDirectory, 286 startParams.getProperty( 287 Property.STORAGE_TEMP_DIRECTORY, 288 PropertyUtil.getSystemProperty( 289 Property.STORAGE_TEMP_DIRECTORY)), 290 identifier.toANSIidentifier()); 291 } 292 catch(IOException ioe) 293 { 294 if (create) 295 { 296 throw StandardException.newException( 297 SQLState.SERVICE_DIRECTORY_CREATE_ERROR, 298 ioe, dataDirectory); 299 } 300 else 301 { 302 throw StandardException.newException( 303 SQLState.DATABASE_NOT_FOUND, ioe, dataDirectory); 304 } 305 } 306 307 if (storageFactory instanceof WritableStorageFactory) 308 writableStorageFactory = (WritableStorageFactory) storageFactory; 309 310 actionCode = BOOT_ACTION; 311 312 try 313 { 314 AccessController.doPrivileged( this); 315 } 316 catch (PrivilegedActionException pae) 317 { 318 } 320 321 String value = 322 startParams.getProperty(Property.FORCE_DATABASE_LOCK, 323 PropertyUtil.getSystemProperty(Property.FORCE_DATABASE_LOCK)); 324 throwDBlckException = 325 Boolean.valueOf( 326 (value != null ? value.trim() : value)).booleanValue(); 327 328 if (!isReadOnly()) getJBMSLockOnDB(identifier, uf, dataDirectory); 330 331 332 String restoreFrom =null; 335 restoreFrom = startParams.getProperty(Attribute.CREATE_FROM); 336 if(restoreFrom == null) 337 restoreFrom = startParams.getProperty(Attribute.RESTORE_FROM); 338 if(restoreFrom == null) 339 restoreFrom = startParams.getProperty(Attribute.ROLL_FORWARD_RECOVERY_FROM); 340 341 if (restoreFrom !=null) 342 { 343 try 344 { 345 String dataEncryption = 348 startParams.getProperty(Attribute.DATA_ENCRYPTION); 349 databaseEncrypted = Boolean.valueOf(dataEncryption).booleanValue(); 350 restoreDataDirectory(restoreFrom); 351 } 352 catch(StandardException se) 353 { 354 releaseJBMSLockOnDB(); 355 throw se; 356 } 357 } 358 359 logMsg(LINE); 360 long bootTime = System.currentTimeMillis(); 361 String readOnlyMsg = (isReadOnly()) 362 ? MessageService.getTextMessage(MessageId.STORE_BOOT_READONLY_MSG) 363 : ""; 364 365 logMsg(CheapDateFormatter.formatDate(bootTime) + 366 MessageService.getTextMessage(MessageId.STORE_BOOT_MSG, 367 jbmsVersion, 368 identifier, 369 dataDirectory, 370 readOnlyMsg)); 371 372 uf = null; 373 374 375 376 CacheFactory cf = (CacheFactory) 377 Monitor.startSystemModule( 378 org.apache.derby.iapi.reference.Module.CacheFactory); 379 380 int pageCacheSize = getIntParameter( 381 RawStoreFactory.PAGE_CACHE_SIZE_PARAMETER, 382 null, 383 RawStoreFactory.PAGE_CACHE_SIZE_DEFAULT, 384 RawStoreFactory.PAGE_CACHE_SIZE_MINIMUM, 385 RawStoreFactory.PAGE_CACHE_SIZE_MAXIMUM); 386 387 pageCache = 388 cf.newCacheManager(this, 389 "PageCache", 390 pageCacheSize / 2, 391 pageCacheSize); 392 393 int fileCacheSize = getIntParameter( 394 "derby.storage.fileCacheSize", 395 null, 396 100, 397 2, 398 100); 399 400 containerCache = 401 cf.newCacheManager( 402 this, "ContainerCache", fileCacheSize / 2, fileCacheSize); 403 404 if (create) 405 { 406 String noLog = 407 startParams.getProperty(Property.CREATE_WITH_NO_LOG); 408 409 inCreateNoLog = 410 (noLog != null && Boolean.valueOf(noLog).booleanValue()); 411 412 } 413 414 freezeSemaphore = new Object (); 415 416 droppedTableStubInfo = new Hashtable (); 417 418 if (Property.DURABILITY_TESTMODE_NO_SYNC.equalsIgnoreCase( 422 PropertyUtil.getSystemProperty(Property.DURABILITY_PROPERTY))) 423 { 424 dataNotSyncedAtCheckpoint = true; 426 427 Monitor.logMessage(MessageService.getTextMessage( 431 MessageId.STORE_DURABILITY_TESTMODE_NO_SYNC, 432 Property.DURABILITY_PROPERTY, 433 Property.DURABILITY_TESTMODE_NO_SYNC)); 434 } 435 else if (Performance.MEASURE) 436 { 437 443 dataNotSyncedAtCheckpoint = 445 PropertyUtil.getSystemBoolean( 446 Property.STORAGE_DATA_NOT_SYNCED_AT_CHECKPOINT); 447 448 if (dataNotSyncedAtCheckpoint) 449 Monitor.logMessage( 450 "Warning: " + 451 Property.STORAGE_DATA_NOT_SYNCED_AT_CHECKPOINT + 452 "set to true."); 453 } 454 455 fileHandler = new RFResource( this); 456 } 458 public void stop() 459 { 460 boolean OK = false; 461 462 if (rawStoreFactory != null) 463 { 464 DaemonService rawStoreDaemon = rawStoreFactory.getDaemon(); 465 if (rawStoreDaemon != null) 466 rawStoreDaemon.stop(); 467 } 468 469 long shutdownTime = System.currentTimeMillis(); 470 logMsg("\n" + CheapDateFormatter.formatDate(shutdownTime) + 471 MessageService.getTextMessage( 472 MessageId.STORE_SHUTDOWN_MSG, 473 getIdentifier())); 474 istream.println(LINE); 475 476 if (!isCorrupt) 477 { 478 try 479 { 480 if (pageCache != null && containerCache != null) 481 { 482 pageCache.shutdown(); 483 containerCache.shutdown(); 484 485 OK = true; 486 } 487 488 } 489 catch (StandardException se) 490 { 491 se.printStackTrace(istream.getPrintWriter()); 492 } 493 } 494 495 removeTempDirectory(); 496 497 if (isReadOnly()) { 499 return; 500 } 501 502 503 if (removeStubsOK && OK) 506 removeStubs(); 507 508 releaseJBMSLockOnDB(); 509 } 511 514 public Cacheable newCacheable(CacheManager cm) 515 { 516 if (cm == pageCache) 517 { 518 StoredPage sp = new StoredPage(); 519 sp.setFactory(this); 520 return sp; 521 } 522 523 return newContainerObject(); 525 } 526 527 532 public void createFinished() throws StandardException 533 { 534 if (!inCreateNoLog) 535 { 536 throw StandardException.newException( 537 SQLState.FILE_DATABASE_NOT_IN_CREATE); 538 } 539 540 checkpoint(); 542 inCreateNoLog = false; 543 } 544 545 548 549 public ContainerHandle openContainer( 550 RawTransaction t, 551 ContainerKey containerId, 552 LockingPolicy locking, 553 int mode) 554 throws StandardException 555 { 556 return openContainer( 557 t, containerId, locking, mode, false ); 558 } 559 560 561 565 public RawContainerHandle openDroppedContainer( 566 RawTransaction t, 567 ContainerKey containerId, 568 LockingPolicy locking, 569 int mode) 570 throws StandardException 571 { 572 mode |= ContainerHandle.MODE_NO_ACTIONS_ON_COMMIT; 575 576 return openContainer( 577 t, containerId, locking, mode, true ); 578 } 579 580 584 private RawContainerHandle openContainer( 585 RawTransaction t, 586 ContainerKey identity, 587 LockingPolicy locking, 588 int mode, 589 boolean droppedOK) 590 throws StandardException 591 { 592 593 if (SanityManager.DEBUG) 594 { 595 596 if ((mode & (ContainerHandle.MODE_READONLY | ContainerHandle.MODE_FORUPDATE)) 597 == (ContainerHandle.MODE_READONLY | ContainerHandle.MODE_FORUPDATE)) 598 { 599 SanityManager.THROWASSERT("update and readonly mode specified"); 600 } 601 602 } 603 604 boolean waitForLock = ((mode & ContainerHandle.MODE_LOCK_NOWAIT) == 0); 605 606 607 if ((mode & ContainerHandle.MODE_OPEN_FOR_LOCK_ONLY) != 0) 608 { 609 BaseContainerHandle lockOnlyHandle = 613 new BaseContainerHandle( 614 getIdentifier(), t, identity, locking, mode); 615 616 if (lockOnlyHandle.useContainer(true, waitForLock)) 617 return lockOnlyHandle; 618 else 619 return null; 620 } 621 622 623 BaseContainerHandle c; 624 625 FileContainer container = (FileContainer) containerCache.find(identity); 627 if (container == null) 628 return null; 629 630 if (identity.getSegmentId() == ContainerHandle.TEMPORARY_SEGMENT) 631 { 632 633 if (SanityManager.DEBUG) 634 { 635 SanityManager.ASSERT(container instanceof TempRAFContainer); 636 } 637 638 if ((mode & ContainerHandle.MODE_TEMP_IS_KEPT) == 639 ContainerHandle.MODE_TEMP_IS_KEPT) 640 { 641 mode |= ContainerHandle.MODE_UNLOGGED; 643 } 644 else 645 { 646 mode |= 648 (ContainerHandle.MODE_UNLOGGED | 649 ContainerHandle.MODE_TRUNCATE_ON_ROLLBACK); 650 } 651 652 locking = 653 t.newLockingPolicy( 654 LockingPolicy.MODE_NONE, 655 TransactionController.ISOLATION_NOLOCK, true); 656 } 657 else 658 { 659 if (inCreateNoLog) 661 { 662 mode |= 663 (ContainerHandle.MODE_UNLOGGED | 664 ContainerHandle.MODE_CREATE_UNLOGGED); 665 } else { 666 667 if (logFactory.logArchived()) { 670 mode &= ~(ContainerHandle.MODE_UNLOGGED | 671 ContainerHandle.MODE_CREATE_UNLOGGED); 672 673 } else { 674 675 680 if (((mode & ContainerHandle.MODE_UNLOGGED) == 681 ContainerHandle.MODE_UNLOGGED) || 682 ((mode & ContainerHandle.MODE_CREATE_UNLOGGED) == 683 ContainerHandle.MODE_CREATE_UNLOGGED)) 684 { 685 if (!t.blockBackup(false)) { 686 mode &= ~(ContainerHandle.MODE_UNLOGGED | 690 ContainerHandle.MODE_CREATE_UNLOGGED); 691 } 692 } 693 694 } 695 696 } 697 698 if (((mode & ContainerHandle.MODE_UNLOGGED) == 703 ContainerHandle.MODE_UNLOGGED) && 704 ((mode & ContainerHandle.MODE_CREATE_UNLOGGED) == 0)) 705 { 706 mode |= ContainerHandle.MODE_FLUSH_ON_COMMIT; 707 } 708 } 709 710 PageActions pageActions = null; 711 AllocationActions allocActions = null; 712 713 if ((mode & ContainerHandle.MODE_FORUPDATE) == 714 ContainerHandle.MODE_FORUPDATE) 715 { 716 717 if ((mode & ContainerHandle.MODE_UNLOGGED) == 0) 718 { 719 pageActions = getLoggablePageActions(); 721 allocActions = getLoggableAllocationActions(); 722 723 } 724 else 725 { 726 pageActions = new DirectActions(); 728 allocActions = new DirectAllocActions(); 729 } 730 } 731 732 c = new BaseContainerHandle( 733 getIdentifier(), t, pageActions, 734 allocActions, locking, container, mode); 735 736 try 738 { 739 if (!c.useContainer(droppedOK, waitForLock)) 740 { 741 containerCache.release(container); 742 return null; 743 } 744 } 745 catch (StandardException se) 746 { 747 containerCache.release(container); 748 throw se; 749 } 750 751 return c; 752 } 753 754 757 public long addContainer( 758 RawTransaction t, 759 long segmentId, 760 long input_containerid, 761 int mode, 762 Properties tableProperties, 763 int temporaryFlag) 764 throws StandardException 765 { 766 if (SanityManager.DEBUG) 767 { 768 if ((mode & ContainerHandle.MODE_CREATE_UNLOGGED) != 0) 769 SanityManager.ASSERT( 770 (mode & ContainerHandle.MODE_UNLOGGED) != 0, 771 "cannot have CREATE_UNLOGGED set but UNLOGGED not set"); 772 } 773 774 long containerId = 777 ((input_containerid != ContainerHandle.DEFAULT_ASSIGN_ID) ? 778 input_containerid : getNextId()); 779 780 ContainerKey identity = new ContainerKey(segmentId, containerId); 781 782 boolean tmpContainer = (segmentId == ContainerHandle.TEMPORARY_SEGMENT); 783 784 ContainerHandle ch = null; 785 LockingPolicy cl = null; 786 787 if (!tmpContainer) 788 { 789 791 if (isReadOnly()) 792 { 793 throw StandardException.newException( 794 SQLState.DATA_CONTAINER_READ_ONLY); 795 } 796 797 cl = t.newLockingPolicy(LockingPolicy.MODE_CONTAINER, 798 TransactionController.ISOLATION_SERIALIZABLE, true); 799 800 if (SanityManager.DEBUG) 801 SanityManager.ASSERT(cl != null); 802 803 ch = t.openContainer(identity, cl, 804 (ContainerHandle.MODE_FORUPDATE | 805 ContainerHandle.MODE_OPEN_FOR_LOCK_ONLY)); 806 } 807 808 FileContainer container = 809 (FileContainer) containerCache.create(identity, tableProperties); 810 811 ContainerHandle containerHdl = null; 815 Page firstPage = null; 816 817 try 818 { 819 if (tmpContainer && 822 ((temporaryFlag & TransactionController.IS_KEPT) == 823 TransactionController.IS_KEPT)) 824 { 825 826 mode |= ContainerHandle.MODE_TEMP_IS_KEPT; 827 } 828 829 containerHdl = 831 t.openContainer( 832 identity, null, (ContainerHandle.MODE_FORUPDATE | mode)); 833 834 if (SanityManager.DEBUG) 836 SanityManager.ASSERT(containerHdl != null); 837 838 if (!tmpContainer) 839 { 840 RawContainerHandle rch = (RawContainerHandle)containerHdl; 842 843 ContainerOperation lop = 844 new ContainerOperation(rch, ContainerOperation.CREATE); 845 846 rch.preDirty(true); 850 try 851 { 852 t.logAndDo(lop); 853 854 flush(t.getLastLogInstant()); 859 } 860 finally 861 { 862 rch.preDirty(false); 865 } 866 } 867 868 firstPage = containerHdl.addPage(); 869 870 } 871 finally 872 { 873 874 if (firstPage != null) 875 { 876 firstPage.unlatch(); 877 firstPage = null; 878 } 879 880 containerCache.release(container); 881 882 if (containerHdl != null) 883 { 884 containerHdl.close(); 885 containerHdl = null; 886 } 887 888 if (!tmpContainer) 889 { 890 893 cl.unlockContainer(t, ch); 894 } 895 } 896 897 return containerId; 898 } 899 900 903 public long addAndLoadStreamContainer( 904 RawTransaction t, 905 long segmentId, 906 Properties tableProperties, 907 RowSource rowSource) 908 throws StandardException 909 { 910 long containerId = getNextId(); 911 912 ContainerKey identity = new ContainerKey(segmentId, containerId); 913 914 StreamFileContainer sContainer = 916 new StreamFileContainer(identity, this, tableProperties); 917 sContainer.load(rowSource); 918 919 return containerId; 920 } 921 922 923 929 public StreamContainerHandle openStreamContainer( 930 RawTransaction t, 931 long segmentId, 932 long containerId, 933 boolean hold) 934 throws StandardException 935 { 936 937 ContainerKey identity = new ContainerKey(segmentId, containerId); 938 939 StreamFileContainerHandle c; 940 941 StreamFileContainer container = new StreamFileContainer(identity, this); 943 container = container.open(false); 944 if (container == null) 945 return null; 946 947 c = new StreamFileContainerHandle(getIdentifier(), t, container, hold); 948 949 if (c.useContainer()) 951 return c; 952 else 953 return null; 954 } 955 956 965 public void dropStreamContainer( 966 RawTransaction t, 967 long segmentId, 968 long containerId) 969 throws StandardException 970 { 971 972 boolean tmpContainer = (segmentId == ContainerHandle.TEMPORARY_SEGMENT); 973 974 StreamContainerHandle containerHdl = null; 975 976 try 977 { 978 ContainerKey ckey = new ContainerKey(segmentId, containerId); 979 980 t.notifyObservers(ckey); 982 983 containerHdl = t.openStreamContainer(segmentId, containerId, false); 984 if (tmpContainer && (containerHdl != null)) 985 { 986 containerHdl.removeContainer(); 987 return; 988 } 989 } 990 finally 991 { 992 if (containerHdl != null) 993 containerHdl.close(); 994 } 995 } 996 997 1004 public void reCreateContainerForRedoRecovery( 1005 RawTransaction t, 1006 long segmentId, 1007 long containerId, 1008 ByteArray containerInfo) 1009 throws StandardException 1010 { 1011 if (SanityManager.DEBUG) 1012 SanityManager.ASSERT(segmentId != ContainerHandle.TEMPORARY_SEGMENT, 1013 "Cannot recreate temp container during load tran"); 1014 1015 ContainerKey identity = new ContainerKey(segmentId, containerId); 1016 1017 1021 FileContainer container = 1022 (FileContainer)containerCache.create(identity, containerInfo); 1023 1024 containerCache.release(container); 1025 } 1026 1027 1043 public void dropContainer( 1044 RawTransaction t, 1045 ContainerKey ckey) 1046 throws StandardException 1047 { 1048 boolean tmpContainer = 1049 (ckey.getSegmentId() == ContainerHandle.TEMPORARY_SEGMENT); 1050 1051 LockingPolicy cl = null; 1052 1053 if (!tmpContainer) 1054 { 1055 if (isReadOnly()) 1056 { 1057 throw StandardException.newException( 1058 SQLState.DATA_CONTAINER_READ_ONLY); 1059 } 1060 1061 cl = 1062 t.newLockingPolicy( 1063 LockingPolicy.MODE_CONTAINER, 1064 TransactionController.ISOLATION_SERIALIZABLE, true); 1065 1066 if (SanityManager.DEBUG) 1067 SanityManager.ASSERT(cl != null); 1068 } 1069 1070 t.notifyObservers(ckey); 1072 1073 RawContainerHandle containerHdl = (RawContainerHandle) 1074 t.openContainer(ckey, cl, ContainerHandle.MODE_FORUPDATE); 1075 1076 try 1081 { 1082 if (containerHdl == null || 1083 containerHdl.getContainerStatus() != RawContainerHandle.NORMAL) 1084 { 1085 if (tmpContainer) 1087 { 1088 if (containerHdl != null) 1089 containerHdl.removeContainer((LogInstant)null); 1090 return; 1091 } 1092 else 1093 { 1094 throw StandardException.newException( 1095 SQLState.DATA_CONTAINER_VANISHED, ckey); 1096 } 1097 } 1098 1099 if (tmpContainer) 1101 { 1102 containerHdl.dropContainer((LogInstant)null, true); 1103 containerHdl.removeContainer((LogInstant)null); 1104 } 1105 else 1106 { 1107 ContainerOperation lop = 1108 new ContainerOperation( 1109 containerHdl, ContainerOperation.DROP); 1110 1111 containerHdl.preDirty(true); 1115 try 1116 { 1117 t.logAndDo(lop); 1118 } 1119 finally 1120 { 1121 containerHdl.preDirty(false); 1124 } 1125 1126 1127 Serviceable p = 1129 new ReclaimSpace( 1130 ReclaimSpace.CONTAINER, 1131 ckey, 1132 this, 1133 true ); 1134 1135 if (SanityManager.DEBUG) 1136 { 1137 if (SanityManager.DEBUG_ON(DaemonService.DaemonTrace)) 1138 { 1139 SanityManager.DEBUG( 1140 DaemonService.DaemonTrace, 1141 "Add post commit work " + p); 1142 } 1143 } 1144 1145 t.addPostCommitWork(p); 1146 } 1147 1148 } 1149 finally 1150 { 1151 if (containerHdl != null) 1152 containerHdl.close(); 1153 } 1154 1155 1156 } 1157 1158 1159 1196 public void checkpoint() throws StandardException 1197 { 1198 pageCache.cleanAll(); 1199 containerCache.cleanAll(); 1200 } 1201 1202 public void idle() throws StandardException 1203 { 1204 pageCache.ageOut(); 1205 containerCache.ageOut(); 1206 } 1207 1208 public void setRawStoreFactory( 1209 RawStoreFactory rsf, 1210 boolean create, 1211 Properties startParams) 1212 throws StandardException 1213 { 1214 1215 rawStoreFactory = rsf; 1216 1217 1221 bootLogFactory(create, startParams); 1222 1223 } 1224 1225 1226 1231 public UUID getIdentifier() 1232 { 1233 return identifier; 1234 } 1235 1236 1239 public int reclaimSpace( 1240 Serviceable work, 1241 ContextManager contextMgr) 1242 throws StandardException 1243 { 1244 if (work == null) 1245 return Serviceable.DONE; 1246 1247 Transaction tran = 1248 rawStoreFactory.findUserTransaction( 1249 contextMgr, AccessFactoryGlobals.SYS_TRANS_NAME); 1250 1251 if (SanityManager.DEBUG) 1252 { 1253 SanityManager.ASSERT(tran != null, "null transaction"); 1254 1255 if (SanityManager.DEBUG_ON(DaemonService.DaemonTrace)) 1256 SanityManager.DEBUG(DaemonService.DaemonTrace, 1257 "Performing post commit work " + work); 1258 } 1259 1260 return ReclaimSpaceHelper.reclaimSpace(this, (RawTransaction)tran, 1261 (ReclaimSpace)work); 1262 } 1263 1264 1268 public StandardException markCorrupt(StandardException originalError) 1269 { 1270 boolean firsttime = !isCorrupt; 1271 1272 isCorrupt = true; 1273 if (getLogFactory() != null) 1274 getLogFactory().markCorrupt(originalError); 1275 1276 if (firsttime) 1279 { 1280 if (pageCache != null) 1282 pageCache.discard(null); 1283 1284 if (containerCache != null) 1285 containerCache.discard(null); 1286 1287 pageCache = null; 1289 containerCache = null; 1290 1291 releaseJBMSLockOnDB(); 1292 } 1293 1294 return originalError; 1295 } 1296 1297 public FileResource getFileHandler() 1298 { 1299 return fileHandler; 1300 } 1301 1302 public void removeStubsOK() 1303 { 1304 removeStubsOK = true; 1305 } 1306 1307 1310 1311 public int getIntParameter( 1312 String parameterName, 1313 Properties properties, 1314 int defaultValue, 1315 int minimumValue, 1316 int maximumValue) 1317 { 1318 1319 int newValue; 1320 1321 String parameter = null; 1322 1323 if (properties != null) 1324 parameter = properties.getProperty(parameterName); 1325 1326 if (parameter == null) 1327 parameter = PropertyUtil.getSystemProperty(parameterName); 1328 1329 if (parameter != null) 1330 { 1331 try 1332 { 1333 newValue = Integer.parseInt(parameter); 1334 1335 if ((newValue >= minimumValue) && (newValue <= maximumValue)) 1336 return newValue; 1337 } 1338 catch (NumberFormatException nfe) 1339 { 1340 } 1342 } 1343 1344 return defaultValue; 1345 } 1346 1347 CacheManager getContainerCache() 1348 { 1349 return containerCache; 1350 } 1351 1352 CacheManager getPageCache() 1353 { 1354 return pageCache; 1355 } 1356 1357 public long[] getCacheStats(String cacheName) 1358 { 1359 1360 if (cacheName == null) 1361 { 1362 return getPageCache().getCacheStats(); 1364 } 1365 1366 if (cacheName.equals("pageCache")) 1367 { 1368 return getPageCache().getCacheStats(); 1369 } 1370 else 1371 { 1372 return getPageCache().getCacheStats(); 1374 } 1375 } 1376 1377 public void resetCacheStats(String cacheName) 1378 { 1379 if (cacheName == null) 1380 { 1381 getPageCache().resetCacheStats(); 1383 return; 1384 } 1385 1386 if (cacheName.equals("pageCache")) 1387 { 1388 getPageCache().resetCacheStats(); 1389 } 1390 else 1391 { 1392 getPageCache().resetCacheStats(); 1394 } 1395 } 1396 1397 1402 void flush(LogInstant instant) 1403 throws StandardException 1404 { 1405 getLogFactory().flush(instant); 1406 } 1407 1408 1415 private void syncSideLog(long bipLocation) 1416 throws StandardException 1417 { 1418 return; 1419 } 1420 1421 1422 LogFactory getLogFactory() 1423 { 1424 return logFactory; 1425 } 1426 1427 1428 RawStoreFactory getRawStoreFactory() 1429 { 1430 return rawStoreFactory; 1431 } 1432 1433 1437 public String getRootDirectory() 1438 { 1439 return dataDirectory; 1440 } 1441 1442 1455 Cacheable newContainerObject() 1456 { 1457 if( supportsRandomAccess) 1458 return new RAFContainer(this); 1459 else 1460 return new InputStreamContainer( this); 1461 } 1462 1463 1469 private void pageToDirty(RawTransaction t, StoredPage page) 1470 throws StandardException 1471 { 1472 return; } 1474 1475 1481 private PageActions getLoggablePageActions() throws StandardException 1482 { 1483 if (loggablePageActions == null) 1484 loggablePageActions = new LoggableActions(); 1485 return loggablePageActions; 1486 } 1487 1488 1493 private AllocationActions getLoggableAllocationActions() 1494 { 1495 if (loggableAllocActions == null) 1496 loggableAllocActions = new LoggableAllocActions(); 1497 return loggableAllocActions; 1498 } 1499 1500 synchronized StorageFile getTempDirectory() 1501 { 1502 actionCode = GET_TEMP_DIRECTORY_ACTION; 1503 try 1504 { 1505 return (StorageFile) AccessController.doPrivileged( this); 1506 } 1507 catch (PrivilegedActionException pae) 1508 { 1509 return null; 1511 } 1512 } 1513 1514 private synchronized void removeTempDirectory() 1515 { 1516 if( storageFactory != null) 1517 { 1518 actionCode = REMOVE_TEMP_DIRECTORY_ACTION; 1519 try 1520 { 1521 AccessController.doPrivileged( this); 1522 } 1523 catch (PrivilegedActionException pae) 1524 { 1525 } 1527 } 1528 } 1529 1530 1563 public StorageFile getContainerPath( 1564 ContainerKey containerId, 1565 boolean stub) 1566 { 1567 return getContainerPath(containerId, stub, GET_CONTAINER_PATH_ACTION); 1568 } 1569 1570 private synchronized StorageFile getContainerPath( 1571 ContainerKey containerId, 1572 boolean stub, 1573 int code) 1574 { 1575 actionCode = code; 1576 try 1577 { 1578 this.containerId = containerId; 1579 this.stub = stub; 1580 try 1581 { 1582 return (StorageFile) AccessController.doPrivileged( this); 1583 } 1584 catch (PrivilegedActionException pae) 1585 { 1586 return null; 1588 } 1589 } 1590 finally 1591 { 1592 this.containerId = null; 1593 } 1594 } 1595 1596 1597 1609 public StorageFile getAlternateContainerPath( 1610 ContainerKey containerId, 1611 boolean stub) 1612 { 1613 return getContainerPath( 1614 containerId, stub, GET_ALTERNATE_CONTAINER_PATH_ACTION); 1615 } 1616 1617 1618 1619 1622 private synchronized void removeStubs() 1623 { 1624 actionCode = REMOVE_STUBS_ACTION; 1625 try 1626 { 1627 AccessController.doPrivileged( this); 1628 } 1629 catch (PrivilegedActionException pae) 1630 { 1631 } 1633 } 1634 1635 1645 public void stubFileToRemoveAfterCheckPoint( 1646 StorageFile file, 1647 LogInstant logInstant, 1648 Object identity) 1649 { 1650 if(droppedTableStubInfo != null) 1651 { 1652 Object [] removeInfo = new Object [2]; 1653 removeInfo[0] = file; 1654 removeInfo[1] = identity; 1655 droppedTableStubInfo.put(logInstant, removeInfo); 1656 } 1657 } 1658 1659 1670 public void removeDroppedContainerFileStubs( 1671 LogInstant redoLWM) 1672 throws StandardException 1673 { 1674 1675 if (droppedTableStubInfo != null) 1676 { 1677 synchronized(droppedTableStubInfo) 1678 { 1679 for (Enumeration e = droppedTableStubInfo.keys(); 1680 e.hasMoreElements(); ) 1681 { 1682 LogInstant logInstant = (LogInstant) e.nextElement(); 1683 if(logInstant.lessThan(redoLWM)) 1684 { 1685 1686 Object [] removeInfo = 1687 (Object []) droppedTableStubInfo.get(logInstant); 1688 Object identity = removeInfo[1]; 1689 Cacheable ccentry = containerCache.findCached(identity); 1691 if(ccentry!=null) 1692 containerCache.remove(ccentry); 1693 1694 synchronized( this) 1696 { 1697 actionFile = (StorageFile)removeInfo[0]; 1698 actionCode = DELETE_IF_EXISTS_ACTION; 1699 try 1700 { 1701 if (AccessController.doPrivileged(this) != null) 1702 { 1703 droppedTableStubInfo.remove(logInstant); 1706 } 1707 } 1708 catch (PrivilegedActionException pae) 1709 { 1710 } 1712 } 1713 } 1714 } 1715 } 1716 } 1717 } 1718 1719 1720 1721 1722 1723 1724 1739 private synchronized long findMaxContainerId() 1740 { 1741 actionCode = FIND_MAX_CONTAINER_ID_ACTION; 1742 try 1743 { 1744 return ((Long ) AccessController.doPrivileged( this)).longValue(); 1745 } 1746 catch (PrivilegedActionException pae) 1747 { 1748 return 0; 1750 } 1751 } 1752 1753 private void bootLogFactory( 1754 boolean create, 1755 Properties startParams) 1756 throws StandardException 1757 { 1758 1759 if (isReadOnly()) 1760 { 1761 startParams.put( 1762 LogFactory.RUNTIME_ATTRIBUTES, LogFactory.RT_READONLY); 1763 } 1764 1765 logFactory = (LogFactory) 1766 Monitor.bootServiceModule( 1767 create, this, 1768 rawStoreFactory.getLogFactoryModule(), startParams); 1769 } 1770 1771 1772 1775 private boolean handleServiceType( 1776 String type) 1777 { 1778 try 1779 { 1780 PersistentService ps = 1781 Monitor.getMonitor().getServiceProvider(type); 1782 return ps != null && ps.hasStorageFactory(); 1783 } 1784 catch (StandardException se) 1785 { 1786 return false; 1787 } 1788 } 1789 1790 1821 private void getJBMSLockOnDB( 1822 UUID myUUID, 1823 UUIDFactory uuidFactory, 1824 String databaseDirectory) 1825 throws StandardException 1826 { 1827 if (fileLockOnDB != null) return; 1829 1830 if (isReadOnly()) 1831 return; 1832 if (SanityManager.DEBUG) 1833 { 1834 if (myUUID == null) 1835 SanityManager.THROWASSERT("myUUID == null"); 1836 } 1837 1838 synchronized( this) 1839 { 1840 actionCode = GET_LOCK_ON_DB_ACTION; 1841 this.myUUID = myUUID; 1842 this.uuidFactory = uuidFactory; 1843 this.databaseDirectory = databaseDirectory; 1844 1845 try 1846 { 1847 AccessController.doPrivileged( this); 1848 } 1849 catch (PrivilegedActionException pae) 1850 { 1851 throw (StandardException) pae.getException(); 1852 } 1853 finally 1854 { 1855 this.myUUID = null; 1856 this.uuidFactory = null; 1857 this.databaseDirectory = null; 1858 } 1859 } 1860 1861 } 1865 1866 private void privGetJBMSLockOnDB() throws StandardException 1868 { 1869 boolean fileLockExisted = false; 1870 String blownUUID = null; 1871 1872 StorageFile fileLock = storageFactory.newStorageFile( DB_LOCKFILE_NAME); 1873 1874 try 1875 { 1876 if (fileLock.exists()) 1879 { 1880 fileLockExisted = true; 1881 1882 fileLockOnDB = fileLock.getRandomAccessFile( "rw"); 1888 try 1889 { 1890 blownUUID = fileLockOnDB.readUTF(); 1891 } 1892 catch (IOException ioe) 1893 { 1894 fileLockExisted = false; 1897 } 1898 1899 fileLockOnDB.close(); 1900 fileLockOnDB = null; 1901 1902 if (!fileLock.delete()) 1904 { 1905 throw StandardException.newException( 1906 SQLState.DATA_MULTIPLE_JBMS_ON_DB, 1907 databaseDirectory); 1908 } 1909 } 1910 1911 fileLockOnDB = fileLock.getRandomAccessFile( "rw"); 1917 1918 fileLockOnDB.writeUTF(myUUID.toString()); 1920 1921 fileLockOnDB.sync( false); 1922 fileLockOnDB.seek(0); 1923 UUID checkUUID = uuidFactory.recreateUUID(fileLockOnDB.readUTF()); 1925 if (!checkUUID.equals(myUUID)) 1926 { 1927 throw StandardException.newException( 1928 SQLState.DATA_MULTIPLE_JBMS_ON_DB, databaseDirectory); 1929 } 1930 } 1931 catch (IOException ioe) 1932 { 1933 readOnly = true; 1935 try 1936 { 1937 if (fileLockOnDB != null) 1938 fileLockOnDB.close(); 1939 } 1940 catch (IOException ioe2) 1941 { } 1942 fileLockOnDB = null; 1943 1944 return; 1945 } 1946 1947 if (fileLock.delete()) 1948 { 1949 1953 Object [] args = new Object [3]; 1954 args[0] = myUUID; 1955 args[1] = databaseDirectory; 1956 args[2] = blownUUID; 1957 1958 1963 int exLockStatus = StorageFile.NO_FILE_LOCK_SUPPORT ; 1964 if(!throwDBlckException) 1967 { 1968 exFileLock = 1969 storageFactory.newStorageFile( DB_EX_LOCKFILE_NAME); 1970 exLockStatus = exFileLock.getExclusiveFileLock(); 1971 } 1972 1973 if (exLockStatus == StorageFile.NO_FILE_LOCK_SUPPORT) 1974 { 1975 if (fileLockExisted && !throwDBlckException) 1976 { 1977 1978 StandardException multipleJBMSWarning = 1979 StandardException.newException( 1980 SQLState.DATA_MULTIPLE_JBMS_WARNING, args); 1981 1982 String warningMsg = 1983 MessageService.getCompleteMessage( 1984 SQLState.DATA_MULTIPLE_JBMS_WARNING, args); 1985 1986 logMsg(warningMsg); 1987 1988 System.err.println(warningMsg); 1992 1993 } 1994 } 1995 1996 try 1999 { 2000 if(fileLockOnDB != null) 2004 fileLockOnDB.close(); 2005 fileLockOnDB = fileLock.getRandomAccessFile( "rw"); 2006 2007 fileLockOnDB.writeUTF(myUUID.toString()); 2009 2010 fileLockOnDB.sync( false); 2011 fileLockOnDB.close(); 2012 } 2013 catch (IOException ioe) 2014 { 2015 try 2016 { 2017 fileLockOnDB.close(); 2018 } 2019 catch (IOException ioe2) 2020 { 2021 2022 } 2023 } 2024 finally 2025 { 2026 fileLockOnDB = null; 2027 } 2028 2029 if (fileLockExisted && throwDBlckException) 2030 { 2031 throw StandardException.newException( 2034 SQLState.DATA_MULTIPLE_JBMS_FORCE_LOCK, args); 2035 } 2036 2037 if(exLockStatus == StorageFile.EXCLUSIVE_FILE_LOCK_NOT_AVAILABLE) 2038 { 2039 2040 throw StandardException.newException( 2041 SQLState.DATA_MULTIPLE_JBMS_ON_DB, 2042 databaseDirectory); 2043 } 2044 2045 } 2046 } 2048 private void releaseJBMSLockOnDB() 2049 { 2050 if (isReadOnly()) 2051 return; 2052 2053 synchronized( this) 2054 { 2055 actionCode = RELEASE_LOCK_ON_DB_ACTION; 2056 try 2057 { 2058 AccessController.doPrivileged( this); 2059 } 2060 catch (PrivilegedActionException pae) 2061 { 2062 } 2065 finally 2066 { 2067 fileLockOnDB = null; 2068 } 2069 } 2070 } 2071 2072 private void privReleaseJBMSLockOnDB() throws IOException 2073 { 2074 if (fileLockOnDB != null) 2075 fileLockOnDB.close(); 2076 2077 if (storageFactory != null) 2078 { 2079 StorageFile fileLock = 2080 storageFactory.newStorageFile(DB_LOCKFILE_NAME); 2081 2082 fileLock.delete(); 2083 } 2084 2085 if(exFileLock != null) 2088 exFileLock.releaseExclusiveFileLock(); 2089 2090 return; 2091 } 2093 private void logMsg(String msg) 2094 { 2095 if (istream == null) 2096 { 2097 istream = Monitor.getStream(); 2098 } 2099 2100 istream.println(msg); 2101 } 2102 2103 public final boolean databaseEncrypted() 2104 { 2105 return databaseEncrypted; 2106 } 2107 2108 public void setDatabaseEncrypted() 2109 { 2110 databaseEncrypted = true; 2111 } 2112 2113 public int encrypt( 2114 byte[] cleartext, 2115 int offset, 2116 int length, 2117 byte[] ciphertext, 2118 int outputOffset, 2119 boolean newEngine) 2120 throws StandardException 2121 { 2122 return rawStoreFactory.encrypt( 2123 cleartext, offset, length, 2124 ciphertext, outputOffset, 2125 newEngine); 2126 } 2127 2128 public int decrypt( 2129 byte[] ciphertext, 2130 int offset, 2131 int length, 2132 byte[] cleartext, 2133 int outputOffset) 2134 throws StandardException 2135 { 2136 return rawStoreFactory.decrypt( 2137 ciphertext, offset, length, cleartext, outputOffset); 2138 } 2139 2140 2141 2142 public void encryptAllContainers(RawTransaction t) throws StandardException 2143 { 2144 containerEncrypter = new EncryptData(this); 2145 containerEncrypter.encryptAllContainers(t); 2147 } 2148 2149 2150 2156 public void removeOldVersionOfContainers(boolean inRecovery) 2157 throws StandardException 2158 { 2159 if (inRecovery) { 2164 containerEncrypter = new EncryptData(this); 2165 } 2166 containerEncrypter.removeOldVersionOfContainers(inRecovery); 2167 containerEncrypter = null; 2168 } 2169 2170 2171 2175 public int getEncryptionBlockSize() 2176 { 2177 return rawStoreFactory.getEncryptionBlockSize(); 2178 } 2179 2180 public String getVersionedName(String name, long generationId) 2181 { 2182 return name.concat(".G".concat(Long.toString(generationId))); 2183 } 2184 2185 2198 public long getMaxContainerId() 2199 throws StandardException 2200 { 2201 return(findMaxContainerId()); 2202 } 2203 2204 synchronized long getNextId() { 2205 return nextContainerId++; 2206 } 2207 2208 2209 int random() 2210 { 2211 return databaseEncrypted ? rawStoreFactory.random() : 0; 2212 } 2213 2214 2217 void fileToRemove( StorageFile file, boolean remove) 2218 { 2219 if (postRecoveryRemovedFiles == null) 2220 postRecoveryRemovedFiles = new Hashtable (); 2221 String path = null; 2222 synchronized( this) 2223 { 2224 actionCode = GET_PATH_ACTION; 2225 actionFile = file; 2226 try 2227 { 2228 path = (String ) AccessController.doPrivileged( this); 2229 } 2230 catch (PrivilegedActionException pae) 2231 { 2232 } 2234 finally 2235 { 2236 actionFile = null; 2237 } 2238 } 2239 if (remove) postRecoveryRemovedFiles.put(path, file); 2241 else 2242 postRecoveryRemovedFiles.remove(path); 2243 2244 } 2245 2246 2251 public void postRecovery() throws StandardException 2252 { 2253 2254 DaemonService daemon = rawStoreFactory.getDaemon(); 2256 2257 if (daemon == null) 2258 return; 2259 2260 containerCache.useDaemonService(daemon); 2261 2262 pageCache.useDaemonService(daemon); 2263 if (postRecoveryRemovedFiles != null) 2264 { 2265 synchronized( this) 2266 { 2267 actionCode = POST_RECOVERY_REMOVE_ACTION; 2268 try 2269 { 2270 AccessController.doPrivileged( this); 2271 } 2272 catch (PrivilegedActionException pae) 2273 { 2274 } 2276 } 2277 postRecoveryRemovedFiles = null; 2278 } 2279 } 2280 2281 public void freezePersistentStore() throws StandardException 2282 { 2283 synchronized(freezeSemaphore) 2284 { 2285 if (isFrozen) 2286 { 2287 throw StandardException.newException( 2288 SQLState.RAWSTORE_NESTED_FREEZE); 2289 } 2290 2291 isFrozen = true; 2294 2295 try 2297 { 2298 while(writersInProgress > 0) 2299 { 2300 try 2301 { 2302 freezeSemaphore.wait(); 2303 } 2304 catch (InterruptedException ie) 2305 { 2306 isFrozen = false; 2310 freezeSemaphore.notifyAll(); 2311 2312 throw StandardException.interrupt(ie); 2313 } 2314 } 2315 } 2316 catch (RuntimeException rte) 2317 { 2318 isFrozen = false; 2322 freezeSemaphore.notifyAll(); 2323 throw rte; } 2325 2326 if (SanityManager.DEBUG) 2327 SanityManager.ASSERT(writersInProgress == 0 && 2328 isFrozen == true, 2329 "data store is not properly frozen"); 2330 } 2331 } 2332 2333 public void unfreezePersistentStore() 2334 { 2335 synchronized(freezeSemaphore) 2336 { 2337 isFrozen = false; 2338 freezeSemaphore.notifyAll(); 2339 } 2340 } 2341 2342 public void writeInProgress() throws StandardException 2343 { 2344 synchronized(freezeSemaphore) 2345 { 2346 while(isFrozen) 2348 { 2349 try 2350 { 2351 freezeSemaphore.wait(); 2352 } 2353 catch (InterruptedException ie) 2354 { 2355 throw StandardException.interrupt(ie); 2356 } 2357 } 2358 2359 writersInProgress++; 2361 } 2362 } 2363 2364 public void writeFinished() 2365 { 2366 synchronized(freezeSemaphore) 2367 { 2368 if (SanityManager.DEBUG) 2369 SanityManager.ASSERT(writersInProgress > 0, 2370 "no writers in progress"); 2371 2372 writersInProgress--; 2373 freezeSemaphore.notifyAll(); } 2375 } 2376 2377 2378 2382 public void backupDataFiles(Transaction rt, File backupDir) throws StandardException 2383 { 2384 2385 2392 2393 String [] files = getContainerNames(); 2394 2395 if (files != null) { 2396 LockingPolicy lockPolicy = rt.newLockingPolicy(LockingPolicy.MODE_NONE, 2400 TransactionController.ISOLATION_NOLOCK, 2401 false); 2402 long segmentId = 0; 2403 2404 for (int f = files.length-1; f >= 0 ; f--) { 2406 long containerId; 2407 try { 2408 containerId = 2409 Long.parseLong(files[f].substring(1, (files[f].length() -4)), 16); 2410 } 2411 catch (Throwable t) 2412 { 2413 continue; 2417 } 2418 2419 ContainerKey identity = new ContainerKey(segmentId, containerId); 2420 2421 2430 2431 ContainerHandle containerHdl = openDroppedContainer((RawTransaction)rt, 2432 identity, lockPolicy, 2433 ContainerHandle.MODE_READONLY); 2434 2448 2449 if( containerHdl != null) { 2450 containerHdl.backupContainer(backupDir.getPath()); 2451 containerHdl.close(); 2452 } 2453 } 2454 } else 2455 { 2456 if (SanityManager.DEBUG) 2457 SanityManager.THROWASSERT("backup process is unable to read container names in seg0"); 2458 } 2459 } 2460 2461 2467 synchronized String [] getContainerNames() 2468 { 2469 actionCode = GET_CONTAINER_NAMES_ACTION; 2470 try{ 2471 return (String []) AccessController.doPrivileged( this); 2472 } 2473 catch( PrivilegedActionException pae){ return null;} 2474 } 2475 2476 2477 2478 2494 private void restoreDataDirectory(String backupPath) 2495 throws StandardException 2496 { 2497 File bsegdir; File backupRoot = new java.io.File (backupPath); 2500 2506 String [] bfilelist = backupRoot.list(); 2507 if(bfilelist !=null) 2508 { 2509 boolean segmentexist = false; 2510 for (int i = 0; i < bfilelist.length; i++) 2511 { 2512 if(bfilelist[i].startsWith("seg")) 2514 { 2515 bsegdir = new File(backupRoot , bfilelist[i]); 2516 if(bsegdir.exists() && bsegdir.isDirectory()) 2517 { 2518 segmentexist = true; 2519 break; 2520 } 2521 } 2522 } 2523 2524 if(!segmentexist) 2525 { 2526 throw 2527 StandardException.newException( 2528 SQLState.DATA_DIRECTORY_NOT_FOUND_IN_BACKUP, backupRoot); 2529 } 2530 } 2531 else 2532 { 2533 2534 throw StandardException.newException( 2535 SQLState.DATA_DIRECTORY_NOT_FOUND_IN_BACKUP, backupRoot); 2536 } 2537 2538 synchronized (this) 2539 { 2540 actionCode = RESTORE_DATA_DIRECTORY_ACTION; 2541 this.backupPath = backupPath; 2542 this.backupRoot = backupRoot; 2543 this.bfilelist = bfilelist; 2544 try 2545 { 2546 AccessController.doPrivileged( this); 2547 } 2548 catch (PrivilegedActionException pae) 2549 { 2550 throw (StandardException) pae.getException(); 2551 } 2552 finally 2553 { 2554 this.backupPath = null; 2555 this.backupRoot = null; 2556 this.bfilelist = null; 2557 } 2558 } 2559 } 2560 2561 private void privRestoreDataDirectory() throws StandardException 2562 { 2563 StorageFile csegdir; StorageFile dataRoot = 2565 storageFactory.newStorageFile( null); 2567 String [] cfilelist = dataRoot.list(); 2569 if(cfilelist!=null) 2570 { 2571 for (int i = 0; i < cfilelist.length; i++) 2572 { 2573 if(cfilelist[i].startsWith("seg")) 2575 { 2576 csegdir = storageFactory.newStorageFile( cfilelist[i]); 2577 if(!csegdir.deleteAll()) 2578 { 2579 throw 2580 StandardException.newException( 2581 SQLState.UNABLE_TO_REMOVE_DATA_DIRECTORY, 2582 csegdir); 2583 } 2584 } 2585 } 2586 } 2587 2588 for (int i = 0; i < bfilelist.length; i++) 2590 { 2591 if (bfilelist[i].startsWith("seg")) 2593 { 2594 csegdir = storageFactory.newStorageFile( bfilelist[i]); 2595 File bsegdir1 = new java.io.File (backupRoot, bfilelist[i]); 2596 if (!FileUtil.copyDirectory( 2597 writableStorageFactory, bsegdir1, csegdir)) 2598 { 2599 throw 2600 StandardException.newException( 2601 SQLState.UNABLE_TO_COPY_DATA_DIRECTORY, 2602 bsegdir1, csegdir); 2603 } 2604 } 2605 else if (databaseEncrypted && 2606 bfilelist[i].startsWith( 2607 Attribute.CRYPTO_EXTERNAL_KEY_VERIFY_FILE)) 2608 { 2609 2614 File fromFile = new File(backupRoot,bfilelist[i]); 2616 StorageFile toFile = 2617 storageFactory.newStorageFile(bfilelist[i]); 2618 2619 if (!FileUtil.copyFile(writableStorageFactory,fromFile,toFile)) 2620 { 2621 throw StandardException.newException( 2622 SQLState.UNABLE_TO_COPY_DATA_DIRECTORY, 2623 bfilelist[i], toFile); 2624 } 2625 } 2626 } 2627 2628 } 2630 2633 public boolean isReadOnly() 2634 { 2635 return readOnly; 2637 } 2638 2639 2642 public StorageFactory getStorageFactory() 2643 { 2644 return storageFactory; 2645 } 2646 2647 public final Object run() throws Exception 2649 { 2650 switch( actionCode) 2651 { 2652 case BOOT_ACTION: 2653 readOnly = storageFactory.isReadOnlyDatabase(); 2654 supportsRandomAccess = storageFactory.supportsRandomAccess(); 2655 return null; 2656 2657 case GET_TEMP_DIRECTORY_ACTION: 2658 return storageFactory.getTempDir(); 2659 2660 case REMOVE_TEMP_DIRECTORY_ACTION: 2661 StorageFile tempDir = storageFactory.getTempDir(); 2662 if( tempDir != null) 2663 tempDir.deleteAll(); 2664 return null; 2665 2666 case GET_CONTAINER_PATH_ACTION: 2667 case GET_ALTERNATE_CONTAINER_PATH_ACTION: 2668 { 2669 StringBuffer sb = new StringBuffer ("seg"); 2670 sb.append(containerId.getSegmentId()); 2671 sb.append(storageFactory.getSeparator()); 2672 if( actionCode == GET_CONTAINER_PATH_ACTION) 2673 { 2674 sb.append(stub ? 'd' : 'c'); 2675 sb.append(Long.toHexString(containerId.getContainerId())); 2676 sb.append(".dat"); 2677 } 2678 else 2679 { 2680 sb.append(stub ? 'D' : 'C'); 2681 sb.append(Long.toHexString(containerId.getContainerId())); 2682 sb.append(".DAT"); 2683 } 2684 return storageFactory.newStorageFile( sb.toString()); 2685 } 2687 case REMOVE_STUBS_ACTION: 2688 { 2689 char separator = storageFactory.getSeparator(); 2690 StorageFile root = storageFactory.newStorageFile( null); 2691 2692 String [] segs = root.list(); 2694 for (int s = segs.length-1; s >= 0; s--) 2695 { 2696 if (segs[s].startsWith("seg")) 2697 { 2698 StorageFile seg = 2699 storageFactory.newStorageFile(root, segs[s]); 2700 2701 if (seg.exists() && seg.isDirectory()) 2702 { 2703 String [] files = seg.list(); 2704 for (int f = files.length-1; f >= 0 ; f--) 2705 { 2706 if (files[f].startsWith("D") || 2708 files[f].startsWith("d")) 2709 { 2710 StorageFile stub = 2711 storageFactory.newStorageFile( 2712 root, segs[s] + separator + files[f]); 2713 2714 boolean delete_status = stub.delete(); 2715 2716 if (SanityManager.DEBUG) 2717 { 2718 if (!delete_status) 2722 { 2723 SanityManager.THROWASSERT( 2724 "delete of stub (" + 2725 stub + ") failed."); 2726 } 2727 } 2728 } 2729 } 2730 } 2731 } 2732 } 2733 break; 2734 } 2736 case FIND_MAX_CONTAINER_ID_ACTION: 2737 { 2738 long maxnum = 1; 2739 StorageFile seg = storageFactory.newStorageFile( "seg0"); 2740 2741 if (seg.exists() && seg.isDirectory()) 2742 { 2743 String [] files = seg.list(); 2745 2746 for (int f = files.length-1; f >= 0 ; f--) 2748 { 2749 try 2750 { 2751 long fileNumber = 2752 Long.parseLong( 2753 files[f].substring( 2754 1, (files[f].length() -4)), 16); 2755 2756 if (fileNumber > maxnum) 2757 maxnum = fileNumber; 2758 } 2759 catch (Throwable t) 2760 { 2761 } 2765 } 2766 } 2767 return ReuseFactory.getLong( maxnum); 2768 } 2770 case DELETE_IF_EXISTS_ACTION: 2771 { 2772 boolean ret = actionFile.exists() && actionFile.delete(); 2773 actionFile = null; 2774 return ret ? this : null; 2775 } 2777 case GET_PATH_ACTION: 2778 { 2779 String path = actionFile.getPath(); 2780 actionFile = null; 2781 return path; 2782 } 2784 case POST_RECOVERY_REMOVE_ACTION: 2785 { 2786 for (Enumeration e = postRecoveryRemovedFiles.elements(); 2787 e.hasMoreElements(); ) 2788 { 2789 StorageFile f = (StorageFile) e.nextElement(); 2790 if (f.exists()) 2791 { 2792 boolean delete_status = f.delete(); 2793 2794 if (SanityManager.DEBUG) 2795 { 2796 if (!delete_status) 2800 { 2801 SanityManager.THROWASSERT( 2802 "delete of stub (" + stub + ") failed."); 2803 } 2804 } 2805 } 2806 } 2807 return null; 2808 } 2809 2810 case GET_LOCK_ON_DB_ACTION: 2811 privGetJBMSLockOnDB(); 2812 return null; 2813 2814 case RELEASE_LOCK_ON_DB_ACTION: 2815 privReleaseJBMSLockOnDB(); 2816 return null; 2817 2818 case RESTORE_DATA_DIRECTORY_ACTION: 2819 privRestoreDataDirectory(); 2820 return null; 2821 case GET_CONTAINER_NAMES_ACTION: 2822 { 2823 StorageFile seg = storageFactory.newStorageFile( "seg0"); 2824 if (seg.exists() && seg.isDirectory()) 2825 { 2826 return seg.list(); 2828 } 2829 return null; 2830 } 2832 } 2833 return null; 2834 } } 2836 | Popular Tags |