/
MapServiceContextImpl.java
832 lines (726 loc) · 33.2 KB
/
MapServiceContextImpl.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
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
/*
* Copyright (c) 2008-2018, 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.map.impl;
import com.hazelcast.cluster.ClusterState;
import com.hazelcast.config.InMemoryFormat;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MergePolicyConfig;
import com.hazelcast.config.PartitioningStrategyConfig;
import com.hazelcast.core.PartitioningStrategy;
import com.hazelcast.internal.serialization.InternalSerializationService;
import com.hazelcast.internal.util.InvocationUtil;
import com.hazelcast.internal.util.LocalRetryableExecution;
import com.hazelcast.logging.ILogger;
import com.hazelcast.map.MapInterceptor;
import com.hazelcast.map.impl.event.MapEventPublisher;
import com.hazelcast.map.impl.event.MapEventPublisherImpl;
import com.hazelcast.map.impl.eviction.ExpirationManager;
import com.hazelcast.map.impl.journal.MapEventJournal;
import com.hazelcast.map.impl.journal.RingbufferMapEventJournalImpl;
import com.hazelcast.map.impl.mapstore.MapDataStore;
import com.hazelcast.map.impl.nearcache.MapNearCacheManager;
import com.hazelcast.map.impl.operation.BasePutOperation;
import com.hazelcast.map.impl.operation.BaseRemoveOperation;
import com.hazelcast.map.impl.operation.GetOperation;
import com.hazelcast.map.impl.operation.MapOperationProvider;
import com.hazelcast.map.impl.operation.MapOperationProviders;
import com.hazelcast.map.impl.operation.MapPartitionDestroyOperation;
import com.hazelcast.map.impl.query.AccumulationExecutor;
import com.hazelcast.map.impl.query.AggregationResult;
import com.hazelcast.map.impl.query.AggregationResultProcessor;
import com.hazelcast.map.impl.query.CallerRunsAccumulationExecutor;
import com.hazelcast.map.impl.query.CallerRunsPartitionScanExecutor;
import com.hazelcast.map.impl.query.DefaultIndexProvider;
import com.hazelcast.map.impl.query.IndexProvider;
import com.hazelcast.map.impl.query.MapQueryEngine;
import com.hazelcast.map.impl.query.MapQueryEngineImpl;
import com.hazelcast.map.impl.query.ParallelAccumulationExecutor;
import com.hazelcast.map.impl.query.ParallelPartitionScanExecutor;
import com.hazelcast.map.impl.query.PartitionScanExecutor;
import com.hazelcast.map.impl.query.PartitionScanRunner;
import com.hazelcast.map.impl.query.QueryResult;
import com.hazelcast.map.impl.query.QueryResultProcessor;
import com.hazelcast.map.impl.query.QueryRunner;
import com.hazelcast.map.impl.query.ResultProcessorRegistry;
import com.hazelcast.map.impl.querycache.NodeQueryCacheContext;
import com.hazelcast.map.impl.querycache.QueryCacheContext;
import com.hazelcast.map.impl.record.DataRecordComparator;
import com.hazelcast.map.impl.record.ObjectRecordComparator;
import com.hazelcast.map.impl.record.RecordComparator;
import com.hazelcast.map.impl.recordstore.DefaultRecordStore;
import com.hazelcast.map.impl.recordstore.RecordStore;
import com.hazelcast.map.listener.MapPartitionLostListener;
import com.hazelcast.map.merge.MergePolicyProvider;
import com.hazelcast.monitor.impl.LocalMapStatsImpl;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.nio.serialization.DataType;
import com.hazelcast.query.impl.IndexCopyBehavior;
import com.hazelcast.query.impl.getters.Extractors;
import com.hazelcast.query.impl.predicates.QueryOptimizer;
import com.hazelcast.spi.EventFilter;
import com.hazelcast.spi.EventRegistration;
import com.hazelcast.spi.EventService;
import com.hazelcast.spi.NodeEngine;
import com.hazelcast.spi.Operation;
import com.hazelcast.spi.impl.eventservice.impl.TrueEventFilter;
import com.hazelcast.spi.partition.IPartitionService;
import com.hazelcast.spi.serialization.SerializationService;
import com.hazelcast.util.ConcurrencyUtil;
import com.hazelcast.util.ConstructorFunction;
import com.hazelcast.util.ContextMutexFactory;
import com.hazelcast.util.executor.ManagedExecutorService;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static com.hazelcast.map.impl.ListenerAdapters.createListenerAdapter;
import static com.hazelcast.map.impl.MapListenerFlagOperator.setAndGetListenerFlags;
import static com.hazelcast.map.impl.MapService.SERVICE_NAME;
import static com.hazelcast.query.impl.predicates.QueryOptimizerFactory.newOptimizer;
import static com.hazelcast.spi.ExecutionService.QUERY_EXECUTOR;
import static com.hazelcast.spi.Operation.GENERIC_PARTITION_ID;
import static com.hazelcast.spi.properties.GroupProperty.AGGREGATION_ACCUMULATION_PARALLEL_EVALUATION;
import static com.hazelcast.spi.properties.GroupProperty.INDEX_COPY_BEHAVIOR;
import static com.hazelcast.spi.properties.GroupProperty.OPERATION_CALL_TIMEOUT_MILLIS;
import static com.hazelcast.spi.properties.GroupProperty.QUERY_PREDICATE_PARALLEL_EVALUATION;
import static java.lang.Thread.currentThread;
/**
* Default implementation of {@link MapServiceContext}.
*/
@SuppressWarnings("WeakerAccess")
class MapServiceContextImpl implements MapServiceContext {
protected static final long DESTROY_TIMEOUT_SECONDS = 30;
protected final ConcurrentMap<String, MapContainer> mapContainers = new ConcurrentHashMap<String, MapContainer>();
protected final AtomicReference<Collection<Integer>> ownedPartitions = new AtomicReference<Collection<Integer>>();
protected final IndexProvider indexProvider = new DefaultIndexProvider();
protected final ContextMutexFactory contextMutexFactory = new ContextMutexFactory();
protected final Map<InMemoryFormat, RecordComparator> recordComparatorMap
= new HashMap<InMemoryFormat, RecordComparator>();
/**
* Per node global write behind queue item counter.
* Creating here because we want to have a counter per node.
* This is used by owner and backups together so it should be defined
* getting this into account.
*/
protected final AtomicInteger writeBehindQueueItemCounter = new AtomicInteger(0);
protected final NodeEngine nodeEngine;
protected final InternalSerializationService serializationService;
protected final ConstructorFunction<String, MapContainer> mapConstructor;
protected final PartitionContainer[] partitionContainers;
protected final ExpirationManager expirationManager;
protected final MapNearCacheManager mapNearCacheManager;
protected final LocalMapStatsProvider localMapStatsProvider;
protected final MergePolicyProvider mergePolicyProvider;
protected final MapQueryEngine mapQueryEngine;
protected final QueryRunner mapQueryRunner;
protected final PartitionScanRunner partitionScanRunner;
protected final QueryOptimizer queryOptimizer;
protected final PartitioningStrategyFactory partitioningStrategyFactory;
protected final QueryCacheContext queryCacheContext;
protected final MapEventJournal eventJournal;
protected final MapEventPublisher mapEventPublisher;
protected final EventService eventService;
protected final MapOperationProviders operationProviders;
protected final ResultProcessorRegistry resultProcessorRegistry;
protected ILogger logger;
protected MapService mapService;
@SuppressWarnings("checkstyle:executablestatementcount")
MapServiceContextImpl(NodeEngine nodeEngine) {
this.nodeEngine = nodeEngine;
this.serializationService = ((InternalSerializationService) nodeEngine.getSerializationService());
this.mapConstructor = createMapConstructor();
this.queryCacheContext = new NodeQueryCacheContext(this);
this.partitionContainers = createPartitionContainers();
this.expirationManager = new ExpirationManager(partitionContainers, nodeEngine);
this.mapNearCacheManager = createMapNearCacheManager();
this.localMapStatsProvider = createLocalMapStatsProvider();
this.mergePolicyProvider = new MergePolicyProvider(nodeEngine);
this.mapEventPublisher = createMapEventPublisherSupport();
this.eventJournal = createEventJournal();
this.queryOptimizer = newOptimizer(nodeEngine.getProperties());
this.resultProcessorRegistry = createResultProcessorRegistry(serializationService);
this.partitionScanRunner = createPartitionScanRunner();
this.mapQueryEngine = createMapQueryEngine();
this.mapQueryRunner = createMapQueryRunner(nodeEngine, queryOptimizer, resultProcessorRegistry, partitionScanRunner);
this.eventService = nodeEngine.getEventService();
this.operationProviders = createOperationProviders();
this.partitioningStrategyFactory = new PartitioningStrategyFactory(nodeEngine.getConfigClassLoader());
this.logger = nodeEngine.getLogger(getClass());
initRecordComparators();
}
ConstructorFunction<String, MapContainer> createMapConstructor() {
return new ConstructorFunction<String, MapContainer>() {
@Override
public MapContainer createNew(String mapName) {
MapServiceContext mapServiceContext = getService().getMapServiceContext();
return new MapContainer(mapName, nodeEngine.getConfig(), mapServiceContext);
}
};
}
// this method is overridden in another context
MapNearCacheManager createMapNearCacheManager() {
return new MapNearCacheManager(this);
}
// this method is overridden in another context
MapOperationProviders createOperationProviders() {
return new MapOperationProviders(this);
}
// this method is overridden in another context
MapEventPublisherImpl createMapEventPublisherSupport() {
return new MapEventPublisherImpl(this);
}
// this method is overridden in another context
void initRecordComparators() {
recordComparatorMap.put(InMemoryFormat.OBJECT, new ObjectRecordComparator(serializationService));
recordComparatorMap.put(InMemoryFormat.BINARY, new DataRecordComparator(serializationService));
}
private MapEventJournal createEventJournal() {
return new RingbufferMapEventJournalImpl(getNodeEngine(), this);
}
private LocalMapStatsProvider createLocalMapStatsProvider() {
return new LocalMapStatsProvider(this);
}
private MapQueryEngineImpl createMapQueryEngine() {
return new MapQueryEngineImpl(this);
}
private PartitionScanRunner createPartitionScanRunner() {
return new PartitionScanRunner(this);
}
protected QueryRunner createMapQueryRunner(NodeEngine nodeEngine, QueryOptimizer queryOptimizer,
ResultProcessorRegistry resultProcessorRegistry,
PartitionScanRunner partitionScanRunner) {
boolean parallelEvaluation = nodeEngine.getProperties().getBoolean(QUERY_PREDICATE_PARALLEL_EVALUATION);
PartitionScanExecutor partitionScanExecutor;
if (parallelEvaluation) {
int opTimeoutInMillis = nodeEngine.getProperties().getInteger(OPERATION_CALL_TIMEOUT_MILLIS);
ManagedExecutorService queryExecutorService = nodeEngine.getExecutionService().getExecutor(QUERY_EXECUTOR);
partitionScanExecutor = new ParallelPartitionScanExecutor(partitionScanRunner, queryExecutorService,
opTimeoutInMillis);
} else {
partitionScanExecutor = new CallerRunsPartitionScanExecutor(partitionScanRunner);
}
return new QueryRunner(this, queryOptimizer, partitionScanExecutor, resultProcessorRegistry);
}
private ResultProcessorRegistry createResultProcessorRegistry(SerializationService ss) {
ResultProcessorRegistry registry = new ResultProcessorRegistry();
registry.registerProcessor(QueryResult.class, createQueryResultProcessor(ss));
registry.registerProcessor(AggregationResult.class, createAggregationResultProcessor(ss));
return registry;
}
private QueryResultProcessor createQueryResultProcessor(SerializationService ss) {
return new QueryResultProcessor(ss);
}
private AggregationResultProcessor createAggregationResultProcessor(SerializationService ss) {
boolean parallelAccumulation = nodeEngine.getProperties().getBoolean(AGGREGATION_ACCUMULATION_PARALLEL_EVALUATION);
int opTimeoutInMillis = nodeEngine.getProperties().getInteger(OPERATION_CALL_TIMEOUT_MILLIS);
AccumulationExecutor accumulationExecutor;
if (parallelAccumulation) {
ManagedExecutorService queryExecutorService = nodeEngine.getExecutionService().getExecutor(QUERY_EXECUTOR);
accumulationExecutor = new ParallelAccumulationExecutor(queryExecutorService, ss, opTimeoutInMillis);
} else {
accumulationExecutor = new CallerRunsAccumulationExecutor(ss);
}
return new AggregationResultProcessor(accumulationExecutor, serializationService);
}
private PartitionContainer[] createPartitionContainers() {
int partitionCount = nodeEngine.getPartitionService().getPartitionCount();
return new PartitionContainer[partitionCount];
}
@Override
public RecordComparator getRecordComparator(InMemoryFormat inMemoryFormat) {
return recordComparatorMap.get(inMemoryFormat);
}
@Override
public MapContainer getMapContainer(String mapName) {
return ConcurrencyUtil.getOrPutSynchronized(mapContainers, mapName, contextMutexFactory, mapConstructor);
}
@Override
public Map<String, MapContainer> getMapContainers() {
return mapContainers;
}
@Override
public PartitionContainer getPartitionContainer(int partitionId) {
assert partitionId != GENERIC_PARTITION_ID : "Cannot be called with GENERIC_PARTITION_ID";
return partitionContainers[partitionId];
}
@Override
public void initPartitionsContainers() {
final int partitionCount = nodeEngine.getPartitionService().getPartitionCount();
for (int i = 0; i < partitionCount; i++) {
partitionContainers[i] = new PartitionContainer(getService(), i);
}
}
@Override
public void clearMapsHavingLesserBackupCountThan(int partitionId, int backupCount) {
PartitionContainer container = getPartitionContainer(partitionId);
if (container != null) {
Iterator<RecordStore> iter = container.getMaps().values().iterator();
while (iter.hasNext()) {
RecordStore recordStore = iter.next();
final MapContainer mapContainer = recordStore.getMapContainer();
if (backupCount > mapContainer.getTotalBackupCount()) {
recordStore.clearPartition(false, true);
iter.remove();
}
}
}
}
@Override
public void clearPartitionData(int partitionId) {
final PartitionContainer container = partitionContainers[partitionId];
if (container != null) {
for (RecordStore mapPartition : container.getMaps().values()) {
mapPartition.clearPartition(false, true);
}
container.getMaps().clear();
}
}
@Override
public MapService getService() {
return mapService;
}
@Override
public void setService(MapService mapService) {
this.mapService = mapService;
}
@Override
public void clearPartitions(boolean onShutdown, boolean onRecordStoreDestroy) {
for (PartitionContainer container : partitionContainers) {
if (container != null) {
container.clear(onShutdown, onRecordStoreDestroy);
}
}
}
@Override
public void destroyMapStores() {
for (MapContainer mapContainer : mapContainers.values()) {
MapStoreWrapper store = mapContainer.getMapStoreContext().getMapStoreWrapper();
if (store != null) {
store.destroy();
}
}
}
@Override
public void flushMaps() {
for (MapContainer mapContainer : mapContainers.values()) {
mapContainer.getMapStoreContext().stop();
}
for (PartitionContainer partitionContainer : partitionContainers) {
for (String mapName : mapContainers.keySet()) {
RecordStore recordStore = partitionContainer.getExistingRecordStore(mapName);
if (recordStore != null) {
MapDataStore mapDataStore = recordStore.getMapDataStore();
mapDataStore.hardFlush();
}
}
}
}
@Override
public void destroyMap(String mapName) {
// on LiteMembers we don't have a MapContainer, but we may have a Near Cache and listeners
mapNearCacheManager.destroyNearCache(mapName);
nodeEngine.getEventService().deregisterAllListeners(SERVICE_NAME, mapName);
MapContainer mapContainer = mapContainers.get(mapName);
if (mapContainer == null) {
return;
}
nodeEngine.getWanReplicationService().removeWanEventCounters(MapService.SERVICE_NAME, mapName);
mapContainer.getMapStoreContext().stop();
localMapStatsProvider.destroyLocalMapStatsImpl(mapContainer.getName());
destroyPartitionsAndMapContainer(mapContainer);
}
/**
* Destroys the map data on local partition threads and waits for
* {@value #DESTROY_TIMEOUT_SECONDS} seconds
* for each partition segment destruction to complete.
*
* @param mapContainer the map container to destroy
*/
private void destroyPartitionsAndMapContainer(MapContainer mapContainer) {
final List<LocalRetryableExecution> executions = new ArrayList<LocalRetryableExecution>();
for (PartitionContainer container : partitionContainers) {
final MapPartitionDestroyOperation op = new MapPartitionDestroyOperation(container, mapContainer);
executions.add(InvocationUtil.executeLocallyWithRetry(nodeEngine, op));
}
for (LocalRetryableExecution execution : executions) {
try {
if (!execution.awaitCompletion(DESTROY_TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
logger.warning("Map partition was not destroyed in expected time, possible leak");
}
} catch (InterruptedException e) {
currentThread().interrupt();
nodeEngine.getLogger(getClass()).warning(e);
}
}
}
@Override
public void reset() {
clearPartitions(false, false);
mapNearCacheManager.reset();
}
@Override
public void shutdown() {
clearPartitions(true, false);
mapNearCacheManager.shutdown();
mapContainers.clear();
}
@Override
public RecordStore getRecordStore(int partitionId, String mapName) {
return getPartitionContainer(partitionId).getRecordStore(mapName);
}
@Override
public RecordStore getRecordStore(int partitionId, String mapName, boolean skipLoadingOnCreate) {
return getPartitionContainer(partitionId).getRecordStore(mapName, skipLoadingOnCreate);
}
@Override
public RecordStore getExistingRecordStore(int partitionId, String mapName) {
return getPartitionContainer(partitionId).getExistingRecordStore(mapName);
}
@Override
public Collection<Integer> getOwnedPartitions() {
Collection<Integer> partitions = ownedPartitions.get();
if (partitions == null) {
reloadOwnedPartitions();
partitions = ownedPartitions.get();
}
return partitions;
}
/**
* {@inheritDoc}
* <p>
* The method will set the owned partition set in a CAS loop because
* this method can be called concurrently.
*/
@Override
public void reloadOwnedPartitions() {
final IPartitionService partitionService = nodeEngine.getPartitionService();
for (; ; ) {
final Collection<Integer> expected = ownedPartitions.get();
final Collection<Integer> partitions = partitionService.getMemberPartitions(nodeEngine.getThisAddress());
final Set<Integer> newSet = Collections.unmodifiableSet(new LinkedHashSet<Integer>(partitions));
if (ownedPartitions.compareAndSet(expected, newSet)) {
return;
}
}
}
@Override
public AtomicInteger getWriteBehindQueueItemCounter() {
return writeBehindQueueItemCounter;
}
@Override
public ExpirationManager getExpirationManager() {
return expirationManager;
}
@Override
public NodeEngine getNodeEngine() {
return nodeEngine;
}
@Override
public MergePolicyProvider getMergePolicyProvider() {
return mergePolicyProvider;
}
@Override
public Object getMergePolicy(String name) {
MapContainer mapContainer = getMapContainer(name);
MergePolicyConfig mergePolicyConfig = mapContainer.getMapConfig().getMergePolicyConfig();
return mergePolicyProvider.getMergePolicy(mergePolicyConfig.getPolicy());
}
@Override
public MapEventPublisher getMapEventPublisher() {
return mapEventPublisher;
}
@Override
public MapEventJournal getEventJournal() {
return eventJournal;
}
@Override
public MapQueryEngine getMapQueryEngine(String mapName) {
return mapQueryEngine;
}
@Override
public QueryRunner getMapQueryRunner(String name) {
return mapQueryRunner;
}
@Override
public QueryOptimizer getQueryOptimizer() {
return queryOptimizer;
}
@Override
public LocalMapStatsProvider getLocalMapStatsProvider() {
return localMapStatsProvider;
}
@Override
public Object toObject(Object data) {
return serializationService.toObject(data);
}
@Override
public Data toData(Object object, PartitioningStrategy partitionStrategy) {
return serializationService.toData(object, partitionStrategy);
}
@Override
public Data toData(Object object) {
return serializationService.toData(object, DataType.HEAP);
}
@Override
public void interceptAfterGet(String mapName, Object value) {
MapContainer mapContainer = getMapContainer(mapName);
List<MapInterceptor> interceptors = mapContainer.getInterceptorRegistry().getInterceptors();
if (!interceptors.isEmpty()) {
value = toObject(value);
for (MapInterceptor interceptor : interceptors) {
interceptor.afterGet(value);
}
}
}
@Override
public Object interceptPut(String mapName, Object oldValue, Object newValue) {
MapContainer mapContainer = getMapContainer(mapName);
List<MapInterceptor> interceptors = mapContainer.getInterceptorRegistry().getInterceptors();
Object result = null;
if (!interceptors.isEmpty()) {
result = toObject(newValue);
oldValue = toObject(oldValue);
for (MapInterceptor interceptor : interceptors) {
Object temp = interceptor.interceptPut(oldValue, result);
if (temp != null) {
result = temp;
}
}
}
return result == null ? newValue : result;
}
@Override
public void interceptAfterPut(String mapName, Object newValue) {
MapContainer mapContainer = getMapContainer(mapName);
List<MapInterceptor> interceptors = mapContainer.getInterceptorRegistry().getInterceptors();
if (!interceptors.isEmpty()) {
newValue = toObject(newValue);
for (MapInterceptor interceptor : interceptors) {
interceptor.afterPut(newValue);
}
}
}
@Override
public Object interceptRemove(String mapName, Object value) {
MapContainer mapContainer = getMapContainer(mapName);
List<MapInterceptor> interceptors = mapContainer.getInterceptorRegistry().getInterceptors();
Object result = null;
if (!interceptors.isEmpty()) {
result = toObject(value);
for (MapInterceptor interceptor : interceptors) {
Object temp = interceptor.interceptRemove(result);
if (temp != null) {
result = temp;
}
}
}
return result == null ? value : result;
}
@Override
public void interceptAfterRemove(String mapName, Object value) {
MapContainer mapContainer = getMapContainer(mapName);
InterceptorRegistry interceptorRegistry = mapContainer.getInterceptorRegistry();
List<MapInterceptor> interceptors = interceptorRegistry.getInterceptors();
if (!interceptors.isEmpty()) {
for (MapInterceptor interceptor : interceptors) {
value = toObject(value);
interceptor.afterRemove(value);
}
}
}
@Override
public void addInterceptor(String id, String mapName, MapInterceptor interceptor) {
MapContainer mapContainer = getMapContainer(mapName);
mapContainer.getInterceptorRegistry().register(id, interceptor);
}
@Override
public String generateInterceptorId(String mapName, MapInterceptor interceptor) {
return interceptor.getClass().getName() + interceptor.hashCode();
}
@Override
public void removeInterceptor(String mapName, String id) {
MapContainer mapContainer = getMapContainer(mapName);
mapContainer.getInterceptorRegistry().deregister(id);
}
// TODO: interceptors should get a wrapped object which includes the serialized version
@Override
public Object interceptGet(String mapName, Object value) {
MapContainer mapContainer = getMapContainer(mapName);
InterceptorRegistry interceptorRegistry = mapContainer.getInterceptorRegistry();
List<MapInterceptor> interceptors = interceptorRegistry.getInterceptors();
Object result = null;
if (!interceptors.isEmpty()) {
result = toObject(value);
for (MapInterceptor interceptor : interceptors) {
Object temp = interceptor.interceptGet(result);
if (temp != null) {
result = temp;
}
}
}
return result == null ? value : result;
}
@Override
public boolean hasInterceptor(String mapName) {
MapContainer mapContainer = getMapContainer(mapName);
return !mapContainer.getInterceptorRegistry().getInterceptors().isEmpty();
}
@Override
public String addLocalEventListener(Object listener, String mapName) {
EventRegistration registration = addListenerInternal(listener, TrueEventFilter.INSTANCE, mapName, true);
return registration.getId();
}
@Override
public String addLocalEventListener(Object listener, EventFilter eventFilter, String mapName) {
EventRegistration registration = addListenerInternal(listener, eventFilter, mapName, true);
return registration.getId();
}
@Override
public String addLocalPartitionLostListener(MapPartitionLostListener listener, String mapName) {
ListenerAdapter listenerAdapter = new InternalMapPartitionLostListenerAdapter(listener);
EventFilter filter = new MapPartitionLostEventFilter();
EventRegistration registration = eventService.registerLocalListener(SERVICE_NAME, mapName, filter, listenerAdapter);
return registration.getId();
}
@Override
public String addEventListener(Object listener, EventFilter eventFilter, String mapName) {
EventRegistration registration = addListenerInternal(listener, eventFilter, mapName, false);
return registration.getId();
}
@Override
public String addPartitionLostListener(MapPartitionLostListener listener, String mapName) {
ListenerAdapter listenerAdapter = new InternalMapPartitionLostListenerAdapter(listener);
EventFilter filter = new MapPartitionLostEventFilter();
EventRegistration registration = eventService.registerListener(SERVICE_NAME, mapName, filter, listenerAdapter);
return registration.getId();
}
private EventRegistration addListenerInternal(Object listener, EventFilter filter, String mapName, boolean local) {
ListenerAdapter listenerAdaptor = createListenerAdapter(listener);
if (!(filter instanceof EventListenerFilter)) {
int enabledListeners = setAndGetListenerFlags(listenerAdaptor);
filter = new EventListenerFilter(enabledListeners, filter);
}
if (local) {
return eventService.registerLocalListener(SERVICE_NAME, mapName, filter, listenerAdaptor);
} else {
return eventService.registerListener(SERVICE_NAME, mapName, filter, listenerAdaptor);
}
}
@Override
public boolean removeEventListener(String mapName, String registrationId) {
return eventService.deregisterListener(SERVICE_NAME, mapName, registrationId);
}
@Override
public boolean removePartitionLostListener(String mapName, String registrationId) {
return eventService.deregisterListener(SERVICE_NAME, mapName, registrationId);
}
@Override
public MapOperationProvider getMapOperationProvider(String name) {
return operationProviders.getOperationProvider(name);
}
@Override
public MapOperationProvider getMapOperationProvider(MapConfig mapConfig) {
return operationProviders.getOperationProvider(mapConfig);
}
@Override
public IndexProvider getIndexProvider(MapConfig mapConfig) {
return indexProvider;
}
@Override
public Extractors getExtractors(String mapName) {
MapContainer mapContainer = getMapContainer(mapName);
return mapContainer.getExtractors();
}
@Override
public void incrementOperationStats(long startTime, LocalMapStatsImpl localMapStats, String mapName, Operation operation) {
final long durationNanos = System.nanoTime() - startTime;
if (operation instanceof BasePutOperation) {
localMapStats.incrementPutLatencyNanos(durationNanos);
} else if (operation instanceof BaseRemoveOperation) {
localMapStats.incrementRemoveLatencyNanos(durationNanos);
} else if (operation instanceof GetOperation) {
localMapStats.incrementGetLatencyNanos(durationNanos);
}
}
@Override
public RecordStore createRecordStore(MapContainer mapContainer, int partitionId, MapKeyLoader keyLoader) {
assert partitionId != GENERIC_PARTITION_ID : "Cannot be called with GENERIC_PARTITION_ID";
ILogger logger = nodeEngine.getLogger(DefaultRecordStore.class);
return new DefaultRecordStore(mapContainer, partitionId, keyLoader, logger);
}
@Override
public boolean removeMapContainer(MapContainer mapContainer) {
return mapContainers.remove(mapContainer.getName(), mapContainer);
}
@Override
public PartitioningStrategy getPartitioningStrategy(String mapName, PartitioningStrategyConfig config) {
return partitioningStrategyFactory.getPartitioningStrategy(mapName, config);
}
@Override
public void removePartitioningStrategyFromCache(String mapName) {
partitioningStrategyFactory.removePartitioningStrategyFromCache(mapName);
}
@Override
public PartitionContainer[] getPartitionContainers() {
return partitionContainers;
}
@Override
public void onClusterStateChange(ClusterState newState) {
expirationManager.onClusterStateChange(newState);
}
@Override
public PartitionScanRunner getPartitionScanRunner() {
return partitionScanRunner;
}
@Override
public ResultProcessorRegistry getResultProcessorRegistry() {
return resultProcessorRegistry;
}
@Override
public MapNearCacheManager getMapNearCacheManager() {
return mapNearCacheManager;
}
@Override
public String addListenerAdapter(ListenerAdapter listenerAdaptor, EventFilter eventFilter, String mapName) {
EventRegistration registration = getNodeEngine().getEventService().
registerListener(MapService.SERVICE_NAME, mapName, eventFilter, listenerAdaptor);
return registration.getId();
}
@Override
public String addLocalListenerAdapter(ListenerAdapter adapter, String mapName) {
EventService eventService = getNodeEngine().getEventService();
EventRegistration registration = eventService.registerLocalListener(MapService.SERVICE_NAME, mapName, adapter);
return registration.getId();
}
@Override
public QueryCacheContext getQueryCacheContext() {
return queryCacheContext;
}
@Override
public IndexCopyBehavior getIndexCopyBehavior() {
return nodeEngine.getProperties().getEnum(INDEX_COPY_BEHAVIOR, IndexCopyBehavior.class);
}
}