Search in sources :

Example 1 with ScheduledExecutorMergingEntryImpl

use of com.hazelcast.spi.impl.merge.ScheduledExecutorMergingEntryImpl in project hazelcast by hazelcast.

the class ScheduledExecutorContainer method merge.

/**
 * Merges the given {@link ScheduledExecutorMergeTypes} via the given {@link SplitBrainMergePolicy}.
 *
 * @param mergingEntry the {@link ScheduledExecutorMergeTypes} instance to merge
 * @param mergePolicy  the {@link SplitBrainMergePolicy} instance to apply
 * @return the used {@link ScheduledTaskDescriptor} if merge is applied, otherwise {@code null}
 */
public ScheduledTaskDescriptor merge(ScheduledExecutorMergeTypes mergingEntry, SplitBrainMergePolicy<ScheduledTaskDescriptor, ScheduledExecutorMergeTypes, ScheduledTaskDescriptor> mergePolicy) {
    SerializationService serializationService = nodeEngine.getSerializationService();
    mergingEntry = (ScheduledExecutorMergeTypes) serializationService.getManagedContext().initialize(mergingEntry);
    mergePolicy = (SplitBrainMergePolicy<ScheduledTaskDescriptor, ScheduledExecutorMergeTypes, ScheduledTaskDescriptor>) serializationService.getManagedContext().initialize(mergePolicy);
    // try to find an existing task with the same definition
    ScheduledTaskDescriptor mergingTask = ((ScheduledExecutorMergingEntryImpl) mergingEntry).getRawValue();
    ScheduledTaskDescriptor existingTask = null;
    for (ScheduledTaskDescriptor task : tasks.values()) {
        if (mergingTask.equals(task)) {
            existingTask = task;
            break;
        }
    }
    if (existingTask == null) {
        ScheduledTaskDescriptor newTask = mergePolicy.merge(mergingEntry, null);
        if (newTask != null) {
            enqueueSuspended(newTask, false);
            return newTask;
        }
    } else {
        ScheduledExecutorMergeTypes existingEntry = createMergingEntry(serializationService, existingTask);
        ScheduledTaskDescriptor newTask = mergePolicy.merge(mergingEntry, existingEntry);
        // but we still want to be able to choose which one is merged (e.g. PassThroughMergePolicy)
        if (newTask != null && newTask != existingTask) {
            // cancel the existing task, before replacing it
            existingTask.cancel(true);
            enqueueSuspended(newTask, true);
            return newTask;
        }
    }
    // the merging task was already suspended on the original node, so we don't have to cancel it here
    return null;
}
Also used : ScheduledExecutorMergeTypes(com.hazelcast.spi.merge.SplitBrainMergeTypes.ScheduledExecutorMergeTypes) SerializationService(com.hazelcast.internal.serialization.SerializationService) ScheduledExecutorMergingEntryImpl(com.hazelcast.spi.impl.merge.ScheduledExecutorMergingEntryImpl)

Aggregations

SerializationService (com.hazelcast.internal.serialization.SerializationService)1 ScheduledExecutorMergingEntryImpl (com.hazelcast.spi.impl.merge.ScheduledExecutorMergingEntryImpl)1 ScheduledExecutorMergeTypes (com.hazelcast.spi.merge.SplitBrainMergeTypes.ScheduledExecutorMergeTypes)1