diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/DiskInitFile.java b/geode-core/src/main/java/org/apache/geode/internal/cache/DiskInitFile.java index c6baad2ce398..8087b518bbe4 100644 --- a/geode-core/src/main/java/org/apache/geode/internal/cache/DiskInitFile.java +++ b/geode-core/src/main/java/org/apache/geode/internal/cache/DiskInitFile.java @@ -1410,9 +1410,14 @@ private void compact() { boolean success = false; try { // create the new file - openRAF(); + openRAF(true); // fill the new file with data writeLiveData(); + + if (shouldCloseAndReopenRAF()) { + ifRAF.close(); + openRAF(false); + } success = true; // delete the old file @@ -1424,6 +1429,10 @@ private void compact() { if (logger.isDebugEnabled()) { logger.debug("Exception compacting init file {}", this, e); } + } catch (IOException e) { + if (logger.isDebugEnabled()) { + logger.debug("Exception when closing init file {}", this, e); + } } finally { if (!success) { // if we failed @@ -1440,7 +1449,7 @@ private void compact() { "could not rename file " + tmpFile + " to " + ifFile, parent); } // reopen the old file since we couldn't write the new one - openRAF(); + openRAF(false); // reset the counts to 0 so we will try a compaction again // in the future but not right away. ifLiveRecordCount = 0; @@ -1449,7 +1458,7 @@ private void compact() { } } else { // reopen the old file since we couldn't rename it - openRAF(); + openRAF(false); // reset the counts to 0 so we will try a compaction again // in the future but not right away. ifLiveRecordCount = 0; @@ -1461,14 +1470,14 @@ private void compact() { } } - private void openRAF() { + private void openRAF(boolean isCompaction) { if (DiskStoreImpl.PREALLOCATE_IF) { - openRAF2(); + openRAF2(isCompaction); return; } try { - ifRAF = new RandomAccessFile(ifFile, getFileMode()); + ifRAF = new RandomAccessFile(ifFile, isCompaction ? getCompactionFileMode() : getFileMode()); long len = ifRAF.length(); if (len != 0) { ifRAF.seek(len); @@ -1484,9 +1493,17 @@ protected String getFileMode() { return DiskStoreImpl.SYNC_IF_WRITES ? "rwd" : "rw"; } - private void openRAF2() { + protected String getCompactionFileMode() { + return DiskStoreImpl.SYNC_IF_COMPACTION_WRITES ? "rwd" : "rw"; + } + + protected boolean shouldCloseAndReopenRAF() { + return !getCompactionFileMode().equals(getFileMode()); + } + + private void openRAF2(boolean isCompaction) { try { - ifRAF = new RandomAccessFile(ifFile, getFileMode()); + ifRAF = new RandomAccessFile(ifFile, isCompaction ? getCompactionFileMode() : getFileMode()); long len = ifRAF.length(); if (len != 0) { // this.ifRAF.seek(len); @@ -1810,7 +1827,7 @@ private byte[] pmidToBytes(PersistentMemberID id) { && !this.parent.isOfflineModify()) { dump(); } - openRAF(); + openRAF(false); if (!this.parent.isOffline() || this.parent.isOfflineCompacting()) { if (didNotExist) { this.parent.setDiskStoreID(DiskStoreID.random()); diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/DiskStoreImpl.java b/geode-core/src/main/java/org/apache/geode/internal/cache/DiskStoreImpl.java index 8415a00b34ca..594ba0fabf4b 100644 --- a/geode-core/src/main/java/org/apache/geode/internal/cache/DiskStoreImpl.java +++ b/geode-core/src/main/java/org/apache/geode/internal/cache/DiskStoreImpl.java @@ -267,10 +267,16 @@ public static boolean getBoolean(String sysProp, boolean def) { public static boolean SET_IGNORE_PREALLOCATE = false; /** - * This system property turns on synchronous writes just the the init file. + * This system property turns on synchronous writes just the init file. */ - static final boolean SYNC_IF_WRITES = - Boolean.getBoolean(GeodeGlossary.GEMFIRE_PREFIX + "syncMetaDataWrites"); + static final boolean SYNC_IF_WRITES = true; + + + /** + * This system property turns on synchronous writes just for the init file during compaction. + */ + static final boolean SYNC_IF_COMPACTION_WRITES = + Boolean.getBoolean(GeodeGlossary.GEMFIRE_PREFIX + "syncMetaDataCompactionWrites"); /** * For testing - to keep track of files for which fallocate happened