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

leonchen83 / redis-replicator / #2065

09 Dec 2023 01:40AM UTC coverage: 71.666% (+0.02%) from 71.644%
#2065

push

Baoyi Chen
offset

3 of 3 new or added lines in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

6647 of 9275 relevant lines covered (71.67%)

0.72 hits per line

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

70.9
/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.ConcurrentHashMap;
22
import java.util.concurrent.ScheduledExecutorService;
23
import java.util.concurrent.atomic.AtomicLong;
24

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

211
    public Configuration setConnectionTimeout(int connectionTimeout) {
212
        this.connectionTimeout = connectionTimeout;
1✔
213
        return this;
1✔
214
    }
215

216
    public int getReadTimeout() {
217
        return readTimeout;
1✔
218
    }
219

220
    public Configuration setReadTimeout(int readTimeout) {
221
        this.readTimeout = readTimeout;
1✔
222
        return this;
1✔
223
    }
224

225
    public int getRetries() {
226
        return retries;
1✔
227
    }
228

229
    public Configuration setRetries(int retries) {
230
        this.retries = retries;
1✔
231
        return this;
1✔
232
    }
233

234
    public String getAuthUser() {
235
        return authUser;
1✔
236
    }
237

238
    public Configuration setAuthUser(String authUser) {
239
        this.authUser = authUser;
1✔
240
        return this;
1✔
241
    }
242

243
    public String getAuthPassword() {
244
        return authPassword;
1✔
245
    }
246

247
    public Configuration setAuthPassword(String authPassword) {
248
        this.authPassword = authPassword;
1✔
249
        return this;
1✔
250
    }
251

252
    public int getReceiveBufferSize() {
253
        return receiveBufferSize;
1✔
254
    }
255

256
    public Configuration setReceiveBufferSize(int receiveBufferSize) {
257
        this.receiveBufferSize = receiveBufferSize;
1✔
258
        return this;
1✔
259
    }
260

261
    public int getSendBufferSize() {
262
        return sendBufferSize;
1✔
263
    }
264

265
    public Configuration setSendBufferSize(int sendBufferSize) {
266
        this.sendBufferSize = sendBufferSize;
1✔
267
        return this;
1✔
268
    }
269

270
    public int getBufferSize() {
271
        return bufferSize;
1✔
272
    }
273

274
    public Configuration setBufferSize(int bufferSize) {
275
        this.bufferSize = bufferSize;
1✔
276
        return this;
1✔
277
    }
278

279
    public boolean isDiscardRdbEvent() {
280
        return discardRdbEvent;
1✔
281
    }
282

283
    public Configuration setDiscardRdbEvent(boolean discardRdbEvent) {
284
        this.discardRdbEvent = discardRdbEvent;
×
285
        return this;
×
286
    }
287

288
    public String getReplId() {
289
        return replId;
1✔
290
    }
291

292
    public Configuration setReplId(String replId) {
293
        this.replId = replId;
1✔
294
        return this;
1✔
295
    }
296

297
    public int getReplStreamDB() {
298
        return replStreamDB;
×
299
    }
300

301
    public Configuration setReplStreamDB(int replStreamDB) {
302
        this.replStreamDB = replStreamDB;
1✔
303
        return this;
1✔
304
    }
305

306
    public long getReplOffset() {
307
        return replOffset.get();
1✔
308
    }
309

310
    public Configuration setReplOffset(long replOffset) {
311
        this.replOffset.set(replOffset);
1✔
312
        return this;
1✔
313
    }
314

315
    public Configuration addOffset(long offset) {
316
        this.replOffset.addAndGet(offset);
1✔
317
        return this;
1✔
318
    }
319

320
    public int getAsyncCachedBytes() {
321
        return asyncCachedBytes;
1✔
322
    }
323

324
    public Configuration setAsyncCachedBytes(int asyncCachedBytes) {
325
        this.asyncCachedBytes = asyncCachedBytes;
1✔
326
        return this;
1✔
327
    }
328

329
    public int getRateLimit() {
330
        return rateLimit;
1✔
331
    }
332

333
    public Configuration setRateLimit(int rateLimit) {
334
        this.rateLimit = rateLimit;
×
335
        return this;
×
336
    }
337

338
    public boolean isVerbose() {
339
        return verbose;
1✔
340
    }
341

342
    public Configuration setVerbose(boolean verbose) {
343
        this.verbose = verbose;
1✔
344
        return this;
1✔
345
    }
346

347
    public int getHeartbeatPeriod() {
348
        return heartbeatPeriod;
1✔
349
    }
350

351
    public Configuration setHeartbeatPeriod(int heartbeatPeriod) {
352
        this.heartbeatPeriod = heartbeatPeriod;
1✔
353
        return this;
1✔
354
    }
355

356
    public boolean isUseDefaultExceptionListener() {
357
        return useDefaultExceptionListener;
1✔
358
    }
359

360
    public Configuration setUseDefaultExceptionListener(boolean useDefaultExceptionListener) {
361
        this.useDefaultExceptionListener = useDefaultExceptionListener;
1✔
362
        return this;
1✔
363
    }
364

365
    public int getRetryTimeInterval() {
366
        return retryTimeInterval;
1✔
367
    }
368

369
    public Configuration setRetryTimeInterval(int retryTimeInterval) {
370
        this.retryTimeInterval = retryTimeInterval;
1✔
371
        return this;
1✔
372
    }
373

374
    public boolean isSsl() {
375
        return ssl;
1✔
376
    }
377

378
    public Configuration setSsl(boolean ssl) {
379
        this.ssl = ssl;
×
380
        return this;
×
381
    }
382

383
    public SSLSocketFactory getSslSocketFactory() {
384
        return sslSocketFactory;
×
385
    }
386

387
    public Configuration setSslSocketFactory(SSLSocketFactory sslSocketFactory) {
388
        this.sslSocketFactory = sslSocketFactory;
×
389
        return this;
×
390
    }
391

392
    public SslContextFactory getSslContextFactory() {
393
        return sslContextFactory;
×
394
    }
395

396
    public Configuration setSslContextFactory(SslContextFactory sslContextFactory) {
397
        this.sslContextFactory = sslContextFactory;
×
398
        return this;
×
399
    }
400

401
    public SSLParameters getSslParameters() {
402
        return sslParameters;
×
403
    }
404

405
    public Configuration setSslParameters(SSLParameters sslParameters) {
406
        this.sslParameters = sslParameters;
×
407
        return this;
×
408
    }
409

410
    public HostnameVerifier getHostnameVerifier() {
411
        return hostnameVerifier;
×
412
    }
413

414
    public Configuration setHostnameVerifier(HostnameVerifier hostnameVerifier) {
415
        this.hostnameVerifier = hostnameVerifier;
×
416
        return this;
×
417
    }
418
    
419
    public ScheduledExecutorService getScheduledExecutor() {
420
        return scheduledExecutor;
1✔
421
    }
422
    
423
    public Configuration setScheduledExecutor(ScheduledExecutorService scheduledExecutor) {
424
        this.scheduledExecutor = scheduledExecutor;
1✔
425
        return this;
1✔
426
    }
427
    
428
    public ReplFilter[] getReplFilters() {
429
        return replFilters;
1✔
430
    }
431
    
432
    public Configuration setReplFilters(ReplFilter... replFilters) {
433
        this.replFilters = replFilters;
×
434
        return this;
×
435
    }
436
    
437
    public boolean isEnableScan() {
438
        return enableScan;
1✔
439
    }
440
    
441
    public Configuration setEnableScan(boolean enableScan) {
442
        this.enableScan = enableScan;
1✔
443
        return this;
1✔
444
    }
445
    
446
    public int getScanStep() {
447
        return scanStep;
1✔
448
    }
449
    
450
    public Configuration setScanStep(int scanStep) {
451
        this.scanStep = scanStep;
1✔
452
        return this;
1✔
453
    }
454
    
455
    @SuppressWarnings("unchecked")
456
    public <T> T getCookie(String key) {
UNCOV
457
        return (T) this.cookies.get(key);
×
458
    }
459
    
460
    @SuppressWarnings("unchecked")
461
    public <T> T setCookie(String key, Object value) {
462
        return (T) this.cookies.put(key, value);
1✔
463
    }
464
    
465
    public Configuration merge(SslConfiguration sslConfiguration) {
466
        if (sslConfiguration == null) return this;
1✔
467
        this.setSslParameters(sslConfiguration.getSslParameters());
×
468
        this.setSslSocketFactory(sslConfiguration.getSslSocketFactory());
×
469
        this.setHostnameVerifier(sslConfiguration.getHostnameVerifier());
×
470
        this.setSslContextFactory(sslConfiguration.getSslContextFactory());
×
471
        return this;
×
472
    }
473

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

556
    private static boolean getBool(String value, boolean defaultValue) {
557
        if (value == null)
1✔
558
            return defaultValue;
×
559
        if (value.equals("false") || value.equals("no"))
1✔
560
            return false;
×
561
        if (value.equals("true") || value.equals("yes"))
1✔
562
            return true;
1✔
563
        return defaultValue;
×
564
    }
565

566
    private static int getInt(String value, int defaultValue) {
567
        if (value == null)
1✔
568
            return defaultValue;
×
569
        try {
570
            return Integer.parseInt(value);
1✔
571
        } catch (NumberFormatException e) {
×
572
            return defaultValue;
×
573
        }
574
    }
575

576
    private static long getLong(String value, long defaultValue) {
577
        if (value == null)
×
578
            return defaultValue;
×
579
        try {
580
            return Long.parseLong(value);
×
581
        } catch (NumberFormatException e) {
×
582
            return defaultValue;
×
583
        }
584
    }
585

586
    @Override
587
    public String toString() {
588
        return "Configuration{" +
1✔
589
                "connectionTimeout=" + connectionTimeout +
590
                ", readTimeout=" + readTimeout +
591
                ", receiveBufferSize=" + receiveBufferSize +
592
                ", sendBufferSize=" + sendBufferSize +
593
                ", retries=" + retries +
594
                ", retryTimeInterval=" + retryTimeInterval +
595
                ", bufferSize=" + bufferSize +
596
                ", authUser='" + authUser + '\'' +
597
                ", authPassword='" + Strings.mask(authPassword) + '\'' +
1✔
598
                ", discardRdbEvent=" + discardRdbEvent +
599
                ", asyncCachedBytes=" + asyncCachedBytes +
600
                ", rateLimit=" + rateLimit +
601
                ", verbose=" + verbose +
602
                ", heartbeatPeriod=" + heartbeatPeriod +
603
                ", scheduledExecutor=" + scheduledExecutor +
604
                ", useDefaultExceptionListener=" + useDefaultExceptionListener +
605
                ", enableScan=" + enableScan +
606
                ", scanStep=" + scanStep +
607
                ", ssl=" + ssl +
608
                ", sslSocketFactory=" + sslSocketFactory +
609
                ", sslContextFactory=" + sslContextFactory +
610
                ", sslParameters=" + sslParameters +
611
                ", hostnameVerifier=" + hostnameVerifier +
612
                ", replId='" + replId + '\'' +
613
                ", replStreamDB=" + replStreamDB +
614
                ", replOffset=" + replOffset +
615
                ", replFilters=" + Arrays.toString(replFilters) +
1✔
616
                ", cookies=" + cookies +
617
                '}';
618
    }
619
}
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