-
Notifications
You must be signed in to change notification settings - Fork 565
Expand file tree
/
Copy pathSingleChronicleQueueBuilder.java
More file actions
1204 lines (1030 loc) · 44.4 KB
/
SingleChronicleQueueBuilder.java
File metadata and controls
1204 lines (1030 loc) · 44.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2016-2020 chronicle.software
*
* https://chronicle.software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.queue.impl.single;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesRingBufferStats;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.bytes.MappedBytes;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.core.Maths;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.core.io.Closeable;
import net.openhft.chronicle.core.io.IORuntimeException;
import net.openhft.chronicle.core.threads.EventLoop;
import net.openhft.chronicle.core.threads.HandlerPriority;
import net.openhft.chronicle.core.threads.OnDemandEventLoop;
import net.openhft.chronicle.core.time.SystemTimeProvider;
import net.openhft.chronicle.core.time.TimeProvider;
import net.openhft.chronicle.core.util.Builder;
import net.openhft.chronicle.core.util.ObjectUtils;
import net.openhft.chronicle.core.util.ThrowingBiFunction;
import net.openhft.chronicle.core.util.Updater;
import net.openhft.chronicle.queue.*;
import net.openhft.chronicle.queue.impl.*;
import net.openhft.chronicle.queue.impl.table.ReadonlyTableStore;
import net.openhft.chronicle.queue.impl.table.SingleTableBuilder;
import net.openhft.chronicle.queue.internal.domestic.QueueOffsetSpec;
import net.openhft.chronicle.queue.util.QueueUtil;
import net.openhft.chronicle.threads.MediumEventLoop;
import net.openhft.chronicle.threads.Pauser;
import net.openhft.chronicle.threads.TimeoutPauser;
import net.openhft.chronicle.threads.TimingPauser;
import net.openhft.chronicle.wire.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.lang.reflect.Constructor;
import java.nio.file.Path;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import static java.util.Objects.requireNonNull;
import static net.openhft.chronicle.core.pool.ClassAliasPool.CLASS_ALIASES;
import static net.openhft.chronicle.queue.impl.single.QueueFileShrinkManager.defaultFileShrink;
import static net.openhft.chronicle.queue.impl.single.SingleChronicleQueue.QUEUE_METADATA_FILE;
import static net.openhft.chronicle.wire.WireType.DEFAULT_ZERO_BINARY;
import static net.openhft.chronicle.wire.WireType.DELTA_BINARY;
public class SingleChronicleQueueBuilder extends SelfDescribingMarshallable implements Cloneable, Builder<SingleChronicleQueue> {
public static final long DEFAULT_SPARSE_CAPACITY = 512L << 30;
private static final Constructor ENTERPRISE_QUEUE_CONSTRUCTOR;
private static final WireStoreFactory storeFactory = SingleChronicleQueueBuilder::createStore;
private static final Supplier<TimingPauser> TIMING_PAUSER_SUPPLIER = DefaultPauserSupplier.INSTANCE;
static {
CLASS_ALIASES.addAlias(WireType.class);
CLASS_ALIASES.addAlias(SCQMeta.class, "SCQMeta");
CLASS_ALIASES.addAlias(SCQRoll.class, "SCQSRoll");
CLASS_ALIASES.addAlias(SCQIndexing.class, "SCQSIndexing");
CLASS_ALIASES.addAlias(SingleChronicleQueueStore.class, "SCQStore");
{
Constructor co;
try {
co = ((Class) Class.forName("software.chronicle.enterprise.queue.EnterpriseSingleChronicleQueue")).getDeclaredConstructors()[0];
Jvm.setAccessible(co);
} catch (Exception e) {
co = null;
}
ENTERPRISE_QUEUE_CONSTRUCTOR = co;
}
}
private BufferMode writeBufferMode = BufferMode.None;
private BufferMode readBufferMode = BufferMode.None;
private WireType wireType = WireType.BINARY_LIGHT;
private Long blockSize;
private Boolean useSparseFiles;
private Long sparseCapacity;
private File path;
private RollCycle rollCycle;
private Long epoch; // default is 1970-01-01 00:00:00.000 UTC
private Long bufferCapacity;
private Integer indexSpacing;
private Integer indexCount;
private Boolean enableRingBufferMonitoring;
private Boolean ringBufferReaderCanDrain;
private Boolean ringBufferForceCreateReader;
private Boolean ringBufferReopenReader;
private Supplier<Pauser> ringBufferPauserSupplier;
private HandlerPriority drainerPriority;
private int drainerTimeoutMS = -1;
@Nullable
private EventLoop eventLoop;
/**
* by default does not log any stats of the ring buffer
*/
private Consumer<BytesRingBufferStats> onRingBufferStats;
private TimeProvider timeProvider;
private Supplier<TimingPauser> pauserSupplier;
private Long timeoutMS; // 10 seconds.
private Integer sourceId;
private StoreFileListener storeFileListener;
private Boolean readOnly;
private boolean checkInterrupts;
private transient TableStore<SCQMeta> metaStore;
// enterprise stuff
private int deltaCheckpointInterval = -1;
private Supplier<BiConsumer<BytesStore, Bytes<?>>> encodingSupplier;
private Supplier<BiConsumer<BytesStore, Bytes<?>>> decodingSupplier;
private Updater<Bytes<?>> messageInitializer;
private Consumer<Bytes<?>> messageHeaderReader;
private SecretKeySpec key;
private int maxTailers;
private ThrowingBiFunction<Long, Integer, BytesStore, Exception> bufferBytesStoreCreator;
private Long pretouchIntervalMillis;
private LocalTime rollTime;
private ZoneId rollTimeZone;
private QueueOffsetSpec queueOffsetSpec;
private boolean doubleBuffer;
private Function<SingleChronicleQueue, Condition> createAppenderConditionCreator;
private long forceDirectoryListingRefreshIntervalMs = 60_000;
private AppenderListener appenderListener;
protected SingleChronicleQueueBuilder() {
}
/*
* ========================
* Builders
* ========================
*/
public static void addAliases() {
// static initialiser.
}
private FileShrinkage fileShrinkage = defaultFileShrink();
/**
* @return if set, shrinks the .cq4 file after roll
*/
public FileShrinkage fileShrinkage() {
return fileShrinkage;
}
/**
* @return sets shrinks the .cq4 file after roll, or use net.openhft.chronicle.queue.impl.single.FileShrink#NONE
* if not required, default is net.openhft.chronicle.queue.impl.single.FileShrink#SHRINK_ASYNCHRONOUSLY
*/
public SingleChronicleQueueBuilder fileShrinkage(FileShrinkage fileShrink) {
this.fileShrinkage = fileShrink;
return this;
}
/**
* @return an empty builder
*/
public static SingleChronicleQueueBuilder builder() {
return new SingleChronicleQueueBuilder();
}
@NotNull
public static SingleChronicleQueueBuilder builder(@NotNull Path path, @NotNull WireType wireType) {
return builder(path.toFile(), wireType);
}
@NotNull
public static SingleChronicleQueueBuilder builder(@NotNull File file, @NotNull WireType wireType) {
SingleChronicleQueueBuilder result = builder().wireType(wireType);
if (file.isFile()) {
if (!file.getName().endsWith(SingleChronicleQueue.SUFFIX)) {
throw new IllegalArgumentException("Invalid file type: " + file.getName());
}
Jvm.warn().on(SingleChronicleQueueBuilder.class,
"Queues should be configured with the queue directory, not a specific filename. Actual file used: "
+ file.getParentFile());
result.path(file.getParentFile());
} else
result.path(file);
return result;
}
public static SingleChronicleQueueBuilder single() {
SingleChronicleQueueBuilder builder = builder();
builder.wireType(WireType.BINARY_LIGHT);
return builder;
}
public static SingleChronicleQueueBuilder single(@NotNull String basePath) {
return binary(basePath);
}
public static SingleChronicleQueueBuilder single(@NotNull File basePath) {
return binary(basePath);
}
public static SingleChronicleQueueBuilder binary(@NotNull Path path) {
return binary(path.toFile());
}
public static SingleChronicleQueueBuilder binary(@NotNull String basePath) {
return binary(new File(basePath));
}
public static SingleChronicleQueueBuilder binary(@NotNull File basePathFile) {
return builder(basePathFile, WireType.BINARY_LIGHT);
}
public static SingleChronicleQueueBuilder fieldlessBinary(@NotNull File name) {
return builder(name, WireType.FIELDLESS_BINARY);
}
public static SingleChronicleQueueBuilder defaultZeroBinary(@NotNull File basePathFile) {
return builder(basePathFile, DEFAULT_ZERO_BINARY);
}
public static SingleChronicleQueueBuilder deltaBinary(@NotNull File basePathFile) {
return builder(basePathFile, DELTA_BINARY);
}
/**
* @param name the file name
* @param deltaIntervalShift default value of 6, the shift for deltaInterval, the should be a
* number between 0-63 ( inclusive ), default the delta messaging is
* check pointed every 64 messages, so the default {@code
* deltaIntervalShift == 6}, as {@code 1 << 6 == 64 }
* @return the SingleChronicleQueueBuilder
*/
public static SingleChronicleQueueBuilder deltaBinary(@NotNull File name, byte deltaIntervalShift) {
@NotNull SingleChronicleQueueBuilder ret = deltaBinary(name);
if (deltaIntervalShift < 0 || deltaIntervalShift > 63)
throw new IllegalArgumentException("deltaIntervalShift=" + deltaIntervalShift + ", but " +
"should be a value between 0-63 inclusive");
ret.deltaCheckpointInterval(1 << deltaIntervalShift);
return ret;
}
@NotNull
static SingleChronicleQueueStore createStore(@NotNull RollingChronicleQueue queue,
@NotNull Wire wire) {
MappedBytes mappedBytes = (MappedBytes) wire.bytes();
final SingleChronicleQueueStore wireStore = new SingleChronicleQueueStore(
queue.rollCycle(),
queue.wireType(),
mappedBytes,
queue.indexCount(),
queue.indexSpacing());
wireStore.fileShrinkage(queue.fileShrinkage());
wire.writeEventName(MetaDataKeys.header).typedMarshallable(wireStore);
return wireStore;
}
static boolean isQueueReplicationAvailable() {
return ENTERPRISE_QUEUE_CONSTRUCTOR != null;
}
private static RollCycle loadDefaultRollCycle() {
String rollCycleProperty = Jvm.getProperty(QueueSystemProperties.DEFAULT_ROLL_CYCLE_PROPERTY);
if (null == rollCycleProperty) {
return RollCycles.DEFAULT;
}
String[] rollCyclePropertyParts = rollCycleProperty.split(":");
if (rollCyclePropertyParts.length > 0) {
try {
Class rollCycleClass = Class.forName(rollCyclePropertyParts[0]);
if (Enum.class.isAssignableFrom(rollCycleClass)) {
if (rollCyclePropertyParts.length < 2) {
Jvm.warn().on(SingleChronicleQueueBuilder.class,
"Default roll cycle configured as enum, but enum value not specified: " + rollCycleProperty);
} else {
@SuppressWarnings("unchecked")
Class<Enum> eClass = (Class<Enum>) rollCycleClass;
Object instance = ObjectUtils.valueOfIgnoreCase(eClass, rollCyclePropertyParts[1]);
if (instance instanceof RollCycle) {
return (RollCycle) instance;
} else {
Jvm.warn().on(SingleChronicleQueueBuilder.class,
"Configured default rollcycle is not a subclass of RollCycle");
}
}
} else {
Object instance = ObjectUtils.newInstance(rollCycleClass);
if (instance instanceof RollCycle) {
return (RollCycle) instance;
} else {
Jvm.warn().on(SingleChronicleQueueBuilder.class,
"Configured default rollcycle is not a subclass of RollCycle");
}
}
} catch (ClassNotFoundException ignored) {
Jvm.warn().on(SingleChronicleQueueBuilder.class,
"Default roll cycle class: " + rollCyclePropertyParts[0] + " was not found");
}
}
return RollCycles.DEFAULT;
}
public WireStoreFactory storeFactory() {
return storeFactory;
}
@NotNull
public SingleChronicleQueue build() {
boolean needEnterprise = checkEnterpriseFeaturesRequested();
preBuild();
if (Boolean.TRUE.equals(useSparseFiles) && sparseCapacity == null &&
(rollCycle == null || rollCycle.lengthInMillis() > 60_000)) {
RollCycle rc = rollCycle == null ? RollCycles.FAST_DAILY : rollCycle;
final long msgs = rc.maxMessagesPerCycle();
sparseCapacity = Math.min(512L << 30, Math.max(4L << 30, msgs * 128));
useSparseFiles = true;
}
SingleChronicleQueue chronicleQueue;
if (needEnterprise)
chronicleQueue = buildEnterprise();
else
chronicleQueue = new SingleChronicleQueue(this);
postBuild(chronicleQueue);
return chronicleQueue;
}
private void postBuild(@NotNull SingleChronicleQueue chronicleQueue) {
if (!readOnly()) {
/*
The condition has a circular dependency with the Queue, so we need to add it after the queue is
constructed. This is to avoid passing `this` out of the constructor.
*/
chronicleQueue.createAppenderCondition(requireNonNull(createAppenderConditionCreator().apply(chronicleQueue)));
}
}
private boolean checkEnterpriseFeaturesRequested() {
boolean result = false;
if (readBufferMode != BufferMode.None)
result = onlyAvailableInEnterprise("Buffering");
if (writeBufferMode != BufferMode.None)
result = onlyAvailableInEnterprise("Buffering");
if (rollTimeZone != null && !rollTimeZone.getId().equals("UTC") && !rollTimeZone.getId().equals("Z"))
result = onlyAvailableInEnterprise("Non-UTC roll time zone");
if (wireType == WireType.DELTA_BINARY)
result = onlyAvailableInEnterprise("Wire type " + wireType.name());
if (encodingSupplier != null)
result = onlyAvailableInEnterprise("Encoding");
if (key != null)
result = onlyAvailableInEnterprise("Encryption");
if (hasPretouchIntervalMillis())
result = onlyAvailableInEnterprise("Out of process pretouching");
return result;
}
private boolean onlyAvailableInEnterprise(final String feature) {
if (ENTERPRISE_QUEUE_CONSTRUCTOR == null)
Jvm.warn().on(getClass(), feature + " is only supported in Chronicle Queue Enterprise. If you would like to use this feature, please contact sales@chronicle.software for more information.");
return true;
}
@NotNull
private SingleChronicleQueue buildEnterprise() {
if (ENTERPRISE_QUEUE_CONSTRUCTOR == null)
throw new IllegalStateException("Enterprise features requested but Chronicle Queue Enterprise is not in the class path!");
try {
return (SingleChronicleQueue) ENTERPRISE_QUEUE_CONSTRUCTOR.newInstance(this);
} catch (Exception e) {
throw new IllegalStateException("Couldn't create an instance of Enterprise queue", e);
}
}
public SingleChronicleQueueBuilder aesEncryption(@Nullable byte[] keyBytes) {
if (keyBytes == null) {
codingSuppliers(null, null);
return this;
}
key = new SecretKeySpec(keyBytes, "AES");
return this;
}
public Updater<Bytes<?>> messageInitializer() {
return messageInitializer == null ? Bytes::clear : messageInitializer;
}
public Consumer<Bytes<?>> messageHeaderReader() {
return messageHeaderReader == null ? b -> {
} : messageHeaderReader;
}
public SingleChronicleQueueBuilder messageHeader(Updater<Bytes<?>> messageInitializer,
Consumer<Bytes<?>> messageHeaderReader) {
this.messageInitializer = messageInitializer;
this.messageHeaderReader = messageHeaderReader;
return this;
}
public SingleChronicleQueueBuilder rollTime(@NotNull final LocalTime rollTime) {
rollTime(rollTime, rollTimeZone);
return this;
}
public ZoneId rollTimeZone() {
return rollTimeZone;
}
public SingleChronicleQueueBuilder rollTimeZone(@NotNull final ZoneId rollTimeZone) {
rollTime(rollTime, rollTimeZone);
return this;
}
public SingleChronicleQueueBuilder rollTime(@NotNull final LocalTime rollTime, @NotNull final ZoneId zoneId) {
this.rollTime = rollTime;
this.rollTimeZone = zoneId;
this.epoch = TimeUnit.SECONDS.toMillis(rollTime.toSecondOfDay());
this.queueOffsetSpec = QueueOffsetSpec.ofRollTime(rollTime, zoneId);
return this;
}
protected void initializeMetadata() {
File metapath = metapath();
validateRollCycle(metapath);
SCQMeta metadata = new SCQMeta(new SCQRoll(rollCycle(), epoch(), rollTime, rollTimeZone), deltaCheckpointInterval(),
sourceId());
try {
boolean readOnly = readOnly();
metaStore = SingleTableBuilder.binary(metapath, metadata).readOnly(readOnly).build();
// check if metadata was overridden
SCQMeta newMeta = metaStore.metadata();
sourceId(newMeta.sourceId());
String format = newMeta.roll().format();
if (!format.equals(rollCycle().format())) {
// roll cycle changed
overrideRollCycleForFileName(format);
}
// if it was overridden - reset
rollTime = newMeta.roll().rollTime();
rollTimeZone = newMeta.roll().rollTimeZone();
epoch = newMeta.roll().epoch();
} catch (IORuntimeException ex) {
// readonly=true and file doesn't exist
if (OS.isWindows())
throw ex; // we cant have a read-only table store on windows so we have no option but to throw the ex.
if (ex.getMessage().equals("Metadata file not found in readOnly mode"))
Jvm.warn().on(getClass(), "Failback to readonly tablestore " + ex);
else
Jvm.warn().on(getClass(), "Failback to readonly tablestore", ex);
metaStore = new ReadonlyTableStore<>(metadata);
}
}
private void validateRollCycle(File metapath) {
if (!metapath.exists()) {
// no metadata, so we need to check if there're cq4 files and if so try to validate roll cycle
// the code is slightly brutal and crude but should work for most cases. It will NOT work if files were created with
// the following cycles: LARGE_HOURLY_SPARSE LARGE_HOURLY_XSPARSE LARGE_DAILY XLARGE_DAILY HUGE_DAILY HUGE_DAILY_XSPARSE
// for such cases user MUST use correct roll cycle when creating the queue
String[] list = path.list((d, name) -> name.endsWith(SingleChronicleQueue.SUFFIX));
if (list != null && list.length > 0) {
String filename = list[0];
for (RollCycles cycle : RollCycles.all()) {
try {
DateTimeFormatter.ofPattern(cycle.format())
.parse(filename.substring(0, filename.length() - 4));
overrideRollCycle(cycle);
break;
} catch (Exception expected) {
}
}
}
}
}
private void overrideRollCycleForFileName(String pattern) {
for (RollCycles cycle : RollCycles.all()) {
if (cycle.format().equals(pattern)) {
overrideRollCycle(cycle);
return;
}
}
throw new IllegalStateException("Can't find an appropriate RollCycles to override to of length " + pattern);
}
private void overrideRollCycle(RollCycles cycle) {
if (rollCycle != cycle && rollCycle != null)
Jvm.warn().on(getClass(), "Overriding roll cycle from " + rollCycle + " to " + cycle);
rollCycle = cycle;
}
private File metapath() {
final File storeFilePath;
if ("".equals(path.getPath())) {
storeFilePath = new File(QUEUE_METADATA_FILE);
} else {
storeFilePath = new File(path, QUEUE_METADATA_FILE);
path.mkdirs();
}
return storeFilePath;
}
@NotNull
public Function<SingleChronicleQueue, Condition> createAppenderConditionCreator() {
if (createAppenderConditionCreator == null) {
return QueueLockUnlockedCondition::new;
}
return createAppenderConditionCreator;
}
/**
* @return Factory for the {@link Condition} that will be waited on before a new appender is created
* <p>
* NOTE: The returned {@link Condition} will not block subsequent calls to acquireAppender from the
* same thread, only when the call would result in the creation of a new appender.
*/
@NotNull
public SingleChronicleQueueBuilder createAppenderConditionCreator(Function<SingleChronicleQueue, Condition> creator) {
createAppenderConditionCreator = creator;
return this;
}
@NotNull
WriteLock writeLock() {
return readOnly() ? new ReadOnlyWriteLock() : new TableStoreWriteLock(metaStore, pauserSupplier(), timeoutMS() * 3 / 2);
}
public int deltaCheckpointInterval() {
return deltaCheckpointInterval == -1 ? 64 : deltaCheckpointInterval;
}
public QueueOffsetSpec queueOffsetSpec() {
return queueOffsetSpec == null ? QueueOffsetSpec.ofNone() : queueOffsetSpec;
}
TableStore<SCQMeta> metaStore() {
return metaStore;
}
/**
* RingBuffer tailers need to be preallocated. Only set this if using readBufferMode=Asynchronous.
* By default 1 tailer will be created for the user.
*
* @param maxTailers number of tailers that will be required from this queue, not including the draining tailer
* @return this
*/
public SingleChronicleQueueBuilder maxTailers(int maxTailers) {
this.maxTailers = maxTailers;
return this;
}
/**
* maxTailers
*
* @return number of tailers that will be required from this queue, not including the draining tailer
*/
public int maxTailers() {
return maxTailers;
}
public SingleChronicleQueueBuilder bufferBytesStoreCreator(ThrowingBiFunction<Long, Integer, BytesStore, Exception> bufferBytesStoreCreator) {
this.bufferBytesStoreCreator = bufferBytesStoreCreator;
return this;
}
/**
* Creator for BytesStore for underlying ring buffer. Allows visibility of RB's data to be controlled.
* See also EnterpriseSingleChronicleQueue.RB_BYTES_STORE_CREATOR_NATIVE, EnterpriseSingleChronicleQueue.RB_BYTES_STORE_CREATOR_MAPPED_FILE.
* <p>
* If you are using more than one {@link ChronicleQueue} object to access the ring buffer'd queue then you
* will need to set this. If this is not set then each queue will create its own in-memory ring buffer
*
* @return bufferBytesStoreCreator
*/
@Nullable
public ThrowingBiFunction<Long, Integer, BytesStore, Exception> bufferBytesStoreCreator() {
return bufferBytesStoreCreator;
}
/**
* Enable out-of-process pretoucher (AKA preloader) (Queue Enterprise feature)
*/
public SingleChronicleQueueBuilder enablePreloader(final long pretouchIntervalMillis) {
this.pretouchIntervalMillis = pretouchIntervalMillis;
return this;
}
/**
* Interval in ms to invoke out of process pretoucher. Default is not to turn on
*
* @return interval ms
*/
public long pretouchIntervalMillis() {
return pretouchIntervalMillis;
}
public boolean hasPretouchIntervalMillis() {
return pretouchIntervalMillis != null;
}
public SingleChronicleQueueBuilder path(String path) {
return path(new File(path));
}
public SingleChronicleQueueBuilder path(final File path) {
this.path = path;
return this;
}
public SingleChronicleQueueBuilder path(final Path path) {
this.path = path.toFile();
return this;
}
/**
* consumer will be called every second, also as there is data to report
*
* @param onRingBufferStats a consumer of the BytesRingBufferStats
* @return this
*/
public SingleChronicleQueueBuilder onRingBufferStats(@NotNull Consumer<BytesRingBufferStats> onRingBufferStats) {
this.onRingBufferStats = onRingBufferStats;
return this;
}
public Consumer<BytesRingBufferStats> onRingBufferStats() {
return this.onRingBufferStats;
}
@NotNull
public File path() {
return this.path;
}
public SingleChronicleQueueBuilder blockSize(long blockSize) {
this.blockSize = Math.max(QueueUtil.testBlockSize(), blockSize);
return this;
}
public SingleChronicleQueueBuilder blockSize(int blockSize) {
return blockSize((long) blockSize);
}
/**
* @return - this is the size of a memory mapping chunk, a queue is read/written by using a number of blocks, you should avoid changing this unnecessarily.
*/
public long blockSize() {
long bs = blockSize == null ? OS.is64Bit() ? 64L << 20 : QueueUtil.testBlockSize() : blockSize;
// can add an index2index & an index in one go.
long minSize = Math.max(QueueUtil.testBlockSize(), 32L * indexCount());
return Math.max(minSize, bs);
}
public SingleChronicleQueueBuilder useSparseFiles(boolean useSparseFiles) {
if (useSparseFiles && OS.isLinux() && OS.is64Bit())
this.useSparseFiles = useSparseFiles;
if (!useSparseFiles)
this.useSparseFiles = useSparseFiles;
return this;
}
public SingleChronicleQueueBuilder sparseCapacity(long sparseCapacity) {
this.sparseCapacity = sparseCapacity;
return this;
}
public long sparseCapacity() {
long bs = sparseCapacity == null ? DEFAULT_SPARSE_CAPACITY : sparseCapacity;
// can add an index2index & an index in one go.
long minSize = Math.max(QueueUtil.testBlockSize(), 64L * indexCount());
return Math.max(minSize, bs);
}
public boolean useSparseFiles() {
return OS.isLinux() && OS.is64Bit() && sparseCapacity != null;
}
/**
* THIS IS FOR TESTING ONLY.
* This makes the block size small to speed up short tests and show up issues which occur when moving from one block to another.
* <p>
* Using this will be slower when you have many messages, and break when you have large messages.
* </p>
*
* @return this
*/
public SingleChronicleQueueBuilder testBlockSize() {
// small size for testing purposes only.
return blockSize(OS.isWindows() ? 64 << 10 : OS.pageSize());
}
@NotNull
public SingleChronicleQueueBuilder wireType(@NotNull WireType wireType) {
if (wireType == WireType.DELTA_BINARY)
deltaCheckpointInterval(64);
this.wireType = wireType;
return this;
}
private void deltaCheckpointInterval(int deltaCheckpointInterval) {
assert checkIsPowerOf2(deltaCheckpointInterval);
this.deltaCheckpointInterval = deltaCheckpointInterval;
}
private boolean checkIsPowerOf2(long value) {
return (value & (value - 1)) == 0;
}
@NotNull
public WireType wireType() {
return this.wireType == null ? WireType.BINARY_LIGHT : wireType;
}
@NotNull
public SingleChronicleQueueBuilder rollCycle(@NotNull RollCycle rollCycle) {
assert rollCycle != null;
this.rollCycle = rollCycle;
return this;
}
@NotNull
public RollCycle rollCycle() {
return this.rollCycle == null ? loadDefaultRollCycle() : this.rollCycle;
}
/**
* @return ring buffer capacity in bytes [ Chronicle-Ring is an enterprise product ]
*/
public long bufferCapacity() {
return Math.min(blockSize() / 4, bufferCapacity == null ? 2 << 20 : bufferCapacity);
}
/**
* @param bufferCapacity sets the ring buffer capacity in bytes
* @return this
*/
@NotNull
public SingleChronicleQueueBuilder bufferCapacity(long bufferCapacity) {
this.bufferCapacity = bufferCapacity;
return this;
}
/**
* sets epoch offset in milliseconds
*
* @param epoch sets an epoch offset as the number of number of milliseconds since January 1,
* 1970, 00:00:00 GMT
* @return {@code this}
*/
@NotNull
public SingleChronicleQueueBuilder epoch(long epoch) {
this.epoch = epoch;
queueOffsetSpec = QueueOffsetSpec.ofEpoch(epoch);
return this;
}
/**
* @return epoch offset as the number of number of milliseconds since January 1, 1970, 00:00:00
* GMT
*/
public long epoch() {
return epoch == null ? Jvm.getLong(QueueSystemProperties.DEFAULT_EPOCH_PROPERTY, 0L) : epoch;
}
/**
* when set to {@code true}. uses a ring buffer to buffer appends, excerpts are written to the
* Chronicle Queue using a background thread. See also {@link #writeBufferMode()}
*
* @param isBuffered {@code true} if the append is buffered
* @return this
*/
@NotNull
@Deprecated // use writeBufferMode(Asynchronous) instead
public SingleChronicleQueueBuilder buffered(boolean isBuffered) {
this.writeBufferMode = isBuffered ? BufferMode.Asynchronous : BufferMode.None;
return this;
}
/**
* @return BufferMode to use for writes. Only None is available is the OSS
*/
@NotNull
public BufferMode writeBufferMode() {
return wireType() == WireType.DELTA_BINARY ? BufferMode.None : (writeBufferMode == null)
? BufferMode.None : writeBufferMode;
}
/**
* When writeBufferMode is set to {@code Asynchronous}, uses a ring buffer to buffer appends, excerpts are written to the
* Chronicle Queue using a background thread.
* See also {@link #bufferCapacity()}
* See also {@link #bufferBytesStoreCreator()}
* See also software.chronicle.enterprise.ring.EnterpriseRingBuffer
*
* @param writeBufferMode bufferMode for writing
* @return this
*/
public SingleChronicleQueueBuilder writeBufferMode(BufferMode writeBufferMode) {
this.writeBufferMode = writeBufferMode;
return this;
}
/**
* @return BufferMode to use for reads. Only None is available is the OSS
*/
public BufferMode readBufferMode() {
return readBufferMode == null ? BufferMode.None : readBufferMode;
}
/**
* When readBufferMode is set to {@code Asynchronous}, reads from the ring buffer. This requires
* that {@link #writeBufferMode()} is also set to {@code Asynchronous}.
* See also {@link #bufferCapacity()}
* See also {@link #bufferBytesStoreCreator()}
* See also software.chronicle.enterprise.ring.EnterpriseRingBuffer
*
* @param readBufferMode BufferMode for read
* @return this
*/
public SingleChronicleQueueBuilder readBufferMode(BufferMode readBufferMode) {
this.readBufferMode = readBufferMode;
return this;
}
/**
* @return a new event loop instance if none has been set, otherwise the {@code eventLoop}
* that was set
*/
@NotNull
public EventLoop eventLoop() {
if (eventLoop == null)
return new OnDemandEventLoop(
() -> new MediumEventLoop(null, path.getName(), Pauser.busy(), true, "none"));
return eventLoop;
}
@NotNull
public SingleChronicleQueueBuilder eventLoop(EventLoop eventLoop) {
this.eventLoop = eventLoop;
return this;
}
/**
* @return if the ring buffer's monitoring capability is turned on. Not available in OSS
*/
public boolean enableRingBufferMonitoring() {
return enableRingBufferMonitoring == null ? false : enableRingBufferMonitoring;
}
public SingleChronicleQueueBuilder enableRingBufferMonitoring(boolean enableRingBufferMonitoring) {
this.enableRingBufferMonitoring = enableRingBufferMonitoring;
return this;
}
/**
* default value is {@code false} since 5.21ea0
*
* @return if ring buffer reader processes can invoke the CQ drainer, otherwise only writer processes can
*/
public boolean ringBufferReaderCanDrain() {
return ringBufferReaderCanDrain == null ? false : ringBufferReaderCanDrain;
}
public SingleChronicleQueueBuilder ringBufferReaderCanDrain(boolean ringBufferReaderCanDrain) {
this.ringBufferReaderCanDrain = ringBufferReaderCanDrain;
return this;
}
/**
* @return whether to force creating a reader (to recover from crash)
*/
public boolean ringBufferForceCreateReader() {
return ringBufferForceCreateReader == null ? false : ringBufferForceCreateReader;
}
public SingleChronicleQueueBuilder ringBufferForceCreateReader(boolean ringBufferForceCreateReader) {
this.ringBufferForceCreateReader = ringBufferForceCreateReader;
return this;
}
/**
* @return if ring buffer readers are not reset on close. If true then re-opening a reader puts you back
* at the same place. If true, your reader can block writers if the reader is not open
*/
public boolean ringBufferReopenReader() {
return ringBufferReopenReader == null ? false : ringBufferReopenReader;
}
public SingleChronicleQueueBuilder ringBufferReopenReader(boolean ringBufferReopenReader) {
this.ringBufferReopenReader = ringBufferReopenReader;
return this;
}
/**
* Priority for ring buffer's drainer handler
*
* @return drainerPriority
*/
public HandlerPriority drainerPriority() {
return drainerPriority == null ? HandlerPriority.MEDIUM : drainerPriority;
}
public SingleChronicleQueueBuilder drainerPriority(HandlerPriority drainerPriority) {
this.drainerPriority = drainerPriority;
return this;
}
public int drainerTimeoutMS() {
return drainerTimeoutMS <= 0 ? 10_000 : drainerTimeoutMS;
}
public SingleChronicleQueueBuilder drainerTimeoutMS(int timeout) {
drainerTimeoutMS = timeout;
return this;
}
/**
* Pauser supplier for the pauser to be used by ring buffer when waiting
*/
public Supplier<Pauser> ringBufferPauserSupplier() {
return ringBufferPauserSupplier == null ? Pauser::busy : ringBufferPauserSupplier;
}
public SingleChronicleQueueBuilder ringBufferPauserSupplier(Supplier<Pauser> ringBufferPauserSupplier) {
this.ringBufferPauserSupplier = ringBufferPauserSupplier;
return this;
}
public SingleChronicleQueueBuilder indexCount(int indexCount) {
this.indexCount = Maths.nextPower2(indexCount, 8);
return this;
}
public int indexCount() {
return indexCount == null || indexCount <= 0 ? rollCycle().defaultIndexCount() : indexCount;
}
public SingleChronicleQueueBuilder indexSpacing(int indexSpacing) {
this.indexSpacing = Maths.nextPower2(indexSpacing, 1);
return this;
}
public int indexSpacing() {
return indexSpacing == null || indexSpacing <= 0 ? rollCycle().defaultIndexSpacing() :
indexSpacing;
}
public TimeProvider timeProvider() {
return timeProvider == null ? SystemTimeProvider.INSTANCE : timeProvider;
}
public SingleChronicleQueueBuilder timeProvider(TimeProvider timeProvider) {
this.timeProvider = timeProvider;
return this;
}