You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by za...@apache.org on 2024/05/07 10:06:32 UTC
(flink) branch master updated (ffa3869c48a -> ea4112aefa7)
This is an automated email from the ASF dual-hosted git repository.
zakelly pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
from ffa3869c48a [FLINK-25537] [JUnit5 Migration] Migrate the api package of flink-core module to JUnit5
new 88f6d069784 [FLINK-35161][state] Refactor ForStGeneralMultiGetOperation and ForStWriteBatchOperation
new ea4112aefa7 [FLINK-35161][state] Implement StateExecutor for ForStStateBackend
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../asyncprocessing/AsyncExecutionController.java | 12 +-
.../runtime/asyncprocessing/StateExecutor.java | 15 ++-
.../runtime/asyncprocessing/StateRequest.java | 10 +-
.../asyncprocessing/StateRequestBuffer.java | 21 ++--
...ateExecutor.java => StateRequestContainer.java} | 26 ++--
.../AsyncExecutionControllerTest.java | 20 ++-
.../MockStateRequestContainer.java} | 32 +++--
.../flink/state/forst/ForStDBGetRequest.java | 66 ++++++++++
.../apache/flink/state/forst/ForStDBOperation.java | 8 +-
.../flink/state/forst/ForStDBPutRequest.java | 82 +++++++++++++
.../state/forst/ForStGeneralMultiGetOperation.java | 46 ++-----
.../apache/flink/state/forst/ForStInnerTable.java | 20 +++
.../flink/state/forst/ForStStateExecutor.java | 130 ++++++++++++++++++++
.../state/forst/ForStStateRequestClassifier.java | 98 +++++++++++++++
.../apache/flink/state/forst/ForStValueState.java | 33 +++++
.../state/forst/ForStWriteBatchOperation.java | 54 +++------
.../state/forst/ForStDBOperationTestBase.java | 46 +++++++
.../forst/ForStGeneralMultiGetOperationTest.java | 32 +++--
.../flink/state/forst/ForStStateExecutorTest.java | 134 +++++++++++++++++++++
.../state/forst/ForStWriteBatchOperationTest.java | 65 ++++++----
.../InternalTimerServiceAsyncImplTest.java | 17 +--
.../AbstractAsyncStateStreamOperatorTest.java | 15 +--
.../AbstractAsyncStateStreamOperatorV2Test.java | 15 +--
.../asyncprocessing/MockStateExecutor.java | 50 ++++++++
24 files changed, 832 insertions(+), 215 deletions(-)
copy flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/{StateExecutor.java => StateRequestContainer.java} (57%)
copy flink-runtime/src/{main/java/org/apache/flink/runtime/asyncprocessing/StateExecutor.java => test/java/org/apache/flink/runtime/asyncprocessing/MockStateRequestContainer.java} (59%)
create mode 100644 flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStDBGetRequest.java
create mode 100644 flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStDBPutRequest.java
create mode 100644 flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStStateExecutor.java
create mode 100644 flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStStateRequestClassifier.java
create mode 100644 flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStStateExecutorTest.java
create mode 100644 flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/operators/asyncprocessing/MockStateExecutor.java
(flink) 02/02: [FLINK-35161][state] Implement StateExecutor for ForStStateBackend
Posted by za...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
zakelly pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit ea4112aefa72e9d15525a72157ced3e3da3650ff
Author: Jinzhong Li <li...@gmail.com>
AuthorDate: Mon Apr 29 15:12:15 2024 +0800
[FLINK-35161][state] Implement StateExecutor for ForStStateBackend
This closes #24739.
---
.../asyncprocessing/AsyncExecutionController.java | 12 +-
.../runtime/asyncprocessing/StateExecutor.java | 15 ++-
.../runtime/asyncprocessing/StateRequest.java | 10 +-
.../asyncprocessing/StateRequestBuffer.java | 21 ++--
...ateExecutor.java => StateRequestContainer.java} | 26 ++--
.../AsyncExecutionControllerTest.java | 20 ++-
.../MockStateRequestContainer.java} | 32 +++--
.../apache/flink/state/forst/ForStInnerTable.java | 20 +++
.../flink/state/forst/ForStStateExecutor.java | 130 ++++++++++++++++++++
.../state/forst/ForStStateRequestClassifier.java | 98 +++++++++++++++
.../apache/flink/state/forst/ForStValueState.java | 33 +++++
.../flink/state/forst/ForStStateExecutorTest.java | 134 +++++++++++++++++++++
.../InternalTimerServiceAsyncImplTest.java | 17 +--
.../AbstractAsyncStateStreamOperatorTest.java | 15 +--
.../AbstractAsyncStateStreamOperatorV2Test.java | 15 +--
.../asyncprocessing/MockStateExecutor.java | 50 ++++++++
16 files changed, 553 insertions(+), 95 deletions(-)
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/AsyncExecutionController.java b/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/AsyncExecutionController.java
index fa1044dbdf0..83c18a70be0 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/AsyncExecutionController.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/AsyncExecutionController.java
@@ -32,7 +32,7 @@ import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
-import java.util.List;
+import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
/**
@@ -252,8 +252,14 @@ public class AsyncExecutionController<K> implements StateRequestHandler {
if (!force && stateRequestsBuffer.activeQueueSize() < batchSize) {
return;
}
- List<StateRequest<?, ?, ?>> toRun = stateRequestsBuffer.popActive(batchSize);
- stateExecutor.executeBatchRequests(toRun);
+
+ Optional<StateRequestContainer> toRun =
+ stateRequestsBuffer.popActive(
+ batchSize, () -> stateExecutor.createStateRequestContainer());
+ if (!toRun.isPresent() || toRun.get().isEmpty()) {
+ return;
+ }
+ stateExecutor.executeBatchRequests(toRun.get());
stateRequestsBuffer.advanceSeq();
}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateExecutor.java b/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateExecutor.java
index 63b0257f8ec..caf0f504d94 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateExecutor.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateExecutor.java
@@ -28,9 +28,18 @@ public interface StateExecutor {
/**
* Execute a batch of state requests.
*
- * @param processingRequests the given batch of processing requests
+ * @param stateRequestContainer The StateRequestContainer which holds the given batch of
+ * processing requests.
* @return A future can determine whether execution has completed.
*/
- CompletableFuture<Boolean> executeBatchRequests(
- Iterable<StateRequest<?, ?, ?>> processingRequests);
+ CompletableFuture<Void> executeBatchRequests(StateRequestContainer stateRequestContainer);
+
+ /**
+ * Create a {@link StateRequestContainer} which is used to hold the batched {@link
+ * StateRequest}.
+ */
+ StateRequestContainer createStateRequestContainer();
+
+ /** Shutdown the StateExecutor, and new committed state execution requests will be rejected. */
+ void shutdown();
}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateRequest.java b/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateRequest.java
index 59009361751..87fd02926f2 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateRequest.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateRequest.java
@@ -51,7 +51,7 @@ public class StateRequest<K, IN, OUT> implements Serializable {
/** The record context of this request. */
private final RecordContext<K> context;
- StateRequest(
+ public StateRequest(
@Nullable State state,
StateRequestType type,
@Nullable IN payload,
@@ -64,17 +64,17 @@ public class StateRequest<K, IN, OUT> implements Serializable {
this.context = context;
}
- StateRequestType getRequestType() {
+ public StateRequestType getRequestType() {
return type;
}
@Nullable
- IN getPayload() {
+ public IN getPayload() {
return payload;
}
@Nullable
- State getState() {
+ public State getState() {
return state;
}
@@ -82,7 +82,7 @@ public class StateRequest<K, IN, OUT> implements Serializable {
return stateFuture;
}
- RecordContext<K> getRecordContext() {
+ public RecordContext<K> getRecordContext() {
return context;
}
}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateRequestBuffer.java b/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateRequestBuffer.java
index eca28ab695a..263b7cce092 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateRequestBuffer.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateRequestBuffer.java
@@ -23,19 +23,18 @@ import org.apache.flink.util.concurrent.ExecutorThreadFactory;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
-import java.util.ArrayList;
-import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
-import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
+import java.util.function.Supplier;
/**
* A buffer to hold state requests to execute state requests in batch, which can only be manipulated
@@ -198,18 +197,20 @@ public class StateRequestBuffer<K> {
* Try to pop state requests from active queue, if the size of active queue is less than N,
* return all the requests in active queue.
*
- * @param n the number of state requests to pop.
- * @return A list of state requests.
+ * @param n The number of state requests to pop.
+ * @param requestContainerInitializer Initializer for the stateRequest container
+ * @return A StateRequestContainer which holds the popped state requests.
*/
- List<StateRequest<?, ?, ?>> popActive(int n) {
+ Optional<StateRequestContainer> popActive(
+ int n, Supplier<StateRequestContainer> requestContainerInitializer) {
final int count = Math.min(n, activeQueue.size());
if (count <= 0) {
- return Collections.emptyList();
+ return Optional.empty();
}
- ArrayList<StateRequest<?, ?, ?>> ret = new ArrayList<>(count);
+ StateRequestContainer stateRequestContainer = requestContainerInitializer.get();
for (int i = 0; i < count; i++) {
- ret.add(activeQueue.pop());
+ stateRequestContainer.offer(activeQueue.pop());
}
- return ret;
+ return Optional.of(stateRequestContainer);
}
}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateExecutor.java b/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateRequestContainer.java
similarity index 57%
copy from flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateExecutor.java
copy to flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateRequestContainer.java
index 63b0257f8ec..1a389272263 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateExecutor.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateRequestContainer.java
@@ -18,19 +18,19 @@
package org.apache.flink.runtime.asyncprocessing;
-import org.apache.flink.annotation.Internal;
+/**
+ * A container which is used to hold {@link StateRequest}s. The role of {@code
+ * StateRequestContainer} is to serve as an intermediary carrier for data transmission between the
+ * runtime layer and the state layer. It stores the stateRequest from the runtime layer, which is
+ * then processed by the state layer.
+ *
+ * <p>Notice that the {@code StateRequestContainer} may not be thread-safe.
+ */
+public interface StateRequestContainer {
-import java.util.concurrent.CompletableFuture;
+ /** Preserve a stateRequest into the {@code StateRequestContainer}. */
+ void offer(StateRequest<?, ?, ?> stateRequest);
-/** Executor for executing batch {@link StateRequest}s. */
-@Internal
-public interface StateExecutor {
- /**
- * Execute a batch of state requests.
- *
- * @param processingRequests the given batch of processing requests
- * @return A future can determine whether execution has completed.
- */
- CompletableFuture<Boolean> executeBatchRequests(
- Iterable<StateRequest<?, ?, ?>> processingRequests);
+ /** Returns whether the container is empty. */
+ boolean isEmpty();
}
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/asyncprocessing/AsyncExecutionControllerTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/asyncprocessing/AsyncExecutionControllerTest.java
index c55232f2cf1..ee83d5f7c79 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/asyncprocessing/AsyncExecutionControllerTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/asyncprocessing/AsyncExecutionControllerTest.java
@@ -552,10 +552,12 @@ class AsyncExecutionControllerTest {
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
- public CompletableFuture<Boolean> executeBatchRequests(
- Iterable<StateRequest<?, ?, ?>> processingRequests) {
- CompletableFuture<Boolean> future = new CompletableFuture<>();
- for (StateRequest request : processingRequests) {
+ public CompletableFuture<Void> executeBatchRequests(
+ StateRequestContainer stateRequestContainer) {
+ Preconditions.checkArgument(stateRequestContainer instanceof MockStateRequestContainer);
+ CompletableFuture<Void> future = new CompletableFuture<>();
+ for (StateRequest request :
+ ((MockStateRequestContainer) stateRequestContainer).getStateRequestList()) {
if (request.getRequestType() == StateRequestType.VALUE_GET) {
Preconditions.checkState(request.getState() != null);
TestValueState state = (TestValueState) request.getState();
@@ -574,8 +576,16 @@ class AsyncExecutionControllerTest {
throw new UnsupportedOperationException("Unsupported request type");
}
}
- future.complete(true);
+ future.complete(null);
return future;
}
+
+ @Override
+ public StateRequestContainer createStateRequestContainer() {
+ return new MockStateRequestContainer();
+ }
+
+ @Override
+ public void shutdown() {}
}
}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateExecutor.java b/flink-runtime/src/test/java/org/apache/flink/runtime/asyncprocessing/MockStateRequestContainer.java
similarity index 59%
copy from flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateExecutor.java
copy to flink-runtime/src/test/java/org/apache/flink/runtime/asyncprocessing/MockStateRequestContainer.java
index 63b0257f8ec..f2200a55c03 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/StateExecutor.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/asyncprocessing/MockStateRequestContainer.java
@@ -18,19 +18,25 @@
package org.apache.flink.runtime.asyncprocessing;
-import org.apache.flink.annotation.Internal;
+import java.util.ArrayList;
+import java.util.List;
-import java.util.concurrent.CompletableFuture;
+/** The mocked {@link StateRequestContainer} for testing. */
+public class MockStateRequestContainer implements StateRequestContainer {
-/** Executor for executing batch {@link StateRequest}s. */
-@Internal
-public interface StateExecutor {
- /**
- * Execute a batch of state requests.
- *
- * @param processingRequests the given batch of processing requests
- * @return A future can determine whether execution has completed.
- */
- CompletableFuture<Boolean> executeBatchRequests(
- Iterable<StateRequest<?, ?, ?>> processingRequests);
+ private final List<StateRequest<?, ?, ?>> stateRequestList = new ArrayList<>();
+
+ @Override
+ public void offer(StateRequest<?, ?, ?> stateRequest) {
+ stateRequestList.add(stateRequest);
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return stateRequestList.isEmpty();
+ }
+
+ public List<StateRequest<?, ?, ?>> getStateRequestList() {
+ return stateRequestList;
+ }
}
diff --git a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStInnerTable.java b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStInnerTable.java
index 8a3ac9172ad..5d36b72097b 100644
--- a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStInnerTable.java
+++ b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStInnerTable.java
@@ -18,6 +18,8 @@
package org.apache.flink.state.forst;
+import org.apache.flink.runtime.asyncprocessing.StateRequest;
+
import org.rocksdb.ColumnFamilyHandle;
import java.io.IOException;
@@ -63,4 +65,22 @@ public interface ForStInnerTable<K, V> {
* @throws IOException Thrown if the deserialization encountered an I/O related error.
*/
V deserializeValue(byte[] value) throws IOException;
+
+ /**
+ * Build a {@link ForStDBGetRequest} that belong to this {@code ForStInnerTable} with the given
+ * stateRequest.
+ *
+ * @param stateRequest The given stateRequest.
+ * @return The corresponding ForSt GetRequest.
+ */
+ ForStDBGetRequest<K, V> buildDBGetRequest(StateRequest<?, ?, ?> stateRequest);
+
+ /**
+ * Build a {@link ForStDBPutRequest} that belong to {@code ForStInnerTable} with the given
+ * stateRequest.
+ *
+ * @param stateRequest The given stateRequest.
+ * @return The corresponding ForSt PutRequest.
+ */
+ ForStDBPutRequest<K, V> buildDBPutRequest(StateRequest<?, ?, ?> stateRequest);
}
diff --git a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStStateExecutor.java b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStStateExecutor.java
new file mode 100644
index 00000000000..28c3c945ccf
--- /dev/null
+++ b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStStateExecutor.java
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.state.forst;
+
+import org.apache.flink.runtime.asyncprocessing.StateExecutor;
+import org.apache.flink.runtime.asyncprocessing.StateRequest;
+import org.apache.flink.runtime.asyncprocessing.StateRequestContainer;
+import org.apache.flink.util.FlinkRuntimeException;
+import org.apache.flink.util.Preconditions;
+import org.apache.flink.util.concurrent.ExecutorThreadFactory;
+import org.apache.flink.util.concurrent.FutureUtils;
+
+import org.rocksdb.RocksDB;
+import org.rocksdb.WriteOptions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+/**
+ * The {@link StateExecutor} implementation which executing batch {@link StateRequest}s for
+ * ForStStateBackend.
+ */
+public class ForStStateExecutor implements StateExecutor {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ForStStateExecutor.class);
+
+ /**
+ * The coordinator thread which schedules the execution of multiple batches of stateRequests.
+ * The number of coordinator threads is 1 to ensure that multiple batches of stateRequests can
+ * be executed sequentially.
+ */
+ private final ExecutorService coordinatorThread;
+
+ /** The worker thread that actually executes the {@link StateRequest}s. */
+ private final ExecutorService workerThreads;
+
+ private final RocksDB db;
+
+ private final WriteOptions writeOptions;
+
+ public ForStStateExecutor(int ioParallelism, RocksDB db, WriteOptions writeOptions) {
+ this.coordinatorThread =
+ Executors.newSingleThreadScheduledExecutor(
+ new ExecutorThreadFactory("ForSt-StateExecutor-Coordinator"));
+ this.workerThreads =
+ Executors.newFixedThreadPool(
+ ioParallelism, new ExecutorThreadFactory("ForSt-StateExecutor-IO"));
+ this.db = db;
+ this.writeOptions = writeOptions;
+ }
+
+ @Override
+ public CompletableFuture<Void> executeBatchRequests(
+ StateRequestContainer stateRequestContainer) {
+ Preconditions.checkArgument(stateRequestContainer instanceof ForStStateRequestClassifier);
+ ForStStateRequestClassifier stateRequestClassifier =
+ (ForStStateRequestClassifier) stateRequestContainer;
+ return CompletableFuture.supplyAsync(
+ () -> {
+ long startTime = System.currentTimeMillis();
+ List<CompletableFuture<Void>> futures = new ArrayList<>(2);
+ List<ForStDBPutRequest<?, ?>> putRequests =
+ stateRequestClassifier.pollDbPutRequests();
+ if (!putRequests.isEmpty()) {
+ ForStWriteBatchOperation writeOperations =
+ new ForStWriteBatchOperation(
+ db, putRequests, writeOptions, workerThreads);
+ futures.add(writeOperations.process());
+ }
+
+ List<ForStDBGetRequest<?, ?>> getRequests =
+ stateRequestClassifier.pollDbGetRequests();
+ if (!getRequests.isEmpty()) {
+ ForStGeneralMultiGetOperation getOperations =
+ new ForStGeneralMultiGetOperation(db, getRequests, workerThreads);
+ futures.add(getOperations.process());
+ }
+
+ try {
+ FutureUtils.waitForAll(futures).get();
+ } catch (InterruptedException | ExecutionException e) {
+ throw new FlinkRuntimeException(
+ "Exception when executing ForSt writeBatch or multiGet operation",
+ e);
+ }
+ long duration = System.currentTimeMillis() - startTime;
+ LOG.debug(
+ "Complete executing a batch of state requests, putRequest size {}, getRequest size {}, duration {} ms",
+ putRequests.size(),
+ getRequests.size(),
+ duration);
+ return null;
+ },
+ coordinatorThread);
+ }
+
+ @Override
+ public StateRequestContainer createStateRequestContainer() {
+ return new ForStStateRequestClassifier();
+ }
+
+ @Override
+ public void shutdown() {
+ workerThreads.shutdown();
+ coordinatorThread.shutdown();
+ LOG.info("Shutting down the ForStStateExecutor.");
+ }
+}
diff --git a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStStateRequestClassifier.java b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStStateRequestClassifier.java
new file mode 100644
index 00000000000..f32b3b665af
--- /dev/null
+++ b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStStateRequestClassifier.java
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.state.forst;
+
+import org.apache.flink.runtime.asyncprocessing.StateRequest;
+import org.apache.flink.runtime.asyncprocessing.StateRequestContainer;
+import org.apache.flink.runtime.asyncprocessing.StateRequestType;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * The ForSt {@link StateRequestContainer} which can classify the state requests by ForStDB
+ * requestType (Get、Put or Iterator).
+ */
+public class ForStStateRequestClassifier implements StateRequestContainer {
+
+ private final List<ForStDBGetRequest<?, ?>> dbGetRequests;
+
+ private final List<ForStDBPutRequest<?, ?>> dbPutRequests;
+
+ public ForStStateRequestClassifier() {
+ this.dbGetRequests = new ArrayList<>();
+ this.dbPutRequests = new ArrayList<>();
+ }
+
+ @Override
+ public void offer(StateRequest<?, ?, ?> stateRequest) {
+ convertStateRequestsToForStDBRequests(stateRequest);
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return dbGetRequests.isEmpty() && dbPutRequests.isEmpty();
+ }
+
+ @SuppressWarnings("ConstantConditions")
+ private void convertStateRequestsToForStDBRequests(StateRequest<?, ?, ?> stateRequest) {
+ StateRequestType stateRequestType = stateRequest.getRequestType();
+ switch (stateRequestType) {
+ case VALUE_GET:
+ {
+ ForStValueState<?, ?> forStValueState =
+ (ForStValueState<?, ?>) stateRequest.getState();
+ dbGetRequests.add(forStValueState.buildDBGetRequest(stateRequest));
+ return;
+ }
+ case VALUE_UPDATE:
+ {
+ ForStValueState<?, ?> forStValueState =
+ (ForStValueState<?, ?>) stateRequest.getState();
+ dbPutRequests.add(forStValueState.buildDBPutRequest(stateRequest));
+ return;
+ }
+ case CLEAR:
+ {
+ if (stateRequest.getState() instanceof ForStValueState) {
+ ForStValueState<?, ?> forStValueState =
+ (ForStValueState<?, ?>) stateRequest.getState();
+ dbPutRequests.add(forStValueState.buildDBPutRequest(stateRequest));
+ return;
+ } else {
+ throw new UnsupportedOperationException(
+ "The State "
+ + stateRequest.getState().getClass()
+ + " doesn't yet support the clear method.");
+ }
+ }
+ default:
+ throw new UnsupportedOperationException(
+ "Unsupported state request type:" + stateRequestType);
+ }
+ }
+
+ public List<ForStDBGetRequest<?, ?>> pollDbGetRequests() {
+ return dbGetRequests;
+ }
+
+ public List<ForStDBPutRequest<?, ?>> pollDbPutRequests() {
+ return dbPutRequests;
+ }
+}
diff --git a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStValueState.java b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStValueState.java
index 7386e65fc32..52f2e6d848c 100644
--- a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStValueState.java
+++ b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStValueState.java
@@ -21,10 +21,15 @@ package org.apache.flink.state.forst;
import org.apache.flink.api.common.state.v2.ValueState;
import org.apache.flink.core.memory.DataInputDeserializer;
import org.apache.flink.core.memory.DataOutputSerializer;
+import org.apache.flink.core.state.InternalStateFuture;
+import org.apache.flink.runtime.asyncprocessing.RecordContext;
+import org.apache.flink.runtime.asyncprocessing.StateRequest;
import org.apache.flink.runtime.asyncprocessing.StateRequestHandler;
+import org.apache.flink.runtime.asyncprocessing.StateRequestType;
import org.apache.flink.runtime.state.SerializedCompositeKeyBuilder;
import org.apache.flink.runtime.state.v2.InternalValueState;
import org.apache.flink.runtime.state.v2.ValueStateDescriptor;
+import org.apache.flink.util.Preconditions;
import org.rocksdb.ColumnFamilyHandle;
@@ -95,4 +100,32 @@ public class ForStValueState<K, V> extends InternalValueState<K, V>
inputView.setBuffer(valueBytes);
return getValueSerializer().deserialize(inputView);
}
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public ForStDBGetRequest<ContextKey<K>, V> buildDBGetRequest(
+ StateRequest<?, ?, ?> stateRequest) {
+ Preconditions.checkArgument(stateRequest.getRequestType() == StateRequestType.VALUE_GET);
+ ContextKey<K> contextKey =
+ new ContextKey<>((RecordContext<K>) stateRequest.getRecordContext());
+ return ForStDBGetRequest.of(
+ contextKey, this, (InternalStateFuture<V>) stateRequest.getFuture());
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public ForStDBPutRequest<ContextKey<K>, V> buildDBPutRequest(
+ StateRequest<?, ?, ?> stateRequest) {
+ Preconditions.checkArgument(
+ stateRequest.getRequestType() == StateRequestType.VALUE_UPDATE
+ || stateRequest.getRequestType() == StateRequestType.CLEAR);
+ ContextKey<K> contextKey =
+ new ContextKey<>((RecordContext<K>) stateRequest.getRecordContext());
+ V value =
+ (stateRequest.getRequestType() == StateRequestType.CLEAR)
+ ? null // "Delete(key)" is equivalent to "Put(key, null)"
+ : (V) stateRequest.getPayload();
+ return ForStDBPutRequest.of(
+ contextKey, value, this, (InternalStateFuture<Void>) stateRequest.getFuture());
+ }
}
diff --git a/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStStateExecutorTest.java b/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStStateExecutorTest.java
new file mode 100644
index 00000000000..3e17f30d34b
--- /dev/null
+++ b/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStStateExecutorTest.java
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.state.forst;
+
+import org.apache.flink.runtime.asyncprocessing.RecordContext;
+import org.apache.flink.runtime.asyncprocessing.StateRequest;
+import org.apache.flink.runtime.asyncprocessing.StateRequestContainer;
+import org.apache.flink.runtime.asyncprocessing.StateRequestType;
+import org.apache.flink.runtime.state.KeyGroupRangeAssignment;
+import org.apache.flink.runtime.state.v2.InternalKeyedState;
+
+import org.junit.jupiter.api.Test;
+import org.rocksdb.WriteOptions;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/** Unit test for {@link ForStStateExecutor}. */
+public class ForStStateExecutorTest extends ForStDBOperationTestBase {
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void testExecuteValueStateRequest() throws Exception {
+ ForStStateExecutor forStStateExecutor = new ForStStateExecutor(4, db, new WriteOptions());
+ ForStValueState<Integer, String> state1 = buildForStValueState("value-state-1");
+ ForStValueState<Integer, String> state2 = buildForStValueState("value-state-2");
+
+ StateRequestContainer stateRequestContainer =
+ forStStateExecutor.createStateRequestContainer();
+ assertTrue(stateRequestContainer.isEmpty());
+
+ // 1. Update value state: keyRange [0, keyNum)
+ int keyNum = 1000;
+ for (int i = 0; i < keyNum; i++) {
+ ForStValueState<Integer, String> state = (i % 2 == 0 ? state1 : state2);
+ stateRequestContainer.offer(
+ buildStateRequest(state, StateRequestType.VALUE_UPDATE, i, "test-" + i, i * 2));
+ }
+
+ forStStateExecutor.executeBatchRequests(stateRequestContainer);
+
+ List<StateRequest<?, ?, ?>> checkList = new ArrayList<>();
+ stateRequestContainer = forStStateExecutor.createStateRequestContainer();
+ // 2. Get value state: keyRange [0, keyNum)
+ // Update value state: keyRange [keyNum, keyNum + 100]
+ for (int i = 0; i < keyNum; i++) {
+ ForStValueState<Integer, String> state = (i % 2 == 0 ? state1 : state2);
+ StateRequest<?, ?, ?> getRequest =
+ buildStateRequest(state, StateRequestType.VALUE_GET, i, null, i * 2);
+ stateRequestContainer.offer(getRequest);
+ checkList.add(getRequest);
+ }
+ for (int i = keyNum; i < keyNum + 100; i++) {
+ ForStValueState<Integer, String> state = (i % 2 == 0 ? state1 : state2);
+ stateRequestContainer.offer(
+ buildStateRequest(state, StateRequestType.VALUE_UPDATE, i, "test-" + i, i * 2));
+ }
+ forStStateExecutor.executeBatchRequests(stateRequestContainer).get();
+
+ // 3. Check value state Get result : [0, keyNum)
+ for (StateRequest<?, ?, ?> getRequest : checkList) {
+ assertThat(getRequest.getRequestType()).isEqualTo(StateRequestType.VALUE_GET);
+ int key = (Integer) getRequest.getRecordContext().getKey();
+ assertThat(getRequest.getRecordContext().getRecord()).isEqualTo(key * 2);
+ assertThat(((TestStateFuture<String>) getRequest.getFuture()).getCompletedResult())
+ .isEqualTo("test-" + key);
+ }
+
+ // 4. Clear value state: keyRange [keyNum - 100, keyNum)
+ // Update state with null-value : keyRange [keyNum, keyNum + 100]
+ stateRequestContainer = forStStateExecutor.createStateRequestContainer();
+ for (int i = keyNum - 100; i < keyNum; i++) {
+ ForStValueState<Integer, String> state = (i % 2 == 0 ? state1 : state2);
+ stateRequestContainer.offer(
+ buildStateRequest(state, StateRequestType.CLEAR, i, null, i * 2));
+ }
+ for (int i = keyNum; i < keyNum + 100; i++) {
+ ForStValueState<Integer, String> state = (i % 2 == 0 ? state1 : state2);
+ stateRequestContainer.offer(
+ buildStateRequest(state, StateRequestType.VALUE_UPDATE, i, null, i * 2));
+ }
+ forStStateExecutor.executeBatchRequests(stateRequestContainer);
+
+ // 5. Check that the deleted value is null : keyRange [keyNum - 100, keyNum + 100)
+ stateRequestContainer = forStStateExecutor.createStateRequestContainer();
+ checkList.clear();
+ for (int i = keyNum - 100; i < keyNum + 100; i++) {
+ ForStValueState<Integer, String> state = (i % 2 == 0 ? state1 : state2);
+ StateRequest<?, ?, ?> getRequest =
+ buildStateRequest(state, StateRequestType.VALUE_GET, i, null, i * 2);
+ stateRequestContainer.offer(getRequest);
+ checkList.add(getRequest);
+ }
+ forStStateExecutor.executeBatchRequests(stateRequestContainer);
+ for (StateRequest<?, ?, ?> getRequest : checkList) {
+ assertThat(getRequest.getRequestType()).isEqualTo(StateRequestType.VALUE_GET);
+ assertThat(((TestStateFuture<String>) getRequest.getFuture()).getCompletedResult())
+ .isEqualTo(null);
+ }
+ forStStateExecutor.shutdown();
+ }
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ private <K, V, R> StateRequest<?, ?, ?> buildStateRequest(
+ InternalKeyedState<K, V> innerTable,
+ StateRequestType requestType,
+ K key,
+ V value,
+ R record) {
+ int keyGroup = KeyGroupRangeAssignment.assignToKeyGroup(key, 128);
+ RecordContext<K> recordContext = new RecordContext<>(record, key, t -> {}, keyGroup);
+ TestStateFuture stateFuture = new TestStateFuture<>();
+ return new StateRequest<>(innerTable, requestType, value, stateFuture, recordContext);
+ }
+}
diff --git a/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/InternalTimerServiceAsyncImplTest.java b/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/InternalTimerServiceAsyncImplTest.java
index 5e5d4353fed..26e23fb5db3 100644
--- a/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/InternalTimerServiceAsyncImplTest.java
+++ b/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/InternalTimerServiceAsyncImplTest.java
@@ -23,8 +23,6 @@ import org.apache.flink.api.common.typeutils.base.IntSerializer;
import org.apache.flink.api.common.typeutils.base.StringSerializer;
import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController;
import org.apache.flink.runtime.asyncprocessing.RecordContext;
-import org.apache.flink.runtime.asyncprocessing.StateExecutor;
-import org.apache.flink.runtime.asyncprocessing.StateRequest;
import org.apache.flink.runtime.asyncprocessing.StateRequestType;
import org.apache.flink.runtime.mailbox.SyncMailboxExecutor;
import org.apache.flink.runtime.metrics.groups.TaskIOMetricGroup;
@@ -32,6 +30,7 @@ import org.apache.flink.runtime.metrics.groups.UnregisteredMetricGroups;
import org.apache.flink.runtime.state.KeyGroupRange;
import org.apache.flink.runtime.state.PriorityQueueSetFactory;
import org.apache.flink.runtime.state.heap.HeapPriorityQueueSetFactory;
+import org.apache.flink.streaming.runtime.operators.asyncprocessing.MockStateExecutor;
import org.apache.flink.streaming.runtime.tasks.ProcessingTimeService;
import org.apache.flink.streaming.runtime.tasks.StreamTaskCancellationContext;
import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
@@ -39,8 +38,6 @@ import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import java.util.concurrent.CompletableFuture;
-
import static org.assertj.core.api.Assertions.assertThat;
/** Test for {@link InternalTimerServiceAsyncImpl}. */
@@ -54,7 +51,7 @@ class InternalTimerServiceAsyncImplTest {
void setup() throws Exception {
asyncExecutionController =
new AsyncExecutionController(
- new SyncMailboxExecutor(), new TestStateExecutor(), 128, 2, 1000L, 10);
+ new SyncMailboxExecutor(), new MockStateExecutor(), 128, 2, 1000L, 10);
// ensure arbitrary key is in the key group
int totalKeyGroups = 128;
KeyGroupRange testKeyGroupList = new KeyGroupRange(0, totalKeyGroups - 1);
@@ -208,14 +205,4 @@ class InternalTimerServiceAsyncImplTest {
return key;
}
}
-
- private static class TestStateExecutor implements StateExecutor {
- public TestStateExecutor() {}
-
- @Override
- public CompletableFuture<Boolean> executeBatchRequests(
- Iterable<StateRequest<?, ?, ?>> processingRequests) {
- return CompletableFuture.completedFuture(true);
- }
- }
}
diff --git a/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/operators/asyncprocessing/AbstractAsyncStateStreamOperatorTest.java b/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/operators/asyncprocessing/AbstractAsyncStateStreamOperatorTest.java
index 36149e6960c..17e36fb3af3 100644
--- a/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/operators/asyncprocessing/AbstractAsyncStateStreamOperatorTest.java
+++ b/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/operators/asyncprocessing/AbstractAsyncStateStreamOperatorTest.java
@@ -23,8 +23,6 @@ import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController;
-import org.apache.flink.runtime.asyncprocessing.StateExecutor;
-import org.apache.flink.runtime.asyncprocessing.StateRequest;
import org.apache.flink.runtime.asyncprocessing.StateRequestType;
import org.apache.flink.runtime.checkpoint.CheckpointOptions;
import org.apache.flink.runtime.checkpoint.CheckpointType;
@@ -43,7 +41,6 @@ import org.apache.flink.util.function.ThrowingConsumer;
import org.junit.jupiter.api.Test;
-import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
@@ -153,17 +150,7 @@ public class AbstractAsyncStateStreamOperatorTest {
AsyncExecutionController asyncExecutionController =
((AbstractAsyncStateStreamOperator) testHarness.getOperator())
.getAsyncExecutionController();
- asyncExecutionController.setStateExecutor(
- new StateExecutor() {
- @Override
- public CompletableFuture<Boolean> executeBatchRequests(
- Iterable<StateRequest<?, ?, ?>> processingRequests) {
- for (StateRequest request : processingRequests) {
- request.getFuture().complete(true);
- }
- return CompletableFuture.completedFuture(true);
- }
- });
+ asyncExecutionController.setStateExecutor(new MockStateExecutor());
((AbstractAsyncStateStreamOperator<String>) testHarness.getOperator())
.setAsyncKeyedContextElement(
new StreamRecord<>(Tuple2.of(5, "5")), new TestKeySelector());
diff --git a/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/operators/asyncprocessing/AbstractAsyncStateStreamOperatorV2Test.java b/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/operators/asyncprocessing/AbstractAsyncStateStreamOperatorV2Test.java
index 0e32c486b96..be004e711ff 100644
--- a/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/operators/asyncprocessing/AbstractAsyncStateStreamOperatorV2Test.java
+++ b/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/operators/asyncprocessing/AbstractAsyncStateStreamOperatorV2Test.java
@@ -24,8 +24,6 @@ import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController;
-import org.apache.flink.runtime.asyncprocessing.StateExecutor;
-import org.apache.flink.runtime.asyncprocessing.StateRequest;
import org.apache.flink.runtime.asyncprocessing.StateRequestType;
import org.apache.flink.runtime.checkpoint.CheckpointOptions;
import org.apache.flink.runtime.checkpoint.CheckpointType;
@@ -53,7 +51,6 @@ import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.List;
-import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
@@ -165,17 +162,7 @@ public class AbstractAsyncStateStreamOperatorV2Test {
CheckpointStorageLocationReference.getDefault();
AsyncExecutionController asyncExecutionController =
testOperator.getAsyncExecutionController();
- asyncExecutionController.setStateExecutor(
- new StateExecutor() {
- @Override
- public CompletableFuture<Boolean> executeBatchRequests(
- Iterable<StateRequest<?, ?, ?>> processingRequests) {
- for (StateRequest request : processingRequests) {
- request.getFuture().complete(true);
- }
- return CompletableFuture.completedFuture(true);
- }
- });
+ asyncExecutionController.setStateExecutor(new MockStateExecutor());
testOperator.setAsyncKeyedContextElement(
new StreamRecord<>(Tuple2.of(5, "5")), new TestKeySelector());
asyncExecutionController.handleRequest(null, StateRequestType.VALUE_GET, null);
diff --git a/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/operators/asyncprocessing/MockStateExecutor.java b/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/operators/asyncprocessing/MockStateExecutor.java
new file mode 100644
index 00000000000..b052717ca2f
--- /dev/null
+++ b/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/operators/asyncprocessing/MockStateExecutor.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.streaming.runtime.operators.asyncprocessing;
+
+import org.apache.flink.runtime.asyncprocessing.MockStateRequestContainer;
+import org.apache.flink.runtime.asyncprocessing.StateExecutor;
+import org.apache.flink.runtime.asyncprocessing.StateRequest;
+import org.apache.flink.runtime.asyncprocessing.StateRequestContainer;
+import org.apache.flink.util.Preconditions;
+
+import java.util.concurrent.CompletableFuture;
+
+/** The mocked {@link StateExecutor} for testing. */
+public class MockStateExecutor implements StateExecutor {
+
+ @Override
+ public CompletableFuture<Void> executeBatchRequests(
+ StateRequestContainer stateRequestContainer) {
+ Preconditions.checkArgument(stateRequestContainer instanceof MockStateRequestContainer);
+ for (StateRequest<?, ?, ?> request :
+ ((MockStateRequestContainer) stateRequestContainer).getStateRequestList()) {
+ request.getFuture().complete(null);
+ }
+ return CompletableFuture.completedFuture(null);
+ }
+
+ @Override
+ public StateRequestContainer createStateRequestContainer() {
+ return new MockStateRequestContainer();
+ }
+
+ @Override
+ public void shutdown() {}
+}
(flink) 01/02: [FLINK-35161][state] Refactor ForStGeneralMultiGetOperation and ForStWriteBatchOperation
Posted by za...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
zakelly pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit 88f6d069784355a516ba3b94ef780597dca1adf4
Author: Jinzhong Li <li...@gmail.com>
AuthorDate: Mon Apr 29 15:12:02 2024 +0800
[FLINK-35161][state] Refactor ForStGeneralMultiGetOperation and ForStWriteBatchOperation
---
.../flink/state/forst/ForStDBGetRequest.java | 66 +++++++++++++++++
.../apache/flink/state/forst/ForStDBOperation.java | 8 +--
.../flink/state/forst/ForStDBPutRequest.java | 82 ++++++++++++++++++++++
.../state/forst/ForStGeneralMultiGetOperation.java | 46 +++---------
.../state/forst/ForStWriteBatchOperation.java | 54 ++++----------
.../state/forst/ForStDBOperationTestBase.java | 46 ++++++++++++
.../forst/ForStGeneralMultiGetOperationTest.java | 32 +++++----
.../state/forst/ForStWriteBatchOperationTest.java | 65 ++++++++++-------
8 files changed, 279 insertions(+), 120 deletions(-)
diff --git a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStDBGetRequest.java b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStDBGetRequest.java
new file mode 100644
index 00000000000..7bd8400192c
--- /dev/null
+++ b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStDBGetRequest.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.state.forst;
+
+import org.apache.flink.core.state.InternalStateFuture;
+
+import org.rocksdb.ColumnFamilyHandle;
+
+import java.io.IOException;
+
+/**
+ * The Get access request for ForStDB.
+ *
+ * @param <K> The type of key in get access request.
+ * @param <V> The type of value returned by get request.
+ */
+public class ForStDBGetRequest<K, V> {
+
+ private final K key;
+ private final ForStInnerTable<K, V> table;
+ private final InternalStateFuture<V> future;
+
+ private ForStDBGetRequest(K key, ForStInnerTable<K, V> table, InternalStateFuture<V> future) {
+ this.key = key;
+ this.table = table;
+ this.future = future;
+ }
+
+ public byte[] buildSerializedKey() throws IOException {
+ return table.serializeKey(key);
+ }
+
+ public ColumnFamilyHandle getColumnFamilyHandle() {
+ return table.getColumnFamilyHandle();
+ }
+
+ public void completeStateFuture(byte[] bytesValue) throws IOException {
+ if (bytesValue == null) {
+ future.complete(null);
+ return;
+ }
+ V value = table.deserializeValue(bytesValue);
+ future.complete(value);
+ }
+
+ static <K, V> ForStDBGetRequest<K, V> of(
+ K key, ForStInnerTable<K, V> table, InternalStateFuture<V> future) {
+ return new ForStDBGetRequest<>(key, table, future);
+ }
+}
diff --git a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStDBOperation.java b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStDBOperation.java
index 236ef831855..48ed6c5b4bb 100644
--- a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStDBOperation.java
+++ b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStDBOperation.java
@@ -26,16 +26,14 @@ import java.util.concurrent.CompletableFuture;
* Data access operation to ForStDB. This interface is used to encapsulate the DB access operations
* formed after grouping state access. For more information about “Grouping state access”, please
* refer to FLIP-426.
- *
- * @param <OUT> The type of output for DB access.
*/
@Internal
-public interface ForStDBOperation<OUT> {
+public interface ForStDBOperation {
/**
* Process the ForStDB access requests.
*
- * @return Processing result.
+ * @return The future which indicates whether the operation is completed.
*/
- CompletableFuture<OUT> process();
+ CompletableFuture<Void> process();
}
diff --git a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStDBPutRequest.java b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStDBPutRequest.java
new file mode 100644
index 00000000000..6e9821aeaa5
--- /dev/null
+++ b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStDBPutRequest.java
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.state.forst;
+
+import org.apache.flink.core.state.InternalStateFuture;
+
+import org.rocksdb.ColumnFamilyHandle;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+
+/**
+ * The Put access request for ForStDB.
+ *
+ * @param <K> The type of key in put access request.
+ * @param <V> The type of value in put access request.
+ */
+public class ForStDBPutRequest<K, V> {
+
+ private final K key;
+ @Nullable private final V value;
+ private final ForStInnerTable<K, V> table;
+ private final InternalStateFuture<Void> future;
+
+ private ForStDBPutRequest(
+ K key, V value, ForStInnerTable<K, V> table, InternalStateFuture<Void> future) {
+ this.key = key;
+ this.value = value;
+ this.table = table;
+ this.future = future;
+ }
+
+ public boolean valueIsNull() {
+ return value == null;
+ }
+
+ public ColumnFamilyHandle getColumnFamilyHandle() {
+ return table.getColumnFamilyHandle();
+ }
+
+ public byte[] buildSerializedKey() throws IOException {
+ return table.serializeKey(key);
+ }
+
+ public byte[] buildSerializedValue() throws IOException {
+ assert value != null;
+ return table.serializeValue(value);
+ }
+
+ public void completeStateFuture() {
+ future.complete(null);
+ }
+
+ /**
+ * If the value of the ForStDBPutRequest is null, then the request will signify the deletion of
+ * the data associated with that key.
+ */
+ static <K, V> ForStDBPutRequest<K, V> of(
+ K key,
+ @Nullable V value,
+ ForStInnerTable<K, V> table,
+ InternalStateFuture<Void> future) {
+ return new ForStDBPutRequest<>(key, value, table, future);
+ }
+}
diff --git a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStGeneralMultiGetOperation.java b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStGeneralMultiGetOperation.java
index 8faa5599e44..ce74b574b7b 100644
--- a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStGeneralMultiGetOperation.java
+++ b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStGeneralMultiGetOperation.java
@@ -22,7 +22,6 @@ import org.rocksdb.RocksDB;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
@@ -31,48 +30,38 @@ import java.util.concurrent.atomic.AtomicInteger;
/**
* The general-purpose multiGet operation implementation for ForStDB, which simulates multiGet by
* calling the Get API multiple times with multiple threads.
- *
- * @param <K> The type of key in get access request.
- * @param <V> The type of value in get access request.
*/
-public class ForStGeneralMultiGetOperation<K, V> implements ForStDBOperation<List<V>> {
+public class ForStGeneralMultiGetOperation implements ForStDBOperation {
private static final Logger LOG = LoggerFactory.getLogger(ForStGeneralMultiGetOperation.class);
private final RocksDB db;
- private final List<GetRequest<K, V>> batchRequest;
+ private final List<ForStDBGetRequest<?, ?>> batchRequest;
private final Executor executor;
ForStGeneralMultiGetOperation(
- RocksDB db, List<GetRequest<K, V>> batchRequest, Executor executor) {
+ RocksDB db, List<ForStDBGetRequest<?, ?>> batchRequest, Executor executor) {
this.db = db;
this.batchRequest = batchRequest;
this.executor = executor;
}
@Override
- public CompletableFuture<List<V>> process() {
+ public CompletableFuture<Void> process() {
- CompletableFuture<List<V>> future = new CompletableFuture<>();
- @SuppressWarnings("unchecked")
- V[] result = (V[]) new Object[batchRequest.size()];
- Arrays.fill(result, null);
+ CompletableFuture<Void> future = new CompletableFuture<>();
AtomicInteger counter = new AtomicInteger(batchRequest.size());
for (int i = 0; i < batchRequest.size(); i++) {
- GetRequest<K, V> request = batchRequest.get(i);
- final int index = i;
+ ForStDBGetRequest<?, ?> request = batchRequest.get(i);
executor.execute(
() -> {
try {
- ForStInnerTable<K, V> table = request.table;
- byte[] key = table.serializeKey(request.key);
- byte[] value = db.get(table.getColumnFamilyHandle(), key);
- if (value != null) {
- result[index] = table.deserializeValue(value);
- }
+ byte[] key = request.buildSerializedKey();
+ byte[] value = db.get(request.getColumnFamilyHandle(), key);
+ request.completeStateFuture(value);
} catch (Exception e) {
LOG.warn(
"Error when process general multiGet operation for forStDB", e);
@@ -80,26 +69,11 @@ public class ForStGeneralMultiGetOperation<K, V> implements ForStDBOperation<Lis
} finally {
if (counter.decrementAndGet() == 0
&& !future.isCompletedExceptionally()) {
- future.complete(Arrays.asList(result));
+ future.complete(null);
}
}
});
}
return future;
}
-
- /** The Get access request for ForStDB. */
- static class GetRequest<K, V> {
- final K key;
- final ForStInnerTable<K, V> table;
-
- private GetRequest(K key, ForStInnerTable<K, V> table) {
- this.key = key;
- this.table = table;
- }
-
- static <K, V> GetRequest<K, V> of(K key, ForStInnerTable<K, V> table) {
- return new GetRequest<>(key, table);
- }
- }
}
diff --git a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStWriteBatchOperation.java b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStWriteBatchOperation.java
index a0c71f572cd..90e811c7ccb 100644
--- a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStWriteBatchOperation.java
+++ b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStWriteBatchOperation.java
@@ -22,26 +22,19 @@ import org.rocksdb.RocksDB;
import org.rocksdb.WriteBatch;
import org.rocksdb.WriteOptions;
-import javax.annotation.Nullable;
-
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.Executor;
-/**
- * The writeBatch operation implementation for ForStDB.
- *
- * @param <K> The type of key in put access request.
- * @param <V> The type of value in put access request.
- */
-public class ForStWriteBatchOperation<K, V> implements ForStDBOperation<Void> {
+/** The writeBatch operation implementation for ForStDB. */
+public class ForStWriteBatchOperation implements ForStDBOperation {
private static final int PER_RECORD_ESTIMATE_BYTES = 100;
private final RocksDB db;
- private final List<PutRequest<K, V>> batchRequest;
+ private final List<ForStDBPutRequest<?, ?>> batchRequest;
private final WriteOptions writeOptions;
@@ -49,7 +42,7 @@ public class ForStWriteBatchOperation<K, V> implements ForStDBOperation<Void> {
ForStWriteBatchOperation(
RocksDB db,
- List<PutRequest<K, V>> batchRequest,
+ List<ForStDBPutRequest<?, ?>> batchRequest,
WriteOptions writeOptions,
Executor executor) {
this.db = db;
@@ -64,46 +57,27 @@ public class ForStWriteBatchOperation<K, V> implements ForStDBOperation<Void> {
() -> {
try (WriteBatch writeBatch =
new WriteBatch(batchRequest.size() * PER_RECORD_ESTIMATE_BYTES)) {
- for (PutRequest<K, V> request : batchRequest) {
- ForStInnerTable<K, V> table = request.table;
- if (request.value == null) {
+ for (ForStDBPutRequest<?, ?> request : batchRequest) {
+ if (request.valueIsNull()) {
// put(key, null) == delete(key)
writeBatch.delete(
- table.getColumnFamilyHandle(),
- table.serializeKey(request.key));
+ request.getColumnFamilyHandle(),
+ request.buildSerializedKey());
} else {
writeBatch.put(
- table.getColumnFamilyHandle(),
- table.serializeKey(request.key),
- table.serializeValue(request.value));
+ request.getColumnFamilyHandle(),
+ request.buildSerializedKey(),
+ request.buildSerializedValue());
}
}
db.write(writeOptions, writeBatch);
+ for (ForStDBPutRequest<?, ?> request : batchRequest) {
+ request.completeStateFuture();
+ }
} catch (Exception e) {
throw new CompletionException("Error while adding data to ForStDB", e);
}
},
executor);
}
-
- /** The Put access request for ForStDB. */
- static class PutRequest<K, V> {
- final K key;
- @Nullable final V value;
- final ForStInnerTable<K, V> table;
-
- private PutRequest(K key, V value, ForStInnerTable<K, V> table) {
- this.key = key;
- this.value = value;
- this.table = table;
- }
-
- /**
- * If the value of the PutRequest is null, then the request will signify the deletion of the
- * data associated with that key.
- */
- static <K, V> PutRequest<K, V> of(K key, @Nullable V value, ForStInnerTable<K, V> table) {
- return new PutRequest<>(key, value, table);
- }
- }
}
diff --git a/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStDBOperationTestBase.java b/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStDBOperationTestBase.java
index 1e5939bcd01..ff0e0246353 100644
--- a/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStDBOperationTestBase.java
+++ b/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStDBOperationTestBase.java
@@ -19,6 +19,7 @@
package org.apache.flink.state.forst;
import org.apache.flink.api.common.state.v2.State;
+import org.apache.flink.api.common.state.v2.StateFuture;
import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
import org.apache.flink.api.common.typeutils.base.IntSerializer;
import org.apache.flink.configuration.ConfigConstants;
@@ -42,6 +43,10 @@ import org.rocksdb.ColumnFamilyOptions;
import org.rocksdb.RocksDB;
import java.nio.file.Path;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.BiFunction;
+import java.util.function.Consumer;
+import java.util.function.Function;
import java.util.function.Supplier;
/** Base class for {@link ForStDBOperation} tests. */
@@ -104,4 +109,45 @@ public class ForStDBOperationTestBase {
valueSerializerView,
valueDeserializerView);
}
+
+ static class TestStateFuture<T> implements InternalStateFuture<T> {
+
+ public CompletableFuture<T> future = new CompletableFuture<>();
+
+ @Override
+ public <U> StateFuture<U> thenApply(Function<? super T, ? extends U> fn) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public StateFuture<Void> thenAccept(Consumer<? super T> action) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public <U> StateFuture<U> thenCompose(
+ Function<? super T, ? extends StateFuture<U>> action) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public <U, V> StateFuture<V> thenCombine(
+ StateFuture<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void complete(T result) {
+ future.complete(result);
+ }
+
+ public T getCompletedResult() throws Exception {
+ return future.get();
+ }
+
+ @Override
+ public void thenSyncAccept(Consumer<? super T> action) {
+ throw new UnsupportedOperationException();
+ }
+ }
}
diff --git a/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStGeneralMultiGetOperationTest.java b/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStGeneralMultiGetOperationTest.java
index 3b7bdb39785..5b8263ec25e 100644
--- a/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStGeneralMultiGetOperationTest.java
+++ b/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStGeneralMultiGetOperationTest.java
@@ -18,6 +18,8 @@
package org.apache.flink.state.forst;
+import org.apache.flink.api.java.tuple.Tuple2;
+
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
@@ -25,7 +27,6 @@ import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
-import static org.apache.flink.state.forst.ForStGeneralMultiGetOperation.GetRequest;
import static org.assertj.core.api.Assertions.assertThat;
/** Unit test for {@link ForStGeneralMultiGetOperation}. */
@@ -35,30 +36,35 @@ public class ForStGeneralMultiGetOperationTest extends ForStDBOperationTestBase
public void testValueStateMultiGet() throws Exception {
ForStValueState<Integer, String> valueState1 = buildForStValueState("test-multiGet-1");
ForStValueState<Integer, String> valueState2 = buildForStValueState("test-multiGet-2");
- List<GetRequest<ContextKey<Integer>, String>> batchGetRequest = new ArrayList<>();
- List<String> resultCheckList = new ArrayList<>();
+ List<ForStDBGetRequest<?, ?>> batchGetRequest = new ArrayList<>();
+ List<Tuple2<String, TestStateFuture<String>>> resultCheckList = new ArrayList<>();
int keyNum = 1000;
for (int i = 0; i < keyNum; i++) {
- GetRequest<ContextKey<Integer>, String> request =
- GetRequest.of(buildContextKey(i), ((i % 2 == 0) ? valueState1 : valueState2));
+ TestStateFuture<String> future = new TestStateFuture<>();
+ ForStValueState<Integer, String> table = ((i % 2 == 0) ? valueState1 : valueState2);
+ ForStDBGetRequest<ContextKey<Integer>, String> request =
+ ForStDBGetRequest.of(buildContextKey(i), table, future);
batchGetRequest.add(request);
+
String value = (i % 10 != 0 ? String.valueOf(i) : null);
- resultCheckList.add(value);
+ resultCheckList.add(Tuple2.of(value, future));
if (value == null) {
continue;
}
- byte[] keyBytes = request.table.serializeKey(request.key);
- byte[] valueBytes = request.table.serializeValue(value);
- db.put(request.table.getColumnFamilyHandle(), keyBytes, valueBytes);
+ byte[] keyBytes = request.buildSerializedKey();
+ byte[] valueBytes = table.serializeValue(value);
+ db.put(request.getColumnFamilyHandle(), keyBytes, valueBytes);
}
ExecutorService executor = Executors.newFixedThreadPool(4);
- ForStGeneralMultiGetOperation<ContextKey<Integer>, String> generalMultiGetOperation =
- new ForStGeneralMultiGetOperation<>(db, batchGetRequest, executor);
- List<String> result = generalMultiGetOperation.process().get();
+ ForStGeneralMultiGetOperation generalMultiGetOperation =
+ new ForStGeneralMultiGetOperation(db, batchGetRequest, executor);
+ generalMultiGetOperation.process().get();
- assertThat(result).isEqualTo(resultCheckList);
+ for (Tuple2<String, TestStateFuture<String>> tuple : resultCheckList) {
+ assertThat(tuple.f1.getCompletedResult()).isEqualTo(tuple.f0);
+ }
executor.shutdownNow();
}
diff --git a/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStWriteBatchOperationTest.java b/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStWriteBatchOperationTest.java
index 38abac4a709..19e11684fbf 100644
--- a/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStWriteBatchOperationTest.java
+++ b/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStWriteBatchOperationTest.java
@@ -26,8 +26,8 @@ import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
-import static org.apache.flink.state.forst.ForStWriteBatchOperation.PutRequest;
-import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/** Unit test for {@link ForStWriteBatchOperation}. */
public class ForStWriteBatchOperationTest extends ForStDBOperationTestBase {
@@ -36,65 +36,78 @@ public class ForStWriteBatchOperationTest extends ForStDBOperationTestBase {
public void testValueStateWriteBatch() throws Exception {
ForStValueState<Integer, String> valueState1 = buildForStValueState("test-write-batch-1");
ForStValueState<Integer, String> valueState2 = buildForStValueState("test-write-batch-2");
- List<PutRequest<ContextKey<Integer>, String>> batchPutRequest = new ArrayList<>();
+ List<ForStDBPutRequest<?, ?>> batchPutRequest = new ArrayList<>();
int keyNum = 100;
for (int i = 0; i < keyNum; i++) {
batchPutRequest.add(
- PutRequest.of(
+ ForStDBPutRequest.of(
buildContextKey(i),
String.valueOf(i),
- ((i % 2 == 0) ? valueState1 : valueState2)));
+ ((i % 2 == 0) ? valueState1 : valueState2),
+ new TestStateFuture<>()));
}
ExecutorService executor = Executors.newFixedThreadPool(2);
- ForStWriteBatchOperation<ContextKey<Integer>, String> writeBatchOperation =
- new ForStWriteBatchOperation<>(db, batchPutRequest, new WriteOptions(), executor);
+ ForStWriteBatchOperation writeBatchOperation =
+ new ForStWriteBatchOperation(db, batchPutRequest, new WriteOptions(), executor);
writeBatchOperation.process().get();
// check data correctness
- for (PutRequest<ContextKey<Integer>, String> request : batchPutRequest) {
- ForStInnerTable<ContextKey<Integer>, String> table = request.table;
- byte[] keyBytes = table.serializeKey(request.key);
- byte[] valueBytes = db.get(table.getColumnFamilyHandle(), keyBytes);
- assertThat(table.deserializeValue(valueBytes)).isEqualTo(request.value);
+ for (ForStDBPutRequest<?, ?> request : batchPutRequest) {
+ byte[] keyBytes = request.buildSerializedKey();
+ byte[] valueBytes = db.get(request.getColumnFamilyHandle(), keyBytes);
+ assertArrayEquals(valueBytes, request.buildSerializedValue());
}
}
@Test
public void testWriteBatchWithNullValue() throws Exception {
ForStValueState<Integer, String> valueState = buildForStValueState("test-write-batch");
- List<PutRequest<ContextKey<Integer>, String>> batchPutRequest = new ArrayList<>();
+ List<ForStDBPutRequest<?, ?>> batchPutRequest = new ArrayList<>();
// 1. write some data without null value
int keyNum = 100;
for (int i = 0; i < keyNum; i++) {
- batchPutRequest.add(PutRequest.of(buildContextKey(i), String.valueOf(i), valueState));
+ batchPutRequest.add(
+ ForStDBPutRequest.of(
+ buildContextKey(i),
+ String.valueOf(i),
+ valueState,
+ new TestStateFuture<>()));
}
ExecutorService executor = Executors.newFixedThreadPool(2);
- ForStWriteBatchOperation<ContextKey<Integer>, String> writeBatchOperation =
- new ForStWriteBatchOperation<>(db, batchPutRequest, new WriteOptions(), executor);
+ ForStWriteBatchOperation writeBatchOperation =
+ new ForStWriteBatchOperation(db, batchPutRequest, new WriteOptions(), executor);
writeBatchOperation.process().get();
// 2. update data with null value
batchPutRequest.clear();
for (int i = 0; i < keyNum; i++) {
if (i % 8 == 0) {
- batchPutRequest.add(PutRequest.of(buildContextKey(i), null, valueState));
+ batchPutRequest.add(
+ ForStDBPutRequest.of(
+ buildContextKey(i), null, valueState, new TestStateFuture<>()));
} else {
batchPutRequest.add(
- PutRequest.of(buildContextKey(i), String.valueOf(i * 2), valueState));
+ ForStDBPutRequest.of(
+ buildContextKey(i),
+ String.valueOf(i * 2),
+ valueState,
+ new TestStateFuture<>()));
}
}
- ForStWriteBatchOperation<ContextKey<Integer>, String> writeBatchOperation2 =
- new ForStWriteBatchOperation<>(db, batchPutRequest, new WriteOptions(), executor);
+ ForStWriteBatchOperation writeBatchOperation2 =
+ new ForStWriteBatchOperation(db, batchPutRequest, new WriteOptions(), executor);
writeBatchOperation2.process().get();
// 3. check data correctness
- for (PutRequest<ContextKey<Integer>, String> request : batchPutRequest) {
- ForStInnerTable<ContextKey<Integer>, String> table = request.table;
- byte[] keyBytes = table.serializeKey(request.key);
- byte[] valueBytes = db.get(table.getColumnFamilyHandle(), keyBytes);
- String value = (valueBytes == null) ? null : table.deserializeValue(valueBytes);
- assertThat(value).isEqualTo(request.value);
+ for (ForStDBPutRequest<?, ?> request : batchPutRequest) {
+ byte[] keyBytes = request.buildSerializedKey();
+ byte[] valueBytes = db.get(request.getColumnFamilyHandle(), keyBytes);
+ if (valueBytes == null) {
+ assertTrue(request.valueIsNull());
+ } else {
+ assertArrayEquals(valueBytes, request.buildSerializedValue());
+ }
}
}
}