Search in sources :

Example 1 with CreateTableEvent

use of io.pravega.shared.controller.event.kvtable.CreateTableEvent in project pravega by pravega.

the class TableMetadataTasks method createKeyValueTable.

/**
 *  Create a Key-Value Table.
 *
 * @param scope      scope name.
 * @param kvtName    KVTable name.
 * @param kvtConfig  KVTable configuration.
 * @param createTimestamp  KVTable creation timestamp.
 * @param requestId  request id
 * @return update status.
 */
public CompletableFuture<CreateKeyValueTableStatus.Status> createKeyValueTable(String scope, String kvtName, KeyValueTableConfiguration kvtConfig, final long createTimestamp, long requestId) {
    OperationContext context = kvtMetadataStore.createContext(scope, kvtName, requestId);
    return RetryHelper.withRetriesAsync(() -> {
        // 1. check if scope with this name exists...
        return kvtMetadataStore.checkScopeExists(scope, context, executor).thenCompose(exists -> {
            if (!exists) {
                return CompletableFuture.completedFuture(CreateKeyValueTableStatus.Status.SCOPE_NOT_FOUND);
            }
            return kvtMetadataStore.isScopeSealed(scope, context, executor).thenCompose(isScopeSealed -> {
                if (isScopeSealed) {
                    return CompletableFuture.completedFuture(CreateKeyValueTableStatus.Status.SCOPE_NOT_FOUND);
                }
                // 2. check state of the KVTable, if found
                return Futures.exceptionallyExpecting(kvtMetadataStore.getState(scope, kvtName, true, context, executor), e -> Exceptions.unwrap(e) instanceof StoreException.DataNotFoundException, KVTableState.UNKNOWN).thenCompose(state -> {
                    if (state.equals(KVTableState.UNKNOWN) || state.equals(KVTableState.CREATING)) {
                        // 3. get a new UUID for the KVTable we will be creating.
                        UUID id = kvtMetadataStore.newScope(scope).newId();
                        CreateTableEvent event = new CreateTableEvent(scope, kvtName, kvtConfig.getPartitionCount(), kvtConfig.getPrimaryKeyLength(), kvtConfig.getSecondaryKeyLength(), createTimestamp, requestId, id, kvtConfig.getRolloverSizeBytes());
                        // 4. Update ScopeTable with the entry for this KVT and Publish the event for creation
                        return eventHelper.addIndexAndSubmitTask(event, () -> kvtMetadataStore.createEntryForKVTable(scope, kvtName, id, context, executor)).thenCompose(x -> isCreateProcessed(scope, kvtName, kvtConfig, createTimestamp, executor, context));
                    }
                    log.info(requestId, "KeyValue table {}/{} already exists", scope, kvtName);
                    return isCreateProcessed(scope, kvtName, kvtConfig, createTimestamp, executor, context);
                });
            });
        });
    }, e -> Exceptions.unwrap(e) instanceof RetryableException, NUM_RETRIES, executor);
}
Also used : OperationContext(io.pravega.controller.store.stream.OperationContext) OperationContext(io.pravega.controller.store.stream.OperationContext) ControllerEventProcessors(io.pravega.controller.server.eventProcessor.ControllerEventProcessors) SegmentHelper(io.pravega.controller.server.SegmentHelper) Exceptions(io.pravega.common.Exceptions) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) KeyValueTableConfiguration(io.pravega.client.tables.KeyValueTableConfiguration) CreateTableEvent(io.pravega.shared.controller.event.kvtable.CreateTableEvent) TagLogger(io.pravega.common.tracing.TagLogger) DeleteKVTableStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteKVTableStatus) StoreException(io.pravega.controller.store.stream.StoreException) TaskStepsRetryHelper.withRetries(io.pravega.controller.task.Stream.TaskStepsRetryHelper.withRetries) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) EventHelper(io.pravega.controller.task.EventHelper) EventStreamClientFactory(io.pravega.client.EventStreamClientFactory) DeleteTableEvent(io.pravega.shared.controller.event.kvtable.DeleteTableEvent) NameUtils.getQualifiedTableSegmentName(io.pravega.shared.NameUtils.getQualifiedTableSegmentName) RetryHelper(io.pravega.controller.util.RetryHelper) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) AbstractKVTableMetadataStore(io.pravega.controller.store.kvtable.AbstractKVTableMetadataStore) Synchronized(lombok.Synchronized) Executor(java.util.concurrent.Executor) Set(java.util.Set) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) KVTableMetadataStore(io.pravega.controller.store.kvtable.KVTableMetadataStore) List(java.util.List) RetryableException(io.pravega.controller.retryable.RetryableException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CreateKeyValueTableStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateKeyValueTableStatus) Futures(io.pravega.common.concurrent.Futures) GrpcAuthHelper(io.pravega.controller.server.security.auth.GrpcAuthHelper) KVTableState(io.pravega.controller.store.kvtable.KVTableState) CreateTableEvent(io.pravega.shared.controller.event.kvtable.CreateTableEvent) RetryableException(io.pravega.controller.retryable.RetryableException) UUID(java.util.UUID)

Example 2 with CreateTableEvent

use of io.pravega.shared.controller.event.kvtable.CreateTableEvent in project pravega by pravega.

the class CreateTableTask method execute.

@Override
public CompletableFuture<Void> execute(final CreateTableEvent request) {
    String scope = request.getScopeName();
    String kvt = request.getKvtName();
    int partitionCount = request.getPartitionCount();
    int primaryKeyLength = request.getPrimaryKeyLength();
    int secondaryKeyLength = request.getSecondaryKeyLength();
    long creationTime = request.getTimestamp();
    long requestId = request.getRequestId();
    long rolloverSize = request.getRolloverSizeBytes();
    String kvTableId = request.getTableId().toString();
    KeyValueTableConfiguration config = KeyValueTableConfiguration.builder().partitionCount(partitionCount).primaryKeyLength(primaryKeyLength).secondaryKeyLength(secondaryKeyLength).rolloverSizeBytes(rolloverSize).build();
    final OperationContext context = kvtMetadataStore.createContext(scope, kvt, requestId);
    return RetryHelper.withRetriesAsync(() -> getKeyValueTable(scope, kvt).thenCompose(table -> table.getId(context)).thenCompose(id -> {
        if (!id.equals(kvTableId)) {
            log.debug(requestId, "Skipped processing create event for KeyValueTable {}/{} with Id:{} as UUIDs did not match.", scope, kvt, id);
            return CompletableFuture.completedFuture(null);
        } else {
            return kvtMetadataStore.isScopeSealed(scope, context, executor).thenCompose(isScopeSealed -> {
                if (isScopeSealed) {
                    log.warn(requestId, "Scope {} is in sealed state: ", scope);
                    throw new IllegalStateException("Scope in sealed state: " + scope);
                }
                return this.kvtMetadataStore.createKeyValueTable(scope, kvt, config, creationTime, context, executor).thenComposeAsync(response -> {
                    // segments and change the state of the kvtable to active.
                    if (response.getStatus().equals(CreateKVTableResponse.CreateStatus.NEW) || response.getStatus().equals(CreateKVTableResponse.CreateStatus.EXISTS_CREATING)) {
                        final int startingSegmentNumber = response.getStartingSegmentNumber();
                        final int minNumSegments = response.getConfiguration().getPartitionCount();
                        final int keyLength = response.getConfiguration().getPrimaryKeyLength() + response.getConfiguration().getSecondaryKeyLength();
                        List<Long> newSegments = IntStream.range(startingSegmentNumber, startingSegmentNumber + minNumSegments).boxed().map(x -> NameUtils.computeSegmentId(x, 0)).collect(Collectors.toList());
                        kvtMetadataTasks.createNewSegments(scope, kvt, newSegments, keyLength, requestId, config.getRolloverSizeBytes()).thenCompose(y -> {
                            kvtMetadataStore.getVersionedState(scope, kvt, context, executor).thenCompose(state -> {
                                if (state.getObject().equals(KVTableState.CREATING)) {
                                    kvtMetadataStore.updateVersionedState(scope, kvt, KVTableState.ACTIVE, state, context, executor);
                                }
                                return CompletableFuture.completedFuture(null);
                            });
                            return CompletableFuture.completedFuture(null);
                        });
                    }
                    return CompletableFuture.completedFuture(null);
                }, executor);
            });
        }
    }), e -> Exceptions.unwrap(e) instanceof RetryableException, Integer.MAX_VALUE, executor);
}
Also used : OperationContext(io.pravega.controller.store.stream.OperationContext) IntStream(java.util.stream.IntStream) OperationContext(io.pravega.controller.store.stream.OperationContext) NameUtils(io.pravega.shared.NameUtils) KeyValueTable(io.pravega.controller.store.kvtable.KeyValueTable) Exceptions(io.pravega.common.Exceptions) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) CreateKVTableResponse(io.pravega.controller.store.kvtable.CreateKVTableResponse) KeyValueTableConfiguration(io.pravega.client.tables.KeyValueTableConfiguration) TableMetadataTasks(io.pravega.controller.task.KeyValueTable.TableMetadataTasks) Collectors(java.util.stream.Collectors) KVTableMetadataStore(io.pravega.controller.store.kvtable.KVTableMetadataStore) CreateTableEvent(io.pravega.shared.controller.event.kvtable.CreateTableEvent) List(java.util.List) TagLogger(io.pravega.common.tracing.TagLogger) RetryableException(io.pravega.controller.retryable.RetryableException) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Preconditions(com.google.common.base.Preconditions) KVTableState(io.pravega.controller.store.kvtable.KVTableState) RetryHelper(io.pravega.controller.util.RetryHelper) KeyValueTableConfiguration(io.pravega.client.tables.KeyValueTableConfiguration) RetryableException(io.pravega.controller.retryable.RetryableException) List(java.util.List)

Aggregations

KeyValueTableConfiguration (io.pravega.client.tables.KeyValueTableConfiguration)2 Exceptions (io.pravega.common.Exceptions)2 TagLogger (io.pravega.common.tracing.TagLogger)2 RetryableException (io.pravega.controller.retryable.RetryableException)2 KVTableMetadataStore (io.pravega.controller.store.kvtable.KVTableMetadataStore)2 KVTableState (io.pravega.controller.store.kvtable.KVTableState)2 OperationContext (io.pravega.controller.store.stream.OperationContext)2 RetryHelper (io.pravega.controller.util.RetryHelper)2 CreateTableEvent (io.pravega.shared.controller.event.kvtable.CreateTableEvent)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 Collectors (java.util.stream.Collectors)2 LoggerFactory (org.slf4j.LoggerFactory)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions (com.google.common.base.Preconditions)1 EventStreamClientFactory (io.pravega.client.EventStreamClientFactory)1 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)1 Futures (io.pravega.common.concurrent.Futures)1 SegmentHelper (io.pravega.controller.server.SegmentHelper)1