aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/Pong.java
blob: b6deee61b816bb457b5c1fa8da66522b41471b8e (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude;

import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.statistics.ElapsedTime;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * An answer from Ping.
 *
 * @author bratseth
 */
public class Pong {

    private final ElapsedTime elapsed = new ElapsedTime();
    private final Optional<Long> activeDocuments;
    private final boolean isBlockingWrites;
    private final Optional<ErrorMessage> error;

    public Pong() {
        this(Optional.empty(), false, Optional.empty());
    }

    public Pong(ErrorMessage error) {
        this(Optional.empty(), false, Optional.of(error));
    }

    public Pong(long activeDocuments) {
        this(Optional.of(activeDocuments), false, Optional.empty());
    }

    public Pong(long activeDocuments, boolean isBlockingWrites) {
        this(Optional.of(activeDocuments), isBlockingWrites, Optional.empty());
    }

    private Pong(Optional<Long> activeDocuments, boolean isBlockingWrites, Optional<ErrorMessage> error) {
        this.activeDocuments = activeDocuments;
        this.isBlockingWrites = isBlockingWrites;
        this.error = error;
    }

    /**
     * @deprecated do not use. Additional errors are ignored.
     */
    @Deprecated
    public void addError(ErrorMessage error) { }

    /**
     * @deprecated use error() instead
     */
    @Deprecated
    public ErrorMessage getError(int i) {
        if (i > 1) throw new IllegalArgumentException("No error at position " + i);
        if (i == 0 && error.isEmpty()) throw new IllegalArgumentException("No error at position " + i);
        return error.get();
    }

    public Optional<ErrorMessage> error() { return error; }

    /** Returns the number of active documents in the backend responding in this Pong, if available */
    public Optional<Long> activeDocuments() { return activeDocuments; }

    /** Returns true if the pinged node is currently blocking write operations due to being full */
    public boolean isBlockingWrites() { return isBlockingWrites; }

    /**
     * Returns Optional.empty()
     *
     * @return empty
     * @deprecated do not use. There is always one pong per node.
     */
    @Deprecated
    public Optional<Integer> activeNodes() {
        return Optional.empty();
    }

    /**
     * Returns a list containing 0 or 1 errors
     *
     * @deprecated use error() instead
     */
    @Deprecated
    public List<ErrorMessage> getErrors() {
        return error.stream().collect(Collectors.toList());
    }

    /** Returns whether there is an error or not */
    public boolean badResponse() { return error.isPresent(); }

    public ElapsedTime getElapsedTime() { return elapsed; }

    /** Returns a string which included the ping info (if any) and any errors added to this */
    @Override
    public String toString() {
        StringBuilder m = new StringBuilder("Ping result");
        activeDocuments.ifPresent(docCount -> m.append(" active docs: ").append(docCount));
        if (isBlockingWrites)
            m.append(" blocking writes: true");
        error.ifPresent(e -> m.append(" error: ").append(error));
        return m.toString();
    }

}