summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/ha/Membership.cpp
blob: b53291ba8950e79bbe7425ec6bdaa2a9071cf8f4 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 *
 * 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 "ConnectionObserver.h"
#include "HaBroker.h"
#include "Membership.h"
#include "qpid/broker/Broker.h"
#include "qpid/framing/FieldTable.h"
#include "qpid/management/ManagementAgent.h"
#include "qpid/types/Variant.h"
#include "qmf/org/apache/qpid/ha/EventMembersUpdate.h"
#include "qmf/org/apache/qpid/ha/HaBroker.h"
#include <boost/bind.hpp>
#include <iostream>
#include <iterator>

namespace qpid {
namespace ha {

namespace _qmf = ::qmf::org::apache::qpid::ha;

using sys::Mutex;
using types::Variant;

Membership::Membership(const BrokerInfo& info, HaBroker& b)
    : haBroker(b), self(info.getSystemId())
{
    brokers[self] = info;
    oldStatus = info.getStatus();
}

void Membership::clear() {
    Mutex::ScopedLock l(lock);
    BrokerInfo me = brokers[self];
    brokers.clear();
    brokers[self] = me;
}

void Membership::add(const BrokerInfo& b) {
    Mutex::ScopedLock l(lock);
    assert(b.getSystemId() != self);
    brokers[b.getSystemId()] = b;
    update(l);
}


void Membership::remove(const types::Uuid& id) {
    Mutex::ScopedLock l(lock);
    if (id == self) return;     // Never remove myself
    BrokerInfo::Map::iterator i = brokers.find(id);
    if (i != brokers.end()) {
        brokers.erase(i);
        update(l);
    }
}

bool Membership::contains(const types::Uuid& id) {
    Mutex::ScopedLock l(lock);
    return brokers.find(id) != brokers.end();
}

void Membership::assign(const types::Variant::List& list) {
    Mutex::ScopedLock l(lock);
    clear();
    for (types::Variant::List::const_iterator i = list.begin(); i != list.end(); ++i) {
        BrokerInfo b(i->asMap());
        brokers[b.getSystemId()] = b;
    }
    update(l);
}

types::Variant::List Membership::asList() const {
    Mutex::ScopedLock l(lock);
    return asList(l);
}

types::Variant::List Membership::asList(sys::Mutex::ScopedLock&) const {
    types::Variant::List list;
    for (BrokerInfo::Map::const_iterator i = brokers.begin(); i != brokers.end(); ++i)
        list.push_back(i->second.asMap());
    return list;
}

BrokerInfo::Set Membership::otherBackups() const {
    Mutex::ScopedLock l(lock);
    BrokerInfo::Set result;
    for (BrokerInfo::Map::const_iterator i = brokers.begin(); i != brokers.end(); ++i)
        if (i->second.getStatus() == READY && i->second.getSystemId() != self)
            result.insert(i->second);
    return result;
}

BrokerInfo::Set Membership::getBrokers() const {
    Mutex::ScopedLock l(lock);
    BrokerInfo::Set result;
    transform(brokers.begin(), brokers.end(), inserter(result, result.begin()),
              bind(&BrokerInfo::Map::value_type::second, _1));
    return result;
}

bool Membership::get(const types::Uuid& id, BrokerInfo& result) const {
    Mutex::ScopedLock l(lock);
    BrokerInfo::Map::const_iterator i = brokers.find(id);
    if (i == brokers.end()) return false;
    result = i->second;
    return true;
}

namespace {
bool checkTransition(BrokerStatus from, BrokerStatus to) {
    // Legal state transitions. Initial state is JOINING, ACTIVE is terminal.
    static const BrokerStatus TRANSITIONS[][2] = {
        { STANDALONE, JOINING }, // Initialization of backup broker
        { JOINING, CATCHUP },    // Connected to primary
        { JOINING, RECOVERING }, // Chosen as initial primary.
        { CATCHUP, READY },      // Caught up all queues, ready to take over.
        { READY, RECOVERING },   // Chosen as new primary
        { READY, CATCHUP },      // Timed out failing over, demoted to catch-up.
        { RECOVERING, ACTIVE }   // All expected backups are ready
    };
    static const size_t N = sizeof(TRANSITIONS)/sizeof(TRANSITIONS[0]);
    for (size_t i = 0; i < N; ++i) {
        if (TRANSITIONS[i][0] == from && TRANSITIONS[i][1] == to)
            return true;
    }
    return false;
}
} // namespace


void Membership::update(Mutex::ScopedLock& l) {
    QPID_LOG(info, "Membership: " <<  brokers);
    // Update managment and send update event.
    BrokerStatus newStatus = getStatus(l);
    Variant::List brokerList = asList(l);
    if (mgmtObject) {
        mgmtObject->set_status(printable(newStatus).str());
        mgmtObject->set_members(brokerList);
    }
    haBroker.getBroker().getManagementAgent()->raiseEvent(
        _qmf::EventMembersUpdate(brokerList));

    // Update link client properties
    framing::FieldTable linkProperties = haBroker.getBroker().getLinkClientProperties();
    if (isBackup(newStatus)) {
        // Set backup tag on outgoing link properties.
        linkProperties.setTable(
            ConnectionObserver::BACKUP_TAG, brokers[types::Uuid(self)].asFieldTable());
        haBroker.getBroker().setLinkClientProperties(linkProperties);
    } else {
        // Remove backup tag property from outgoing link properties.
        linkProperties.erase(ConnectionObserver::BACKUP_TAG);
        haBroker.getBroker().setLinkClientProperties(linkProperties);
    }

    // Check status transitions
    if (oldStatus != newStatus) {
        QPID_LOG(info, "Status change: "
                 << printable(oldStatus) << " -> " << printable(newStatus));
        if (!checkTransition(oldStatus, newStatus)) {
            haBroker.shutdown(QPID_MSG("Illegal state transition: " << printable(oldStatus)
                                       << " -> " << printable(newStatus)));
        }
        oldStatus = newStatus;
    }
}

void Membership::setMgmtObject(boost::shared_ptr<_qmf::HaBroker> mo) {
    Mutex::ScopedLock l(lock);
    mgmtObject = mo;
    update(l);
}


void Membership::setStatus(BrokerStatus newStatus) {
    Mutex::ScopedLock l(lock);
    brokers[self].setStatus(newStatus);
    update(l);
}

BrokerStatus Membership::getStatus() const  {
    Mutex::ScopedLock l(lock);
    return getStatus(l);
}

BrokerStatus Membership::getStatus(sys::Mutex::ScopedLock&) const  {
    BrokerInfo::Map::const_iterator i = brokers.find(self);
    assert(i != brokers.end());
    return i->second.getStatus();
}

BrokerInfo Membership::getInfo() const  {
    Mutex::ScopedLock l(lock);
    BrokerInfo::Map::const_iterator i = brokers.find(self);
    assert(i != brokers.end());
    return i->second;
}

void Membership::setAddress(const Address& a) {
    Mutex::ScopedLock l(lock);
    brokers[self].setAddress(a);
    update(l);
}

}} // namespace qpid::ha