use of io.pravega.segmentstore.contracts.AttributeId in project pravega by pravega.
the class MetadataStoreTestBase method testCreateSegmentAlreadyExists.
/**
* 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<AttributeId, Long> originalAttributes = ImmutableMap.of(AttributeId.randomUUID(), 123L, Attributes.EVENT_COUNT, 1L);
final Map<AttributeId, Long> expectedAttributes = getExpectedCoreAttributes(originalAttributes, SEGMENT_TYPE);
final Collection<AttributeUpdate> correctAttributeUpdates = originalAttributes.entrySet().stream().map(e -> new AttributeUpdate(e.getKey(), AttributeUpdateType.Replace, e.getValue())).collect(Collectors.toList());
final Map<AttributeId, Long> badAttributes = Collections.singletonMap(AttributeId.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, SEGMENT_TYPE, correctAttributeUpdates, TIMEOUT).join();
// Try to create it again.
AssertExtensions.assertSuppliedFutureThrows("createSegment did not fail when Segment already exists.", () -> context.getMetadataStore().createSegment(segmentName, SEGMENT_TYPE, 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());
}
use of io.pravega.segmentstore.contracts.AttributeId in project pravega by pravega.
the class SegmentAttributeIteratorTests method createTestData.
private TestData createTestData(int baseIteratorCount, int metadataAttributeCount) {
val rnd = new Random(0);
val builder = TestData.builder();
val expectedResult = new HashMap<AttributeId, Long>();
val sortedAttributeIds = new ArrayList<AttributeId>();
int nextAttributeId = 0;
// Create base iterators.
int iteratorSize = 0;
for (int i = 0; i < baseIteratorCount; i++) {
val indexItems = new ArrayList<Map.Entry<AttributeId, Long>>(iteratorSize);
for (int j = 0; j < iteratorSize; j++) {
AttributeId attributeId = AttributeId.uuid(nextAttributeId, nextAttributeId);
long attributeValue = rnd.nextLong();
indexItems.add(Maps.immutableEntry(attributeId, attributeValue));
sortedAttributeIds.add(attributeId);
expectedResult.put(attributeId, attributeValue);
nextAttributeId++;
}
builder.baseIteratorAttribute(indexItems);
// Next iterator will have one more item.
iteratorSize++;
}
// Create metadata
val metadata = new StreamSegmentMetadata("Segment", 0, 0);
metadata.setLength(0);
for (int i = 0; i < metadataAttributeCount; i++) {
AttributeId attributeId = baseIteratorCount <= 1 ? null : sortedAttributeIds.get(rnd.nextInt(sortedAttributeIds.size()));
if (attributeId == null) {
attributeId = AttributeId.uuid(nextAttributeId, nextAttributeId);
nextAttributeId++;
} else if (rnd.nextDouble() > ATTRIBUTE_OVERLAP_RATIO) {
// Do not reuse attribute id, but choose one nearby.
attributeId = AttributeId.uuid(attributeId.getBitGroup(0) + 1, attributeId.getBitGroup(1) + 1);
}
long attributeValue = rnd.nextLong();
expectedResult.put(attributeId, attributeValue);
metadata.updateAttributes(Collections.singletonMap(attributeId, attributeValue));
}
sortedAttributeIds.addAll(metadata.getAttributes().keySet());
sortedAttributeIds.sort(AttributeId::compareTo);
return builder.segmentMetadata(metadata).expectedResult(expectedResult.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).collect(Collectors.toList())).sortedAttributeIds(sortedAttributeIds).build();
}
use of io.pravega.segmentstore.contracts.AttributeId in project pravega by pravega.
the class SegmentAttributeIteratorTests method test.
private void test(TestData testData) {
for (int i = 0; i < testData.sortedAttributeIds.size() / 2; i++) {
AttributeId fromId = testData.sortedAttributeIds.get(i);
AttributeId toId = testData.sortedAttributeIds.get(testData.sortedAttributeIds.size() - i - 1);
val iterator = new SegmentAttributeIterator(testData.getAttributeIterator(fromId, toId), testData.segmentMetadata, fromId, toId);
val finalResult = new ArrayList<Map.Entry<AttributeId, Long>>();
val ids = new HashSet<AttributeId>();
iterator.forEachRemaining(intermediateResult -> {
for (val e : intermediateResult) {
Assert.assertTrue("Duplicate key found: " + e.getKey(), ids.add(e.getKey()));
finalResult.add(e);
}
}, executorService()).join();
val expectedResult = testData.expectedResult.stream().filter(e -> isBetween(e.getKey(), fromId, toId)).collect(Collectors.toList());
AssertExtensions.assertListEquals("Unexpected final result.", expectedResult, finalResult, (e1, e2) -> e1.getKey().equals(e2.getKey()) && e1.getValue().equals(e2.getValue()));
}
}
use of io.pravega.segmentstore.contracts.AttributeId in project pravega by pravega.
the class AttributeAggregatorTests method testAdd.
/**
* Tests the {@link AttributeAggregator#add} method with valid and invalid operations.
*/
@Test
public void testAdd() throws Exception {
final AttributeId extendedId = EXTENDED_ATTRIBUTE_IDS.get(0);
@Cleanup TestContext context = new TestContext(DEFAULT_CONFIG);
// We want to make sure we do not prematurely acknowledge anything.
context.dataSource.setCompleteMergeCallback((target, source) -> Assert.fail("Not expecting any merger callbacks yet."));
// Add non-Attributes.
context.aggregator.add(new StreamSegmentTruncateOperation(SEGMENT_ID, 1234));
// Add some attributes
context.aggregator.add(new StreamSegmentAppendOperation(SEGMENT_ID, new ByteArraySegment(new byte[123]), AttributeUpdateCollection.from(createAttributeUpdate(extendedId, 1))));
context.aggregator.add(new UpdateAttributesOperation(SEGMENT_ID, AttributeUpdateCollection.from(createAttributeUpdate(extendedId, 2))));
Assert.assertFalse("Unexpected value from mustFlush().", context.aggregator.mustFlush());
// Seal using operation.
context.aggregator.add(new StreamSegmentSealOperation(SEGMENT_ID));
Assert.assertTrue("Unexpected value from mustFlush() after sealing.", context.aggregator.mustFlush());
// Verify further adds fail.
AssertExtensions.assertThrows("No operations should be allowed after sealing.", () -> context.aggregator.add(new UpdateAttributesOperation(SEGMENT_ID, AttributeUpdateCollection.from(createAttributeUpdate(extendedId, 3)))), ex -> ex instanceof DataCorruptionException);
// Delete and verify nothing else changes.
context.segmentMetadata.markDeleted();
context.aggregator.add(new UpdateAttributesOperation(SEGMENT_ID, AttributeUpdateCollection.from(createAttributeUpdate(extendedId, 4))));
Assert.assertFalse("Unexpected value from mustFlush() after deleting.", context.aggregator.mustFlush());
}
use of io.pravega.segmentstore.contracts.AttributeId in project pravega by pravega.
the class StreamSegmentStoreTestBase method checkSegmentStatus.
private void checkSegmentStatus(HashMap<String, Long> segmentLengths, HashMap<String, Long> startOffsets, boolean expectSealed, boolean expectDeleted, long expectedAttributeValue, StreamSegmentStore store) {
for (Map.Entry<String, Long> e : segmentLengths.entrySet()) {
String segmentName = e.getKey();
if (expectDeleted) {
AssertExtensions.assertSuppliedFutureThrows("Segment '" + segmentName + "' was not deleted.", () -> store.getStreamSegmentInfo(segmentName, TIMEOUT), ex -> ex instanceof StreamSegmentNotExistsException);
} else {
SegmentProperties sp = store.getStreamSegmentInfo(segmentName, TIMEOUT).join();
long expectedStartOffset = startOffsets.getOrDefault(segmentName, 0L);
long expectedLength = e.getValue();
Assert.assertEquals("Unexpected Start Offset for segment " + segmentName, expectedStartOffset, sp.getStartOffset());
Assert.assertEquals("Unexpected length for segment " + segmentName, expectedLength, sp.getLength());
Assert.assertEquals("Unexpected value for isSealed for segment " + segmentName, expectSealed, sp.isSealed());
Assert.assertFalse("Unexpected value for isDeleted for segment " + segmentName, sp.isDeleted());
// Check attributes.
val allAttributes = store.getAttributes(segmentName, ATTRIBUTES, true, TIMEOUT).join();
for (AttributeId attributeId : ATTRIBUTES) {
Assert.assertEquals("Unexpected attribute value from getAttributes().", expectedAttributeValue, (long) allAttributes.getOrDefault(attributeId, Attributes.NULL_ATTRIBUTE_VALUE));
if (Attributes.isCoreAttribute(attributeId)) {
// Core attributes must always be available from getInfo
Assert.assertEquals("Unexpected core attribute value from getInfo().", expectedAttributeValue, (long) sp.getAttributes().getOrDefault(attributeId, Attributes.NULL_ATTRIBUTE_VALUE));
} else {
val extAttrValue = sp.getAttributes().getOrDefault(attributeId, Attributes.NULL_ATTRIBUTE_VALUE);
Assert.assertTrue("Unexpected extended attribute value from getInfo()", extAttrValue == Attributes.NULL_ATTRIBUTE_VALUE || extAttrValue == expectedAttributeValue);
}
}
}
}
}
Aggregations