private void doSchedule(Object mapKey, ScheduledEntry<K, V> entry, Integer second) { Map<Object, ScheduledEntry<K, V>> entries = scheduledEntries.get(second); boolean shouldSchedule = false; if (entries == null) { entries = new HashMap<Object, ScheduledEntry<K, V>>(INITIAL_CAPACITY); scheduledEntries.put(second, entries); // we created the second // so we will schedule its execution shouldSchedule = true; } entries.put(mapKey, entry); if (shouldSchedule) { schedule(second, entry.getActualDelaySeconds()); } }
@Override public int compare(ScheduledEntry o1, ScheduledEntry o2) { if (o1.getScheduleId() > o2.getScheduleId()) { return 1; } else if (o1.getScheduleId() < o2.getScheduleId()) { return -1; } return 0; } };
private boolean scheduleEntry(long delayMillis, K key, V value) { int delaySeconds = ceilToSecond(delayMillis); Integer newSecond = findRelativeSecond(delayMillis); synchronized (mutex) { long id = uniqueIdGenerator.incrementAndGet(); Object compositeKey = new CompositeKey(key, id); secondsOfKeys.put(compositeKey, newSecond); ScheduledEntry<K, V> scheduledEntry = new ScheduledEntry<K, V>(key, value, delayMillis, delaySeconds, id); doSchedule(compositeKey, scheduledEntry, newSecond); } return true; }
@Override public void run() throws Exception { for (ScheduledEntry<Object, Object> entry : entries) { Object key = entry.getKey(); store.evict(key); } }
/** * Removes the entry if it exists from being scheduled to be evicted. * <p/> * Cleans up parent container (second -> entries map) if it doesn't hold anymore items this second. * <p/> * Cancels associated scheduler (second -> scheduler map ) if there are no more items to remove for this second. * <p/> * Returns associated scheduled entry. * * @param second second at which this entry was scheduled to be evicted * @param entries entries which were already scheduled to be evicted for this second * @param key entry key * @param entryToRemove entry value that is expected to exist in the map * @return true if entryToRemove exists in the map and removed */ private boolean cancelAndCleanUpIfEmpty(Integer second, Map<Object, ScheduledEntry<K, V>> entries, Object key, ScheduledEntry<K, V> entryToRemove) { ScheduledEntry<K, V> entry = entries.get(key); if (entry == null || !entry.equals(entryToRemove)) { return false; } entries.remove(key); cleanUpScheduledFuturesIfEmpty(second, entries); return true; }
@Override public int cancelIfExists(K key, V value) { synchronized (mutex) { ScheduledEntry<K, V> scheduledEntry = new ScheduledEntry<K, V>(key, value, 0, 0, 0); if (scheduleType.equals(ScheduleType.FOR_EACH)) { return cancelByCompositeKey(key, scheduledEntry); } Integer second = secondsOfKeys.remove(key); if (second == null) { return 0; } Map<Object, ScheduledEntry<K, V>> entries = scheduledEntries.get(second); if (entries == null) { return 0; } return cancelAndCleanUpIfEmpty(second, entries, key, scheduledEntry) ? 1 : 0; } }
@Override public void process(EntryTaskScheduler<ReplicaFragmentSyncInfo, Void> scheduler, Collection<ScheduledEntry<ReplicaFragmentSyncInfo, Void>> entries) { for (ScheduledEntry<ReplicaFragmentSyncInfo, Void> entry : entries) { ReplicaFragmentSyncInfo syncInfo = entry.getKey(); if (replicaSyncRequests.remove(syncInfo)) { releaseReplicaSyncPermits(1); } } } }
/** * Removes the entry if it exists from being scheduled to be evicted. * <p/> * Cleans up parent container (second -> entries map) if it doesn't hold anymore items this second. * <p/> * Cancels associated scheduler (second -> scheduler map ) if there are no more items to remove for this second. * <p/> * Returns associated scheduled entry. * * @param second second at which this entry was scheduled to be evicted * @param entries entries which were already scheduled to be evicted for this second * @param key entry key * @param entryToRemove entry value that is expected to exist in the map * @return true if entryToRemove exists in the map and removed */ private boolean cancelAndCleanUpIfEmpty(Integer second, Map<Object, ScheduledEntry<K, V>> entries, Object key, ScheduledEntry<K, V> entryToRemove) { ScheduledEntry<K, V> entry = entries.get(key); if (entry == null || !entry.equals(entryToRemove)) { return false; } entries.remove(key); cleanUpScheduledFuturesIfEmpty(second, entries); return true; }
@Override public int cancelIfExists(K key, V value) { synchronized (mutex) { ScheduledEntry<K, V> scheduledEntry = new ScheduledEntry<K, V>(key, value, 0, 0, 0); if (scheduleType.equals(ScheduleType.FOR_EACH)) { return cancelByCompositeKey(key, scheduledEntry); } Integer second = secondsOfKeys.remove(key); if (second == null) { return 0; } Map<Object, ScheduledEntry<K, V>> entries = scheduledEntries.get(second); if (entries == null) { return 0; } return cancelAndCleanUpIfEmpty(second, entries, key, scheduledEntry) ? 1 : 0; } }
@Override public void process(EntryTaskScheduler<ReplicaFragmentSyncInfo, Void> scheduler, Collection<ScheduledEntry<ReplicaFragmentSyncInfo, Void>> entries) { for (ScheduledEntry<ReplicaFragmentSyncInfo, Void> entry : entries) { ReplicaFragmentSyncInfo syncInfo = entry.getKey(); if (replicaSyncRequests.remove(syncInfo)) { releaseReplicaSyncPermits(1); } } } }
@Override public int compare(ScheduledEntry o1, ScheduledEntry o2) { if (o1.getScheduleId() > o2.getScheduleId()) { return 1; } else if (o1.getScheduleId() < o2.getScheduleId()) { return -1; } return 0; } };
private void doSchedule(Object mapKey, ScheduledEntry<K, V> entry, Integer second) { Map<Object, ScheduledEntry<K, V>> entries = scheduledEntries.get(second); boolean shouldSchedule = false; if (entries == null) { entries = new HashMap<Object, ScheduledEntry<K, V>>(INITIAL_CAPACITY); scheduledEntries.put(second, entries); // we created the second // so we will schedule its execution shouldSchedule = true; } entries.put(mapKey, entry); if (shouldSchedule) { schedule(second, entry.getActualDelaySeconds()); } }
private boolean schedulePostponeEntry(long delayMillis, K key, V value) { int delaySeconds = ceilToSecond(delayMillis); Integer newSecond = findRelativeSecond(delayMillis); synchronized (mutex) { Integer existingSecond = secondsOfKeys.put(key, newSecond); if (existingSecond != null) { if (existingSecond.equals(newSecond)) { return false; } removeKeyFromSecond(key, existingSecond); } long id = uniqueIdGenerator.incrementAndGet(); ScheduledEntry<K, V> scheduledEntry = new ScheduledEntry<K, V>(key, value, delayMillis, delaySeconds, id); doSchedule(key, scheduledEntry, newSecond); } return true; }
@Override public void run() throws Exception { for (ScheduledEntry<Object, Object> entry : entries) { Object key = entry.getKey(); store.evict(key); } }
private boolean scheduleEntry(long delayMillis, K key, V value) { int delaySeconds = ceilToSecond(delayMillis); Integer newSecond = findRelativeSecond(delayMillis); synchronized (mutex) { long id = uniqueIdGenerator.incrementAndGet(); Object compositeKey = new CompositeKey(key, id); secondsOfKeys.put(compositeKey, newSecond); ScheduledEntry<K, V> scheduledEntry = new ScheduledEntry<K, V>(key, value, delayMillis, delaySeconds, id); doSchedule(compositeKey, scheduledEntry, newSecond); } return true; }
@Override public void process(EntryTaskScheduler<String, Void> scheduler, Collection<ScheduledEntry<String, Void>> entries) { if (entries.isEmpty()) { return; } IPartitionService partitionService = nodeEngine.getPartitionService(); OperationService operationService = nodeEngine.getOperationService(); for (ScheduledEntry<String, Void> entry : entries) { String name = entry.getKey(); int partitionId = partitionService.getPartitionId(nodeEngine.toData(name)); Operation op = new CheckAndEvictOperation(entry.getKey()) .setPartitionId(partitionId); operationService.invokeOnPartition(op).join(); } } }
private boolean schedulePostponeEntry(long delayMillis, K key, V value) { int delaySeconds = ceilToSecond(delayMillis); Integer newSecond = findRelativeSecond(delayMillis); synchronized (mutex) { Integer existingSecond = secondsOfKeys.put(key, newSecond); if (existingSecond != null) { if (existingSecond.equals(newSecond)) { return false; } removeKeyFromSecond(key, existingSecond); } long id = uniqueIdGenerator.incrementAndGet(); ScheduledEntry<K, V> scheduledEntry = new ScheduledEntry<K, V>(key, value, delayMillis, delaySeconds, id); doSchedule(key, scheduledEntry, newSecond); } return true; }
@Override public void process(EntryTaskScheduler<String, Void> scheduler, Collection<ScheduledEntry<String, Void>> entries) { if (entries.isEmpty()) { return; } IPartitionService partitionService = nodeEngine.getPartitionService(); OperationService operationService = nodeEngine.getOperationService(); for (ScheduledEntry<String, Void> entry : entries) { String name = entry.getKey(); int partitionId = partitionService.getPartitionId(nodeEngine.toData(name)); Operation op = new CheckAndEvictOperation(entry.getKey()) .setPartitionId(partitionId); operationService.invokeOnPartition(op).join(); } } }