summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/broker/IncomingExecutionContext.cpp
blob: 6c6cae67401d28ca22f099d8f1f888dc7ce69869 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 *
 * 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 "IncomingExecutionContext.h"
#include "qpid/Exception.h"

namespace qpid {
namespace broker {

using boost::intrusive_ptr;
using qpid::framing::AccumulatedAck;
using qpid::framing::SequenceNumber;
using qpid::framing::SequenceNumberSet;

void IncomingExecutionContext::noop()
{
    complete(next());
}

void IncomingExecutionContext::flush()
{
    for (Messages::iterator i = incomplete.begin(); i != incomplete.end(); ) {
        if ((*i)->isEnqueueComplete()) {
            complete((*i)->getCommandId());
            i = incomplete.erase(i);
        } else {
            i++;
        }
    }
    window.lwm = completed.mark;
}

void IncomingExecutionContext::sync()
{
    while (completed.mark < window.hwm) {
        wait();
    }    
}

void IncomingExecutionContext::sync(const SequenceNumber& point)
{
    while (!isComplete(point)) {
        wait();
    }    
}

/**
 * Every call to next() should be followed be either a call to
 * complete() - in the case of commands, which are always synchronous
 * - or track() - in the case of messages which may be asynchronously
 * stored.
 */
SequenceNumber IncomingExecutionContext::next()
{
    return ++window.hwm;
}

void IncomingExecutionContext::complete(const SequenceNumber& command)
{
    completed.update(command, command);
}

void IncomingExecutionContext::track(intrusive_ptr<Message> msg)
{
    if (msg->isEnqueueComplete()) {
        complete(msg->getCommandId());
    } else {
        incomplete.push_back(msg);
    }
}

bool IncomingExecutionContext::isComplete(const SequenceNumber& command)
{
    if (command > window.hwm) {
        throw Exception(QPID_MSG("Bad sync request: point exceeds last command received [" 
                                 << command.getValue() << " > " << window.hwm.getValue() << "]"));
    }

    return completed.covers(command);
}


const SequenceNumber& IncomingExecutionContext::getMark()
{
    return completed.mark;
}

SequenceNumberSet IncomingExecutionContext::getRange()
{
    SequenceNumberSet range;
    completed.collectRanges(range);
    return range;
}

void IncomingExecutionContext::wait()
{
    check();
	// for IO flush on the store
    for (Messages::iterator i = incomplete.begin(); i != incomplete.end(); i++) {
        (*i)->flush();
    }
    incomplete.front()->waitForEnqueueComplete();
    flush();
}

/**
 * This is a check of internal state consistency.
 */
void IncomingExecutionContext::check()
{
    if (incomplete.empty()) {
        if (window.hwm != completed.mark) {
            //can only happen if there is a call to next() without a
            //corresponding call to completed() or track() - or if
            //there is a logical error in flush() or
            //AccumulatedAck::update()
            throw Exception(QPID_MSG("Completion tracking error: window.hwm=" 
                                     << window.hwm.getValue() << ", completed.mark="
                                     << completed.mark.getValue()));
        }
    }
}

}}