summaryrefslogtreecommitdiffstats
path: root/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/api/FeedClientImpl.java
blob: 7238a0c4ba7ee9512fd7cc14e8bad7867db8deb1 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.http.client.core.api;

import com.yahoo.vespa.http.client.FeedClient;
import com.yahoo.vespa.http.client.config.SessionParams;
import com.yahoo.vespa.http.client.core.Document;
import com.yahoo.vespa.http.client.core.ThrottlePolicy;
import com.yahoo.vespa.http.client.core.operationProcessor.IncompleteResultsThrottler;
import com.yahoo.vespa.http.client.core.operationProcessor.OperationProcessor;

import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Optional;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Implementation of FeedClient. It is a thin layer on top of multiClusterHandler and multiClusterResultAggregator.
 *
 * @author dybis
 */
public class FeedClientImpl implements FeedClient {

    private final OperationProcessor operationProcessor;
    private final long closeTimeoutMs;
    private final long sleepTimeMs = 500;

    public FeedClientImpl(SessionParams sessionParams,
                          ResultCallback resultCallback,
                          ScheduledThreadPoolExecutor timeoutExecutor) {
        this.closeTimeoutMs = (10 + 3 * sessionParams.getConnectionParams().getMaxRetries()) * (
                sessionParams.getFeedParams().getServerTimeout(TimeUnit.MILLISECONDS) +
                sessionParams.getFeedParams().getClientTimeout(TimeUnit.MILLISECONDS));
        this.operationProcessor = new OperationProcessor(
                new IncompleteResultsThrottler(
                        sessionParams.getThrottlerMinSize(),
                        sessionParams.getClientQueueSize(),
                        ()->System.currentTimeMillis(),
                        new ThrottlePolicy()),
                resultCallback,
                sessionParams,
                timeoutExecutor);
    }

    @Override
    public void stream(String documentId, String operationId, CharSequence documentData, Object context) {
        CharsetEncoder charsetEncoder = StandardCharsets.UTF_8.newEncoder();
        charsetEncoder.onMalformedInput(CodingErrorAction.REPORT);
        charsetEncoder.onUnmappableCharacter(CodingErrorAction.REPORT);

        Document document = new Document(documentId, operationId, documentData, context);
        operationProcessor.sendDocument(document);
    }

    @Override
    public void close() {
        Instant lastOldestResultReceivedAt = Instant.now();
        Optional<String> oldestIncompleteId = operationProcessor.oldestIncompleteResultId();

        while (oldestIncompleteId.isPresent() && waitForOperations(lastOldestResultReceivedAt, sleepTimeMs, closeTimeoutMs)) {
            Optional<String> oldestIncompleteIdNow = operationProcessor.oldestIncompleteResultId();
            if ( ! oldestIncompleteId.equals(oldestIncompleteIdNow))
                lastOldestResultReceivedAt = Instant.now();
            oldestIncompleteId = oldestIncompleteIdNow;
        }
        operationProcessor.close();
    }

    @Override
    public String getStatsAsJson() {
        return operationProcessor.getStatsAsJson();
    }

    // On return value true, wait more. Public for testing.
    public static boolean waitForOperations(Instant lastResultReceived, long sleepTimeMs, long closeTimeoutMs) {
        if (lastResultReceived.plusMillis(closeTimeoutMs).isBefore(Instant.now())) {
            return false;
        }
        try {
            Thread.sleep(sleepTimeMs);
        } catch (InterruptedException e) {
            return false;
        }
        return true;
    }

}