sashapolo commented on code in PR #1832: URL: https://github.com/apache/ignite-3/pull/1832#discussion_r1145799333
########## modules/metastorage/src/testFixtures/java/org/apache/ignite/internal/metastorage/impl/StandaloneMetaStorageManager.java: ########## @@ -0,0 +1,135 @@ +/* + * 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.metastorage.impl; + +import static java.util.Collections.singleton; +import static java.util.concurrent.CompletableFuture.completedFuture; +import static java.util.concurrent.CompletableFuture.failedFuture; + +import java.io.Serializable; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicReference; +import org.apache.ignite.internal.cluster.management.ClusterManagementGroupManager; +import org.apache.ignite.internal.cluster.management.topology.api.LogicalTopologyService; +import org.apache.ignite.internal.metastorage.server.KeyValueStorage; +import org.apache.ignite.internal.metastorage.server.SimpleInMemoryKeyValueStorage; +import org.apache.ignite.internal.metastorage.server.raft.MetaStorageListener; +import org.apache.ignite.internal.raft.Command; +import org.apache.ignite.internal.raft.RaftManager; +import org.apache.ignite.internal.raft.ReadCommand; +import org.apache.ignite.internal.raft.WriteCommand; +import org.apache.ignite.internal.raft.service.CommandClosure; +import org.apache.ignite.internal.raft.service.RaftGroupService; +import org.apache.ignite.internal.util.IgniteSpinBusyLock; +import org.apache.ignite.internal.vault.VaultManager; +import org.apache.ignite.network.ClusterNode; +import org.apache.ignite.network.ClusterService; +import org.apache.ignite.network.NetworkAddress; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.TestOnly; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; + +/** + * MetaStorageManager dummy implementation. + */ +@TestOnly +public class StandaloneMetaStorageManager extends MetaStorageManagerImpl { + private static final String TEST_NODE_NAME = "test"; + + /** + * Creates standalone MetaStorage manager for provided VaultManager. + */ + public static StandaloneMetaStorageManager create(VaultManager vaultMgr) { + return create(vaultMgr, new SimpleInMemoryKeyValueStorage(TEST_NODE_NAME)); + } + + /** + * Creates standalone MetaStorage manager for provided VaultManager. + */ + public static StandaloneMetaStorageManager create(VaultManager vaultMgr, KeyValueStorage storage) { + return new StandaloneMetaStorageManager( + vaultMgr, + Mockito.mock(ClusterService.class), + Mockito.mock(ClusterManagementGroupManager.class), + Mockito.mock(LogicalTopologyService.class), + Mockito.mock(RaftManager.class), + storage + ); + } + + private final KeyValueStorage keyValueStorage; + + /** + * The constructor. + * + * @param vaultMgr Vault manager. + * @param clusterService Cluster network service. + * @param cmgMgr Cluster management service Manager. + * @param logicalTopologyService Logical topology service. + * @param raftMgr Raft manager. + * @param storage Storage. This component owns this resource and will manage its lifecycle. + */ + public StandaloneMetaStorageManager(VaultManager vaultMgr, ClusterService clusterService, ClusterManagementGroupManager cmgMgr, + LogicalTopologyService logicalTopologyService, RaftManager raftMgr, KeyValueStorage storage) { + super(vaultMgr, clusterService, cmgMgr, logicalTopologyService, raftMgr, storage); + + keyValueStorage = storage; + } + + @Override + public void start() { + keyValueStorage.start(); + + MetaStorageListener metaStorageListener = new MetaStorageListener(keyValueStorage); + + RaftGroupService raftGroupService = Mockito.mock(RaftGroupService.class); + Mockito.when(raftGroupService.run(ArgumentMatchers.any())) + .thenAnswer(invocation -> runCommand(invocation.getArgument(0), metaStorageListener)); + + var localNode = new ClusterNode(TEST_NODE_NAME, TEST_NODE_NAME, new NetworkAddress("localhost", 10000)); Review Comment: `localNode` should be a constructor parameter ########## modules/metastorage/src/testFixtures/java/org/apache/ignite/internal/metastorage/impl/StandaloneMetaStorageManager.java: ########## @@ -0,0 +1,135 @@ +/* + * 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.metastorage.impl; + +import static java.util.Collections.singleton; +import static java.util.concurrent.CompletableFuture.completedFuture; +import static java.util.concurrent.CompletableFuture.failedFuture; + +import java.io.Serializable; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicReference; +import org.apache.ignite.internal.cluster.management.ClusterManagementGroupManager; +import org.apache.ignite.internal.cluster.management.topology.api.LogicalTopologyService; +import org.apache.ignite.internal.metastorage.server.KeyValueStorage; +import org.apache.ignite.internal.metastorage.server.SimpleInMemoryKeyValueStorage; +import org.apache.ignite.internal.metastorage.server.raft.MetaStorageListener; +import org.apache.ignite.internal.raft.Command; +import org.apache.ignite.internal.raft.RaftManager; +import org.apache.ignite.internal.raft.ReadCommand; +import org.apache.ignite.internal.raft.WriteCommand; +import org.apache.ignite.internal.raft.service.CommandClosure; +import org.apache.ignite.internal.raft.service.RaftGroupService; +import org.apache.ignite.internal.util.IgniteSpinBusyLock; +import org.apache.ignite.internal.vault.VaultManager; +import org.apache.ignite.network.ClusterNode; +import org.apache.ignite.network.ClusterService; +import org.apache.ignite.network.NetworkAddress; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.TestOnly; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; + +/** + * MetaStorageManager dummy implementation. + */ +@TestOnly +public class StandaloneMetaStorageManager extends MetaStorageManagerImpl { + private static final String TEST_NODE_NAME = "test"; + + /** + * Creates standalone MetaStorage manager for provided VaultManager. + */ + public static StandaloneMetaStorageManager create(VaultManager vaultMgr) { + return create(vaultMgr, new SimpleInMemoryKeyValueStorage(TEST_NODE_NAME)); + } + + /** + * Creates standalone MetaStorage manager for provided VaultManager. + */ + public static StandaloneMetaStorageManager create(VaultManager vaultMgr, KeyValueStorage storage) { + return new StandaloneMetaStorageManager( + vaultMgr, + Mockito.mock(ClusterService.class), + Mockito.mock(ClusterManagementGroupManager.class), + Mockito.mock(LogicalTopologyService.class), + Mockito.mock(RaftManager.class), + storage + ); + } + + private final KeyValueStorage keyValueStorage; + + /** + * The constructor. + * + * @param vaultMgr Vault manager. + * @param clusterService Cluster network service. + * @param cmgMgr Cluster management service Manager. + * @param logicalTopologyService Logical topology service. + * @param raftMgr Raft manager. + * @param storage Storage. This component owns this resource and will manage its lifecycle. + */ + public StandaloneMetaStorageManager(VaultManager vaultMgr, ClusterService clusterService, ClusterManagementGroupManager cmgMgr, + LogicalTopologyService logicalTopologyService, RaftManager raftMgr, KeyValueStorage storage) { + super(vaultMgr, clusterService, cmgMgr, logicalTopologyService, raftMgr, storage); + + keyValueStorage = storage; + } + + @Override + public void start() { + keyValueStorage.start(); + + MetaStorageListener metaStorageListener = new MetaStorageListener(keyValueStorage); + + RaftGroupService raftGroupService = Mockito.mock(RaftGroupService.class); + Mockito.when(raftGroupService.run(ArgumentMatchers.any())) + .thenAnswer(invocation -> runCommand(invocation.getArgument(0), metaStorageListener)); + + var localNode = new ClusterNode(TEST_NODE_NAME, TEST_NODE_NAME, new NetworkAddress("localhost", 10000)); + + MetaStorageServiceImpl service = new MetaStorageServiceImpl(raftGroupService, new IgniteSpinBusyLock(), localNode); + + metaStorageServiceFuture().complete(service); Review Comment: This looks like a hack, I would like to propose a more clean solution using mocks, this way we won't have to complete the future explicitly, but it will be completed with our mock RaftGroupService: ``` private static RaftManager mockRaftManager() { ArgumentCaptor<RaftGroupListener> listenerCaptor = ArgumentCaptor.forClass(RaftGroupListener.class); RaftManager raftManager = mock(RaftManager.class); RaftGroupService raftGroupService = mock(RaftGroupService.class); try { when(raftManager.startRaftGroupNode(any(), any(), listenerCaptor.capture(), any())) .thenReturn(completedFuture(raftGroupService)); } catch (NodeStoppingException e) { throw new RuntimeException(e); } when(raftGroupService.run(any())) .thenAnswer(invocation -> { Command command = invocation.getArgument(0); RaftGroupListener listener = listenerCaptor.getValue(); return runCommand(command, listener); }); return raftManager; } ``` ########## modules/metastorage/src/testFixtures/java/org/apache/ignite/internal/metastorage/impl/StandaloneMetaStorageManager.java: ########## @@ -0,0 +1,135 @@ +/* + * 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.metastorage.impl; + +import static java.util.Collections.singleton; +import static java.util.concurrent.CompletableFuture.completedFuture; +import static java.util.concurrent.CompletableFuture.failedFuture; + +import java.io.Serializable; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicReference; +import org.apache.ignite.internal.cluster.management.ClusterManagementGroupManager; +import org.apache.ignite.internal.cluster.management.topology.api.LogicalTopologyService; +import org.apache.ignite.internal.metastorage.server.KeyValueStorage; +import org.apache.ignite.internal.metastorage.server.SimpleInMemoryKeyValueStorage; +import org.apache.ignite.internal.metastorage.server.raft.MetaStorageListener; +import org.apache.ignite.internal.raft.Command; +import org.apache.ignite.internal.raft.RaftManager; +import org.apache.ignite.internal.raft.ReadCommand; +import org.apache.ignite.internal.raft.WriteCommand; +import org.apache.ignite.internal.raft.service.CommandClosure; +import org.apache.ignite.internal.raft.service.RaftGroupService; +import org.apache.ignite.internal.util.IgniteSpinBusyLock; +import org.apache.ignite.internal.vault.VaultManager; +import org.apache.ignite.network.ClusterNode; +import org.apache.ignite.network.ClusterService; +import org.apache.ignite.network.NetworkAddress; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.TestOnly; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; + +/** + * MetaStorageManager dummy implementation. + */ +@TestOnly +public class StandaloneMetaStorageManager extends MetaStorageManagerImpl { + private static final String TEST_NODE_NAME = "test"; + + /** + * Creates standalone MetaStorage manager for provided VaultManager. + */ + public static StandaloneMetaStorageManager create(VaultManager vaultMgr) { + return create(vaultMgr, new SimpleInMemoryKeyValueStorage(TEST_NODE_NAME)); + } + + /** + * Creates standalone MetaStorage manager for provided VaultManager. + */ + public static StandaloneMetaStorageManager create(VaultManager vaultMgr, KeyValueStorage storage) { + return new StandaloneMetaStorageManager( + vaultMgr, + Mockito.mock(ClusterService.class), + Mockito.mock(ClusterManagementGroupManager.class), + Mockito.mock(LogicalTopologyService.class), + Mockito.mock(RaftManager.class), + storage + ); + } + + private final KeyValueStorage keyValueStorage; + + /** + * The constructor. + * + * @param vaultMgr Vault manager. + * @param clusterService Cluster network service. + * @param cmgMgr Cluster management service Manager. + * @param logicalTopologyService Logical topology service. + * @param raftMgr Raft manager. + * @param storage Storage. This component owns this resource and will manage its lifecycle. + */ + public StandaloneMetaStorageManager(VaultManager vaultMgr, ClusterService clusterService, ClusterManagementGroupManager cmgMgr, + LogicalTopologyService logicalTopologyService, RaftManager raftMgr, KeyValueStorage storage) { + super(vaultMgr, clusterService, cmgMgr, logicalTopologyService, raftMgr, storage); + + keyValueStorage = storage; + } + + @Override + public void start() { + keyValueStorage.start(); + + MetaStorageListener metaStorageListener = new MetaStorageListener(keyValueStorage); + + RaftGroupService raftGroupService = Mockito.mock(RaftGroupService.class); + Mockito.when(raftGroupService.run(ArgumentMatchers.any())) + .thenAnswer(invocation -> runCommand(invocation.getArgument(0), metaStorageListener)); + + var localNode = new ClusterNode(TEST_NODE_NAME, TEST_NODE_NAME, new NetworkAddress("localhost", 10000)); + + MetaStorageServiceImpl service = new MetaStorageServiceImpl(raftGroupService, new IgniteSpinBusyLock(), localNode); + + metaStorageServiceFuture().complete(service); + } + + private static CompletableFuture<Serializable> runCommand(Command command, MetaStorageListener listener) { + AtomicReference<CompletableFuture<Serializable>> resRef = new AtomicReference<>(); Review Comment: There's no need to use an `AtomicReference`, you can use a `CompletableFuture` instead: ``` CompletableFuture<Serializable> future = new CompletableFuture<>(); CommandClosure<? extends Command> closure = new CommandClosure<>() { @Override public Command command() { return command; } @Override public void result(@Nullable Serializable res) { if (res instanceof Throwable) { future.completeExceptionally((Throwable) res); } else { future.complete(res); } } }; if (command instanceof ReadCommand) { listener.onRead(singleton((CommandClosure<ReadCommand>) closure).iterator()); } else { listener.onWrite(singleton((CommandClosure<WriteCommand>) closure).iterator()); } return future; ``` ########## modules/metastorage/src/testFixtures/java/org/apache/ignite/internal/metastorage/impl/StandaloneMetaStorageManager.java: ########## @@ -0,0 +1,135 @@ +/* + * 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.metastorage.impl; + +import static java.util.Collections.singleton; +import static java.util.concurrent.CompletableFuture.completedFuture; +import static java.util.concurrent.CompletableFuture.failedFuture; + +import java.io.Serializable; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicReference; +import org.apache.ignite.internal.cluster.management.ClusterManagementGroupManager; +import org.apache.ignite.internal.cluster.management.topology.api.LogicalTopologyService; +import org.apache.ignite.internal.metastorage.server.KeyValueStorage; +import org.apache.ignite.internal.metastorage.server.SimpleInMemoryKeyValueStorage; +import org.apache.ignite.internal.metastorage.server.raft.MetaStorageListener; +import org.apache.ignite.internal.raft.Command; +import org.apache.ignite.internal.raft.RaftManager; +import org.apache.ignite.internal.raft.ReadCommand; +import org.apache.ignite.internal.raft.WriteCommand; +import org.apache.ignite.internal.raft.service.CommandClosure; +import org.apache.ignite.internal.raft.service.RaftGroupService; +import org.apache.ignite.internal.util.IgniteSpinBusyLock; +import org.apache.ignite.internal.vault.VaultManager; +import org.apache.ignite.network.ClusterNode; +import org.apache.ignite.network.ClusterService; +import org.apache.ignite.network.NetworkAddress; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.TestOnly; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; + +/** + * MetaStorageManager dummy implementation. + */ +@TestOnly +public class StandaloneMetaStorageManager extends MetaStorageManagerImpl { + private static final String TEST_NODE_NAME = "test"; + + /** + * Creates standalone MetaStorage manager for provided VaultManager. + */ + public static StandaloneMetaStorageManager create(VaultManager vaultMgr) { + return create(vaultMgr, new SimpleInMemoryKeyValueStorage(TEST_NODE_NAME)); + } + + /** + * Creates standalone MetaStorage manager for provided VaultManager. + */ + public static StandaloneMetaStorageManager create(VaultManager vaultMgr, KeyValueStorage storage) { + return new StandaloneMetaStorageManager( + vaultMgr, + Mockito.mock(ClusterService.class), Review Comment: Let's statically import all Mockito method, that looks cleaner ########## modules/distribution-zones/src/test/java/org/apache/ignite/internal/distributionzones/DistributionZoneManagerConfigurationChangesTest.java: ########## @@ -356,10 +211,15 @@ void testZoneDeleteDoNotRemoveMetaStorageKey() throws Exception { assertDataNodesForZone(1, nodes, keyValueStorage); } - private void mockVaultZonesLogicalTopologyKey(Set<String> nodes) { - byte[] newLogicalTopology = toBytes(nodes); + private static TablesConfiguration mockTablesConfiguration() { Review Comment: I'm pretty sure this mocking is not needed at all, we can replace it with `@InjectConfiguration` ########## modules/metastorage/src/testFixtures/java/org/apache/ignite/internal/metastorage/impl/StandaloneMetaStorageManager.java: ########## @@ -0,0 +1,135 @@ +/* + * 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.metastorage.impl; + +import static java.util.Collections.singleton; +import static java.util.concurrent.CompletableFuture.completedFuture; +import static java.util.concurrent.CompletableFuture.failedFuture; + +import java.io.Serializable; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicReference; +import org.apache.ignite.internal.cluster.management.ClusterManagementGroupManager; +import org.apache.ignite.internal.cluster.management.topology.api.LogicalTopologyService; +import org.apache.ignite.internal.metastorage.server.KeyValueStorage; +import org.apache.ignite.internal.metastorage.server.SimpleInMemoryKeyValueStorage; +import org.apache.ignite.internal.metastorage.server.raft.MetaStorageListener; +import org.apache.ignite.internal.raft.Command; +import org.apache.ignite.internal.raft.RaftManager; +import org.apache.ignite.internal.raft.ReadCommand; +import org.apache.ignite.internal.raft.WriteCommand; +import org.apache.ignite.internal.raft.service.CommandClosure; +import org.apache.ignite.internal.raft.service.RaftGroupService; +import org.apache.ignite.internal.util.IgniteSpinBusyLock; +import org.apache.ignite.internal.vault.VaultManager; +import org.apache.ignite.network.ClusterNode; +import org.apache.ignite.network.ClusterService; +import org.apache.ignite.network.NetworkAddress; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.TestOnly; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; + +/** + * MetaStorageManager dummy implementation. + */ +@TestOnly +public class StandaloneMetaStorageManager extends MetaStorageManagerImpl { + private static final String TEST_NODE_NAME = "test"; + + /** + * Creates standalone MetaStorage manager for provided VaultManager. + */ + public static StandaloneMetaStorageManager create(VaultManager vaultMgr) { + return create(vaultMgr, new SimpleInMemoryKeyValueStorage(TEST_NODE_NAME)); + } + + /** + * Creates standalone MetaStorage manager for provided VaultManager. + */ + public static StandaloneMetaStorageManager create(VaultManager vaultMgr, KeyValueStorage storage) { + return new StandaloneMetaStorageManager( + vaultMgr, + Mockito.mock(ClusterService.class), + Mockito.mock(ClusterManagementGroupManager.class), + Mockito.mock(LogicalTopologyService.class), + Mockito.mock(RaftManager.class), + storage + ); + } + + private final KeyValueStorage keyValueStorage; + + /** + * The constructor. + * + * @param vaultMgr Vault manager. + * @param clusterService Cluster network service. + * @param cmgMgr Cluster management service Manager. + * @param logicalTopologyService Logical topology service. + * @param raftMgr Raft manager. + * @param storage Storage. This component owns this resource and will manage its lifecycle. + */ + public StandaloneMetaStorageManager(VaultManager vaultMgr, ClusterService clusterService, ClusterManagementGroupManager cmgMgr, + LogicalTopologyService logicalTopologyService, RaftManager raftMgr, KeyValueStorage storage) { + super(vaultMgr, clusterService, cmgMgr, logicalTopologyService, raftMgr, storage); + + keyValueStorage = storage; + } + + @Override + public void start() { + keyValueStorage.start(); Review Comment: You will also need to override the `stop` method in order to close the `keyValueStorage` ########## modules/metastorage/src/testFixtures/java/org/apache/ignite/internal/metastorage/impl/StandaloneMetaStorageManager.java: ########## @@ -0,0 +1,135 @@ +/* + * 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.metastorage.impl; + +import static java.util.Collections.singleton; +import static java.util.concurrent.CompletableFuture.completedFuture; +import static java.util.concurrent.CompletableFuture.failedFuture; + +import java.io.Serializable; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicReference; +import org.apache.ignite.internal.cluster.management.ClusterManagementGroupManager; +import org.apache.ignite.internal.cluster.management.topology.api.LogicalTopologyService; +import org.apache.ignite.internal.metastorage.server.KeyValueStorage; +import org.apache.ignite.internal.metastorage.server.SimpleInMemoryKeyValueStorage; +import org.apache.ignite.internal.metastorage.server.raft.MetaStorageListener; +import org.apache.ignite.internal.raft.Command; +import org.apache.ignite.internal.raft.RaftManager; +import org.apache.ignite.internal.raft.ReadCommand; +import org.apache.ignite.internal.raft.WriteCommand; +import org.apache.ignite.internal.raft.service.CommandClosure; +import org.apache.ignite.internal.raft.service.RaftGroupService; +import org.apache.ignite.internal.util.IgniteSpinBusyLock; +import org.apache.ignite.internal.vault.VaultManager; +import org.apache.ignite.network.ClusterNode; +import org.apache.ignite.network.ClusterService; +import org.apache.ignite.network.NetworkAddress; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.TestOnly; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; + +/** + * MetaStorageManager dummy implementation. + */ +@TestOnly +public class StandaloneMetaStorageManager extends MetaStorageManagerImpl { + private static final String TEST_NODE_NAME = "test"; + + /** + * Creates standalone MetaStorage manager for provided VaultManager. + */ + public static StandaloneMetaStorageManager create(VaultManager vaultMgr) { + return create(vaultMgr, new SimpleInMemoryKeyValueStorage(TEST_NODE_NAME)); + } + + /** + * Creates standalone MetaStorage manager for provided VaultManager. + */ + public static StandaloneMetaStorageManager create(VaultManager vaultMgr, KeyValueStorage storage) { + return new StandaloneMetaStorageManager( + vaultMgr, + Mockito.mock(ClusterService.class), + Mockito.mock(ClusterManagementGroupManager.class), + Mockito.mock(LogicalTopologyService.class), + Mockito.mock(RaftManager.class), + storage + ); + } + + private final KeyValueStorage keyValueStorage; + + /** + * The constructor. + * + * @param vaultMgr Vault manager. + * @param clusterService Cluster network service. + * @param cmgMgr Cluster management service Manager. + * @param logicalTopologyService Logical topology service. + * @param raftMgr Raft manager. + * @param storage Storage. This component owns this resource and will manage its lifecycle. + */ + public StandaloneMetaStorageManager(VaultManager vaultMgr, ClusterService clusterService, ClusterManagementGroupManager cmgMgr, Review Comment: Shouldn't this constructor be `private` since we have a factory method? ########## modules/distribution-zones/src/test/java/org/apache/ignite/internal/distributionzones/DistributionZoneManagerConfigurationChangesTest.java: ########## @@ -115,178 +98,46 @@ public void setUp() { DistributionZonesConfiguration zonesConfiguration = clusterCfgMgr.configurationRegistry() .getConfiguration(DistributionZonesConfiguration.KEY); - MetaStorageManager metaStorageManager = mock(MetaStorageManager.class); - - when(metaStorageManager.appliedRevision(any())).thenReturn(completedFuture(0L)); + vaultMgr = new VaultManager(new InMemoryVaultService()); LogicalTopologyService logicalTopologyService = mock(LogicalTopologyService.class); + doNothing().when(logicalTopologyService).addEventListener(any()); + when(logicalTopologyService.logicalTopologyOnLeader()).thenReturn(completedFuture(new LogicalTopologySnapshot(1, Set.of()))); - vaultMgr = mock(VaultManager.class); - - when(vaultMgr.get(any())).thenReturn(completedFuture(null)); - - TablesConfiguration tablesConfiguration = mock(TablesConfiguration.class); - - NamedConfigurationTree<TableConfiguration, TableView, TableChange> tables = mock(NamedConfigurationTree.class); - - when(tablesConfiguration.tables()).thenReturn(tables); - - NamedListView<TableView> value = mock(NamedListView.class); + keyValueStorage = spy(new SimpleInMemoryKeyValueStorage("test")); - when(tables.value()).thenReturn(value); + metaStorageManager = StandaloneMetaStorageManager.create(vaultMgr, keyValueStorage); - when(value.namedListKeys()).thenReturn(new ArrayList<>()); + TablesConfiguration tablesConfiguration = mockTablesConfiguration(); distributionZoneManager = new DistributionZoneManager( zonesConfiguration, tablesConfiguration, metaStorageManager, logicalTopologyService, vaultMgr, - "node" + "test" ); - clusterCfgMgr.start(); - - doNothing().when(logicalTopologyService).addEventListener(any()); - - when(logicalTopologyService.logicalTopologyOnLeader()).thenReturn(completedFuture(new LogicalTopologySnapshot(1, Set.of()))); - - mockVaultZonesLogicalTopologyKey(nodes); + // Mock logical topology for distribution zone. + // vaultMgr.put(zonesLogicalTopologyKey(), toBytes(nodes)); + keyValueStorage.put(zonesLogicalTopologyVersionKey().bytes(), longToBytes(1)); Review Comment: I like the previous approach with mocking `LogicalTopologyService` more ########## modules/distribution-zones/src/test/java/org/apache/ignite/internal/distributionzones/DistributionZoneManagerConfigurationChangesTest.java: ########## @@ -332,6 +183,8 @@ void testSeveralZoneCreationsUpdatesTriggerKey() throws Exception { void testDataNodesNotPropagatedAfterZoneCreation() throws Exception { keyValueStorage.put(zonesChangeTriggerKey(1).bytes(), longToBytes(100)); + clearInvocations(keyValueStorage); Review Comment: Why is this call needed? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
