/** * 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()))); } } }
/*------------------------------------------------------------ */ @Override protected synchronized NoSqlSession loadSession(String clusterId) { __log.debug("KVStoreSessionManager::loadSession id={}", clusterId); SortedMap<Key, ValueVersion> context_keyvalues = _kvstorehandler.multiGet( Key.createKey(Arrays.asList(__storeprefix, clusterId)), null, Depth.PARENT_AND_DESCENDANTS); // DBObject o = _kvstorehandler.findOne(new BasicDBObject(__ID,clusterId)); if (context_keyvalues == null || context_keyvalues.size() == 0) return null; ValueVersion vver_valid = context_keyvalues.get( Key.createKey(Arrays.asList(__storeprefix, clusterId), Arrays.asList(__VALID))); Boolean valid = null; if (vver_valid != null && vver_valid.getValue() != null && vver_valid.getValue().getValue() != null) try { valid = "1".equals(new String(vver_valid.getValue().getValue(), "UTF-8")); } catch (UnsupportedEncodingException e) { } __log.debug("KVStoreSessionManager:id={} valid={}", clusterId, valid); if (valid == null || !valid) return null; try { Object version = new Long( Base62Converter.toBase10( new String( context_keyvalues .get( Key.createKey( Arrays.asList(__storeprefix, clusterId), Arrays.asList(__CONTEXT, _contextId, __VERSION))) .getValue() .getValue(), "UTF-8"))); Long created = new Long( Base62Converter.toBase10( new String( context_keyvalues .get( Key.createKey( Arrays.asList(__storeprefix, clusterId), Arrays.asList(__CREATED))) .getValue() .getValue(), "UTF-8"))); Long accessed = new Long( Base62Converter.toBase10( new String( context_keyvalues .get( Key.createKey( Arrays.asList(__storeprefix, clusterId), Arrays.asList(__ACCESSED))) .getValue() .getValue(), "UTF-8"))); NoSqlSession session = null; for (Entry<Key, ValueVersion> attr_entry : context_keyvalues.entrySet()) { if (attr_entry .getKey() .toString() .startsWith(__storeprefix + "/" + clusterId + "/" + __CONTEXT + "/" + _contextId)) { // got our attribute if (session == null) { __log.debug( "KVStoreSessionManager: session {} present for context {}", clusterId, _contextId); session = new NoSqlSession(this, created, accessed, clusterId, version); } List<String> attr_minorpath = attr_entry.getKey().getMinorPath(); String attribute_name = attr_minorpath.get(attr_minorpath.size() - 1); String attr = decodeName(attribute_name); Object value = null; if (attr_entry.getValue() != null && attr_entry.getValue().getValue() != null && attr_entry.getValue().getValue().getValue() != null) { value = decodeValue(attr_entry.getValue().getValue().getValue()); session.doPutOrRemove(attr, value); session.bindValue(attr, value); } } } if (session != null) session.didActivate(); else __log.debug( "KVStoreSessionManager: session {} not present for context {}", clusterId, _contextId); return session; } catch (Exception e) { LOG.warn(e); } return null; }
/*------------------------------------------------------------ */ @Override protected Object refresh(NoSqlSession session, Object version) { __log.debug("KVStoreSessionManager:refresh session {}", session.getId()); // check if our in memory version is the same as what is on the disk if (version != null) { ValueVersion vval_version = this._kvstorehandler.get( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__CONTEXT, _contextId, __VERSION))); if (vval_version != null && vval_version.getValue().getValue() != null) { Long saved_version = new Long(-1L); try { saved_version = new Long( Base62Converter.toBase10( new String(vval_version.getValue().getValue(), "UTF-8"))); } catch (UnsupportedEncodingException e) { } if (saved_version.equals(version)) { __log.debug("KVStoreSessionManager:refresh not needed, session {}", session.getId()); return version; } version = saved_version; } } // If we are here, we have to load the object ValueVersion vver_valid = _kvstorehandler.get( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__VALID))); // If it doesn't exist, invalidate if (vver_valid == null || vver_valid.getValue() == null || vver_valid.getValue().getValue() == null) { __log.debug( "KVStoreSessionManager:refresh:marking session {} invalid, no object", session.getClusterId()); session.invalidate(); return null; } try { // If it has been flagged invalid, invalidate String valid_str = new String(vver_valid.getValue().getValue(), "UTF-8"); Boolean valid = valid_str == "1" ? true : false; if (!valid) { __log.debug( "KVstoreSessionManager:refresh:marking session {} invalid, valid flag {}", session.getClusterId(), valid); session.invalidate(); return null; } // We need to update the attributes. We will model this as a passivate, // followed by bindings and then activation. session.willPassivate(); SortedMap<Key, ValueVersion> context_keyvalues = _kvstorehandler.multiGet( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__CONTEXT, _contextId)), null, Depth.CHILDREN_ONLY); // if disk version now has no attributes, get rid of them if (context_keyvalues == null || context_keyvalues.size() == 0) { session.clearAttributes(); } else { // iterate over the names of the attributes on the disk version, updating the value ArrayList<String> stored_attrs = new ArrayList<String>(); for (Entry<Key, ValueVersion> attr_entry : context_keyvalues.entrySet()) { List<String> attr_minorpath = attr_entry.getKey().getMinorPath(); String attribute_name = attr_minorpath.get(attr_minorpath.size() - 1); String attr = decodeName(attribute_name); Object value = null; if (attr_entry.getValue() != null && attr_entry.getValue().getValue() != null && attr_entry.getValue().getValue().getValue() != null) value = decodeValue(attr_entry.getValue().getValue().getValue()); // session does not already contain this attribute, so bind it if (value != null) { stored_attrs.add(attr); if (session.getAttribute(attr) == null) { session.doPutOrRemove(attr, value); session.bindValue(attr, value); } else // session already contains this attribute, update its value session.doPutOrRemove(attr, value); } } // cleanup, remove values from session, that don't exist in data anymore: for (String sess_attr_entry : session.getNames()) { if (!stored_attrs.contains(sess_attr_entry)) { session.doPutOrRemove(sess_attr_entry, null); session.unbindValue(sess_attr_entry, session.getAttribute(sess_attr_entry)); } } } /* * We are refreshing so we should update the last accessed time. */ // update purge index too, if we here we assume valid is set to 1, so we do not need to check // for old value ValueVersion old_accessed = _kvstorehandler.get( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__ACCESSED))); if (old_accessed != null && old_accessed.getValue() != null && old_accessed.getValue().getValue() != null) { _kvstorehandler.delete( Key.createKey( Arrays.asList( __purgeindexprefix, "1", new String(old_accessed.getValue().getValue(), "UTF-8")), Arrays.asList(session.getClusterId()))); } String new_accessed = Base62Converter.longToLexiSortableBase62(System.currentTimeMillis()); _kvstorehandler.put( Key.createKey( Arrays.asList(__purgeindexprefix, "1", new_accessed), Arrays.asList(session.getClusterId())), Value.EMPTY_VALUE); _kvstorehandler.put( Key.createKey( Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__ACCESSED)), Value.createValue(new_accessed.getBytes("UTF-8"))); session.didActivate(); return version; } catch (Exception e) { LOG.warn(e); } return null; }