Search in sources :

Example 6 with AttributeUpdate

use of io.pravega.segmentstore.contracts.AttributeUpdate in project pravega by pravega.

the class AppendProcessor method storeAppend.

private CompletableFuture<Void> storeAppend(Append append) {
    long lastEventNumber;
    synchronized (lock) {
        lastEventNumber = latestEventNumbers.get(Pair.of(append.getSegment(), append.getWriterId()));
    }
    List<AttributeUpdate> attributes = Arrays.asList(new AttributeUpdate(append.getWriterId(), AttributeUpdateType.ReplaceIfEquals, append.getEventNumber(), lastEventNumber), new AttributeUpdate(EVENT_COUNT, AttributeUpdateType.Accumulate, append.getEventCount()));
    ByteBuf buf = append.getData().asReadOnly();
    byte[] bytes = new byte[buf.readableBytes()];
    buf.readBytes(bytes);
    if (append.isConditional()) {
        return store.append(append.getSegment(), append.getExpectedLength(), bytes, attributes, TIMEOUT);
    } else {
        return store.append(append.getSegment(), bytes, attributes, TIMEOUT);
    }
}
Also used : AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) ByteBuf(io.netty.buffer.ByteBuf)

Example 7 with AttributeUpdate

use of io.pravega.segmentstore.contracts.AttributeUpdate in project pravega by pravega.

the class PravegaRequestProcessor method updateSegmentAttribute.

@Override
public void updateSegmentAttribute(UpdateSegmentAttribute updateSegmentAttribute) {
    long requestId = updateSegmentAttribute.getRequestId();
    String segmentName = updateSegmentAttribute.getSegmentName();
    UUID attributeId = updateSegmentAttribute.getAttributeId();
    long newValue = updateSegmentAttribute.getNewValue();
    long expectedValue = updateSegmentAttribute.getExpectedValue();
    if (!verifyToken(segmentName, updateSegmentAttribute.getRequestId(), updateSegmentAttribute.getDelegationToken(), READ, "Update Segment Attribute")) {
        return;
    }
    long trace = LoggerHelpers.traceEnter(log, "updateSegmentAttribute", updateSegmentAttribute);
    val update = new AttributeUpdate(attributeId, AttributeUpdateType.ReplaceIfEquals, newValue, expectedValue);
    segmentStore.updateAttributes(segmentName, Collections.singletonList(update), TIMEOUT).whenComplete((v, e) -> {
        LoggerHelpers.traceLeave(log, "updateSegmentAttribute", trace, e);
        if (e == null) {
            connection.send(new SegmentAttributeUpdated(requestId, true));
        } else {
            if (Exceptions.unwrap(e) instanceof BadAttributeUpdateException) {
                log.debug("Updating segment attribute {} failed due to: {}", update, e.getMessage());
                connection.send(new SegmentAttributeUpdated(requestId, false));
            } else {
                handleException(requestId, segmentName, "Update attribute", e);
            }
        }
    }).exceptionally(e -> handleException(requestId, segmentName, "Update attribute", e));
}
Also used : lombok.val(lombok.val) SCALE_POLICY_RATE(io.pravega.segmentstore.contracts.Attributes.SCALE_POLICY_RATE) Arrays(java.util.Arrays) READ(io.pravega.auth.AuthHandler.Permissions.READ) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException) SEGMENT_CREATE_LATENCY(io.pravega.shared.MetricsNames.SEGMENT_CREATE_LATENCY) AuthHandler(io.pravega.auth.AuthHandler) SegmentIsTruncated(io.pravega.shared.protocol.netty.WireCommands.SegmentIsTruncated) CREATION_TIME(io.pravega.segmentstore.contracts.Attributes.CREATION_TIME) MetricsNames.nameFromSegment(io.pravega.shared.MetricsNames.nameFromSegment) GetStreamSegmentInfo(io.pravega.shared.protocol.netty.WireCommands.GetStreamSegmentInfo) ReadResultEntryContents(io.pravega.segmentstore.contracts.ReadResultEntryContents) Duration(java.time.Duration) Map(java.util.Map) SegmentCreated(io.pravega.shared.protocol.netty.WireCommands.SegmentCreated) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) Attributes(io.pravega.segmentstore.contracts.Attributes) CancellationException(java.util.concurrent.CancellationException) ContainerNotFoundException(io.pravega.segmentstore.contracts.ContainerNotFoundException) Slf4j(lombok.extern.slf4j.Slf4j) CreateSegment(io.pravega.shared.protocol.netty.WireCommands.CreateSegment) SealSegment(io.pravega.shared.protocol.netty.WireCommands.SealSegment) SegmentSealed(io.pravega.shared.protocol.netty.WireCommands.SegmentSealed) EndOfStreamSegment(io.pravega.segmentstore.contracts.ReadResultEntryType.EndOfStreamSegment) SegmentAttribute(io.pravega.shared.protocol.netty.WireCommands.SegmentAttribute) SEGMENT_READ_LATENCY(io.pravega.shared.MetricsNames.SEGMENT_READ_LATENCY) Exceptions(io.pravega.common.Exceptions) BadAttributeUpdateException(io.pravega.segmentstore.contracts.BadAttributeUpdateException) GetSegmentAttribute(io.pravega.shared.protocol.netty.WireCommands.GetSegmentAttribute) ArrayList(java.util.ArrayList) READ_UPDATE(io.pravega.auth.AuthHandler.Permissions.READ_UPDATE) SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead) AccessLevel(lombok.AccessLevel) Future(io.pravega.segmentstore.contracts.ReadResultEntryType.Future) FailingRequestProcessor(io.pravega.shared.protocol.netty.FailingRequestProcessor) StreamHelpers(io.pravega.common.io.StreamHelpers) StreamSegmentTruncatedException(io.pravega.segmentstore.contracts.StreamSegmentTruncatedException) StatsLogger(io.pravega.shared.metrics.StatsLogger) OpStatsLogger(io.pravega.shared.metrics.OpStatsLogger) lombok.val(lombok.val) SEGMENT_WRITE_EVENTS(io.pravega.shared.MetricsNames.SEGMENT_WRITE_EVENTS) IOException(java.io.IOException) WireCommands(io.pravega.shared.protocol.netty.WireCommands) WrongHost(io.pravega.shared.protocol.netty.WireCommands.WrongHost) SegmentDeleted(io.pravega.shared.protocol.netty.WireCommands.SegmentDeleted) CreateTransaction(io.pravega.shared.protocol.netty.WireCommands.CreateTransaction) Truncated(io.pravega.segmentstore.contracts.ReadResultEntryType.Truncated) SegmentTruncated(io.pravega.shared.protocol.netty.WireCommands.SegmentTruncated) RequestProcessor(io.pravega.shared.protocol.netty.RequestProcessor) OperationUnsupported(io.pravega.shared.protocol.netty.WireCommands.OperationUnsupported) Preconditions(com.google.common.base.Preconditions) TransactionInfo(io.pravega.shared.protocol.netty.WireCommands.TransactionInfo) AbortTransaction(io.pravega.shared.protocol.netty.WireCommands.AbortTransaction) AuthenticationException(io.pravega.common.auth.AuthenticationException) Cache(io.pravega.segmentstore.contracts.ReadResultEntryType.Cache) SneakyThrows(lombok.SneakyThrows) ByteBuffer(java.nio.ByteBuffer) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) StreamSegmentSealedException(io.pravega.segmentstore.contracts.StreamSegmentSealedException) UpdateSegmentAttribute(io.pravega.shared.protocol.netty.WireCommands.UpdateSegmentAttribute) UpdateSegmentPolicy(io.pravega.shared.protocol.netty.WireCommands.UpdateSegmentPolicy) SegmentStatsRecorder(io.pravega.segmentstore.server.host.stat.SegmentStatsRecorder) CommitTransaction(io.pravega.shared.protocol.netty.WireCommands.CommitTransaction) Collection(java.util.Collection) UUID(java.util.UUID) Math.min(java.lang.Math.min) StreamSegmentNameUtils(io.pravega.shared.segment.StreamSegmentNameUtils) MetricsProvider(io.pravega.shared.metrics.MetricsProvider) List(java.util.List) StreamSegmentExistsException(io.pravega.segmentstore.contracts.StreamSegmentExistsException) PassingTokenVerifier(io.pravega.segmentstore.server.host.delegationtoken.PassingTokenVerifier) BadOffsetException(io.pravega.segmentstore.contracts.BadOffsetException) DelegationTokenVerifier(io.pravega.segmentstore.server.host.delegationtoken.DelegationTokenVerifier) Math.max(java.lang.Math.max) SegmentIsSealed(io.pravega.shared.protocol.netty.WireCommands.SegmentIsSealed) ReadResult(io.pravega.segmentstore.contracts.ReadResult) TransactionAborted(io.pravega.shared.protocol.netty.WireCommands.TransactionAborted) DeleteSegment(io.pravega.shared.protocol.netty.WireCommands.DeleteSegment) SegmentPolicyUpdated(io.pravega.shared.protocol.netty.WireCommands.SegmentPolicyUpdated) Getter(lombok.Getter) NoSuchSegment(io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment) CompletableFuture(java.util.concurrent.CompletableFuture) GetTransactionInfo(io.pravega.shared.protocol.netty.WireCommands.GetTransactionInfo) DynamicLogger(io.pravega.shared.metrics.DynamicLogger) SEGMENT_WRITE_BYTES(io.pravega.shared.MetricsNames.SEGMENT_WRITE_BYTES) ReadResultEntry(io.pravega.segmentstore.contracts.ReadResultEntry) SEGMENT_READ_BYTES(io.pravega.shared.MetricsNames.SEGMENT_READ_BYTES) SCALE_POLICY_TYPE(io.pravega.segmentstore.contracts.Attributes.SCALE_POLICY_TYPE) TransactionCommitted(io.pravega.shared.protocol.netty.WireCommands.TransactionCommitted) TYPE_PLUS_LENGTH_SIZE(io.pravega.shared.protocol.netty.WireCommands.TYPE_PLUS_LENGTH_SIZE) StreamSegmentInfo(io.pravega.shared.protocol.netty.WireCommands.StreamSegmentInfo) TransactionCreated(io.pravega.shared.protocol.netty.WireCommands.TransactionCreated) TruncateSegment(io.pravega.shared.protocol.netty.WireCommands.TruncateSegment) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) SegmentAlreadyExists(io.pravega.shared.protocol.netty.WireCommands.SegmentAlreadyExists) LoggerHelpers(io.pravega.common.LoggerHelpers) StreamSegmentMergedException(io.pravega.segmentstore.contracts.StreamSegmentMergedException) Timer(io.pravega.common.Timer) SegmentAttributeUpdated(io.pravega.shared.protocol.netty.WireCommands.SegmentAttributeUpdated) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) Collections(java.util.Collections) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) BadAttributeUpdateException(io.pravega.segmentstore.contracts.BadAttributeUpdateException) SegmentAttributeUpdated(io.pravega.shared.protocol.netty.WireCommands.SegmentAttributeUpdated) UUID(java.util.UUID)

Example 8 with AttributeUpdate

use of io.pravega.segmentstore.contracts.AttributeUpdate in project pravega by pravega.

the class SegmentMetadataUpdateTransaction method preProcessAttributes.

/**
 * Pre-processes a collection of attributes.
 * After this method returns, all AttributeUpdates in the given collection will have the actual (and updated) value
 * of that attribute in the Segment.
 *
 * @param attributeUpdates The Updates to process (if any).
 * @throws BadAttributeUpdateException If any of the given AttributeUpdates is invalid given the current state of
 *                                     the segment.
 * @throws TooManyAttributesException  If, as a result of applying the given updates, the Segment would exceed the
 *                                     maximum allowed number of Attributes.
 */
private void preProcessAttributes(Collection<AttributeUpdate> attributeUpdates) throws BadAttributeUpdateException, TooManyAttributesException {
    if (attributeUpdates == null) {
        return;
    }
    int newAttributeCount = this.attributeValues.size();
    for (AttributeUpdate u : attributeUpdates) {
        AttributeUpdateType updateType = u.getUpdateType();
        long previousValue = this.attributeValues.getOrDefault(u.getAttributeId(), SegmentMetadata.NULL_ATTRIBUTE_VALUE);
        // Perform validation, and set the AttributeUpdate.value to the updated value, if necessary.
        switch(updateType) {
            case ReplaceIfGreater:
                // Verify value against existing value, if any.
                boolean hasValue = previousValue != SegmentMetadata.NULL_ATTRIBUTE_VALUE;
                if (hasValue && u.getValue() <= previousValue) {
                    throw new BadAttributeUpdateException(this.name, u, String.format("Expected greater than '%s'.", previousValue));
                }
                break;
            case ReplaceIfEquals:
                // Verify value against existing value, if any.
                if (u.getComparisonValue() != previousValue) {
                    throw new BadAttributeUpdateException(this.name, u, String.format("Expected existing value to be '%s', actual '%s'.", u.getComparisonValue(), previousValue));
                }
                break;
            case None:
                // Verify value is not already set.
                if (previousValue != SegmentMetadata.NULL_ATTRIBUTE_VALUE) {
                    throw new BadAttributeUpdateException(this.name, u, String.format("Attribute value already set (%s).", previousValue));
                }
                break;
            case Accumulate:
                if (previousValue != SegmentMetadata.NULL_ATTRIBUTE_VALUE) {
                    u.setValue(previousValue + u.getValue());
                }
                break;
            case Replace:
                break;
            default:
                throw new BadAttributeUpdateException(this.name, u, "Unexpected update type: " + updateType);
        }
        if (previousValue == SegmentMetadata.NULL_ATTRIBUTE_VALUE && u.getValue() != SegmentMetadata.NULL_ATTRIBUTE_VALUE) {
            // This attribute did not exist and is about to be added.
            newAttributeCount++;
        } else if (previousValue != SegmentMetadata.NULL_ATTRIBUTE_VALUE && u.getValue() == SegmentMetadata.NULL_ATTRIBUTE_VALUE) {
            // This attribute existed and is about to be removed.
            newAttributeCount--;
        }
    }
    if (newAttributeCount > SegmentMetadata.MAXIMUM_ATTRIBUTE_COUNT && newAttributeCount > this.attributeValues.size()) {
        // attributes of existing segments, but not increase their count.
        throw new TooManyAttributesException(this.name, SegmentMetadata.MAXIMUM_ATTRIBUTE_COUNT);
    }
}
Also used : AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) TooManyAttributesException(io.pravega.segmentstore.contracts.TooManyAttributesException) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) BadAttributeUpdateException(io.pravega.segmentstore.contracts.BadAttributeUpdateException)

Example 9 with AttributeUpdate

use of io.pravega.segmentstore.contracts.AttributeUpdate in project pravega by pravega.

the class SegmentStateMapperTests method testGetSegmentInfoFromStorage.

/**
 * Tests the ability to retrieve Segment Info from Storage (base info and its State).
 */
@Test
public void testGetSegmentInfoFromStorage() {
    final UUID attributeId = UUID.randomUUID();
    @Cleanup val s = createStorage();
    val stateStore = new InMemoryStateStore();
    val m = new SegmentStateMapper(stateStore, s);
    // Save some state.
    val info = s.create("s", TIMEOUT).thenCompose(si -> s.openWrite(si.getName())).thenCompose(handle -> s.write(handle, 0, new ByteArrayInputStream(new byte[10]), 10, TIMEOUT).thenCompose(v -> s.seal(handle, TIMEOUT)).thenCompose(v -> s.getStreamSegmentInfo(handle.getSegmentName(), TIMEOUT))).join();
    val expectedInfo = StreamSegmentInformation.from(info).startOffset(info.getLength() / 2).build();
    val attributes = Collections.singleton(new AttributeUpdate(attributeId, AttributeUpdateType.Replace, 100L));
    val expectedAttributes = Collections.singletonMap(attributeId, 100L);
    m.saveState(expectedInfo, attributes, TIMEOUT).join();
    // Retrieve the state and verify it.
    val actual = m.getSegmentInfoFromStorage(expectedInfo.getName(), TIMEOUT).join();
    Assert.assertEquals("Unexpected Name.", expectedInfo.getName(), actual.getName());
    Assert.assertEquals("Unexpected Length.", expectedInfo.getLength(), actual.getLength());
    Assert.assertEquals("Unexpected Sealed status.", expectedInfo.isSealed(), actual.isSealed());
    Assert.assertEquals("Unexpected Start Offset.", expectedInfo.getStartOffset(), actual.getStartOffset());
    AssertExtensions.assertMapEquals("Unexpected Attributes.", expectedAttributes, actual.getAttributes());
}
Also used : lombok.val(lombok.val) Storage(io.pravega.segmentstore.storage.Storage) StreamSegmentInformation(io.pravega.segmentstore.contracts.StreamSegmentInformation) AssertExtensions(io.pravega.test.common.AssertExtensions) lombok.val(lombok.val) Cleanup(lombok.Cleanup) Test(org.junit.Test) UUID(java.util.UUID) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) Rule(org.junit.Rule) ByteArrayInputStream(java.io.ByteArrayInputStream) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) InMemoryStorageFactory(io.pravega.segmentstore.storage.mocks.InMemoryStorageFactory) Duration(java.time.Duration) Timeout(org.junit.rules.Timeout) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) Assert(org.junit.Assert) Collections(java.util.Collections) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) ByteArrayInputStream(java.io.ByteArrayInputStream) UUID(java.util.UUID) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 10 with AttributeUpdate

use of io.pravega.segmentstore.contracts.AttributeUpdate in project pravega by pravega.

the class SegmentStateMapperTests method testStateOperations.

/**
 * Tests the ability to Save and later retrieve a State from the State Store.
 */
@Test
public void testStateOperations() {
    final UUID attributeId = UUID.randomUUID();
    @Cleanup val s = createStorage();
    val stateStore = new InMemoryStateStore();
    val m = new SegmentStateMapper(stateStore, s);
    // Save some state.
    val sp = StreamSegmentInformation.builder().name("s").length(10).startOffset(4).sealed(true).build();
    val attributes = Collections.singleton(new AttributeUpdate(attributeId, AttributeUpdateType.Replace, 100L));
    val expectedAttributes = Collections.singletonMap(attributeId, 100L);
    m.saveState(sp, attributes, TIMEOUT).join();
    // Get raw state.
    val state = m.getState(sp.getName(), TIMEOUT).join();
    Assert.assertEquals("Unexpected SegmentName.", sp.getName(), state.getSegmentName());
    Assert.assertEquals("Unexpected StartOffset.", sp.getStartOffset(), state.getStartOffset());
    AssertExtensions.assertMapEquals("Unexpected Attributes.", expectedAttributes, state.getAttributes());
    // Combine a state with raw properties.
    val rawSp = StreamSegmentInformation.builder().name(sp.getName()).length(10).startOffset(0).sealed(true).build();
    val si = m.attachState(rawSp, TIMEOUT).join();
    Assert.assertEquals("Unexpected StartOffset.", sp.getStartOffset(), si.getProperties().getStartOffset());
    AssertExtensions.assertMapEquals("Unexpected Attributes.", expectedAttributes, si.getProperties().getAttributes());
}
Also used : lombok.val(lombok.val) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) UUID(java.util.UUID) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Aggregations

AttributeUpdate (io.pravega.segmentstore.contracts.AttributeUpdate)22 lombok.val (lombok.val)16 ArrayList (java.util.ArrayList)13 UUID (java.util.UUID)12 AttributeUpdateType (io.pravega.segmentstore.contracts.AttributeUpdateType)8 HashMap (java.util.HashMap)8 Collection (java.util.Collection)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 Test (org.junit.Test)7 Duration (java.time.Duration)6 Cleanup (lombok.Cleanup)6 Exceptions (io.pravega.common.Exceptions)5 Attributes (io.pravega.segmentstore.contracts.Attributes)5 BadAttributeUpdateException (io.pravega.segmentstore.contracts.BadAttributeUpdateException)5 StreamSegmentExistsException (io.pravega.segmentstore.contracts.StreamSegmentExistsException)5 StreamSegmentNotExistsException (io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)5 StreamSegmentNameUtils (io.pravega.shared.segment.StreamSegmentNameUtils)5 Collections (java.util.Collections)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 Preconditions (com.google.common.base.Preconditions)4