aboutsummaryrefslogtreecommitdiffstats
path: root/service-monitor/src/main/java/com/yahoo/vespa/service/executor/RunletExecutorImpl.java
blob: 48ad1bb8c9b7179b6428a26079374ec432d4b112 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.service.executor;

import java.util.logging.Level;

import java.time.Duration;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

/**
 * @author hakonhall
 */
public class RunletExecutorImpl implements RunletExecutor {
    private static Logger logger = Logger.getLogger(RunletExecutorImpl.class.getName());

    private final AtomicInteger executionId = new AtomicInteger(0);
    private final ConcurrentHashMap<Integer, CancellableImpl> cancellables = new ConcurrentHashMap<>();
    private final ScheduledThreadPoolExecutor executor;

    public RunletExecutorImpl(int threadPoolSize) {
        executor = new ScheduledThreadPoolExecutor(threadPoolSize);
    }

    public Cancellable scheduleWithFixedDelay(Runlet runlet, Duration delay) {
        Duration initialDelay = Duration.ofMillis((long) ThreadLocalRandom.current().nextInt((int) delay.toMillis()));
        CancellableImpl cancellable = new CancellableImpl(runlet);
        ScheduledFuture<?> future = executor.scheduleWithFixedDelay(cancellable, initialDelay.toMillis(), delay.toMillis(), TimeUnit.MILLISECONDS);
        cancellable.setPeriodicExecutionCancellationCallback(() -> future.cancel(false));
        Integer id = executionId.incrementAndGet();
        cancellables.put(id, cancellable);
        return () -> cancelRunlet(id);
    }

    private void cancelRunlet(Integer id) {
        CancellableImpl cancellable = cancellables.remove(id);
        if (cancellable != null) {
            cancellable.cancel();
        }
    }

    @Override
    public void close() {
        // At this point no-one should be scheduling new runlets, so this ought to clear the map.
        cancellables.keySet().forEach(this::cancelRunlet);

        if (cancellables.size() > 0) {
            throw new IllegalStateException("Runlets scheduled while closing the executor");
        }

        // The cancellables will cancel themselves from the executor only after up-to delay time,
        // so wait until all have drained.
        while (executor.getQueue().size() > 0) {
            try { Thread.sleep(200); } catch (InterruptedException ignored) { }
        }

        executor.shutdown();
        try {
            executor.awaitTermination(10, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            logger.log(Level.WARNING, "Timed out waiting for termination of executor", e);
        }
    }
}