use of org.apache.bookkeeper.api.kv.op.TxnOp in project bookkeeper by apache.
the class RootRangeStoreImpl method executeCreateNamespaceTxn.
private CompletableFuture<CreateNamespaceResponse> executeCreateNamespaceTxn(long currentNsId, long currentNsIdRev, CreateNamespaceRequest request) {
long namespaceId = currentNsId + 1;
String nsName = request.getName();
NamespaceMetadata metadata = NamespaceMetadata.newBuilder().setProps(NamespaceProperties.newBuilder().setNamespaceId(namespaceId).setNamespaceName(nsName).setDefaultStreamConf(request.getColConf().getDefaultStreamConf())).build();
byte[] nsNameKey = getNamespaceNameKey(nsName);
byte[] nsNameVal = Bytes.toBytes(namespaceId);
byte[] nsIdKey = getNamespaceIdKey(namespaceId);
byte[] nsIdVal = metadata.toByteArray();
TxnOp<byte[], byte[]> txn = store.newTxn().If(store.newCompareValue(CompareResult.EQUAL, nsNameKey, null), currentNsIdRev < 0 ? store.newCompareValue(CompareResult.EQUAL, NS_ID_KEY, null) : store.newCompareModRevision(CompareResult.EQUAL, NS_ID_KEY, currentNsIdRev)).Then(store.newPut(nsNameKey, nsNameVal), store.newPut(nsIdKey, nsIdVal), store.newPut(NS_ID_KEY, Bytes.toBytes(namespaceId))).build();
return store.txn(txn).thenApply(txnResult -> {
try {
CreateNamespaceResponse.Builder respBuilder = CreateNamespaceResponse.newBuilder();
if (txnResult.isSuccess()) {
respBuilder.setCode(StatusCode.SUCCESS);
respBuilder.setColProps(metadata.getProps());
} else {
// TODO: differentiate the error code
respBuilder.setCode(StatusCode.INTERNAL_SERVER_ERROR);
}
return respBuilder.build();
} finally {
txnResult.close();
}
}).whenComplete((resp, cause) -> txn.close());
}
use of org.apache.bookkeeper.api.kv.op.TxnOp in project bookkeeper by apache.
the class RootRangeStoreImpl method executeCreateStreamTxn.
private CompletableFuture<CreateStreamResponse> executeCreateStreamTxn(long nsId, String streamName, StreamConfiguration streamConf, long currentStreamId, long currentStreamIdRev) {
long streamId;
if (currentStreamId < 0) {
streamId = MIN_DATA_STREAM_ID;
} else {
streamId = currentStreamId + 1;
}
long scId = placementPolicy.placeStreamRange(streamId, 0L);
StreamConfiguration newStreamConf = streamConf;
// no backend service url is provided, use the default service url
if (isBlank(streamConf.getBackendServiceUrl())) {
newStreamConf = StreamConfiguration.newBuilder(streamConf).setBackendServiceUrl(defaultServiceUri.toString()).build();
}
StreamProperties streamProps = StreamProperties.newBuilder().setStreamId(streamId).setStreamName(streamName).setStorageContainerId(scId).setStreamConf(newStreamConf).build();
byte[] nsIdKey = getNamespaceIdKey(nsId);
byte[] streamNameKey = getStreamNameKey(nsId, streamName);
byte[] streamNameVal = Bytes.toBytes(streamId);
byte[] streamIdKey = getStreamIdKey(nsId, streamId);
byte[] streamIdVal = streamProps.toByteArray();
TxnOp<byte[], byte[]> txn = store.newTxn().If(store.newCompareValue(CompareResult.NOT_EQUAL, nsIdKey, null), currentStreamIdRev < 0 ? store.newCompareValue(CompareResult.EQUAL, STREAM_ID_KEY, null) : store.newCompareModRevision(CompareResult.EQUAL, STREAM_ID_KEY, currentStreamIdRev), store.newCompareValue(CompareResult.EQUAL, streamNameKey, null)).Then(store.newPut(streamNameKey, streamNameVal), store.newPut(streamIdKey, streamIdVal), store.newPut(STREAM_ID_KEY, Bytes.toBytes(streamId))).build();
return store.txn(txn).thenApply(txnResult -> {
try {
CreateStreamResponse.Builder respBuilder = CreateStreamResponse.newBuilder();
if (txnResult.isSuccess()) {
respBuilder.setCode(StatusCode.SUCCESS);
respBuilder.setStreamProps(streamProps);
} else {
// TODO: differentiate the error codes
respBuilder.setCode(StatusCode.INTERNAL_SERVER_ERROR);
}
return respBuilder.build();
} finally {
txnResult.close();
txn.close();
}
}).exceptionally(cause -> {
txn.close();
return CreateStreamResponse.newBuilder().setCode(StatusCode.INTERNAL_SERVER_ERROR).build();
});
}
use of org.apache.bookkeeper.api.kv.op.TxnOp in project bookkeeper by apache.
the class MVCCStoreImpl method processTxn.
synchronized TxnResult<K, V> processTxn(long revision, TxnOp<K, V> op) {
checkStoreOpen();
// 1. process the compares
boolean success = processCompares(op);
// 2. prepare the response list
List<Op<K, V>> operations;
List<Result<K, V>> results;
if (success) {
operations = op.successOps();
} else {
operations = op.failureOps();
}
if (operations == null) {
operations = Collections.emptyList();
}
results = Lists.newArrayListWithExpectedSize(operations.size());
// 3. process the operations
try (WriteBatch batch = new WriteBatch()) {
for (Op<K, V> o : operations) {
results.add(executeOp(revision, batch, o));
}
executeBatch(batch);
// 4. repare the result
TxnResultImpl<K, V> txnResult = resultFactory.newTxnResult(revision);
txnResult.isSuccess(success);
txnResult.results(results);
txnResult.code(Code.OK);
return txnResult;
} catch (StateStoreRuntimeException e) {
results.forEach(Result::close);
throw e;
}
}
use of org.apache.bookkeeper.api.kv.op.TxnOp in project bookkeeper by apache.
the class MetaRangeImpl method unsafeCreate.
private void unsafeCreate(CompletableFuture<Boolean> createFuture, StreamProperties streamProps) {
// 1. verify the state
checkLifecycleState(LifecycleState.UNINIT);
this.lifecycleState = LifecycleState.CREATING;
// 2. store the props/configuration
synchronized (this) {
this.streamProps = streamProps;
}
this.streamId = streamProps.getStreamId();
this.cTime = this.mTime = System.currentTimeMillis();
// 3. create the ranges
List<RangeProperties> propertiesList = split(streamId, streamProps.getStreamConf().getInitialNumRanges(), nextRangeId, placementPolicy);
List<Op<byte[], byte[]>> successOps = Lists.newArrayListWithExpectedSize(propertiesList.size() + 1);
for (RangeProperties props : propertiesList) {
RangeMetadata meta = RangeMetadata.newBuilder().setProps(props).setCreateTime(cTime).setFenceTime(Long.MAX_VALUE).setState(RangeState.RANGE_ACTIVE).addAllParents(Lists.newArrayList()).build();
ranges.put(props.getRangeId(), meta);
currentRanges.add(props.getRangeId());
successOps.add(store.newPut(getStreamRangeKey(streamId, props.getRangeId()), meta.toByteArray()));
}
nextRangeId += propertiesList.size();
// serialize the stream metadata
byte[] streamMetadataKey = getStreamMetadataKey(streamId);
successOps.add(store.newPut(streamMetadataKey, toStreamMetadata(LifecycleState.CREATED).toByteArray()));
TxnOp<byte[], byte[]> txn = store.newTxn().If(store.newCompareValue(CompareResult.EQUAL, streamMetadataKey, null)).Then(successOps.toArray(new Op[successOps.size()])).build();
if (log.isTraceEnabled()) {
log.trace("Execute create stream metadata range txn {}", streamProps);
}
store.txn(txn).thenApplyAsync(txnResult -> {
try {
if (log.isTraceEnabled()) {
log.trace("Create stream metadata range txn result = {}", txnResult.isSuccess());
}
if (txnResult.isSuccess()) {
List<Result<byte[], byte[]>> results = txnResult.results();
MetaRangeImpl.this.revision = results.get(results.size() - 1).revision();
// mark the state to CREATED
this.lifecycleState = LifecycleState.CREATED;
createFuture.complete(true);
} else {
createFuture.complete(false);
}
return null;
} finally {
txnResult.close();
}
}, executor).exceptionally(cause -> {
createFuture.completeExceptionally(cause);
return null;
});
}
Aggregations