aboutsummaryrefslogtreecommitdiffstats
path: root/config-proxy/src/main/java/com/yahoo/vespa/config/proxy/ResponseHandler.java
blob: c9cfbdd3e166fa4bcf9b1155f413000e9e1f5595 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.proxy;

import com.yahoo.vespa.config.RawConfig;
import com.yahoo.vespa.config.protocol.JRTServerConfigRequest;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.yahoo.vespa.config.proxy.ConfigProxyRpcServer.TRACELEVEL;

/**
 * An RPC server that handles config and file distribution requests.
 *
 * @author hmusum
 */
public class ResponseHandler  {

    private final Optional<AtomicLong> sentResponses;

    public ResponseHandler() {
        this(false);
    }

    // For testing only
    ResponseHandler(boolean trackResponses) {
        sentResponses = trackResponses ? Optional.of(new AtomicLong()) : Optional.empty();
    }

    private final static Logger log = Logger.getLogger(ResponseHandler.class.getName());

    public void returnOkResponse(JRTServerConfigRequest request, RawConfig config) {
        request.getRequestTrace().trace(TRACELEVEL, "Config proxy returnOkResponse()");
        request.addOkResponse(config.getPayload(),
                              config.getGeneration(),
                              config.applyOnRestart(),
                              config.getPayloadChecksums());
        log.log(Level.FINE, () -> "Return response: " + request.getShortDescription() + ",config checksums=" + config.getPayloadChecksums() +
                ",generation=" + config.getGeneration());
        log.log(Level.FINEST, () -> "Config payload in response for " + request.getShortDescription() + ":" + config.getPayload());


        // TODO Catch exception for now, since the request might have been returned in CheckDelayedResponse
        // TODO Move logic so that all requests are returned in CheckDelayedResponse
        try {
            request.getRequest().returnRequest();
        } catch (IllegalStateException e) {
            log.log(Level.FINE, () -> "Something bad happened when sending response for '" + request.getShortDescription() + "':" + e.getMessage());
        }
        sentResponses.ifPresent(AtomicLong::getAndIncrement);
    }

    public void returnErrorResponse(JRTServerConfigRequest request, int errorCode, String message) {
        request.getRequestTrace().trace(TRACELEVEL, "Config proxy returnErrorResponse()");
        request.addErrorResponse(errorCode, message);
        request.getRequest().returnRequest();
    }

    public long sentResponses() { return sentResponses.map(AtomicLong::get).orElse(0L); }

}