summaryrefslogtreecommitdiff
path: root/src/mongo/dbtests/config_upgrade_tests.cpp
blob: 88db3071a1a27c186f634257dcaf4b1f29c34d6a (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
 *    Copyright (C) 2012 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the GNU Affero General Public License in all respects
 *    for all of the code used other than as permitted herein. If you modify
 *    file(s) with this exception, you may extend this exception to your
 *    version of the file(s), but you are not obligated to do so. If you do not
 *    wish to do so, delete this exception statement from your version. If you
 *    delete this exception statement from all source files in the program,
 *    then also delete it in the license file.
 */

#include "mongo/dbtests/config_server_fixture.h"
#include "mongo/s/catalog/config_server_version.h"
#include "mongo/s/catalog/legacy/cluster_client_internal.h"
#include "mongo/s/catalog/legacy/config_upgrade.h"
#include "mongo/s/catalog/type_chunk.h"
#include "mongo/s/catalog/type_config_version.h"
#include "mongo/s/catalog/type_mongos.h"
#include "mongo/s/catalog/type_settings.h"
#include "mongo/s/catalog/type_shard.h"
#include "mongo/s/chunk_version.h"
#include "mongo/s/grid.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/version.h"

namespace mongo {

using std::string;

namespace {

/**
 * Specialization of the config server fixture with helpers for the tests below.
 */
class ConfigUpgradeFixture : public ConfigServerFixture {
public:
    void stopBalancer() {
        // Note: The balancer key is needed in the update portion, for some reason related to
        // DBDirectClient
        DBDirectClient client(&_txn);
        client.update(SettingsType::ConfigNS,
                      BSON(SettingsType::key(SettingsType::BalancerDocKey)),
                      BSON(SettingsType::key(SettingsType::BalancerDocKey)
                           << SettingsType::balancerStopped(true)),
                      true,
                      false);
    }

    /**
     * Stores a legacy { version : X } config server entry
     */
    void storeLegacyConfigVersion(int version) {
        if (version == 0)
            return;

        DBDirectClient client(&_txn);

        if (version == 1) {
            ShardType shard;
            shard.setName("test");
            shard.setHost("$dummy:10000");
            client.insert(ShardType::ConfigNS, shard.toBSON());
            return;
        }

        client.insert(VersionType::ConfigNS, BSON("_id" << 1 << "version" << version));
    }

    VersionType loadLegacyConfigVersion() {
        DBDirectClient client(&_txn);
        return unittest::assertGet(
            VersionType::fromBSON(client.findOne(VersionType::ConfigNS, BSONObj())));
    }

    /**
     * Stores a newer { version, minVersion, currentVersion, clusterId } config server entry
     */
    void storeConfigVersion(const VersionType& versionInfo) {
        DBDirectClient client(&_txn);
        client.insert(VersionType::ConfigNS, versionInfo.toBSON());
    }

    /**
     * Stores a newer { version, minVersion, currentVersion, clusterId } config server entry.
     *
     * @return clusterId
     */
    OID storeConfigVersion(int configVersion) {
        if (configVersion < CURRENT_CONFIG_VERSION) {
            storeLegacyConfigVersion(configVersion);
            return OID();
        }

        VersionType version;
        version.setMinCompatibleVersion(configVersion);
        version.setCurrentVersion(configVersion);

        OID clusterId = OID::gen();

        version.setClusterId(clusterId);

        storeConfigVersion(version);
        return clusterId;
    }

    /**
     * Stores sample shard and ping information at the current version.
     */
    void storeShardsAndPings(int numShards, int numPings) {
        DBDirectClient client(&_txn);

        for (int i = 0; i < numShards; i++) {
            ShardType shard;
            shard.setName(OID::gen().toString());
            shard.setHost((string)(str::stream() << "$dummyShard:" << (i + 1) << "0000"));

            client.insert(ShardType::ConfigNS, shard.toBSON());
        }

        time_t started = time(0);
        for (int i = 0; i < numPings; i++) {
            MongosType ping;
            ping.setName((string)(str::stream() << "$dummyMongos:" << (i + 1) << "0000"));
            ping.setPing(jsTime());
            ping.setUptime(time(0) - started);
            ping.setWaiting(false);
            ping.setMongoVersion(versionString);
            ping.setConfigVersion(CURRENT_CONFIG_VERSION);

            if (i % 2 == 0) {
                ping.setPing(ping.getPing() - Minutes(10));
            }

            client.insert(MongosType::ConfigNS, ping.toBSON());
        }
    }
};

//
// Tests for upgrading the config server between versions.
//
// In general these tests do pretty minimal validation of the config server data itself, but
// do ensure that the upgrade mechanism is working correctly w.r.t the config.version
// collection.
//

// Rename the fixture so that our tests have a useful name in the executable
typedef ConfigUpgradeFixture ConfigUpgradeTests;

TEST_F(ConfigUpgradeTests, EmptyVersion) {
    //
    // Tests detection of empty config version
    //

    // Zero version (no version doc)
    VersionType oldVersion;
    Status status = getConfigVersion(grid.catalogManager(&_txn), &oldVersion);
    ASSERT(status.isOK());

    ASSERT_EQUALS(oldVersion.getMinCompatibleVersion(), 0);
    ASSERT_EQUALS(oldVersion.getCurrentVersion(), 0);
}

TEST_F(ConfigUpgradeTests, ClusterIDVersion) {
    //
    // Tests detection of newer config versions
    //

    VersionType newVersion;
    newVersion.setMinCompatibleVersion(MIN_COMPATIBLE_CONFIG_VERSION);
    newVersion.setCurrentVersion(CURRENT_CONFIG_VERSION);
    storeConfigVersion(newVersion);

    newVersion.clear();

    // Current Version w/o clusterId (invalid!)
    Status status = getConfigVersion(grid.catalogManager(&_txn), &newVersion);
    ASSERT(!status.isOK());

    newVersion.clear();

    OID clusterId = OID::gen();
    newVersion.setClusterId(clusterId);
    newVersion.setMinCompatibleVersion(MIN_COMPATIBLE_CONFIG_VERSION);
    newVersion.setCurrentVersion(CURRENT_CONFIG_VERSION);

    clearVersion();
    storeConfigVersion(newVersion);

    newVersion.clear();

    // Current version w/ clusterId (valid!)
    status = getConfigVersion(grid.catalogManager(&_txn), &newVersion);
    ASSERT(status.isOK());

    ASSERT_EQUALS(newVersion.getMinCompatibleVersion(), MIN_COMPATIBLE_CONFIG_VERSION);
    ASSERT_EQUALS(newVersion.getCurrentVersion(), CURRENT_CONFIG_VERSION);
    ASSERT_EQUALS(newVersion.getClusterId(), clusterId);
}

TEST_F(ConfigUpgradeTests, InitialUpgrade) {
    //
    // Tests initializing the config server to the initial version
    //

    string errMsg;
    ASSERT_OK(grid.catalogManager(&_txn)->initConfigVersion(&_txn));

    VersionType version;
    ASSERT_OK(getConfigVersion(grid.catalogManager(&_txn), &version));

    ASSERT_EQUALS(MIN_COMPATIBLE_CONFIG_VERSION, version.getMinCompatibleVersion());
    ASSERT_EQUALS(CURRENT_CONFIG_VERSION, version.getCurrentVersion());
    ASSERT_TRUE(version.getClusterId().isSet());
}

TEST_F(ConfigUpgradeTests, BadVersionUpgrade) {
    //
    // Tests that we can't upgrade from a config version we don't have an upgrade path for
    //

    stopBalancer();

    storeLegacyConfigVersion(1);

    // Default version (not upgradeable)
    ASSERT_EQ(ErrorCodes::IncompatibleShardingMetadata,
              grid.catalogManager(&_txn)->initConfigVersion(&_txn));
}

}  // namespace
}  // namespace mongo