aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/TimeoutHandler.java
blob: 148a514d624afe276a1f0acac25fd9c5c5d95424 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.utils.communication.http;

import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncCallback;
import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation;
import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperationImpl;
import com.yahoo.vespa.clustercontroller.utils.util.Clock;

import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeoutException;
import java.util.logging.Logger;

public class TimeoutHandler<V extends HttpResult> extends AsyncHttpClientWithBase<V> {
    public static class InternalRequest<V extends HttpResult> extends AsyncOperationImpl<V> {
        final AsyncOperation<V> operation;
        long startTime;
        long timeout;

        public InternalRequest(AsyncOperation<V> op, long startTime, long timeout) {
            super(op.getName(), op.getDescription());
            this.operation = op;
            this.startTime = startTime;
            this.timeout = timeout;
            op.register(new AsyncCallback<V>() {
                @Override
                public void done(AsyncOperation<V> op) {
                    if (!isDone()) {
                        if (op.isSuccess()) {
                            setResult(op.getResult());
                        } else {
                            setFailure(op.getCause(), op.getResult());
                        }
                    }
                }
            });
        }

        public long getTimeoutTime() { return startTime + timeout; }

        public void handleTimeout(long currentTime) {
            long timePassed = currentTime - startTime;
            this.setFailure(new TimeoutException("Operation timeout. " + timePassed + " ms since operation was issued. Timeout was " + timeout + " ms."));
            operation.cancel();
        }

        @Override
        public boolean cancel() { return operation.cancel(); }
        @Override
        public boolean isCanceled() { return operation.isCanceled(); }
        @Override
        public Double getProgress() { return (isDone() ? Double.valueOf(1.0) : operation.getProgress()); }
    }

    public static class ChangeLogger {
        private InternalRequest lastTimeoutLogged = null;
        private boolean emptyLogged = true;

        public void logChanges(TreeMap<Long, InternalRequest> requests) {
            if (requests.isEmpty()) {
                if (!emptyLogged) {
                    log.finest("No more pending requests currently.");
                    emptyLogged = true;
                }
            } else {
                emptyLogged = false;
                InternalRequest r = requests.firstEntry().getValue();
                if (lastTimeoutLogged == null || !lastTimeoutLogged.equals(r)) {
                    lastTimeoutLogged = r;
                    log.finest("Next operation to possibly timeout will do so at " + r.getTimeoutTime());
                }
            }
        }
    }

    private final static Logger log = Logger.getLogger(TimeoutHandler.class.getName());
    private final TreeMap<Long, InternalRequest> requests = new TreeMap<>();
    private final ChangeLogger changeLogger = new ChangeLogger();
    private final Clock clock;
    private boolean run = true;
    private Runnable timeoutHandler = new Runnable() {
        @Override
        public void run() {
            log.fine("Starting timeout monitor thread");
            while (true) {
                performTimeoutHandlerTick();
                synchronized (clock) {
                    try{ clock.wait(100); } catch (InterruptedException e) {}
                    if (!run) break;
                }
            }
            log.fine("Stopped timeout monitor thread");
        }
    };

    public TimeoutHandler(Executor executor, Clock clock, AsyncHttpClient<V> client) {
        super(client);
        this.clock = clock;
        executor.execute(timeoutHandler);
    }

    @Override
    public void close() {
        synchronized (clock) {
            run = false;
            clock.notifyAll();
        }
        synchronized (requests) {
            for (InternalRequest r : requests.values()) {
                r.operation.cancel();
                r.setFailure(new TimeoutException("Timeout handler shutting down. Shutting down all requests monitored."));
            }
            requests.clear();
        }
    }

    @Override
    public AsyncOperation<V> execute(HttpRequest r) {
        AsyncOperation<V> op = super.execute(r);
        InternalRequest<V> request = new InternalRequest<>(op, clock.getTimeInMillis(), r.getTimeoutMillis());
        synchronized (requests) {
            requests.put(request.getTimeoutTime(), request);
        }
        return request;
    }

    void performTimeoutHandlerTick() {
        synchronized (requests) {
            removeCompletedRequestsFromTimeoutList();
            handleTimeoutsAtTime(clock.getTimeInMillis());
            changeLogger.logChanges(requests);
        }
    }

    private void removeCompletedRequestsFromTimeoutList() {
        while (!requests.isEmpty() && requests.firstEntry().getValue().operation.isDone()) {
            requests.remove(requests.firstEntry().getKey());
            log.finest("Removed completed request from operation timeout list.");
        }
    }

    private void handleTimeoutsAtTime(long currentTime) {
        Map<Long, InternalRequest> timeouts = requests.subMap(0l, currentTime + 1);
        for (InternalRequest r : timeouts.values()) {
            r.handleTimeout(currentTime);
            requests.values().remove(r);
        }
    }
}