You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by tk...@apache.org on 2023/02/24 08:07:25 UTC
[ignite-3] branch main updated: IGNITE-18565 Modify getOrCreateMvPartition and getMvPartition of MvTableStorage to return the future (#1666)
This is an automated email from the ASF dual-hosted git repository.
tkalkirill pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git
The following commit(s) were added to refs/heads/main by this push:
new cbe2eb2842 IGNITE-18565 Modify getOrCreateMvPartition and getMvPartition of MvTableStorage to return the future (#1666)
cbe2eb2842 is described below
commit cbe2eb284221234648e49355b00fc76fb69ec13d
Author: Kirill Tkalenko <tk...@yandex.ru>
AuthorDate: Fri Feb 24 11:07:19 2023 +0300
IGNITE-18565 Modify getOrCreateMvPartition and getMvPartition of MvTableStorage to return the future (#1666)
---
.../ignite/internal/storage/StorageException.java | 13 +
.../storage/StorageRebalanceException.java | 13 +
.../internal/storage/engine/MvTableStorage.java | 10 +-
.../internal/storage/util/MvPartitionStorages.java | 503 +++++++++++++++++
.../internal/storage/util/StorageOperation.java | 90 +++
.../internal/storage/TestMvTableStorageTest.java | 2 +-
.../storage/util/MvPartitionStoragesTest.java | 609 +++++++++++++++++++++
.../storage/AbstractMvTableStorageTest.java | 70 ++-
.../storage/BaseMvPartitionStorageTest.java | 2 +-
.../internal/storage/BaseMvStoragesTest.java | 20 +
.../storage/impl/TestMvPartitionStorage.java | 14 +-
.../internal/storage/impl/TestMvTableStorage.java | 206 ++-----
.../index/AbstractHashIndexStorageTest.java | 3 +-
.../index/AbstractSortedIndexStorageTest.java | 3 +-
.../pagememory/AbstractPageMemoryTableStorage.java | 249 ++-------
.../PersistentPageMemoryTableStorage.java | 23 -
.../VolatilePageMemoryMvTableStorageTest.java | 12 +-
.../storage/rocksdb/RocksDbTableStorage.java | 236 ++------
.../storage/rocksdb/RocksDbMvTableStorageTest.java | 8 +-
.../storage/rocksdb/RocksDbStorageEngineTest.java | 5 +-
.../ignite/distributed/ItTablePersistenceTest.java | 19 +-
.../internal/table/distributed/TableManager.java | 16 +-
.../raft/snapshot/PartitionAccessImplTest.java | 20 +-
.../incoming/IncomingSnapshotCopierTest.java | 8 +-
24 files changed, 1529 insertions(+), 625 deletions(-)
diff --git a/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/StorageException.java b/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/StorageException.java
index 58ddbcecfe..c70d17c08d 100644
--- a/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/StorageException.java
+++ b/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/StorageException.java
@@ -89,6 +89,19 @@ public class StorageException extends IgniteInternalException {
this(IgniteStringFormatter.format(messagePattern, params), cause);
}
+ /**
+ * Constructor.
+ *
+ * @param code Full error code.
+ * @param messagePattern Error message pattern.
+ * @param cause Non-null throwable cause.
+ * @param params Error message params.
+ * @see IgniteStringFormatter#format(String, Object...)
+ */
+ public StorageException(int code, String messagePattern, Throwable cause, Object... params) {
+ this(code, IgniteStringFormatter.format(messagePattern, params), cause);
+ }
+
/**
* Constructor.
*
diff --git a/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/StorageRebalanceException.java b/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/StorageRebalanceException.java
index c18ecb25f7..85958709fe 100644
--- a/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/StorageRebalanceException.java
+++ b/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/StorageRebalanceException.java
@@ -18,6 +18,7 @@
package org.apache.ignite.internal.storage;
import org.apache.ignite.lang.ErrorGroups.Storage;
+import org.apache.ignite.lang.IgniteStringFormatter;
import org.jetbrains.annotations.Nullable;
/**
@@ -51,4 +52,16 @@ public class StorageRebalanceException extends StorageException {
public StorageRebalanceException(String message, @Nullable Throwable cause) {
super(Storage.STORAGE_REBALANCE_ERR, message, cause);
}
+
+ /**
+ * Constructor.
+ *
+ * @param messagePattern Error message pattern.
+ * @param cause Non-null throwable cause.
+ * @param params Error message params.
+ * @see IgniteStringFormatter#format(String, Object...)
+ */
+ public StorageRebalanceException(String messagePattern, Throwable cause, Object... params) {
+ super(Storage.STORAGE_REBALANCE_ERR, messagePattern, cause, params);
+ }
}
diff --git a/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/engine/MvTableStorage.java b/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/engine/MvTableStorage.java
index 341623ae47..7770cbd394 100644
--- a/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/engine/MvTableStorage.java
+++ b/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/engine/MvTableStorage.java
@@ -46,14 +46,18 @@ import org.jetbrains.annotations.Nullable;
*/
public interface MvTableStorage extends ManuallyCloseable {
/**
- * Retrieves or creates a partition for the current table. Not expected to be called concurrently with the same Partition ID.
+ * Creates a partition for the current table.
+ *
+ * <p>If the partition has already been created or is in the process of being created, a {@link StorageException} will be thrown.
+ *
+ * <p>If the partition is in the process of being destroyed, it will be re-created after the destruction is completed.
*
* @param partitionId Partition ID.
- * @return Partition storage.
+ * @return Future that will be completed when the partition creation completes.
* @throws IllegalArgumentException If Partition ID is out of configured bounds.
* @throws StorageException If an error has occurred during the partition creation.
*/
- MvPartitionStorage getOrCreateMvPartition(int partitionId) throws StorageException;
+ CompletableFuture<MvPartitionStorage> createMvPartition(int partitionId);
/**
* Returns the partition storage or {@code null} if the requested storage doesn't exist.
diff --git a/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/util/MvPartitionStorages.java b/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/util/MvPartitionStorages.java
new file mode 100644
index 0000000000..8f8ce9fcbe
--- /dev/null
+++ b/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/util/MvPartitionStorages.java
@@ -0,0 +1,503 @@
+/*
+ * 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.ignite.internal.storage.util;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static java.util.function.Function.identity;
+import static java.util.stream.Collectors.toList;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicReferenceArray;
+import java.util.function.Function;
+import java.util.function.IntFunction;
+import java.util.stream.IntStream;
+import org.apache.ignite.internal.schema.configuration.TableView;
+import org.apache.ignite.internal.storage.MvPartitionStorage;
+import org.apache.ignite.internal.storage.StorageException;
+import org.apache.ignite.internal.storage.StorageRebalanceException;
+import org.apache.ignite.internal.storage.util.StorageOperation.AbortRebalanceStorageOperation;
+import org.apache.ignite.internal.storage.util.StorageOperation.CleanupStorageOperation;
+import org.apache.ignite.internal.storage.util.StorageOperation.CreateStorageOperation;
+import org.apache.ignite.internal.storage.util.StorageOperation.DestroyStorageOperation;
+import org.apache.ignite.internal.storage.util.StorageOperation.FinishRebalanceStorageOperation;
+import org.apache.ignite.internal.storage.util.StorageOperation.StartRebalanceStorageOperation;
+import org.apache.ignite.lang.IgniteStringFormatter;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Class for storing stores and performing operations on them.
+ */
+public class MvPartitionStorages<T extends MvPartitionStorage> {
+ private final TableView tableView;
+
+ private final AtomicReferenceArray<T> storageByPartitionId;
+
+ private final ConcurrentMap<Integer, StorageOperation> operationByPartitionId = new ConcurrentHashMap<>();
+
+ private final ConcurrentMap<Integer, CompletableFuture<Void>> rebalaceFutureByPartitionId = new ConcurrentHashMap<>();
+
+ /**
+ * Constructor.
+ *
+ * @param tableView Table configuration.
+ */
+ public MvPartitionStorages(TableView tableView) {
+ this.tableView = tableView;
+
+ storageByPartitionId = new AtomicReferenceArray<>(tableView.partitions());
+ }
+
+ /**
+ * Returns the multi-versioned partition storage, {@code null} if the storage does not exist (not created or destroyed).
+ *
+ * @throws IllegalArgumentException If partition ID is out of configured bounds.
+ */
+ public @Nullable T get(int partitionId) {
+ checkPartitionId(partitionId);
+
+ return storageByPartitionId.get(partitionId);
+ }
+
+ /**
+ * Creates and adds a new multi-versioned partition storage, if the storage is in the process of being destroyed, it will be recreated
+ * after the destruction.
+ *
+ * @param partitionId Partition ID.
+ * @param createStorageFunction Storage creation function, the argument is the partition ID.
+ * @return Future of creating a multi-versioned partition storage.
+ * @throws IllegalArgumentException If partition ID is out of configured bounds.
+ * @throws StorageException If the storage already exists or another operation is already in progress.
+ * @throws StorageException If the creation of the storage after its destruction is already planned.
+ */
+ public CompletableFuture<MvPartitionStorage> create(int partitionId, IntFunction<T> createStorageFunction) {
+ StorageOperation storageOperation = operationByPartitionId.compute(partitionId, (partId, operation) -> {
+ if (operation instanceof DestroyStorageOperation) {
+ if (!((DestroyStorageOperation) operation).setCreationOperation(new CreateStorageOperation())) {
+ throw new StorageException(
+ "Creation of the storage after its destruction is already planned: [" + createStorageInfo(partitionId) + ']'
+ );
+ }
+
+ return operation;
+ }
+
+ if (get(partitionId) != null) {
+ throw new StorageException("Storage already exists: [" + createStorageInfo(partitionId) + ']');
+ }
+
+ if (operation != null) {
+ throwExceptionDependingOnOperation(operation, partitionId);
+ }
+
+ return new CreateStorageOperation();
+ });
+
+ CompletableFuture<Void> destroyStorageFuture = storageOperation instanceof DestroyStorageOperation
+ ? ((DestroyStorageOperation) storageOperation).getDestroyFuture()
+ : completedFuture(null);
+
+ return destroyStorageFuture.thenApply(unused -> {
+ T newStorage = createStorageFunction.apply(partitionId);
+
+ boolean set = storageByPartitionId.compareAndSet(partitionId, null, newStorage);
+
+ assert set : createStorageInfo(partitionId);
+
+ return (MvPartitionStorage) newStorage;
+ }).whenComplete((storage, throwable) -> operationByPartitionId.compute(partitionId, (partId, operation) -> {
+ assert operation instanceof CreateStorageOperation : createStorageInfo(partitionId) + ", op=" + operation;
+
+ return null;
+ }));
+ }
+
+ /**
+ * Destroys a multi-versioned partition storage.
+ *
+ * @param partitionId Partition ID.
+ * @param destroyStorageFunction Partition destruction function.
+ * @return Future of multi-versioned partition storage destruction.
+ * @throws IllegalArgumentException If partition ID is out of configured bounds.
+ * @throws StorageException If the storage does not exist or another operation is already in progress.
+ * @throws StorageRebalanceException If the storage is in the process of rebalancing.
+ */
+ public CompletableFuture<Void> destroy(int partitionId, Function<T, CompletableFuture<Void>> destroyStorageFunction) {
+ DestroyStorageOperation destroyOp = (DestroyStorageOperation) operationByPartitionId.compute(partitionId, (partId, operation) -> {
+ checkStorageExists(partitionId);
+
+ if (operation != null) {
+ throwExceptionDependingOnOperation(operation, partitionId);
+ }
+
+ return new DestroyStorageOperation();
+ });
+
+ return completedFuture(null)
+ .thenCompose(unused -> destroyStorageFunction.apply(storageByPartitionId.getAndSet(partitionId, null)))
+ .whenComplete((unused, throwable) -> {
+ operationByPartitionId.compute(partitionId, (partId, operation) -> {
+ assert operation instanceof DestroyStorageOperation : createStorageInfo(partitionId) + ", op=" + operation;
+
+ DestroyStorageOperation destroyStorageOperation = (DestroyStorageOperation) operation;
+
+ return destroyStorageOperation.getCreateStorageOperation();
+ });
+
+ if (throwable == null) {
+ destroyOp.getDestroyFuture().complete(null);
+ } else {
+ destroyOp.getDestroyFuture().completeExceptionally(throwable);
+ }
+ });
+ }
+
+ /**
+ * Clears a multi-versioned partition storage.
+ *
+ * @param partitionId Partition ID.
+ * @param clearStorageFunction Partition clean up function.
+ * @return Future of cleaning a multi-versioned partition storage.
+ * @throws IllegalArgumentException If partition ID is out of configured bounds.
+ * @throws StorageException If the storage does not exist or another operation is already in progress.
+ * @throws StorageRebalanceException If the storage is in the process of rebalancing.
+ */
+ public CompletableFuture<Void> clear(int partitionId, Function<T, CompletableFuture<Void>> clearStorageFunction) {
+ operationByPartitionId.compute(partitionId, (partId, operation) -> {
+ checkStorageExists(partitionId);
+
+ if (operation != null) {
+ throwExceptionDependingOnOperation(operation, partitionId);
+ }
+
+ return new CleanupStorageOperation();
+ });
+
+ return completedFuture(null)
+ .thenCompose(unused -> clearStorageFunction.apply(get(partitionId)))
+ .whenComplete((unused, throwable) ->
+ operationByPartitionId.compute(partitionId, (partId, operation) -> {
+ assert operation instanceof CleanupStorageOperation : createStorageInfo(partitionId) + ", op=" + operation;
+
+ return null;
+ })
+ );
+ }
+
+ /**
+ * Starts a multi-versioned partition storage rebalance.
+ *
+ * @param partitionId Partition ID.
+ * @param startRebalanceStorageFunction Partition start rebalance function.
+ * @return Future of starting rebalance a multi-versioned partition storage.
+ * @throws IllegalArgumentException If partition ID is out of configured bounds.
+ * @throws StorageRebalanceException If the storage does not exist or another operation is already in progress.
+ * @throws StorageRebalanceException If rebalancing is already in progress.
+ */
+ public CompletableFuture<Void> startRebalace(int partitionId, Function<T, CompletableFuture<Void>> startRebalanceStorageFunction) {
+ operationByPartitionId.compute(partitionId, (partId, operation) -> {
+ checkStorageExistsForRebalance(partitionId);
+
+ if (operation != null) {
+ throwExceptionDependingOnOperationForRebalance(operation, partitionId);
+ }
+
+ if (rebalaceFutureByPartitionId.containsKey(partitionId)) {
+ throw new StorageRebalanceException(createStorageInProgressOfRebalanceErrorMessage(partitionId));
+ }
+
+ return new StartRebalanceStorageOperation();
+ });
+
+ return completedFuture(null)
+ .thenCompose(unused -> {
+ CompletableFuture<Void> startRebalanceFuture = startRebalanceStorageFunction.apply(get(partitionId));
+
+ CompletableFuture<Void> old = rebalaceFutureByPartitionId.put(partitionId, startRebalanceFuture);
+
+ assert old == null : createStorageInfo(partitionId);
+
+ return startRebalanceFuture;
+ }).whenComplete((unused, throwable) ->
+ operationByPartitionId.compute(partitionId, (partId, operation) -> {
+ assert operation instanceof StartRebalanceStorageOperation :
+ createStorageInfo(partitionId) + ", op=" + operation;
+
+ return null;
+ })
+ );
+ }
+
+ /**
+ * Aborts a multi-versioned partition storage rebalance if started (successful or not).
+ *
+ * @param partitionId Partition ID.
+ * @param abortRebalanceStorageFunction Partition abort rebalance function.
+ * @return Future of aborting rebalance a multi-versioned partition storage.
+ * @throws IllegalArgumentException If partition ID is out of configured bounds.
+ * @throws StorageRebalanceException If the storage does not exist or another operation is already in progress.
+ */
+ public CompletableFuture<Void> abortRebalance(int partitionId, Function<T, CompletableFuture<Void>> abortRebalanceStorageFunction) {
+ operationByPartitionId.compute(partitionId, (partId, operation) -> {
+ checkStorageExistsForRebalance(partitionId);
+
+ if (operation != null) {
+ throwExceptionDependingOnOperationForRebalance(operation, partitionId);
+ }
+
+ return new AbortRebalanceStorageOperation();
+ });
+
+ return completedFuture(null)
+ .thenCompose(unused -> {
+ CompletableFuture<Void> rebalanceFuture = rebalaceFutureByPartitionId.remove(partitionId);
+
+ if (rebalanceFuture == null) {
+ return completedFuture(null);
+ }
+
+ return rebalanceFuture
+ .handle((unused1, throwable) -> abortRebalanceStorageFunction.apply(get(partitionId)))
+ .thenCompose(identity());
+ }).whenComplete((unused, throwable) ->
+ operationByPartitionId.compute(partitionId, (partId, operation) -> {
+ assert operation instanceof AbortRebalanceStorageOperation :
+ createStorageInfo(partitionId) + ", op=" + operation;
+
+ return null;
+ })
+ );
+ }
+
+ /**
+ * Finishes a successful started multi-versioned partition storage rebalance.
+ *
+ * @param partitionId Partition ID.
+ * @param finishRebalanceStorageFunction Partition finish rebalance function, the argument is the partition ID.
+ * @return Future of aborting rebalance a multi-versioned partition storage.
+ * @throws IllegalArgumentException If partition ID is out of configured bounds.
+ * @throws StorageRebalanceException If the storage does not exist or another operation is already in progress.
+ * @throws StorageRebalanceException If storage rebalancing has not started.
+ */
+ public CompletableFuture<Void> finishRebalance(int partitionId, Function<T, CompletableFuture<Void>> finishRebalanceStorageFunction) {
+ operationByPartitionId.compute(partitionId, (partId, operation) -> {
+ checkStorageExistsForRebalance(partitionId);
+
+ if (operation != null) {
+ throwExceptionDependingOnOperationForRebalance(operation, partitionId);
+ }
+
+ if (!rebalaceFutureByPartitionId.containsKey(partitionId)) {
+ throw new StorageRebalanceException("Storage rebalancing did not start: [" + createStorageInfo(partitionId) + ']');
+ }
+
+ return new FinishRebalanceStorageOperation();
+ });
+
+ return completedFuture(null)
+ .thenCompose(unused -> {
+ CompletableFuture<Void> rebalanceFuture = rebalaceFutureByPartitionId.remove(partitionId);
+
+ assert rebalanceFuture != null : createStorageInfo(partitionId);
+
+ return rebalanceFuture.thenCompose(unused1 -> finishRebalanceStorageFunction.apply(get(partitionId)));
+ }).whenComplete((unused, throwable) ->
+ operationByPartitionId.compute(partitionId, (partId, operation) -> {
+ assert operation instanceof FinishRebalanceStorageOperation :
+ createStorageInfo(partitionId) + ", op=" + operation;
+
+ return null;
+ })
+ );
+ }
+
+ /**
+ * Collects all multi-versioned partition storages to close.
+ */
+ // TODO: IGNITE-18529 We need to wait for all current operations and disable new ones
+ public List<T> getAllForClose() {
+ return IntStream.range(0, storageByPartitionId.length())
+ .mapToObj(partitionId -> storageByPartitionId.getAndSet(partitionId, null))
+ .filter(Objects::nonNull)
+ .collect(toList());
+ }
+
+ /**
+ * Destroys all created multi-versioned partition storages.
+ *
+ * @param destroyStorageFunction Partition destruction function.
+ * @return Future destruction of all created multi-versioned partition storages.
+ */
+ // TODO: IGNITE-18529 We need to deal with parallel operations
+ public CompletableFuture<Void> destroyAll(Function<T, CompletableFuture<Void>> destroyStorageFunction) {
+ List<CompletableFuture<Void>> destroyFutures = new ArrayList<>();
+
+ for (int partitionId = 0; partitionId < storageByPartitionId.length(); partitionId++) {
+ StorageOperation storageOperation = operationByPartitionId.get(partitionId);
+
+ if (storageOperation instanceof DestroyStorageOperation) {
+ destroyFutures.add(((DestroyStorageOperation) storageOperation).getDestroyFuture());
+ } else {
+ T storage = storageByPartitionId.getAndSet(partitionId, null);
+
+ if (storage != null) {
+ destroyFutures.add(destroyStorageFunction.apply(storage));
+ }
+ }
+ }
+
+ return CompletableFuture.allOf(destroyFutures.toArray(CompletableFuture[]::new));
+ }
+
+ /**
+ * Returns table name.
+ */
+ public String getTableName() {
+ return tableView.name();
+ }
+
+ /**
+ * Creates a short info of the multi-versioned partition storage in the format "table=user, partitionId=1".
+ *
+ * @param partitionId Partition ID.
+ */
+ public String createStorageInfo(int partitionId) {
+ return IgniteStringFormatter.format("table={}, partitionId={}", getTableName(), partitionId);
+ }
+
+ /**
+ * Checks that the partition ID is within the scope of the configuration.
+ *
+ * @param partitionId Partition ID.
+ * @throws IllegalArgumentException If partition ID is out of configured bounds.
+ */
+ private void checkPartitionId(int partitionId) {
+ int partitions = storageByPartitionId.length();
+
+ if (partitionId < 0 || partitionId >= partitions) {
+ throw new IllegalArgumentException(IgniteStringFormatter.format(
+ "Unable to access partition with id outside of configured range: [table={}, partitionId={}, partitions={}]",
+ getTableName(),
+ partitionId,
+ partitions
+ ));
+ }
+ }
+
+ /**
+ * Checks if the storage exists.
+ *
+ * @param partitionId Partition ID.
+ * @throws IllegalArgumentException If partition ID is out of configured bounds.
+ * @throws StorageException If the storage does not exist.
+ */
+ private void checkStorageExists(int partitionId) {
+ if (get(partitionId) == null) {
+ throw new StorageException(createStorageDoesNotExistErrorMessage(partitionId));
+ }
+ }
+
+ /**
+ * Checks if the storage exists.
+ *
+ * @param partitionId Partition ID.
+ * @throws IllegalArgumentException If partition ID is out of configured bounds.
+ * @throws StorageRebalanceException If the storage does not exist.
+ */
+ private void checkStorageExistsForRebalance(int partitionId) {
+ if (get(partitionId) == null) {
+ throw new StorageRebalanceException(createStorageDoesNotExistErrorMessage(partitionId));
+ }
+ }
+
+ private void throwExceptionDependingOnOperation(StorageOperation operation, int partitionId) {
+ if (operation instanceof CreateStorageOperation) {
+ throw new StorageException(createStorageInProgressOfCreationErrorMessage(partitionId));
+ } else if (operation instanceof DestroyStorageOperation) {
+ throw new StorageException(createStorageInProgressOfDestructionErrorMessage(partitionId));
+ } else if (operation instanceof StartRebalanceStorageOperation) {
+ throw new StorageRebalanceException(createStorageInProgressOfStartRebalanceErrorMessage(partitionId));
+ } else if (operation instanceof AbortRebalanceStorageOperation) {
+ throw new StorageRebalanceException(createStorageInProgressOfAbortRebalanceErrorMessage(partitionId));
+ } else if (operation instanceof FinishRebalanceStorageOperation) {
+ throw new StorageRebalanceException(createStorageInProgressOfFinishRebalanceErrorMessage(partitionId));
+ } else if (operation instanceof CleanupStorageOperation) {
+ throw new StorageException(createStorageInProgressOfCleanupErrorMessage(partitionId));
+ } else {
+ throw new StorageException(createUnknownOperationErrorMessage(partitionId, operation));
+ }
+ }
+
+ private void throwExceptionDependingOnOperationForRebalance(StorageOperation operation, int partitionId) {
+ if (operation instanceof CreateStorageOperation) {
+ throw new StorageRebalanceException(createStorageInProgressOfCreationErrorMessage(partitionId));
+ } else if (operation instanceof DestroyStorageOperation) {
+ throw new StorageRebalanceException(createStorageInProgressOfDestructionErrorMessage(partitionId));
+ } else if (operation instanceof StartRebalanceStorageOperation) {
+ throw new StorageRebalanceException(createStorageInProgressOfStartRebalanceErrorMessage(partitionId));
+ } else if (operation instanceof AbortRebalanceStorageOperation) {
+ throw new StorageRebalanceException(createStorageInProgressOfAbortRebalanceErrorMessage(partitionId));
+ } else if (operation instanceof FinishRebalanceStorageOperation) {
+ throw new StorageRebalanceException(createStorageInProgressOfFinishRebalanceErrorMessage(partitionId));
+ } else if (operation instanceof CleanupStorageOperation) {
+ throw new StorageRebalanceException(createStorageInProgressOfCleanupErrorMessage(partitionId));
+ } else {
+ throw new StorageRebalanceException(createUnknownOperationErrorMessage(partitionId, operation));
+ }
+ }
+
+ private String createStorageDoesNotExistErrorMessage(int partitionId) {
+ return "Storage does not exist: [" + createStorageInfo(partitionId) + ']';
+ }
+
+ private String createStorageInProgressOfCreationErrorMessage(int partitionId) {
+ return "Storage is in process of being created: [" + createStorageInfo(partitionId) + ']';
+ }
+
+ private String createStorageInProgressOfDestructionErrorMessage(int partitionId) {
+ return "Storage is already in process of being destroyed: [" + createStorageInfo(partitionId) + ']';
+ }
+
+ private String createStorageInProgressOfStartRebalanceErrorMessage(int partitionId) {
+ return "Storage in the process of starting a rebalance: [" + createStorageInfo(partitionId) + ']';
+ }
+
+ private String createStorageInProgressOfAbortRebalanceErrorMessage(int partitionId) {
+ return "Storage in the process of aborting a rebalance: [" + createStorageInfo(partitionId) + ']';
+ }
+
+ private String createStorageInProgressOfFinishRebalanceErrorMessage(int partitionId) {
+ return "Storage in the process of finishing a rebalance: [" + createStorageInfo(partitionId) + ']';
+ }
+
+ private String createStorageInProgressOfRebalanceErrorMessage(int partitionId) {
+ return "Storage in the process of rebalance: [" + createStorageInfo(partitionId) + ']';
+ }
+
+ private String createStorageInProgressOfCleanupErrorMessage(int partitionId) {
+ return "Storage is in process of being cleaned up: [" + createStorageInfo(partitionId) + ']';
+ }
+
+ private String createUnknownOperationErrorMessage(int partitionId, StorageOperation operation) {
+ return "Unknown operation: [" + createStorageInfo(partitionId) + ", operation=" + operation + ']';
+ }
+}
diff --git a/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/util/StorageOperation.java b/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/util/StorageOperation.java
new file mode 100644
index 0000000000..09b8156cde
--- /dev/null
+++ b/modules/storage-api/src/main/java/org/apache/ignite/internal/storage/util/StorageOperation.java
@@ -0,0 +1,90 @@
+/*
+ * 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.ignite.internal.storage.util;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Storage operations.
+ */
+interface StorageOperation {
+ /**
+ * Storage creation operation.
+ */
+ class CreateStorageOperation implements StorageOperation {
+ }
+
+ /**
+ * Storage destruction operation.
+ */
+ class DestroyStorageOperation implements StorageOperation {
+ private final CompletableFuture<Void> destroyFuture = new CompletableFuture<>();
+
+ private final AtomicReference<CreateStorageOperation> createStorageOperationReference = new AtomicReference<>();
+
+ /**
+ * Attempts to set the storage creation operation.
+ *
+ * @param createStorageOperation Storage creation operation.
+ * @return {@code True} if the operation was set by current method invocation, {@code false} if by another method invocation.
+ */
+ public boolean setCreationOperation(CreateStorageOperation createStorageOperation) {
+ return createStorageOperationReference.compareAndSet(null, createStorageOperation);
+ }
+
+ /**
+ * Returns {@link #setCreationOperation(CreateStorageOperation) set} a storage creation operation.
+ */
+ public @Nullable CreateStorageOperation getCreateStorageOperation() {
+ return createStorageOperationReference.get();
+ }
+
+ /**
+ * Returns the storage destruction future.
+ */
+ public CompletableFuture<Void> getDestroyFuture() {
+ return destroyFuture;
+ }
+ }
+
+ /**
+ * Storage rebalancing start operation.
+ */
+ class StartRebalanceStorageOperation implements StorageOperation {
+ }
+
+ /**
+ * Storage rebalancing abort operation.
+ */
+ class AbortRebalanceStorageOperation implements StorageOperation {
+ }
+
+ /**
+ * Storage rebalancing finish operation.
+ */
+ class FinishRebalanceStorageOperation implements StorageOperation {
+ }
+
+ /**
+ * Storage cleanup operation.
+ */
+ class CleanupStorageOperation implements StorageOperation {
+ }
+}
diff --git a/modules/storage-api/src/test/java/org/apache/ignite/internal/storage/TestMvTableStorageTest.java b/modules/storage-api/src/test/java/org/apache/ignite/internal/storage/TestMvTableStorageTest.java
index 3de0a3912e..25343558ec 100644
--- a/modules/storage-api/src/test/java/org/apache/ignite/internal/storage/TestMvTableStorageTest.java
+++ b/modules/storage-api/src/test/java/org/apache/ignite/internal/storage/TestMvTableStorageTest.java
@@ -32,7 +32,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
public class TestMvTableStorageTest extends AbstractMvTableStorageTest {
@BeforeEach
void setUp(
- @InjectConfiguration("mock.tables.foo.dataStorage.name = " + TestStorageEngine.ENGINE_NAME)
+ @InjectConfiguration("mock.tables.foo{ partitions = 512, dataStorage.name = " + TestStorageEngine.ENGINE_NAME + "}")
TablesConfiguration tablesConfig
) {
initialize(new TestStorageEngine(), tablesConfig);
diff --git a/modules/storage-api/src/test/java/org/apache/ignite/internal/storage/util/MvPartitionStoragesTest.java b/modules/storage-api/src/test/java/org/apache/ignite/internal/storage/util/MvPartitionStoragesTest.java
new file mode 100644
index 0000000000..aed8c7d2e5
--- /dev/null
+++ b/modules/storage-api/src/test/java/org/apache/ignite/internal/storage/util/MvPartitionStoragesTest.java
@@ -0,0 +1,609 @@
+/*
+ * 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.ignite.internal.storage.util;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static java.util.concurrent.CompletableFuture.failedFuture;
+import static org.apache.ignite.internal.testframework.IgniteTestUtils.runAsync;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willFailFast;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willTimeoutFast;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.containsString;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import org.apache.ignite.internal.configuration.testframework.InjectConfiguration;
+import org.apache.ignite.internal.schema.configuration.TableConfiguration;
+import org.apache.ignite.internal.storage.MvPartitionStorage;
+import org.apache.ignite.internal.storage.StorageException;
+import org.apache.ignite.internal.storage.StorageRebalanceException;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.function.Executable;
+
+/**
+ * Class for testing {@link MvPartitionStorages}.
+ */
+@ExtendWith(ConfigurationExtension.class)
+public class MvPartitionStoragesTest {
+ @InjectConfiguration
+ private TableConfiguration tableConfig;
+
+ private MvPartitionStorages<MvPartitionStorage> mvPartitionStorages;
+
+ @BeforeEach
+ void setUp() {
+ mvPartitionStorages = new MvPartitionStorages(tableConfig.value());
+ }
+
+ @Test
+ void testGet() {
+ assertThrows(IllegalArgumentException.class, () -> getMvStorage(getPartitionIdOutOfConfig()));
+
+ assertNull(getMvStorage(0));
+
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ assertNotNull(getMvStorage(0));
+
+ assertNull(getMvStorage(1));
+ }
+
+ @Test
+ void testCreate() {
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ CompletableFuture<Void> startCreateMvStorageFuture = new CompletableFuture<>();
+ CompletableFuture<Void> finishCreateMvStorageFuture = new CompletableFuture<>();
+
+ CompletableFuture<?> createMvStorageFuture = runAsync(() ->
+ assertThat(mvPartitionStorages.create(1, partId -> {
+ startCreateMvStorageFuture.complete(null);
+
+ assertThat(finishCreateMvStorageFuture, willCompleteSuccessfully());
+
+ return mock(MvPartitionStorage.class);
+ }), willCompleteSuccessfully())
+ );
+
+ assertThat(startCreateMvStorageFuture, willCompleteSuccessfully());
+
+ assertThrowsWithMessage(StorageException.class, () -> createMvStorage(1), "Storage is in process of being created");
+
+ finishCreateMvStorageFuture.complete(null);
+
+ assertThat(createMvStorageFuture, willCompleteSuccessfully());
+ }
+
+ @Test
+ void testCreateError() {
+ assertThrows(IllegalArgumentException.class, () -> createMvStorage(getPartitionIdOutOfConfig()));
+
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ assertThrowsWithMessage(StorageException.class, () -> createMvStorage(0), "Storage already exists");
+
+ // What if there is an error during the operation?
+
+ assertThat(mvPartitionStorages.create(2, partId -> {
+ throw new RuntimeException("from test");
+ }), willFailFast(RuntimeException.class));
+
+ assertNull(getMvStorage(2));
+ }
+
+ @Test
+ void testCreateDuringDestroy() {
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ MvPartitionStorage mvStorage = getMvStorage(0);
+
+ CompletableFuture<Void> startDestroyMvStorageFuture = new CompletableFuture<>();
+ CompletableFuture<Void> finishDestroyMvStorageFuture = new CompletableFuture<>();
+
+ CompletableFuture<?> destroyMvStorageFuture = runAsync(() ->
+ assertThat(mvPartitionStorages.destroy(0, mvStorage1 -> {
+ startDestroyMvStorageFuture.complete(null);
+
+ return finishDestroyMvStorageFuture;
+ }), willCompleteSuccessfully())
+ );
+
+ assertThat(startDestroyMvStorageFuture, willCompleteSuccessfully());
+
+ CompletableFuture<MvPartitionStorage> reCreateMvStorageFuture = createMvStorage(0);
+
+ assertThrowsWithMessage(StorageException.class, () -> createMvStorage(0),
+ "Creation of the storage after its destruction is already planned"
+ );
+
+ finishDestroyMvStorageFuture.complete(null);
+
+ assertThat(destroyMvStorageFuture, willCompleteSuccessfully());
+
+ assertThat(reCreateMvStorageFuture, willCompleteSuccessfully());
+
+ assertNotSame(mvStorage, getMvStorage(0));
+ }
+
+ @Test
+ void testCreateDuringDestroyError() {
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ // What if there is an error during the destroy?
+
+ CompletableFuture<Void> startErrorDestroyMvStorageFuture = new CompletableFuture<>();
+ CompletableFuture<Void> finishErrorDestroyMvStorageFuture = new CompletableFuture<>();
+
+ CompletableFuture<?> errorDestroyMvStorageFuture = runAsync(() ->
+ assertThat(mvPartitionStorages.destroy(0, mvStorage1 -> {
+ startErrorDestroyMvStorageFuture.complete(null);
+
+ return finishErrorDestroyMvStorageFuture;
+ }), willCompleteSuccessfully())
+ );
+
+ assertThat(startErrorDestroyMvStorageFuture, willCompleteSuccessfully());
+
+ CompletableFuture<MvPartitionStorage> errorReCreateMvStorageFuture = createMvStorage(0);
+
+ finishErrorDestroyMvStorageFuture.completeExceptionally(new RuntimeException("from test"));
+
+ assertThat(errorDestroyMvStorageFuture, willFailFast(RuntimeException.class));
+ assertThat(errorReCreateMvStorageFuture, willFailFast(RuntimeException.class));
+
+ assertNull(getMvStorage(0));
+ }
+
+ @Test
+ void testCreateAfterDestroy() {
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ MvPartitionStorage mvStorage = getMvStorage(0);
+
+ assertThat(destroyMvStorage(0), willCompleteSuccessfully());
+
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ assertNotSame(mvStorage, getMvStorage(0));
+ }
+
+ @Test
+ void testDestroy() {
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ CompletableFuture<Void> startDestroyMvStorageFuture = new CompletableFuture<>();
+ CompletableFuture<Void> finishDestroyMvStorageFuture = new CompletableFuture<>();
+
+ CompletableFuture<?> destroyMvStorageFuture = runAsync(() ->
+ assertThat(mvPartitionStorages.destroy(0, mvStorage -> {
+ startDestroyMvStorageFuture.complete(null);
+
+ return finishDestroyMvStorageFuture;
+ }), willCompleteSuccessfully())
+ );
+
+ assertThat(startDestroyMvStorageFuture, willCompleteSuccessfully());
+
+ assertThrowsWithMessage(StorageException.class, () -> destroyMvStorage(0), "Storage does not exist");
+ assertThrowsWithMessage(StorageException.class, () -> clearMvStorage(0), "Storage does not exist");
+
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> startRebalanceMvStorage(0), "Storage does not exist");
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> abortRebalanceMvStorage(0), "Storage does not exist");
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> finishRebalanceMvStorage(0), "Storage does not exist");
+
+ finishDestroyMvStorageFuture.complete(null);
+
+ assertThat(destroyMvStorageFuture, willCompleteSuccessfully());
+
+ assertNull(getMvStorage(0));
+
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+ }
+
+ @Test
+ void testDestroyError() {
+ assertThrows(IllegalArgumentException.class, () -> destroyMvStorage(getPartitionIdOutOfConfig()));
+
+ assertThrowsWithMessage(StorageException.class, () -> destroyMvStorage(0), "Storage does not exist");
+
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ // What if there is an error during the operation?
+
+ assertThat(
+ mvPartitionStorages.destroy(0, mvStorage -> failedFuture(new RuntimeException("from test"))),
+ willFailFast(RuntimeException.class)
+ );
+
+ assertNull(getMvStorage(0));
+
+ assertThrowsWithMessage(StorageException.class, () -> destroyMvStorage(0), "Storage does not exist");
+ }
+
+ @Test
+ void testClear() {
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ MvPartitionStorage mvStorage = getMvStorage(0);
+
+ CompletableFuture<Void> startCleanupMvStorageFuture = new CompletableFuture<>();
+ CompletableFuture<Void> finishCleanupMvStorageFuture = new CompletableFuture<>();
+
+ CompletableFuture<?> cleanupMvStorageFuture = runAsync(() ->
+ assertThat(mvPartitionStorages.clear(0, mvStorage1 -> {
+ startCleanupMvStorageFuture.complete(null);
+
+ return finishCleanupMvStorageFuture;
+ }), willCompleteSuccessfully())
+ );
+
+ assertThat(startCleanupMvStorageFuture, willCompleteSuccessfully());
+
+ assertThrowsWithMessage(StorageException.class, () -> destroyMvStorage(0), "Storage is in process of being cleaned up");
+ assertThrowsWithMessage(StorageException.class, () -> clearMvStorage(0), "Storage is in process of being cleaned up");
+
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> startRebalanceMvStorage(0),
+ "Storage is in process of being cleaned up");
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> abortRebalanceMvStorage(0),
+ "Storage is in process of being cleaned up");
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> finishRebalanceMvStorage(0),
+ "Storage is in process of being cleaned up");
+
+ finishCleanupMvStorageFuture.complete(null);
+
+ assertThat(cleanupMvStorageFuture, willCompleteSuccessfully());
+
+ assertSame(mvStorage, getMvStorage(0));
+
+ assertThat(clearMvStorage(0), willCompleteSuccessfully());
+ }
+
+ @Test
+ void testClearError() {
+ assertThrows(IllegalArgumentException.class, () -> clearMvStorage(getPartitionIdOutOfConfig()));
+
+ assertThrowsWithMessage(StorageException.class, () -> clearMvStorage(0), "Storage does not exist");
+
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ // What if there is an error during the operation?
+
+ assertThat(
+ mvPartitionStorages.clear(0, mvStorage -> failedFuture(new RuntimeException("from test"))),
+ willFailFast(RuntimeException.class)
+ );
+
+ assertNotNull(getMvStorage(0));
+ }
+
+ @Test
+ void testStartRebalance() {
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ CompletableFuture<Void> startStartRebalanceMvStorage = new CompletableFuture<>();
+ CompletableFuture<Void> finishStartRebalanceMvStorage = new CompletableFuture<>();
+
+ CompletableFuture<?> startRebalanceFuture = runAsync(() ->
+ assertThat(mvPartitionStorages.startRebalace(0, mvStorage -> {
+ startStartRebalanceMvStorage.complete(null);
+
+ return finishStartRebalanceMvStorage;
+ }), willCompleteSuccessfully())
+ );
+
+ assertThat(startStartRebalanceMvStorage, willCompleteSuccessfully());
+
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> startRebalanceMvStorage(0),
+ "Storage in the process of starting a rebalance");
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> abortRebalanceMvStorage(0),
+ "Storage in the process of starting a rebalance");
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> finishRebalanceMvStorage(0),
+ "Storage in the process of starting a rebalance");
+
+ assertThrowsWithMessage(StorageException.class, () -> destroyMvStorage(0), "Storage in the process of starting a rebalance");
+ assertThrowsWithMessage(StorageException.class, () -> clearMvStorage(0), "Storage in the process of starting a rebalance");
+
+ finishStartRebalanceMvStorage.complete(null);
+
+ assertThat(startRebalanceFuture, willCompleteSuccessfully());
+
+ assertNotNull(getMvStorage(0));
+
+ // What if we restart the rebalance?
+
+ assertThat(finishRebalanceMvStorage(0), willCompleteSuccessfully());
+ assertThat(startRebalanceMvStorage(0), willCompleteSuccessfully());
+ }
+
+ @Test
+ void testStartRebalanceError() {
+ assertThrows(IllegalArgumentException.class, () -> startRebalanceMvStorage(getPartitionIdOutOfConfig()));
+
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> startRebalanceMvStorage(0), "Storage does not exist");
+
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ // What if there is an error during the operation?
+
+ assertThat(
+ mvPartitionStorages.startRebalace(0, mvStorage -> failedFuture(new RuntimeException("from test"))),
+ willFailFast(RuntimeException.class)
+ );
+
+ assertNotNull(getMvStorage(0));
+
+ // What if we restart the rebalance?
+
+ assertThat(abortRebalanceMvStorage(0), willCompleteSuccessfully());
+
+ assertThat(startRebalanceMvStorage(0), willCompleteSuccessfully());
+
+ assertThrows(StorageRebalanceException.class, () -> startRebalanceMvStorage(0), "Storage in the process of rebalance");
+ }
+
+ @Test
+ void testAbortRebalance() {
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ assertThat(startRebalanceMvStorage(0), willCompleteSuccessfully());
+
+ CompletableFuture<Void> startAbortRebalanceFuture = new CompletableFuture<>();
+ CompletableFuture<Void> finishAbortRebalanceFuture = new CompletableFuture<>();
+
+ CompletableFuture<?> abortRebalanceFuture = runAsync(() ->
+ assertThat(mvPartitionStorages.abortRebalance(0, mvStorage -> {
+ startAbortRebalanceFuture.complete(null);
+
+ return finishAbortRebalanceFuture;
+ }), willCompleteSuccessfully())
+ );
+
+ assertThat(startAbortRebalanceFuture, willCompleteSuccessfully());
+
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> startRebalanceMvStorage(0),
+ "Storage in the process of aborting a rebalance");
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> abortRebalanceMvStorage(0),
+ "Storage in the process of aborting a rebalance");
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> finishRebalanceMvStorage(0),
+ "Storage in the process of aborting a rebalance");
+
+ assertThrowsWithMessage(StorageException.class, () -> destroyMvStorage(0), "Storage in the process of aborting a rebalance");
+ assertThrowsWithMessage(StorageException.class, () -> clearMvStorage(0), "Storage in the process of aborting a rebalance");
+
+ finishAbortRebalanceFuture.complete(null);
+
+ assertThat(abortRebalanceFuture, willCompleteSuccessfully());
+
+ assertNotNull(getMvStorage(0));
+
+ // What if the rebalancing didn't start?
+
+ AtomicBoolean invokeAbortFunction = new AtomicBoolean();
+
+ assertThat(mvPartitionStorages.abortRebalance(0, mvStorage -> {
+ invokeAbortFunction.set(true);
+
+ return completedFuture(null);
+ }), willCompleteSuccessfully());
+
+ assertFalse(invokeAbortFunction.get());
+
+ // What if the start of the rebalance ended with an error?
+
+ invokeAbortFunction.set(false);
+
+ assertThat(
+ mvPartitionStorages.startRebalace(0, mvStorage -> failedFuture(new RuntimeException("from test"))),
+ willFailFast(RuntimeException.class)
+ );
+
+ assertThat(mvPartitionStorages.abortRebalance(0, mvStorage -> {
+ invokeAbortFunction.set(true);
+
+ return completedFuture(null);
+ }), willCompleteSuccessfully());
+
+ assertTrue(invokeAbortFunction.get());
+ }
+
+ @Test
+ void testAbortRebalanceError() {
+ assertThrows(IllegalArgumentException.class, () -> abortRebalanceMvStorage(getPartitionIdOutOfConfig()));
+
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> abortRebalanceMvStorage(0), "Storage does not exist");
+
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ assertThat(startRebalanceMvStorage(0), willCompleteSuccessfully());
+
+ // What if there is an error during the operation?
+
+ assertThat(
+ mvPartitionStorages.abortRebalance(0, mvStorage -> failedFuture(new RuntimeException("from test"))),
+ willFailFast(RuntimeException.class)
+ );
+ }
+
+ @Test
+ void testFinishRebalance() {
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ assertThat(startRebalanceMvStorage(0), willCompleteSuccessfully());
+
+ CompletableFuture<Void> startFinishRebalanceFuture = new CompletableFuture<>();
+ CompletableFuture<Void> finishFinishRebalanceFuture = new CompletableFuture<>();
+
+ CompletableFuture<?> finishRebalanceFuture = runAsync(() ->
+ assertThat(mvPartitionStorages.finishRebalance(0, mvStorage -> {
+ startFinishRebalanceFuture.complete(null);
+
+ return finishFinishRebalanceFuture;
+ }), willCompleteSuccessfully())
+ );
+
+ assertThat(startFinishRebalanceFuture, willCompleteSuccessfully());
+
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> startRebalanceMvStorage(0),
+ "Storage in the process of finishing a rebalance");
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> abortRebalanceMvStorage(0),
+ "Storage in the process of finishing a rebalance");
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> finishRebalanceMvStorage(0),
+ "Storage in the process of finishing a rebalance");
+
+ assertThrowsWithMessage(StorageException.class, () -> destroyMvStorage(0), "Storage in the process of finishing a rebalance");
+ assertThrowsWithMessage(StorageException.class, () -> clearMvStorage(0), "Storage in the process of finishing a rebalance");
+
+ finishFinishRebalanceFuture.complete(null);
+
+ assertThat(finishRebalanceFuture, willCompleteSuccessfully());
+
+ assertNotNull(getMvStorage(0));
+ }
+
+ @Test
+ void testFinishRebalanceError() {
+ assertThrows(IllegalArgumentException.class, () -> finishRebalanceMvStorage(getPartitionIdOutOfConfig()));
+
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> finishRebalanceMvStorage(0), "Storage does not exist");
+
+ assertThat(createMvStorage(0), willCompleteSuccessfully());
+
+ assertThrowsWithMessage(StorageRebalanceException.class, () -> finishRebalanceMvStorage(0), "Storage rebalancing did not start");
+
+ assertThat(startRebalanceMvStorage(0), willCompleteSuccessfully());
+
+ // What if there is an error during the operation?
+
+ assertThat(
+ mvPartitionStorages.finishRebalance(0, mvStorage -> failedFuture(new RuntimeException("from test"))),
+ willFailFast(RuntimeException.class)
+ );
+
+ // What if the start of the rebalance fails?
+
+ assertThat(abortRebalanceMvStorage(0), willCompleteSuccessfully());
+
+ assertThat(
+ mvPartitionStorages.startRebalace(0, mvStorage -> failedFuture(new RuntimeException("from test"))),
+ willFailFast(RuntimeException.class)
+ );
+
+ assertThat(finishRebalanceMvStorage(0), willFailFast(RuntimeException.class));
+ }
+
+ @Test
+ void testGetAllForClose() {
+ MvPartitionStorage storage0 = mock(MvPartitionStorage.class);
+ MvPartitionStorage storage1 = mock(MvPartitionStorage.class);
+
+ assertThat(mvPartitionStorages.create(0, partId -> storage0), willCompleteSuccessfully());
+ assertThat(mvPartitionStorages.create(1, partId -> storage1), willCompleteSuccessfully());
+
+ assertThat(mvPartitionStorages.getAllForClose(), contains(storage0, storage1));
+ }
+
+ @Test
+ void testDestroyAll() {
+ MvPartitionStorage storage0 = mock(MvPartitionStorage.class);
+ MvPartitionStorage storage1 = mock(MvPartitionStorage.class);
+
+ assertThat(mvPartitionStorages.create(0, partId -> storage0), willCompleteSuccessfully());
+ assertThat(mvPartitionStorages.create(1, partId -> storage1), willCompleteSuccessfully());
+
+ CompletableFuture<Void> startDestroyMvStorage0Future = new CompletableFuture<>();
+ CompletableFuture<Void> finishDestroyMvStorage0Future = new CompletableFuture<>();
+
+ CompletableFuture<?> destroyMvStorage0Future = runAsync(() ->
+ assertThat(mvPartitionStorages.destroy(0, mvStorage -> {
+ startDestroyMvStorage0Future.complete(null);
+
+ return finishDestroyMvStorage0Future;
+ }), willCompleteSuccessfully())
+ );
+
+ assertThat(startDestroyMvStorage0Future, willCompleteSuccessfully());
+
+ CompletableFuture<Void> destroyAllMvStoragesFuture = mvPartitionStorages.destroyAll(mvStorage -> {
+ assertSame(mvStorage, storage1);
+
+ return completedFuture(null);
+ });
+
+ assertThat(destroyAllMvStoragesFuture, willTimeoutFast());
+
+ finishDestroyMvStorage0Future.complete(null);
+
+ assertThat(destroyMvStorage0Future, willCompleteSuccessfully());
+ assertThat(destroyAllMvStoragesFuture, willCompleteSuccessfully());
+ }
+
+ private MvPartitionStorage getMvStorage(int partitionId) {
+ return mvPartitionStorages.get(partitionId);
+ }
+
+ private CompletableFuture<MvPartitionStorage> createMvStorage(int partitionId) {
+ return mvPartitionStorages.create(partitionId, partId -> mock(MvPartitionStorage.class));
+ }
+
+ private CompletableFuture<Void> destroyMvStorage(int partitionId) {
+ return mvPartitionStorages.destroy(partitionId, mvStorage -> completedFuture(null));
+ }
+
+ private CompletableFuture<Void> clearMvStorage(int partitionId) {
+ return mvPartitionStorages.clear(partitionId, mvStorage -> completedFuture(null));
+ }
+
+ private CompletableFuture<Void> startRebalanceMvStorage(int partitionId) {
+ return mvPartitionStorages.startRebalace(partitionId, mvStorage -> completedFuture(null));
+ }
+
+ private CompletableFuture<Void> abortRebalanceMvStorage(int partitionId) {
+ return mvPartitionStorages.abortRebalance(partitionId, mvStorage -> completedFuture(null));
+ }
+
+ private CompletableFuture<Void> finishRebalanceMvStorage(int partitionId) {
+ return mvPartitionStorages.finishRebalance(partitionId, mvStorage -> completedFuture(null));
+ }
+
+ private int getPartitionIdOutOfConfig() {
+ return tableConfig.partitions().value();
+ }
+
+ private static <T extends Throwable> void assertThrowsWithMessage(
+ Class<T> expectedType,
+ Executable executable,
+ String expectedErrorMessageSubString
+ ) {
+ T throwable = assertThrows(expectedType, executable);
+
+ assertThat(throwable.getMessage(), containsString(expectedErrorMessageSubString));
+ }
+}
diff --git a/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/AbstractMvTableStorageTest.java b/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/AbstractMvTableStorageTest.java
index ca17393233..b940f280f1 100644
--- a/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/AbstractMvTableStorageTest.java
+++ b/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/AbstractMvTableStorageTest.java
@@ -21,6 +21,7 @@ import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.toList;
import static org.apache.ignite.internal.storage.MvPartitionStorage.REBALANCE_IN_PROGRESS;
import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willFailFast;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
@@ -145,15 +146,21 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
*/
@Test
void testCreatePartition() {
+ assertThrows(IllegalArgumentException.class, () -> tableStorage.createMvPartition(getPartitionIdOutOfRange()));
+
MvPartitionStorage absentStorage = tableStorage.getMvPartition(0);
assertThat(absentStorage, is(nullValue()));
- MvPartitionStorage partitionStorage = tableStorage.getOrCreateMvPartition(0);
+ MvPartitionStorage partitionStorage = getOrCreateMvPartition(0);
assertThat(partitionStorage, is(notNullValue()));
assertThat(partitionStorage, is(sameInstance(tableStorage.getMvPartition(0))));
+
+ StorageException exception = assertThrows(StorageException.class, () -> tableStorage.createMvPartition(0));
+
+ assertThat(exception.getMessage(), containsString("Storage already exists"));
}
/**
@@ -161,9 +168,9 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
*/
@Test
void testPartitionIndependence() {
- MvPartitionStorage partitionStorage0 = tableStorage.getOrCreateMvPartition(PARTITION_ID_0);
+ MvPartitionStorage partitionStorage0 = getOrCreateMvPartition(PARTITION_ID_0);
// Using a shifted ID value to test a multibyte scenario.
- MvPartitionStorage partitionStorage1 = tableStorage.getOrCreateMvPartition(PARTITION_ID_1);
+ MvPartitionStorage partitionStorage1 = getOrCreateMvPartition(PARTITION_ID_1);
var testData0 = binaryRow(new TestKey(1, "0"), new TestValue(10, "10"));
@@ -199,7 +206,7 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
assertThrows(StorageException.class, () -> tableStorage.getOrCreateIndex(PARTITION_ID, hashIdx.id()));
// Index should only be available after the associated partition has been created.
- tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ tableStorage.createMvPartition(PARTITION_ID);
assertThat(tableStorage.getOrCreateIndex(PARTITION_ID, sortedIdx.id()), is(instanceOf(SortedIndexStorage.class)));
assertThat(tableStorage.getOrCreateIndex(PARTITION_ID, hashIdx.id()), is(instanceOf(HashIndexStorage.class)));
@@ -215,7 +222,7 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
assertThrows(StorageException.class, () -> tableStorage.getOrCreateSortedIndex(PARTITION_ID, sortedIdx.id()));
// Index should only be available after the associated partition has been created.
- tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ tableStorage.createMvPartition(PARTITION_ID);
assertThat(tableStorage.getOrCreateSortedIndex(PARTITION_ID, sortedIdx.id()), is(notNullValue()));
}
@@ -228,7 +235,7 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
assertThrows(StorageException.class, () -> tableStorage.getOrCreateHashIndex(PARTITION_ID, hashIdx.id()));
// Index should only be available after the associated partition has been created.
- tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ tableStorage.createMvPartition(PARTITION_ID);
assertThat(tableStorage.getOrCreateHashIndex(PARTITION_ID, hashIdx.id()), is(notNullValue()));
}
@@ -239,7 +246,7 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
@Disabled("https://issues.apache.org/jira/browse/IGNITE-17626")
@Test
public void testDestroyIndex() {
- MvPartitionStorage partitionStorage = tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ MvPartitionStorage partitionStorage = getOrCreateMvPartition(PARTITION_ID);
assertThat(tableStorage.getOrCreateSortedIndex(PARTITION_ID, sortedIdx.id()), is(notNullValue()));
assertThat(tableStorage.getOrCreateHashIndex(PARTITION_ID, hashIdx.id()), is(notNullValue()));
@@ -254,12 +261,12 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
@Test
public void testHashIndexIndependence() {
- MvPartitionStorage partitionStorage1 = tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ MvPartitionStorage partitionStorage1 = getOrCreateMvPartition(PARTITION_ID);
assertThat(tableStorage.getOrCreateHashIndex(PARTITION_ID, hashIdx.id()), is(notNullValue()));
assertThrows(StorageException.class, () -> tableStorage.getOrCreateHashIndex(PARTITION_ID + 1, hashIdx.id()));
- MvPartitionStorage partitionStorage2 = tableStorage.getOrCreateMvPartition(PARTITION_ID + 1);
+ MvPartitionStorage partitionStorage2 = getOrCreateMvPartition(PARTITION_ID + 1);
HashIndexStorage storage1 = tableStorage.getOrCreateHashIndex(PARTITION_ID, hashIdx.id());
HashIndexStorage storage2 = tableStorage.getOrCreateHashIndex(PARTITION_ID + 1, hashIdx.id());
@@ -317,7 +324,7 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
assertThat(e.getMessage(), containsString("Partition ID " + PARTITION_ID + " does not exist"));
- tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ tableStorage.createMvPartition(PARTITION_ID);
UUID invalidUuid = UUID.randomUUID();
@@ -353,7 +360,7 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
public void testDestroyPartition() throws Exception {
assertThrows(IllegalArgumentException.class, () -> tableStorage.destroyPartition(getPartitionIdOutOfRange()));
- MvPartitionStorage mvPartitionStorage = tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ MvPartitionStorage mvPartitionStorage = getOrCreateMvPartition(PARTITION_ID);
HashIndexStorage hashIndexStorage = tableStorage.getOrCreateHashIndex(PARTITION_ID, hashIdx.id());
SortedIndexStorage sortedIndexStorage = tableStorage.getOrCreateSortedIndex(PARTITION_ID, sortedIdx.id());
@@ -401,13 +408,13 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
assertThrows(StorageClosedException.class, () -> getAll(getFromSortedIndexCursor));
assertThrows(StorageClosedException.class, () -> getAll(scanFromSortedIndexCursor));
- // Let's check that nothing will happen if we try to destroy a non-existing partition.
- assertThat(tableStorage.destroyPartition(PARTITION_ID), willCompleteSuccessfully());
+ // What happens if there is no partition?
+ assertThrows(StorageException.class, () -> tableStorage.destroyPartition(PARTITION_ID));
}
@Test
public void testReCreatePartition() throws Exception {
- MvPartitionStorage mvPartitionStorage = tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ MvPartitionStorage mvPartitionStorage = getOrCreateMvPartition(PARTITION_ID);
RowId rowId = new RowId(PARTITION_ID);
@@ -421,14 +428,14 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
tableStorage.destroyPartition(PARTITION_ID).get(1, SECONDS);
- MvPartitionStorage newMvPartitionStorage = tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ MvPartitionStorage newMvPartitionStorage = getOrCreateMvPartition(PARTITION_ID);
assertThat(getAll(newMvPartitionStorage.scanVersions(rowId)), empty());
}
@Test
public void testSuccessRebalance() {
- MvPartitionStorage mvPartitionStorage = tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ MvPartitionStorage mvPartitionStorage = getOrCreateMvPartition(PARTITION_ID);
HashIndexStorage hashIndexStorage = tableStorage.getOrCreateHashIndex(PARTITION_ID, hashIdx.id());
SortedIndexStorage sortedIndexStorage = tableStorage.getOrCreateSortedIndex(PARTITION_ID, sortedIdx.id());
@@ -490,7 +497,7 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
@Test
public void testFailRebalance() throws Exception {
- MvPartitionStorage mvPartitionStorage = tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ MvPartitionStorage mvPartitionStorage = getOrCreateMvPartition(PARTITION_ID);
HashIndexStorage hashIndexStorage = tableStorage.getOrCreateHashIndex(PARTITION_ID, hashIdx.id());
SortedIndexStorage sortedIndexStorage = tableStorage.getOrCreateSortedIndex(PARTITION_ID, sortedIdx.id());
@@ -537,16 +544,16 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
@Test
public void testStartRebalanceForClosedPartition() {
- MvPartitionStorage mvPartitionStorage = tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ MvPartitionStorage mvPartitionStorage = getOrCreateMvPartition(PARTITION_ID);
mvPartitionStorage.close();
- assertThrows(StorageRebalanceException.class, () -> tableStorage.startRebalancePartition(PARTITION_ID));
+ assertThat(tableStorage.startRebalancePartition(PARTITION_ID), willFailFast(StorageRebalanceException.class));
}
@Test
public void testDestroyTableStorage() {
- MvPartitionStorage mvPartitionStorage = tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ MvPartitionStorage mvPartitionStorage = getOrCreateMvPartition(PARTITION_ID);
HashIndexStorage hashIndexStorage = tableStorage.getOrCreateHashIndex(PARTITION_ID, hashIdx.id());
SortedIndexStorage sortedIndexStorage = tableStorage.getOrCreateSortedIndex(PARTITION_ID, sortedIdx.id());
@@ -590,7 +597,7 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
tableStorage.start();
- mvPartitionStorage = tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ mvPartitionStorage = getOrCreateMvPartition(PARTITION_ID);
hashIndexStorage = tableStorage.getOrCreateHashIndex(PARTITION_ID, hashIdx.id());
sortedIndexStorage = tableStorage.getOrCreateSortedIndex(PARTITION_ID, sortedIdx.id());
@@ -599,7 +606,7 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
@Test
public void testRestartStoragesInTheMiddleOfRebalance() {
- MvPartitionStorage mvPartitionStorage = tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ MvPartitionStorage mvPartitionStorage = getOrCreateMvPartition(PARTITION_ID);
HashIndexStorage hashIndexStorage = tableStorage.getOrCreateHashIndex(PARTITION_ID, hashIdx.id());
SortedIndexStorage sortedIndexStorage = tableStorage.getOrCreateSortedIndex(PARTITION_ID, sortedIdx.id());
@@ -628,7 +635,7 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
tableStorage.start();
- mvPartitionStorage = tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ mvPartitionStorage = getOrCreateMvPartition(PARTITION_ID);
hashIndexStorage = tableStorage.getOrCreateHashIndex(PARTITION_ID, hashIdx.id());
sortedIndexStorage = tableStorage.getOrCreateSortedIndex(PARTITION_ID, sortedIdx.id());
@@ -651,7 +658,7 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
// Let's check that there will be an error for a non-existent partition.
assertThrows(StorageException.class, () -> tableStorage.clearPartition(PARTITION_ID));
- MvPartitionStorage mvPartitionStorage = tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ MvPartitionStorage mvPartitionStorage = getOrCreateMvPartition(PARTITION_ID);
HashIndexStorage hashIndexStorage = tableStorage.getOrCreateHashIndex(PARTITION_ID, hashIdx.id());
SortedIndexStorage sortedIndexStorage = tableStorage.getOrCreateSortedIndex(PARTITION_ID, sortedIdx.id());
@@ -690,15 +697,15 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
@Test
void testClearForCanceledOrRebalancedPartition() {
- MvPartitionStorage mvPartitionStorage0 = tableStorage.getOrCreateMvPartition(PARTITION_ID);
- tableStorage.getOrCreateMvPartition(PARTITION_ID + 1);
+ MvPartitionStorage mvPartitionStorage0 = getOrCreateMvPartition(PARTITION_ID);
+ getOrCreateMvPartition(PARTITION_ID + 1);
mvPartitionStorage0.close();
assertThat(tableStorage.startRebalancePartition(PARTITION_ID + 1), willCompleteSuccessfully());
try {
- assertThrows(StorageClosedException.class, () -> tableStorage.clearPartition(PARTITION_ID));
- assertThrows(StorageRebalanceException.class, () -> tableStorage.clearPartition(PARTITION_ID + 1));
+ assertThat(tableStorage.clearPartition(PARTITION_ID), willFailFast(StorageClosedException.class));
+ assertThat(tableStorage.clearPartition(PARTITION_ID + 1), willFailFast(StorageRebalanceException.class));
} finally {
assertThat(tableStorage.abortRebalancePartition(PARTITION_ID + 1), willCompleteSuccessfully());
}
@@ -1014,4 +1021,11 @@ public abstract class AbstractMvTableStorageTest extends BaseMvStoragesTest {
assertArrayEquals(exp, act);
}
+
+ /**
+ * Retrieves or creates a multi-versioned partition storage.
+ */
+ protected MvPartitionStorage getOrCreateMvPartition(int partitionId) {
+ return getOrCreateMvPartition(tableStorage, partitionId);
+ }
}
diff --git a/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/BaseMvPartitionStorageTest.java b/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/BaseMvPartitionStorageTest.java
index 7724b0c2d5..8f980ab348 100644
--- a/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/BaseMvPartitionStorageTest.java
+++ b/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/BaseMvPartitionStorageTest.java
@@ -83,7 +83,7 @@ public abstract class BaseMvPartitionStorageTest extends BaseMvStoragesTest {
table.start();
- storage = table.getOrCreateMvPartition(PARTITION_ID);
+ storage = getOrCreateMvPartition(table, PARTITION_ID);
}
@AfterEach
diff --git a/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/BaseMvStoragesTest.java b/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/BaseMvStoragesTest.java
index bc376c654b..6c69eee36e 100644
--- a/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/BaseMvStoragesTest.java
+++ b/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/BaseMvStoragesTest.java
@@ -17,6 +17,7 @@
package org.apache.ignite.internal.storage;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
@@ -25,6 +26,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.List;
import java.util.Objects;
+import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.ignite.internal.hlc.HybridClock;
@@ -40,6 +42,7 @@ import org.apache.ignite.internal.schema.marshaller.MarshallerException;
import org.apache.ignite.internal.schema.marshaller.MarshallerFactory;
import org.apache.ignite.internal.schema.marshaller.reflection.ReflectionMarshallerFactory;
import org.apache.ignite.internal.schema.row.Row;
+import org.apache.ignite.internal.storage.engine.MvTableStorage;
import org.apache.ignite.internal.storage.index.IndexDescriptor;
import org.apache.ignite.internal.storage.index.IndexRow;
import org.apache.ignite.internal.storage.index.IndexRowImpl;
@@ -235,4 +238,21 @@ public abstract class BaseMvStoragesTest {
return S.toString(TestValue.class, this);
}
}
+
+ /**
+ * Retrieves or creates a multi-versioned partition storage.
+ */
+ public static MvPartitionStorage getOrCreateMvPartition(MvTableStorage tableStorage, int partitionId) {
+ MvPartitionStorage mvPartition = tableStorage.getMvPartition(partitionId);
+
+ if (mvPartition != null) {
+ return mvPartition;
+ }
+
+ CompletableFuture<MvPartitionStorage> createMvPartitionStorageFuture = tableStorage.createMvPartition(partitionId);
+
+ assertThat(createMvPartitionStorageFuture, willCompleteSuccessfully());
+
+ return createMvPartitionStorageFuture.join();
+ }
}
diff --git a/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/impl/TestMvPartitionStorage.java b/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/impl/TestMvPartitionStorage.java
index be754ecacd..6afd07d7dd 100644
--- a/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/impl/TestMvPartitionStorage.java
+++ b/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/impl/TestMvPartitionStorage.java
@@ -60,7 +60,7 @@ public class TestMvPartitionStorage implements MvPartitionStorage {
private volatile byte @Nullable [] groupConfig;
- private final int partitionId;
+ final int partitionId;
private volatile boolean closed;
@@ -580,6 +580,12 @@ public class TestMvPartitionStorage implements MvPartitionStorage {
}
}
+ private void checkStorageClosedForRebalance() {
+ if (closed) {
+ throw new StorageRebalanceException();
+ }
+ }
+
private void checkStorageInProcessOfRebalance() {
if (rebalance) {
throw new StorageRebalanceException();
@@ -592,7 +598,7 @@ public class TestMvPartitionStorage implements MvPartitionStorage {
}
void startRebalance() {
- checkStorageClosed();
+ checkStorageClosedForRebalance();
rebalance = true;
@@ -605,7 +611,7 @@ public class TestMvPartitionStorage implements MvPartitionStorage {
}
void abortRebalance() {
- checkStorageClosed();
+ checkStorageClosedForRebalance();
if (!rebalance) {
return;
@@ -622,7 +628,7 @@ public class TestMvPartitionStorage implements MvPartitionStorage {
}
void finishRebalance(long lastAppliedIndex, long lastAppliedTerm, byte[] groupConfig) {
- checkStorageClosed();
+ checkStorageClosedForRebalance();
assert rebalance;
diff --git a/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/impl/TestMvTableStorage.java b/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/impl/TestMvTableStorage.java
index 771c00960f..a049b4da13 100644
--- a/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/impl/TestMvTableStorage.java
+++ b/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/impl/TestMvTableStorage.java
@@ -17,7 +17,6 @@
package org.apache.ignite.internal.storage.impl;
-import static java.util.concurrent.CompletableFuture.allOf;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.apache.ignite.internal.storage.util.StorageUtils.createMissingMvPartitionErrorMessage;
import static org.mockito.Mockito.spy;
@@ -32,7 +31,6 @@ import org.apache.ignite.internal.schema.configuration.TableConfiguration;
import org.apache.ignite.internal.schema.configuration.TablesConfiguration;
import org.apache.ignite.internal.storage.MvPartitionStorage;
import org.apache.ignite.internal.storage.StorageException;
-import org.apache.ignite.internal.storage.StorageRebalanceException;
import org.apache.ignite.internal.storage.engine.MvTableStorage;
import org.apache.ignite.internal.storage.index.HashIndexDescriptor;
import org.apache.ignite.internal.storage.index.HashIndexStorage;
@@ -40,23 +38,20 @@ import org.apache.ignite.internal.storage.index.SortedIndexDescriptor;
import org.apache.ignite.internal.storage.index.SortedIndexStorage;
import org.apache.ignite.internal.storage.index.impl.TestHashIndexStorage;
import org.apache.ignite.internal.storage.index.impl.TestSortedIndexStorage;
-import org.apache.ignite.internal.tostring.S;
+import org.apache.ignite.internal.storage.util.MvPartitionStorages;
+import org.apache.ignite.lang.IgniteStringFormatter;
import org.jetbrains.annotations.Nullable;
/**
* Test table storage implementation.
*/
public class TestMvTableStorage implements MvTableStorage {
- private final Map<Integer, TestMvPartitionStorage> partitions = new ConcurrentHashMap<>();
+ private final MvPartitionStorages<TestMvPartitionStorage> mvPartitionStorages;
private final Map<UUID, SortedIndices> sortedIndicesById = new ConcurrentHashMap<>();
private final Map<UUID, HashIndices> hashIndicesById = new ConcurrentHashMap<>();
- private final Map<Integer, CompletableFuture<Void>> destroyFutureByPartitionId = new ConcurrentHashMap<>();
-
- private final Map<Integer, CompletableFuture<Void>> rebalanceFutureByPartitionId = new ConcurrentHashMap<>();
-
private final TableConfiguration tableCfg;
private final TablesConfiguration tablesCfg;
@@ -99,72 +94,52 @@ public class TestMvTableStorage implements MvTableStorage {
public TestMvTableStorage(TableConfiguration tableCfg, TablesConfiguration tablesCfg) {
this.tableCfg = tableCfg;
this.tablesCfg = tablesCfg;
+
+ mvPartitionStorages = new MvPartitionStorages<>(tableCfg.value());
}
@Override
- public MvPartitionStorage getOrCreateMvPartition(int partitionId) throws StorageException {
- return partitions.computeIfAbsent(partitionId, partId -> spy(new TestMvPartitionStorage(partId)));
+ public CompletableFuture<MvPartitionStorage> createMvPartition(int partitionId) {
+ return mvPartitionStorages.create(partitionId, partId -> spy(new TestMvPartitionStorage(partId)));
}
@Override
- @Nullable
- public MvPartitionStorage getMvPartition(int partitionId) {
- return partitions.get(partitionId);
+ public @Nullable MvPartitionStorage getMvPartition(int partitionId) {
+ return mvPartitionStorages.get(partitionId);
}
@Override
public CompletableFuture<Void> destroyPartition(int partitionId) {
- checkPartitionId(partitionId);
-
- assert !rebalanceFutureByPartitionId.containsKey(partitionId);
+ return mvPartitionStorages.destroy(partitionId, this::destroyPartition);
+ }
- CompletableFuture<Void> destroyPartitionFuture = new CompletableFuture<>();
+ private CompletableFuture<Void> destroyPartition(TestMvPartitionStorage mvPartitionStorage) {
+ mvPartitionStorage.destroy();
- CompletableFuture<Void> previousDestroyPartitionFuture = destroyFutureByPartitionId.putIfAbsent(
- partitionId,
- destroyPartitionFuture
- );
+ for (HashIndices hashIndices : hashIndicesById.values()) {
+ HashIndexStorage removedHashIndexStorage = hashIndices.storageByPartitionId.remove(mvPartitionStorage.partitionId);
- if (previousDestroyPartitionFuture != null) {
- return previousDestroyPartitionFuture;
+ if (removedHashIndexStorage != null) {
+ removedHashIndexStorage.destroy();
+ }
}
- MvPartitionStorage removedMvPartitionStorage = partitions.remove(partitionId);
-
- if (removedMvPartitionStorage != null) {
- try {
- ((TestMvPartitionStorage) removedMvPartitionStorage).destroy();
+ for (SortedIndices sortedIndices : sortedIndicesById.values()) {
+ SortedIndexStorage removedSortedIndexStorage = sortedIndices.storageByPartitionId.remove(mvPartitionStorage.partitionId);
- for (HashIndices hashIndices : hashIndicesById.values()) {
- HashIndexStorage removedHashIndexStorage = hashIndices.storageByPartitionId.remove(partitionId);
-
- if (removedHashIndexStorage != null) {
- removedHashIndexStorage.destroy();
- }
- }
-
- for (SortedIndices sortedIndices : sortedIndicesById.values()) {
- SortedIndexStorage removedSortedIndexStorage = sortedIndices.storageByPartitionId.remove(partitionId);
-
- if (removedSortedIndexStorage != null) {
- ((TestSortedIndexStorage) removedSortedIndexStorage).destroy();
- }
- }
-
- destroyFutureByPartitionId.remove(partitionId).complete(null);
- } catch (Throwable throwable) {
- destroyFutureByPartitionId.remove(partitionId).completeExceptionally(throwable);
+ if (removedSortedIndexStorage != null) {
+ ((TestSortedIndexStorage) removedSortedIndexStorage).destroy();
}
- } else {
- destroyFutureByPartitionId.remove(partitionId).complete(null);
}
- return destroyPartitionFuture;
+ return completedFuture(null);
}
@Override
public SortedIndexStorage getOrCreateSortedIndex(int partitionId, UUID indexId) {
- if (!partitions.containsKey(partitionId)) {
+ TestMvPartitionStorage mvPartitionStorage = mvPartitionStorages.get(partitionId);
+
+ if (mvPartitionStorage == null) {
throw new StorageException(createMissingMvPartitionErrorMessage(partitionId));
}
@@ -178,7 +153,9 @@ public class TestMvTableStorage implements MvTableStorage {
@Override
public HashIndexStorage getOrCreateHashIndex(int partitionId, UUID indexId) {
- if (!partitions.containsKey(partitionId)) {
+ TestMvPartitionStorage mvPartitionStorage = mvPartitionStorages.get(partitionId);
+
+ if (mvPartitionStorage == null) {
throw new StorageException(createMissingMvPartitionErrorMessage(partitionId));
}
@@ -235,76 +212,33 @@ public class TestMvTableStorage implements MvTableStorage {
public CompletableFuture<Void> destroy() {
stop();
- CompletableFuture[] futures = new CompletableFuture[tableCfg.partitions().value()];
-
- for (int partitionId = 0; partitionId < futures.length; partitionId++) {
- futures[partitionId] = destroyPartition(partitionId);
- }
-
- return allOf(futures);
+ return mvPartitionStorages.destroyAll(this::destroyPartition);
}
@Override
public CompletableFuture<Void> startRebalancePartition(int partitionId) {
- checkPartitionId(partitionId);
-
- TestMvPartitionStorage partitionStorage = partitions.get(partitionId);
-
- if (partitionStorage == null) {
- throw new StorageRebalanceException(createMissingMvPartitionErrorMessage(partitionId));
- }
-
- assert !destroyFutureByPartitionId.containsKey(partitionId) : partitionId;
-
- if (partitionStorage.closed()) {
- throw new StorageRebalanceException("Partition closed: " + partitionId);
- }
-
- CompletableFuture<Void> rebalanceFuture = new CompletableFuture<>();
-
- if (rebalanceFutureByPartitionId.putIfAbsent(partitionId, rebalanceFuture) != null) {
- throw new StorageRebalanceException("Rebalance for the partition is already in progress: " + partitionId);
- }
-
- try {
- partitionStorage.startRebalance();
+ return mvPartitionStorages.startRebalace(partitionId, mvPartitionStorage -> {
+ mvPartitionStorage.startRebalance();
testHashIndexStorageStream(partitionId).forEach(TestHashIndexStorage::startRebalance);
testSortedIndexStorageStream(partitionId).forEach(TestSortedIndexStorage::startRebalance);
- rebalanceFuture.complete(null);
- } catch (Throwable t) {
- rebalanceFuture.completeExceptionally(t);
- }
-
- return rebalanceFuture;
+ return completedFuture(null);
+ });
}
@Override
public CompletableFuture<Void> abortRebalancePartition(int partitionId) {
- checkPartitionId(partitionId);
-
- CompletableFuture<Void> rebalanceFuture = rebalanceFutureByPartitionId.remove(partitionId);
-
- if (rebalanceFuture == null) {
- return completedFuture(null);
- }
-
- TestMvPartitionStorage partitionStorage = partitions.get(partitionId);
-
- if (partitionStorage == null) {
- throw new StorageRebalanceException(createMissingMvPartitionErrorMessage(partitionId));
- }
+ return mvPartitionStorages.abortRebalance(partitionId, mvPartitionStorage -> {
+ mvPartitionStorage.abortRebalance();
- return rebalanceFuture
- .thenAccept(unused -> {
- partitionStorage.abortRebalance();
+ testHashIndexStorageStream(partitionId).forEach(TestHashIndexStorage::abortRebalance);
- testHashIndexStorageStream(partitionId).forEach(TestHashIndexStorage::abortRebalance);
+ testSortedIndexStorageStream(partitionId).forEach(TestSortedIndexStorage::abortRebalance);
- testSortedIndexStorageStream(partitionId).forEach(TestSortedIndexStorage::abortRebalance);
- });
+ return completedFuture(null);
+ });
}
@Override
@@ -314,59 +248,27 @@ public class TestMvTableStorage implements MvTableStorage {
long lastAppliedTerm,
byte[] groupConfig
) {
- checkPartitionId(partitionId);
-
- CompletableFuture<Void> rebalanceFuture = rebalanceFutureByPartitionId.remove(partitionId);
-
- if (rebalanceFuture == null) {
- throw new StorageRebalanceException("Rebalance for the partition did not start: " + partitionId);
- }
-
- TestMvPartitionStorage partitionStorage = partitions.get(partitionId);
-
- if (partitionStorage == null) {
- throw new StorageRebalanceException(createMissingMvPartitionErrorMessage(partitionId));
- }
+ return mvPartitionStorages.finishRebalance(partitionId, mvPartitionStorage -> {
+ mvPartitionStorage.finishRebalance(lastAppliedIndex, lastAppliedTerm, groupConfig);
- return rebalanceFuture
- .thenAccept(unused -> {
- partitionStorage.finishRebalance(lastAppliedIndex, lastAppliedTerm, groupConfig);
+ testHashIndexStorageStream(partitionId).forEach(TestHashIndexStorage::finishRebalance);
- testHashIndexStorageStream(partitionId).forEach(TestHashIndexStorage::finishRebalance);
+ testSortedIndexStorageStream(partitionId).forEach(TestSortedIndexStorage::finishRebalance);
- testSortedIndexStorageStream(partitionId).forEach(TestSortedIndexStorage::finishRebalance);
- });
+ return completedFuture(null);
+ });
}
@Override
public CompletableFuture<Void> clearPartition(int partitionId) {
- checkPartitionId(partitionId);
-
- TestMvPartitionStorage mvPartitionStorage = partitions.get(partitionId);
-
- if (mvPartitionStorage == null) {
- throw new StorageException(createMissingMvPartitionErrorMessage(partitionId));
- }
+ return mvPartitionStorages.clear(partitionId, mvPartitionStorage -> {
+ mvPartitionStorage.clear();
- mvPartitionStorage.clear();
+ testHashIndexStorageStream(partitionId).forEach(TestHashIndexStorage::clear);
+ testSortedIndexStorageStream(partitionId).forEach(TestSortedIndexStorage::clear);
- testHashIndexStorageStream(partitionId).forEach(TestHashIndexStorage::clear);
- testSortedIndexStorageStream(partitionId).forEach(TestSortedIndexStorage::clear);
-
- return completedFuture(null);
- }
-
- private void checkPartitionId(int partitionId) {
- Integer partitions = tableCfg.partitions().value();
-
- if (partitionId < 0 || partitionId >= partitions) {
- throw new IllegalArgumentException(S.toString(
- "Unable to access partition with id outside of configured range",
- "table", tableCfg.value().name(), false,
- "partitionId", partitionId, false,
- "partitions", partitions, false
- ));
- }
+ return completedFuture(null);
+ });
}
private Stream<TestHashIndexStorage> testHashIndexStorageStream(Integer partitionId) {
@@ -380,4 +282,8 @@ public class TestMvTableStorage implements MvTableStorage {
.map(hashIndices -> hashIndices.storageByPartitionId.get(partitionId))
.filter(Objects::nonNull);
}
+
+ private String createStorageInfo(int partitionId) {
+ return IgniteStringFormatter.format("table={}, partitionId={}", tableCfg.name().value(), partitionId);
+ }
}
diff --git a/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/index/AbstractHashIndexStorageTest.java b/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/index/AbstractHashIndexStorageTest.java
index d51c015dc8..1df8a47f9f 100644
--- a/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/index/AbstractHashIndexStorageTest.java
+++ b/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/index/AbstractHashIndexStorageTest.java
@@ -22,6 +22,7 @@ import static org.apache.ignite.internal.schema.testutils.SchemaConfigurationCon
import static org.apache.ignite.internal.schema.testutils.SchemaConfigurationConverter.convert;
import static org.apache.ignite.internal.schema.testutils.builder.SchemaBuilders.column;
import static org.apache.ignite.internal.schema.testutils.builder.SchemaBuilders.tableBuilder;
+import static org.apache.ignite.internal.storage.BaseMvStoragesTest.getOrCreateMvPartition;
import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -78,7 +79,7 @@ public abstract class AbstractHashIndexStorageTest {
createTestTable(tableStorage.configuration());
- this.partitionStorage = tableStorage.getOrCreateMvPartition(TEST_PARTITION);
+ this.partitionStorage = getOrCreateMvPartition(tableStorage, TEST_PARTITION);
this.indexStorage = createIndex(tableStorage, tablesCfg);
this.serializer = new BinaryTupleRowSerializer(indexStorage.indexDescriptor());
}
diff --git a/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/index/AbstractSortedIndexStorageTest.java b/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/index/AbstractSortedIndexStorageTest.java
index cfc5760288..8f4596a6bc 100644
--- a/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/index/AbstractSortedIndexStorageTest.java
+++ b/modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/index/AbstractSortedIndexStorageTest.java
@@ -24,6 +24,7 @@ import static org.apache.ignite.internal.schema.testutils.SchemaConfigurationCon
import static org.apache.ignite.internal.schema.testutils.SchemaConfigurationConverter.convert;
import static org.apache.ignite.internal.schema.testutils.builder.SchemaBuilders.column;
import static org.apache.ignite.internal.schema.testutils.builder.SchemaBuilders.tableBuilder;
+import static org.apache.ignite.internal.storage.BaseMvStoragesTest.getOrCreateMvPartition;
import static org.apache.ignite.internal.storage.index.SortedIndexStorage.GREATER;
import static org.apache.ignite.internal.storage.index.SortedIndexStorage.GREATER_OR_EQUAL;
import static org.apache.ignite.internal.storage.index.SortedIndexStorage.LESS;
@@ -149,7 +150,7 @@ public abstract class AbstractSortedIndexStorageTest {
protected final void initialize(MvTableStorage tableStorage, TablesConfiguration tablesCfg) {
this.tablesCfg = tablesCfg;
this.tableStorage = tableStorage;
- this.partitionStorage = tableStorage.getOrCreateMvPartition(TEST_PARTITION);
+ this.partitionStorage = getOrCreateMvPartition(tableStorage, TEST_PARTITION);
createTestTable(tableStorage.configuration());
}
diff --git a/modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/AbstractPageMemoryTableStorage.java b/modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/AbstractPageMemoryTableStorage.java
index 01fbf1d519..7141005556 100644
--- a/modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/AbstractPageMemoryTableStorage.java
+++ b/modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/AbstractPageMemoryTableStorage.java
@@ -22,14 +22,9 @@ import static org.apache.ignite.internal.storage.MvPartitionStorage.REBALANCE_IN
import static org.apache.ignite.internal.storage.util.StorageUtils.createMissingMvPartitionErrorMessage;
import static org.apache.ignite.internal.util.IgniteUtils.inBusyLock;
-import java.util.ArrayList;
-import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.function.Supplier;
import org.apache.ignite.internal.pagememory.DataRegion;
import org.apache.ignite.internal.pagememory.PageMemory;
@@ -37,17 +32,16 @@ import org.apache.ignite.internal.pagememory.freelist.FreeList;
import org.apache.ignite.internal.pagememory.reuse.ReuseList;
import org.apache.ignite.internal.pagememory.tree.BplusTree;
import org.apache.ignite.internal.schema.configuration.TableConfiguration;
-import org.apache.ignite.internal.schema.configuration.TableView;
import org.apache.ignite.internal.schema.configuration.TablesConfiguration;
+import org.apache.ignite.internal.storage.MvPartitionStorage;
import org.apache.ignite.internal.storage.StorageException;
-import org.apache.ignite.internal.storage.StorageRebalanceException;
import org.apache.ignite.internal.storage.engine.MvTableStorage;
import org.apache.ignite.internal.storage.index.HashIndexStorage;
import org.apache.ignite.internal.storage.index.SortedIndexStorage;
import org.apache.ignite.internal.storage.pagememory.mv.AbstractPageMemoryMvPartitionStorage;
+import org.apache.ignite.internal.storage.util.MvPartitionStorages;
import org.apache.ignite.internal.util.IgniteSpinBusyLock;
import org.apache.ignite.internal.util.IgniteUtils;
-import org.apache.ignite.lang.IgniteStringFormatter;
import org.jetbrains.annotations.Nullable;
/**
@@ -58,13 +52,8 @@ public abstract class AbstractPageMemoryTableStorage implements MvTableStorage {
protected final TablesConfiguration tablesCfg;
- protected volatile AtomicReferenceArray<AbstractPageMemoryMvPartitionStorage> mvPartitions;
+ protected volatile MvPartitionStorages<AbstractPageMemoryMvPartitionStorage> mvPartitionStorages;
- protected final ConcurrentMap<Integer, CompletableFuture<Void>> destroyFutureByPartitionId = new ConcurrentHashMap<>();
-
- protected final ConcurrentMap<Integer, CompletableFuture<Void>> rebalanceFutureByPartitionId = new ConcurrentHashMap<>();
-
- /** Busy lock. */
private final IgniteSpinBusyLock busyLock = new IgniteSpinBusyLock();
/** Prevents double stopping of the component. */
@@ -99,9 +88,7 @@ public abstract class AbstractPageMemoryTableStorage implements MvTableStorage {
@Override
public void start() throws StorageException {
busy(() -> {
- TableView tableView = tableCfg.value();
-
- mvPartitions = new AtomicReferenceArray<>(tableView.partitions());
+ mvPartitionStorages = new MvPartitionStorages(tableCfg.value());
return null;
});
@@ -115,18 +102,8 @@ public abstract class AbstractPageMemoryTableStorage implements MvTableStorage {
busyLock.block();
- List<AutoCloseable> closeables = new ArrayList<>();
-
- for (int i = 0; i < mvPartitions.length(); i++) {
- AbstractPageMemoryMvPartitionStorage partition = mvPartitions.getAndUpdate(i, p -> null);
-
- if (partition != null) {
- closeables.add(partition::close);
- }
- }
-
try {
- IgniteUtils.closeAll(closeables);
+ IgniteUtils.closeAll(mvPartitionStorages.getAllForClose().stream().map(mvPartitionStorage -> mvPartitionStorage::close));
} catch (Exception e) {
throw new StorageException("Failed to stop PageMemory table storage: " + getTableName(), e);
}
@@ -140,34 +117,12 @@ public abstract class AbstractPageMemoryTableStorage implements MvTableStorage {
busyLock.block();
- List<CompletableFuture<Void>> destroyFutures = new ArrayList<>();
-
- for (int i = 0; i < mvPartitions.length(); i++) {
- CompletableFuture<Void> destroyPartitionFuture = destroyFutureByPartitionId.get(i);
-
- if (destroyPartitionFuture != null) {
- destroyFutures.add(destroyPartitionFuture);
- } else {
- AbstractPageMemoryMvPartitionStorage partition = mvPartitions.getAndUpdate(i, p -> null);
-
- if (partition != null) {
- destroyFutures.add(destroyMvPartitionStorage(partition));
- }
- }
- }
-
- if (destroyFutures.isEmpty()) {
- finishDestruction();
-
- return completedFuture(null);
- } else {
- return CompletableFuture.allOf(destroyFutures.toArray(CompletableFuture[]::new))
- .whenComplete((unused, throwable) -> {
- if (throwable == null) {
- finishDestruction();
- }
- });
- }
+ return mvPartitionStorages.destroyAll(this::destroyMvPartitionStorage)
+ .whenComplete((unused, throwable) -> {
+ if (throwable == null) {
+ finishDestruction();
+ }
+ });
}
/**
@@ -192,78 +147,30 @@ public abstract class AbstractPageMemoryTableStorage implements MvTableStorage {
abstract CompletableFuture<Void> destroyMvPartitionStorage(AbstractPageMemoryMvPartitionStorage mvPartitionStorage);
@Override
- public AbstractPageMemoryMvPartitionStorage getOrCreateMvPartition(int partitionId) throws StorageException {
- return busy(() -> {
- AbstractPageMemoryMvPartitionStorage partition = getMvPartitionBusy(partitionId);
-
- if (partition != null) {
- return partition;
- }
-
- partition = createMvPartitionStorage(partitionId);
+ public CompletableFuture<MvPartitionStorage> createMvPartition(int partitionId) {
+ return busy(() -> mvPartitionStorages.create(partitionId, partId -> {
+ AbstractPageMemoryMvPartitionStorage partition = createMvPartitionStorage(partitionId);
partition.start();
- mvPartitions.set(partitionId, partition);
-
return partition;
- });
+ }));
}
@Override
- public @Nullable AbstractPageMemoryMvPartitionStorage getMvPartition(int partitionId) {
- return busy(() -> getMvPartitionBusy(partitionId));
- }
-
- private @Nullable AbstractPageMemoryMvPartitionStorage getMvPartitionBusy(int partitionId) {
- checkPartitionId(partitionId);
-
- return mvPartitions.get(partitionId);
+ public @Nullable MvPartitionStorage getMvPartition(int partitionId) {
+ return busy(() -> mvPartitionStorages.get(partitionId));
}
@Override
public CompletableFuture<Void> destroyPartition(int partitionId) {
- return busy(() -> {
- checkPartitionId(partitionId);
-
- assert !rebalanceFutureByPartitionId.containsKey(partitionId)
- : IgniteStringFormatter.format("table={}, partitionId={}", getTableName(), partitionId);
-
- CompletableFuture<Void> destroyPartitionFuture = new CompletableFuture<>();
-
- CompletableFuture<Void> previousDestroyPartitionFuture = destroyFutureByPartitionId.putIfAbsent(
- partitionId,
- destroyPartitionFuture
- );
-
- if (previousDestroyPartitionFuture != null) {
- return previousDestroyPartitionFuture;
- }
-
- AbstractPageMemoryMvPartitionStorage partition = mvPartitions.getAndSet(partitionId, null);
-
- if (partition != null) {
- destroyMvPartitionStorage(partition).whenComplete((unused, throwable) -> {
- destroyFutureByPartitionId.remove(partitionId);
-
- if (throwable != null) {
- destroyPartitionFuture.completeExceptionally(throwable);
- } else {
- destroyPartitionFuture.complete(null);
- }
- });
- } else {
- destroyFutureByPartitionId.remove(partitionId).complete(null);
- }
-
- return destroyPartitionFuture;
- });
+ return busy(() -> mvPartitionStorages.destroy(partitionId, this::destroyMvPartitionStorage));
}
@Override
public SortedIndexStorage getOrCreateSortedIndex(int partitionId, UUID indexId) {
return busy(() -> {
- AbstractPageMemoryMvPartitionStorage partitionStorage = getMvPartitionBusy(partitionId);
+ AbstractPageMemoryMvPartitionStorage partitionStorage = mvPartitionStorages.get(partitionId);
if (partitionStorage == null) {
throw new StorageException(createMissingMvPartitionErrorMessage(partitionId));
@@ -276,7 +183,7 @@ public abstract class AbstractPageMemoryTableStorage implements MvTableStorage {
@Override
public HashIndexStorage getOrCreateHashIndex(int partitionId, UUID indexId) {
return busy(() -> {
- AbstractPageMemoryMvPartitionStorage partitionStorage = getMvPartitionBusy(partitionId);
+ AbstractPageMemoryMvPartitionStorage partitionStorage = mvPartitionStorages.get(partitionId);
if (partitionStorage == null) {
throw new StorageException(createMissingMvPartitionErrorMessage(partitionId));
@@ -296,42 +203,16 @@ public abstract class AbstractPageMemoryTableStorage implements MvTableStorage {
stop();
}
- /**
- * Checks that the partition ID is within the scope of the configuration.
- *
- * @param partitionId Partition ID.
- */
- private void checkPartitionId(int partitionId) {
- int partitions = mvPartitions.length();
-
- if (partitionId < 0 || partitionId >= partitions) {
- throw new IllegalArgumentException(IgniteStringFormatter.format(
- "Unable to access partition with id outside of configured range: [table={}, partitionId={}, partitions={}]",
- getTableName(),
- partitionId,
- partitions
- ));
- }
- }
-
private <V> V busy(Supplier<V> supplier) {
return inBusyLock(busyLock, supplier);
}
@Override
public CompletableFuture<Void> startRebalancePartition(int partitionId) {
- return inBusyLock(busyLock, () -> {
- AbstractPageMemoryMvPartitionStorage mvPartitionStorage = getMvPartitionBusy(partitionId);
-
- if (mvPartitionStorage == null) {
- throw new StorageRebalanceException(createMissingMvPartitionErrorMessage(partitionId));
- }
-
- assert !destroyFutureByPartitionId.containsKey(partitionId) : mvPartitionStorage.createStorageInfo();
-
+ return inBusyLock(busyLock, () -> mvPartitionStorages.startRebalace(partitionId, mvPartitionStorage -> {
mvPartitionStorage.startRebalance();
- CompletableFuture<Void> rebalanceFuture = clearStorageAndUpdateDataStructures(mvPartitionStorage)
+ return clearStorageAndUpdateDataStructures(mvPartitionStorage)
.thenAccept(unused ->
mvPartitionStorage.runConsistently(() -> {
mvPartitionStorage.lastAppliedOnRebalance(REBALANCE_IN_PROGRESS, REBALANCE_IN_PROGRESS);
@@ -339,42 +220,23 @@ public abstract class AbstractPageMemoryTableStorage implements MvTableStorage {
return null;
})
);
-
- CompletableFuture<Void> previousRebalanceFuture = rebalanceFutureByPartitionId.putIfAbsent(partitionId, rebalanceFuture);
-
- assert previousRebalanceFuture == null : mvPartitionStorage.createStorageInfo();
-
- return rebalanceFuture;
- });
+ }));
}
@Override
public CompletableFuture<Void> abortRebalancePartition(int partitionId) {
- return inBusyLock(busyLock, () -> {
- AbstractPageMemoryMvPartitionStorage mvPartitionStorage = getMvPartitionBusy(partitionId);
-
- if (mvPartitionStorage == null) {
- throw new StorageRebalanceException(createMissingMvPartitionErrorMessage(partitionId));
- }
-
- CompletableFuture<Void> rebalanceFuture = rebalanceFutureByPartitionId.remove(partitionId);
-
- if (rebalanceFuture == null) {
- return completedFuture(null);
- }
-
- return rebalanceFuture
- .thenCompose(unused -> clearStorageAndUpdateDataStructures(mvPartitionStorage))
- .thenAccept(unused -> {
- mvPartitionStorage.runConsistently(() -> {
- mvPartitionStorage.lastAppliedOnRebalance(0, 0);
+ return inBusyLock(busyLock, () -> mvPartitionStorages.abortRebalance(partitionId, mvPartitionStorage ->
+ clearStorageAndUpdateDataStructures(mvPartitionStorage)
+ .thenAccept(unused -> {
+ mvPartitionStorage.runConsistently(() -> {
+ mvPartitionStorage.lastAppliedOnRebalance(0, 0);
- return null;
- });
+ return null;
+ });
- mvPartitionStorage.completeRebalance();
- });
- });
+ mvPartitionStorage.completeRebalance();
+ })
+ ));
}
@Override
@@ -384,42 +246,24 @@ public abstract class AbstractPageMemoryTableStorage implements MvTableStorage {
long lastAppliedTerm,
byte[] groupConfig
) {
- return inBusyLock(busyLock, () -> {
- AbstractPageMemoryMvPartitionStorage mvPartitionStorage = getMvPartitionBusy(partitionId);
-
- if (mvPartitionStorage == null) {
- throw new StorageRebalanceException(createMissingMvPartitionErrorMessage(partitionId));
- }
-
- CompletableFuture<Void> rebalanceFuture = rebalanceFutureByPartitionId.remove(partitionId);
-
- if (rebalanceFuture == null) {
- throw new StorageRebalanceException("Rebalance for partition did not start: " + mvPartitionStorage.createStorageInfo());
- }
+ return inBusyLock(busyLock, () -> mvPartitionStorages.finishRebalance(partitionId, mvPartitionStorage -> {
+ mvPartitionStorage.runConsistently(() -> {
+ mvPartitionStorage.lastAppliedOnRebalance(lastAppliedIndex, lastAppliedTerm);
- return rebalanceFuture.thenAccept(unused -> {
- mvPartitionStorage.runConsistently(() -> {
- mvPartitionStorage.lastAppliedOnRebalance(lastAppliedIndex, lastAppliedTerm);
+ mvPartitionStorage.committedGroupConfigurationOnRebalance(groupConfig);
- mvPartitionStorage.committedGroupConfigurationOnRebalance(groupConfig);
+ return null;
+ });
- return null;
- });
+ mvPartitionStorage.completeRebalance();
- mvPartitionStorage.completeRebalance();
- });
- });
+ return completedFuture(null);
+ }));
}
@Override
public CompletableFuture<Void> clearPartition(int partitionId) {
- return inBusyLock(busyLock, () -> {
- AbstractPageMemoryMvPartitionStorage mvPartitionStorage = getMvPartitionBusy(partitionId);
-
- if (mvPartitionStorage == null) {
- throw new StorageException(createMissingMvPartitionErrorMessage(partitionId));
- }
-
+ return inBusyLock(busyLock, () -> mvPartitionStorages.clear(partitionId, mvPartitionStorage -> {
try {
mvPartitionStorage.startCleanup();
@@ -432,12 +276,9 @@ public abstract class AbstractPageMemoryTableStorage implements MvTableStorage {
} catch (Throwable t) {
mvPartitionStorage.finishCleanup();
- throw new StorageException(
- IgniteStringFormatter.format("Failed to cleanup storage: [{}]", mvPartitionStorage.createStorageInfo()),
- t
- );
+ throw new StorageException("Failed to cleanup storage: [{}]", t, mvPartitionStorage.createStorageInfo());
}
- });
+ }));
}
/**
diff --git a/modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/PersistentPageMemoryTableStorage.java b/modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/PersistentPageMemoryTableStorage.java
index f732709438..793994842a 100644
--- a/modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/PersistentPageMemoryTableStorage.java
+++ b/modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/PersistentPageMemoryTableStorage.java
@@ -21,7 +21,6 @@ import static org.apache.ignite.internal.pagememory.PageIdAllocator.FLAG_AUX;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
import org.apache.ignite.internal.pagememory.PageMemory;
@@ -108,8 +107,6 @@ public class PersistentPageMemoryTableStorage extends AbstractPageMemoryTableSto
@Override
public PersistentPageMemoryMvPartitionStorage createMvPartitionStorage(int partitionId) {
- waitPartitionToBeDestroyed(partitionId);
-
TableView tableView = tableCfg.value();
GroupPartitionId groupPartitionId = createGroupPartitionId(partitionId);
@@ -552,24 +549,4 @@ public class PersistentPageMemoryTableStorage extends AbstractPageMemoryTableSto
return getOrCreatePartitionMeta(groupPartitionId, filePageStore);
}
-
- private void waitPartitionToBeDestroyed(int partitionId) {
- CompletableFuture<Void> partitionDestroyFuture = destroyFutureByPartitionId.get(partitionId);
-
- if (partitionDestroyFuture != null) {
- try {
- // TODO: IGNITE-18565 We need to return a CompletableFuture
- partitionDestroyFuture.get(10, TimeUnit.SECONDS);
- } catch (Exception e) {
- throw new StorageException(
- IgniteStringFormatter.format(
- "Error waiting for the destruction of the previous version of the partition: [table={}, partitionId={}]",
- getTableName(),
- partitionId
- ),
- e
- );
- }
- }
- }
}
diff --git a/modules/storage-page-memory/src/test/java/org/apache/ignite/internal/storage/pagememory/VolatilePageMemoryMvTableStorageTest.java b/modules/storage-page-memory/src/test/java/org/apache/ignite/internal/storage/pagememory/VolatilePageMemoryMvTableStorageTest.java
index 37fcba4eaa..b26a0128ea 100644
--- a/modules/storage-page-memory/src/test/java/org/apache/ignite/internal/storage/pagememory/VolatilePageMemoryMvTableStorageTest.java
+++ b/modules/storage-page-memory/src/test/java/org/apache/ignite/internal/storage/pagememory/VolatilePageMemoryMvTableStorageTest.java
@@ -76,7 +76,7 @@ public class VolatilePageMemoryMvTableStorageTest extends AbstractMvTableStorage
@Test
void partitionDestructionFreesPartitionPages() throws Exception {
- MvPartitionStorage partitionStorage = tableStorage.getOrCreateMvPartition(0);
+ MvPartitionStorage partitionStorage = getOrCreateMvPartition(0);
insertOneRow(partitionStorage);
@@ -111,7 +111,7 @@ public class VolatilePageMemoryMvTableStorageTest extends AbstractMvTableStorage
@Test
void tableStorageDestructionFreesPartitionsPages() throws Exception {
- MvPartitionStorage partitionStorage = tableStorage.getOrCreateMvPartition(0);
+ MvPartitionStorage partitionStorage = getOrCreateMvPartition(0);
insertOneRow(partitionStorage);
@@ -124,7 +124,7 @@ public class VolatilePageMemoryMvTableStorageTest extends AbstractMvTableStorage
@Test
void partitionDestructionFreesHashIndexPages() throws Exception {
- tableStorage.getOrCreateMvPartition(0);
+ getOrCreateMvPartition(0);
HashIndexStorage indexStorage = tableStorage.getOrCreateHashIndex(0, hashIdx.id());
@@ -171,7 +171,7 @@ public class VolatilePageMemoryMvTableStorageTest extends AbstractMvTableStorage
@Test
void partitionDestructionFreesSortedIndexPages() throws Exception {
- tableStorage.getOrCreateMvPartition(0);
+ getOrCreateMvPartition(0);
SortedIndexStorage indexStorage = tableStorage.getOrCreateSortedIndex(0, sortedIdx.id());
@@ -187,7 +187,7 @@ public class VolatilePageMemoryMvTableStorageTest extends AbstractMvTableStorage
@Test
void tableStorageDestructionFreesHashIndexPages() throws Exception {
- tableStorage.getOrCreateMvPartition(0);
+ getOrCreateMvPartition(0);
HashIndexStorage indexStorage = tableStorage.getOrCreateHashIndex(0, hashIdx.id());
@@ -203,7 +203,7 @@ public class VolatilePageMemoryMvTableStorageTest extends AbstractMvTableStorage
@Test
void tableStorageDestructionFreesSortedIndexPages() throws Exception {
- tableStorage.getOrCreateMvPartition(0);
+ getOrCreateMvPartition(0);
SortedIndexStorage indexStorage = tableStorage.getOrCreateSortedIndex(0, sortedIdx.id());
diff --git a/modules/storage-rocksdb/src/main/java/org/apache/ignite/internal/storage/rocksdb/RocksDbTableStorage.java b/modules/storage-rocksdb/src/main/java/org/apache/ignite/internal/storage/rocksdb/RocksDbTableStorage.java
index 0504e95d75..f7b75eb41f 100644
--- a/modules/storage-rocksdb/src/main/java/org/apache/ignite/internal/storage/rocksdb/RocksDbTableStorage.java
+++ b/modules/storage-rocksdb/src/main/java/org/apache/ignite/internal/storage/rocksdb/RocksDbTableStorage.java
@@ -37,13 +37,11 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
-import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicReferenceArray;
import org.apache.ignite.internal.logger.IgniteLogger;
import org.apache.ignite.internal.logger.Loggers;
import org.apache.ignite.internal.rocksdb.ColumnFamily;
@@ -51,6 +49,7 @@ import org.apache.ignite.internal.rocksdb.flush.RocksDbFlusher;
import org.apache.ignite.internal.schema.configuration.TableConfiguration;
import org.apache.ignite.internal.schema.configuration.TableView;
import org.apache.ignite.internal.schema.configuration.TablesConfiguration;
+import org.apache.ignite.internal.storage.MvPartitionStorage;
import org.apache.ignite.internal.storage.StorageException;
import org.apache.ignite.internal.storage.StorageRebalanceException;
import org.apache.ignite.internal.storage.engine.MvTableStorage;
@@ -62,9 +61,9 @@ import org.apache.ignite.internal.storage.rocksdb.ColumnFamilyUtils.ColumnFamily
import org.apache.ignite.internal.storage.rocksdb.index.RocksDbBinaryTupleComparator;
import org.apache.ignite.internal.storage.rocksdb.index.RocksDbHashIndexStorage;
import org.apache.ignite.internal.storage.rocksdb.index.RocksDbSortedIndexStorage;
+import org.apache.ignite.internal.storage.util.MvPartitionStorages;
import org.apache.ignite.internal.util.IgniteSpinBusyLock;
import org.apache.ignite.internal.util.IgniteUtils;
-import org.apache.ignite.lang.IgniteStringFormatter;
import org.jetbrains.annotations.Nullable;
import org.rocksdb.ColumnFamilyDescriptor;
import org.rocksdb.ColumnFamilyHandle;
@@ -121,7 +120,7 @@ public class RocksDbTableStorage implements MvTableStorage {
private volatile ColumnFamily hashIndexCf;
/** Partition storages. */
- private volatile AtomicReferenceArray<RocksDbMvPartitionStorage> partitions;
+ private volatile MvPartitionStorages<RocksDbMvPartitionStorage> mvPartitionStorages;
/** Hash Index storages by Index IDs. */
private final ConcurrentMap<UUID, HashIndex> hashIndices = new ConcurrentHashMap<>();
@@ -135,10 +134,6 @@ public class RocksDbTableStorage implements MvTableStorage {
/** Prevents double stopping of the component. */
private final AtomicBoolean stopGuard = new AtomicBoolean();
- private final ConcurrentMap<Integer, CompletableFuture<Void>> destroyFutureByPartitionId = new ConcurrentHashMap<>();
-
- private final Set<Integer> rebalancePartitions = ConcurrentHashMap.newKeySet();
-
/**
* Constructor.
*
@@ -287,11 +282,14 @@ public class RocksDbTableStorage implements MvTableStorage {
throw new StorageException("Failed to initialize RocksDB instance", e);
}
- partitions = new AtomicReferenceArray<>(tableCfg.value().partitions());
+ MvPartitionStorages<RocksDbMvPartitionStorage> mvPartitionStorages = new MvPartitionStorages<>(tableCfg.value());
- for (int partId : meta.getPartitionIds()) {
- partitions.set(partId, new RocksDbMvPartitionStorage(this, partId));
+ for (int partitionId : meta.getPartitionIds()) {
+ // There is no need to wait for futures, since there will be no parallel operations yet.
+ mvPartitionStorages.create(partitionId, partId -> new RocksDbMvPartitionStorage(this, partitionId));
}
+
+ this.mvPartitionStorages = mvPartitionStorages;
});
}
@@ -314,7 +312,7 @@ public class RocksDbTableStorage implements MvTableStorage {
TableView tableCfgView = configuration().value();
for (int partitionId = 0; partitionId < tableCfgView.partitions(); partitionId++) {
- RocksDbMvPartitionStorage partition = getMvPartitionBusy(partitionId);
+ RocksDbMvPartitionStorage partition = mvPartitionStorages.get(partitionId);
if (partition != null) {
try {
@@ -360,13 +358,7 @@ public class RocksDbTableStorage implements MvTableStorage {
resources.add(writeOptions);
- for (int i = 0; i < partitions.length(); i++) {
- RocksDbMvPartitionStorage partition = partitions.get(i);
-
- if (partition != null) {
- resources.add(partition::close);
- }
- }
+ mvPartitionStorages.getAllForClose().forEach(mvPartitionStorage -> resources.add(mvPartitionStorage::close));
for (HashIndex index : hashIndices.values()) {
resources.add(index::close);
@@ -404,92 +396,46 @@ public class RocksDbTableStorage implements MvTableStorage {
}
@Override
- public RocksDbMvPartitionStorage getOrCreateMvPartition(int partitionId) throws StorageException {
- return inBusyLock(busyLock, () -> {
- RocksDbMvPartitionStorage partition = getMvPartitionBusy(partitionId);
-
- if (partition != null) {
- return partition;
- }
-
- partition = new RocksDbMvPartitionStorage(this, partitionId);
-
- partitions.set(partitionId, partition);
+ public CompletableFuture<MvPartitionStorage> createMvPartition(int partitionId) throws StorageException {
+ return inBusyLock(busyLock, () -> mvPartitionStorages.create(partitionId, partId -> {
+ RocksDbMvPartitionStorage partition = new RocksDbMvPartitionStorage(this, partitionId);
meta.putPartitionId(partitionId);
return partition;
- });
+ }));
}
@Override
public @Nullable RocksDbMvPartitionStorage getMvPartition(int partitionId) {
- return inBusyLock(busyLock, () -> getMvPartitionBusy(partitionId));
- }
-
- private @Nullable RocksDbMvPartitionStorage getMvPartitionBusy(int partitionId) {
- checkPartitionId(partitionId);
-
- return partitions.get(partitionId);
+ return inBusyLock(busyLock, () -> mvPartitionStorages.get(partitionId));
}
@Override
public CompletableFuture<Void> destroyPartition(int partitionId) {
- return inBusyLock(busyLock, () -> {
- checkPartitionId(partitionId);
-
- assert !rebalancePartitions.contains(partitionId)
- : IgniteStringFormatter.format("table={}, partitionId={}", getTableName(), partitionId);
-
- CompletableFuture<Void> destroyPartitionFuture = new CompletableFuture<>();
-
- CompletableFuture<Void> previousDestroyPartitionFuture = destroyFutureByPartitionId.putIfAbsent(
- partitionId,
- destroyPartitionFuture
- );
-
- if (previousDestroyPartitionFuture != null) {
- return previousDestroyPartitionFuture;
- }
-
- RocksDbMvPartitionStorage mvPartition = partitions.getAndSet(partitionId, null);
-
- if (mvPartition != null) {
- try (WriteBatch writeBatch = new WriteBatch()) {
- mvPartition.close();
-
- // Operation to delete partition data should be fast, since we will write only the range of keys for deletion, and the
- // RocksDB itself will then destroy the data on flash.
- mvPartition.destroyData(writeBatch);
+ return inBusyLock(busyLock, () -> mvPartitionStorages.destroy(partitionId, mvPartitionStorage -> {
+ try (WriteBatch writeBatch = new WriteBatch()) {
+ mvPartitionStorage.close();
- for (HashIndex hashIndex : hashIndices.values()) {
- hashIndex.destroy(partitionId, writeBatch);
- }
+ // Operation to delete partition data should be fast, since we will write only the range of keys for deletion, and the
+ // RocksDB itself will then destroy the data on flash.
+ mvPartitionStorage.destroyData(writeBatch);
- for (SortedIndex sortedIndex : sortedIndices.values()) {
- sortedIndex.destroy(partitionId, writeBatch);
- }
+ for (HashIndex hashIndex : hashIndices.values()) {
+ hashIndex.destroy(partitionId, writeBatch);
+ }
- db.write(writeOptions, writeBatch);
+ for (SortedIndex sortedIndex : sortedIndices.values()) {
+ sortedIndex.destroy(partitionId, writeBatch);
+ }
- CompletableFuture<?> flushFuture = awaitFlush(true);
+ db.write(writeOptions, writeBatch);
- flushFuture.whenComplete((unused, throwable) -> {
- if (throwable == null) {
- destroyFutureByPartitionId.remove(partitionId).complete(null);
- } else {
- destroyFutureByPartitionId.remove(partitionId).completeExceptionally(throwable);
- }
- });
- } catch (Throwable throwable) {
- destroyFutureByPartitionId.remove(partitionId).completeExceptionally(throwable);
- }
- } else {
- destroyFutureByPartitionId.remove(partitionId).complete(null);
+ return awaitFlush(true);
+ } catch (RocksDBException e) {
+ throw new StorageException("Error when destroying storage: [{}]", e, mvPartitionStorages.createStorageInfo(partitionId));
}
-
- return destroyPartitionFuture;
- });
+ }));
}
@Override
@@ -497,7 +443,7 @@ public class RocksDbTableStorage implements MvTableStorage {
return inBusyLock(busyLock, () -> {
SortedIndex storages = sortedIndices.computeIfAbsent(indexId, this::createSortedIndex);
- RocksDbMvPartitionStorage partitionStorage = getMvPartitionBusy(partitionId);
+ RocksDbMvPartitionStorage partitionStorage = mvPartitionStorages.get(partitionId);
if (partitionStorage == null) {
throw new StorageException(createMissingMvPartitionErrorMessage(partitionId));
@@ -533,7 +479,7 @@ public class RocksDbTableStorage implements MvTableStorage {
return new HashIndex(hashIndexCf, indexDescriptor);
});
- RocksDbMvPartitionStorage partitionStorage = getMvPartitionBusy(partitionId);
+ RocksDbMvPartitionStorage partitionStorage = mvPartitionStorages.get(partitionId);
if (partitionStorage == null) {
throw new StorageException(String.format("Partition ID %d does not exist", partitionId));
@@ -576,22 +522,6 @@ public class RocksDbTableStorage implements MvTableStorage {
return false;
}
- /**
- * Checks that a passed partition id is within the proper bounds.
- *
- * @param partitionId Partition id.
- */
- private void checkPartitionId(int partitionId) {
- if (partitionId < 0 || partitionId >= partitions.length()) {
- throw new IllegalArgumentException(IgniteStringFormatter.format(
- "Unable to access partition with id outside of configured range [table={}, partitionId={}, partitions={}]",
- getTableName(),
- partitionId,
- partitions.length()
- ));
- }
- }
-
/**
* Returns a list of Column Families' names that belong to a RocksDB instance in the given path.
*
@@ -675,15 +605,7 @@ public class RocksDbTableStorage implements MvTableStorage {
@Override
public CompletableFuture<Void> startRebalancePartition(int partitionId) {
- return inBusyLock(busyLock, () -> {
- RocksDbMvPartitionStorage mvPartitionStorage = getMvPartitionBusy(partitionId);
-
- if (mvPartitionStorage == null) {
- throw new StorageRebalanceException(createMissingMvPartitionErrorMessage(partitionId));
- }
-
- assert !destroyFutureByPartitionId.containsKey(partitionId) : mvPartitionStorage.createStorageInfo();
-
+ return inBusyLock(busyLock, () -> mvPartitionStorages.startRebalace(partitionId, mvPartitionStorage -> {
try (WriteBatch writeBatch = new WriteBatch()) {
mvPartitionStorage.startRebalance(writeBatch);
@@ -692,38 +614,20 @@ public class RocksDbTableStorage implements MvTableStorage {
db.write(writeOptions, writeBatch);
- boolean added = rebalancePartitions.add(partitionId);
-
- assert added : mvPartitionStorage.createStorageInfo();
-
return completedFuture(null);
} catch (RocksDBException e) {
throw new StorageRebalanceException(
- IgniteStringFormatter.format(
- "Error when trying to start rebalancing storage: [{}]",
- mvPartitionStorage.createStorageInfo()
- ),
- e
+ "Error when trying to start rebalancing storage: [{}]",
+ e,
+ mvPartitionStorage.createStorageInfo()
);
}
- });
+ }));
}
@Override
public CompletableFuture<Void> abortRebalancePartition(int partitionId) {
- return inBusyLock(busyLock, () -> {
- RocksDbMvPartitionStorage mvPartitionStorage = getMvPartitionBusy(partitionId);
-
- if (mvPartitionStorage == null) {
- throw new StorageRebalanceException(createMissingMvPartitionErrorMessage(partitionId));
- }
-
- boolean removed = rebalancePartitions.remove(partitionId);
-
- if (!removed) {
- return completedFuture(null);
- }
-
+ return inBusyLock(busyLock, () -> mvPartitionStorages.abortRebalance(partitionId, mvPartitionStorage -> {
try (WriteBatch writeBatch = new WriteBatch()) {
mvPartitionStorage.abortRebalance(writeBatch);
@@ -734,15 +638,12 @@ public class RocksDbTableStorage implements MvTableStorage {
return completedFuture(null);
} catch (RocksDBException e) {
- throw new StorageRebalanceException(
- IgniteStringFormatter.format(
- "Error when trying to abort rebalancing storage: [{}]",
- mvPartitionStorage.createStorageInfo()
- ),
- e
+ throw new StorageRebalanceException("Error when trying to abort rebalancing storage: [{}]",
+ e,
+ mvPartitionStorage.createStorageInfo()
);
}
- });
+ }));
}
@Override
@@ -752,19 +653,7 @@ public class RocksDbTableStorage implements MvTableStorage {
long lastAppliedTerm,
byte[] groupConfig
) {
- return inBusyLock(busyLock, () -> {
- RocksDbMvPartitionStorage mvPartitionStorage = getMvPartitionBusy(partitionId);
-
- if (mvPartitionStorage == null) {
- throw new StorageRebalanceException(createMissingMvPartitionErrorMessage(partitionId));
- }
-
- boolean removed = rebalancePartitions.remove(partitionId);
-
- if (!removed) {
- throw new StorageRebalanceException("Rebalance for partition did not start: " + mvPartitionStorage.createStorageInfo());
- }
-
+ return inBusyLock(busyLock, () -> mvPartitionStorages.finishRebalance(partitionId, mvPartitionStorage -> {
try (WriteBatch writeBatch = new WriteBatch()) {
mvPartitionStorage.finishRebalance(writeBatch, lastAppliedIndex, lastAppliedTerm, groupConfig);
@@ -775,26 +664,17 @@ public class RocksDbTableStorage implements MvTableStorage {
return completedFuture(null);
} catch (RocksDBException e) {
- throw new StorageRebalanceException(
- IgniteStringFormatter.format(
- "Error when trying to finish rebalancing storage: [{}]",
- mvPartitionStorage.createStorageInfo()
- ),
- e
+ throw new StorageRebalanceException("Error when trying to finish rebalancing storage: [{}]",
+ e,
+ mvPartitionStorage.createStorageInfo()
);
}
- });
+ }));
}
@Override
public CompletableFuture<Void> clearPartition(int partitionId) {
- return inBusyLock(busyLock, () -> {
- RocksDbMvPartitionStorage mvPartitionStorage = getMvPartitionBusy(partitionId);
-
- if (mvPartitionStorage == null) {
- throw new StorageException(createMissingMvPartitionErrorMessage(partitionId));
- }
-
+ return inBusyLock(busyLock, () -> mvPartitionStorages.clear(partitionId, mvPartitionStorage -> {
List<RocksDbHashIndexStorage> hashIndexStorages = getHashIndexStorages(partitionId);
List<RocksDbSortedIndexStorage> sortedIndexStorages = getSortedIndexStorages(partitionId);
@@ -810,23 +690,17 @@ public class RocksDbTableStorage implements MvTableStorage {
}
db.write(writeOptions, writeBatch);
+
+ return completedFuture(null);
} catch (RocksDBException e) {
- throw new StorageException(
- IgniteStringFormatter.format(
- "Error when trying to cleanup storage: [{}]",
- mvPartitionStorage.createStorageInfo()
- ),
- e
- );
+ throw new StorageException("Error when trying to cleanup storage: [{}]", e, mvPartitionStorage.createStorageInfo());
} finally {
mvPartitionStorage.finishCleanup();
hashIndexStorages.forEach(RocksDbHashIndexStorage::finishCleanup);
sortedIndexStorages.forEach(RocksDbSortedIndexStorage::finishCleanup);
}
-
- return completedFuture(null);
- });
+ }));
}
/**
diff --git a/modules/storage-rocksdb/src/test/java/org/apache/ignite/internal/storage/rocksdb/RocksDbMvTableStorageTest.java b/modules/storage-rocksdb/src/test/java/org/apache/ignite/internal/storage/rocksdb/RocksDbMvTableStorageTest.java
index 568a6e3c34..d8793c779c 100644
--- a/modules/storage-rocksdb/src/test/java/org/apache/ignite/internal/storage/rocksdb/RocksDbMvTableStorageTest.java
+++ b/modules/storage-rocksdb/src/test/java/org/apache/ignite/internal/storage/rocksdb/RocksDbMvTableStorageTest.java
@@ -68,13 +68,13 @@ public class RocksDbMvTableStorageTest extends AbstractMvTableStorageTest {
UUID txId = UUID.randomUUID();
- MvPartitionStorage partitionStorage0 = tableStorage.getOrCreateMvPartition(PARTITION_ID_0);
+ MvPartitionStorage partitionStorage0 = getOrCreateMvPartition(PARTITION_ID_0);
RowId rowId0 = new RowId(PARTITION_ID_0);
partitionStorage0.runConsistently(() -> partitionStorage0.addWrite(rowId0, testData, txId, UUID.randomUUID(), 0));
- MvPartitionStorage partitionStorage1 = tableStorage.getOrCreateMvPartition(PARTITION_ID_1);
+ MvPartitionStorage partitionStorage1 = getOrCreateMvPartition(PARTITION_ID_1);
RowId rowId1 = new RowId(PARTITION_ID_1);
@@ -86,7 +86,7 @@ public class RocksDbMvTableStorageTest extends AbstractMvTableStorageTest {
((RocksDbTableStorage) tableStorage).awaitFlush(true);
assertThat(tableStorage.getMvPartition(PARTITION_ID_0), is(nullValue()));
- assertThat(tableStorage.getOrCreateMvPartition(PARTITION_ID_0).read(rowId0, HybridTimestamp.MAX_VALUE).binaryRow(),
+ assertThat(getOrCreateMvPartition(PARTITION_ID_0).read(rowId0, HybridTimestamp.MAX_VALUE).binaryRow(),
is(nullValue()));
assertThat(unwrap(tableStorage.getMvPartition(PARTITION_ID_1).read(rowId1, HybridTimestamp.MAX_VALUE).binaryRow()),
is(equalTo(unwrap(testData))));
@@ -101,7 +101,7 @@ public class RocksDbMvTableStorageTest extends AbstractMvTableStorageTest {
UUID txId = UUID.randomUUID();
- MvPartitionStorage partitionStorage0 = tableStorage.getOrCreateMvPartition(PARTITION_ID);
+ MvPartitionStorage partitionStorage0 = getOrCreateMvPartition(PARTITION_ID);
RowId rowId0 = new RowId(PARTITION_ID);
diff --git a/modules/storage-rocksdb/src/test/java/org/apache/ignite/internal/storage/rocksdb/RocksDbStorageEngineTest.java b/modules/storage-rocksdb/src/test/java/org/apache/ignite/internal/storage/rocksdb/RocksDbStorageEngineTest.java
index 2681f096f9..a999b1be36 100644
--- a/modules/storage-rocksdb/src/test/java/org/apache/ignite/internal/storage/rocksdb/RocksDbStorageEngineTest.java
+++ b/modules/storage-rocksdb/src/test/java/org/apache/ignite/internal/storage/rocksdb/RocksDbStorageEngineTest.java
@@ -17,6 +17,7 @@
package org.apache.ignite.internal.storage.rocksdb;
+import static org.apache.ignite.internal.storage.BaseMvStoragesTest.getOrCreateMvPartition;
import static org.apache.ignite.internal.storage.rocksdb.configuration.schema.RocksDbStorageEngineConfigurationSchema.DEFAULT_DATA_REGION_NAME;
import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -76,7 +77,7 @@ public class RocksDbStorageEngineTest {
assertThat(dataStorageConfig.dataRegion().value(), is(DEFAULT_DATA_REGION_NAME));
- table.getOrCreateMvPartition(1);
+ getOrCreateMvPartition(table, 1);
} finally {
table.stop();
}
@@ -105,7 +106,7 @@ public class RocksDbStorageEngineTest {
assertThat(dataStorageConfig.dataRegion().value(), is(customRegionName));
- table.getOrCreateMvPartition(1);
+ getOrCreateMvPartition(table, 1);
} finally {
table.stop();
}
diff --git a/modules/table/src/integrationTest/java/org/apache/ignite/distributed/ItTablePersistenceTest.java b/modules/table/src/integrationTest/java/org/apache/ignite/distributed/ItTablePersistenceTest.java
index edf9d6d758..ac567a00b2 100644
--- a/modules/table/src/integrationTest/java/org/apache/ignite/distributed/ItTablePersistenceTest.java
+++ b/modules/table/src/integrationTest/java/org/apache/ignite/distributed/ItTablePersistenceTest.java
@@ -20,8 +20,10 @@ package org.apache.ignite.distributed;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.apache.ignite.internal.table.distributed.replicator.PartitionReplicaListener.hybridTimestamp;
import static org.apache.ignite.internal.table.distributed.replicator.PartitionReplicaListener.tablePartitionId;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
import static org.apache.ignite.internal.util.ArrayUtils.asList;
import static org.apache.ignite.internal.util.IgniteUtils.closeAll;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
@@ -35,6 +37,7 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
@@ -363,7 +366,7 @@ public class ItTablePersistenceTest extends ItAbstractListenerSnapshotTest<Parti
mvTableStorages.put(index, mvTableStorage);
closeables.add(mvTableStorage::close);
- MvPartitionStorage mvPartitionStorage = mvTableStorage.getOrCreateMvPartition(0);
+ MvPartitionStorage mvPartitionStorage = getOrCreateMvPartition(mvTableStorage, 0);
mvPartitionStorages.put(index, mvPartitionStorage);
closeables.add(mvPartitionStorage::close);
@@ -406,4 +409,18 @@ public class ItTablePersistenceTest extends ItAbstractListenerSnapshotTest<Parti
return new Row(SCHEMA, rowBuilder.build());
}
+
+ private static MvPartitionStorage getOrCreateMvPartition(MvTableStorage tableStorage, int partitionId) {
+ MvPartitionStorage mvPartition = tableStorage.getMvPartition(partitionId);
+
+ if (mvPartition != null) {
+ return mvPartition;
+ }
+
+ CompletableFuture<MvPartitionStorage> createMvPartitionFuture = tableStorage.createMvPartition(0);
+
+ assertThat(createMvPartitionFuture, willCompleteSuccessfully());
+
+ return createMvPartitionFuture.join();
+ }
}
diff --git a/modules/table/src/main/java/org/apache/ignite/internal/table/distributed/TableManager.java b/modules/table/src/main/java/org/apache/ignite/internal/table/distributed/TableManager.java
index 22ba3f815e..b038f64cd7 100644
--- a/modules/table/src/main/java/org/apache/ignite/internal/table/distributed/TableManager.java
+++ b/modules/table/src/main/java/org/apache/ignite/internal/table/distributed/TableManager.java
@@ -2181,22 +2181,24 @@ public class TableManager extends Producer<TableEvent, TableEventParameters> imp
*/
// TODO: IGNITE-18619 Maybe we should wait here to create indexes, if you add now, then the tests start to hang
private CompletableFuture<PartitionStorages> getOrCreatePartitionStorages(TableImpl table, int partitionId) {
- return CompletableFuture
- .supplyAsync(() -> {
- MvPartitionStorage mvPartitionStorage = table.internalTable().storage().getOrCreateMvPartition(partitionId);
- TxStateStorage txStateStorage = table.internalTable().txStateStorage().getOrCreateTxStateStorage(partitionId);
+ InternalTable internalTable = table.internalTable();
+
+ MvPartitionStorage mvPartition = internalTable.storage().getMvPartition(partitionId);
+
+ return (mvPartition != null ? completedFuture(mvPartition) : internalTable.storage().createMvPartition(partitionId))
+ .thenComposeAsync(mvPartitionStorage -> {
+ TxStateStorage txStateStorage = internalTable.txStateStorage().getOrCreateTxStateStorage(partitionId);
if (mvPartitionStorage.persistedIndex() == MvPartitionStorage.REBALANCE_IN_PROGRESS
|| txStateStorage.persistedIndex() == TxStateStorage.REBALANCE_IN_PROGRESS) {
return allOf(
- table.internalTable().storage().clearPartition(partitionId),
+ internalTable.storage().clearPartition(partitionId),
txStateStorage.clear()
).thenApply(unused -> new PartitionStorages(mvPartitionStorage, txStateStorage));
} else {
return completedFuture(new PartitionStorages(mvPartitionStorage, txStateStorage));
}
- }, ioExecutor)
- .thenCompose(Function.identity());
+ }, ioExecutor);
}
/**
diff --git a/modules/table/src/test/java/org/apache/ignite/internal/table/distributed/raft/snapshot/PartitionAccessImplTest.java b/modules/table/src/test/java/org/apache/ignite/internal/table/distributed/raft/snapshot/PartitionAccessImplTest.java
index 617b4ef8de..84e328c9d3 100644
--- a/modules/table/src/test/java/org/apache/ignite/internal/table/distributed/raft/snapshot/PartitionAccessImplTest.java
+++ b/modules/table/src/test/java/org/apache/ignite/internal/table/distributed/raft/snapshot/PartitionAccessImplTest.java
@@ -17,6 +17,8 @@
package org.apache.ignite.internal.table.distributed.raft.snapshot;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
@@ -27,6 +29,7 @@ import static org.mockito.Mockito.verify;
import java.util.List;
import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
import org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
import org.apache.ignite.internal.configuration.testframework.InjectConfiguration;
import org.apache.ignite.internal.hlc.HybridTimestamp;
@@ -34,6 +37,7 @@ import org.apache.ignite.internal.schema.BinaryRow;
import org.apache.ignite.internal.schema.configuration.TablesConfiguration;
import org.apache.ignite.internal.storage.MvPartitionStorage;
import org.apache.ignite.internal.storage.RowId;
+import org.apache.ignite.internal.storage.engine.MvTableStorage;
import org.apache.ignite.internal.storage.impl.TestMvTableStorage;
import org.apache.ignite.internal.table.distributed.TableSchemaAwareIndexStorage;
import org.apache.ignite.internal.tx.storage.state.TxStateStorage;
@@ -56,7 +60,7 @@ public class PartitionAccessImplTest {
TestMvTableStorage mvTableStorage = new TestMvTableStorage(tablesConfig.tables().get("foo"), tablesConfig);
TestTxStateTableStorage txStateTableStorage = new TestTxStateTableStorage();
- MvPartitionStorage mvPartitionStorage = mvTableStorage.getOrCreateMvPartition(TEST_PARTITION_ID);
+ MvPartitionStorage mvPartitionStorage = createMvPartition(mvTableStorage, TEST_PARTITION_ID);
TxStateStorage txStateStorage = txStateTableStorage.getOrCreateTxStateStorage(TEST_PARTITION_ID);
PartitionAccess partitionAccess = new PartitionAccessImpl(
@@ -96,7 +100,7 @@ public class PartitionAccessImplTest {
TestMvTableStorage mvTableStorage = new TestMvTableStorage(tablesConfig.tables().get("foo"), tablesConfig);
TestTxStateTableStorage txStateTableStorage = new TestTxStateTableStorage();
- MvPartitionStorage mvPartitionStorage = mvTableStorage.getOrCreateMvPartition(TEST_PARTITION_ID);
+ MvPartitionStorage mvPartitionStorage = createMvPartition(mvTableStorage, TEST_PARTITION_ID);
TxStateStorage txStateStorage = txStateTableStorage.getOrCreateTxStateStorage(TEST_PARTITION_ID);
PartitionAccess partitionAccess = new PartitionAccessImpl(
@@ -135,7 +139,7 @@ public class PartitionAccessImplTest {
void testAddWrite() {
TestMvTableStorage mvTableStorage = new TestMvTableStorage(tablesConfig.tables().get("foo"), tablesConfig);
- MvPartitionStorage mvPartitionStorage = mvTableStorage.getOrCreateMvPartition(TEST_PARTITION_ID);
+ MvPartitionStorage mvPartitionStorage = createMvPartition(mvTableStorage, TEST_PARTITION_ID);
TableSchemaAwareIndexStorage indexStorage = mock(TableSchemaAwareIndexStorage.class);
@@ -173,7 +177,7 @@ public class PartitionAccessImplTest {
void testAddWriteCommitted() {
TestMvTableStorage mvTableStorage = new TestMvTableStorage(tablesConfig.tables().get("foo"), tablesConfig);
- MvPartitionStorage mvPartitionStorage = mvTableStorage.getOrCreateMvPartition(TEST_PARTITION_ID);
+ MvPartitionStorage mvPartitionStorage = createMvPartition(mvTableStorage, TEST_PARTITION_ID);
TableSchemaAwareIndexStorage indexStorage = mock(TableSchemaAwareIndexStorage.class);
@@ -204,4 +208,12 @@ public class PartitionAccessImplTest {
verify(indexStorage, never()).put(eq(binaryRow), eq(rowId));
}
+
+ private static MvPartitionStorage createMvPartition(MvTableStorage tableStorage, int partitionId) {
+ CompletableFuture<MvPartitionStorage> createMvPartitionFuture = tableStorage.createMvPartition(partitionId);
+
+ assertThat(createMvPartitionFuture, willCompleteSuccessfully());
+
+ return createMvPartitionFuture.join();
+ }
}
diff --git a/modules/table/src/test/java/org/apache/ignite/internal/table/distributed/raft/snapshot/incoming/IncomingSnapshotCopierTest.java b/modules/table/src/test/java/org/apache/ignite/internal/table/distributed/raft/snapshot/incoming/IncomingSnapshotCopierTest.java
index f09ffbd30e..006eddf52b 100644
--- a/modules/table/src/test/java/org/apache/ignite/internal/table/distributed/raft/snapshot/incoming/IncomingSnapshotCopierTest.java
+++ b/modules/table/src/test/java/org/apache/ignite/internal/table/distributed/raft/snapshot/incoming/IncomingSnapshotCopierTest.java
@@ -160,7 +160,7 @@ public class IncomingSnapshotCopierTest {
MvTableStorage incomingMvTableStorage = spy(new TestMvTableStorage(getTableConfig(), tablesConfig));
TxStateTableStorage incomingTxStateTableStorage = spy(new TestTxStateTableStorage());
- incomingMvTableStorage.getOrCreateMvPartition(TEST_PARTITION);
+ assertThat(incomingMvTableStorage.createMvPartition(TEST_PARTITION), willCompleteSuccessfully());
incomingTxStateTableStorage.getOrCreateTxStateStorage(TEST_PARTITION);
MessagingService messagingService = messagingServiceForSuccessScenario(outgoingMvPartitionStorage,
@@ -417,7 +417,7 @@ public class IncomingSnapshotCopierTest {
MvTableStorage incomingMvTableStorage = spy(new TestMvTableStorage(tablesConfig.tables().get("foo"), tablesConfig));
TxStateTableStorage incomingTxStateTableStorage = spy(new TestTxStateTableStorage());
- incomingMvTableStorage.getOrCreateMvPartition(TEST_PARTITION);
+ assertThat(incomingMvTableStorage.createMvPartition(TEST_PARTITION), willCompleteSuccessfully());
incomingTxStateTableStorage.getOrCreateTxStateStorage(TEST_PARTITION);
CountDownLatch networkInvokeLatch = new CountDownLatch(1);
@@ -473,7 +473,7 @@ public class IncomingSnapshotCopierTest {
MvTableStorage incomingMvTableStorage = spy(new TestMvTableStorage(getTableConfig(), tablesConfig));
TxStateTableStorage incomingTxStateTableStorage = spy(new TestTxStateTableStorage());
- incomingMvTableStorage.getOrCreateMvPartition(TEST_PARTITION);
+ assertThat(incomingMvTableStorage.createMvPartition(TEST_PARTITION), willCompleteSuccessfully());
incomingTxStateTableStorage.getOrCreateTxStateStorage(TEST_PARTITION);
MessagingService messagingService = messagingServiceForSuccessScenario(outgoingMvPartitionStorage,
@@ -541,7 +541,7 @@ public class IncomingSnapshotCopierTest {
MvTableStorage incomingMvTableStorage = spy(new TestMvTableStorage(getTableConfig(), tablesConfig));
TxStateTableStorage incomingTxStateTableStorage = spy(new TestTxStateTableStorage());
- incomingMvTableStorage.getOrCreateMvPartition(TEST_PARTITION);
+ assertThat(incomingMvTableStorage.createMvPartition(TEST_PARTITION), willCompleteSuccessfully());
incomingTxStateTableStorage.getOrCreateTxStateStorage(TEST_PARTITION);
MessagingService messagingService = messagingServiceForSuccessScenario(outgoingMvPartitionStorage,