DockerCluster(DiscoveryStorageImpl storage, DockerClusterConfig config) { super(config, storage, Collections.singleton(Feature.SWARM_MODE)); long cacheTimeAfterWrite = config.getConfig().getCacheTimeAfterWrite(); nodesMap = SingleValueCache.builder(this::loadNodesMap) .timeAfterWrite(cacheTimeAfterWrite) .nullStrategy(SingleValueCache.NullStrategy.DIRTY) .build(); this.scheduledExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder() .setDaemon(true) .setNameFormat(getClass().getSimpleName() + "-" + getName() + "-%d") .build()); this.rereadNodesTask = RescheduledTask.builder() .runnable(this::rereadNodes) .service(this.scheduledExecutor) .maxDelay(10L, TimeUnit.SECONDS) .build(); }
/** * Maximal delay between first call of {@link RescheduledTask#schedule(long, TimeUnit)} and * execution of task. Without this, infinite call of 'schedule' (when timeout greater that time between calls) * can delay execution to heat death of universe. * @param maxDelay - delay, any non positive value mean infinity delay * @return this */ public Builder maxDelay(long maxDelay, TimeUnit timeUnit) { setMaxDelay(timeUnit.toMillis(maxDelay)); return this; }
/** * Schedule update of specified node in yhe next 10 seconds. Concrete time may be changed in future. * @param node name of node */ private void scheduleNodeUpdate(String node) { RescheduledTask task = this.scheduledNodes.computeIfAbsent(node, (n) -> { Runnable runnable = () -> this.updateNodeByName(n); return RescheduledTask.builder() .service(scheduledService) .runnable(runnable) .maxDelay(1L, TimeUnit.MINUTES) .build(); }); task.schedule(10L, TimeUnit.SECONDS); }
public static Builder builder() { return new Builder(); }
public Builder runnable(Runnable runnable) { setRunnable(runnable); return this; }
public Builder service(ScheduledExecutorService service) { setService(service); return this; }
ContainerRegistration(ContainerStorageImpl csi, String id) { this.id = id; Assert.notNull(id, "id is null"); this.container = DockerContainer.builder().id(id); this.map = csi.map; this.resheduleTask = RescheduledTask.builder() .maxDelay(10L, TimeUnit.SECONDS) .service(csi.executorService) .runnable(this::flush) .build(); }