forked from hazelcast/hazelcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapReplicationStateHolder.java
408 lines (349 loc) · 17.3 KB
/
MapReplicationStateHolder.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
/*
* Copyright (c) 2008-2021, 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.operation;
import com.hazelcast.config.EvictionConfig;
import com.hazelcast.config.IndexConfig;
import com.hazelcast.config.MapConfig;
import com.hazelcast.internal.cluster.Versions;
import com.hazelcast.internal.monitor.LocalRecordStoreStats;
import com.hazelcast.internal.monitor.impl.LocalRecordStoreStatsImpl;
import com.hazelcast.internal.nio.IOUtil;
import com.hazelcast.internal.partition.IPartitionService;
import com.hazelcast.internal.serialization.Data;
import com.hazelcast.internal.serialization.SerializationService;
import com.hazelcast.internal.services.ObjectNamespace;
import com.hazelcast.internal.services.ServiceNamespace;
import com.hazelcast.internal.util.Clock;
import com.hazelcast.internal.util.ExceptionUtil;
import com.hazelcast.internal.util.ThreadUtil;
import com.hazelcast.map.impl.MapContainer;
import com.hazelcast.map.impl.MapDataSerializerHook;
import com.hazelcast.map.impl.MapService;
import com.hazelcast.map.impl.MapServiceContext;
import com.hazelcast.map.impl.PartitionContainer;
import com.hazelcast.map.impl.eviction.Evictor;
import com.hazelcast.map.impl.record.Record;
import com.hazelcast.map.impl.record.Records;
import com.hazelcast.map.impl.recordstore.RecordStore;
import com.hazelcast.map.impl.recordstore.expiry.ExpiryMetadata;
import com.hazelcast.map.impl.recordstore.expiry.ExpiryMetadataImpl;
import com.hazelcast.map.impl.recordstore.expiry.ExpiryReason;
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.query.impl.Index;
import com.hazelcast.query.impl.Indexes;
import com.hazelcast.query.impl.InternalIndex;
import com.hazelcast.query.impl.MapIndexInfo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.hazelcast.config.MaxSizePolicy.PER_NODE;
import static com.hazelcast.internal.util.MapUtil.createHashMap;
import static com.hazelcast.internal.util.MapUtil.isNullOrEmpty;
/**
* Holder for raw IMap key-value pairs and their metadata.
*/
// keep this `protected`, extended in another context.
public class MapReplicationStateHolder implements IdentifiedDataSerializable, Versioned {
// holds recordStore-references of this partitions' maps
protected transient Map<String, RecordStore<Record>> storesByMapName;
// data for each map
protected transient Map<String, List> data;
// propagates the information if the given record store has been already loaded with map-loaded
// if so, the loading won't be triggered again after a migration to avoid duplicate loading.
protected transient Map<String, Boolean> loaded;
// Definitions of indexes for each map. The indexes are sent in the map-replication operation for each partition
// since only this approach guarantees that that there is no race between index migration and data migration.
// Earlier the index definition used to arrive in the post-join operations, but these operation has no guarantee
// on order of execution, so it was possible that the post-join operations were executed after some map-replication
// operations, which meant that the index did not include some data.
protected transient List<MapIndexInfo> mapIndexInfos;
private MapReplicationOperation operation;
private Map<String, LocalRecordStoreStats> recordStoreStatsPerMapName;
/**
* This constructor exists solely for instantiation by {@code MapDataSerializerHook}. The object is not ready to use
* unless {@code operation} is set.
*/
public MapReplicationStateHolder() {
}
public void setOperation(MapReplicationOperation operation) {
this.operation = operation;
}
void prepare(PartitionContainer container, Collection<ServiceNamespace> namespaces, int replicaIndex) {
storesByMapName = createHashMap(namespaces.size());
loaded = createHashMap(namespaces.size());
mapIndexInfos = new ArrayList<>(namespaces.size());
for (ServiceNamespace namespace : namespaces) {
ObjectNamespace mapNamespace = (ObjectNamespace) namespace;
String mapName = mapNamespace.getObjectName();
RecordStore recordStore = container.getExistingRecordStore(mapName);
if (recordStore == null) {
continue;
}
MapContainer mapContainer = recordStore.getMapContainer();
MapConfig mapConfig = mapContainer.getMapConfig();
if (mapConfig.getTotalBackupCount() < replicaIndex) {
continue;
}
loaded.put(mapName, recordStore.isLoaded());
storesByMapName.put(mapName, recordStore);
Set<IndexConfig> indexConfigs = new HashSet<>();
if (mapContainer.isGlobalIndexEnabled()) {
// global-index
final Indexes indexes = mapContainer.getIndexes();
for (Index index : indexes.getIndexes()) {
indexConfigs.add(index.getConfig());
}
indexConfigs.addAll(indexes.getIndexDefinitions());
} else {
// partitioned-index
final Indexes indexes = mapContainer.getIndexes(container.getPartitionId());
if (indexes != null && indexes.haveAtLeastOneIndexOrDefinition()) {
for (Index index : indexes.getIndexes()) {
indexConfigs.add(index.getConfig());
}
indexConfigs.addAll(indexes.getIndexDefinitions());
}
}
MapIndexInfo mapIndexInfo = new MapIndexInfo(mapName);
mapIndexInfo.addIndexCofigs(indexConfigs);
mapIndexInfos.add(mapIndexInfo);
}
}
@SuppressWarnings({"checkstyle:npathcomplexity",
"checkstyle:cyclomaticcomplexity", "checkstyle:nestedifdepth"})
void applyState() {
ThreadUtil.assertRunningOnPartitionThread();
applyIndexesState();
if (!isNullOrEmpty(data)) {
for (Map.Entry<String, List> dataEntry : data.entrySet()) {
String mapName = dataEntry.getKey();
List keyRecordExpiry = dataEntry.getValue();
RecordStore recordStore = operation.getRecordStore(mapName);
recordStore.reset();
recordStore.setPreMigrationLoadedStatus(loaded.get(mapName));
MapContainer mapContainer = recordStore.getMapContainer();
PartitionContainer partitionContainer = recordStore.getMapContainer().getMapServiceContext()
.getPartitionContainer(operation.getPartitionId());
for (Map.Entry<String, IndexConfig> indexDefinition : mapContainer.getIndexDefinitions().entrySet()) {
Indexes indexes = mapContainer.getIndexes(partitionContainer.getPartitionId());
indexes.addOrGetIndex(indexDefinition.getValue());
}
final Indexes indexes = mapContainer.getIndexes(partitionContainer.getPartitionId());
final boolean populateIndexes = indexesMustBePopulated(indexes, operation);
InternalIndex[] indexesSnapshot = null;
if (populateIndexes) {
// defensively clear possible stale leftovers in non-global indexes from the previous failed promotion attempt
indexesSnapshot = indexes.getIndexes();
Indexes.beginPartitionUpdate(indexesSnapshot);
indexes.clearAll();
}
long ownedEntryCountOnThisNode = entryCountOnThisNode(mapContainer);
EvictionConfig evictionConfig = mapContainer.getMapConfig().getEvictionConfig();
boolean perNodeEvictionConfigured = mapContainer.getEvictor() != Evictor.NULL_EVICTOR
&& evictionConfig.getMaxSizePolicy() == PER_NODE;
long nowInMillis = Clock.currentTimeMillis();
for (int i = 0; i < keyRecordExpiry.size(); i += 3) {
Data dataKey = (Data) keyRecordExpiry.get(i);
Record record = (Record) keyRecordExpiry.get(i + 1);
ExpiryMetadata expiryMetadata = (ExpiryMetadata) keyRecordExpiry.get(i + 2);
if (perNodeEvictionConfigured) {
if (ownedEntryCountOnThisNode >= evictionConfig.getSize()) {
if (operation.getReplicaIndex() == 0) {
recordStore.doPostEvictionOperations(dataKey, record.getValue(), ExpiryReason.NOT_EXPIRED);
}
} else {
recordStore.putReplicatedRecord(dataKey, record, expiryMetadata, populateIndexes, nowInMillis);
ownedEntryCountOnThisNode++;
}
} else {
recordStore.putReplicatedRecord(dataKey, record, expiryMetadata, populateIndexes, nowInMillis);
if (recordStore.shouldEvict()) {
// No need to continue replicating records anymore.
// We are already over eviction threshold, each put record will cause another eviction.
recordStore.evictEntries(dataKey);
break;
}
}
recordStore.disposeDeferredBlocks();
}
if (populateIndexes) {
Indexes.markPartitionAsIndexed(partitionContainer.getPartitionId(), indexesSnapshot);
}
}
}
for (Map.Entry<String, LocalRecordStoreStats> statsEntry : recordStoreStatsPerMapName.entrySet()) {
String mapName = statsEntry.getKey();
LocalRecordStoreStats stats = statsEntry.getValue();
RecordStore recordStore = operation.getRecordStore(mapName);
recordStore.setStats(stats);
}
}
// owned or backup
private long entryCountOnThisNode(MapContainer mapContainer) {
int replicaIndex = operation.getReplicaIndex();
long owned = 0;
if (mapContainer.getEvictor() != Evictor.NULL_EVICTOR
&& PER_NODE == mapContainer.getMapConfig().getEvictionConfig().getMaxSizePolicy()) {
MapService mapService = operation.getService();
MapServiceContext mapServiceContext = mapService.getMapServiceContext();
IPartitionService partitionService = mapServiceContext.getNodeEngine().getPartitionService();
int partitionCount = partitionService.getPartitionCount();
for (int partitionId = 0; partitionId < partitionCount; partitionId++) {
if (replicaIndex == 0 ? partitionService.isPartitionOwner(partitionId)
: !partitionService.isPartitionOwner(partitionId)) {
RecordStore store = mapServiceContext.getExistingRecordStore(partitionId, mapContainer.getName());
if (store != null) {
owned += store.size();
}
}
}
}
return owned;
}
private void applyIndexesState() {
if (mapIndexInfos != null) {
for (MapIndexInfo mapIndexInfo : mapIndexInfos) {
addIndexes(mapIndexInfo.getMapName(), mapIndexInfo.getIndexConfigs());
}
}
}
private void addIndexes(String mapName, Collection<IndexConfig> indexConfigs) {
if (indexConfigs == null) {
return;
}
RecordStore recordStore = operation.getRecordStore(mapName);
MapContainer mapContainer = recordStore.getMapContainer();
if (mapContainer.isGlobalIndexEnabled()) {
// creating global indexes on partition thread in case they do not exist
for (IndexConfig indexConfig : indexConfigs) {
Indexes indexes = mapContainer.getIndexes();
// optimisation not to synchronize each partition thread on the addOrGetIndex method
if (indexes.getIndex(indexConfig.getName()) == null) {
indexes.addOrGetIndex(indexConfig);
}
}
} else {
Indexes indexes = mapContainer.getIndexes(operation.getPartitionId());
indexes.createIndexesFromRecordedDefinitions();
for (IndexConfig indexConfig : indexConfigs) {
indexes.addOrGetIndex(indexConfig);
}
}
}
@Override
public void writeData(ObjectDataOutput out) throws IOException {
out.writeInt(storesByMapName.size());
for (Map.Entry<String, RecordStore<Record>> entry : storesByMapName.entrySet()) {
String mapName = entry.getKey();
out.writeString(mapName);
SerializationService ss = getSerializationService(operation.getRecordStore(mapName).getMapContainer());
RecordStore<Record> recordStore = entry.getValue();
out.writeInt(recordStore.size());
// No expiration should be done in forEach, since we have serialized size before.
recordStore.forEach((dataKey, record) -> {
try {
IOUtil.writeData(out, dataKey);
Records.writeRecord(out, record, ss.toData(record.getValue()),
recordStore.getExpirySystem().getExpiredMetadata(dataKey));
} catch (IOException e) {
throw ExceptionUtil.rethrow(e);
}
}, operation.getReplicaIndex() != 0, true);
if (out.getVersion().isGreaterOrEqual(Versions.V4_2)) {
recordStore.getStats().writeData(out);
}
}
out.writeInt(loaded.size());
for (Map.Entry<String, Boolean> loadedEntry : loaded.entrySet()) {
out.writeString(loadedEntry.getKey());
out.writeBoolean(loadedEntry.getValue());
}
out.writeInt(mapIndexInfos.size());
for (MapIndexInfo mapIndexInfo : mapIndexInfos) {
out.writeObject(mapIndexInfo);
}
}
private static SerializationService getSerializationService(MapContainer mapContainer) {
return mapContainer.getMapServiceContext()
.getNodeEngine().getSerializationService();
}
@Override
public void readData(ObjectDataInput in) throws IOException {
int size = in.readInt();
data = createHashMap(size);
recordStoreStatsPerMapName = createHashMap(size);
for (int i = 0; i < size; i++) {
String name = in.readString();
int numOfRecords = in.readInt();
List keyRecordExpiry = new ArrayList<>(numOfRecords * 3);
for (int j = 0; j < numOfRecords; j++) {
Data dataKey = IOUtil.readData(in);
ExpiryMetadata expiryMetadata = new ExpiryMetadataImpl();
Record record = Records.readRecord(in, expiryMetadata);
keyRecordExpiry.add(dataKey);
keyRecordExpiry.add(record);
keyRecordExpiry.add(expiryMetadata);
}
if (in.getVersion().isGreaterOrEqual(Versions.V4_2)) {
LocalRecordStoreStatsImpl stats = new LocalRecordStoreStatsImpl();
stats.readData(in);
recordStoreStatsPerMapName.put(name, stats);
}
data.put(name, keyRecordExpiry);
}
int loadedSize = in.readInt();
loaded = createHashMap(loadedSize);
for (int i = 0; i < loadedSize; i++) {
loaded.put(in.readString(), in.readBoolean());
}
int mapIndexInfoSize = in.readInt();
mapIndexInfos = new ArrayList<>(mapIndexInfoSize);
for (int i = 0; i < mapIndexInfoSize; i++) {
MapIndexInfo mapIndexInfo = in.readObject();
mapIndexInfos.add(mapIndexInfo);
}
}
@Override
public int getFactoryId() {
return MapDataSerializerHook.F_ID;
}
@Override
public int getClassId() {
return MapDataSerializerHook.MAP_REPLICATION_STATE_HOLDER;
}
private static boolean indexesMustBePopulated(Indexes indexes, MapReplicationOperation operation) {
if (!indexes.haveAtLeastOneIndex()) {
// no indexes to populate
return false;
}
if (indexes.isGlobal()) {
// global indexes are populated during migration finalization
return false;
}
if (operation.getReplicaIndex() != 0) {
// backup partitions have no indexes to populate
return false;
}
return true;
}
}