/** * Tests the ability of the MetadataStore to create a new Segment if the Segment already exists. */ @Test public void testCreateSegmentAlreadyExists() { final String segmentName = "NewSegment"; final Map<UUID, Long> originalAttributes = ImmutableMap.of(UUID.randomUUID(), 123L, Attributes.EVENT_COUNT, 1L); final Map<UUID, Long> expectedAttributes = Attributes.getCoreNonNullAttributes(originalAttributes); final Collection<AttributeUpdate> correctAttributeUpdates = originalAttributes.entrySet().stream() .map(e -> new AttributeUpdate(e.getKey(), AttributeUpdateType.Replace, e.getValue())) .collect(Collectors.toList()); final Map<UUID, Long> badAttributes = Collections.singletonMap(UUID.randomUUID(), 456L); final Collection<AttributeUpdate> badAttributeUpdates = badAttributes.entrySet().stream() .map(e -> new AttributeUpdate(e.getKey(), AttributeUpdateType.Replace, e.getValue())) .collect(Collectors.toList()); @Cleanup TestContext context = createTestContext(); // Create a segment. context.getMetadataStore().createSegment(segmentName, correctAttributeUpdates, TIMEOUT).join(); // Try to create it again. AssertExtensions.assertSuppliedFutureThrows( "createSegment did not fail when Segment already exists.", () -> context.getMetadataStore().createSegment(segmentName, badAttributeUpdates, TIMEOUT), ex -> ex instanceof StreamSegmentExistsException); val si = context.getMetadataStore().getSegmentInfo(segmentName, TIMEOUT).join(); AssertExtensions.assertMapEquals("Unexpected attributes after failed attempt to recreate correctly created segment", expectedAttributes, si.getAttributes()); }
@SneakyThrows private void assertSegmentCreated(String segmentName, Collection<AttributeUpdate> attributeUpdates, TestContext context) { SegmentProperties sp; try { sp = context.getMetadataStore().getSegmentInfo(segmentName, TIMEOUT).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS); } catch (Throwable ex) { ex = Exceptions.unwrap(ex); if (ex instanceof StreamSegmentNotExistsException) { Assert.fail("No segment has been created in the Storage for " + segmentName); } throw ex; } long segmentId = context.getMetadata().getStreamSegmentId(segmentName, false); Assert.assertEquals("Segment '" + segmentName + "' has been registered in the metadata.", ContainerMetadata.NO_STREAM_SEGMENT_ID, segmentId); val attributes = Attributes.getCoreNonNullAttributes(toAttributes(attributeUpdates)); AssertExtensions.assertMapEquals("Wrong attributes.", attributes, sp.getAttributes()); }
private void write00(SegmentInfo s, RevisionDataOutput output) throws IOException { output.writeLong(s.getSegmentId()); SegmentProperties sp = s.getProperties(); output.writeUTF(sp.getName()); output.writeLong(sp.getLength()); output.writeLong(sp.getStartOffset()); output.writeBoolean(sp.isSealed()); // We only serialize Core Attributes. Extended Attributes can be retrieved from the AttributeIndex. output.writeMap(Attributes.getCoreNonNullAttributes(sp.getAttributes()), RevisionDataOutput::writeUUID, RevisionDataOutput::writeLong); }
val coreAttributes = Attributes.getCoreNonNullAttributes(expectedAttributes); // We expect extended attributes to be dropped in this case. SegmentMetadataComparer.assertSameAttributes("Unexpected attributes after eviction.", coreAttributes, sp);
expectedAttributes = Attributes.getCoreNonNullAttributes(expectedAttributes); // We expect extended attributes to be dropped in this case. localContainer.createStreamSegment(segmentName, initialAttributes, TIMEOUT).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS); SegmentProperties sp = localContainer.getStreamSegmentInfo(segmentName, TIMEOUT).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS); expectedAttributes = Attributes.getCoreNonNullAttributes(expectedAttributes); // We expect extended attributes to be dropped in this case. SegmentMetadataComparer.assertSameAttributes("Unexpected attributes after eviction & resurrection.", expectedAttributes, sp); expectedAttributes = Attributes.getCoreNonNullAttributes(expectedAttributes); // We expect extended attributes to be dropped in this case. SegmentMetadataComparer.assertSameAttributes("Unexpected attributes after deletion and re-creation.", expectedAttributes, sp);
private void writeSegmentMetadata00(RevisionDataOutput output, SegmentMetadata sm) throws IOException { output.writeLong(sm.getId()); output.writeUTF(sm.getName()); output.writeLong(sm.getLength()); output.writeLong(sm.getStorageLength()); output.writeBoolean(sm.isMerged()); output.writeBoolean(sm.isSealed()); output.writeBoolean(sm.isSealedInStorage()); output.writeBoolean(sm.isDeleted()); output.writeBoolean(sm.isDeletedInStorage()); output.writeLong(sm.getLastModified().getTime()); output.writeLong(sm.getStartOffset()); // We only serialize Core Attributes. Extended Attributes can be retrieved from the AttributeIndex. output.writeMap(Attributes.getCoreNonNullAttributes(sm.getAttributes()), RevisionDataOutput::writeUUID, RevisionDataOutput::writeLong); }
val expectedAttributes = Attributes.getCoreNonNullAttributes(segmentInfo.getAttributes()); SegmentMetadataComparer.assertSameAttributes("Unexpected attributes when Segment exists in Storage", expectedAttributes, inStorageInfo); Assert.assertEquals("Not expecting any segments to be mapped.", 0, context.getNonPinnedMappedSegmentCount());