forked from hazelcast/hazelcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractPartitionPrimaryReplicaAntiEntropyTask.java
158 lines (133 loc) · 7.07 KB
/
AbstractPartitionPrimaryReplicaAntiEntropyTask.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
/*
* Copyright (c) 2008-2022, 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.internal.partition.impl;
import com.hazelcast.internal.cluster.impl.ClusterServiceImpl;
import com.hazelcast.internal.partition.FragmentedMigrationAwareService;
import com.hazelcast.internal.partition.NonFragmentedServiceNamespace;
import com.hazelcast.internal.partition.PartitionReplica;
import com.hazelcast.internal.partition.PartitionReplicationEvent;
import com.hazelcast.internal.partition.operation.PartitionBackupReplicaAntiEntropyOperation;
import com.hazelcast.internal.services.ServiceNamespace;
import com.hazelcast.logging.ILogger;
import com.hazelcast.spi.impl.NodeEngineImpl;
import com.hazelcast.spi.impl.PartitionSpecificRunnable;
import com.hazelcast.spi.impl.executionservice.ExecutionService;
import com.hazelcast.spi.impl.operationservice.Operation;
import com.hazelcast.spi.impl.operationservice.OperationService;
import com.hazelcast.spi.impl.operationservice.UrgentSystemOperation;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.function.BiConsumer;
import static com.hazelcast.internal.partition.IPartitionService.SERVICE_NAME;
public abstract class AbstractPartitionPrimaryReplicaAntiEntropyTask
implements PartitionSpecificRunnable, UrgentSystemOperation {
private static final int OPERATION_TRY_COUNT = 10;
private static final int OPERATION_TRY_PAUSE_MILLIS = 250;
protected final NodeEngineImpl nodeEngine;
protected final InternalPartitionServiceImpl partitionService;
protected final int partitionId;
public AbstractPartitionPrimaryReplicaAntiEntropyTask(NodeEngineImpl nodeEngine, int partitionId) {
this.nodeEngine = nodeEngine;
this.partitionService = (InternalPartitionServiceImpl) nodeEngine.getPartitionService();
this.partitionId = partitionId;
}
@Override
public int getPartitionId() {
return partitionId;
}
// works only on primary. backups are retained
// in PartitionBackupReplicaAntiEntropyTask
final Collection<ServiceNamespace> retainAndGetNamespaces() {
PartitionReplicationEvent event = new PartitionReplicationEvent(null, partitionId, 0);
Collection<FragmentedMigrationAwareService> services
= nodeEngine.getServices(FragmentedMigrationAwareService.class);
Collection<ServiceNamespace> namespaces = new HashSet<>();
for (FragmentedMigrationAwareService service : services) {
Collection<ServiceNamespace> serviceNamespaces = service.getAllServiceNamespaces(event);
if (serviceNamespaces != null) {
namespaces.addAll(serviceNamespaces);
}
}
namespaces.add(NonFragmentedServiceNamespace.INSTANCE);
PartitionReplicaManager replicaManager = partitionService.getReplicaManager();
replicaManager.retainNamespaces(partitionId, namespaces);
ILogger logger = nodeEngine.getLogger(getClass());
if (logger.isFinestEnabled()) {
logger.finest("Retained namespaces for partitionId=" + partitionId + ". Service namespaces="
+ namespaces + ", retained namespaces=" + replicaManager.getNamespaces(partitionId));
}
return replicaManager.getNamespaces(partitionId);
}
final void invokePartitionBackupReplicaAntiEntropyOp(int replicaIndex, PartitionReplica target,
Collection<ServiceNamespace> namespaces,
BiConsumer<Object, Throwable> callback) {
if (skipSendingToTarget(target)) {
return;
}
PartitionReplicaManager replicaManager = partitionService.getReplicaManager();
Map<ServiceNamespace, Long> versionMap = new HashMap<>();
for (ServiceNamespace ns : namespaces) {
long[] versions = replicaManager.getPartitionReplicaVersions(partitionId, ns);
long currentReplicaVersion = versions[replicaIndex - 1];
versionMap.put(ns, currentReplicaVersion);
}
boolean hasCallback = (callback != null);
Operation op = new PartitionBackupReplicaAntiEntropyOperation(versionMap, hasCallback);
op.setPartitionId(partitionId)
.setReplicaIndex(replicaIndex)
.setServiceName(SERVICE_NAME);
ILogger logger = nodeEngine.getLogger(getClass());
if (logger.isFinestEnabled()) {
logger.finest("Sending anti-entropy operation to " + target + " for partitionId=" + partitionId
+ ", replicaIndex=" + replicaIndex + ", namespaces=" + versionMap);
}
OperationService operationService = nodeEngine.getOperationService();
if (hasCallback) {
ExecutorService asyncExecutor =
nodeEngine.getExecutionService().getExecutor(ExecutionService.ASYNC_EXECUTOR);
operationService.createInvocationBuilder(SERVICE_NAME, op, target.address())
.setTryCount(OPERATION_TRY_COUNT)
.setTryPauseMillis(OPERATION_TRY_PAUSE_MILLIS)
.invoke()
.whenCompleteAsync(callback, asyncExecutor);
} else {
operationService.send(op, target.address());
}
}
private boolean skipSendingToTarget(PartitionReplica target) {
ClusterServiceImpl clusterService = nodeEngine.getNode().getClusterService();
assert !target.isIdentical(nodeEngine.getLocalMember()) : "Could not send anti-entropy operation, because "
+ target + " is local member itself! Local-member: " + clusterService.getLocalMember()
+ ", " + partitionService.getPartition(partitionId);
if (clusterService.getMember(target.address(), target.uuid()) == null) {
ILogger logger = nodeEngine.getLogger(getClass());
if (logger.isFinestEnabled()) {
if (clusterService.isMissingMember(target.address(), target.uuid())) {
logger.finest("Could not send anti-entropy operation, because " + target + " is a missing member. "
+ partitionService.getPartition(partitionId));
} else {
logger.finest("Could not send anti-entropy operation, because " + target + " is not a known member. "
+ partitionService.getPartition(partitionId));
}
}
return true;
}
return false;
}
}