Debian Codegraph @

jetty-nosql/src/main/java/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStore.java

raw
1 //
2 // ========================================================================
3 // Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.nosql.mongodb;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.ObjectOutputStream;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Set;
28
29 import com.mongodb.BasicDBList;
30 import com.mongodb.BasicDBObject;
31 import com.mongodb.BasicDBObjectBuilder;
32 import com.mongodb.DBCollection;
33 import com.mongodb.DBCursor;
34 import com.mongodb.DBObject;
35 import com.mongodb.MongoException;
36 import com.mongodb.WriteConcern;
37 import com.mongodb.WriteResult;
38 import org.eclipse.jetty.nosql.NoSqlSessionDataStore;
39 import org.eclipse.jetty.server.session.SessionContext;
40 import org.eclipse.jetty.server.session.SessionData;
41 import org.eclipse.jetty.server.session.UnreadableSessionDataException;
42 import org.eclipse.jetty.util.ClassLoadingObjectInputStream;
43 import org.eclipse.jetty.util.StringUtil;
44 import org.eclipse.jetty.util.annotation.ManagedAttribute;
45 import org.eclipse.jetty.util.annotation.ManagedObject;
46 import org.eclipse.jetty.util.log.Log;
47 import org.eclipse.jetty.util.log.Logger;
48
49 /**
50 * MongoSessionDataStore
51 *
52 * The document model is an outer object that contains the elements:
53 * <ul>
54 * <li>"id" : session_id </li>
55 * <li>"created" : create_time </li>
56 * <li>"accessed": last_access_time </li>
57 * <li>"maxIdle" : max_idle_time setting as session was created </li>
58 * <li>"expiry" : time at which session should expire </li>
59 * <li>"valid" : session_valid </li>
60 * <li>"context" : a nested object containing 1 nested object per context for which the session id is in use
61 * </ul>
62 * Each of the nested objects inside the "context" element contains:
63 * <ul>
64 * <li>unique_context_name : nested object containing name:value pairs of the session attributes for that context</li>
65 * <li>unique_context_name: vhost:contextpath, where no vhosts="0_0_0_0", root context = "", contextpath "/" replaced by "_"
66 * </ul>
67 * <p>
68 * One of the name:value attribute pairs will always be the special attribute "__metadata__". The value
69 * is an object representing a version counter which is incremented every time the attributes change.
70 * </p>
71 * <p>
72 * For example:
73 * <pre>
74 * { "_id" : ObjectId("52845534a40b66410f228f23"),
75 * "accessed" : NumberLong("1384818548903"),
76 * "maxIdle" : 1,
77 * "context" : { "0_0_0_0:_testA" : { "A" : "A",
78 * "__metadata__" : { "version" : NumberLong(2) }
79 * },
80 * "0_0_0_0:_testB" : { "B" : "B",
81 * "__metadata__" : { "version" : NumberLong(1) }
82 * }
83 * },
84 * "created" : NumberLong("1384818548903"),
85 * "expiry" : NumberLong("1384818549903"),
86 * "id" : "w01ijx2vnalgv1sqrpjwuirprp7",
87 * "valid" : true
88 * }
89 * </pre>
90 * <p>
91 * In MongoDB, the nesting level is indicated by "." separators for the key name. Thus to
92 * interact with session fields, the key is composed of:
93 * <code>"context".unique_context_name.field_name</code>
94 * Eg <code>"context"."0_0_0_0:_testA"."lastSaved"</code>
95 */
96 @ManagedObject
97 public class MongoSessionDataStore extends NoSqlSessionDataStore
98 {
99
100 private static final Logger LOG = Log.getLogger("org.eclipse.jetty.server.session");
101
102 /**
103 * Special attribute for a session that is context-specific
104 */
105 public static final String METADATA = "__metadata__";
106
107 /**
108 * Name of nested document field containing 1 sub document per context for which the session id is in use
109 */
110 public static final String CONTEXT = "context";
111
112 /**
113 * Special attribute per session per context, incremented each time attributes are modified
114 */
115 public static final String VERSION = METADATA + ".version";
116
117 public static final String LASTSAVED = METADATA + ".lastSaved";
118
119 public static final String LASTNODE = METADATA + ".lastNode";
120
121 /**
122 * Last access time of session
123 */
124 public static final String ACCESSED = "accessed";
125
126 public static final String LAST_ACCESSED = "lastAccessed";
127
128 public static final String ATTRIBUTES = "attributes";
129
130 /**
131 * Time this session will expire, based on last access time and maxIdle
132 */
133 public static final String EXPIRY = "expiry";
134
135 /**
136 * The max idle time of a session (smallest value across all contexts which has a session with the same id)
137 */
138 public static final String MAX_IDLE = "maxIdle";
139
140 /**
141 * Time of session creation
142 */
143 public static final String CREATED = "created";
144
145 /**
146 * Whether or not session is valid
147 */
148 public static final String VALID = "valid";
149
150 /**
151 * Session id
152 */
153 public static final String ID = "id";
154
155 /**
156 * Utility value of 1 for a session version for this context
157 */
158 private DBObject _version1;
159
160 /**
161 * Access to MongoDB
162 */
163 private DBCollection _dbSessions;
164
165 public void setDBCollection(DBCollection collection)
166 {
167 _dbSessions = collection;
168 }
169
170 @ManagedAttribute(value = "DBCollection", readonly = true)
171 public DBCollection getDBCollection()
172 {
173 return _dbSessions;
174 }
175
176 @Override
177 public SessionData doLoad(String id) throws Exception
178 {
179 DBObject sessionDocument = _dbSessions.findOne(new BasicDBObject(ID, id));
180
181 try
182 {
183 if (LOG.isDebugEnabled())
184 LOG.debug("id={} loaded={}", id, sessionDocument);
185
186 if (sessionDocument == null)
187 return null;
188
189 Boolean valid = (Boolean)sessionDocument.get(VALID);
190
191 if (LOG.isDebugEnabled())
192 LOG.debug("id={} valid={}", id, valid);
193 if (valid == null || !valid)
194 return null;
195
196 Object version = MongoUtils.getNestedValue(sessionDocument, getContextSubfield(VERSION));
197 Long lastSaved = (Long)MongoUtils.getNestedValue(sessionDocument, getContextSubfield(LASTSAVED));
198 String lastNode = (String)MongoUtils.getNestedValue(sessionDocument, getContextSubfield(LASTNODE));
199 byte[] attributes = (byte[])MongoUtils.getNestedValue(sessionDocument, getContextSubfield(ATTRIBUTES));
200
201 Long created = (Long)sessionDocument.get(CREATED);
202 Long accessed = (Long)sessionDocument.get(ACCESSED);
203 Long lastAccessed = (Long)sessionDocument.get(LAST_ACCESSED);
204 Long maxInactive = (Long)sessionDocument.get(MAX_IDLE);
205 Long expiry = (Long)sessionDocument.get(EXPIRY);
206
207 NoSqlSessionData data = null;
208
209 // get the session for the context
210 DBObject sessionSubDocumentForContext = (DBObject)MongoUtils.getNestedValue(sessionDocument, getContextField());
211
212 if (LOG.isDebugEnabled())
213 LOG.debug("attrs {}", sessionSubDocumentForContext);
214
215 if (sessionSubDocumentForContext != null)
216 {
217 if (LOG.isDebugEnabled())
218 LOG.debug("Session {} present for context {}", id, _context);
219
220 //only load a session if it exists for this context
221 data = (NoSqlSessionData)newSessionData(id, created, accessed, (lastAccessed == null ? accessed : lastAccessed), maxInactive);
222 data.setVersion(version);
223 data.setExpiry(expiry);
224 data.setContextPath(_context.getCanonicalContextPath());
225 data.setVhost(_context.getVhost());
226 data.setLastSaved(lastSaved);
227 data.setLastNode(lastNode);
228
229 if (attributes == null)
230 {
231 //legacy attribute storage format: the attributes are all fields in the document
232 Map<String, Object> map = new HashMap<>();
233 for (String name : sessionSubDocumentForContext.keySet())
234 {
235 //skip special metadata attribute which is not one of the actual session attributes
236 if (METADATA.equals(name))
237 continue;
238 String attr = MongoUtils.decodeName(name);
239 Object value = MongoUtils.decodeValue(sessionSubDocumentForContext.get(name));
240 map.put(attr, value);
241 }
242 data.putAllAttributes(map);
243 }
244 else
245 {
246 //attributes have special serialized format
247 try (ByteArrayInputStream bais = new ByteArrayInputStream(attributes);
248 ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(bais))
249 {
250 SessionData.deserializeAttributes(data, ois);
251 }
252 }
253 }
254 else
255 {
256 if (LOG.isDebugEnabled())
257 LOG.debug("Session {} not present for context {}", id, _context);
258 }
259
260 return data;
261 }
262 catch (Exception e)
263 {
264 throw (new UnreadableSessionDataException(id, _context, e));
265 }
266 }
267
268 @Override
269 public boolean delete(String id) throws Exception
270 {
271 if (LOG.isDebugEnabled())
272 LOG.debug("Remove:session {} for context ", id, _context);
273
274 /*
275 * Check if the session exists and if it does remove the context
276 * associated with this session
277 */
278 BasicDBObject mongoKey = new BasicDBObject(ID, id);
279
280 //DBObject sessionDocument = _dbSessions.findOne(mongoKey,_version1);
281 DBObject sessionDocument = _dbSessions.findOne(new BasicDBObject(ID, id));
282
283 if (sessionDocument != null)
284 {
285 DBObject c = (DBObject)MongoUtils.getNestedValue(sessionDocument, CONTEXT);
286 if (c == null)
287 {
288 //delete whole doc
289 _dbSessions.remove(mongoKey, WriteConcern.SAFE);
290 return false;
291 }
292
293 Set<String> contexts = c.keySet();
294 if (contexts.isEmpty())
295 {
296 //delete whole doc
297 _dbSessions.remove(mongoKey, WriteConcern.SAFE);
298 return false;
299 }
300
301 if (contexts.size() == 1 && contexts.iterator().next().equals(getCanonicalContextId()))
302 {
303 //delete whole doc
304 _dbSessions.remove(new BasicDBObject(ID, id), WriteConcern.SAFE);
305 return true;
306 }
307
308 //just remove entry for my context
309 BasicDBObject remove = new BasicDBObject();
310 BasicDBObject unsets = new BasicDBObject();
311 unsets.put(getContextField(), 1);
312 remove.put("$unset", unsets);
313 _dbSessions.update(mongoKey, remove, false, false, WriteConcern.SAFE);
314 return true;
315 }
316 else
317 {
318 return false;
319 }
320 }
321
322 @Override
323 public boolean exists(String id) throws Exception
324 {
325 DBObject fields = new BasicDBObject();
326 fields.put(EXPIRY, 1);
327 fields.put(VALID, 1);
328 fields.put(getContextSubfield(VERSION), 1);
329
330 DBObject sessionDocument = _dbSessions.findOne(new BasicDBObject(ID, id), fields);
331
332 if (sessionDocument == null)
333 return false; //doesn't exist
334
335 Boolean valid = (Boolean)sessionDocument.get(VALID);
336 if (!valid)
337 return false; //invalid - nb should not happen
338
339 Long expiry = (Long)sessionDocument.get(EXPIRY);
340
341 //expired?
342 if (expiry.longValue() > 0 && expiry.longValue() < System.currentTimeMillis())
343 return false; //it's expired
344
345 //does it exist for this context?
346 Object version = MongoUtils.getNestedValue(sessionDocument, getContextSubfield(VERSION));
347 return version != null;
348 }
349
350 @Override
351 public Set<String> doGetExpired(Set<String> candidates)
352 {
353 long now = System.currentTimeMillis();
354 long upperBound = now;
355 Set<String> expiredSessions = new HashSet<>();
356
357 //firstly ask mongo to verify if these candidate ids have expired - all of
358 //these candidates will be for our node
359 BasicDBObject query = new BasicDBObject();
360 query.append(ID, new BasicDBObject("$in", candidates));
361 query.append(EXPIRY, new BasicDBObject("$gt", 0).append("$lt", upperBound));
362
363 DBCursor verifiedExpiredSessions = null;
364 try
365 {
366 verifiedExpiredSessions = _dbSessions.find(query, new BasicDBObject(ID, 1));
367 for (DBObject session : verifiedExpiredSessions)
368 {
369 String id = (String)session.get(ID);
370 if (LOG.isDebugEnabled())
371 LOG.debug("{} Mongo confirmed expired session {}", _context, id);
372 expiredSessions.add(id);
373 }
374 }
375 finally
376 {
377 if (verifiedExpiredSessions != null)
378 verifiedExpiredSessions.close();
379 }
380
381 //now ask mongo to find sessions last managed by any nodes that expired a while ago
382 //if this is our first expiry check, make sure that we only grab really old sessions
383 if (_lastExpiryCheckTime <= 0)
384 upperBound = (now - (3 * (1000L * _gracePeriodSec)));
385 else
386 upperBound = _lastExpiryCheckTime - (1000L * _gracePeriodSec);
387
388 query = new BasicDBObject();
389 BasicDBObject gt = new BasicDBObject(EXPIRY, new BasicDBObject("$gt", 0));
390 BasicDBObject lt = new BasicDBObject(EXPIRY, new BasicDBObject("$lt", upperBound));
391 BasicDBList list = new BasicDBList();
392 list.add(gt);
393 list.add(lt);
394 query.append("$and", list);
395
396 DBCursor oldExpiredSessions = null;
397 try
398 {
399 BasicDBObject bo = new BasicDBObject(ID, 1);
400 bo.append(EXPIRY, 1);
401
402 oldExpiredSessions = _dbSessions.find(query, bo);
403 for (DBObject session : oldExpiredSessions)
404 {
405 String id = (String)session.get(ID);
406 if (LOG.isDebugEnabled())
407 LOG.debug("{} Mongo found old expired session {}", _context, id + " exp=" + session.get(EXPIRY));
408 expiredSessions.add(id);
409 }
410 }
411 finally
412 {
413 if (oldExpiredSessions != null)
414 oldExpiredSessions.close();
415 }
416
417 //check through sessions that were candidates, but not found as expired.
418 //they may no longer be persisted, in which case they are treated as expired.
419 for (String c : candidates)
420 {
421 if (!expiredSessions.contains(c))
422 {
423 try
424 {
425 if (!exists(c))
426 expiredSessions.add(c);
427 }
428 catch (Exception e)
429 {
430 LOG.warn("Problem checking potentially expired session {}", c, e);
431 }
432 }
433 }
434 return expiredSessions;
435 }
436
437 public void initialize(SessionContext context) throws Exception
438 {
439 if (isStarted())
440 throw new IllegalStateException("Context set after SessionDataStore started");
441 _context = context;
442 ensureIndexes();
443 }
444
445 @Override
446 public void doStore(String id, SessionData data, long lastSaveTime) throws Exception
447 {
448 // Form query for upsert
449 BasicDBObject key = new BasicDBObject(ID, id);
450
451 // Form updates
452 BasicDBObject update = new BasicDBObject();
453 boolean upsert = false;
454 BasicDBObject sets = new BasicDBObject();
455
456 Object version = ((NoSqlSessionData)data).getVersion();
457
458 // New session
459 if (lastSaveTime <= 0)
460 {
461 upsert = true;
462 version = new Long(1);
463 sets.put(CREATED, data.getCreated());
464 sets.put(VALID, true);
465 sets.put(getContextSubfield(VERSION), version);
466 sets.put(getContextSubfield(LASTSAVED), data.getLastSaved());
467 sets.put(getContextSubfield(LASTNODE), data.getLastNode());
468 sets.put(MAX_IDLE, data.getMaxInactiveMs());
469 sets.put(EXPIRY, data.getExpiry());
470 ((NoSqlSessionData)data).setVersion(version);
471 }
472 else
473 {
474 sets.put(getContextSubfield(LASTSAVED), data.getLastSaved());
475 sets.put(getContextSubfield(LASTNODE), data.getLastNode());
476 version = new Long(((Number)version).longValue() + 1);
477 ((NoSqlSessionData)data).setVersion(version);
478 update.put("$inc", _version1);
479 //if max idle time and/or expiry is smaller for this context, then choose that for the whole session doc
480 BasicDBObject fields = new BasicDBObject();
481 fields.append(MAX_IDLE, true);
482 fields.append(EXPIRY, true);
483 DBObject o = _dbSessions.findOne(new BasicDBObject("id", id), fields);
484 if (o != null)
485 {
486 Long tmpLong = (Long)o.get(MAX_IDLE);
487 long currentMaxIdle = (tmpLong == null ? 0 : tmpLong.longValue());
488 tmpLong = (Long)o.get(EXPIRY);
489 long currentExpiry = (tmpLong == null ? 0 : tmpLong.longValue());
490
491 if (currentMaxIdle != data.getMaxInactiveMs())
492 sets.put(MAX_IDLE, data.getMaxInactiveMs());
493
494 if (currentExpiry != data.getExpiry())
495 sets.put(EXPIRY, data.getExpiry());
496 }
497 else
498 LOG.warn("Session {} not found, can't update", id);
499 }
500
501 sets.put(ACCESSED, data.getAccessed());
502 sets.put(LAST_ACCESSED, data.getLastAccessed());
503
504 try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
505 ObjectOutputStream oos = new ObjectOutputStream(baos))
506 {
507 SessionData.serializeAttributes(data, oos);
508 sets.put(getContextSubfield(ATTRIBUTES), baos.toByteArray());
509 }
510
511 // Do the upsert
512 if (!sets.isEmpty())
513 update.put("$set", sets);
514
515 WriteResult res = _dbSessions.update(key, update, upsert, false, WriteConcern.SAFE);
516 if (LOG.isDebugEnabled())
517 LOG.debug("Save:db.sessions.update( {}, {},{} )", key, update, res);
518 }
519
520 protected void ensureIndexes() throws MongoException
521 {
522 _version1 = new BasicDBObject(getContextSubfield(VERSION), 1);
523 DBObject idKey = BasicDBObjectBuilder.start().add("id", 1).get();
524 _dbSessions.createIndex(idKey,
525 BasicDBObjectBuilder.start()
526 .add("name", "id_1")
527 .add("ns", _dbSessions.getFullName())
528 .add("sparse", false)
529 .add("unique", true)
530 .get());
531
532 DBObject versionKey = BasicDBObjectBuilder.start().add("id", 1).add("version", 1).get();
533 _dbSessions.createIndex(versionKey, BasicDBObjectBuilder.start()
534 .add("name", "id_1_version_1")
535 .add("ns", _dbSessions.getFullName())
536 .add("sparse", false)
537 .add("unique", true)
538 .get());
539 LOG.debug("done ensure Mongodb indexes existing");
540 //TODO perhaps index on expiry time?
541 }
542
543 private String getContextField()
544 {
545 return CONTEXT + "." + getCanonicalContextId();
546 }
547
548 private String getCanonicalContextId()
549 {
550 return canonicalizeVHost(_context.getVhost()) + ":" + _context.getCanonicalContextPath();
551 }
552
553 private String canonicalizeVHost(String vhost)
554 {
555 if (vhost == null)
556 return "";
557
558 return StringUtil.replace(vhost, '.', '_');
559 }
560
561 private String getContextSubfield(String attr)
562 {
563 return getContextField() + "." + attr;
564 }
565
566 @ManagedAttribute(value = "does store serialize sessions", readonly = true)
567 @Override
568 public boolean isPassivating()
569 {
570 return true;
571 }
572
573 @Override
574 public String toString()
575 {
576 return String.format("%s[collection=%s]", super.toString(), getDBCollection());
577 }
578 }