summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/ha/Primary.cpp
blob: 0a577d08e4b471843342083944d021e98de4c747 (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
/*
 *
 * 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 "Backup.h"
#include "ConnectionExcluder.h"
#include "HaBroker.h"
#include "Primary.h"
#include "qpid/broker/Broker.h"
#include "qpid/broker/Queue.h"
#include "qpid/framing/FieldTable.h"
#include <boost/bind.hpp>

namespace qpid {
namespace ha {

Primary* Primary::instance = 0;

Primary::Primary(HaBroker& b) :
    haBroker(b), logPrefix(b),
    expected(b.getSettings().expectedBackups),
    unready(0), activated(false)
{
    instance = this;            // Let queue replicators find us.
    if (expected == 0)          // No need for ready check
        activate(*(sys::Mutex::ScopedLock*)0); // fake lock, ok in ctor.
    else {
        // Set up the primary-ready check: ready when all queues have
        // expected number of backups. Note clients are excluded at this point
        // so dont't have to worry about changes to the set of queues.
        HaBroker::QueueNames names = haBroker.getActiveBackups();
        for (HaBroker::QueueNames::const_iterator i = names.begin(); i != names.end(); ++i)
        {
            queues[*i] = 0;
            ++unready;
            QPID_LOG(debug, logPrefix << "Need backup of " << *i
                     << ", " << unready << " unready queues");
        }
        if (queues.empty())
            activate(*(sys::Mutex::ScopedLock*)0); // fake lock, ok in ctor.
        else {
            QPID_LOG(debug, logPrefix << "Waiting for  " << expected
                     << " backups on " << queues.size() << " queues");
            // Allow backups to connect.
            haBroker.getExcluder()->setBackupAllowed(true);
        }
    }
}

void Primary::readyReplica(const std::string& q) {
    sys::Mutex::ScopedLock l(lock);
    if (!activated) {
        QueueCounts::iterator i = queues.find(q);
        if (i != queues.end()) {
            ++i->second;
            if (i->second == expected) --unready;
            QPID_LOG(debug, logPrefix << i->second << " backups caught up on " << q
                     << ", " << unready << " unready queues");
            if (unready == 0) activate(l);
        }
    }
}

void Primary::activate(sys::Mutex::ScopedLock&) {
    activated = true;
    haBroker.activate();
}

}} // namespace qpid::ha