Search in sources :

Example 6 with TableSegmentEntry

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

the class SegmentHelper method readTable.

public CompletableFuture<List<TableSegmentEntry>> readTable(final String tableName, final PravegaNodeUri uri, final List<TableSegmentKey> keys, String delegationToken, final long clientRequestId) {
    final WireCommandType type = WireCommandType.READ_TABLE;
    // the version is always NO_VERSION as read returns the latest version of value.
    List<WireCommands.TableKey> keyList = keys.stream().map(k -> new WireCommands.TableKey(k.getKey(), k.getVersion().getSegmentVersion())).collect(Collectors.toList());
    RawClient connection = new RawClient(uri, connectionPool);
    final long requestId = connection.getFlow().asLong();
    WireCommands.ReadTable request = new WireCommands.ReadTable(requestId, tableName, delegationToken, keyList);
    return sendRequest(connection, clientRequestId, request).thenApply(rpl -> {
        handleReply(clientRequestId, rpl, connection, tableName, WireCommands.ReadTable.class, type);
        return ((WireCommands.TableRead) rpl).getEntries().getEntries().stream().map(this::convertFromWireCommand).collect(Collectors.toList());
    });
}
Also used : SneakyThrows(lombok.SneakyThrows) TokenExpiredException(io.pravega.auth.TokenExpiredException) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) Unpooled(io.netty.buffer.Unpooled) TagLogger(io.pravega.common.tracing.TagLogger) Pair(org.apache.commons.lang3.tuple.Pair) Duration(java.time.Duration) Map(java.util.Map) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) HashTableIteratorItem(io.pravega.client.tables.impl.HashTableIteratorItem) Controller(io.pravega.controller.stream.api.grpc.v1.Controller) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) Request(io.pravega.shared.protocol.netty.Request) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) Config(io.pravega.controller.util.Config) Futures(io.pravega.common.concurrent.Futures) TableSegmentKeyVersion(io.pravega.client.tables.impl.TableSegmentKeyVersion) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Reply(io.pravega.shared.protocol.netty.Reply) ModelHelper(io.pravega.client.control.impl.ModelHelper) Exceptions(io.pravega.common.Exceptions) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) NameUtils.getSegmentNumber(io.pravega.shared.NameUtils.getSegmentNumber) RawClient(io.pravega.client.connection.impl.RawClient) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) WireCommandType(io.pravega.shared.protocol.netty.WireCommandType) NameUtils.getQualifiedStreamSegmentName(io.pravega.shared.NameUtils.getQualifiedStreamSegmentName) RecordHelper(io.pravega.controller.store.stream.records.RecordHelper) Host(io.pravega.common.cluster.Host) TableSegmentKey(io.pravega.client.tables.impl.TableSegmentKey) ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) AuthenticationException(io.pravega.auth.AuthenticationException) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) WireCommand(io.pravega.shared.protocol.netty.WireCommand) AbstractMap(java.util.AbstractMap) TableSegmentEntry(io.pravega.client.tables.impl.TableSegmentEntry) HostControllerStore(io.pravega.controller.store.host.HostControllerStore) TxnStatus(io.pravega.controller.stream.api.grpc.v1.Controller.TxnStatus) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ConnectionClosedException(io.pravega.client.stream.impl.ConnectionClosedException) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) NameUtils.getTransactionNameFromId(io.pravega.shared.NameUtils.getTransactionNameFromId) WireCommandType(io.pravega.shared.protocol.netty.WireCommandType) RawClient(io.pravega.client.connection.impl.RawClient) WireCommands(io.pravega.shared.protocol.netty.WireCommands)

Example 7 with TableSegmentEntry

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

the class PravegaTablesScopeTest method testRemoveTagsUnderScope.

@Test(timeout = 5000)
@SuppressWarnings("unchecked")
public void testRemoveTagsUnderScope() {
    // Setup Mocks.
    GrpcAuthHelper authHelper = mock(GrpcAuthHelper.class);
    when(authHelper.retrieveMasterToken()).thenReturn("");
    SegmentHelper segmentHelper = mock(SegmentHelper.class);
    PravegaTablesStoreHelper storeHelper = new PravegaTablesStoreHelper(segmentHelper, authHelper, executorService());
    PravegaTablesScope tablesScope = spy(new PravegaTablesScope(scope, storeHelper));
    doReturn(CompletableFuture.completedFuture(indexTable)).when(tablesScope).getAllStreamTagsInScopeTableNames(stream, context);
    // Simulate an empty value being returned.
    TableSegmentEntry entry = TableSegmentEntry.versioned(tagBytes, new byte[0], 1L);
    when(segmentHelper.readTable(eq(indexTable), any(), anyString(), anyLong())).thenReturn(CompletableFuture.completedFuture(singletonList(entry)));
    when(segmentHelper.updateTableEntries(eq(indexTable), any(), anyString(), anyLong())).thenReturn(CompletableFuture.completedFuture(singletonList(TableSegmentKeyVersion.from(2L))));
    when(segmentHelper.removeTableKeys(eq(indexTable), any(), anyString(), anyLong())).thenAnswer(invocation -> {
        // Capture the key value sent during removeTableKeys.
        keySnapshot = (List<TableSegmentKey>) invocation.getArguments()[1];
        return CompletableFuture.completedFuture(null);
    });
    // Invoke the removeTags method.
    tablesScope.removeTagsUnderScope(stream, Set.of(tag), context).join();
    // Verify if correctly detect that the data is empty and the entry is cleaned up.
    verify(segmentHelper, times(1)).removeTableKeys(eq(indexTable), eq(keySnapshot), anyString(), anyLong());
    // Verify if the version number is as expected.
    assertEquals(2L, keySnapshot.get(0).getVersion().getSegmentVersion());
}
Also used : TableSegmentKey(io.pravega.client.tables.impl.TableSegmentKey) TableSegmentEntry(io.pravega.client.tables.impl.TableSegmentEntry) GrpcAuthHelper(io.pravega.controller.server.security.auth.GrpcAuthHelper) SegmentHelper(io.pravega.controller.server.SegmentHelper) Test(org.junit.Test)

Example 8 with TableSegmentEntry

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

the class ControllerMetadataCommand method getTableEntry.

/**
 * Method to get the entry corresponding to the provided key in the table.
 *
 * @param tableName          The name of the table.
 * @param key                The key.
 * @param segmentStoreHost   The address of the segment store instance.
 * @param adminSegmentHelper An instance of {@link AdminSegmentHelper}.
 * @return The queried table segment entry.
 */
TableSegmentEntry getTableEntry(String tableName, String key, String segmentStoreHost, AdminSegmentHelper adminSegmentHelper) {
    ByteArraySegment serializedKey = new ByteArraySegment(KEY_SERIALIZER.serialize(key));
    List<TableSegmentEntry> entryList = completeSafely(adminSegmentHelper.readTable(tableName, new PravegaNodeUri(segmentStoreHost, getServiceConfig().getAdminGatewayPort()), Collections.singletonList(TableSegmentKey.unversioned(serializedKey.getCopy())), authHelper.retrieveMasterToken(), 0L), tableName, key);
    if (entryList == null) {
        return null;
    }
    if (entryList.get(0).getKey().getVersion().equals(TableSegmentKeyVersion.NOT_EXISTS)) {
        output(String.format("Key not found: %s", key));
        return null;
    }
    return entryList.get(0);
}
Also used : TableSegmentEntry(io.pravega.client.tables.impl.TableSegmentEntry) ByteArraySegment(io.pravega.common.util.ByteArraySegment) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri)

Example 9 with TableSegmentEntry

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

the class ControllerMetadataCommand method updateTableEntry.

/**
 * Method to update entry corresponding to the provided key in the table.
 *
 * @param tableName          The name of the table.
 * @param key                The key.
 * @param value              The new value.
 * @param version            The expected update version.
 * @param segmentStoreHost   The address of the segment store instance.
 * @param adminSegmentHelper An instance of {@link AdminSegmentHelper}.
 * @return The new key version after the update takes place successfully.
 */
TableSegmentKeyVersion updateTableEntry(String tableName, String key, ByteBuffer value, long version, String segmentStoreHost, AdminSegmentHelper adminSegmentHelper) {
    ByteArraySegment serializedKey = new ByteArraySegment(KEY_SERIALIZER.serialize(key));
    ByteArraySegment serializedValue = new ByteArraySegment(value);
    TableSegmentEntry updatedEntry = TableSegmentEntry.versioned(serializedKey.getCopy(), serializedValue.getCopy(), version);
    List<TableSegmentKeyVersion> keyVersions = completeSafely(adminSegmentHelper.updateTableEntries(tableName, new PravegaNodeUri(segmentStoreHost, getServiceConfig().getAdminGatewayPort()), Collections.singletonList(updatedEntry), authHelper.retrieveMasterToken(), 0L), tableName, key);
    if (keyVersions == null) {
        return null;
    }
    return keyVersions.get(0);
}
Also used : TableSegmentEntry(io.pravega.client.tables.impl.TableSegmentEntry) ByteArraySegment(io.pravega.common.util.ByteArraySegment) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) TableSegmentKeyVersion(io.pravega.client.tables.impl.TableSegmentKeyVersion)

Example 10 with TableSegmentEntry

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

the class TableSegmentCommand method updateTableEntry.

/**
 * Method to update the entry corresponding to the provided key in the table segment.
 *
 * @param tableSegmentName   The name of the table segment.
 * @param key                The key.
 * @param value              The entry to be updated in the table segment.
 * @param segmentStoreHost   The address of the segment store instance.
 * @param adminSegmentHelper An instance of {@link AdminSegmentHelper}
 * @return A long indicating the version obtained from updating the provided key in the table segment.
 */
long updateTableEntry(String tableSegmentName, String key, String value, String segmentStoreHost, AdminSegmentHelper adminSegmentHelper) {
    ByteArraySegment serializedKey = new ByteArraySegment(getCommandArgs().getState().getKeySerializer().serialize(key));
    ByteArraySegment serializedValue = new ByteArraySegment(getCommandArgs().getState().getValueSerializer().serialize(value));
    TableSegmentEntry updatedEntry = TableSegmentEntry.unversioned(serializedKey.getCopy(), serializedValue.getCopy());
    CompletableFuture<List<TableSegmentKeyVersion>> reply = adminSegmentHelper.updateTableEntries(tableSegmentName, new PravegaNodeUri(segmentStoreHost, getServiceConfig().getAdminGatewayPort()), Collections.singletonList(updatedEntry), super.authHelper.retrieveMasterToken(), 0L);
    return reply.join().get(0).getSegmentVersion();
}
Also used : TableSegmentEntry(io.pravega.client.tables.impl.TableSegmentEntry) ByteArraySegment(io.pravega.common.util.ByteArraySegment) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) List(java.util.List)

Aggregations

TableSegmentEntry (io.pravega.client.tables.impl.TableSegmentEntry)14 TableSegmentKeyVersion (io.pravega.client.tables.impl.TableSegmentKeyVersion)8 List (java.util.List)8 HashTableIteratorItem (io.pravega.client.tables.impl.HashTableIteratorItem)7 TableSegmentKey (io.pravega.client.tables.impl.TableSegmentKey)7 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)7 Map (java.util.Map)7 Collectors (java.util.stream.Collectors)7 Unpooled (io.netty.buffer.Unpooled)6 Futures (io.pravega.common.concurrent.Futures)6 ByteArraySegment (io.pravega.common.util.ByteArraySegment)6 UUID (java.util.UUID)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)5 Exceptions (io.pravega.common.Exceptions)5 TagLogger (io.pravega.common.tracing.TagLogger)5 Preconditions (com.google.common.base.Preconditions)4 ConnectionPool (io.pravega.client.connection.impl.ConnectionPool)4 SegmentHelper (io.pravega.controller.server.SegmentHelper)4