/
MapStoreConfig.java
459 lines (415 loc) · 14.7 KB
/
MapStoreConfig.java
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
/*
* Copyright (c) 2008-2024, Hazelcast, Inc. All Rights Reserved.
*
* 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 com.hazelcast.config;
import com.hazelcast.client.impl.protocol.util.PropertiesUtil;
import com.hazelcast.internal.config.ConfigDataSerializerHook;
import com.hazelcast.map.MapStore;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;
import com.hazelcast.nio.serialization.impl.Versioned;
import com.hazelcast.spi.properties.ClusterProperty;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Objects;
import java.util.Properties;
import static com.hazelcast.internal.util.Preconditions.checkHasText;
import static com.hazelcast.internal.util.Preconditions.checkNotNull;
/**
* Contains the configuration for a Map Store.
*/
@SuppressWarnings({"checkstyle:methodcount", "checkstyle:cyclomaticcomplexity"})
public class MapStoreConfig implements IdentifiedDataSerializable, Versioned {
/**
* Default delay seconds for writing
*/
public static final int DEFAULT_WRITE_DELAY_SECONDS = 0;
/**
* Default batch size for writing
*/
public static final int DEFAULT_WRITE_BATCH_SIZE = 1;
/**
* Default write coalescing behavior
*/
public static final boolean DEFAULT_WRITE_COALESCING = true;
/**
* Default offload behavior
*/
public static final boolean DEFAULT_OFFLOAD = true;
private boolean enabled = true;
private boolean offload = DEFAULT_OFFLOAD;
private boolean writeCoalescing = DEFAULT_WRITE_COALESCING;
private int writeDelaySeconds = DEFAULT_WRITE_DELAY_SECONDS;
private int writeBatchSize = DEFAULT_WRITE_BATCH_SIZE;
private String className;
private String factoryClassName;
private Object implementation;
private Object factoryImplementation;
private Properties properties;
private InitialLoadMode initialLoadMode = InitialLoadMode.LAZY;
/**
* Initial load module
*/
public enum InitialLoadMode {
/**
* Each partition is loaded when it is first touched.
*/
LAZY,
/**
* getMap() method does not return till the map is completely loaded.
*/
EAGER
}
public MapStoreConfig() {
properties = new Properties();
}
public MapStoreConfig(MapStoreConfig config) {
enabled = config.isEnabled();
className = config.getClassName();
implementation = config.getImplementation();
factoryClassName = config.getFactoryClassName();
factoryImplementation = config.getFactoryImplementation();
writeDelaySeconds = config.getWriteDelaySeconds();
writeBatchSize = config.getWriteBatchSize();
initialLoadMode = config.getInitialLoadMode();
writeCoalescing = config.isWriteCoalescing();
offload = config.isOffload();
properties = PropertiesUtil.clone(config.getProperties());
}
/**
* Returns the name of the MapStore implementation class
*
* @return the name of the MapStore implementation class
*/
public String getClassName() {
return className;
}
/**
* Sets the name for the MapStore implementation class
*
* @param className the name to set for the MapStore implementation class
*/
public MapStoreConfig setClassName(@Nonnull String className) {
this.className = checkHasText(className, "Map store class name must contain text");
this.implementation = null;
return this;
}
/**
* Returns the name of the MapStoreFactory implementation class
*
* @return the name of the MapStoreFactory implementation class
*/
public String getFactoryClassName() {
return factoryClassName;
}
/**
* Sets the name for the MapStoreFactory implementation class
*
* @param factoryClassName the name to set for the MapStoreFactory implementation class
*/
public MapStoreConfig setFactoryClassName(@Nonnull String factoryClassName) {
this.factoryClassName = checkHasText(factoryClassName, "Map store factory class name must contain text");
this.factoryImplementation = null;
return this;
}
/**
* Returns the number of seconds to delay the store writes.
*
* @return the number of seconds to delay the store writes
*/
public int getWriteDelaySeconds() {
return writeDelaySeconds;
}
/**
* Sets the number of seconds to delay before writing (storing) the dirty records
* <p>
* Default value is {@value #DEFAULT_WRITE_DELAY_SECONDS}.
*
* @param writeDelaySeconds the number of seconds to delay before writing the dirty records
*/
public MapStoreConfig setWriteDelaySeconds(int writeDelaySeconds) {
this.writeDelaySeconds = writeDelaySeconds;
return this;
}
/**
* Returns the number of operations to be included in each batch processing round.
*
* @return write batch size: the number of operations to be included in each batch processing round
*/
public int getWriteBatchSize() {
return writeBatchSize;
}
/**
* Sets the number of operations to be included in each batch processing round.
* <p>
* Default value is {@value #DEFAULT_WRITE_BATCH_SIZE}.
*
* @param writeBatchSize the number of operations to be included in each batch processing round
*/
public MapStoreConfig setWriteBatchSize(int writeBatchSize) {
if (writeBatchSize < 1) {
throw new IllegalArgumentException("Write batch size must be at least 1");
}
this.writeBatchSize = writeBatchSize;
return this;
}
/**
* Returns if this configuration is enabled
*
* @return {@code true} if this configuration is enabled, {@code false} otherwise
*/
public boolean isEnabled() {
return enabled;
}
/**
* Enables and disables this configuration
*
* @param enabled {@code true} to enables this configuration, {@code false} to disable
*/
public MapStoreConfig setEnabled(boolean enabled) {
this.enabled = enabled;
return this;
}
/**
* Default value is {@value #DEFAULT_OFFLOAD}.
* <p>
* {@link MapStore} interaction can be slow compared to in-memory
* processing of data, to make partition threads available during
* {@link MapStore} interaction for other cluster operations,
* MapStore/MapLoader implementations are offloaded from
* partition threads to a different thread pool by default.
* <p>
* If you set this property to {@code false}, {@link
* MapStore} interaction will be done on partition threads.
* This may introduce latency for other operations
* which don't require {@link MapStore} interaction.
*
* @param offload set {@code true} to enable
* offloading, otherwise set {@code false}
* @since 5.2
*/
public MapStoreConfig setOffload(boolean offload) {
this.offload = offload;
return this;
}
/**
* Default value is {@value #DEFAULT_OFFLOAD}.
* <p>
* Enables and disables offloading behavior.
*
* @return {@code true} if this configuration
* is enabled, {@code false} otherwise
* @see #setOffload
* @since 5.2
*/
public boolean isOffload() {
return offload;
}
/**
* Sets the map store implementation object
*
* @param implementation the map store implementation object to set
* @return this MapStoreConfig instance
*/
public MapStoreConfig setImplementation(@Nonnull Object implementation) {
this.implementation = checkNotNull(implementation, "Map store cannot be null!");
this.className = null;
return this;
}
/**
* Returns the map store implementation object.
*
* @return the map store implementation object
*/
public Object getImplementation() {
return implementation;
}
/**
* Sets the map store factory implementation object.
*
* @param factoryImplementation the factory implementation object
* @return this MapStoreConfig instance
*/
public MapStoreConfig setFactoryImplementation(@Nonnull Object factoryImplementation) {
this.factoryImplementation = checkNotNull(factoryImplementation, "Map store factory cannot be null!");
this.factoryClassName = null;
return this;
}
/**
* Returns the map store factory implementation object.
*
* @return the map store factory implementation object
*/
public Object getFactoryImplementation() {
return factoryImplementation;
}
public MapStoreConfig setProperty(String name, String value) {
properties.setProperty(name, value);
return this;
}
/**
* Returns the given property
*
* @return the given property
*/
public String getProperty(String name) {
return properties.getProperty(name);
}
/**
* Returns all the properties
*
* @return all the properties
*/
public Properties getProperties() {
return properties;
}
/**
* Sets the properties
*
* @param properties the properties to be set
* @return this MapStoreConfig
*/
public MapStoreConfig setProperties(Properties properties) {
this.properties = checkNotNull(properties, "Map store config properties cannot be null!");
return this;
}
/**
* Returns the initial load mode.
*
* @return the initial load mode object
*/
public InitialLoadMode getInitialLoadMode() {
return initialLoadMode;
}
/**
* Sets the initial load mode.
* <ul>
* <li>LAZY: Default load mode where load is async</li>
* <li>EAGER: load mode where load is blocked till all partitions are loaded</li>
* </ul>
*
* @param initialLoadMode the initial load mode object
*/
public MapStoreConfig setInitialLoadMode(InitialLoadMode initialLoadMode) {
this.initialLoadMode = initialLoadMode;
return this;
}
/**
* Returns {@code true} if write-coalescing is enabled.
*
* @return {@code true} if coalescing enabled, {@code false} otherwise
* @see #setWriteCoalescing(boolean)
*/
public boolean isWriteCoalescing() {
return writeCoalescing;
}
/**
* Setting {@link #writeCoalescing} is meaningful
* if you are using write-behind {@link MapStore}.
* <p>
* When {@link #writeCoalescing} is {@code true}, only the latest
* store operation on a key in the {@link #writeDelaySeconds}
* time-window will be reflected to {@link MapStore}.
* <p>
* Default value is {@value #DEFAULT_WRITE_COALESCING}.
*
* @param writeCoalescing {@code true} to enable
* write-coalescing, otherwise {@code false}.
* @see ClusterProperty#MAP_WRITE_BEHIND_QUEUE_CAPACITY
*/
public MapStoreConfig setWriteCoalescing(boolean writeCoalescing) {
this.writeCoalescing = writeCoalescing;
return this;
}
@Override
public String toString() {
return "MapStoreConfig{"
+ "enabled=" + enabled
+ ", className='" + className + '\''
+ ", factoryClassName='" + factoryClassName + '\''
+ ", writeDelaySeconds=" + writeDelaySeconds
+ ", writeBatchSize=" + writeBatchSize
+ ", implementation=" + implementation
+ ", factoryImplementation=" + factoryImplementation
+ ", properties=" + properties
+ ", initialLoadMode=" + initialLoadMode
+ ", writeCoalescing=" + writeCoalescing
+ ", offload=" + offload
+ '}';
}
@Override
public final boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof MapStoreConfig)) {
return false;
}
MapStoreConfig that = (MapStoreConfig) o;
return enabled == that.enabled
&& writeCoalescing == that.writeCoalescing
&& writeDelaySeconds == that.writeDelaySeconds
&& writeBatchSize == that.writeBatchSize
&& Objects.equals(implementation, that.implementation)
&& Objects.equals(className, that.className)
&& Objects.equals(factoryImplementation, that.factoryImplementation)
&& Objects.equals(factoryClassName, that.factoryClassName)
&& properties.equals(that.properties)
&& initialLoadMode == that.initialLoadMode
&& offload == that.offload;
}
@Override
public final int hashCode() {
return Objects.hash(enabled, writeCoalescing, implementation, className, factoryImplementation, factoryClassName,
writeDelaySeconds, writeBatchSize, properties, initialLoadMode, offload);
}
@Override
public int getFactoryId() {
return ConfigDataSerializerHook.F_ID;
}
@Override
public int getClassId() {
return ConfigDataSerializerHook.MAP_STORE_CONFIG;
}
@Override
public void writeData(ObjectDataOutput out) throws IOException {
out.writeBoolean(enabled);
out.writeBoolean(writeCoalescing);
out.writeString(className);
out.writeString(factoryClassName);
out.writeInt(writeDelaySeconds);
out.writeInt(writeBatchSize);
out.writeObject(implementation);
out.writeObject(factoryImplementation);
out.writeObject(properties);
out.writeString(initialLoadMode.name());
out.writeBoolean(offload);
}
@Override
public void readData(ObjectDataInput in) throws IOException {
enabled = in.readBoolean();
writeCoalescing = in.readBoolean();
className = in.readString();
factoryClassName = in.readString();
writeDelaySeconds = in.readInt();
writeBatchSize = in.readInt();
implementation = in.readObject();
factoryImplementation = in.readObject();
properties = in.readObject();
initialLoadMode = InitialLoadMode.valueOf(in.readString());
offload = in.readBoolean();
}
}