1
1
package redis .clients .jedis .scenario ;
2
2
3
- import io .github .resilience4j .circuitbreaker .CircuitBreakerConfig ;
4
-
5
3
import org .junit .jupiter .api .BeforeAll ;
6
4
import org .junit .jupiter .api .Tag ;
7
5
import org .junit .jupiter .api .Tags ;
34
32
import static redis .clients .jedis .Protocol .DEFAULT_TIMEOUT ;
35
33
36
34
@ Tags ({ @ Tag ("failover" ), @ Tag ("scenario" ) })
37
- public class ActiveActiveFailoverTest {
38
- private static final Logger log = LoggerFactory .getLogger (ActiveActiveFailoverTest .class );
35
+ public class ActiveActiveFailoverIT {
36
+ private static final Logger log = LoggerFactory .getLogger (ActiveActiveFailoverIT .class );
39
37
private static final int NUM_OF_THREADS = 18 ;
40
38
private static final int SOCKET_TIMEOUT_MS = DEFAULT_TIMEOUT ;
41
39
private static final int CONNECTION_TIMEOUT_MS = DEFAULT_TIMEOUT ;
@@ -48,7 +46,7 @@ public class ActiveActiveFailoverTest {
48
46
@ BeforeAll
49
47
public static void beforeClass () {
50
48
try {
51
- ActiveActiveFailoverTest .endpoint = HostAndPorts .getRedisEndpoint ("re-active-active" );
49
+ ActiveActiveFailoverIT .endpoint = HostAndPorts .getRedisEndpoint ("re-active-active" );
52
50
} catch (IllegalArgumentException e ) {
53
51
log .warn ("Skipping test because no Redis endpoint is configured" );
54
52
assumeTrue (false );
@@ -59,29 +57,22 @@ public static void beforeClass() {
59
57
public void testFailover () {
60
58
61
59
JedisClientConfig config = endpoint .getClientConfigBuilder ()
62
- .socketTimeoutMillis (SOCKET_TIMEOUT_MS )
63
- . connectionTimeoutMillis ( CONNECTION_TIMEOUT_MS ) .build ();
60
+ .socketTimeoutMillis (SOCKET_TIMEOUT_MS ). connectionTimeoutMillis ( CONNECTION_TIMEOUT_MS )
61
+ .build ();
64
62
65
63
DatabaseConfig primary = DatabaseConfig .builder (endpoint .getHostAndPort (0 ), config )
66
- .connectionPoolConfig (RecommendedSettings .poolConfig ).weight (1.0f ).build ();
64
+ .connectionPoolConfig (RecommendedSettings .poolConfig ).weight (1.0f ).build ();
67
65
68
66
DatabaseConfig secondary = DatabaseConfig .builder (endpoint .getHostAndPort (1 ), config )
69
- .connectionPoolConfig (RecommendedSettings .poolConfig ).weight (0.5f ).build ();
70
-
71
- MultiDbConfig multiConfig = MultiDbConfig .builder ()
72
- .endpoint (primary )
73
- .endpoint (secondary )
74
- .circuitBreakerSlidingWindowSize (1 ) // SLIDING WINDOW SIZE IN SECONDS
75
- .circuitBreakerFailureRateThreshold (10.0f ) // percentage of failures to trigger circuit breaker
76
- .failbackSupported (true )
77
- .failbackCheckInterval (1000 )
78
- .gracePeriod (2000 )
79
- .retryWaitDuration (10 )
80
- .retryMaxAttempts (1 )
81
- .retryWaitDurationExponentialBackoffMultiplier (1 )
82
- .fastFailover (true )
83
- .retryOnFailover (false )
84
- .build ();
67
+ .connectionPoolConfig (RecommendedSettings .poolConfig ).weight (0.5f ).build ();
68
+
69
+ MultiDbConfig multiConfig = MultiDbConfig .builder ().endpoint (primary ).endpoint (secondary )
70
+ .circuitBreakerSlidingWindowSize (1 ) // SLIDING WINDOW SIZE IN SECONDS
71
+ .circuitBreakerFailureRateThreshold (10.0f ) // percentage of failures to trigger circuit
72
+ // breaker
73
+ .failbackSupported (true ).failbackCheckInterval (1000 ).gracePeriod (2000 ).retryWaitDuration (10 )
74
+ .retryMaxAttempts (1 ).retryWaitDurationExponentialBackoffMultiplier (1 ).fastFailover (true )
75
+ .retryOnFailover (false ).build ();
85
76
class FailoverReporter implements Consumer <DatabaseSwitchEvent > {
86
77
87
78
String currentClusterName = "not set" ;
@@ -101,7 +92,8 @@ public String getCurrentClusterName() {
101
92
@ Override
102
93
public void accept (DatabaseSwitchEvent e ) {
103
94
this .currentClusterName = e .getDatabaseName ();
104
- log .info ("\n \n ====FailoverEvent=== \n Jedis failover to cluster: {}\n ====FailoverEvent===\n \n " ,
95
+ log .info (
96
+ "\n \n ====FailoverEvent=== \n Jedis failover to cluster: {}\n ====FailoverEvent===\n \n " ,
105
97
e .getDatabaseName ());
106
98
107
99
if (failoverHappened ) {
@@ -116,10 +108,8 @@ public void accept(DatabaseSwitchEvent e) {
116
108
117
109
FailoverReporter reporter = new FailoverReporter ();
118
110
119
- MultiDbClient client = MultiDbClient .builder ()
120
- .multiDbConfig (multiConfig )
121
- .databaseSwitchListener (reporter )
122
- .build ();
111
+ MultiDbClient client = MultiDbClient .builder ().multiDbConfig (multiConfig )
112
+ .databaseSwitchListener (reporter ).build ();
123
113
124
114
AtomicLong executedCommands = new AtomicLong (0 );
125
115
AtomicLong retryingThreadsCounter = new AtomicLong (0 );
@@ -160,7 +150,8 @@ public void accept(DatabaseSwitchEvent e) {
160
150
161
151
if (attempt == 0 ) {
162
152
long failedThreads = retryingThreadsCounter .incrementAndGet ();
163
- log .warn ("Thread {} failed to execute command. Failed threads: {}" , threadId , failedThreads );
153
+ log .warn ("Thread {} failed to execute command. Failed threads: {}" , threadId ,
154
+ failedThreads );
164
155
}
165
156
try {
166
157
Thread .sleep (retryingDelay );
@@ -215,8 +206,10 @@ public void accept(DatabaseSwitchEvent e) {
215
206
await ().atMost (Duration .ofSeconds (1 )).until (() -> pool1 .getNumActive () == 0 );
216
207
await ().atMost (Duration .ofSeconds (1 )).until (() -> pool2 .getNumActive () == 0 );
217
208
218
- log .info ("Connection pool {}: active: {}, idle: {}" , endpoint .getHostAndPort (0 ), pool1 .getNumActive (), pool1 .getNumIdle ());
219
- log .info ("Connection pool {}: active: {}, idle: {}" , endpoint .getHostAndPort (1 ), pool2 .getNumActive (), pool2 .getNumIdle ());
209
+ log .info ("Connection pool {}: active: {}, idle: {}" , endpoint .getHostAndPort (0 ),
210
+ pool1 .getNumActive (), pool1 .getNumIdle ());
211
+ log .info ("Connection pool {}: active: {}, idle: {}" , endpoint .getHostAndPort (1 ),
212
+ pool2 .getNumActive (), pool2 .getNumIdle ());
220
213
log .info ("Failover happened at: {}" , reporter .failoverAt );
221
214
log .info ("Failback happened at: {}" , reporter .failbackAt );
222
215
log .info ("Last failed command at: {}" , lastFailedCommandAt .get ());
@@ -228,7 +221,8 @@ public void accept(DatabaseSwitchEvent e) {
228
221
assertTrue (fakeApp .capturedExceptions ().isEmpty ());
229
222
assertTrue (reporter .failoverHappened );
230
223
assertTrue (reporter .failbackHappened );
231
- assertThat ( Duration .between (reporter .failoverAt , reporter .failbackAt ).getSeconds (), greaterThanOrEqualTo (NETWORK_FAILURE_INTERVAL ));
224
+ assertThat (Duration .between (reporter .failoverAt , reporter .failbackAt ).getSeconds (),
225
+ greaterThanOrEqualTo (NETWORK_FAILURE_INTERVAL ));
232
226
233
227
client .close ();
234
228
}
0 commit comments