-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
MergeOperation.java
265 lines (222 loc) · 8.83 KB
/
MergeOperation.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
/*
* 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.InvalidConfigurationException;
import com.hazelcast.internal.serialization.Data;
import com.hazelcast.internal.util.Clock;
import com.hazelcast.map.impl.MapDataSerializerHook;
import com.hazelcast.map.impl.record.Record;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.spi.impl.operationservice.BackupAwareOperation;
import com.hazelcast.spi.impl.operationservice.Operation;
import com.hazelcast.spi.impl.operationservice.PartitionAwareOperation;
import com.hazelcast.spi.merge.SplitBrainMergePolicy;
import com.hazelcast.spi.merge.SplitBrainMergeTypes.MapMergeTypes;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import static com.hazelcast.core.EntryEventType.MERGED;
import static com.hazelcast.internal.config.MergePolicyValidator.checkMapMergePolicy;
/**
* Contains multiple merge entries for split-brain
* healing with a {@link SplitBrainMergePolicy}.
*
* @since 3.10
*/
public class MergeOperation extends MapOperation
implements PartitionAwareOperation, BackupAwareOperation {
private static final long MERGE_POLICY_CHECK_PERIOD = TimeUnit.MINUTES.toMillis(1);
private boolean disableWanReplicationEvent;
private List<MapMergeTypes<Object, Object>> mergingEntries;
private SplitBrainMergePolicy<Object, MapMergeTypes<Object, Object>, Object> mergePolicy;
private transient int currentIndex;
private transient boolean hasMapListener;
private transient boolean hasWanReplication;
private transient boolean hasBackups;
private transient boolean hasInvalidation;
private transient List<Data> invalidationKeys;
private transient boolean hasMergedValues;
private List backupPairs;
public MergeOperation() {
}
public MergeOperation(String name, List<MapMergeTypes<Object, Object>> mergingEntries,
SplitBrainMergePolicy<Object, MapMergeTypes<Object, Object>, Object> mergePolicy,
boolean disableWanReplicationEvent) {
super(name);
this.mergingEntries = mergingEntries;
this.mergePolicy = mergePolicy;
this.disableWanReplicationEvent = disableWanReplicationEvent;
}
@Override
protected boolean disableWanReplicationEvent() {
return disableWanReplicationEvent;
}
@Override
protected void runInternal() {
// Check once in a minute as earliest to avoid log bursts.
if (shouldCheckNow(mapContainer.getLastInvalidMergePolicyCheckTime())) {
try {
checkMapMergePolicy(mapContainer.getMapConfig(), mergePolicy.getClass().getName(),
getNodeEngine().getSplitBrainMergePolicyProvider());
} catch (InvalidConfigurationException e) {
logger().log(Level.WARNING, e.getMessage(), e);
}
}
hasMapListener = mapEventPublisher.hasEventListener(name);
hasWanReplication = mapContainer.isWanReplicationEnabled()
&& !disableWanReplicationEvent;
hasBackups = mapContainer.getTotalBackupCount() > 0;
hasInvalidation = mapContainer.hasInvalidationListener();
if (hasBackups) {
backupPairs = new ArrayList(2 * mergingEntries.size());
}
if (hasInvalidation) {
invalidationKeys = new ArrayList<>(mergingEntries.size());
}
// if currentIndex is not zero, this is a
// continuation of the operation after a NativeOOME
int size = mergingEntries.size();
while (currentIndex < size) {
merge(mergingEntries.get(currentIndex));
currentIndex++;
}
}
private static boolean shouldCheckNow(AtomicLong lastLogTime) {
long now = Clock.currentTimeMillis();
long lastLogged = lastLogTime.get();
if (now - lastLogged >= MERGE_POLICY_CHECK_PERIOD) {
return lastLogTime.compareAndSet(lastLogged, now);
}
return false;
}
private void merge(MapMergeTypes<Object, Object> mergingEntry) {
Data dataKey = getNodeEngine().toData(mergingEntry.getRawKey());
Data oldValue = hasMapListener ? getValue(dataKey) : null;
if (recordStore.merge(mergingEntry, mergePolicy, getCallerProvenance())) {
hasMergedValues = true;
Data dataValue = getValueOrPostProcessedValue(dataKey, getValue(dataKey));
mapServiceContext.interceptAfterPut(mapContainer.getInterceptorRegistry(), dataValue);
if (hasMapListener) {
mapEventPublisher.publishEvent(getCallerAddress(), name, MERGED, dataKey, oldValue, dataValue);
}
if (hasWanReplication) {
publishWanUpdate(dataKey, dataValue);
}
if (hasInvalidation) {
invalidationKeys.add(dataKey);
}
if (hasBackups) {
backupPairs.add(dataKey);
backupPairs.add(dataValue);
}
evict(dataKey);
}
}
private Data getValueOrPostProcessedValue(Data dataKey, Data dataValue) {
if (!isPostProcessing(recordStore)) {
return dataValue;
}
Record record = recordStore.getRecord(dataKey);
return mapServiceContext.toData(record.getValue());
}
private Data getValue(Data dataKey) {
Record record = recordStore.getRecord(dataKey);
if (record != null) {
return mapServiceContext.toData(record.getValue());
}
return null;
}
@Override
public Object getResponse() {
return hasMergedValues;
}
@Override
public boolean shouldBackup() {
return hasBackups && !backupPairs.isEmpty();
}
@Override
public int getSyncBackupCount() {
return mapContainer.getBackupCount();
}
@Override
public int getAsyncBackupCount() {
return mapContainer.getAsyncBackupCount();
}
@Override
protected void afterRunInternal() {
invalidateNearCache(invalidationKeys);
super.afterRunInternal();
}
@Override
public Operation getBackupOperation() {
return new PutAllBackupOperation(name,
toBackupListByRemovingEvictedRecords(), disableWanReplicationEvent);
}
/**
* Since records may get evicted on NOOME after
* they have been merged. We are re-checking
* backup pair list to eliminate evicted entries.
*
* @return list of existing records which can
* safely be transferred to backup replica.
*/
@Nonnull
private List toBackupListByRemovingEvictedRecords() {
List toBackupList = new ArrayList(backupPairs.size());
for (int i = 0; i < backupPairs.size(); i += 2) {
Data dataKey = ((Data) backupPairs.get(i));
Record record = recordStore.getRecord(dataKey);
if (record != null) {
toBackupList.add(dataKey);
toBackupList.add(backupPairs.get(i + 1));
toBackupList.add(record);
toBackupList.add(recordStore.getExpirySystem().getExpiredMetadata(dataKey));
}
}
return toBackupList;
}
@Override
protected void writeInternal(ObjectDataOutput out) throws IOException {
super.writeInternal(out);
out.writeInt(mergingEntries.size());
for (MapMergeTypes mergingEntry : mergingEntries) {
out.writeObject(mergingEntry);
}
out.writeObject(mergePolicy);
out.writeBoolean(disableWanReplicationEvent);
}
@Override
protected void readInternal(ObjectDataInput in) throws IOException {
super.readInternal(in);
int size = in.readInt();
mergingEntries = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
MapMergeTypes mergingEntry = in.readObject();
mergingEntries.add(mergingEntry);
}
mergePolicy = in.readObject();
disableWanReplicationEvent = in.readBoolean();
}
@Override
public int getClassId() {
return MapDataSerializerHook.MERGE;
}
}