summaryrefslogtreecommitdiff
path: root/java/bdbstore/jmx/src/main/java/org/apache/qpid/server/store/berkeleydb/jmx/BDBHAMessageStoreManagerMBean.java
blob: cfcea602b448f9d3149c39bae66d3e3fb0f73bd8 (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
223
224
225
226
227
228
229
230
231
232
/*
 * 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.
 *
 */
package org.apache.qpid.server.store.berkeleydb.jmx;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.management.JMException;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenDataException;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
import javax.management.openmbean.TabularData;
import javax.management.openmbean.TabularDataSupport;
import javax.management.openmbean.TabularType;

import org.apache.log4j.Logger;
import org.apache.qpid.AMQStoreException;
import org.apache.qpid.server.jmx.AMQManagedObject;
import org.apache.qpid.server.jmx.ManagedObject;
import org.apache.qpid.server.store.berkeleydb.BDBHAMessageStore;

/**
 * Management mbean for BDB HA.
 * <p>
 * At runtime, the classloader loading this clas must have visibility of the other Qpid JMX classes. This is
 * currently arranged through OSGI using the <b>fragment</b> feature so that this bundle shares the
 * same classloader as broker-plugins-management-jmx.  See the <b>Fragment-Host:</b> header within the MANIFEST.MF
 * of this bundle.
 * </p>
 */
public class BDBHAMessageStoreManagerMBean extends AMQManagedObject implements ManagedBDBHAMessageStore
{
    private static final Logger LOGGER = Logger.getLogger(BDBHAMessageStoreManagerMBean.class);

    private static  final TabularType GROUP_MEMBERS_TABLE;
    private static final CompositeType GROUP_MEMBER_ROW;
    private static final OpenType<?>[] GROUP_MEMBER_ATTRIBUTE_TYPES;

    static
    {
        try
        {
            GROUP_MEMBER_ATTRIBUTE_TYPES = new OpenType<?>[] {SimpleType.STRING, SimpleType.STRING};
            final String[] itemNames = new String[] {BDBHAMessageStore.GRP_MEM_COL_NODE_NAME, BDBHAMessageStore.GRP_MEM_COL_NODE_HOST_PORT};
            final String[] itemDescriptions = new String[] {"Unique node name", "Node host / port "};
            GROUP_MEMBER_ROW = new CompositeType("GroupMember", "Replication group member",
                                                itemNames,
                                                itemDescriptions,
                                                GROUP_MEMBER_ATTRIBUTE_TYPES );
            GROUP_MEMBERS_TABLE = new TabularType("GroupMembers", "Replication group memebers",
                                                GROUP_MEMBER_ROW,
                                                new String[] {BDBHAMessageStore.GRP_MEM_COL_NODE_NAME});
        }
        catch (final OpenDataException ode)
        {
            throw new ExceptionInInitializerError(ode);
        }
    }

    private final BDBHAMessageStore _store;

    protected BDBHAMessageStoreManagerMBean(BDBHAMessageStore store, ManagedObject parent) throws JMException
    {
        super(ManagedBDBHAMessageStore.class, ManagedBDBHAMessageStore.TYPE, ((AMQManagedObject)parent).getRegistry());
        LOGGER.debug("Creating BDBHAMessageStoreManagerMBean");
        _store = store;
        register();
    }

    @Override
    public String getObjectInstanceName()
    {
        return _store.getName();
    }

    @Override
    public String getGroupName()
    {
        return _store.getGroupName();
    }

    @Override
    public String getNodeName()
    {
        return _store.getNodeName();
    }

    @Override
    public String getNodeHostPort()
    {
        return _store.getNodeHostPort();
    }

    @Override
    public String getHelperHostPort()
    {
        return _store.getHelperHostPort();
    }

    @Override
    public String getDurability() throws IOException, JMException
    {
        try
        {
            return _store.getDurability();
        }
        catch (RuntimeException e)
        {
            LOGGER.debug("Failed query replication policy", e);
            throw new JMException(e.getMessage());
        }
    }


    @Override
    public boolean getCoalescingSync() throws IOException, JMException
    {
        return _store.isCoalescingSync();
    }

    @Override
    public String getNodeState() throws IOException, JMException
    {
        try
        {
            return _store.getNodeState();
        }
        catch (RuntimeException e)
        {
            LOGGER.debug("Failed query node state", e);
            throw new JMException(e.getMessage());
        }
    }

    @Override
    public boolean getDesignatedPrimary() throws IOException, JMException
    {
        try
        {
            return _store.isDesignatedPrimary();
        }
        catch (RuntimeException e)
        {
            LOGGER.debug("Failed query designated primary", e);
            throw new JMException(e.getMessage());
        }
    }

    @Override
    public TabularData getAllNodesInGroup() throws IOException, JMException
    {
        final TabularDataSupport data = new TabularDataSupport(GROUP_MEMBERS_TABLE);
        final List<Map<String, String>> members = _store.getGroupMembers();

        for (Map<String, String> map : members)
        {
            CompositeData memberData = new CompositeDataSupport(GROUP_MEMBER_ROW, map);
            data.put(memberData);
        }
        return data;
    }

    @Override
    public void removeNodeFromGroup(String nodeName) throws JMException
    {
        try
        {
            _store.removeNodeFromGroup(nodeName);
        }
        catch (AMQStoreException e)
        {
            LOGGER.error("Failed to remove node " + nodeName + " from group", e);
            throw new JMException(e.getMessage());
        }
    }

    @Override
    public void setDesignatedPrimary(boolean primary) throws JMException
    {
        try
        {
            _store.setDesignatedPrimary(primary);
        }
        catch (AMQStoreException e)
        {
            LOGGER.error("Failed to set node " + _store.getNodeName() + " as designated primary", e);
            throw new JMException(e.getMessage());
        }
    }

    @Override
    public void updateAddress(String nodeName, String newHostName, int newPort) throws JMException
    {
        try
        {
            _store.updateAddress(nodeName, newHostName, newPort);
        }
        catch(AMQStoreException e)
        {
            LOGGER.error("Failed to update address for node " + nodeName + " to " + newHostName + ":" + newPort, e);
            throw new JMException(e.getMessage());
        }
    }

    @Override
    public ManagedObject getParentObject()
    {
        return null;
    }

}