summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/ha/HaBroker.h
blob: 7dabe6e35bfab8076a61e8e770a6a49c6a20e4de (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
#ifndef QPID_HA_BROKER_H
#define QPID_HA_BROKER_H

/*
 *
 * 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 "BrokerInfo.h"
#include "Membership.h"
#include "types.h"
#include "ReplicationTest.h"
#include "Settings.h"
#include "qpid/Url.h"
#include "qpid/sys/Mutex.h"
#include "qmf/org/apache/qpid/ha/HaBroker.h"
#include "qpid/management/Manageable.h"
#include "qpid/types/Variant.h"
#include <memory>
#include <set>
#include <boost/shared_ptr.hpp>

namespace qpid {

namespace types {
class Variant;
}

namespace broker {
class Broker;
class Queue;
}
namespace framing {
class FieldTable;
}

namespace ha {
class Backup;
class ConnectionObserver;
class Primary;

/**
 * HA state and actions associated with a HA broker. Holds all the management info.
 *
 * THREAD SAFE: may be called in arbitrary broker IO or timer threads.
 */
class HaBroker : public management::Manageable
{
  public:
    /** HaBroker is constructed during earlyInitialize */
    HaBroker(broker::Broker&, const Settings&);
    ~HaBroker();

    /** Called during plugin initialization */
    void initialize();

    // Implement Manageable.
    qpid::management::ManagementObject* GetManagementObject() const { return mgmtObject; }
    management::Manageable::status_t ManagementMethod (
        uint32_t methodId, management::Args& args, std::string& text);

    broker::Broker& getBroker() { return broker; }
    const Settings& getSettings() const { return settings; }

    /** Shut down the broker. Caller should log a critical error message. */
    void shutdown();

    BrokerStatus getStatus() const;
    void setStatus(BrokerStatus);
    void activate();

    Backup* getBackup() { return backup.get(); }
    ReplicationTest getReplicationTest() const { return replicationTest; }

    boost::shared_ptr<ConnectionObserver> getObserver() { return observer; }

    const BrokerInfo& getBrokerInfo() const { return brokerInfo; }

    void setMembership(const types::Variant::List&); // Set membership from list.
    void resetMembership(const BrokerInfo& b); // Reset to contain just one member.
    void addBroker(const BrokerInfo& b);       // Add a broker to the membership.
    void removeBroker(const types::Uuid& id);  // Remove a broker from membership.

    types::Uuid getSystemId() const { return systemId; }

  private:
    void setClientUrl(const Url&);
    void setBrokerUrl(const Url&);
    void updateClientUrl(sys::Mutex::ScopedLock&);

    bool isPrimary(sys::Mutex::ScopedLock&) { return !backup.get(); }

    void setStatus(BrokerStatus, sys::Mutex::ScopedLock&);
    void recover();
    void statusChanged(sys::Mutex::ScopedLock&);
    void setLinkProperties(sys::Mutex::ScopedLock&);

    std::vector<Url> getKnownBrokers() const;

    void membershipUpdated(sys::Mutex::ScopedLock&);

    std::string logPrefix;
    broker::Broker& broker;
    types::Uuid systemId;
    const Settings settings;

    mutable sys::Mutex lock;
    boost::shared_ptr<ConnectionObserver> observer; // Used by Backup and Primary
    std::auto_ptr<Backup> backup;
    std::auto_ptr<Primary> primary;
    qmf::org::apache::qpid::ha::HaBroker* mgmtObject;
    Url clientUrl, brokerUrl;
    std::vector<Url> knownBrokers;
    BrokerStatus status;
    BrokerInfo brokerInfo;
    Membership membership;
    ReplicationTest replicationTest;
};
}} // namespace qpid::ha

#endif  /*!QPID_HA_BROKER_H*/