summaryrefslogtreecommitdiff
path: root/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/BDBHAMessageStoreTest.java
blob: 8e32a1d11379bd0d73d4966d1241352cdce643f1 (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
/*
 *
 * 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;

import java.io.File;
import java.net.InetAddress;

import org.apache.commons.configuration.XMLConfiguration;
import org.apache.qpid.server.configuration.VirtualHostConfiguration;
import org.apache.qpid.server.util.BrokerTestHelper;
import org.apache.qpid.server.virtualhost.VirtualHost;
import org.apache.qpid.test.utils.QpidTestCase;
import org.apache.qpid.util.FileUtils;

import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.rep.ReplicatedEnvironment;
import com.sleepycat.je.rep.ReplicationConfig;


public class BDBHAMessageStoreTest extends QpidTestCase
{
    private static final String TEST_LOG_FILE_MAX = "1000000";
    private static final String TEST_ELECTION_RETRIES = "1000";
    private static final String TEST_NUMBER_OF_THREADS = "10";
    private static final String TEST_ENV_CONSISTENCY_TIMEOUT = "9999999";
    private String _groupName;
    private String _workDir;
    private int _masterPort;
    private String _host;
    private XMLConfiguration _configXml;
    private VirtualHost _virtualHost;

    public void setUp() throws Exception
    {
        super.setUp();

        _workDir = TMP_FOLDER + File.separator + getName();
        _host = InetAddress.getByName("localhost").getHostAddress();
        _groupName = "group" + getName();
        _masterPort = -1;

        FileUtils.delete(new File(_workDir), true);
        _configXml = new XMLConfiguration();

        BrokerTestHelper.setUp();
     }

    public void tearDown() throws Exception
    {
        try
        {
            FileUtils.delete(new File(_workDir), true);
            if (_virtualHost != null)
            {
                _virtualHost.close();
            }
        }
        finally
        {
            BrokerTestHelper.tearDown();
            super.tearDown();
        }
    }

    public void testSetSystemConfiguration() throws Exception
    {
        // create virtual host configuration, registry and host instance
        addVirtualHostConfiguration();
        String vhostName = "test" + _masterPort;
        VirtualHostConfiguration configuration = new VirtualHostConfiguration(vhostName, _configXml.subset("virtualhosts.virtualhost." + vhostName), BrokerTestHelper.createBrokerMock());
        _virtualHost = BrokerTestHelper.createVirtualHost(configuration);
        BDBHAMessageStore store = (BDBHAMessageStore) _virtualHost.getMessageStore();

        // test whether JVM system settings were applied
        Environment env = store.getEnvironment();
        assertEquals("Unexpected number of cleaner threads", TEST_NUMBER_OF_THREADS, env.getConfig().getConfigParam(EnvironmentConfig.CLEANER_THREADS));
        assertEquals("Unexpected log file max", TEST_LOG_FILE_MAX, env.getConfig().getConfigParam(EnvironmentConfig.LOG_FILE_MAX));

        ReplicatedEnvironment repEnv = store.getReplicatedEnvironment();
        assertEquals("Unexpected number of elections primary retries", TEST_ELECTION_RETRIES,
                repEnv.getConfig().getConfigParam(ReplicationConfig.ELECTIONS_PRIMARY_RETRIES));
        assertEquals("Unexpected number of elections primary retries", TEST_ENV_CONSISTENCY_TIMEOUT,
                repEnv.getConfig().getConfigParam(ReplicationConfig.ENV_CONSISTENCY_TIMEOUT));
    }

    private void addVirtualHostConfiguration() throws Exception
    {
        int port = findFreePort();
        if (_masterPort == -1)
        {
            _masterPort = port;
        }
        String nodeName = getNodeNameForNodeAt(port);

        String vhostName = "test" + port;
        String vhostPrefix = "virtualhosts.virtualhost." + vhostName;

        _configXml.addProperty("virtualhosts.virtualhost.name", vhostName);
        _configXml.addProperty(vhostPrefix + ".store.class", BDBHAMessageStore.class.getName());
        _configXml.addProperty(vhostPrefix + ".store.environment-path", _workDir + File.separator
                + port);
        _configXml.addProperty(vhostPrefix + ".store.highAvailability.groupName", _groupName);
        _configXml.addProperty(vhostPrefix + ".store.highAvailability.nodeName", nodeName);
        _configXml.addProperty(vhostPrefix + ".store.highAvailability.nodeHostPort",
                getNodeHostPortForNodeAt(port));
        _configXml.addProperty(vhostPrefix + ".store.highAvailability.helperHostPort",
                getHelperHostPort());

        _configXml.addProperty(vhostPrefix + ".store.envConfig(-1).name", EnvironmentConfig.CLEANER_THREADS);
        _configXml.addProperty(vhostPrefix + ".store.envConfig.value", TEST_NUMBER_OF_THREADS);

        _configXml.addProperty(vhostPrefix + ".store.envConfig(-1).name", EnvironmentConfig.LOG_FILE_MAX);
        _configXml.addProperty(vhostPrefix + ".store.envConfig.value", TEST_LOG_FILE_MAX);

        _configXml.addProperty(vhostPrefix + ".store.repConfig(-1).name", ReplicationConfig.ELECTIONS_PRIMARY_RETRIES);
        _configXml.addProperty(vhostPrefix + ".store.repConfig.value", TEST_ELECTION_RETRIES);

        _configXml.addProperty(vhostPrefix + ".store.repConfig(-1).name", ReplicationConfig.ENV_CONSISTENCY_TIMEOUT);
        _configXml.addProperty(vhostPrefix + ".store.repConfig.value", TEST_ENV_CONSISTENCY_TIMEOUT);
    }

    private String getNodeNameForNodeAt(final int bdbPort)
    {
        return "node" + getName() + bdbPort;
    }

    private String getNodeHostPortForNodeAt(final int bdbPort)
    {
        return _host + ":" + bdbPort;
    }

    private String getHelperHostPort()
    {
        if (_masterPort == -1)
        {
            throw new IllegalStateException("Helper port not yet assigned.");
        }
        return _host + ":" + _masterPort;
    }
}