aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/dispatch/rpc/Client.java
blob: a15fb4164cfcd9f5cafa90ab88720296bc8b66f7 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.dispatch.rpc;

import com.yahoo.compress.CompressionType;
import com.yahoo.prelude.fastsearch.FastHit;

import java.util.List;
import java.util.Optional;

/**
 * A dispatch client.
 *
 * @author bratseth
 */
public interface Client {

    /** Creates a connection to a particular node in this */
    NodeConnection createConnection(String hostname, int port);
    void close();

    interface ResponseReceiver {
        void receive(ResponseOrError<ProtobufResponse> response);
    }

    class ResponseOrError<T> {

        final Optional<T> response;
        final Optional<String> error;
        final boolean isTimeout;

        public static <T> ResponseOrError<T> fromResponse(T response) {
            return new ResponseOrError<>(response);
        }

        public static <T> ResponseOrError<T> fromError(String error) {
            return new ResponseOrError<T>(error, false);
        }

        public static <T> ResponseOrError<T> fromTimeoutError(String error) {
            return new ResponseOrError<T>(error, true);
        }

        ResponseOrError(T response) {
            this.response = Optional.of(response);
            this.error = Optional.empty();
            this.isTimeout = false;
        }

        ResponseOrError(String error, boolean isTimeout) {
            this.response = Optional.empty();
            this.error = Optional.of(error);
            this.isTimeout = isTimeout;
        }

        /** Returns the response, or empty if there is an error */
        public Optional<T> response() { return response; }

        /** Returns the error or empty if there is a response */
        public Optional<String> error() { return error; }

        /** @return true if error is a timeout */
        public boolean timeout() { return isTimeout; }
    }

    class GetDocsumsResponse {
        private final byte compression;
        private final int uncompressedSize;
        private final byte[] compressedSlimeBytes;
        private final List<FastHit> hitsContext;

        public GetDocsumsResponse(byte compression, int uncompressedSize, byte[] compressedSlimeBytes, List<FastHit> hitsContext) {
            this.compression = compression;
            this.uncompressedSize = uncompressedSize;
            this.compressedSlimeBytes = compressedSlimeBytes;
            this.hitsContext = hitsContext;
        }

        public byte compression() {
            return compression;
        }

        public int uncompressedSize() {
            return uncompressedSize;
        }

        public byte[] compressedSlimeBytes() {
            return compressedSlimeBytes;
        }

        public List<FastHit> hitsContext() {
            return hitsContext;
        }

    }

    interface NodeConnection {
        void request(String rpcMethod, CompressionType compression, int uncompressedLength, byte[] compressedPayload,
                     ResponseReceiver responseReceiver, double timeoutSeconds);

        /** Closes this connection */
        void close();

    }

    class ProtobufResponse {

        private final byte compression;
        private final int uncompressedSize;
        private final byte[] compressedPayload;

        public ProtobufResponse(byte compression, int uncompressedSize, byte[] compressedPayload) {
            this.compression = compression;
            this.uncompressedSize = uncompressedSize;
            this.compressedPayload = compressedPayload;
        }

        public byte compression() {
            return compression;
        }

        public int uncompressedSize() {
            return uncompressedSize;
        }

        public byte[] compressedPayload() {
            return compressedPayload;
        }

    }

}