@Override public void close() throws IOException { stateMachineUpdater.stop(); LOG.info("{} closes. The last applied log index is {}", getSelfId(), getLastAppliedIndex()); log.close(); storage.close(); }
@Override public void close() throws IOException { try { stateMachineUpdater.stopAndJoin(); } catch (InterruptedException e) { LOG.warn(getSelfId() + ": Interrupted when joining stateMachineUpdater", e); } LOG.info("{} closes. The last applied log index is {}", getSelfId(), getLastAppliedIndex()); log.close(); storage.close(); }
@Override public void close() throws IOException { try(AutoCloseableLock writeLock = writeLock()) { super.close(); cache.clear(); } fileLogWorker.close(); storage.close(); }
@Override public void close() throws IOException { try(AutoCloseableLock writeLock = writeLock()) { super.close(); cache.clear(); } fileLogWorker.close(); storage.close(); }
@Test public void testZeroSizeInProgressFile() throws Exception { final RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR); final File file = storage.getStorageDir().getOpenLogFile(0); storage.close(); // create zero size in-progress file LOG.info("file: " + file); Assert.assertTrue(file.createNewFile()); final Path path = file.toPath(); Assert.assertTrue(Files.exists(path)); Assert.assertEquals(0, Files.size(path)); // getLogSegmentFiles should remove it. final List<RaftStorageDirectory.LogPathAndIndex> logs = storage.getStorageDir().getLogSegmentFiles(); Assert.assertEquals(0, logs.size()); Assert.assertFalse(Files.exists(path)); } }
@Test public void testZeroSizeInProgressFile() throws Exception { final RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR); final File file = storage.getStorageDir().getOpenLogFile(0); storage.close(); // create zero size in-progress file LOG.info("file: " + file); Assert.assertTrue(file.createNewFile()); final Path path = file.toPath(); Assert.assertTrue(Files.exists(path)); Assert.assertEquals(0, Files.size(path)); // getLogSegmentFiles should remove it. final List<RaftStorageDirectory.LogPathAndIndex> logs = storage.getStorageDir().getLogSegmentFiles(); Assert.assertEquals(0, logs.size()); Assert.assertFalse(Files.exists(path)); } }
@Test public void testNotExistent() throws IOException { FileUtils.deleteFully(storageDir); // we will format the empty directory RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR); Assert.assertEquals(StorageState.NORMAL, storage.getState()); try { new RaftStorage(storageDir, StartupOption.FORMAT).close(); Assert.fail("the format should fail since the storage is still locked"); } catch (IOException e) { Assert.assertTrue(e.getMessage().contains("directory is already locked")); } storage.close(); FileUtils.deleteFully(storageDir); Assert.assertTrue(storageDir.createNewFile()); try { new RaftStorage(storageDir, StartupOption.REGULAR); Assert.fail(); } catch (IOException e) { Assert.assertTrue( e.getMessage().contains(StorageState.NON_EXISTENT.name())); } }
storage.close();
@Test public void testNotExistent() throws IOException { FileUtils.deleteFully(storageDir); // we will format the empty directory RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR); Assert.assertEquals(StorageState.NORMAL, storage.getState()); try { new RaftStorage(storageDir, StartupOption.FORMAT).close(); Assert.fail("the format should fail since the storage is still locked"); } catch (IOException e) { Assert.assertTrue(e.getMessage().contains("directory is already locked")); } storage.close(); FileUtils.deleteFully(storageDir); Assert.assertTrue(storageDir.createNewFile()); try { new RaftStorage(storageDir, StartupOption.REGULAR); Assert.fail(); } catch (IOException e) { Assert.assertTrue( e.getMessage().contains(StorageState.NON_EXISTENT.name())); } }
RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR); Assert.assertEquals(StorageState.NORMAL, storage.getState()); storage.close(); new RaftStorage(storageDir, StartupOption.FORMAT).close(); Assert.assertTrue(sd.getMetaFile().exists()); Assert.assertTrue(sd.getMetaTmpFile().createNewFile()); Assert.assertTrue(sd.getMetaFile().exists()); } finally { storage.close();
storage.close(); return file;
/** * Test basic functionality: write several log entries, then read */ @Test public void testReadWriteLog() throws IOException { final RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR); File openSegment = storage.getStorageDir().getOpenLogFile(0); long size = SegmentedRaftLog.HEADER_BYTES.length; final LogEntryProto[] entries = new LogEntryProto[100]; try (LogOutputStream out = new LogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, bufferSize)) { size += writeMessages(entries, out); } finally { storage.close(); } Assert.assertEquals(size, openSegment.length()); LogEntryProto[] readEntries = readLog(openSegment, 0, RaftServerConstants.INVALID_LOG_INDEX, true); Assert.assertArrayEquals(entries, readEntries); }
/** * Test basic functionality: write several log entries, then read */ @Test public void testReadWriteLog() throws IOException { final RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR); File openSegment = storage.getStorageDir().getOpenLogFile(0); long size = SegmentedRaftLogFormat.getHeaderLength(); final LogEntryProto[] entries = new LogEntryProto[100]; try (LogOutputStream out = new LogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, bufferSize)) { size += writeMessages(entries, out); } finally { storage.close(); } Assert.assertEquals(size, openSegment.length()); LogEntryProto[] readEntries = readLog(openSegment, 0, RaftServerConstants.INVALID_LOG_INDEX, true); Assert.assertArrayEquals(entries, readEntries); }
private void testLoadSegment(boolean loadInitial, boolean isLastEntryPartiallyWritten) throws Exception { // load an open segment final File openSegmentFile = prepareLog(true, 0, 100, 0, isLastEntryPartiallyWritten); RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR); LogSegment openSegment = LogSegment.loadSegment(storage, openSegmentFile, 0, INVALID_LOG_INDEX, true, loadInitial, null); final int delta = isLastEntryPartiallyWritten? 1: 0; checkLogSegment(openSegment, 0, 99 - delta, true, openSegmentFile.length(), 0); storage.close(); // for open segment we currently always keep log entries in the memory Assert.assertEquals(0, openSegment.getLoadingTimes()); // load a closed segment (1000-1099) final File closedSegmentFile = prepareLog(false, 1000, 100, 1, false); LogSegment closedSegment = LogSegment.loadSegment(storage, closedSegmentFile, 1000, 1099, false, loadInitial, null); checkLogSegment(closedSegment, 1000, 1099, false, closedSegment.getTotalSize(), 1); Assert.assertEquals(loadInitial ? 0 : 1, closedSegment.getLoadingTimes()); }
private void testLoadSegment(boolean loadInitial) throws Exception { // load an open segment File openSegmentFile = prepareLog(true, 0, 100, 0); RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR); LogSegment openSegment = LogSegment.loadSegment(storage, openSegmentFile, 0, INVALID_LOG_INDEX, true, loadInitial, null); checkLogSegment(openSegment, 0, 99, true, openSegmentFile.length(), 0); storage.close(); // for open segment we currently always keep log entries in the memory Assert.assertEquals(0, openSegment.getLoadingTimes()); // load a closed segment (1000-1099) File closedSegmentFile = prepareLog(false, 1000, 100, 1); LogSegment closedSegment = LogSegment.loadSegment(storage, closedSegmentFile, 1000, 1099, false, loadInitial, null); checkLogSegment(closedSegment, 1000, 1099, false, closedSegment.getTotalSize(), 1); Assert.assertEquals(loadInitial ? 0 : 1, closedSegment.getLoadingTimes()); }
private File prepareLog(boolean isOpen, long start, int size, long term) throws IOException { RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR); File file = isOpen ? storage.getStorageDir().getOpenLogFile(start) : storage.getStorageDir().getClosedLogFile(start, start + size - 1); LogEntryProto[] entries = new LogEntryProto[size]; try (LogOutputStream out = new LogOutputStream(file, false, segmentMaxSize, preallocatedSize, bufferSize)) { for (int i = 0; i < size; i++) { SimpleOperation op = new SimpleOperation("m" + i); entries[i] = ProtoUtils.toLogEntryProto(op.getLogEntryContent(), term, i + start, clientId, callId); out.write(entries[i]); } } storage.close(); return file; }
@Test public void testMetaFile() throws Exception { RaftStorage storage = new RaftStorage(storageDir, StartupOption.FORMAT); File m = storage.getStorageDir().getMetaFile(); Assert.assertTrue(m.exists()); MetaFile metaFile = new MetaFile(m); Assert.assertEquals(MetaFile.DEFAULT_TERM, metaFile.getTerm()); Assert.assertEquals(MetaFile.EMPTY_VOTEFOR, metaFile.getVotedFor()); metaFile.set(123, "peer1"); metaFile.readFile(); Assert.assertEquals(123, metaFile.getTerm()); Assert.assertEquals("peer1", metaFile.getVotedFor()); MetaFile metaFile2 = new MetaFile(m); Assert.assertFalse((Boolean) Whitebox.getInternalState(metaFile2, "loaded")); Assert.assertEquals(123, metaFile.getTerm()); Assert.assertEquals("peer1", metaFile.getVotedFor()); storage.close(); }
@Test public void testMetaFile() throws Exception { RaftStorage storage = new RaftStorage(storageDir, StartupOption.FORMAT); File m = storage.getStorageDir().getMetaFile(); Assert.assertTrue(m.exists()); MetaFile metaFile = new MetaFile(m); Assert.assertEquals(MetaFile.DEFAULT_TERM, metaFile.getTerm()); Assert.assertEquals(MetaFile.EMPTY_VOTEFOR, metaFile.getVotedFor()); metaFile.set(123, "peer1"); metaFile.readFile(); Assert.assertEquals(123, metaFile.getTerm()); Assert.assertEquals("peer1", metaFile.getVotedFor()); MetaFile metaFile2 = new MetaFile(m); Assert.assertFalse((Boolean) Whitebox.getInternalState(metaFile2, "loaded")); Assert.assertEquals(123, metaFile.getTerm()); Assert.assertEquals("peer1", metaFile.getVotedFor()); storage.close(); }
@Test public void testAppendLog() throws IOException { final RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR); File openSegment = storage.getStorageDir().getOpenLogFile(0); LogEntryProto[] entries = new LogEntryProto[200]; try (LogOutputStream out = new LogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, bufferSize)) { for (int i = 0; i < 100; i++) { SimpleOperation m = new SimpleOperation("m" + i); entries[i] = ServerProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i); out.write(entries[i]); } } try (LogOutputStream out = new LogOutputStream(openSegment, true, segmentMaxSize, preallocatedSize, bufferSize)) { for (int i = 100; i < 200; i++) { SimpleOperation m = new SimpleOperation("m" + i); entries[i] = ServerProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i); out.write(entries[i]); } } LogEntryProto[] readEntries = readLog(openSegment, 0, RaftServerConstants.INVALID_LOG_INDEX, true); Assert.assertArrayEquals(entries, readEntries); storage.close(); }
@Test public void testAppendLog() throws IOException { final RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR); File openSegment = storage.getStorageDir().getOpenLogFile(0); LogEntryProto[] entries = new LogEntryProto[200]; try (LogOutputStream out = new LogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, bufferSize)) { for (int i = 0; i < 100; i++) { SimpleOperation m = new SimpleOperation("m" + i); entries[i] = ProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i, clientId, callId); out.write(entries[i]); } } try (LogOutputStream out = new LogOutputStream(openSegment, true, segmentMaxSize, preallocatedSize, bufferSize)) { for (int i = 100; i < 200; i++) { SimpleOperation m = new SimpleOperation("m" + i); entries[i] = ProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i, clientId, callId); out.write(entries[i]); } } LogEntryProto[] readEntries = readLog(openSegment, 0, RaftServerConstants.INVALID_LOG_INDEX, true); Assert.assertArrayEquals(entries, readEntries); storage.close(); }