-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Autoclose fetched replica connections when checking consistency (#125)
- Loading branch information
1 parent
19cb640
commit 5e2eade
Showing
7 changed files
with
154 additions
and
8 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
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
7 changes: 7 additions & 0 deletions
7
...main/java/com/atlassian/db/replica/api/exception/ConnectionCouldNotBeClosedException.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,7 @@ | ||
package com.atlassian.db.replica.api.exception; | ||
|
||
public final class ConnectionCouldNotBeClosedException extends RuntimeException { | ||
public ConnectionCouldNotBeClosedException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
src/test/java/com/atlassian/db/replica/api/AuroraMultiReplicaConsistencyTest.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,108 @@ | ||
package com.atlassian.db.replica.api; | ||
|
||
import com.atlassian.db.replica.internal.aurora.AuroraReplicaNode; | ||
import com.atlassian.db.replica.spi.ReplicaConnectionPerUrlProvider; | ||
import com.atlassian.db.replica.spi.ReplicaConsistency; | ||
import com.atlassian.db.replica.spi.SuppliedCache; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.Collection; | ||
import java.util.LinkedList; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class AuroraMultiReplicaConsistencyTest { | ||
@Mock | ||
private Connection supplierConnection; | ||
@Mock | ||
private SuppliedCache<Collection<Database>> discoveredReplicasCache; | ||
|
||
private AuroraMultiReplicaConsistency sut; | ||
|
||
private Collection<Connection> mockReplicaConnections(int count) { | ||
Collection<Connection> replicas = new LinkedList<>(); | ||
Collection<Database> nodes = new LinkedList<>(); | ||
|
||
ReplicaConnectionPerUrlProvider replicaConnectionPerUrlProvider = (url) -> () -> { | ||
Connection replica = mock(Connection.class); | ||
replicas.add(replica); | ||
return replica; | ||
}; | ||
|
||
for (int i = 0; i < count; i++) { | ||
AuroraReplicaNode node = new AuroraReplicaNode( | ||
String.valueOf(i), | ||
replicaConnectionPerUrlProvider.getReplicaConnectionProvider(null) | ||
); | ||
nodes.add(node); | ||
} | ||
when(discoveredReplicasCache.get(any())).thenReturn(Optional.of(nodes)); | ||
|
||
return replicas; | ||
} | ||
|
||
@Test | ||
void shouldBeConsistentWhenNoReplicasAreAvailable() { | ||
//given | ||
mockReplicaConnections(0); | ||
|
||
sut = AuroraMultiReplicaConsistency.builder() | ||
.replicaConsistency(mock(ReplicaConsistency.class)) | ||
.discoveredReplicasCache(discoveredReplicasCache) | ||
.build(); | ||
|
||
//when | ||
boolean consistent = sut.isConsistent(() -> supplierConnection); | ||
|
||
//then | ||
assertTrue(consistent); | ||
} | ||
|
||
@Test | ||
void shouldCloseReplicaConnectionsWhenConsistent() throws SQLException { | ||
//given | ||
Collection<Connection> replicas = mockReplicaConnections(5); | ||
|
||
sut = AuroraMultiReplicaConsistency.builder() | ||
.replicaConsistency(new ReplicaConsistencyMock(true)) | ||
.discoveredReplicasCache(discoveredReplicasCache) | ||
.build(); | ||
|
||
//when | ||
sut.isConsistent(() -> supplierConnection); | ||
|
||
//then | ||
verify(supplierConnection, never()).close(); | ||
for (Connection replica : replicas) { | ||
verify(replica).close(); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldCloseReplicaConnectionsWhenNotConsistent() throws SQLException { | ||
//given | ||
Collection<Connection> replicas = mockReplicaConnections(5); | ||
|
||
sut = AuroraMultiReplicaConsistency.builder() | ||
.replicaConsistency(new ReplicaConsistencyMock(false)) | ||
.discoveredReplicasCache(discoveredReplicasCache) | ||
.build(); | ||
|
||
//when | ||
sut.isConsistent(() -> supplierConnection); | ||
|
||
//then | ||
verify(supplierConnection, never()).close(); | ||
for (Connection replica : replicas) { | ||
verify(replica).close(); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/test/java/com/atlassian/db/replica/api/ReplicaConsistencyMock.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,25 @@ | ||
package com.atlassian.db.replica.api; | ||
|
||
import com.atlassian.db.replica.spi.ReplicaConsistency; | ||
|
||
import java.sql.Connection; | ||
import java.util.function.Supplier; | ||
|
||
public class ReplicaConsistencyMock implements ReplicaConsistency { | ||
private final boolean consistent; | ||
|
||
public ReplicaConsistencyMock(boolean consistent) { | ||
this.consistent = consistent; | ||
} | ||
|
||
@Override | ||
public void write(Connection main) { | ||
|
||
} | ||
|
||
@Override | ||
public boolean isConsistent(Supplier<Connection> replica) { | ||
Connection connection = replica.get(); | ||
return consistent; | ||
} | ||
} |