@Test public void testStoreDescriptor() throws IOException { StoreConfig config = new StoreConfig(new VerifiableProperties(new Properties())); File tempDir = StoreTestUtils.createTempDirectory("storeDir"); File storeDescriptorFile = new File(tempDir.getAbsolutePath(), StoreDescriptor.STORE_DESCRIPTOR_FILENAME); StoreDescriptor storeDescriptor = new StoreDescriptor(tempDir.getAbsolutePath(), config); StoreDescriptor newStoreDescriptor = new StoreDescriptor(tempDir.getAbsolutePath(), config); assertEquals("IncarnationId mismatch ", storeDescriptor.getIncarnationId(), newStoreDescriptor.getIncarnationId()); assertTrue("Store descriptor file could not be deleted", storeDescriptorFile.delete()); UUID incarnationIdUUID = UUID.randomUUID(); byte[] toBytes = getBytesForStoreDescriptor(StoreDescriptor.VERSION_0, incarnationIdUUID); storeDescriptorFile = new File(tempDir.getAbsolutePath(), StoreDescriptor.STORE_DESCRIPTOR_FILENAME); assertTrue("Store descriptor file could not be created", storeDescriptorFile.createNewFile()); createStoreFile(storeDescriptorFile, toBytes); storeDescriptor = new StoreDescriptor(tempDir.getAbsolutePath(), config); assertEquals("IncarnationId mismatch ", incarnationIdUUID, storeDescriptor.getIncarnationId()); assertTrue("Store descriptor file could not be deleted", storeDescriptorFile.delete()); toBytes = getBytesForStoreDescriptor((short) 1, incarnationIdUUID); assertTrue("Store descriptor file could not be created", storeDescriptorFile.createNewFile()); createStoreFile(storeDescriptorFile, toBytes); try { new StoreDescriptor(tempDir.getAbsolutePath(), config); fail("Wrong version should have thrown IllegalArgumentException "); } catch (IllegalArgumentException e) { } assertTrue("Store descriptor file could not be deleted", storeDescriptorFile.delete()); assertTrue("Store descriptor file could not be created", storeDescriptorFile.createNewFile()); toBytes = getBytesForStoreDescriptor(StoreDescriptor.VERSION_0, incarnationIdUUID); CrcOutputStream crcOutputStream = new CrcOutputStream(new FileOutputStream(storeDescriptorFile)); DataOutputStream dataOutputStream = new DataOutputStream(crcOutputStream); dataOutputStream.write(toBytes); dataOutputStream.writeLong(crcOutputStream.getValue() + 1); dataOutputStream.close(); try { new StoreDescriptor(tempDir.getAbsolutePath(), config); fail("Wrong CRC should have thrown IllegalStateException "); } catch (IllegalStateException e) { } }
StoreDescriptor(String dataDir, StoreConfig config) throws IOException { File storeDescriptorFile = new File(dataDir, STORE_DESCRIPTOR_FILENAME); if (storeDescriptorFile.exists()) { CrcInputStream crcStream = new CrcInputStream(new FileInputStream(storeDescriptorFile)); DataInputStream stream = new DataInputStream(crcStream); short version = stream.readShort(); switch (version) { case VERSION_0: String incarnationIdStr = Utils.readIntString(stream); incarnationId = UUID.fromString(incarnationIdStr); long crc = crcStream.getValue(); long crcValueInFile = stream.readLong(); if (crc != crcValueInFile) { throw new IllegalStateException( "CRC mismatch for StoreDescriptor. CRC of the stream " + crc + ", CRC from file " + crcValueInFile); } break; default: throw new IllegalArgumentException("Unrecognized version in StoreDescriptor: " + version); } if (config.storeSetFilePermissionEnabled) { Files.setPosixFilePermissions(storeDescriptorFile.toPath(), config.storeOperationFilePermission); } } else { incarnationId = UUID.randomUUID(); File tempFile = new File(dataDir, STORE_DESCRIPTOR_FILENAME + ".tmp"); File actual = new File(dataDir, STORE_DESCRIPTOR_FILENAME); if (tempFile.createNewFile()) { FileOutputStream fileStream = new FileOutputStream(tempFile); CrcOutputStream crc = new CrcOutputStream(fileStream); DataOutputStream writer = new DataOutputStream(crc); writer.write(toBytes()); long crcValue = crc.getValue(); writer.writeLong(crcValue); fileStream.getChannel().force(true); if (!tempFile.renameTo(actual)) { throw new IllegalStateException( "File " + tempFile.getAbsolutePath() + " renaming to " + actual.getAbsolutePath() + " failed "); } if (config.storeSetFilePermissionEnabled) { Files.setPosixFilePermissions(actual.toPath(), config.storeOperationFilePermission); } } else { throw new IllegalStateException("File " + tempFile.getAbsolutePath() + " creation failed "); } } }
StoreDescriptor { StoreDescriptor(String dataDir, StoreConfig config) throws IOException { File storeDescriptorFile = new File(dataDir, STORE_DESCRIPTOR_FILENAME); if (storeDescriptorFile.exists()) { CrcInputStream crcStream = new CrcInputStream(new FileInputStream(storeDescriptorFile)); DataInputStream stream = new DataInputStream(crcStream); short version = stream.readShort(); switch (version) { case VERSION_0: String incarnationIdStr = Utils.readIntString(stream); incarnationId = UUID.fromString(incarnationIdStr); long crc = crcStream.getValue(); long crcValueInFile = stream.readLong(); if (crc != crcValueInFile) { throw new IllegalStateException( "CRC mismatch for StoreDescriptor. CRC of the stream " + crc + ", CRC from file " + crcValueInFile); } break; default: throw new IllegalArgumentException("Unrecognized version in StoreDescriptor: " + version); } if (config.storeSetFilePermissionEnabled) { Files.setPosixFilePermissions(storeDescriptorFile.toPath(), config.storeOperationFilePermission); } } else { incarnationId = UUID.randomUUID(); File tempFile = new File(dataDir, STORE_DESCRIPTOR_FILENAME + ".tmp"); File actual = new File(dataDir, STORE_DESCRIPTOR_FILENAME); if (tempFile.createNewFile()) { FileOutputStream fileStream = new FileOutputStream(tempFile); CrcOutputStream crc = new CrcOutputStream(fileStream); DataOutputStream writer = new DataOutputStream(crc); writer.write(toBytes()); long crcValue = crc.getValue(); writer.writeLong(crcValue); fileStream.getChannel().force(true); if (!tempFile.renameTo(actual)) { throw new IllegalStateException( "File " + tempFile.getAbsolutePath() + " renaming to " + actual.getAbsolutePath() + " failed "); } if (config.storeSetFilePermissionEnabled) { Files.setPosixFilePermissions(actual.toPath(), config.storeOperationFilePermission); } } else { throw new IllegalStateException("File " + tempFile.getAbsolutePath() + " creation failed "); } } } }
StoreDescriptor { StoreDescriptor(String dataDir, StoreConfig config) throws IOException { File storeDescriptorFile = new File(dataDir, STORE_DESCRIPTOR_FILENAME); if (storeDescriptorFile.exists()) { CrcInputStream crcStream = new CrcInputStream(new FileInputStream(storeDescriptorFile)); DataInputStream stream = new DataInputStream(crcStream); short version = stream.readShort(); switch (version) { case VERSION_0: String incarnationIdStr = Utils.readIntString(stream); incarnationId = UUID.fromString(incarnationIdStr); long crc = crcStream.getValue(); long crcValueInFile = stream.readLong(); if (crc != crcValueInFile) { throw new IllegalStateException( "CRC mismatch for StoreDescriptor. CRC of the stream " + crc + ", CRC from file " + crcValueInFile); } break; default: throw new IllegalArgumentException("Unrecognized version in StoreDescriptor: " + version); } if (config.storeSetFilePermissionEnabled) { Files.setPosixFilePermissions(storeDescriptorFile.toPath(), config.storeOperationFilePermission); } } else { incarnationId = UUID.randomUUID(); File tempFile = new File(dataDir, STORE_DESCRIPTOR_FILENAME + ".tmp"); File actual = new File(dataDir, STORE_DESCRIPTOR_FILENAME); if (tempFile.createNewFile()) { FileOutputStream fileStream = new FileOutputStream(tempFile); CrcOutputStream crc = new CrcOutputStream(fileStream); DataOutputStream writer = new DataOutputStream(crc); writer.write(toBytes()); long crcValue = crc.getValue(); writer.writeLong(crcValue); fileStream.getChannel().force(true); if (!tempFile.renameTo(actual)) { throw new IllegalStateException( "File " + tempFile.getAbsolutePath() + " renaming to " + actual.getAbsolutePath() + " failed "); } if (config.storeSetFilePermissionEnabled) { Files.setPosixFilePermissions(actual.toPath(), config.storeOperationFilePermission); } } else { throw new IllegalStateException("File " + tempFile.getAbsolutePath() + " creation failed "); } } } StoreDescriptor(String dataDir, StoreConfig config); }
StoreDescriptor { StoreDescriptor(String dataDir, StoreConfig config) throws IOException { File storeDescriptorFile = new File(dataDir, STORE_DESCRIPTOR_FILENAME); if (storeDescriptorFile.exists()) { CrcInputStream crcStream = new CrcInputStream(new FileInputStream(storeDescriptorFile)); DataInputStream stream = new DataInputStream(crcStream); short version = stream.readShort(); switch (version) { case VERSION_0: String incarnationIdStr = Utils.readIntString(stream); incarnationId = UUID.fromString(incarnationIdStr); long crc = crcStream.getValue(); long crcValueInFile = stream.readLong(); if (crc != crcValueInFile) { throw new IllegalStateException( "CRC mismatch for StoreDescriptor. CRC of the stream " + crc + ", CRC from file " + crcValueInFile); } break; default: throw new IllegalArgumentException("Unrecognized version in StoreDescriptor: " + version); } if (config.storeSetFilePermissionEnabled) { Files.setPosixFilePermissions(storeDescriptorFile.toPath(), config.storeOperationFilePermission); } } else { incarnationId = UUID.randomUUID(); File tempFile = new File(dataDir, STORE_DESCRIPTOR_FILENAME + ".tmp"); File actual = new File(dataDir, STORE_DESCRIPTOR_FILENAME); if (tempFile.createNewFile()) { FileOutputStream fileStream = new FileOutputStream(tempFile); CrcOutputStream crc = new CrcOutputStream(fileStream); DataOutputStream writer = new DataOutputStream(crc); writer.write(toBytes()); long crcValue = crc.getValue(); writer.writeLong(crcValue); fileStream.getChannel().force(true); if (!tempFile.renameTo(actual)) { throw new IllegalStateException( "File " + tempFile.getAbsolutePath() + " renaming to " + actual.getAbsolutePath() + " failed "); } if (config.storeSetFilePermissionEnabled) { Files.setPosixFilePermissions(actual.toPath(), config.storeOperationFilePermission); } } else { throw new IllegalStateException("File " + tempFile.getAbsolutePath() + " creation failed "); } } } StoreDescriptor(String dataDir, StoreConfig config);  }
StoreDescriptor { StoreDescriptor(String dataDir, StoreConfig config) throws IOException { File storeDescriptorFile = new File(dataDir, STORE_DESCRIPTOR_FILENAME); if (storeDescriptorFile.exists()) { CrcInputStream crcStream = new CrcInputStream(new FileInputStream(storeDescriptorFile)); DataInputStream stream = new DataInputStream(crcStream); short version = stream.readShort(); switch (version) { case VERSION_0: String incarnationIdStr = Utils.readIntString(stream); incarnationId = UUID.fromString(incarnationIdStr); long crc = crcStream.getValue(); long crcValueInFile = stream.readLong(); if (crc != crcValueInFile) { throw new IllegalStateException( "CRC mismatch for StoreDescriptor. CRC of the stream " + crc + ", CRC from file " + crcValueInFile); } break; default: throw new IllegalArgumentException("Unrecognized version in StoreDescriptor: " + version); } if (config.storeSetFilePermissionEnabled) { Files.setPosixFilePermissions(storeDescriptorFile.toPath(), config.storeOperationFilePermission); } } else { incarnationId = UUID.randomUUID(); File tempFile = new File(dataDir, STORE_DESCRIPTOR_FILENAME + ".tmp"); File actual = new File(dataDir, STORE_DESCRIPTOR_FILENAME); if (tempFile.createNewFile()) { FileOutputStream fileStream = new FileOutputStream(tempFile); CrcOutputStream crc = new CrcOutputStream(fileStream); DataOutputStream writer = new DataOutputStream(crc); writer.write(toBytes()); long crcValue = crc.getValue(); writer.writeLong(crcValue); fileStream.getChannel().force(true); if (!tempFile.renameTo(actual)) { throw new IllegalStateException( "File " + tempFile.getAbsolutePath() + " renaming to " + actual.getAbsolutePath() + " failed "); } if (config.storeSetFilePermissionEnabled) { Files.setPosixFilePermissions(actual.toPath(), config.storeOperationFilePermission); } } else { throw new IllegalStateException("File " + tempFile.getAbsolutePath() + " creation failed "); } } } StoreDescriptor(String dataDir, StoreConfig config);   }