Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wait for current leader to become available in RaftReplicator
Previously we have used a logic where we send to last known leader. In this commit RaftReplicator will wait indefinitely for a valid leader and will not try to resend the request to an out of date member.
- Loading branch information
Showing
4 changed files
with
211 additions
and
15 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...-clustering/src/main/java/org/neo4j/causalclustering/core/replication/LeaderProvider.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,56 @@ | ||
/* | ||
* Copyright (c) 2002-2018 "Neo4j," | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j Enterprise Edition. The included source | ||
* code can be redistributed and/or modified under the terms of the | ||
* GNU AFFERO GENERAL PUBLIC LICENSE Version 3 | ||
* (http://www.fsf.org/licensing/licenses/agpl-3.0.html) with the | ||
* Commons Clause, as found in the associated LICENSE.txt file. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* Neo4j object code can be licensed independently from the source | ||
* under separate terms from the AGPL. Inquiries can be directed to: | ||
* licensing@neo4j.com | ||
* | ||
* More information is also available at: | ||
* https://neo4j.com/licensing/ | ||
*/ | ||
package org.neo4j.causalclustering.core.replication; | ||
|
||
import org.neo4j.causalclustering.identity.MemberId; | ||
|
||
class LeaderProvider | ||
{ | ||
private MemberId currentLeader; | ||
|
||
synchronized MemberId awaitLeader() throws InterruptedException | ||
{ | ||
if ( currentLeader == null ) | ||
{ | ||
while ( currentLeader == null ) | ||
{ | ||
wait( ); | ||
} | ||
} | ||
return currentLeader; | ||
} | ||
|
||
synchronized void setLeader( MemberId leader ) | ||
{ | ||
this.currentLeader = leader; | ||
if ( currentLeader != null ) | ||
{ | ||
notifyAll(); | ||
} | ||
} | ||
|
||
MemberId currentLeader() | ||
{ | ||
return currentLeader; | ||
} | ||
} |
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
111 changes: 111 additions & 0 deletions
111
...stering/src/test/java/org/neo4j/causalclustering/core/replication/LeaderProviderTest.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,111 @@ | ||
/* | ||
* Copyright (c) 2002-2018 "Neo4j," | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j Enterprise Edition. The included source | ||
* code can be redistributed and/or modified under the terms of the | ||
* GNU AFFERO GENERAL PUBLIC LICENSE Version 3 | ||
* (http://www.fsf.org/licensing/licenses/agpl-3.0.html) with the | ||
* Commons Clause, as found in the associated LICENSE.txt file. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* Neo4j object code can be licensed independently from the source | ||
* under separate terms from the AGPL. Inquiries can be directed to: | ||
* licensing@neo4j.com | ||
* | ||
* More information is also available at: | ||
* https://neo4j.com/licensing/ | ||
*/ | ||
package org.neo4j.causalclustering.core.replication; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import org.neo4j.causalclustering.identity.MemberId; | ||
import org.neo4j.util.concurrent.Futures; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class LeaderProviderTest | ||
{ | ||
|
||
private static final MemberId MEMBER_ID = new MemberId( UUID.randomUUID() ); | ||
private final ExecutorService executorService = Executors.newCachedThreadPool(); | ||
private final LeaderProvider leaderProvider = new LeaderProvider(); | ||
|
||
@Before | ||
public void before() | ||
{ | ||
leaderProvider.setLeader( null ); | ||
} | ||
|
||
@Test | ||
public void shouldGiveCurrentLeaderIfAvailable() throws InterruptedException | ||
{ | ||
leaderProvider.setLeader( MEMBER_ID ); | ||
assertEquals( leaderProvider.currentLeader(), MEMBER_ID ); | ||
assertEquals( leaderProvider.awaitLeader(), MEMBER_ID ); | ||
} | ||
|
||
@Test | ||
public void shouldWaitForNonNullValue() throws InterruptedException, ExecutionException, TimeoutException | ||
{ | ||
// given | ||
int threads = 3; | ||
assertNull( leaderProvider.currentLeader() ); | ||
|
||
// when | ||
List<Future<MemberId>> futures = new ArrayList<>(); | ||
for ( int i = 0; i < threads; i++ ) | ||
{ | ||
Future<MemberId> interrupted = executorService.submit( getCurrentLeader() ); | ||
futures.add( interrupted ); | ||
} | ||
|
||
// then | ||
Future<List<MemberId>> combine = Futures.combine( futures ); | ||
Thread.sleep( 100 ); | ||
assertFalse( combine.isDone() ); | ||
|
||
// when | ||
leaderProvider.setLeader( MEMBER_ID ); | ||
|
||
List<MemberId> memberIds = combine.get( 5, TimeUnit.SECONDS ); | ||
|
||
// then | ||
assertTrue( memberIds.stream().allMatch( memberId -> memberId.equals( MEMBER_ID ) ) ); | ||
} | ||
|
||
private Callable<MemberId> getCurrentLeader() | ||
{ | ||
return () -> | ||
{ | ||
try | ||
{ | ||
return leaderProvider.awaitLeader(); | ||
} | ||
catch ( InterruptedException e ) | ||
{ | ||
throw new RuntimeException( "Interrupted" ); | ||
} | ||
}; | ||
} | ||
} |
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