private void sealActiveChunk(RollingSegmentHandle handle) throws StreamSegmentException { SegmentHandle activeChunk = handle.getActiveChunkHandle(); SegmentChunk last = handle.lastChunk(); if (activeChunk != null && !last.isSealed()) { this.baseStorage.seal(activeChunk); handle.setActiveChunkHandle(null); last.markSealed(); log.debug("Sealed active SegmentChunk '{}' for '{}'.", activeChunk.getSegmentName(), handle.getSegmentName()); } }
@Override public SegmentHandle openWrite(String segmentName) throws StreamSegmentException { long traceId = LoggerHelpers.traceEnter(log, "openWrite", segmentName); val handle = openHandle(segmentName, false); // Finally, open the Active SegmentChunk for writing. SegmentChunk last = handle.lastChunk(); if (last != null && !last.isSealed()) { val activeHandle = this.baseStorage.openWrite(last.getName()); handle.setActiveChunkHandle(activeHandle); } LoggerHelpers.traceLeave(log, "openWrite", traceId, handle); return handle; }
private void unsealLastChunkIfNecessary(RollingSegmentHandle handle) throws StreamSegmentException { SegmentChunk last = handle.lastChunk(); if (last == null || !last.isSealed()) { // Nothing to do. return; } SegmentHandle activeChunk = handle.getActiveChunkHandle(); boolean needsHandleUpdate = activeChunk == null; if (needsHandleUpdate) { // We didn't have a pointer to the active chunk's Handle because the chunk was sealed before open-write. activeChunk = this.baseStorage.openWrite(last.getName()); } try { this.baseStorage.unseal(activeChunk); } catch (UnsupportedOperationException e) { log.warn("Unable to unseal SegmentChunk '{}' since base storage does not support unsealing.", last); return; } last.markUnsealed(); if (needsHandleUpdate) { activeChunk = this.baseStorage.openWrite(last.getName()); handle.setActiveChunkHandle(activeChunk); } log.debug("Unsealed active SegmentChunk '{}' for '{}'.", activeChunk.getSegmentName(), handle.getSegmentName()); }
() -> h.setActiveChunkHandle(new TestHandle("foo", false)), ex -> ex instanceof IllegalStateException); AssertExtensions.assertThrows( "setActiveChunkHandle accepted a handle that does not match the last SegmentChunk's name.", () -> h.setActiveChunkHandle(new TestHandle("foo", false)), ex -> ex instanceof IllegalArgumentException); () -> h.setActiveChunkHandle(new TestHandle(chunkName, true)), ex -> ex instanceof IllegalArgumentException); h.setActiveChunkHandle(activeHandle); Assert.assertEquals("Unexpected value from getActiveChunkHandle.", activeHandle, h.getActiveChunkHandle());
/** * Tests the ability of the Handle to refresh based on information from another similar handle. */ @Test public void testRefresh() { val headerHandle = new TestHandle(HEADER_NAME, true); val target = new RollingSegmentHandle(headerHandle, DEFAULT_ROLLING_POLICY, Collections.singletonList(new SegmentChunk("s1", 0L))); val source = new RollingSegmentHandle(headerHandle, DEFAULT_ROLLING_POLICY, Arrays.asList( new SegmentChunk("s1", 0L), new SegmentChunk("s2", 100L))); source.chunks().get(0).setLength(100); source.markSealed(); source.setHeaderLength(1000); source.setActiveChunkHandle(new TestHandle(source.lastChunk().getName(), false)); target.refresh(source); Assert.assertEquals("Unexpected getHeaderLength()", source.getHeaderLength(), target.getHeaderLength()); AssertExtensions.assertListEquals("Unexpected chunks()", source.chunks(), target.chunks(), Object::equals); Assert.assertTrue("Unexpected isSealed.", target.isSealed()); Assert.assertNull("Not expecting any ActiveSegmentHandle to be copied.", target.getActiveChunkHandle()); }