aboutsummaryrefslogtreecommitdiffstats
path: root/docproc/src/main/java/com/yahoo/docproc/jdisc/messagebus/ResponseMerger.java
blob: 4cb8fa1e92f72abba56f799e8eaa99f51d57c73e (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.docproc.jdisc.messagebus;

import com.yahoo.documentapi.messagebus.protocol.DocumentProtocol;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.ResponseDispatch;
import com.yahoo.jdisc.handler.ResponseHandler;
import com.yahoo.messagebus.Message;
import com.yahoo.messagebus.Reply;
import com.yahoo.messagebus.TraceNode;
import com.yahoo.messagebus.jdisc.MbusResponse;
import com.yahoo.messagebus.jdisc.StatusCodes;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Einar M R Rosenvinge
 */
class ResponseMerger implements ResponseHandler {

    private final Message requestMsg;
    private final TraceNode requestTrace = new TraceNode().setStrict(false);
    private final ResponseHandler responseHandler;
    private final List<Reply> replies;
    private int numPending;

    public ResponseMerger(Message requestMsg, int numPending, ResponseHandler responseHandler) {
        this.requestMsg = requestMsg;
        this.responseHandler = responseHandler;
        this.replies = new ArrayList<>(numPending);
        this.numPending = numPending;
    }

    @Override
    public ContentChannel handleResponse(Response response) {
        synchronized (this) {
            if (response instanceof MbusResponse) {
                Reply reply = ((MbusResponse)response).getReply();
                requestTrace.addChild(reply.getTrace().getRoot());
                replies.add(reply);
            }
            if (--numPending != 0) {
                return null;
            }
        }
        requestMsg.getTrace().getRoot().addChild(requestTrace);
        Reply reply = DocumentProtocol.merge(replies);
        Response mbusResponse = new MbusResponse(StatusCodes.fromMbusReply(reply), reply);
        ResponseDispatch.newInstance(mbusResponse).dispatch(responseHandler);
        return null;
    }

}