1 // Copyright (c) 2013 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
5 #ifndef THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_
6 #define THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_
13 #include "base/files/file.h"
14 #include "base/files/file_path.h"
15 #include "base/metrics/histogram.h"
16 #include "leveldb/env.h"
17 #include "port/port_chromium.h"
18 #include "util/mutexlock.h"
20 namespace leveldb_env
{
22 // These entries map to values in tools/metrics/histograms/histograms.xml. New
23 // values should be appended at the end.
27 kRandomAccessFileRead
,
50 // The default value for leveldb::Options::reuse_logs. Currently log reuse is an
51 // experimental feature in leveldb. More info at:
52 // https://github.com/google/leveldb/commit/251ebf5dc70129ad3
53 #if defined(OS_CHROMEOS)
54 // Reusing logs on Chrome OS resulted in an unacceptably high leveldb corruption
55 // rate (at least for Indexed DB). More info at https://crbug.com/460568
56 const bool kDefaultLogReuseOptionValue
= false;
58 const bool kDefaultLogReuseOptionValue
= true;
61 const char* MethodIDToString(MethodID method
);
63 leveldb::Status
MakeIOError(leveldb::Slice filename
,
64 const std::string
& message
,
66 base::File::Error error
);
67 leveldb::Status
MakeIOError(leveldb::Slice filename
,
68 const std::string
& message
,
71 enum ErrorParsingResult
{
77 ErrorParsingResult
ParseMethodAndError(const leveldb::Status
& status
,
79 base::File::Error
* error
);
80 int GetCorruptionCode(const leveldb::Status
& status
);
81 int GetNumCorruptionCodes();
82 std::string
GetCorruptionMessage(const leveldb::Status
& status
);
83 bool IndicatesDiskFull(const leveldb::Status
& status
);
87 virtual void RecordErrorAt(MethodID method
) const = 0;
88 virtual void RecordOSError(MethodID method
,
89 base::File::Error error
) const = 0;
90 virtual void RecordBackupResult(bool success
) const = 0;
93 class RetrierProvider
{
95 virtual int MaxRetryTimeMillis() const = 0;
96 virtual base::HistogramBase
* GetRetryTimeHistogram(MethodID method
) const = 0;
97 virtual base::HistogramBase
* GetRecoveredFromErrorHistogram(
98 MethodID method
) const = 0;
101 class ChromiumEnv
: public leveldb::Env
,
103 public RetrierProvider
{
107 typedef void(ScheduleFunc
)(void*);
109 static bool MakeBackup(const std::string
& fname
);
110 virtual ~ChromiumEnv();
112 virtual bool FileExists(const std::string
& fname
);
113 virtual leveldb::Status
GetChildren(const std::string
& dir
,
114 std::vector
<std::string
>* result
);
115 virtual leveldb::Status
DeleteFile(const std::string
& fname
);
116 virtual leveldb::Status
CreateDir(const std::string
& name
);
117 virtual leveldb::Status
DeleteDir(const std::string
& name
);
118 virtual leveldb::Status
GetFileSize(const std::string
& fname
, uint64_t* size
);
119 virtual leveldb::Status
RenameFile(const std::string
& src
,
120 const std::string
& dst
);
121 virtual leveldb::Status
LockFile(const std::string
& fname
,
122 leveldb::FileLock
** lock
);
123 virtual leveldb::Status
UnlockFile(leveldb::FileLock
* lock
);
124 virtual void Schedule(ScheduleFunc
*, void* arg
);
125 virtual void StartThread(void (*function
)(void* arg
), void* arg
);
126 virtual leveldb::Status
GetTestDirectory(std::string
* path
);
127 virtual uint64_t NowMicros();
128 virtual void SleepForMicroseconds(int micros
);
129 virtual leveldb::Status
NewSequentialFile(const std::string
& fname
,
130 leveldb::SequentialFile
** result
);
131 virtual leveldb::Status
NewRandomAccessFile(
132 const std::string
& fname
,
133 leveldb::RandomAccessFile
** result
);
134 virtual leveldb::Status
NewWritableFile(const std::string
& fname
,
135 leveldb::WritableFile
** result
);
136 virtual leveldb::Status
NewAppendableFile(const std::string
& fname
,
137 leveldb::WritableFile
** result
);
138 virtual leveldb::Status
NewLogger(const std::string
& fname
,
139 leveldb::Logger
** result
);
143 std::string uma_ioerror_base_name_
;
147 static const char* FileErrorString(base::File::Error error
);
149 virtual void RecordErrorAt(MethodID method
) const;
150 virtual void RecordOSError(MethodID method
,
151 base::File::Error error
) const;
152 void RecordOpenFilesLimit(const std::string
& type
);
153 base::HistogramBase
* GetMaxFDHistogram(const std::string
& type
) const;
154 base::HistogramBase
* GetOSErrorHistogram(MethodID method
, int limit
) const;
156 // File locks may not be exclusive within a process (e.g. on POSIX). Track
157 // locks held by the ChromiumEnv to prevent access within the process.
160 bool Insert(const std::string
& fname
) {
161 leveldb::MutexLock
l(&mu_
);
162 return locked_files_
.insert(fname
).second
;
164 bool Remove(const std::string
& fname
) {
165 leveldb::MutexLock
l(&mu_
);
166 return locked_files_
.erase(fname
) == 1;
169 leveldb::port::Mutex mu_
;
170 std::set
<std::string
> locked_files_
;
173 const int kMaxRetryTimeMillis
;
174 // BGThread() is the body of the background thread
176 static void BGThreadWrapper(void* arg
) {
177 reinterpret_cast<ChromiumEnv
*>(arg
)->BGThread();
180 virtual void RecordBackupResult(bool result
) const;
181 void RestoreIfNecessary(const std::string
& dir
,
182 std::vector
<std::string
>* children
);
183 base::FilePath
RestoreFromBackup(const base::FilePath
& base_name
);
184 void RecordLockFileAncestors(int num_missing_ancestors
) const;
185 base::HistogramBase
* GetMethodIOErrorHistogram() const;
186 base::HistogramBase
* GetLockFileAncestorHistogram() const;
188 // RetrierProvider implementation.
189 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis
; }
190 virtual base::HistogramBase
* GetRetryTimeHistogram(MethodID method
) const;
191 virtual base::HistogramBase
* GetRecoveredFromErrorHistogram(
192 MethodID method
) const;
194 base::FilePath test_directory_
;
197 base::ConditionVariable bgsignal_
;
198 bool started_bgthread_
;
200 // Entry per Schedule() call
203 void (*function
)(void*);
205 typedef std::deque
<BGItem
> BGQueue
;
210 } // namespace leveldb_env
212 #endif // THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_