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
|
/*
*
* 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 "JoiningHandler.h"
#include "Cluster.h"
#include "qpid/framing/ClusterDumpRequestBody.h"
#include "qpid/framing/ClusterReadyBody.h"
#include "qpid/log/Statement.h"
namespace qpid {
namespace cluster {
using namespace sys;
using namespace framing;
JoiningHandler::JoiningHandler(Cluster& c) : ClusterHandler(c), state(START), catchUpConnections(0) {}
void JoiningHandler::configChange(
cpg_address *current, int nCurrent,
cpg_address */*left*/, int nLeft,
cpg_address */*joined*/, int /*nJoined*/)
{
if (nLeft == 0 && nCurrent == 1 && *current == cluster.self) { // First in cluster.
QPID_LOG(notice, cluster.self << " first in cluster.");
cluster.map.ready(cluster.self, cluster.url);
cluster.unstall();
}
}
void JoiningHandler::deliver(Event& e) {
// Discard connection events unless we are stalled to receive a dump.
if (state == STALLED)
cluster.connectionEventQueue.push(e);
else
QPID_LOG(trace, "Discarded pre-join event " << e);
}
void JoiningHandler::update(const MemberId&, const framing::FieldTable& members, uint64_t dumper) {
cluster.map.update(members, dumper);
QPID_LOG(debug, "Cluster update: " << cluster.map);
checkDumpRequest();
}
void JoiningHandler::checkDumpRequest() {
if (state == START && !cluster.map.dumper) {
cluster.broker.getPort(); // ensure the broker is listening.
state = DUMP_REQUESTED;
cluster.mcastControl(ClusterDumpRequestBody(framing::ProtocolVersion(), cluster.url.str()), 0);
}
}
void JoiningHandler::dumpRequest(const MemberId& dumpee, const std::string& ) {
if (cluster.map.dumper) { // Already a dump in progress.
if (dumpee == cluster.self && state == DUMP_REQUESTED)
state = START; // Need to make another request.
}
else { // Start a new dump
cluster.map.dumper = cluster.map.first();
QPID_LOG(debug, "Starting dump, dumper=" << cluster.map.dumper << " dumpee=" << dumpee);
if (dumpee == cluster.self) { // My turn
switch (state) {
case START:
case STALLED:
assert(0); break;
case DUMP_REQUESTED:
QPID_LOG(debug, cluster.self << " stalling for dump from " << cluster.map.dumper);
state = STALLED;
cluster.stall();
break;
case DUMP_COMPLETE:
QPID_LOG(debug, cluster.self << " at start point and dump complete, ready.");
cluster.ready();
break;
}
}
}
}
void JoiningHandler::ready(const MemberId& id, const std::string& url) {
cluster.map.ready(id, Url(url));
checkDumpRequest();
}
void JoiningHandler::insert(const boost::intrusive_ptr<Connection>& c) {
if (c->isCatchUp()) {
++catchUpConnections;
QPID_LOG(debug, "Catch-up connection " << *c << " started, total " << catchUpConnections);
}
cluster.connections.insert(Cluster::ConnectionMap::value_type(c->getId(), c));
}
void JoiningHandler::catchUpClosed(const boost::intrusive_ptr<Connection>& c) {
QPID_LOG(debug, "Catch-up complete for " << *c << ", remaining catch-ups: " << catchUpConnections-1);
if (c->isShadow())
cluster.connections.insert(Cluster::ConnectionMap::value_type(c->getId(), c));
if (--catchUpConnections == 0)
dumpComplete();
}
void JoiningHandler::dumpComplete() {
// FIXME aconway 2008-09-18: need to detect incomplete dump.
//
if (state == STALLED) {
QPID_LOG(debug, cluster.self << " received dump and stalled at start point, unstalling.");
cluster.ready();
}
else {
QPID_LOG(debug, cluster.self << " received dump, waiting for start point.");
assert(state == DUMP_REQUESTED);
state = DUMP_COMPLETE;
}
}
}} // namespace qpid::cluster
|