• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

leonchen83 / redis-replicator / #2078

09 Dec 2023 02:37AM UTC coverage: 71.561%. Remained the same
#2078

push

Baoyi Chen
test

3 of 3 new or added lines in 2 files covered. (100.0%)

140 existing lines in 4 files now uncovered.

6628 of 9262 relevant lines covered (71.56%)

0.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.97
/src/main/java/com/moilioncircle/redis/replicator/Configuration.java
1
/*
2
 * Copyright 2016-2018 Leon Chen
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package com.moilioncircle.redis.replicator;
18

19
import java.util.Arrays;
20
import java.util.Map;
21
import java.util.concurrent.ScheduledExecutorService;
22
import java.util.concurrent.atomic.AtomicLong;
23

24
import javax.net.ssl.HostnameVerifier;
25
import javax.net.ssl.SSLParameters;
26
import javax.net.ssl.SSLSocketFactory;
27

28
import com.moilioncircle.redis.replicator.net.SslContextFactory;
29
import com.moilioncircle.redis.replicator.util.Strings;
30

31
/**
32
 * @author Leon Chen
33
 * @since 2.1.0
34
 */
35
public class Configuration {
36

37
    private Configuration() {
1✔
38
    }
1✔
39

40
    /**
41
     * factory
42
     *
43
     * @return Configuration
44
     */
45
    public static Configuration defaultSetting() {
46
        return new Configuration();
1✔
47
    }
48

49
    /**
50
     * socket connection timeout
51
     * same as redis.conf repl-timeout
52
     */
53
    private int connectionTimeout = 60000;
1✔
54

55
    /**
56
     * socket input stream read timeout
57
     * same as redis.conf repl-timeout
58
     */
59
    private int readTimeout = 60000;
1✔
60

61
    /**
62
     * socket receive buffer size
63
     */
64
    private int receiveBufferSize = 0;
1✔
65

66
    /**
67
     * socket send buffer size
68
     */
69
    private int sendBufferSize = 0;
1✔
70

71
    /**
72
     * connection retry times. if retries <= 0 then always retry
73
     */
74
    private int retries = 5;
1✔
75

76
    /**
77
     * retry time interval
78
     */
79
    private int retryTimeInterval = 1000;
1✔
80

81
    /**
82
     * redis input stream buffer size
83
     */
84
    private int bufferSize = 8 * 1024;
1✔
85
    
86
    /**
87
     * auth user (redis 6.0)
88
     * 
89
     * @since 3.4.0
90
     */
91
    private String authUser = null;
1✔
92

93
    /**
94
     * auth password
95
     */
96
    private String authPassword = null;
1✔
97

98
    /**
99
     * discard rdb event
100
     */
101
    private boolean discardRdbEvent = false;
1✔
102

103
    /**
104
     * async buffer size
105
     */
106
    private int asyncCachedBytes = 512 * 1024;
1✔
107

108
    /**
109
     * rate limit (unit : bytes/second)
110
     *
111
     * @since 2.3.2
112
     */
113
    private int rateLimit = 0;
1✔
114

115
    /**
116
     * trace event log
117
     */
118
    private boolean verbose = false;
1✔
119

120
    /**
121
     * used in psync heartbeat
122
     */
123
    private int heartbeatPeriod = 1000;
1✔
124

125
    /**
126
     * use default exception handler
127
     *
128
     * @since 2.2.0
129
     */
130
    private boolean useDefaultExceptionListener = true;
1✔
131

132
    /**
133
     * open ssl connection
134
     */
135
    private boolean ssl = false;
1✔
136

137
    /**
138
     * ssl socket factory
139
     */
140
    private SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
1✔
141
    
142
    /**
143
     * ssl context factory
144
     * 
145
     * @since 3.4.0
146
     */
147
    private SslContextFactory sslContextFactory;
148

149
    /**
150
     * ssl parameters
151
     */
152
    private SSLParameters sslParameters;
153

154
    /**
155
     * hostname verifier
156
     */
157
    private HostnameVerifier hostnameVerifier;
158

159
    /**
160
     * psync master repl_id
161
     */
162
    private String replId = "?";
1✔
163

164
    /**
165
     * psync2 repl_stream_db
166
     */
167
    private int replStreamDB = -1;
1✔
168

169
    /**
170
     * psync offset
171
     */
172
    private final AtomicLong replOffset = new AtomicLong(-1);
1✔
173
    
174
    /**
175
     * @since 3.6.0
176
     * 
177
     * replication filter since redis-7.0
178
     */
179
    private ReplFilter[] replFilters = new ReplFilter[0];
1✔
180
    
181
    /**
182
     * @since 3.5.0
183
     * heartbeat scheduled executor
184
     */
185
    private ScheduledExecutorService scheduledExecutor;
186
    
187
    /**
188
     * @since 3.7.0
189
     * 
190
     * use SCAN command instead of SYNC and PSYNC
191
     */
192
    private boolean enableScan = false;
1✔
193
    
194
    /**
195
     * @since 3.7.0
196
     * 
197
     * set SCAN COUNT if enableScan = true
198
     */
199
    private int scanStep = 512;
1✔
200
    
201
    public int getConnectionTimeout() {
202
        return connectionTimeout;
1✔
203
    }
204

205
    public Configuration setConnectionTimeout(int connectionTimeout) {
206
        this.connectionTimeout = connectionTimeout;
1✔
207
        return this;
1✔
208
    }
209

210
    public int getReadTimeout() {
211
        return readTimeout;
1✔
212
    }
213

214
    public Configuration setReadTimeout(int readTimeout) {
215
        this.readTimeout = readTimeout;
1✔
216
        return this;
1✔
217
    }
218

219
    public int getRetries() {
220
        return retries;
1✔
221
    }
222

223
    public Configuration setRetries(int retries) {
224
        this.retries = retries;
1✔
225
        return this;
1✔
226
    }
227

228
    public String getAuthUser() {
229
        return authUser;
1✔
230
    }
231

232
    public Configuration setAuthUser(String authUser) {
233
        this.authUser = authUser;
1✔
234
        return this;
1✔
235
    }
236

237
    public String getAuthPassword() {
238
        return authPassword;
1✔
239
    }
240

241
    public Configuration setAuthPassword(String authPassword) {
242
        this.authPassword = authPassword;
1✔
243
        return this;
1✔
244
    }
245

246
    public int getReceiveBufferSize() {
247
        return receiveBufferSize;
1✔
248
    }
249

250
    public Configuration setReceiveBufferSize(int receiveBufferSize) {
251
        this.receiveBufferSize = receiveBufferSize;
1✔
252
        return this;
1✔
253
    }
254

255
    public int getSendBufferSize() {
256
        return sendBufferSize;
1✔
257
    }
258

259
    public Configuration setSendBufferSize(int sendBufferSize) {
260
        this.sendBufferSize = sendBufferSize;
1✔
261
        return this;
1✔
262
    }
263

264
    public int getBufferSize() {
265
        return bufferSize;
1✔
266
    }
267

268
    public Configuration setBufferSize(int bufferSize) {
269
        this.bufferSize = bufferSize;
1✔
270
        return this;
1✔
271
    }
272

273
    public boolean isDiscardRdbEvent() {
274
        return discardRdbEvent;
1✔
275
    }
276

277
    public Configuration setDiscardRdbEvent(boolean discardRdbEvent) {
UNCOV
278
        this.discardRdbEvent = discardRdbEvent;
×
UNCOV
279
        return this;
×
280
    }
281

282
    public String getReplId() {
283
        return replId;
1✔
284
    }
285

286
    public Configuration setReplId(String replId) {
287
        this.replId = replId;
1✔
288
        return this;
1✔
289
    }
290

291
    public int getReplStreamDB() {
UNCOV
292
        return replStreamDB;
×
293
    }
294

295
    public Configuration setReplStreamDB(int replStreamDB) {
296
        this.replStreamDB = replStreamDB;
1✔
297
        return this;
1✔
298
    }
299

300
    public long getReplOffset() {
301
        return replOffset.get();
1✔
302
    }
303

304
    public Configuration setReplOffset(long replOffset) {
305
        this.replOffset.set(replOffset);
1✔
306
        return this;
1✔
307
    }
308

309
    public Configuration addOffset(long offset) {
310
        this.replOffset.addAndGet(offset);
1✔
311
        return this;
1✔
312
    }
313

314
    public int getAsyncCachedBytes() {
315
        return asyncCachedBytes;
1✔
316
    }
317

318
    public Configuration setAsyncCachedBytes(int asyncCachedBytes) {
319
        this.asyncCachedBytes = asyncCachedBytes;
1✔
320
        return this;
1✔
321
    }
322

323
    public int getRateLimit() {
324
        return rateLimit;
1✔
325
    }
326

327
    public Configuration setRateLimit(int rateLimit) {
UNCOV
328
        this.rateLimit = rateLimit;
×
UNCOV
329
        return this;
×
330
    }
331

332
    public boolean isVerbose() {
333
        return verbose;
1✔
334
    }
335

336
    public Configuration setVerbose(boolean verbose) {
337
        this.verbose = verbose;
1✔
338
        return this;
1✔
339
    }
340

341
    public int getHeartbeatPeriod() {
342
        return heartbeatPeriod;
1✔
343
    }
344

345
    public Configuration setHeartbeatPeriod(int heartbeatPeriod) {
346
        this.heartbeatPeriod = heartbeatPeriod;
1✔
347
        return this;
1✔
348
    }
349

350
    public boolean isUseDefaultExceptionListener() {
351
        return useDefaultExceptionListener;
1✔
352
    }
353

354
    public Configuration setUseDefaultExceptionListener(boolean useDefaultExceptionListener) {
355
        this.useDefaultExceptionListener = useDefaultExceptionListener;
1✔
356
        return this;
1✔
357
    }
358

359
    public int getRetryTimeInterval() {
360
        return retryTimeInterval;
1✔
361
    }
362

363
    public Configuration setRetryTimeInterval(int retryTimeInterval) {
364
        this.retryTimeInterval = retryTimeInterval;
1✔
365
        return this;
1✔
366
    }
367

368
    public boolean isSsl() {
369
        return ssl;
1✔
370
    }
371

372
    public Configuration setSsl(boolean ssl) {
UNCOV
373
        this.ssl = ssl;
×
UNCOV
374
        return this;
×
375
    }
376

377
    public SSLSocketFactory getSslSocketFactory() {
UNCOV
378
        return sslSocketFactory;
×
379
    }
380

381
    public Configuration setSslSocketFactory(SSLSocketFactory sslSocketFactory) {
UNCOV
382
        this.sslSocketFactory = sslSocketFactory;
×
UNCOV
383
        return this;
×
384
    }
385

386
    public SslContextFactory getSslContextFactory() {
UNCOV
387
        return sslContextFactory;
×
388
    }
389

390
    public Configuration setSslContextFactory(SslContextFactory sslContextFactory) {
UNCOV
391
        this.sslContextFactory = sslContextFactory;
×
UNCOV
392
        return this;
×
393
    }
394

395
    public SSLParameters getSslParameters() {
UNCOV
396
        return sslParameters;
×
397
    }
398

399
    public Configuration setSslParameters(SSLParameters sslParameters) {
UNCOV
400
        this.sslParameters = sslParameters;
×
UNCOV
401
        return this;
×
402
    }
403

404
    public HostnameVerifier getHostnameVerifier() {
UNCOV
405
        return hostnameVerifier;
×
406
    }
407

408
    public Configuration setHostnameVerifier(HostnameVerifier hostnameVerifier) {
UNCOV
409
        this.hostnameVerifier = hostnameVerifier;
×
UNCOV
410
        return this;
×
411
    }
412
    
413
    public ScheduledExecutorService getScheduledExecutor() {
414
        return scheduledExecutor;
1✔
415
    }
416
    
417
    public Configuration setScheduledExecutor(ScheduledExecutorService scheduledExecutor) {
418
        this.scheduledExecutor = scheduledExecutor;
1✔
419
        return this;
1✔
420
    }
421
    
422
    public ReplFilter[] getReplFilters() {
423
        return replFilters;
1✔
424
    }
425
    
426
    public Configuration setReplFilters(ReplFilter... replFilters) {
UNCOV
427
        this.replFilters = replFilters;
×
UNCOV
428
        return this;
×
429
    }
430
    
431
    public boolean isEnableScan() {
432
        return enableScan;
1✔
433
    }
434
    
435
    public Configuration setEnableScan(boolean enableScan) {
436
        this.enableScan = enableScan;
1✔
437
        return this;
1✔
438
    }
439
    
440
    public int getScanStep() {
441
        return scanStep;
1✔
442
    }
443
    
444
    public Configuration setScanStep(int scanStep) {
445
        this.scanStep = scanStep;
1✔
446
        return this;
1✔
447
    }
448
    
449
    public Configuration merge(SslConfiguration sslConfiguration) {
450
        if (sslConfiguration == null) return this;
1✔
451
        this.setSslParameters(sslConfiguration.getSslParameters());
×
UNCOV
452
        this.setSslSocketFactory(sslConfiguration.getSslSocketFactory());
×
UNCOV
453
        this.setHostnameVerifier(sslConfiguration.getHostnameVerifier());
×
454
        this.setSslContextFactory(sslConfiguration.getSslContextFactory());
×
UNCOV
455
        return this;
×
456
    }
457

458
    public static Configuration valueOf(RedisURI uri) {
459
        Configuration configuration = defaultSetting();
1✔
460
        Map<String, String> parameters = uri.parameters;
1✔
461
        if (parameters.containsKey("connectionTimeout")) {
1✔
UNCOV
462
            configuration.setConnectionTimeout(getInt(parameters.get("connectionTimeout"), 60000));
×
463
        }
464
        if (parameters.containsKey("readTimeout")) {
1✔
UNCOV
465
            configuration.setReadTimeout(getInt(parameters.get("readTimeout"), 60000));
×
466
        }
467
        if (parameters.containsKey("receiveBufferSize")) {
1✔
UNCOV
468
            configuration.setReceiveBufferSize(getInt(parameters.get("receiveBufferSize"), 0));
×
469
        }
470
        if (parameters.containsKey("sendBufferSize")) {
1✔
UNCOV
471
            configuration.setSendBufferSize(getInt(parameters.get("sendBufferSize"), 0));
×
472
        }
473
        if (parameters.containsKey("retries")) {
1✔
474
            configuration.setRetries(getInt(parameters.get("retries"), 5));
1✔
475
        }
476
        if (parameters.containsKey("retryTimeInterval")) {
1✔
UNCOV
477
            configuration.setRetryTimeInterval(getInt(parameters.get("retryTimeInterval"), 1000));
×
478
        }
479
        if (parameters.containsKey("bufferSize")) {
1✔
UNCOV
480
            configuration.setBufferSize(getInt(parameters.get("bufferSize"), 8 * 1024));
×
481
        }
482
        if (parameters.containsKey("authUser")) {
1✔
483
            configuration.setAuthUser(parameters.get("authUser"));
1✔
484
        }
485
        if (parameters.containsKey("authPassword")) {
1✔
486
            configuration.setAuthPassword(parameters.get("authPassword"));
1✔
487
        }
488
        if (parameters.containsKey("discardRdbEvent")) {
1✔
UNCOV
489
            configuration.setDiscardRdbEvent(getBool(parameters.get("discardRdbEvent"), false));
×
490
        }
491
        if (parameters.containsKey("asyncCachedBytes")) {
1✔
UNCOV
492
            configuration.setAsyncCachedBytes(getInt(parameters.get("asyncCachedBytes"), 512 * 1024));
×
493
        }
494
        if (parameters.containsKey("rateLimit")) {
1✔
UNCOV
495
            configuration.setRateLimit(getInt(parameters.get("rateLimit"), 0));
×
496
        }
497
        if (parameters.containsKey("verbose")) {
1✔
UNCOV
498
            configuration.setVerbose(getBool(parameters.get("verbose"), false));
×
499
        }
500
        if (parameters.containsKey("heartbeatPeriod")) {
1✔
UNCOV
501
            configuration.setHeartbeatPeriod(getInt(parameters.get("heartbeatPeriod"), 1000));
×
502
        }
503
        if (parameters.containsKey("useDefaultExceptionListener")) {
1✔
UNCOV
504
            configuration.setUseDefaultExceptionListener(getBool(parameters.get("useDefaultExceptionListener"), true));
×
505
        }
506
        if (parameters.containsKey("ssl")) {
1✔
UNCOV
507
            configuration.setSsl(getBool(parameters.get("ssl"), false));
×
508
        }
509
        if (parameters.containsKey("replId")) {
1✔
UNCOV
510
            configuration.setReplId(parameters.get("replId"));
×
511
        }
512
        if (parameters.containsKey("replStreamDB")) {
1✔
UNCOV
513
            configuration.setReplStreamDB(getInt(parameters.get("replStreamDB"), -1));
×
514
        }
515
        if (parameters.containsKey("replOffset")) {
1✔
UNCOV
516
            configuration.setReplOffset(getLong(parameters.get("replOffset"), -1L));
×
517
        }
518
    
519
        // scan
520
        if (parameters.containsKey("enableScan")) {
1✔
521
            configuration.setEnableScan(getBool(parameters.get("enableScan"), false));
1✔
522
        }
523
        if (parameters.containsKey("scanStep")) {
1✔
524
            configuration.setScanStep(getInt(parameters.get("scanStep"), 512));
1✔
525
        }
526
        
527
        // redis 6
528
        if (uri.isSsl()) {
1✔
UNCOV
529
            configuration.setSsl(true);
×
530
        }
531
        if (uri.getUser() != null) {
1✔
532
            configuration.setAuthUser(uri.getUser());
1✔
533
        }
534
        if (uri.getPassword() != null) {
1✔
535
            configuration.setAuthPassword(uri.getPassword());
1✔
536
        }
537
        return configuration;
1✔
538
    }
539

540
    private static boolean getBool(String value, boolean defaultValue) {
541
        if (value == null)
1✔
542
            return defaultValue;
×
543
        if (value.equals("false") || value.equals("no"))
1✔
UNCOV
544
            return false;
×
545
        if (value.equals("true") || value.equals("yes"))
1✔
546
            return true;
1✔
547
        return defaultValue;
×
548
    }
549

550
    private static int getInt(String value, int defaultValue) {
551
        if (value == null)
1✔
552
            return defaultValue;
×
553
        try {
554
            return Integer.parseInt(value);
1✔
UNCOV
555
        } catch (NumberFormatException e) {
×
UNCOV
556
            return defaultValue;
×
557
        }
558
    }
559

560
    private static long getLong(String value, long defaultValue) {
UNCOV
561
        if (value == null)
×
UNCOV
562
            return defaultValue;
×
563
        try {
UNCOV
564
            return Long.parseLong(value);
×
UNCOV
565
        } catch (NumberFormatException e) {
×
UNCOV
566
            return defaultValue;
×
567
        }
568
    }
569

570
    @Override
571
    public String toString() {
572
        return "Configuration{" +
1✔
573
                "connectionTimeout=" + connectionTimeout +
574
                ", readTimeout=" + readTimeout +
575
                ", receiveBufferSize=" + receiveBufferSize +
576
                ", sendBufferSize=" + sendBufferSize +
577
                ", retries=" + retries +
578
                ", retryTimeInterval=" + retryTimeInterval +
579
                ", bufferSize=" + bufferSize +
580
                ", authUser='" + authUser + '\'' +
581
                ", authPassword='" + Strings.mask(authPassword) + '\'' +
1✔
582
                ", discardRdbEvent=" + discardRdbEvent +
583
                ", asyncCachedBytes=" + asyncCachedBytes +
584
                ", rateLimit=" + rateLimit +
585
                ", verbose=" + verbose +
586
                ", heartbeatPeriod=" + heartbeatPeriod +
587
                ", scheduledExecutor=" + scheduledExecutor +
588
                ", useDefaultExceptionListener=" + useDefaultExceptionListener +
589
                ", enableScan=" + enableScan +
590
                ", scanStep=" + scanStep +
591
                ", ssl=" + ssl +
592
                ", sslSocketFactory=" + sslSocketFactory +
593
                ", sslContextFactory=" + sslContextFactory +
594
                ", sslParameters=" + sslParameters +
595
                ", hostnameVerifier=" + hostnameVerifier +
596
                ", replId='" + replId + '\'' +
597
                ", replStreamDB=" + replStreamDB +
598
                ", replOffset=" + replOffset +
599
                ", replFilters=" + Arrays.toString(replFilters) +
1✔
600
                '}';
601
    }
602
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc