summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/cluster/OutputInterceptor.cpp
blob: cd42446016ac113f83aac1c9a166f8c5a73e90f9 (plain)
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
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
#include "OutputInterceptor.h"
#include "Connection.h"
#include "Cluster.h"
#include "qpid/framing/ClusterConnectionDeliverDoOutputBody.h"
#include "qpid/framing/AMQFrame.h"
#include "qpid/log/Statement.h"
#include <boost/current_function.hpp>


namespace qpid {
namespace cluster {

using namespace framing;

OutputInterceptor::OutputInterceptor(
    cluster::Connection& p, sys::ConnectionOutputHandler& h)
    : parent(p), closing(false), next(&h), sent(),
      writeEstimate(p.getCluster().getWriteEstimate()),
      moreOutput(), doingOutput()
{}

void OutputInterceptor::send(framing::AMQFrame& f) {
    parent.getCluster().checkQuorum();
    {
        sys::Mutex::ScopedLock l(lock);
        next->send(f);
    }
    if (!parent.isCatchUp())
        sent += f.encodedSize();
    QPID_LATENCY_RECORD("up to write queue", f);
}

void OutputInterceptor::activateOutput() {
    if (parent.isCatchUp()) {
        sys::Mutex::ScopedLock l(lock);
        next->activateOutput();
    }
    else if (!closing) {        // Don't send do ouput after output stopped.
        QPID_LOG(trace,  parent << " activateOutput - sending doOutput");
        moreOutput = true;
        sendDoOutput();
    }
}

void OutputInterceptor::giveReadCredit(int32_t credit) {
    sys::Mutex::ScopedLock l(lock);
    next->giveReadCredit(credit);
}

// Called in write thread when the IO layer has no more data to write.
// We do nothing in the write thread, we run doOutput only on delivery
// of doOutput requests.
bool  OutputInterceptor::doOutput() { return false; }

// Delivery of doOutput allows us to run the real connection doOutput()
// which tranfers frames to the codec for writing.
// 
void OutputInterceptor::deliverDoOutput(size_t requested) {
    size_t buf = getBuffered();
    if (parent.isLocal())
        writeEstimate.delivered(requested, sent, buf); // Update the estimate.

    // Run the real doOutput() till we have added the requested data or there's nothing to output.
    sent = 0;
    do {
        moreOutput = parent.getBrokerConnection().doOutput();
    } while (sent < requested && moreOutput);
    sent += buf;                // Include buffered data in the sent total.
    QPID_LOG(trace, parent << " delivereDoOutput: requested=" << requested << " sent=" << sent << " more=" << moreOutput);
    if (parent.isLocal() && moreOutput)  {
        QPID_LOG(trace,  parent << " deliverDoOutput - sending doOutput, more output available.");
        sendDoOutput();
    }
    else
        doingOutput = false;
}

// Send a doOutput request if one is not already in flight.
void OutputInterceptor::sendDoOutput() {
    if (!parent.isLocal()) return;
    QPID_LATENCY_INIT(*this);
    doingOutput = true;
    size_t request = writeEstimate.sending(getBuffered());
    
    // Note we may send 0 size request if there's more than 2*estimate in the buffer.
    // Send it anyway to keep the doOutput chain going until we are sure there's no more output
    // (in deliverDoOutput)
    //
    parent.getCluster().getMulticast().mcastControl(
        ClusterConnectionDeliverDoOutputBody(ProtocolVersion(), request), parent.getId());
    QPID_LOG(trace, parent << "Send doOutput request for " << request);
}

void OutputInterceptor::closeOutput(sys::ConnectionOutputHandler& h) {
    sys::Mutex::ScopedLock l(lock);
    closing = true;
    next = &h;
}

void OutputInterceptor::close() {
    sys::Mutex::ScopedLock l(lock);
    next->close();
}

size_t OutputInterceptor::getBuffered() const {
    sys::Mutex::ScopedLock l(lock);
    return next->getBuffered();
}

}} // namespace qpid::cluster