org.infinispan.context.Flag Java Examples

The following examples show how to use org.infinispan.context.Flag. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InfinispanReplicated.java    From infinispan-simple-tutorials with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
   // Setup up a clustered cache manager
   GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder();
   // Initialize the cache manager
   DefaultCacheManager cacheManager = new DefaultCacheManager(global.build());
   // Create a replicated synchronous configuration
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.clustering().cacheMode(CacheMode.REPL_SYNC);
   Configuration cacheConfig = builder.build();
   // Create a cache
   Cache<String, String> cache = cacheManager.administration()
           .withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
           .getOrCreateCache("cache", cacheConfig);

   // Store the current node address in some random keys
   for(int i=0; i < 10; i++) {
      cache.put(UUID.randomUUID().toString(), cacheManager.getNodeAddress());
   }
   // Display the current cache contents for the whole cluster
   cache.entrySet().forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue()));
   // Display the current cache contents for this node
   cache.getAdvancedCache().withFlags(Flag.SKIP_REMOTE_LOOKUP)
      .entrySet().forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue()));
   // Stop the cache manager and release all resources
   cacheManager.stop();
}
 
Example #2
Source File: InfinispanDistributed.java    From infinispan-simple-tutorials with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
   // Setup up a clustered cache manager
   GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder();
   // Initialize the cache manager
   DefaultCacheManager cacheManager = new DefaultCacheManager(global.build());
   //Create cache configuration
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.clustering().cacheMode(CacheMode.DIST_SYNC);
   // Obtain a cache
   Cache<String, String> cache = cacheManager.administration().withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
         .getOrCreateCache("cache", builder.build());

   // Store the current node address in some random keys
   for (int i = 0; i < 10; i++) {
      cache.put(UUID.randomUUID().toString(), cacheManager.getNodeAddress());
   }
   // Display the current cache contents for the whole cluster
   cache.entrySet().forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue()));
   // Display the current cache contents for this node
   cache.getAdvancedCache().withFlags(Flag.SKIP_REMOTE_LOOKUP).entrySet()
         .forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue()));
   // Stop the cache manager and release all resources
   cacheManager.stop();
}
 
Example #3
Source File: RemoteCacheSessionsLoader.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@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;
    }
}
 
Example #4
Source File: ReplicatedServer.java    From unitime with Apache License 2.0 6 votes vote down vote up
@Override
public void remove(XStudent student) {
	Lock lock = writeLock();
	try {
		XStudent oldStudent = iStudentTable.remove(student.getStudentId());
		if (oldStudent != null) {
			for (XRequest request: oldStudent.getRequests())
				if (request instanceof XCourseRequest)
					for (XCourseId course: ((XCourseRequest)request).getCourseIds()) {
						Set<XCourseRequest> requests = iOfferingRequests.get(course.getOfferingId());
						if (requests != null) {
							if (!requests.remove(request))
								iLog.warn("REMOVE[1]: Request " + student + " " + request + " was not present in the offering requests table for " + course);
							iOfferingRequests.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).put(course.getOfferingId(), requests);
						} else {
							iLog.warn("REMOVE[2]: Request " + student + " " + request + " was not present in the offering requests table for " + course);
						}
					}
		}
	} finally {
		lock.release();
	}
}
 
Example #5
Source File: RemoteCacheSessionListener.java    From keycloak with Apache License 2.0 6 votes vote down vote up
protected void createRemoteEntityInCache(K key, long eventVersion) {
    VersionedValue<SessionEntityWrapper<V>> remoteSessionVersioned = remoteCache.getWithMetadata(key);

    // Maybe can happen under some circumstances that remoteCache doesn't yet contain the value sent in the event (maybe just theoretically...)
    if (remoteSessionVersioned == null || remoteSessionVersioned.getValue() == null) {
        logger.debugf("Entity '%s' not present in remoteCache. Ignoring create",
                key.toString());
        return;
    }


    V remoteSession = remoteSessionVersioned.getValue().getEntity();
    SessionEntityWrapper<V> newWrapper = new SessionEntityWrapper<>(remoteSession);

    logger.debugf("Read session entity wrapper from the remote cache: %s", remoteSession.toString());

    // Using putIfAbsent. Theoretic possibility that entity was already put to cache by someone else
    cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE, Flag.SKIP_CACHE_LOAD, Flag.IGNORE_RETURN_VALUES)
            .putIfAbsent(key, newWrapper);
}
 
Example #6
Source File: BaseCacheInitializer.java    From keycloak with Apache License 2.0 6 votes vote down vote up
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);
            }

        });
    }
 
Example #7
Source File: ReplicatedServerWithMaster.java    From unitime with Apache License 2.0 6 votes vote down vote up
@Override
public XCourseRequest assign(XCourseRequest request, XEnrollment enrollment) {
	iLog.info("Assign " + request + " with " + enrollment);
	if (!isMaster())
		iLog.warn("Assigning a request on a slave node. That is suspicious.");
	Lock lock = writeLock();
	try {
		XStudent student = iStudentTable.get(request.getStudentId());
		for (XRequest r: student.getRequests()) {
			if (r.equals(request)) {
				XCourseRequest cr = (XCourseRequest)r;

				// assign
				cr.setEnrollment(enrollment);
				
				iStudentTable.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).put(student.getStudentId(), student);
				return cr;
			}
		}
		iLog.warn("ASSIGN[3]: Request " + student + " " + request + " was not found among student requests");
		return null;
	} finally {
		lock.release();
	}
}
 
Example #8
Source File: ReplicatedServer.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public void update(XExpectations expectations) {
	Lock lock = writeLock();
	try {
		iExpectations.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).put(expectations.getOfferingId(), expectations);
	} finally {
		lock.release();
	}
}
 
Example #9
Source File: ConcurrencyJDGRemoveSessionTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@ClientCacheEntryRemoved
public void removed(ClientCacheEntryRemovedEvent event) {
    String cacheKey = (String) event.getKey();

    logger.infof("Listener executed for removing of session %s", cacheKey);

    // TODO: for distributed caches, ensure that it is executed just on owner OR if event.isCommandRetried
    origCache
            .getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD, Flag.SKIP_CACHE_STORE)
            .remove(cacheKey);

}
 
Example #10
Source File: ReplicatedServer.java    From unitime with Apache License 2.0 5 votes vote down vote up
protected void remove(XOffering offering, boolean removeExpectations) {
	Lock lock = writeLock();
	try {
		for (XCourse course: offering.getCourses()) {
			iCourseForId.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).remove(course.getCourseId());
			TreeSet<XCourseId> courses = iCourseForName.get(course.getCourseNameInLowerCase());
			if (courses != null) {
				courses.remove(course);
				if (courses.size() == 1) 
					for (XCourseId x: courses) x.setHasUniqueName(true);
				if (courses.isEmpty())
					iCourseForName.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).remove(course.getCourseNameInLowerCase());
				else
					iCourseForName.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).put(course.getCourseNameInLowerCase(), courses);
			}
		}
		iOfferingTable.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).remove(offering.getOfferingId());
		if (removeExpectations)
			iExpectations.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).remove(offering.getOfferingId());
		for (String externalId: offering.getInstructorExternalIds()) {
			Set<Long> offeringIds = iInstructedOfferings.get(externalId);
			if (offeringIds != null) {
				if (offeringIds.remove(offering.getOfferingId()))
					iInstructedOfferings.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).put(externalId, offeringIds);
			}
		}
	} finally {
		lock.release();
	}
}
 
Example #11
Source File: InfinispanNotificationsManager.java    From keycloak with Apache License 2.0 5 votes vote down vote up
void notify(String taskKey, ClusterEvent event, boolean ignoreSender, ClusterProvider.DCNotify dcNotify) {
    WrapperClusterEvent wrappedEvent = new WrapperClusterEvent();
    wrappedEvent.setEventKey(taskKey);
    wrappedEvent.setDelegateEvent(event);
    wrappedEvent.setIgnoreSender(ignoreSender);
    wrappedEvent.setIgnoreSenderSite(dcNotify == ClusterProvider.DCNotify.ALL_BUT_LOCAL_DC);
    wrappedEvent.setSender(myAddress);
    wrappedEvent.setSenderSite(mySite);

    String eventKey = UUID.randomUUID().toString();

    if (logger.isTraceEnabled()) {
        logger.tracef("Sending event with key %s: %s", eventKey, event);
    }

    if (dcNotify == ClusterProvider.DCNotify.LOCAL_DC_ONLY || workRemoteCache == null) {
        // Just put it to workCache, but skip notifying remoteCache
        workCache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES, Flag.SKIP_CACHE_STORE)
                .put(eventKey, wrappedEvent, 120, TimeUnit.SECONDS);
    } else {
        // Add directly to remoteCache. Will notify remote listeners on all nodes in all DCs
        Retry.executeWithBackoff((int iteration) -> {
            try {
                workRemoteCache.put(eventKey, wrappedEvent, 120, TimeUnit.SECONDS);
            } catch (HotRodClientException re) {
            if (logger.isDebugEnabled()) {
                logger.debugf(re, "Failed sending notification to remote cache '%s'. Key: '%s', iteration '%s'. Will try to retry the task",
                        workRemoteCache.getName(), eventKey, iteration);
            }

            // Rethrow the exception. Retry will take care of handle the exception and eventually retry the operation.
            throw re;
        }

    }, 10, 10);

    }
}
 
Example #12
Source File: InfinispanChangelogBasedTransaction.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private void runOperationInCluster(K key, MergedUpdate<V> task,  SessionEntityWrapper<V> sessionWrapper) {
    V session = sessionWrapper.getEntity();
    SessionUpdateTask.CacheOperation operation = task.getOperation(session);

    // Don't need to run update of underlying entity. Local updates were already run
    //task.runUpdate(session);

    switch (operation) {
        case REMOVE:
            // Just remove it
            CacheDecorators.skipCacheStore(cache)
                    .getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES)
                    .remove(key);
            break;
        case ADD:
            CacheDecorators.skipCacheStore(cache)
                    .getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES)
                    .put(key, sessionWrapper, task.getLifespanMs(), TimeUnit.MILLISECONDS);
            break;
        case ADD_IF_ABSENT:
            SessionEntityWrapper<V> existing = CacheDecorators.skipCacheStore(cache).putIfAbsent(key, sessionWrapper);
            if (existing != null) {
                logger.debugf("Existing entity in cache for key: %s . Will update it", key);

                // Apply updates on the existing entity and replace it
                task.runUpdate(existing.getEntity());

                replace(key, task, existing);
            }
            break;
        case REPLACE:
            replace(key, task, sessionWrapper);
            break;
        default:
            throw new IllegalStateException("Unsupported state " +  operation);
    }

}
 
Example #13
Source File: InfinispanClusterManager.java    From vertx-infinispan with Apache License 2.0 5 votes vote down vote up
@Override
public void setNodeInfo(NodeInfo nodeInfo, Promise<Void> promise) {
  synchronized (this) {
    this.nodeInfo = nodeInfo;
  }
  byte[] value = DataConverter.toCachedObject(nodeInfo);
  Future.fromCompletionStage(nodeInfoCache.withFlags(Flag.IGNORE_RETURN_VALUES).putAsync(getNodeId(), value))
    .<Void>mapEmpty()
    .onComplete(promise);
}
 
Example #14
Source File: AbstractSessionCacheCommand.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
protected void doRunCacheCommand(KeycloakSession session, Cache<String, SessionEntityWrapper> cache) {
    String id = getArg(1);
    cache = ((AdvancedCache) cache).withFlags(Flag.CACHE_MODE_LOCAL);
    UserSessionEntity userSession = (UserSessionEntity) cache.get(id).getEntity();
    printSession(id, userSession);
}
 
Example #15
Source File: RemoteCacheSessionListener.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@ClientCacheEntryRemoved
public void removed(ClientCacheEntryRemovedEvent event) {
    K key = (K) event.getKey();

    if (shouldUpdateLocalCache(event.getType(), key, event.isCommandRetried())) {

        this.executor.submit(event, () -> {

            // We received event from remoteCache, so we won't update it back
            cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE, Flag.SKIP_CACHE_LOAD, Flag.IGNORE_RETURN_VALUES)
                    .remove(key);

        });
    }
}
 
Example #16
Source File: RemoteCacheSessionsLoader.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public WorkerResult loadSessions(KeycloakSession session, RemoteCacheSessionsLoaderContext loaderContext, WorkerContext ctx) {
    Cache cache = getCache(session);
    Cache decoratedCache = cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD, Flag.SKIP_CACHE_STORE, Flag.IGNORE_RETURN_VALUES);
    RemoteCache remoteCache = getRemoteCache(session);

    Set<Integer> myIspnSegments = getMyIspnSegments(ctx.getSegment(), loaderContext);

    log.debugf("Will do bulk load of sessions from remote cache '%s' . Segment: %d", cache.getName(), ctx.getSegment());

    Map<Object, Object> remoteEntries = new HashMap<>();
    CloseableIterator<Map.Entry> iterator = null;
    int countLoaded = 0;
    try {
        iterator = remoteCache.retrieveEntries(null, myIspnSegments, loaderContext.getSessionsPerSegment());
        while (iterator.hasNext()) {
            countLoaded++;
            Map.Entry entry = iterator.next();
            remoteEntries.put(entry.getKey(), entry.getValue());
        }
    } catch (RuntimeException e) {
        log.warnf(e, "Error loading sessions from remote cache '%s' for segment '%d'", remoteCache.getName(), ctx.getSegment());
        throw e;
    } finally {
        if (iterator != null) {
            iterator.close();
        }
    }

    decoratedCache.putAll(remoteEntries);

    log.debugf("Successfully finished loading sessions from cache '%s' . Segment: %d, Count of sessions loaded: %d", cache.getName(), ctx.getSegment(), countLoaded);

    return new WorkerResult(true, ctx.getSegment(), ctx.getWorkerId());
}
 
Example #17
Source File: OfflinePersistentUserSessionLoader.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@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!");
}
 
Example #18
Source File: ReplicatedServerWithMaster.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public <E> void setProperty(String name, E value) {
	Cache<String, Object> properties = (Cache<String, Object>)iProperties;
	if (value == null)
		properties.getAdvancedCache().withFlags(Flag.FORCE_SYNCHRONOUS, Flag.IGNORE_RETURN_VALUES).remove(name);
	else
		properties.getAdvancedCache().withFlags(Flag.FORCE_SYNCHRONOUS, Flag.IGNORE_RETURN_VALUES).put(name,  value);
	flushCache(properties);
}
 
Example #19
Source File: ConcurrencyJDGRemoveSessionTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
        public void run() {

            for (int i=0 ; i<ITERATIONS ; i++) {
                String sessionId = String.valueOf(i);

                try {
                    Object o = remoteCache
                            .withFlags(org.infinispan.client.hotrod.Flag.FORCE_RETURN_VALUE)
                            .remove(sessionId);

                    if (o != null) {
                        removalCounts.get(sessionId).incrementAndGet();
                    }
                } catch (HotRodClientException hrce) {
                    errorsCounter.incrementAndGet();
                }
//
//
//                logger.infof("Session %s removed on DC1", sessionId);
//
//                // Check if it's immediately seen that session is removed on 2nd DC
//                RemoteCache secondDCRemoteCache = myThreadId == 1 ? remoteCache2 : remoteCache1;
//                SessionEntityWrapper thatSession = (SessionEntityWrapper) secondDCRemoteCache.get(sessionId);
//                Assert.assertNull("Session with ID " + sessionId + " not removed on the other DC. ThreadID: " + myThreadId, thatSession);
//
//                // Also check that it's immediatelly removed on my DC
//                SessionEntityWrapper mySession = (SessionEntityWrapper) remoteCache.get(sessionId);
//                Assert.assertNull("Session with ID " + sessionId + " not removed on the other DC. ThreadID: " + myThreadId, mySession);
            }

        }
 
Example #20
Source File: ConcurrencyJDGCacheReplaceTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@ClientCacheEntryModified
public void updated(ClientCacheEntryModifiedEvent event) {
    String cacheKey = (String) event.getKey();
    listenerCount.incrementAndGet();

    executor.submit(() -> {
        // TODO: can be optimized - object sent in the event
        VersionedValue<SessionEntity> versionedVal = remoteCache.getWithMetadata(cacheKey);
        for (int i = 0; i < 10; i++) {

            if (versionedVal.getVersion() < event.getVersion()) {
                System.err.println("INCOMPATIBLE VERSION. event version: " + event.getVersion() + ", entity version: " + versionedVal.getVersion() + ", i=" + i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ie) {
                    throw new RuntimeException(ie);
                }

                versionedVal = remoteCache.getWithMetadata(cacheKey);
            } else {
                break;
            }
        }

        SessionEntity session = (SessionEntity) versionedVal.getValue();
        SessionEntityWrapper sessionWrapper = new SessionEntityWrapper(session);

        if (listenerCount.get() % 100 == 0) {
            logger.infof("Listener count: " + listenerCount.get());
        }

        // TODO: for distributed caches, ensure that it is executed just on owner OR if event.isCommandRetried
        origCache
                .getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD, Flag.SKIP_CACHE_STORE)
                .replace(cacheKey, sessionWrapper);
    });
}
 
Example #21
Source File: ReplicatedServerWithMaster.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public void update(XOffering offering) {
	if (!isMaster())
		iLog.warn("Updating offering on a slave node. That is suspicious.");
	Lock lock = writeLock();
	try {
		iOfferingTable.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).put(offering.getOfferingId(), offering);
	} finally {
		lock.release();
	}
}
 
Example #22
Source File: ReplicatedServerWithMaster.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public void remove(XOffering offering) {
	if (!isMaster())
		iLog.warn("Removing offering on a slave node. That is suspicious.");
	Lock lock = writeLock();
	try {
		iOfferingTable.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).remove(offering.getOfferingId());
		iExpectations.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).remove(offering.getOfferingId());
	} finally {
		lock.release();
	}
}
 
Example #23
Source File: ReplicatedServerWithMaster.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public void update(XStudent student, boolean updateRequests) {
	iLog.debug("Update " + student + " with requests " + student.getRequests());
	if (!isMaster())
		iLog.warn("Updating student on a slave node. That is suspicious.");
	Lock lock = writeLock();
	try {
		iStudentTable.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).put(student.getStudentId(), student);
	} finally {
		lock.release();
	}
}
 
Example #24
Source File: ReplicatedServerWithMaster.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public void remove(XStudent student) {
	if (!isMaster())
		iLog.warn("Removing student on a slave node. That is suspicious.");
	Lock lock = writeLock();
	try {
		iStudentTable.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).remove(student.getStudentId());
	} finally {
		lock.release();
	}
}
 
Example #25
Source File: ReplicatedServerWithMaster.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public void update(XExpectations expectations) {
	if (!isMaster())
		iLog.warn("Updating expectations on a slave node. That is suspicious.");
	Lock lock = writeLock();
	try {
		iExpectations.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).put(expectations.getOfferingId(), expectations);
	} finally {
		lock.release();
	}
}
 
Example #26
Source File: PartitionManagerImpl.java    From hawkular-alerts with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void notifyEvents(Collection<Event> events) {
    if (distributed) {
        NotifyData nEvent = new NotifyData(currentNode, events, Event.class);
        Integer key = nEvent.hashCode();
        log.debugf("Sending events [%s]", nEvent);
        dataCache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES)
                .putAsync(key, nEvent, LIFESPAN, TimeUnit.MILLISECONDS);
    }
}
 
Example #27
Source File: PartitionManagerImpl.java    From hawkular-alerts with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void notifyData(Collection<Data> data) {
    if (distributed) {
        NotifyData nData = new NotifyData(currentNode, data, Data.class);
        Integer key = nData.hashCode();
        log.debugf("Sending data [%s]", nData);
        dataCache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES)
                .putAsync(key, nData, LIFESPAN, TimeUnit.MILLISECONDS);
    }
}
 
Example #28
Source File: PartitionManagerImpl.java    From hawkular-alerts with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void notifyTrigger(Operation operation, String tenantId, String triggerId) {
    if (distributed) {
        PartitionEntry newEntry = new PartitionEntry(tenantId, triggerId);
        int toNode = calculateNewEntry(newEntry, (Map<Integer, Integer>)partitionCache.get(BUCKETS));
        NotifyTrigger nTrigger = new NotifyTrigger(currentNode, toNode, operation, tenantId, triggerId);
        Integer key = nTrigger.hashCode();
        triggersCache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES)
                .putAsync(key, nTrigger, LIFESPAN, TimeUnit.MILLISECONDS);
    }
}
 
Example #29
Source File: InfinispanKeycloakTransaction.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private static <K, V> Cache<K, V> decorateCache(Cache<K, V> cache) {
    return cache.getAdvancedCache()
            .withFlags(Flag.IGNORE_RETURN_VALUES, Flag.SKIP_REMOTE_LOOKUP);
}
 
Example #30
Source File: InfinispanUserSessionProvider.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public UserSessionModel getUserSessionWithPredicate(RealmModel realm, String id, boolean offline, Predicate<UserSessionModel> predicate) {
    UserSessionModel userSession = getUserSession(realm, id, offline);
    if (userSession == null) {
        return null;
    }

    // We have userSession, which passes predicate. No need for remote lookup.
    if (predicate.test(userSession)) {
        log.debugf("getUserSessionWithPredicate(%s): found in local cache", id);
        return userSession;
    }

    // Try lookup userSession from remoteCache
    Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = getCache(offline);
    RemoteCache remoteCache = InfinispanUtil.getRemoteCache(cache);

    if (remoteCache != null) {
        SessionEntityWrapper<UserSessionEntity> remoteSessionEntityWrapper = (SessionEntityWrapper<UserSessionEntity>) remoteCache.get(id);
        if (remoteSessionEntityWrapper != null) {
            UserSessionEntity remoteSessionEntity = remoteSessionEntityWrapper.getEntity();
            log.debugf("getUserSessionWithPredicate(%s): remote cache contains session entity %s", id, remoteSessionEntity);

            UserSessionModel remoteSessionAdapter = wrap(realm, remoteSessionEntity, offline);
            if (predicate.test(remoteSessionAdapter)) {

                InfinispanChangelogBasedTransaction<String, UserSessionEntity> tx = getTransaction(offline);

                // Remote entity contains our predicate. Update local cache with the remote entity
                SessionEntityWrapper<UserSessionEntity> sessionWrapper = remoteSessionEntity.mergeRemoteEntityWithLocalEntity(tx.get(id));

                // Replace entity just in ispn cache. Skip remoteStore
                cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE, Flag.SKIP_CACHE_LOAD, Flag.IGNORE_RETURN_VALUES)
                        .replace(id, sessionWrapper);

                tx.reloadEntityInCurrentTransaction(realm, id, sessionWrapper);

                // Recursion. We should have it locally now
                return getUserSessionWithPredicate(realm, id, offline, predicate);
            } else {
                log.debugf("getUserSessionWithPredicate(%s): found, but predicate doesn't pass", id);

                return null;
            }
        } else {
            log.debugf("getUserSessionWithPredicate(%s): not found", id);

            // Session not available on remoteCache. Was already removed there. So removing locally too.
            // TODO: Can be optimized to skip calling remoteCache.remove
            removeUserSession(realm, userSession);

            return null;
        }
    } else {

        log.debugf("getUserSessionWithPredicate(%s): remote cache not available", id);

        return null;
    }
}