aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient-core/src/main/java/com/yahoo/feedhandler/FeedResponse.java
blob: dd95e1dc2023c127f77f332d72c27526e2f613bb (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.feedhandler;

import com.yahoo.clientmetrics.RouteMetricSet;
import com.yahoo.documentapi.messagebus.protocol.DocumentProtocol;
import com.yahoo.documentapi.messagebus.protocol.PutDocumentMessage;
import com.yahoo.documentapi.messagebus.protocol.RemoveDocumentMessage;
import com.yahoo.documentapi.messagebus.protocol.UpdateDocumentMessage;
import com.yahoo.feedapi.SharedSender;
import com.yahoo.messagebus.Error;
import com.yahoo.messagebus.ErrorCode;
import com.yahoo.messagebus.Message;
import com.yahoo.messagebus.Reply;
import com.yahoo.search.result.ErrorMessage;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Stream;

public final class FeedResponse implements SharedSender.ResultCallback {

    private final static Logger log = Logger.getLogger(FeedResponse.class.getName());
    private final List<ErrorMessage> errorMessages = new ArrayList<>();
    private final List<String> errors = new ArrayList<>();
    private final StringBuilder traces = new StringBuilder();
    private final RouteMetricSet metrics;
    private boolean abortOnError = false;
    private boolean isAborted = false;
    private final SharedSender.Pending pendingNumber = new SharedSender.Pending();

    FeedResponse(RouteMetricSet metrics) {
        this.metrics = metrics;
    }

    public boolean isAborted() {
        return isAborted;
    }

    void setAbortOnFeedError(boolean abort) {
        abortOnError = abort;
    }

    private String prettyPrint(Message m) {
        if (m instanceof PutDocumentMessage) {
            return "PUT[" + ((PutDocumentMessage)m).getDocumentPut().getDocument().getId() + "] ";
        }
        if (m instanceof RemoveDocumentMessage) {
            return "REMOVE[" + ((RemoveDocumentMessage)m).getDocumentId() + "] ";
        }
        if (m instanceof UpdateDocumentMessage) {
            return "UPDATE[" + ((UpdateDocumentMessage)m).getDocumentUpdate().getId() + "] ";
        }

        return "";
    }

    public synchronized boolean handleReply(Reply reply) {
        metrics.addReply(reply);
        if (reply.getTrace().getLevel() > 0) {
            String str = reply.getTrace().toString();
            traces.append(str);
            System.out.println(str);
        }

        if (containsFatalErrors(reply.getErrors())) {
            for (int i = 0; i < reply.getNumErrors(); ++i) {
                Error err = reply.getError(i);
                StringBuilder out = new StringBuilder(prettyPrint(reply.getMessage()));
                out.append("[").append(DocumentProtocol.getErrorName(err.getCode())).append("] ");
                if (err.getService() != null) {
                    out.append("(").append(err.getService()).append(") ");
                }
                out.append(err.getMessage());

                String str = out.toString();
                log.finest(str);
                addError(convertErrorCode(err.getCode()), str);
            }
            if (abortOnError) {
                isAborted = true;
                return false;
            }
        }
        return true;
    }

    public SharedSender.Pending getPending() { return pendingNumber; }

    public void done() {
        metrics.done();
    }

    FeedResponse addXMLParseError(String error) {
        errorMessages.add(ErrorMessage.createBadRequest(error));
        errors.add(error);
        return this;
    }

    public FeedResponse addError(com.yahoo.container.protect.Error code, String error) {
        errorMessages.add(new ErrorMessage(code.code, error));
        errors.add(error);
        return this;
    }

    public List<String> getErrorList() {
        return errors;
    }

    private static boolean containsFatalErrors(Stream<Error> errors) {
        return errors.anyMatch(e -> e.getCode() != DocumentProtocol.ERROR_TEST_AND_SET_CONDITION_FAILED);
    }

    private static com.yahoo.container.protect.Error convertErrorCode(int error) {
        // We should try to enumerate these error a bit finer.
        // Like busy, no space etc.
        if (error == DocumentProtocol.ERROR_NO_SPACE) {
            return com.yahoo.container.protect.Error.INSUFFICIENT_STORAGE;
        } else if (isTransientError(error)) {
            return com.yahoo.container.protect.Error.INTERNAL_SERVER_ERROR;
        } if (isFatalError(error)) {
            return com.yahoo.container.protect.Error.INTERNAL_SERVER_ERROR;
        }
        return com.yahoo.container.protect.Error.INTERNAL_SERVER_ERROR;
    }

    private static boolean isFatalError(int error) {
        return (error >= ErrorCode.FATAL_ERROR) && (error < ErrorCode.ERROR_LIMIT);
    }

    private static boolean isTransientError(int error) {
        return (error >= ErrorCode.TRANSIENT_ERROR) && (error < ErrorCode.FATAL_ERROR);
    }

    public boolean isSuccess() {
        return errors.isEmpty();
    }

}