-
Notifications
You must be signed in to change notification settings - Fork 612
/
DefaultIracTombstoneManager.java
233 lines (203 loc) · 8.53 KB
/
DefaultIracTombstoneManager.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
package org.infinispan.container.versioning.irac;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import org.infinispan.commands.CommandsFactory;
import org.infinispan.commands.irac.IracCleanupTombstoneCommand;
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.distribution.DistributionInfo;
import org.infinispan.distribution.DistributionManager;
import org.infinispan.factories.KnownComponentNames;
import org.infinispan.factories.annotations.ComponentName;
import org.infinispan.factories.annotations.Inject;
import org.infinispan.factories.annotations.Start;
import org.infinispan.factories.impl.ComponentRef;
import org.infinispan.factories.scopes.Scope;
import org.infinispan.factories.scopes.Scopes;
import org.infinispan.metadata.impl.IracMetadata;
import org.infinispan.remoting.rpc.RpcManager;
import org.infinispan.remoting.transport.Transport;
import org.infinispan.remoting.transport.impl.VoidResponseCollector;
import org.infinispan.util.ExponentialBackOff;
import org.infinispan.util.concurrent.AggregateCompletionStage;
import org.infinispan.util.concurrent.CompletableFutures;
import org.infinispan.util.concurrent.CompletionStages;
import org.infinispan.xsite.XSiteBackup;
import org.infinispan.xsite.irac.DefaultIracManager;
import org.infinispan.xsite.irac.IracExecutor;
import org.infinispan.xsite.irac.IracManager;
import org.infinispan.xsite.irac.IracXSiteBackup;
import org.infinispan.xsite.status.SiteState;
import org.infinispan.xsite.status.TakeOfflineManager;
/**
* A default implementation for {@link IracTombstoneManager}.
* <p>
* This class is responsible to keep track of the tombstones for the IRAC algorithm. Tombstones are used when a key is
* removed but its metadata is necessary to detect possible conflicts in this and remote sites. When all sites have
* updated the key, the tombstone can be removed.
* <p>
* Tombstones are removed periodically in the background.
*
* @since 14.0
*/
@Scope(Scopes.NAMED_CACHE)
public class DefaultIracTombstoneManager implements IracTombstoneManager {
@Inject DistributionManager distributionManager;
@Inject RpcManager rpcManager;
@Inject CommandsFactory commandsFactory;
@Inject TakeOfflineManager takeOfflineManager;
@Inject ComponentRef<IracManager> iracManager;
private final Map<Object, TombstoneData> tombstoneMap;
private final IracExecutor iracExecutor;
final Collection<IracXSiteBackup> asyncBackups;
public DefaultIracTombstoneManager(Configuration configuration) {
iracExecutor = new IracExecutor(this::performCleanup);
asyncBackups = DefaultIracManager.asyncBackups(configuration);
tombstoneMap = new ConcurrentHashMap<>();
}
@Inject
public void inject(@ComponentName(KnownComponentNames.TIMEOUT_SCHEDULE_EXECUTOR) ScheduledExecutorService executorService,
@ComponentName(KnownComponentNames.BLOCKING_EXECUTOR) Executor blockingExecutor) {
// using the inject method here in order to decrease the class size
iracExecutor.setBackOff(ExponentialBackOff.NO_OP);
iracExecutor.setExecutor(blockingExecutor);
// TODO configure? https://issues.redhat.com/browse/ISPN-13446
executorService.scheduleAtFixedRate(iracExecutor::run, 30, 30, TimeUnit.SECONDS);
}
@Start
public void start() {
Transport transport = rpcManager.getTransport();
transport.checkCrossSiteAvailable();
String localSiteName = transport.localSiteName();
asyncBackups.removeIf(xSiteBackup -> localSiteName.equals(xSiteBackup.getSiteName()));
}
public void storeTombstone(int segment, Object key, IracMetadata metadata) {
tombstoneMap.put(key, new TombstoneData(segment, metadata));
}
@Override
public void storeTombstoneIfAbsent(int segment, Object key, IracMetadata metadata) {
if (metadata == null) {
return;
}
tombstoneMap.putIfAbsent(key, new TombstoneData(segment, metadata));
}
@Override
public IracMetadata getTombstone(Object key) {
TombstoneData data = tombstoneMap.get(key);
return data == null ? null : data.getMetadata();
}
@Override
public void removeTombstone(Object key, IracMetadata iracMetadata) {
if (iracMetadata == null) {
return;
}
remove(key, new TombstoneData(-1, iracMetadata));
}
@Override
public void removeTombstone(Object key) {
tombstoneMap.remove(key);
}
@Override
public boolean isEmpty() {
return tombstoneMap.isEmpty();
}
@Override
public int size() {
return tombstoneMap.size();
}
public void startCleanupTombstone() {
iracExecutor.run();
}
private CompletionStage<Void> performCleanup() {
AggregateCompletionStage<Void> stage = CompletionStages.aggregateCompletionStage();
for (Map.Entry<Object, TombstoneData> entry : tombstoneMap.entrySet()) {
DistributionInfo info = distributionManager.getCacheTopology().getSegmentDistribution(entry.getValue().getSegment());
if (!info.isWriteOwner()) {
// topology changed, no longer an owner
remove(entry.getKey(), entry.getValue());
continue;
}
if (!info.isPrimary() || iracManager.running().containsKey(entry.getKey())) {
// backup owner or the irac manager haven't sent the update successfully
continue;
}
stage.dependsOn(new CleanupTask(entry.getKey(), entry.getValue()).checkRemoteSites());
}
return stage.freeze();
}
private void remove(Object key, TombstoneData data) {
tombstoneMap.remove(key, data);
}
DistributionInfo getSegmentDistribution(int segment) {
return distributionManager.getCacheTopology().getSegmentDistribution(segment);
}
private static final class TombstoneData {
private final int segment;
private final IracMetadata metadata;
TombstoneData(int segment, IracMetadata metadata) {
this.segment = segment;
this.metadata = Objects.requireNonNull(metadata);
}
public int getSegment() {
return segment;
}
public IracMetadata getMetadata() {
return metadata;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TombstoneData that = (TombstoneData) o;
return metadata.equals(that.metadata);
}
@Override
public int hashCode() {
return metadata.hashCode();
}
}
private final class CleanupTask implements Function<Boolean, CompletionStage<Void>>, Runnable {
private final Object key;
private final TombstoneData tombstone;
CleanupTask(Object key, TombstoneData tombstone) {
this.key = key;
this.tombstone = tombstone;
}
CompletionStage<Void> checkRemoteSites() {
// if one of the site return true (i.e. the key is in updateKeys map, then do not remove it)
AggregateCompletionStage<Boolean> stage = CompletionStages.orBooleanAggregateCompletionStage();
for (XSiteBackup backup : asyncBackups) {
if (takeOfflineManager.getSiteState(backup.getSiteName()) == SiteState.OFFLINE) {
continue; // backup is offline
}
// we don't need the tombstone to query the remote site
IracCleanupTombstoneCommand cmd = commandsFactory.buildIracCleanupTombstoneCommand(key, null);
stage.dependsOn(rpcManager.invokeXSite(backup, cmd));
}
// in case of exception, keep the tombstone
return stage.freeze()
.exceptionally(CompletableFutures.toTrueFunction())
.thenCompose(this);
}
@Override
public CompletionStage<Void> apply(Boolean keepTombstone) {
if (keepTombstone) {
return CompletableFutures.completedNull();
}
// send commit to all write owner
IracCleanupTombstoneCommand cmd = commandsFactory.buildIracCleanupTombstoneCommand(key, tombstone.getMetadata());
return rpcManager.invokeCommand(getSegmentDistribution(tombstone.getSegment()).writeOwners(),
cmd, VoidResponseCollector.validOnly(), rpcManager.getSyncRpcOptions()).thenRun(this);
}
@Override
public void run() {
remove(key, tombstone);
}
}
}