protected void saveStateToCache(final InitializerState state) { // 3 attempts to send the message (it may fail if some node fails in the meantime) retry(3, new Runnable() { @Override public void run() { // Save this synchronously to ensure all nodes read correct state // We ignore cacheStore for now, so that in Cross-DC scenario (with RemoteStore enabled) is the remoteStore ignored. BaseCacheInitializer.this.workCache.getAdvancedCache(). withFlags(Flag.IGNORE_RETURN_VALUES, Flag.FORCE_SYNCHRONOUS, Flag.SKIP_CACHE_STORE, Flag.SKIP_CACHE_LOAD) .put(stateKey, state); } }); }
@Override public boolean isFinished(BaseCacheInitializer initializer) { Cache<String, Serializable> workCache = initializer.getWorkCache(); Boolean sessionsLoaded = (Boolean) workCache.get(PERSISTENT_SESSIONS_LOADED); if (sessionsLoaded != null && sessionsLoaded) { log.debugf("Persistent sessions loaded already."); return true; } else { log.debugf("Persistent sessions not yet loaded."); return false; } }
@Override protected boolean isFinished() { // Check if we should skipLoadingSessions. This can happen if someone else already did the task (For example in cross-dc environment, it was done by different DC) boolean isFinishedAlready = this.sessionLoader.isFinished(this); if (isFinishedAlready) { return true; } InitializerState state = getStateFromCache(); return state != null && state.isFinished(); }
@Override public boolean isFinished(BaseCacheInitializer initializer) { Cache<String, Serializable> workCache = initializer.getWorkCache(); // Check if persistent sessions were already loaded in this DC. This is possible just for offline sessions ATM Boolean sessionsLoaded = (Boolean) workCache .getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD, Flag.SKIP_CACHE_STORE) .get(OfflinePersistentUserSessionLoader.PERSISTENT_SESSIONS_LOADED_IN_CURRENT_DC); if ((cacheName.equals(InfinispanConnectionProvider.OFFLINE_USER_SESSION_CACHE_NAME) || (cacheName.equals(InfinispanConnectionProvider.OFFLINE_CLIENT_SESSION_CACHE_NAME))) && sessionsLoaded != null && sessionsLoaded) { log.debugf("Sessions already loaded in current DC. Skip sessions loading from remote cache '%s'", cacheName); return true; } else { log.debugf("Sessions maybe not yet loaded in current DC. Will load them from remote cache '%s'", cacheName); return false; } }
@Override public void afterAllSessionsLoaded(BaseCacheInitializer initializer) { Cache<String, Serializable> workCache = initializer.getWorkCache(); // Will retry few times for the case when backup site not available in cross-dc environment. // The site might be taken offline automatically if "take-offline" properly configured Retry.executeWithBackoff((int iteration) -> { try { // Cross-DC aware flag workCache .getAdvancedCache().withFlags(Flag.SKIP_REMOTE_LOOKUP) .put(PERSISTENT_SESSIONS_LOADED, true); } catch (HotRodClientException re) { log.warnf(re, "Failed to write flag PERSISTENT_SESSIONS_LOADED in iteration '%d' . Retrying", iteration); // Rethrow the exception. Retry will take care of handle the exception and eventually retry the operation. throw re; } }, 10, 10); // Just local-DC aware flag workCache .getAdvancedCache().withFlags(Flag.SKIP_REMOTE_LOOKUP, Flag.SKIP_CACHE_LOAD, Flag.SKIP_CACHE_STORE) .put(PERSISTENT_SESSIONS_LOADED_IN_CURRENT_DC, true); log.debugf("Persistent sessions loaded successfully!"); }