com.swirlds.common.wiring.model.internal.deterministic.DeterministicTaskSchedulerBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swirlds-common Show documentation
Show all versions of swirlds-common Show documentation
Swirlds is a software platform designed to build fully-distributed applications that harness the power of the cloud without servers. Now you can develop applications with fairness in decision making, speed, trust and reliability, at a fraction of the cost of traditional server-based platforms.
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.swirlds.common.wiring.model.internal.deterministic;
import static com.swirlds.common.wiring.schedulers.builders.TaskSchedulerType.DIRECT_THREADSAFE;
import static com.swirlds.common.wiring.schedulers.builders.TaskSchedulerType.NO_OP;
import com.swirlds.common.context.PlatformContext;
import com.swirlds.common.metrics.extensions.FractionalTimer;
import com.swirlds.common.metrics.extensions.NoOpFractionalTimer;
import com.swirlds.common.wiring.model.DeterministicWiringModel;
import com.swirlds.common.wiring.model.TraceableWiringModel;
import com.swirlds.common.wiring.schedulers.TaskScheduler;
import com.swirlds.common.wiring.schedulers.builders.internal.AbstractTaskSchedulerBuilder;
import com.swirlds.common.wiring.schedulers.internal.DirectTaskScheduler;
import com.swirlds.common.wiring.schedulers.internal.NoOpTaskScheduler;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Objects;
import java.util.concurrent.ForkJoinPool;
import java.util.function.Consumer;
/**
* Builds schedulers for a {@link DeterministicWiringModel}.
*
* @param
*/
public class DeterministicTaskSchedulerBuilder extends AbstractTaskSchedulerBuilder {
private final Consumer submitWork;
/**
* Constructor.
*
* @param platformContext the platform context
* @param model the wiring model
* @param name the name of the task scheduler. Used for metrics and debugging. Must be unique. Must only
* contain alphanumeric characters and underscores.
* @param submitWork a method where all work should be submitted
*/
public DeterministicTaskSchedulerBuilder(
@NonNull final PlatformContext platformContext,
@NonNull final TraceableWiringModel model,
@NonNull final String name,
@NonNull final Consumer submitWork) {
super(platformContext, model, name, ForkJoinPool.commonPool());
this.submitWork = Objects.requireNonNull(submitWork);
}
/**
* {@inheritDoc}
*/
@NonNull
@Override
public TaskScheduler build() {
final boolean insertionIsBlocking = unhandledTaskCapacity != UNLIMITED_CAPACITY || externalBackPressure;
final Counters counters = buildCounters();
final FractionalTimer busyFractionTimer = NoOpFractionalTimer.getInstance();
final TaskScheduler scheduler =
switch (type) {
case CONCURRENT, SEQUENTIAL, SEQUENTIAL_THREAD -> new DeterministicTaskScheduler<>(
model,
name,
type,
counters.onRamp(),
counters.offRamp(),
unhandledTaskCapacity,
flushingEnabled,
squelchingEnabled,
insertionIsBlocking,
submitWork);
case DIRECT, DIRECT_THREADSAFE -> new DirectTaskScheduler<>(
model,
name,
buildUncaughtExceptionHandler(),
counters.onRamp(),
counters.offRamp(),
squelchingEnabled,
busyFractionTimer,
type == DIRECT_THREADSAFE);
case NO_OP -> new NoOpTaskScheduler<>(model, name, type, flushingEnabled, squelchingEnabled);
};
if (type != NO_OP) {
model.registerScheduler(scheduler, hyperlink);
}
return scheduler;
}
}