private RollingSegmentHandle readHeader(SegmentProperties headerInfo, SegmentHandle headerHandle) throws StreamSegmentException { byte[] readBuffer = new byte[(int) headerInfo.getLength()]; this.baseStorage.read(headerHandle, 0, readBuffer, 0, readBuffer.length); RollingSegmentHandle handle = HandleSerializer.deserialize(readBuffer, headerHandle); if (headerInfo.isSealed()) { handle.markSealed(); } return handle; }
/** * Updates the contents of this handle with information from the given one. * * @param source The RollingSegmentHandle to update from. */ synchronized void refresh(RollingSegmentHandle source) { Preconditions.checkArgument(source.getSegmentName().equals(this.getSegmentName()), "SegmentName mismatch."); if (this.readOnly == source.readOnly) { // Update the header handle, but only if both this handle and the source one have the same read-only flag. // Otherwise we risk attaching a read-only header handle to a read-write handle or vice-versa. this.headerHandle = source.headerHandle; } this.segmentChunks = new ArrayList<>(source.chunks()); setHeaderLength(source.getHeaderLength()); if (source.isSealed()) { markSealed(); } if (source.isDeleted()) { markDeleted(); } }
@Override public void seal(SegmentHandle handle) throws StreamSegmentException { val h = asWritableHandle(handle); ensureNotDeleted(h); long traceId = LoggerHelpers.traceEnter(log, "seal", handle); sealActiveChunk(h); SegmentHandle headerHandle = h.getHeaderHandle(); if (headerHandle != null) { this.baseStorage.seal(headerHandle); } h.markSealed(); log.debug("Sealed Header for '{}'.", h.getSegmentName()); LoggerHelpers.traceLeave(log, "seal", traceId, handle); }
/** * 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()); }
last.markSealed(); if (handle.getHeaderHandle() == null) { handle.markSealed();
h.markSealed(); Assert.assertTrue("Unexpected value for isSealed.", h.isSealed());