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);
}
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);
}
Aggregations