-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decouple load balancing logic from address set
Round-robin load balancing was previously coupled with the address set implementation and made it hard to use different load balancing algorithm. This commit turns RoundRobinAddressSet into a simple concurrent set of addresses and moves load balancing logic into a separate component that takes list of available addresses and returns one that should be used next. This allows easier implementation of new load balancing algorithms. Rediscovery procedure will now not use load balancing and query routers in a known order (which is randomized by the database).
- Loading branch information
Showing
17 changed files
with
614 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
driver/src/main/java/org/neo4j/driver/internal/cluster/LoadBalancingStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.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; | ||
|
||
import org.neo4j.driver.internal.net.BoltServerAddress; | ||
|
||
public interface LoadBalancingStrategy | ||
{ | ||
BoltServerAddress selectReader( BoltServerAddress[] knownReaders ); | ||
|
||
BoltServerAddress selectWriter( BoltServerAddress[] knownWriters ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
driver/src/main/java/org/neo4j/driver/internal/cluster/RoundRobinArrayIndex.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.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; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class RoundRobinArrayIndex | ||
{ | ||
private final AtomicInteger offset; | ||
|
||
RoundRobinArrayIndex() | ||
{ | ||
this( 0 ); | ||
} | ||
|
||
// only for testing | ||
RoundRobinArrayIndex( int initialOffset ) | ||
{ | ||
this.offset = new AtomicInteger( initialOffset ); | ||
} | ||
|
||
public int next( int arrayLength ) | ||
{ | ||
if ( arrayLength == 0 ) | ||
{ | ||
return -1; | ||
} | ||
|
||
int nextOffset; | ||
while ( (nextOffset = offset.getAndIncrement()) < 0 ) | ||
{ | ||
// overflow, try resetting back to zero | ||
offset.compareAndSet( nextOffset + 1, 0 ); | ||
} | ||
return nextOffset % arrayLength; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
driver/src/main/java/org/neo4j/driver/internal/cluster/RoundRobinLoadBalancingStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.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; | ||
|
||
import org.neo4j.driver.internal.net.BoltServerAddress; | ||
|
||
public class RoundRobinLoadBalancingStrategy implements LoadBalancingStrategy | ||
{ | ||
private final RoundRobinArrayIndex readersIndex = new RoundRobinArrayIndex(); | ||
private final RoundRobinArrayIndex writersIndex = new RoundRobinArrayIndex(); | ||
|
||
@Override | ||
public BoltServerAddress selectReader( BoltServerAddress[] knownReaders ) | ||
{ | ||
return select( knownReaders, readersIndex ); | ||
} | ||
|
||
@Override | ||
public BoltServerAddress selectWriter( BoltServerAddress[] knownWriters ) | ||
{ | ||
return select( knownWriters, writersIndex ); | ||
} | ||
|
||
private BoltServerAddress select( BoltServerAddress[] addresses, RoundRobinArrayIndex roundRobinIndex ) | ||
{ | ||
int length = addresses.length; | ||
if ( length == 0 ) | ||
{ | ||
return null; | ||
} | ||
int index = roundRobinIndex.next( length ); | ||
return addresses[index]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.