summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/ha/HaBroker.h
blob: 023706e7e3f4c5d4c274c46ce89b7273dc5a3524 (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 "LogPrefix.h"
#include "Settings.h"
#include "qpid/Url.h"
#include "FailoverExchange.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 <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;
class Role;
class QueueReplicator;

/**
 * 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.

 * NOTE: HaBroker and Role subclasses follow this lock hierarchy:
 * - HaBroker MUST NOT hold its own lock across calls Role subclasses.
 * - Role subclasses MAY hold their locks accross calls to HaBroker.
 */
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::shared_ptr 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; }
    boost::shared_ptr<Role> getRole() const {return role; }

    /** Shut down the broker because of a critical error. */
    void shutdown(const std::string& message);

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

    BrokerInfo getBrokerInfo() const { return membership.getSelf(); }
    Membership& getMembership() { return membership; }
    types::Uuid getSystemId() const { return systemId; }

    void setAddress(const Address&); // set self address from a self-connection

    boost::shared_ptr<QueueReplicator> findQueueReplicator(const std::string& queueName);

    /** Authenticated user ID for queue create/delete */
    std::string getUserId() const { return userId; }

    /** logPrefix is thread safe and used by other classes (Membership) */
    LogPrefix logPrefix;

  private:
    class BrokerObserver;

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

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

    // Immutable members
    const types::Uuid systemId;
    const Settings settings;
    const std::string userId;

    // Member variables protected by lock
    mutable sys::Mutex lock;
    Url publicUrl, brokerUrl;
    std::vector<Url> knownBrokers;

    // Independently thread-safe member variables
    broker::Broker& broker;
    qmf::org::apache::qpid::ha::HaBroker::shared_ptr mgmtObject;
    boost::shared_ptr<ConnectionObserver> observer; // Used by Backup and Primary
    boost::shared_ptr<Role> role;
    Membership membership;
    boost::shared_ptr<FailoverExchange> failoverExchange;
};
}} // namespace qpid::ha

#endif  /*!QPID_HA_BROKER_H*/