summaryrefslogtreecommitdiff
path: root/src/mongo/db/storage/kv/kv_storage_engine.cpp
blob: 15a17987ff72cf3052c9d1b3be94a279ab801a53 (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
277
278
// kv_storage_engine.cpp

/**
 *    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/db/storage/kv/kv_storage_engine.h"

#include "mongo/db/operation_context_noop.h"
#include "mongo/db/storage/kv/kv_database_catalog_entry.h"
#include "mongo/db/storage/kv/kv_engine.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/log.h"
#include "mongo/util/mongoutils/str.h"

namespace mongo {

    using std::string;
    using std::vector;

    namespace {
        const std::string catalogInfo = "_mdb_catalog";
    }

    class KVStorageEngine::RemoveDBChange : public RecoveryUnit::Change {
    public:
        RemoveDBChange(KVStorageEngine* engine, StringData db, KVDatabaseCatalogEntry* entry)
            : _engine(engine)
            , _db(db.toString())
            , _entry(entry)
        {}

        virtual void commit() {
            delete _entry;
        }

        virtual void rollback() {
            stdx::lock_guard<stdx::mutex> lk(_engine->_dbsLock);
            _engine->_dbs[_db] = _entry;
        }

        KVStorageEngine* const _engine;
        const std::string _db;
        KVDatabaseCatalogEntry* const _entry;
    };

    KVStorageEngine::KVStorageEngine( KVEngine* engine,
                                      const KVStorageEngineOptions& options )
        : _options( options )
        , _engine( engine )
        , _supportsDocLocking(_engine->supportsDocLocking()) {

        uassert(28601, "Storage engine does not support --directoryperdb",
                !(options.directoryPerDB && !engine->supportsDirectoryPerDB()));

        OperationContextNoop opCtx( _engine->newRecoveryUnit() );

        if (options.forRepair && engine->hasIdent(&opCtx, catalogInfo)) {
            log() << "Repairing catalog metadata";
            // TODO should also validate all BSON in the catalog.
            engine->repairIdent(&opCtx, catalogInfo);
        }

        {
            WriteUnitOfWork uow( &opCtx );

            Status status = _engine->createRecordStore( &opCtx,
                                                        catalogInfo,
                                                        catalogInfo,
                                                        CollectionOptions() );
            // BadValue is usually caused by invalid configuration string.
            // We still fassert() but without a stack trace.
            if (status.code() == ErrorCodes::BadValue) {
                fassertFailedNoTrace(28562);
            }
            fassert( 28520, status );

            _catalogRecordStore.reset( _engine->getRecordStore( &opCtx,
                                                                catalogInfo,
                                                                catalogInfo,
                                                                CollectionOptions() ) );
            _catalog.reset( new KVCatalog( _catalogRecordStore.get(),
                                           _supportsDocLocking,
                                           _options.directoryPerDB,
                                           _options.directoryForIndexes) );
            _catalog->init( &opCtx );

            std::vector<std::string> collections;
            _catalog->getAllCollections( &collections );

            for ( size_t i = 0; i < collections.size(); i++ ) {
                std::string coll = collections[i];
                NamespaceString nss( coll );
                string dbName = nss.db().toString();

                // No rollback since this is only for committed dbs.
                KVDatabaseCatalogEntry*& db = _dbs[dbName];
                if ( !db ) {
                    db = new KVDatabaseCatalogEntry( dbName, this );
                }

                db->initCollection( &opCtx, coll, options.forRepair );
            }

            uow.commit();
        }

        opCtx.recoveryUnit()->abandonSnapshot();

        // now clean up orphaned idents

        {
            // get all idents
            std::set<std::string> allIdents;
            {
                std::vector<std::string> v = _engine->getAllIdents( &opCtx );
                allIdents.insert( v.begin(), v.end() );
                allIdents.erase( catalogInfo );
            }

            // remove ones still in use
            {
                vector<string> idents = _catalog->getAllIdents( &opCtx );
                for ( size_t i = 0; i < idents.size(); i++ ) {
                    allIdents.erase( idents[i] );
                }
            }

            for ( std::set<std::string>::const_iterator it = allIdents.begin();
                  it != allIdents.end();
                  ++it ) {
                const std::string& toRemove = *it;
                if ( !_catalog->isUserDataIdent( toRemove ) )
                    continue;
                log() << "dropping unused ident: " << toRemove;
                WriteUnitOfWork wuow( &opCtx );
                _engine->dropIdent( &opCtx, toRemove );
                wuow.commit();
            }
        }

    }

    void KVStorageEngine::cleanShutdown() {

        for ( DBMap::const_iterator it = _dbs.begin(); it != _dbs.end(); ++it ) {
            delete it->second;
        }
        _dbs.clear();

        _catalog.reset( NULL );
        _catalogRecordStore.reset( NULL );

        _engine->cleanShutdown();
        // intentionally not deleting _engine
    }

    KVStorageEngine::~KVStorageEngine() {
    }

    void KVStorageEngine::finishInit() {
    }

    RecoveryUnit* KVStorageEngine::newRecoveryUnit() {
        if ( !_engine ) {
            // shutdown
            return NULL;
        }
        return _engine->newRecoveryUnit();
    }

    void KVStorageEngine::listDatabases( std::vector<std::string>* out ) const {
        stdx::lock_guard<stdx::mutex> lk( _dbsLock );
        for ( DBMap::const_iterator it = _dbs.begin(); it != _dbs.end(); ++it ) {
            if ( it->second->isEmpty() )
                continue;
            out->push_back( it->first );
        }
    }

    DatabaseCatalogEntry* KVStorageEngine::getDatabaseCatalogEntry( OperationContext* opCtx,
                                                                    StringData dbName ) {
        stdx::lock_guard<stdx::mutex> lk( _dbsLock );
        KVDatabaseCatalogEntry*& db = _dbs[dbName.toString()];
        if ( !db ) {
            // Not registering change since db creation is implicit and never rolled back.
            db = new KVDatabaseCatalogEntry( dbName, this );
        }
        return db;
    }

    Status KVStorageEngine::closeDatabase( OperationContext* txn, StringData db ) {
        // This is ok to be a no-op as there is no database layer in kv.
        return Status::OK();
    }

    Status KVStorageEngine::dropDatabase( OperationContext* txn, StringData db ) {

        KVDatabaseCatalogEntry* entry;
        {
            stdx::lock_guard<stdx::mutex> lk( _dbsLock );
            DBMap::const_iterator it = _dbs.find( db.toString() );
            if ( it == _dbs.end() )
                return Status( ErrorCodes::NamespaceNotFound, "db not found to drop" );
            entry = it->second;
        }

        // This is called outside of a WUOW since MMAPv1 has unfortunate behavior around dropping
        // databases. We need to create one here since we want db dropping to all-or-nothing
        // wherever possible. Eventually we want to move this up so that it can include the logOp
        // inside of the WUOW, but that would require making DB dropping happen inside the Dur
        // system for MMAPv1.
        WriteUnitOfWork wuow(txn);

        std::list<std::string> toDrop;
        entry->getCollectionNamespaces( &toDrop );

        for ( std::list<std::string>::iterator it = toDrop.begin(); it != toDrop.end(); ++it ) {
            string coll = *it;
            entry->dropCollection( txn, coll );
        }
        toDrop.clear();
        entry->getCollectionNamespaces( &toDrop );
        invariant( toDrop.empty() );

        {
            stdx::lock_guard<stdx::mutex> lk( _dbsLock );
            txn->recoveryUnit()->registerChange(new RemoveDBChange(this, db, entry));
            _dbs.erase( db.toString() );
        }

        wuow.commit();
        return Status::OK();
    }

    int KVStorageEngine::flushAllFiles( bool sync ) {
        return _engine->flushAllFiles( sync );
    }

    bool KVStorageEngine::isDurable() const {
        return _engine->isDurable();
    }

    Status KVStorageEngine::repairRecordStore(OperationContext* txn, const std::string& ns) {
        Status status = _engine->repairIdent(txn, _catalog->getCollectionIdent(ns));
        if (!status.isOK())
            return status;

        _dbs[nsToDatabase(ns)]->reinitCollectionAfterRepair(txn, ns);
        return Status::OK();
    }
}