forked from wildfly/wildfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfinispanSessionManager.java
468 lines (418 loc) · 19.8 KB
/
InfinispanSessionManager.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* JBoss, Home of Professional Open Source.
* Copyright 2013, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.wildfly.clustering.web.infinispan.session;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.infinispan.Cache;
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.context.Flag;
import org.infinispan.distribution.DistributionManager;
import org.infinispan.notifications.Listener;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryActivated;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryPassivated;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryRemoved;
import org.infinispan.notifications.cachelistener.annotation.DataRehashed;
import org.infinispan.notifications.cachelistener.event.CacheEntryActivatedEvent;
import org.infinispan.notifications.cachelistener.event.CacheEntryPassivatedEvent;
import org.infinispan.notifications.cachelistener.event.CacheEntryRemovedEvent;
import org.infinispan.notifications.cachelistener.event.DataRehashedEvent;
import org.infinispan.remoting.transport.Address;
import org.wildfly.clustering.dispatcher.Command;
import org.wildfly.clustering.dispatcher.CommandDispatcher;
import org.wildfly.clustering.dispatcher.CommandDispatcherFactory;
import org.wildfly.clustering.ee.Batch;
import org.wildfly.clustering.ee.Batcher;
import org.wildfly.clustering.ee.Invoker;
import org.wildfly.clustering.ee.Recordable;
import org.wildfly.clustering.ee.infinispan.RetryingInvoker;
import org.wildfly.clustering.ee.infinispan.TransactionBatch;
import org.wildfly.clustering.group.Node;
import org.wildfly.clustering.group.NodeFactory;
import org.wildfly.clustering.infinispan.spi.distribution.ConsistentHashLocality;
import org.wildfly.clustering.infinispan.spi.distribution.Key;
import org.wildfly.clustering.infinispan.spi.distribution.Locality;
import org.wildfly.clustering.infinispan.spi.distribution.SimpleLocality;
import org.wildfly.clustering.service.concurrent.ServiceExecutor;
import org.wildfly.clustering.service.concurrent.StampedLockServiceExecutor;
import org.wildfly.clustering.web.IdentifierFactory;
import org.wildfly.clustering.web.infinispan.logging.InfinispanWebLogger;
import org.wildfly.clustering.web.session.ImmutableHttpSessionAdapter;
import org.wildfly.clustering.web.session.ImmutableSession;
import org.wildfly.clustering.web.session.ImmutableSessionAttributes;
import org.wildfly.clustering.web.session.Session;
import org.wildfly.clustering.web.session.SessionAttributes;
import org.wildfly.clustering.web.session.SessionContext;
import org.wildfly.clustering.web.session.SessionManager;
import org.wildfly.clustering.web.session.SessionMetaData;
/**
* Generic session manager implementation - independent of cache mapping strategy.
* @author Paul Ferraro
*/
@Listener(primaryOnly = true)
public class InfinispanSessionManager<V, L> implements SessionManager<L, TransactionBatch> {
private final SessionContext context;
private final Batcher<TransactionBatch> batcher;
private final Cache<? extends Key<String>, ?> cache;
private final SessionFactory<V, L> factory;
private final IdentifierFactory<String> identifierFactory;
private final CommandDispatcherFactory dispatcherFactory;
private final NodeFactory<Address> nodeFactory;
private final int maxActiveSessions;
private volatile Duration defaultMaxInactiveInterval = Duration.ofMinutes(30L);
private final boolean persistent;
private final Invoker invoker = new RetryingInvoker(0, 10, 100);
private final SessionCreationMetaDataKeyFilter filter = new SessionCreationMetaDataKeyFilter();
private final Recordable<ImmutableSession> recorder;
private volatile CommandDispatcher<Scheduler> dispatcher;
private volatile Scheduler scheduler;
private volatile ServiceExecutor executor;
public InfinispanSessionManager(SessionFactory<V, L> factory, InfinispanSessionManagerConfiguration configuration) {
this.factory = factory;
this.cache = configuration.getCache();
this.context = configuration.getSessionContext();
this.identifierFactory = configuration.getIdentifierFactory();
this.batcher = configuration.getBatcher();
this.dispatcherFactory = configuration.getCommandDispatcherFactory();
this.nodeFactory = configuration.getNodeFactory();
this.maxActiveSessions = configuration.getMaxActiveSessions();
this.recorder = configuration.getInactiveSessionRecorder();
Configuration config = this.cache.getCacheConfiguration();
// If cache is clustered or configured with a write-through cache store
// then we need to trigger any HttpSessionActivationListeners per request
// See SRV.7.7.2 Distributed Environments
this.persistent = config.clustering().cacheMode().needsStateTransfer() || (config.persistence().usingStores() && !config.persistence().passivation());
}
@Override
public void start() {
this.executor = new StampedLockServiceExecutor();
if (this.recorder != null) {
this.recorder.reset();
}
this.identifierFactory.start();
final List<Scheduler> schedulers = new ArrayList<>(2);
schedulers.add(new SessionExpirationScheduler(this.batcher, new ExpiredSessionRemover<>(this.factory)));
if (this.maxActiveSessions >= 0) {
schedulers.add(new SessionEvictionScheduler(this.cache.getName() + ".eviction", this.factory, this.dispatcherFactory, this.maxActiveSessions));
}
this.scheduler = new Scheduler() {
@Override
public void schedule(ImmutableSession session) {
schedulers.forEach(scheduler -> scheduler.schedule(session));
}
@Override
public void cancel(String sessionId) {
schedulers.forEach(scheduler -> scheduler.cancel(sessionId));
}
@Override
public void cancel(Locality locality) {
schedulers.forEach(scheduler -> scheduler.cancel(locality));
}
@Override
public void close() {
schedulers.forEach(scheduler -> scheduler.close());
}
};
this.dispatcher = this.dispatcherFactory.createCommandDispatcher(this.cache.getName() + ".schedulers", this.scheduler);
this.cache.addListener(this, this.filter);
this.schedule(this.cache, new SimpleLocality(false), new ConsistentHashLocality(this.cache));
}
@Override
public void stop() {
this.executor.close(() -> {
this.cache.removeListener(this);
this.dispatcher.close();
this.scheduler.close();
this.identifierFactory.stop();
});
}
boolean isPersistent() {
return this.persistent;
}
private void cancel(ImmutableSession session) {
try {
this.executeOnPrimaryOwner(session, new CancelSchedulerCommand(session.getId()));
} catch (Exception e) {
InfinispanWebLogger.ROOT_LOGGER.failedToCancelSession(e, session.getId());
}
}
void schedule(ImmutableSession session) {
try {
this.executeOnPrimaryOwner(session, new ScheduleSchedulerCommand(session));
} catch (Exception e) {
InfinispanWebLogger.ROOT_LOGGER.failedToScheduleSession(e, session.getId());
}
}
private void executeOnPrimaryOwner(final ImmutableSession session, final Command<Void, Scheduler> command) throws Exception {
this.invoker.invoke(() -> {
// This should only go remote following a failover
Node node = this.locatePrimaryOwner(session);
return this.dispatcher.executeOnNode(command, node);
}).get();
}
private Node locatePrimaryOwner(ImmutableSession session) {
DistributionManager dist = this.cache.getAdvancedCache().getDistributionManager();
Address address = (dist != null) ? dist.getPrimaryLocation(new Key<>(session.getId())) : null;
return (address != null) ? this.nodeFactory.createNode(address) : this.dispatcherFactory.getGroup().getLocalNode();
}
@Override
public Batcher<TransactionBatch> getBatcher() {
return this.batcher;
}
@Override
public Duration getDefaultMaxInactiveInterval() {
return this.defaultMaxInactiveInterval;
}
@Override
public void setDefaultMaxInactiveInterval(Duration duration) {
this.defaultMaxInactiveInterval = duration;
}
@Override
public String createIdentifier() {
return this.identifierFactory.createIdentifier();
}
@Override
public boolean containsSession(String id) {
return this.cache.containsKey(new SessionCreationMetaDataKey(id));
}
@Override
public Session<L> findSession(String id) {
V value = this.factory.findValue(id);
if (value == null) {
InfinispanWebLogger.ROOT_LOGGER.tracef("Session %s not found", id);
return null;
}
Session<L> session = this.factory.createSession(id, value);
if (session.getMetaData().isExpired()) {
InfinispanWebLogger.ROOT_LOGGER.tracef("Session %s was found, but has expired", id);
session.invalidate();
return null;
}
ImmutableSession immutableSession = this.persistent ? this.factory.createImmutableSession(id, value) : session;
this.cancel(immutableSession);
if (this.persistent) {
triggerPostActivationEvents(immutableSession);
}
return new SchedulableSession(session, immutableSession);
}
@Override
public Session<L> createSession(String id) {
Session<L> session = this.factory.createSession(id, this.factory.createValue(id, null));
session.getMetaData().setMaxInactiveInterval(this.defaultMaxInactiveInterval);
return new SchedulableSession(session, session);
}
@Override
public ImmutableSession viewSession(String id) {
V value = this.factory.findValue(id);
return (value != null) ? new SimpleImmutableSession(this.factory.createImmutableSession(id, value)) : null;
}
@Override
public Set<String> getActiveSessions() {
// Omit remote sessions (i.e. when using DIST mode) as well as passivated sessions
return this.getSessions(Flag.CACHE_MODE_LOCAL, Flag.SKIP_CACHE_LOAD);
}
@Override
public Set<String> getLocalSessions() {
// Omit remote sessions (i.e. when using DIST mode)
return this.getSessions(Flag.CACHE_MODE_LOCAL);
}
private Set<String> getSessions(Flag... flags) {
try (Stream<? extends Key<String>> keys = this.cache.getAdvancedCache().withFlags(flags).keySet().stream()) {
return keys.filter(this.filter).map(key -> key.getValue()).collect(Collectors.toSet());
}
}
@Override
public int getMaxActiveSessions() {
return this.maxActiveSessions;
}
@Override
public long getActiveSessionCount() {
return this.getActiveSessions().size();
}
@CacheEntryActivated
public void activated(CacheEntryActivatedEvent<SessionCreationMetaDataKey, ?> event) {
if (!event.isPre() && !this.persistent) {
this.executor.execute(() -> {
String id = event.getKey().getValue();
InfinispanWebLogger.ROOT_LOGGER.tracef("Session %s was activated", id);
V value = this.factory.findValue(id);
if (value != null) {
ImmutableSession session = this.factory.createImmutableSession(id, value);
triggerPostActivationEvents(session);
}
});
}
}
@CacheEntryPassivated
public void passivated(CacheEntryPassivatedEvent<SessionCreationMetaDataKey, ?> event) {
if (event.isPre() && !this.persistent) {
this.executor.execute(() -> {
String id = event.getKey().getValue();
InfinispanWebLogger.ROOT_LOGGER.tracef("Session %s will be passivated", id);
V value = this.factory.findValue(id);
if (value != null) {
ImmutableSession session = this.factory.createImmutableSession(id, value);
triggerPrePassivationEvents(session);
}
});
}
}
@CacheEntryRemoved
public void removed(CacheEntryRemovedEvent<SessionCreationMetaDataKey, ?> event) {
if (event.isPre()) {
this.executor.execute(() -> {
String id = event.getKey().getValue();
InfinispanWebLogger.ROOT_LOGGER.tracef("Session %s will be removed", id);
V value = this.factory.findValue(id);
if (value != null) {
ImmutableSession session = this.factory.createImmutableSession(id, value);
ImmutableSessionAttributes attributes = session.getAttributes();
HttpSession httpSession = new ImmutableHttpSessionAdapter(session);
HttpSessionEvent sessionEvent = new HttpSessionEvent(httpSession);
for (HttpSessionListener listener: this.context.getSessionListeners()) {
listener.sessionDestroyed(sessionEvent);
}
for (String name: attributes.getAttributeNames()) {
Object attribute = attributes.getAttribute(name);
if (attribute instanceof HttpSessionBindingListener) {
HttpSessionBindingListener listener = (HttpSessionBindingListener) attribute;
listener.valueUnbound(new HttpSessionBindingEvent(httpSession, name, attribute));
}
}
if (this.recorder != null) {
this.recorder.record(session);
}
}
});
}
}
@DataRehashed
public void dataRehashed(DataRehashedEvent<SessionCreationMetaDataKey, ?> event) {
this.executor.execute(() -> {
Cache<SessionCreationMetaDataKey, ?> cache = event.getCache();
Address localAddress = cache.getCacheManager().getAddress();
Locality oldLocality = new ConsistentHashLocality(localAddress, event.getConsistentHashAtStart());
Locality newLocality = new ConsistentHashLocality(localAddress, event.getConsistentHashAtEnd());
if (event.isPre()) {
this.scheduler.cancel(newLocality);
} else {
this.schedule(cache, oldLocality, newLocality);
}
});
}
private void schedule(Cache<? extends Key<String>, ?> cache, Locality oldLocality, Locality newLocality) {
// Iterate over sessions in memory
try (Stream<? extends Key<String>> keys = cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL, Flag.SKIP_CACHE_LOAD).keySet().stream()) {
// If we are the new primary owner of this session then schedule expiration of this session locally
keys.filter(this.filter).filter(key -> !oldLocality.isLocal(key) && newLocality.isLocal(key)).map(key -> key.getValue()).forEach(id -> {
Batch batch = this.batcher.createBatch();
try {
// We need to lookup the session to obtain its meta data
V value = this.factory.tryValue(id);
if (value != null) {
this.scheduler.schedule(this.factory.createImmutableSession(id, value));
}
} finally {
batch.discard();
}
});
}
}
static void triggerPrePassivationEvents(ImmutableSession session) {
List<HttpSessionActivationListener> listeners = findListeners(session);
if (!listeners.isEmpty()) {
HttpSessionEvent event = new HttpSessionEvent(new ImmutableHttpSessionAdapter(session));
listeners.forEach(listener -> listener.sessionWillPassivate(event));
}
}
static void triggerPostActivationEvents(ImmutableSession session) {
List<HttpSessionActivationListener> listeners = findListeners(session);
if (!listeners.isEmpty()) {
HttpSessionEvent event = new HttpSessionEvent(new ImmutableHttpSessionAdapter(session));
listeners.forEach(listener -> listener.sessionDidActivate(event));
}
}
private static List<HttpSessionActivationListener> findListeners(ImmutableSession session) {
ImmutableSessionAttributes attributes = session.getAttributes();
return attributes.getAttributeNames().stream().map(name -> attributes.getAttribute(name)).filter(attribute -> attribute instanceof HttpSessionActivationListener).map(attribute -> (HttpSessionActivationListener) attribute).collect(Collectors.toList());
}
// Session decorator that performs scheduling on close().
private class SchedulableSession implements Session<L> {
private final Session<L> session;
private final ImmutableSession immutableSession;
SchedulableSession(Session<L> session, ImmutableSession immutableSession) {
this.session = session;
this.immutableSession = immutableSession;
}
@Override
public String getId() {
return this.session.getId();
}
@Override
public SessionMetaData getMetaData() {
if (!this.isValid()) {
throw InfinispanWebLogger.ROOT_LOGGER.invalidSession(this.getId());
}
return this.session.getMetaData();
}
@Override
public boolean isValid() {
return this.session.isValid();
}
@Override
public void invalidate() {
this.session.invalidate();
}
@Override
public SessionAttributes getAttributes() {
if (!this.isValid()) {
throw InfinispanWebLogger.ROOT_LOGGER.invalidSession(this.getId());
}
return this.session.getAttributes();
}
@Override
public SessionContext getContext() {
return this.session.getContext();
}
@Override
public void close() {
if (InfinispanSessionManager.this.isPersistent()) {
triggerPrePassivationEvents(this.immutableSession);
}
this.session.close();
InfinispanSessionManager.this.schedule(this.immutableSession);
}
@Override
public L getLocalContext() {
return this.session.getLocalContext();
}
}
}