/** * Change the session id. Note that this will change the session id for all contexts for which the * session id is in use. * * @see org.eclipse.jetty.nosql.NoSqlSessionManager#update(org.eclipse.jetty.nosql.NoSqlSession, * java.lang.String, java.lang.String) */ @Override protected void update(NoSqlSession session, String newClusterId, String newNodeId) throws Exception { __log.debug( "KVStoreSessionManager:update session {} to {}", session.getClusterId(), newClusterId); SortedMap<Key, ValueVersion> old_keyvalues = _kvstorehandler.multiGet( Key.createKey(Arrays.asList(__storeprefix, session.getClusterId())), null, Depth.PARENT_AND_DESCENDANTS); if (old_keyvalues != null && old_keyvalues.size() > 0) { this.kvstore_object_ops.clear(); ValueVersion expiry_old_val = null; for (Entry<Key, ValueVersion> old_entry : old_keyvalues.entrySet()) { List<String> major_keypart = old_entry.getKey().getMajorPath(); List<String> minor_keypart = old_entry.getKey().getMinorPath(); if (old_entry .getKey() .toString() .startsWith(__storeprefix + "/" + session.getClusterId() + "/" + __EXPIRY)) { // got expiry value, save it for linkback expiry_old_val = old_entry.getValue(); } this.kvstore_object_ops.add( this.kvstore_opfactory.createPut( Key.createKey(major_keypart, minor_keypart), old_entry.getValue().getValue())); major_keypart.set(1, newClusterId); } if (this.kvstore_object_ops.size() > 0) { _kvstorehandler.execute(this.kvstore_object_ops); this.kvstore_object_ops.clear(); _kvstorehandler.multiDelete( Key.createKey(Arrays.asList(__storeprefix, session.getClusterId())), null, Depth.PARENT_AND_DESCENDANTS); } if (expiry_old_val != null) { _kvstorehandler.put( Key.createKey( Arrays.asList( __expirydailyindexprefix, new String(expiry_old_val.getValue().getValue()), "UTF-8"), Arrays.asList(newClusterId)), Value.EMPTY_VALUE); _kvstorehandler.delete( Key.createKey( Arrays.asList( __expirydailyindexprefix, new String(expiry_old_val.getValue().getValue()), "UTF-8"), Arrays.asList(session.getClusterId()))); } } }
/** @see org.eclipse.jetty.nosql.NoSqlSessionManager#expire(java.lang.String) */ @Override protected void expire(String idInCluster) { __log.debug("KVStoreSessionManager:expire session {} ", idInCluster); // Expire the session for this context super.expire(idInCluster); // If the outer session document has not already been marked invalid, do so. ValueVersion vver_valid = _kvstorehandler.get( Key.createKey(Arrays.asList(__storeprefix, idInCluster), Arrays.asList(__VALID))); try { if (vver_valid != null && vver_valid.getValue() != null && vver_valid.getValue().getValue() != null && "0".equals(new String(vver_valid.getValue().getValue(), "UTF-8"))) { if (this.kvstore_opfactory == null) this.kvstore_opfactory = this._kvstorehandler.getOperationFactory(); kvstore_object_ops.clear(); kvstore_object_ops.add( this.kvstore_opfactory.createPut( Key.createKey(Arrays.asList(__storeprefix, idInCluster), Arrays.asList(__VALID)), Value.createValue("0".getBytes("UTF-8")))); kvstore_object_ops.add( this.kvstore_opfactory.createPut( Key.createKey( Arrays.asList(__storeprefix, idInCluster), Arrays.asList(__INVALIDATED)), Value.createValue( Base62Converter.fromBase10(System.currentTimeMillis()).getBytes("UTF-8")))); // update indexes // delete old entries, if present ValueVersion old_valid = _kvstorehandler.get( Key.createKey(Arrays.asList(__storeprefix, idInCluster), Arrays.asList(__VALID))); __log.debug("old_valid=" + (old_valid != null ? "notnull" : "null")); ValueVersion old_accessed = _kvstorehandler.get( Key.createKey( Arrays.asList(__storeprefix, idInCluster), Arrays.asList(__ACCESSED))); __log.debug("old_accessed=" + (old_accessed != null ? "notnull" : "null")); if (old_valid != null && old_valid.getValue() != null && old_valid.getValue().getValue() != null && old_accessed != null && old_accessed.getValue() != null && old_accessed.getValue().getValue() != null) { __log.debug("deleting old purgeindex entry"); _kvstorehandler.delete( Key.createKey( Arrays.asList( __purgeindexprefix, new String(old_valid.getValue().getValue(), "UTF-8"), new String(old_accessed.getValue().getValue(), "UTF-8")), Arrays.asList(idInCluster))); } else __log.debug("some keys for purge index missing, nothing to delete"); _kvstorehandler.put( Key.createKey( Arrays.asList( __purgeindexprefix, "0", new String(old_accessed.getValue().getValue(), "UTF-8")), Arrays.asList(idInCluster)), Value.EMPTY_VALUE); if (kvstore_object_ops.size() > 0) { _kvstorehandler.execute(kvstore_object_ops); kvstore_object_ops.clear(); } } } catch (UnsupportedEncodingException | OperationExecutionException | FaultException e) { __log.debug( "KVStoreSessionManager: expire :: error :: session {} context {} ", idInCluster, _contextId); } }
/* ------------------------------------------------------------ */ @Override protected synchronized Object save( NoSqlSession session, Object version, boolean activateAfterSave) { try { __log.debug("KVStoreSessionManager:save session {}", session.getClusterId()); session.willPassivate(); this.kvstore_object_ops.clear(); if (this.kvstore_opfactory == null) this.kvstore_opfactory = this._kvstorehandler.getOperationFactory(); /* // Form query for upsert BasicDBObject key = new BasicDBObject(__ID,session.getClusterId()); // Form updates BasicDBObject update = new BasicDBObject(); BasicDBObject sets = new BasicDBObject(); BasicDBObject unsets = new BasicDBObject(); */ boolean upsert = false; boolean need_to_purge_attributes = false; // handle valid or invalid String new_valid = null; String new_expiry = null; String new_accessed = Base62Converter.longToLexiSortableBase62(session.getAccessed()); if (session.isValid()) { new_valid = "1"; long expiry = (session.getMaxInactiveInterval() > 0 ? (session.getAccessed() + (1000L * getMaxInactiveInterval())) : 0); new_expiry = Base62Converter.longToLexiSortableBase62(expiry); __log.debug( "KVStoreSessionManager: calculated expiry {} for session {}", expiry, session.getId()); // handle new or existing if (version == null) { __log.debug("New session"); upsert = true; version = new Long(1); kvstore_object_ops.add( this.kvstore_opfactory.createPut( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__CREATED)), Value.createValue( Base62Converter.fromBase10(session.getCreationTime()).getBytes("UTF-8")))); kvstore_object_ops.add( this.kvstore_opfactory.createPut( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__VALID)), Value.createValue(new_valid.getBytes("UTF-8")))); kvstore_object_ops.add( this.kvstore_opfactory.createPut( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__CONTEXT, _contextId, __VERSION)), Value.createValue( Base62Converter.fromBase10(((Long) version).longValue()).getBytes("UTF-8")))); kvstore_object_ops.add( this.kvstore_opfactory.createPut( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__MAX_IDLE)), Value.createValue( Base62Converter.fromBase10(getMaxInactiveInterval()).getBytes("UTF-8")))); kvstore_object_ops.add( this.kvstore_opfactory.createPut( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__EXPIRY)), Value.createValue(new_expiry.getBytes("UTF-8")))); } else { version = new Long(((Number) version).longValue() + 1); kvstore_object_ops.add( this.kvstore_opfactory.createPut( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__CONTEXT, _contextId, __VERSION)), Value.createValue( Base62Converter.fromBase10(((Long) version).longValue()).getBytes("UTF-8")))); // if max idle time and/or expiry is smaller for this context, then choose that for the // whole session doc ValueVersion vval_max_idle = this._kvstorehandler.get( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__MAX_IDLE))); ValueVersion vval_expiry = this._kvstorehandler.get( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__EXPIRY))); if (vval_max_idle != null && vval_max_idle.getValue().getValue() != null) { long currentMaxIdle = Base62Converter.toBase10(new String(vval_max_idle.getValue().getValue(), "UTF-8")); if (getMaxInactiveInterval() > 0 && getMaxInactiveInterval() < currentMaxIdle) kvstore_object_ops.add( this.kvstore_opfactory.createPut( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__MAX_IDLE)), Value.createValue( Base62Converter.fromBase10(getMaxInactiveInterval()).getBytes("UTF-8")))); } if (vval_expiry != null && vval_expiry.getValue().getValue() != null) { long currentExpiry = Base62Converter.toBase10(new String(vval_expiry.getValue().getValue(), "UTF-8")); if (expiry > 0 && expiry != currentExpiry) { new_expiry = Base62Converter.longToLexiSortableBase62(expiry); kvstore_object_ops.add( this.kvstore_opfactory.createPut( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__EXPIRY)), Value.createValue(new_expiry.getBytes("UTF-8")))); } } } kvstore_object_ops.add( this.kvstore_opfactory.createPut( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__ACCESSED)), Value.createValue(new_accessed.getBytes("UTF-8")))); __log.debug( "session.isDirty=" + session.isDirty() + ";getSavePeriod=" + this.getSavePeriod()); Set<String> names = session.takeDirty(); if (isSaveAllAttributes() || upsert) { names.addAll(session.getNames()); // note dirty may include removed names } for (String name : names) { Object value = session.getAttribute(name); if (value == null) kvstore_object_ops.add( this.kvstore_opfactory.createDelete( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__CONTEXT, _contextId, name)))); else kvstore_object_ops.add( this.kvstore_opfactory.createPut( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__CONTEXT, _contextId, name)), Value.createValue(encodeValue(value)))); } } else { need_to_purge_attributes = true; new_valid = "0"; kvstore_object_ops.add( this.kvstore_opfactory.createPut( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__VALID)), Value.createValue(new_valid.getBytes("UTF-8")))); kvstore_object_ops.add( this.kvstore_opfactory.createPut( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__INVALIDATED)), Value.createValue( Base62Converter.fromBase10(System.currentTimeMillis()).getBytes("UTF-8")))); // unsets.put(getContextKey(),1); } // update indexes // delete old entries, if present ValueVersion old_valid = _kvstorehandler.get( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__VALID))); ValueVersion old_accessed = _kvstorehandler.get( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__ACCESSED))); ValueVersion old_expiry = _kvstorehandler.get( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__EXPIRY))); if (old_valid != null && old_valid.getValue() != null && old_valid.getValue().getValue() != null && old_accessed != null && old_accessed.getValue() != null && old_accessed.getValue().getValue() != null) { __log.debug( "delete purgeindex entry for old_valid=" + new String(old_valid.getValue().getValue(), "UTF-8") + ";old_accessed=" + new String(old_accessed.getValue().getValue(), "UTF-8")); _kvstorehandler.delete( Key.createKey( Arrays.asList( __purgeindexprefix, new String(old_valid.getValue().getValue(), "UTF-8"), new String(old_accessed.getValue().getValue(), "UTF-8")), Arrays.asList(session.getClusterId()))); } else __log.debug( "nothing to delete from purge index, either accessed or valid keys are missing"); __log.debug( "new_valid=" + new_valid + ";new_accessed=" + new_accessed + ";new_expiry=" + new_expiry); _kvstorehandler.put( Key.createKey( Arrays.asList(__purgeindexprefix, new_valid, new_accessed), Arrays.asList(session.getClusterId())), Value.EMPTY_VALUE); if (new_expiry != null) { if (old_expiry != null && old_expiry.getValue() != null && old_expiry.getValue().getValue() != null) { _kvstorehandler.delete( Key.createKey( Arrays.asList( __expirydailyindexprefix, new String(old_expiry.getValue().getValue(), "UTF-8")), Arrays.asList(session.getClusterId()))); } else __log.debug("nothing to delete from expiry index, old expiry entry key is missing"); _kvstorehandler.put( Key.createKey( Arrays.asList(__expirydailyindexprefix, new_expiry), Arrays.asList(session.getClusterId())), Value.EMPTY_VALUE); } // Done with indexes, do the upsert if (!kvstore_object_ops.isEmpty()) { _kvstorehandler.execute(kvstore_object_ops); kvstore_object_ops.clear(); } if (need_to_purge_attributes) { /* __log.debug("before need to purge attrubutes"); Iterator<Key> it = _kvstorehandler.multiGetKeysIterator(Direction.FORWARD, 10, Key.createKey(Arrays.asList( __storeprefix, session.getClusterId())), null,//new KeyRange("minorpart1",true,"minorpart1",true), Depth.PARENT_AND_DESCENDANTS); while (it.hasNext()) { Key key = it.next(); __log.debug("key="+key.toString()); } */ _kvstorehandler.multiDelete( Key.createKey(Arrays.asList(__storeprefix, session.getClusterId())), new KeyRange(__CONTEXT, true, __CONTEXT, true), Depth.PARENT_AND_DESCENDANTS); /* __log.debug("after need to purge attrubutes"); Iterator<Key> it = _kvstorehandler.multiGetKeysIterator(Direction.FORWARD, 10, Key.createKey(Arrays.asList( __storeprefix, session.getClusterId())), null,//new KeyRange("minorpart1",true,"minorpart1",true), Depth.PARENT_AND_DESCENDANTS); while (it.hasNext()) { Key key = it.next(); __log.debug("key="+key.toString()); } */ } if (__log.isDebugEnabled()) __log.debug("KVStoreSessionManager:save:db.sessions.updated"); if (activateAfterSave) session.didActivate(); return version; } catch (Exception e) { LOG.warn(e); } return null; }