summaryrefslogtreecommitdiffstats
path: root/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/operationProcessor/DocumentSendInfo.java
blob: 8b6c46fb1cdd49c7d7434eb630343c430d602d65 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.http.client.core.operationProcessor;

import com.yahoo.vespa.http.client.Result;
import com.yahoo.vespa.http.client.core.Document;

import java.util.HashMap;
import java.util.Map;

/**
 * Keeps an overview of what is sent and what is received for an operation.
 * This class is NOT thread-safe by design.
 */
class DocumentSendInfo {
    private final Document document;
    private final Map<Integer, Result.Detail> detailByClusterId = new HashMap<>();
    // This is lazily populated as normal cases does not require retries.
    private Map<Integer, Integer> attemptedRetriesByClusterId = null;
    private final StringBuilder localTrace;

    DocumentSendInfo(Document document, boolean traceThisDoc) {
        this.document = document;
        localTrace = traceThisDoc
                ? new StringBuilder("\n" + document.createTimeMillis() + " Trace starting " + "\n")
                : null;
    }

    boolean addIfNotAlreadyThere(Result.Detail detail, int clusterId) {
        if (detailByClusterId.containsKey(clusterId)) {
            if (localTrace != null) {
                localTrace.append(System.currentTimeMillis() + " Got duplicate detail, ignoring this: "
                        + detail.toString() + "\n");
            }
            return false;
        }
        if (localTrace != null) {
            localTrace.append(System.currentTimeMillis() + " Got detail: " + detail.toString() + "\n");
        }
        detailByClusterId.put(clusterId, detail);
        return true;
    }

    int detailCount() {
        return detailByClusterId.size();
    }

    public Result createResult() {
        return new Result(document, detailByClusterId.values(), localTrace);
    }

    int incRetries(int clusterId, Result.Detail detail) {
        if (attemptedRetriesByClusterId == null) {
            attemptedRetriesByClusterId = new HashMap<>();
        }
        int retries = 0;
        if (attemptedRetriesByClusterId.containsKey(clusterId)) {
            retries = attemptedRetriesByClusterId.get(clusterId);
        }
        retries++;
        attemptedRetriesByClusterId.put(clusterId, retries);
        if (localTrace != null) {
            localTrace.append(System.currentTimeMillis() + " Asked about retrying for cluster ID "
                    + clusterId + ", number of retries is " + retries + " Detail:\n" + detail.toString());
        }
        return retries;
    }

    Document getDocument() {
        return document;
    }
}