Storage Engine API
file_allocator.h
Go to the documentation of this file.
1 // @file file_allocator.h
2 
3 /* Copyright 2009 10gen Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License, version 3,
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * As a special exception, the copyright holders give permission to link the
18  * code of portions of this program with the OpenSSL library under certain
19  * conditions as described in each individual source file and distribute
20  * linked combinations including the program with the OpenSSL library. You
21  * must comply with the GNU Affero General Public License in all respects
22  * for all of the code used other than as permitted herein. If you modify
23  * file(s) with this exception, you may extend this exception to your
24  * version of the file(s), but you are not obligated to do so. If you do not
25  * wish to do so, delete this exception statement from your version. If you
26  * delete this exception statement from all source files in the program,
27  * then also delete it in the license file.
28  */
29 
30 #include <boost/filesystem/path.hpp>
31 #include <list>
32 #include <map>
33 
34 #include "mongo/stdx/condition_variable.h"
35 #include "mongo/stdx/mutex.h"
36 #include "mongo/util/concurrency/mutex.h"
37 
38 namespace mongo {
39 
40 /*
41  * Handles allocation of contiguous files on disk. Allocation may be
42  * requested asynchronously or synchronously.
43  * singleton
44  */
47  /*
48  * The public functions may not be called concurrently. The allocation
49  * functions may be called multiple times per file, but only the first
50  * size specified per file will be used.
51  */
52 public:
53  void start();
54 
59  void requestAllocation(const std::string& name, long& size);
60 
61 
66  void allocateAsap(const std::string& name, unsigned long long& size);
67 
68  void waitUntilFinished() const;
69 
70  static void ensureLength(int fd, long size);
71 
73  static FileAllocator* get();
74 
75 private:
76  FileAllocator();
77 
78  void checkFailure();
79 
80  // caller must hold pendingMutex_ lock. Returns size if allocated or
81  // allocation requested, -1 otherwise.
82  long prevSize(const std::string& name) const;
83 
84  // caller must hold pendingMutex_ lock.
85  bool inProgress(const std::string& name) const;
86 
88  static void run(FileAllocator* fa);
89 
90  // generate a unique name for temporary files
91  std::string makeTempFileName(boost::filesystem::path root);
92 
93  mutable stdx::mutex _pendingMutex;
94  mutable stdx::condition_variable _pendingUpdated;
95 
96  std::list<std::string> _pending;
97  mutable std::map<std::string, long> _pendingSize;
98 
99  // unique number for temporary files
100  static unsigned long long _uniqueNumber;
101 
102  bool _failed;
103 };
104 
105 } // namespace mongo
static unsigned long long _uniqueNumber
Definition: file_allocator.h:100
stdx::mutex _pendingMutex
Definition: file_allocator.h:93
std::list< std::string > _pending
Definition: file_allocator.h:96
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
void waitUntilFinished() const
Definition: file_allocator.cpp:181
bool _failed
Definition: file_allocator.h:102
stdx::condition_variable _pendingUpdated
Definition: file_allocator.h:94
void requestAllocation(const std::string &name, long &size)
May be called if file exists.
Definition: file_allocator.cpp:137
std::map< std::string, long > _pendingSize
Definition: file_allocator.h:97
const char * root
Definition: record_store_v1_base.cpp:851
MONGO_DISALLOW_COPYING(FileAllocator)
void start()
Definition: file_allocator.cpp:132
static void ensureLength(int fd, long size)
Definition: file_allocator.cpp:245
std::string makeTempFileName(boost::filesystem::path root)
Definition: file_allocator.cpp:349
bool inProgress(const std::string &name) const
Definition: file_allocator.cpp:342
long prevSize(const std::string &name) const
Definition: file_allocator.cpp:333
void allocateAsap(const std::string &name, unsigned long long &size)
Returns when file has been allocated.
Definition: file_allocator.cpp:151
Definition: file_allocator.h:45
void checkFailure()
Definition: file_allocator.cpp:325
static void run(FileAllocator *fa)
called from the worked thread
Definition: file_allocator.cpp:370
Database *const OperationContext *const const StringData name
Definition: database_impl.cpp:82
FileAllocator()
Definition: file_allocator.cpp:129