summaryrefslogtreecommitdiff
path: root/src/mongo/db/storage/storage_engine_metadata.cpp
blob: 6e9eea67c7289e21a35e6ba8f9d4d994ca64ab24 (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
 *    Copyright (C) 2014 MongoDB 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.
 */

#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kStorage

#include "mongo/platform/basic.h"

#include "mongo/db/storage/storage_engine_metadata.h"

#include <cstdio>
#include <boost/filesystem.hpp>
#include <fstream>
#include <limits>
#include <ostream>
#include <vector>

#include "mongo/db/jsobj.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/log.h"
#include "mongo/util/mongoutils/str.h"

namespace mongo {

namespace {

    const std::string kMetadataBasename = "storage.bson";

    /**
     * Returns true if local.ns is found in 'directory' or 'directory'/local/.
     */
    bool containsMMapV1LocalNsFile(const std::string& directory) {
        boost::filesystem::path directoryPath(directory);
        return boost::filesystem::exists(directoryPath / "local.ns") ||
            boost::filesystem::exists((directoryPath / "local") / "local.ns");
    }

}  // namespace

    // static
    std::auto_ptr<StorageEngineMetadata> StorageEngineMetadata::validate(
        const std::string& dbpath,
        const std::string& storageEngine) {

        std::auto_ptr<StorageEngineMetadata> metadata;
        std::string previousStorageEngine;
        if (boost::filesystem::exists(boost::filesystem::path(dbpath) / kMetadataBasename)) {
            metadata.reset(new StorageEngineMetadata(dbpath));
            Status status = metadata->read();
            if (status.isOK()) {
                previousStorageEngine = metadata->getStorageEngine();
            }
            else {
                // The storage metadata file is present but there was an issue
                // reading its contents.
                warning() << "Unable to read the existing the storage engine metadata: "
                          << status.toString();
                return std::auto_ptr<StorageEngineMetadata>();
            }
        }
        else if (containsMMapV1LocalNsFile(dbpath)) {
            previousStorageEngine = "mmapv1";
        }
        else {
            // Directory contains neither metadata nor mmapv1 files.
            // Allow validation to succeed.
            return metadata;
        }

        uassert(28574, str::stream()
            << "Cannot start server. Detected data files in " << dbpath
            << " created by storage engine '" << previousStorageEngine
            << "'. The configured storage engine is '" << storageEngine << "'.",
            previousStorageEngine == storageEngine);

        return metadata;
    }

    StorageEngineMetadata::StorageEngineMetadata(const std::string& dbpath)
        : _dbpath(dbpath) {
        reset();
    }

    StorageEngineMetadata::~StorageEngineMetadata() { }

    void StorageEngineMetadata::reset() {
        _storageEngine.clear();
        _storageEngineOptions = BSONObj();
    }

    const std::string& StorageEngineMetadata::getStorageEngine() const {
        return _storageEngine;
    }

    const BSONObj& StorageEngineMetadata::getStorageEngineOptions() const {
        return _storageEngineOptions;
    }

    void StorageEngineMetadata::setStorageEngine(const std::string& storageEngine) {
        _storageEngine = storageEngine;
    }

    void StorageEngineMetadata::setStorageEngineOptions(const BSONObj& storageEngineOptions) {
        _storageEngineOptions = storageEngineOptions.getOwned();
    }

    Status StorageEngineMetadata::read() {
        reset();

        boost::filesystem::path metadataPath =
            boost::filesystem::path(_dbpath) / kMetadataBasename;

        if (!boost::filesystem::exists(metadataPath)) {
            return Status(ErrorCodes::NonExistentPath, str::stream()
                << "Metadata file " << metadataPath.string() << " not found.");
        }

        boost::uintmax_t fileSize = boost::filesystem::file_size(metadataPath);
        if (fileSize == 0) {
            return Status(ErrorCodes::InvalidPath, str::stream()
                << "Metadata file " << metadataPath.string() << " cannot be empty.");
        }
        if (fileSize == static_cast<boost::uintmax_t>(-1)) {
            return Status(ErrorCodes::InvalidPath, str::stream()
                << "Unable to determine size of metadata file " << metadataPath.string());
        }

        std::vector<char> buffer(fileSize);
        std::string filename = metadataPath.string();
        try {
            std::ifstream ifs(filename.c_str(), std::ios_base::in | std::ios_base::binary);
            if (!ifs) {
                return Status(ErrorCodes::FileNotOpen, str::stream()
                    << "Failed to read metadata from " << filename);
        }

        // Read BSON from file
        ifs.read(&buffer[0], buffer.size());
        if (!ifs) {
            return Status(ErrorCodes::FileStreamFailed, str::stream()
                << "Unable to read BSON data from " << filename);
        }
        }
        catch (const std::exception& ex) {
            return Status(ErrorCodes::FileStreamFailed, str::stream()
                << "Unexpected error reading BSON data from " << filename
                << ": " << ex.what());
        }

        BSONObj obj;
        try {
            obj = BSONObj(&buffer[0]);
        }
        catch (DBException& ex) {
            return Status(ErrorCodes::FailedToParse, str::stream()
                          << "Failed to convert data in " << filename
                          << " to BSON: " << ex.what());
        }

        // Validate 'storage.engine' field.
        BSONElement storageEngineElement = obj.getFieldDotted("storage.engine");
        if (storageEngineElement.type() != mongo::String) {
            return Status(ErrorCodes::FailedToParse, str::stream()
                          << "The 'storage.engine' field in metadata must be a string: "
                          << storageEngineElement.toString());
        }

        // Extract storage engine name from 'storage.engine' node.
        std::string storageEngine = storageEngineElement.String();
        if (storageEngine.empty()) {
            return Status(ErrorCodes::FailedToParse,
                          "The 'storage.engine' field in metadata cannot be empty string.");
        }
        _storageEngine = storageEngine;

        // Read storage engine options generated by storage engine factory from startup options.
        BSONElement storageEngineOptionsElement = obj.getFieldDotted("storage.options");
        if (!storageEngineOptionsElement.eoo()) {
            if (!storageEngineOptionsElement.isABSONObj()) {
                return Status(ErrorCodes::FailedToParse, str::stream()
                              << "The 'storage.options' field in metadata must be a string: "
                              << storageEngineOptionsElement.toString());
            }
            setStorageEngineOptions(storageEngineOptionsElement.Obj());
        }

        return Status::OK();
    }

    Status StorageEngineMetadata::write() const {
        if (_storageEngine.empty()) {
            return Status(ErrorCodes::BadValue,
                          "Cannot write empty storage engine name to metadata file.");
        }

        boost::filesystem::path metadataTempPath =
            boost::filesystem::path(_dbpath) / (kMetadataBasename + ".tmp");
        {
            std::string filenameTemp = metadataTempPath.string();
            std::ofstream ofs(filenameTemp.c_str(), std::ios_base::out | std::ios_base::binary);
            if (!ofs) {
                return Status(ErrorCodes::FileNotOpen, str::stream()
                    << "Failed to write metadata to " << filenameTemp);
            }

            BSONObj obj = BSON("storage"
                << BSON("engine" << _storageEngine << "options" << _storageEngineOptions));
            ofs.write(obj.objdata(), obj.objsize());
            if (!ofs) {
                return Status(ErrorCodes::InternalError, str::stream()
                    << "Failed to write BSON data to " << filenameTemp);
            }
        }

        // Rename temporary file to actual metadata file.
        boost::filesystem::path metadataPath =
            boost::filesystem::path(_dbpath) / kMetadataBasename;
        try {
            boost::filesystem::rename(metadataTempPath, metadataPath);
        }
        catch (const std::exception& ex) {
            return Status(ErrorCodes::FileRenameFailed, str::stream()
                << "Unexpected error while renaming temporary metadata file "
                << metadataTempPath.string() << " to " << metadataPath.string()
                << ": " << ex.what());
        }

        return Status::OK();
    }

    template <>
    Status StorageEngineMetadata::validateStorageEngineOption<bool>(StringData fieldName,
                                                                    bool expectedValue) const {
        BSONElement element = _storageEngineOptions.getField(fieldName);
        if (element.eoo()) {
            return Status::OK();
        }
        if (!element.isBoolean()) {
            return Status(ErrorCodes::FailedToParse, str::stream()
                << "Expected boolean field " << fieldName << " but got "
                << typeName(element.type()) << " instead: " << element);
        }
        if (element.boolean() == expectedValue) {
            return Status::OK();
        }
        return Status(ErrorCodes::InvalidOptions, str::stream()
            << "Metadata contains unexpected value storage engine option for " << fieldName
            << "Expected " << (expectedValue ? "true" : "false") << " but got "
            << (element.boolean() ? "true" : "false") << "instead");
    }

}  // namespace mongo