Search in sources :

Example 6 with TableSegmentKeyVersion

use of io.pravega.client.tables.impl.TableSegmentKeyVersion in project pravega by pravega.

the class PravegaTablesStoreHelper method updateEntry.

/**
 * Method to update a single entry.
 * @param tableName tablename
 * @param key key
 * @param toBytes to bytes function
 * @param val value
 * @param ver previous key version
 * @param <T> Type of value to be added
 * @param requestId request id
 * @return CompletableFuture which when completed will indicate that the value is updated in the table.
 */
public <T> CompletableFuture<Version> updateEntry(String tableName, String key, T val, Function<T, byte[]> toBytes, Version ver, long requestId) {
    long version = ver.asLongVersion().getLongValue();
    log.trace(requestId, "updateEntry entry called for : {} key : {} version {}", tableName, key, version);
    byte[] value = toBytes.apply(val);
    long time = System.currentTimeMillis();
    List<TableSegmentEntry> entries = Collections.singletonList(TableSegmentEntry.versioned(key.getBytes(Charsets.UTF_8), value, version));
    return withRetries(() -> segmentHelper.updateTableEntries(tableName, entries, authToken.get(), requestId), () -> String.format("updateEntry: key: %s table: %s", key, tableName), true, requestId).thenApplyAsync(x -> {
        TableSegmentKeyVersion first = x.get(0);
        log.debug(requestId, "entry for key {} updated to table {} with new version {}", key, tableName, first.getSegmentVersion());
        Version newVersion = new Version.LongVersion(first.getSegmentVersion());
        putInCache(tableName, key, new VersionedMetadata<>(val, newVersion), time);
        return newVersion;
    }, executor).exceptionally(e -> {
        invalidateCache(tableName, key);
        throw new CompletionException(e);
    }).whenComplete((r, ex) -> releaseEntries(entries));
}
Also used : TableSegmentKeyVersion(io.pravega.client.tables.impl.TableSegmentKeyVersion) Cache(io.pravega.controller.store.stream.Cache) OperationContext(io.pravega.controller.store.stream.OperationContext) SegmentHelper(io.pravega.controller.server.SegmentHelper) BiFunction(java.util.function.BiFunction) Exceptions(io.pravega.common.Exceptions) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) HostStoreException(io.pravega.controller.store.host.HostStoreException) BitConverter(io.pravega.common.util.BitConverter) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Unpooled(io.netty.buffer.Unpooled) ArrayList(java.util.ArrayList) TagLogger(io.pravega.common.tracing.TagLogger) ByteBuf(io.netty.buffer.ByteBuf) StoreException(io.pravega.controller.store.stream.StoreException) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) HashTableIteratorItem(io.pravega.client.tables.impl.HashTableIteratorItem) RetryHelper(io.pravega.controller.util.RetryHelper) Charsets(org.apache.curator.shaded.com.google.common.base.Charsets) TableSegmentKey(io.pravega.client.tables.impl.TableSegmentKey) Predicate(java.util.function.Predicate) Collection(java.util.Collection) AsyncIterator(io.pravega.common.util.AsyncIterator) CompletionException(java.util.concurrent.CompletionException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) RetriesExhaustedException(io.pravega.common.util.RetriesExhaustedException) StandardCharsets(java.nio.charset.StandardCharsets) ConnectionDropped(io.pravega.controller.server.WireCommandFailedException.Reason.ConnectionDropped) AbstractMap(java.util.AbstractMap) List(java.util.List) ByteArraySegment(io.pravega.common.util.ByteArraySegment) WireCommandFailedException(io.pravega.controller.server.WireCommandFailedException) TableSegmentEntry(io.pravega.client.tables.impl.TableSegmentEntry) ContinuationTokenAsyncIterator(io.pravega.common.util.ContinuationTokenAsyncIterator) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) Futures(io.pravega.common.concurrent.Futures) ConnectionFailed(io.pravega.controller.server.WireCommandFailedException.Reason.ConnectionFailed) GrpcAuthHelper(io.pravega.controller.server.security.auth.GrpcAuthHelper) TableSegmentEntry(io.pravega.client.tables.impl.TableSegmentEntry) TableSegmentKeyVersion(io.pravega.client.tables.impl.TableSegmentKeyVersion) CompletionException(java.util.concurrent.CompletionException) TableSegmentKeyVersion(io.pravega.client.tables.impl.TableSegmentKeyVersion)

Aggregations

TableSegmentEntry (io.pravega.client.tables.impl.TableSegmentEntry)6 TableSegmentKeyVersion (io.pravega.client.tables.impl.TableSegmentKeyVersion)6 Unpooled (io.netty.buffer.Unpooled)4 HashTableIteratorItem (io.pravega.client.tables.impl.HashTableIteratorItem)4 TableSegmentKey (io.pravega.client.tables.impl.TableSegmentKey)4 Futures (io.pravega.common.concurrent.Futures)4 List (java.util.List)4 Map (java.util.Map)4 UUID (java.util.UUID)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 ConnectionPool (io.pravega.client.connection.impl.ConnectionPool)3 Exceptions (io.pravega.common.Exceptions)3 TagLogger (io.pravega.common.tracing.TagLogger)3 HostControllerStore (io.pravega.controller.store.host.HostControllerStore)3 TxnStatus (io.pravega.controller.stream.api.grpc.v1.Controller.TxnStatus)3 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)3 WireCommandType (io.pravega.shared.protocol.netty.WireCommandType)3 WireCommands (io.pravega.shared.protocol.netty.WireCommands)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3