Search in sources :

Example 31 with AttributeId

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());
}
Also used : Arrays(java.util.Arrays) StreamSegmentInformation(io.pravega.segmentstore.contracts.StreamSegmentInformation) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException) SneakyThrows(lombok.SneakyThrows) AssertExtensions(io.pravega.test.common.AssertExtensions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) ContainerMetadata(io.pravega.segmentstore.server.ContainerMetadata) TimeoutException(java.util.concurrent.TimeoutException) Cleanup(lombok.Cleanup) UpdateableSegmentMetadata(io.pravega.segmentstore.server.UpdateableSegmentMetadata) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) Attributes(io.pravega.segmentstore.contracts.Attributes) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) GuardedBy(javax.annotation.concurrent.GuardedBy) Collectors(java.util.stream.Collectors) SegmentMetadataComparer(io.pravega.segmentstore.server.SegmentMetadataComparer) ErrorInjector(io.pravega.test.common.ErrorInjector) List(java.util.List) Stream(java.util.stream.Stream) StreamSegmentExistsException(io.pravega.segmentstore.contracts.StreamSegmentExistsException) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) TestUtils(io.pravega.test.common.TestUtils) Futures(io.pravega.common.concurrent.Futures) ObjectClosedException(io.pravega.common.ObjectClosedException) MetadataBuilder(io.pravega.segmentstore.server.MetadataBuilder) Setter(lombok.Setter) Getter(lombok.Getter) TooManyActiveSegmentsException(io.pravega.segmentstore.contracts.TooManyActiveSegmentsException) Exceptions(io.pravega.common.Exceptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UpdateableContainerMetadata(io.pravega.segmentstore.server.UpdateableContainerMetadata) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) SegmentType(io.pravega.segmentstore.contracts.SegmentType) MathHelpers(io.pravega.common.MathHelpers) NameUtils(io.pravega.shared.NameUtils) AttributeId(io.pravega.segmentstore.contracts.AttributeId) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) AttributeUpdateCollection(io.pravega.segmentstore.contracts.AttributeUpdateCollection) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) Assert(org.junit.Assert) Collections(java.util.Collections) lombok.val(lombok.val) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) StreamSegmentExistsException(io.pravega.segmentstore.contracts.StreamSegmentExistsException) AttributeId(io.pravega.segmentstore.contracts.AttributeId) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 32 with AttributeId

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();
}
Also used : lombok.val(lombok.val) Random(java.util.Random) HashMap(java.util.HashMap) AttributeId(io.pravega.segmentstore.contracts.AttributeId) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 33 with AttributeId

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()));
    }
}
Also used : lombok.val(lombok.val) Singular(lombok.Singular) AssertExtensions(io.pravega.test.common.AssertExtensions) AttributeId(io.pravega.segmentstore.contracts.AttributeId) lombok.val(lombok.val) HashMap(java.util.HashMap) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) List(java.util.List) Builder(lombok.Builder) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Map(java.util.Map) AttributeIterator(io.pravega.segmentstore.server.AttributeIterator) Comparator(java.util.Comparator) Assert(org.junit.Assert) Collections(java.util.Collections) AttributeId(io.pravega.segmentstore.contracts.AttributeId) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 34 with AttributeId

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());
}
Also used : StreamSegmentTruncateOperation(io.pravega.segmentstore.server.logs.operations.StreamSegmentTruncateOperation) ByteArraySegment(io.pravega.common.util.ByteArraySegment) StreamSegmentSealOperation(io.pravega.segmentstore.server.logs.operations.StreamSegmentSealOperation) UpdateAttributesOperation(io.pravega.segmentstore.server.logs.operations.UpdateAttributesOperation) AttributeId(io.pravega.segmentstore.contracts.AttributeId) DataCorruptionException(io.pravega.segmentstore.server.DataCorruptionException) CachedStreamSegmentAppendOperation(io.pravega.segmentstore.server.logs.operations.CachedStreamSegmentAppendOperation) StreamSegmentAppendOperation(io.pravega.segmentstore.server.logs.operations.StreamSegmentAppendOperation) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 35 with AttributeId

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);
                }
            }
        }
    }
}
Also used : lombok.val(lombok.val) AttributeId(io.pravega.segmentstore.contracts.AttributeId) AtomicLong(java.util.concurrent.atomic.AtomicLong) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) Map(java.util.Map) HashMap(java.util.HashMap) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)

Aggregations

AttributeId (io.pravega.segmentstore.contracts.AttributeId)54 lombok.val (lombok.val)43 HashMap (java.util.HashMap)38 Test (org.junit.Test)37 CompletableFuture (java.util.concurrent.CompletableFuture)31 AttributeUpdate (io.pravega.segmentstore.contracts.AttributeUpdate)30 Map (java.util.Map)30 SegmentProperties (io.pravega.segmentstore.contracts.SegmentProperties)28 StreamSegmentNotExistsException (io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)28 ArrayList (java.util.ArrayList)28 Collections (java.util.Collections)28 Attributes (io.pravega.segmentstore.contracts.Attributes)27 Collectors (java.util.stream.Collectors)27 Cleanup (lombok.Cleanup)26 ByteArraySegment (io.pravega.common.util.ByteArraySegment)25 AttributeUpdateCollection (io.pravega.segmentstore.contracts.AttributeUpdateCollection)25 StreamSegmentSealedException (io.pravega.segmentstore.contracts.StreamSegmentSealedException)25 UpdateableContainerMetadata (io.pravega.segmentstore.server.UpdateableContainerMetadata)25 Collection (java.util.Collection)25 AtomicLong (java.util.concurrent.atomic.AtomicLong)25