forked from vert-x3/vertx-ignite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsMapHelper.java
More file actions
214 lines (199 loc) · 7.57 KB
/
SubsMapHelper.java
File metadata and controls
214 lines (199 loc) · 7.57 KB
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
/*
* Copyright 2021 Red Hat, Inc.
*
* Red Hat licenses this file to you 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 io.vertx.spi.cluster.ignite.impl;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.VertxException;
import io.vertx.core.internal.VertxInternal;
import io.vertx.core.internal.logging.Logger;
import io.vertx.core.internal.logging.LoggerFactory;
import io.vertx.core.spi.cluster.RegistrationInfo;
import io.vertx.core.spi.cluster.RegistrationListener;
import io.vertx.core.spi.cluster.RegistrationUpdateEvent;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.cache.query.ContinuousQuery;
import org.apache.ignite.cache.query.ScanQuery;
import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.event.CacheEntryEvent;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.StreamSupport;
/**
* @author Thomas Segismont
* @author Lukas Prettenthaler
*/
public class SubsMapHelper {
private static final Logger log = LoggerFactory.getLogger(SubsMapHelper.class);
private final IgniteCache<String, Set<IgniteRegistrationInfo>> map;
private final RegistrationListener registrationListener;
private final ConcurrentMap<String, Set<RegistrationInfo>> localSubs = new ConcurrentHashMap<>();
private final Throttling throttling;
private volatile boolean shutdown;
public SubsMapHelper(Ignite ignite, RegistrationListener registrationListener, VertxInternal vertxInternal) {
map = ignite.getOrCreateCache("__vertx.subs");
this.registrationListener = registrationListener;
throttling = new Throttling(vertxInternal, a -> getAndUpdate(a, vertxInternal));
shutdown = false;
map.query(new ContinuousQuery<String, Set<IgniteRegistrationInfo>>()
.setAutoUnsubscribe(true)
.setTimeInterval(100L)
.setPageSize(128)
.setLocalListener(l -> listen(l, vertxInternal)));
}
public List<RegistrationInfo> get(String address) {
if (shutdown) {
return null;
}
try {
Set<IgniteRegistrationInfo> remote = map.get(address);
List<RegistrationInfo> infos;
int size = (remote != null) ? remote.size() : 0;
Set<RegistrationInfo> local = localSubs.get(address);
if (local != null) {
synchronized (local) {
size += local.size();
if (size == 0) {
return Collections.emptyList();
}
infos = new ArrayList<>(size);
infos.addAll(local);
}
} else if (size == 0) {
return Collections.emptyList();
} else {
infos = new ArrayList<>(size);
}
if (remote != null) {
for (IgniteRegistrationInfo info : remote) {
infos.add(info.registrationInfo());
}
}
return infos;
} catch (IllegalStateException | CacheException e) {
throw new VertxException(e, true);
}
}
public Void put(String address, RegistrationInfo registrationInfo) {
if (shutdown) {
throw new VertxException("shutdown in progress");
}
try {
if (registrationInfo.localOnly()) {
localSubs.compute(address, (add, curr) -> addToSet(registrationInfo, curr));
fireRegistrationUpdateEvent(address);
} else {
Set<IgniteRegistrationInfo> remoteInfos = map.get(address);
if (remoteInfos == null) {
Set<IgniteRegistrationInfo> newInfoSet = new HashSet<>();
newInfoSet.add(new IgniteRegistrationInfo(address, registrationInfo));
map.put(address, newInfoSet);
} else {
remoteInfos.add(new IgniteRegistrationInfo(address, registrationInfo));
map.put(address, remoteInfos);
}
}
} catch (IllegalStateException | CacheException e) {
throw new VertxException(e);
}
return null;
}
private Set<RegistrationInfo> addToSet(RegistrationInfo registrationInfo, Set<RegistrationInfo> curr) {
Set<RegistrationInfo> res = curr != null ? curr : Collections.synchronizedSet(new LinkedHashSet<>());
res.add(registrationInfo);
return res;
}
public Void remove(String address, RegistrationInfo registrationInfo) {
if (shutdown) {
return null;
}
try {
if (registrationInfo.localOnly()) {
localSubs.computeIfPresent(address, (add, curr) -> removeFromSet(registrationInfo, curr));
fireRegistrationUpdateEvent(address);
} else {
Set<IgniteRegistrationInfo> originalInfos = map.get(address);
if (originalInfos != null && originalInfos.remove(new IgniteRegistrationInfo(address, registrationInfo))) {
if (originalInfos.isEmpty()) {
map.remove(address);
} else {
map.put(address, originalInfos);
}
}
}
} catch (IllegalStateException | CacheException e) {
throw new VertxException(e, true);
}
return null;
}
private Set<RegistrationInfo> removeFromSet(RegistrationInfo registrationInfo, Set<RegistrationInfo> curr) {
curr.remove(registrationInfo);
return curr.isEmpty() ? null : curr;
}
public void removeAllForNode(String nodeId) {
try {
List<Cache.Entry<String, Set<IgniteRegistrationInfo>>> allEntries = map
.query(new ScanQuery<String, Set<IgniteRegistrationInfo>>(null))
.getAll();
if (allEntries == null || allEntries.isEmpty()) {
return;
}
for (Cache.Entry<String, Set<IgniteRegistrationInfo>> entry : allEntries) {
String address = entry.getKey();
Set<IgniteRegistrationInfo> registrations = entry.getValue();
boolean modified = registrations.removeIf(info -> info.registrationInfo().nodeId().equals(nodeId));
if (registrations.isEmpty()) {
map.remove(address);
} else if (modified) {
map.put(address, registrations);
}
}
} catch (IllegalStateException | CacheException t) {
log.warn("Could not remove subscribers for nodeId " + nodeId + ": " + t.getMessage());
}
}
public void leave() {
shutdown = true;
}
private void fireRegistrationUpdateEvent(String address) {
throttling.onEvent(address);
}
private Future<List<RegistrationInfo>> getAndUpdate(String address, VertxInternal vertxInternal) {
Promise<List<RegistrationInfo>> prom = Promise.promise();
if (registrationListener.wantsUpdatesFor(address)) {
prom.future().onSuccess(registrationInfos -> {
registrationListener.registrationsUpdated(new RegistrationUpdateEvent(address, registrationInfos));
});
vertxInternal.executeBlocking(() ->
get(address), false
).onComplete(prom);
} else {
prom.complete();
}
return prom.future();
}
private void listen(final Iterable<CacheEntryEvent<? extends String, ? extends Set<IgniteRegistrationInfo>>> events, final VertxInternal vertxInternal) {
vertxInternal.executeBlocking(() -> {
StreamSupport.stream(events.spliterator(), false)
.map(Cache.Entry::getKey)
.distinct()
.forEach(this::fireRegistrationUpdateEvent);
return null;
}, false);
}
}