diff --git a/driver/src/main/java/org/neo4j/driver/internal/cluster/loadbalancing/RoundRobinLoadBalancingStrategy.java b/driver/src/main/java/org/neo4j/driver/internal/cluster/loadbalancing/RoundRobinLoadBalancingStrategy.java deleted file mode 100644 index cbedc75870..0000000000 --- a/driver/src/main/java/org/neo4j/driver/internal/cluster/loadbalancing/RoundRobinLoadBalancingStrategy.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 2002-2019 "Neo4j," - * Neo4j Sweden AB [http://neo4j.com] - * - * This file is part of Neo4j. - * - * Licensed 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.neo4j.driver.internal.cluster.loadbalancing; - -import org.neo4j.driver.internal.BoltServerAddress; -import org.neo4j.driver.Logger; -import org.neo4j.driver.Logging; - -/** - * Load balancing strategy that selects addresses in round-robin fashion. It maintains separate indices for readers and - * writers. - */ -public class RoundRobinLoadBalancingStrategy implements LoadBalancingStrategy -{ - private static final String LOGGER_NAME = RoundRobinLoadBalancingStrategy.class.getSimpleName(); - - private final RoundRobinArrayIndex readersIndex = new RoundRobinArrayIndex(); - private final RoundRobinArrayIndex writersIndex = new RoundRobinArrayIndex(); - - private final Logger log; - - public RoundRobinLoadBalancingStrategy( Logging logging ) - { - this.log = logging.getLog( LOGGER_NAME ); - } - - @Override - public BoltServerAddress selectReader( BoltServerAddress[] knownReaders ) - { - return select( knownReaders, readersIndex, "reader" ); - } - - @Override - public BoltServerAddress selectWriter( BoltServerAddress[] knownWriters ) - { - return select( knownWriters, writersIndex, "writer" ); - } - - private BoltServerAddress select( BoltServerAddress[] addresses, RoundRobinArrayIndex roundRobinIndex, - String addressType ) - { - int length = addresses.length; - if ( length == 0 ) - { - log.trace( "Unable to select %s, no known addresses given", addressType ); - return null; - } - - int index = roundRobinIndex.next( length ); - BoltServerAddress address = addresses[index]; - log.trace( "Selected %s with address: '%s'", addressType, address ); - return address; - } -} diff --git a/driver/src/main/java/org/neo4j/driver/internal/reactive/InternalRxSession.java b/driver/src/main/java/org/neo4j/driver/internal/reactive/InternalRxSession.java index da04440ede..915f51f6fc 100644 --- a/driver/src/main/java/org/neo4j/driver/internal/reactive/InternalRxSession.java +++ b/driver/src/main/java/org/neo4j/driver/internal/reactive/InternalRxSession.java @@ -98,30 +98,30 @@ private Publisher beginTransaction( AccessMode mode, TransactionC } @Override - public Publisher readTransaction( RxTransactionWork> work ) + public Publisher readTransaction( RxTransactionWork> work ) { return readTransaction( work, TransactionConfig.empty() ); } @Override - public Publisher readTransaction( RxTransactionWork> work, TransactionConfig config ) + public Publisher readTransaction( RxTransactionWork> work, TransactionConfig config ) { return runTransaction( AccessMode.READ, work, config ); } @Override - public Publisher writeTransaction( RxTransactionWork> work ) + public Publisher writeTransaction( RxTransactionWork> work ) { return writeTransaction( work, TransactionConfig.empty() ); } @Override - public Publisher writeTransaction( RxTransactionWork> work, TransactionConfig config ) + public Publisher writeTransaction( RxTransactionWork> work, TransactionConfig config ) { return runTransaction( AccessMode.WRITE, work, config ); } - private Publisher runTransaction( AccessMode mode, RxTransactionWork> work, TransactionConfig config ) + private Publisher runTransaction( AccessMode mode, RxTransactionWork> work, TransactionConfig config ) { Flux repeatableWork = Flux.usingWhen( beginTransaction( mode, config ), work::execute, RxTransaction::commit, ( tx, error ) -> tx.rollback(), null ); diff --git a/driver/src/main/java/org/neo4j/driver/reactive/RxSession.java b/driver/src/main/java/org/neo4j/driver/reactive/RxSession.java index 3959e5b4c7..3753bcb45a 100644 --- a/driver/src/main/java/org/neo4j/driver/reactive/RxSession.java +++ b/driver/src/main/java/org/neo4j/driver/reactive/RxSession.java @@ -83,7 +83,7 @@ public interface RxSession extends RxStatementRunner * publisher can be completed exceptionally if given work or commit fails. * */ - Publisher readTransaction( RxTransactionWork> work ); + Publisher readTransaction( RxTransactionWork> work ); /** * Execute given unit of reactive work in a {@link AccessMode#READ read} reactive transaction with @@ -106,7 +106,7 @@ public interface RxSession extends RxStatementRunner * publisher can be completed exceptionally if given work or commit fails. * */ - Publisher readTransaction( RxTransactionWork> work, TransactionConfig config ); + Publisher readTransaction( RxTransactionWork> work, TransactionConfig config ); /** * Execute given unit of reactive work in a {@link AccessMode#WRITE write} reactive transaction. @@ -127,7 +127,7 @@ public interface RxSession extends RxStatementRunner * publisher can be completed exceptionally if given work or commit fails. * */ - Publisher writeTransaction( RxTransactionWork> work ); + Publisher writeTransaction( RxTransactionWork> work ); /** * Execute given unit of reactive work in a {@link AccessMode#WRITE write} reactive transaction with @@ -150,7 +150,7 @@ public interface RxSession extends RxStatementRunner * publisher can be completed exceptionally if given work or commit fails. * */ - Publisher writeTransaction( RxTransactionWork> work, TransactionConfig config ); + Publisher writeTransaction( RxTransactionWork> work, TransactionConfig config ); /** * Run a statement with parameters in an auto-commit transaction with specified {@link TransactionConfig} and return a reactive result stream. diff --git a/driver/src/test/java/org/neo4j/driver/internal/cluster/loadbalancing/RoundRobinLoadBalancingStrategyTest.java b/driver/src/test/java/org/neo4j/driver/internal/cluster/loadbalancing/RoundRobinLoadBalancingStrategyTest.java deleted file mode 100644 index 9702ece45e..0000000000 --- a/driver/src/test/java/org/neo4j/driver/internal/cluster/loadbalancing/RoundRobinLoadBalancingStrategyTest.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright (c) 2002-2019 "Neo4j," - * Neo4j Sweden AB [http://neo4j.com] - * - * This file is part of Neo4j. - * - * Licensed 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.neo4j.driver.internal.cluster.loadbalancing; - -import org.junit.jupiter.api.Test; - -import org.neo4j.driver.internal.BoltServerAddress; -import org.neo4j.driver.Logger; -import org.neo4j.driver.Logging; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.ArgumentMatchers.startsWith; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.neo4j.driver.internal.util.ClusterCompositionUtil.A; -import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING; - -class RoundRobinLoadBalancingStrategyTest -{ - private final RoundRobinLoadBalancingStrategy strategy = new RoundRobinLoadBalancingStrategy( DEV_NULL_LOGGING ); - - @Test - void shouldHandleEmptyReadersArray() - { - assertNull( strategy.selectReader( new BoltServerAddress[0] ) ); - } - - @Test - void shouldHandleEmptyWritersArray() - { - assertNull( strategy.selectWriter( new BoltServerAddress[0] ) ); - } - - @Test - void shouldHandleSingleReader() - { - BoltServerAddress address = new BoltServerAddress( "reader", 9999 ); - - assertEquals( address, strategy.selectReader( new BoltServerAddress[]{address} ) ); - } - - @Test - void shouldHandleSingleWriter() - { - BoltServerAddress address = new BoltServerAddress( "writer", 9999 ); - - assertEquals( address, strategy.selectWriter( new BoltServerAddress[]{address} ) ); - } - - @Test - void shouldReturnReadersInRoundRobinOrder() - { - BoltServerAddress address1 = new BoltServerAddress( "server-1", 1 ); - BoltServerAddress address2 = new BoltServerAddress( "server-2", 2 ); - BoltServerAddress address3 = new BoltServerAddress( "server-3", 3 ); - BoltServerAddress address4 = new BoltServerAddress( "server-4", 4 ); - - BoltServerAddress[] readers = {address1, address2, address3, address4}; - - assertEquals( address1, strategy.selectReader( readers ) ); - assertEquals( address2, strategy.selectReader( readers ) ); - assertEquals( address3, strategy.selectReader( readers ) ); - assertEquals( address4, strategy.selectReader( readers ) ); - - assertEquals( address1, strategy.selectReader( readers ) ); - assertEquals( address2, strategy.selectReader( readers ) ); - assertEquals( address3, strategy.selectReader( readers ) ); - assertEquals( address4, strategy.selectReader( readers ) ); - } - - @Test - void shouldReturnWriterInRoundRobinOrder() - { - BoltServerAddress address1 = new BoltServerAddress( "server-1", 1 ); - BoltServerAddress address2 = new BoltServerAddress( "server-2", 2 ); - BoltServerAddress address3 = new BoltServerAddress( "server-3", 3 ); - - BoltServerAddress[] writers = {address1, address2, address3}; - - assertEquals( address1, strategy.selectWriter( writers ) ); - assertEquals( address2, strategy.selectWriter( writers ) ); - assertEquals( address3, strategy.selectWriter( writers ) ); - - assertEquals( address1, strategy.selectWriter( writers ) ); - assertEquals( address2, strategy.selectWriter( writers ) ); - assertEquals( address3, strategy.selectWriter( writers ) ); - } - - @Test - void shouldTraceLogWhenNoAddressSelected() - { - Logging logging = mock( Logging.class ); - Logger logger = mock( Logger.class ); - when( logging.getLog( anyString() ) ).thenReturn( logger ); - - RoundRobinLoadBalancingStrategy strategy = new RoundRobinLoadBalancingStrategy( logging ); - - strategy.selectReader( new BoltServerAddress[0] ); - strategy.selectWriter( new BoltServerAddress[0] ); - - verify( logger ).trace( startsWith( "Unable to select" ), eq( "reader" ) ); - verify( logger ).trace( startsWith( "Unable to select" ), eq( "writer" ) ); - } - - @Test - void shouldTraceLogSelectedAddress() - { - Logging logging = mock( Logging.class ); - Logger logger = mock( Logger.class ); - when( logging.getLog( anyString() ) ).thenReturn( logger ); - - RoundRobinLoadBalancingStrategy strategy = new RoundRobinLoadBalancingStrategy( logging ); - - strategy.selectReader( new BoltServerAddress[]{A} ); - strategy.selectWriter( new BoltServerAddress[]{A} ); - - verify( logger ).trace( startsWith( "Selected" ), eq( "reader" ), eq( A ) ); - verify( logger ).trace( startsWith( "Selected" ), eq( "writer" ), eq( A ) ); - } -}