1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "mozilla/DebugOnly.h"
8 
9 #include "VacuumManager.h"
10 
11 #include "mozilla/Services.h"
12 #include "mozilla/Preferences.h"
13 #include "nsIObserverService.h"
14 #include "nsIFile.h"
15 #include "nsThreadUtils.h"
16 #include "mozilla/Logging.h"
17 #include "prtime.h"
18 #include "mozilla/StaticPrefs_storage.h"
19 
20 #include "mozStorageConnection.h"
21 #include "mozIStorageStatement.h"
22 #include "mozIStorageStatementCallback.h"
23 #include "mozIStorageAsyncStatement.h"
24 #include "mozIStoragePendingStatement.h"
25 #include "mozIStorageError.h"
26 #include "mozStorageHelper.h"
27 #include "nsXULAppAPI.h"
28 
29 #define OBSERVER_TOPIC_IDLE_DAILY "idle-daily"
30 #define OBSERVER_TOPIC_XPCOM_SHUTDOWN "xpcom-shutdown"
31 
32 // Used to notify begin and end of a heavy IO task.
33 #define OBSERVER_TOPIC_HEAVY_IO "heavy-io-task"
34 #define OBSERVER_DATA_VACUUM_BEGIN u"vacuum-begin"
35 #define OBSERVER_DATA_VACUUM_END u"vacuum-end"
36 
37 // This preferences root will contain last vacuum timestamps (in seconds) for
38 // each database.  The database filename is used as a key.
39 #define PREF_VACUUM_BRANCH "storage.vacuum.last."
40 
41 // Time between subsequent vacuum calls for a certain database.
42 #define VACUUM_INTERVAL_SECONDS 30 * 86400  // 30 days.
43 
44 extern mozilla::LazyLogModule gStorageLog;
45 
46 namespace mozilla {
47 namespace storage {
48 
49 namespace {
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 //// BaseCallback
53 
54 class BaseCallback : public mozIStorageStatementCallback {
55  public:
56   NS_DECL_ISUPPORTS
57   NS_DECL_MOZISTORAGESTATEMENTCALLBACK
BaseCallback()58   BaseCallback() {}
59 
60  protected:
~BaseCallback()61   virtual ~BaseCallback() {}
62 };
63 
64 NS_IMETHODIMP
HandleError(mozIStorageError * aError)65 BaseCallback::HandleError(mozIStorageError* aError) {
66 #ifdef DEBUG
67   int32_t result;
68   nsresult rv = aError->GetResult(&result);
69   NS_ENSURE_SUCCESS(rv, rv);
70   nsAutoCString message;
71   rv = aError->GetMessage(message);
72   NS_ENSURE_SUCCESS(rv, rv);
73 
74   nsAutoCString warnMsg;
75   warnMsg.AppendLiteral("An error occured during async execution: ");
76   warnMsg.AppendInt(result);
77   warnMsg.Append(' ');
78   warnMsg.Append(message);
79   NS_WARNING(warnMsg.get());
80 #endif
81   return NS_OK;
82 }
83 
84 NS_IMETHODIMP
HandleResult(mozIStorageResultSet * aResultSet)85 BaseCallback::HandleResult(mozIStorageResultSet* aResultSet) {
86   // We could get results from PRAGMA statements, but we don't mind them.
87   return NS_OK;
88 }
89 
90 NS_IMETHODIMP
HandleCompletion(uint16_t aReason)91 BaseCallback::HandleCompletion(uint16_t aReason) {
92   // By default BaseCallback will just be silent on completion.
93   return NS_OK;
94 }
95 
96 NS_IMPL_ISUPPORTS(BaseCallback, mozIStorageStatementCallback)
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 //// Vacuumer declaration.
100 
101 class Vacuumer : public BaseCallback {
102  public:
103   NS_DECL_MOZISTORAGESTATEMENTCALLBACK
104 
105   explicit Vacuumer(mozIStorageVacuumParticipant* aParticipant);
106 
107   bool execute();
108   nsresult notifyCompletion(bool aSucceeded);
109 
110  private:
111   nsCOMPtr<mozIStorageVacuumParticipant> mParticipant;
112   nsCString mDBFilename;
113   nsCOMPtr<mozIStorageConnection> mDBConn;
114 };
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 //// Vacuumer implementation.
118 
Vacuumer(mozIStorageVacuumParticipant * aParticipant)119 Vacuumer::Vacuumer(mozIStorageVacuumParticipant* aParticipant)
120     : mParticipant(aParticipant) {}
121 
execute()122 bool Vacuumer::execute() {
123   MOZ_ASSERT(NS_IsMainThread(), "Must be running on the main thread!");
124 
125   // Get the connection and check its validity.
126   nsresult rv = mParticipant->GetDatabaseConnection(getter_AddRefs(mDBConn));
127   NS_ENSURE_SUCCESS(rv, false);
128   bool ready = false;
129   if (!mDBConn || NS_FAILED(mDBConn->GetConnectionReady(&ready)) || !ready) {
130     NS_WARNING("Unable to get a connection to vacuum database");
131     return false;
132   }
133 
134   // Ask for the expected page size.  Vacuum can change the page size, unless
135   // the database is using WAL journaling.
136   // TODO Bug 634374: figure out a strategy to fix page size with WAL.
137   int32_t expectedPageSize = 0;
138   rv = mParticipant->GetExpectedDatabasePageSize(&expectedPageSize);
139   if (NS_FAILED(rv) || !Service::pageSizeIsValid(expectedPageSize)) {
140     NS_WARNING("Invalid page size requested for database, will use default ");
141     NS_WARNING(mDBFilename.get());
142     expectedPageSize = Service::kDefaultPageSize;
143   }
144 
145   // Get the database filename.  Last vacuum time is stored under this name
146   // in PREF_VACUUM_BRANCH.
147   nsCOMPtr<nsIFile> databaseFile;
148   mDBConn->GetDatabaseFile(getter_AddRefs(databaseFile));
149   if (!databaseFile) {
150     NS_WARNING("Trying to vacuum a in-memory database!");
151     return false;
152   }
153   nsAutoString databaseFilename;
154   rv = databaseFile->GetLeafName(databaseFilename);
155   NS_ENSURE_SUCCESS(rv, false);
156   CopyUTF16toUTF8(databaseFilename, mDBFilename);
157   MOZ_ASSERT(!mDBFilename.IsEmpty(), "Database filename cannot be empty");
158 
159   // Check interval from last vacuum.
160   int32_t now = static_cast<int32_t>(PR_Now() / PR_USEC_PER_SEC);
161   int32_t lastVacuum;
162   nsAutoCString prefName(PREF_VACUUM_BRANCH);
163   prefName += mDBFilename;
164   rv = Preferences::GetInt(prefName.get(), &lastVacuum);
165   if (NS_SUCCEEDED(rv) && (now - lastVacuum) < VACUUM_INTERVAL_SECONDS) {
166     // This database was vacuumed recently, skip it.
167     return false;
168   }
169 
170   // Notify that we are about to start vacuuming.  The participant can opt-out
171   // if it cannot handle a vacuum at this time, and then we'll move to the next
172   // one.
173   bool vacuumGranted = false;
174   rv = mParticipant->OnBeginVacuum(&vacuumGranted);
175   NS_ENSURE_SUCCESS(rv, false);
176   if (!vacuumGranted) {
177     return false;
178   }
179 
180   // Notify a heavy IO task is about to start.
181   nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
182   if (os) {
183     rv = os->NotifyObservers(nullptr, OBSERVER_TOPIC_HEAVY_IO,
184                              OBSERVER_DATA_VACUUM_BEGIN);
185     MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to notify");
186   }
187 
188   // Execute the statements separately, since the pragma may conflict with the
189   // vacuum, if they are executed in the same transaction.
190   nsCOMPtr<mozIStorageAsyncStatement> pageSizeStmt;
191   nsAutoCString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
192                               "PRAGMA page_size = ");
193   pageSizeQuery.AppendInt(expectedPageSize);
194   rv = mDBConn->CreateAsyncStatement(pageSizeQuery,
195                                      getter_AddRefs(pageSizeStmt));
196   NS_ENSURE_SUCCESS(rv, false);
197   RefPtr<BaseCallback> callback = new BaseCallback();
198   nsCOMPtr<mozIStoragePendingStatement> ps;
199   rv = pageSizeStmt->ExecuteAsync(callback, getter_AddRefs(ps));
200   NS_ENSURE_SUCCESS(rv, false);
201 
202   nsCOMPtr<mozIStorageAsyncStatement> stmt;
203   rv = mDBConn->CreateAsyncStatement("VACUUM"_ns, getter_AddRefs(stmt));
204   NS_ENSURE_SUCCESS(rv, false);
205   rv = stmt->ExecuteAsync(this, getter_AddRefs(ps));
206   NS_ENSURE_SUCCESS(rv, false);
207 
208   return true;
209 }
210 
211 ////////////////////////////////////////////////////////////////////////////////
212 //// mozIStorageStatementCallback
213 
214 NS_IMETHODIMP
HandleError(mozIStorageError * aError)215 Vacuumer::HandleError(mozIStorageError* aError) {
216   int32_t result;
217   nsresult rv;
218   nsAutoCString message;
219 
220 #ifdef DEBUG
221   rv = aError->GetResult(&result);
222   NS_ENSURE_SUCCESS(rv, rv);
223   rv = aError->GetMessage(message);
224   NS_ENSURE_SUCCESS(rv, rv);
225 
226   nsAutoCString warnMsg;
227   warnMsg.AppendLiteral("Unable to vacuum database: ");
228   warnMsg.Append(mDBFilename);
229   warnMsg.AppendLiteral(" - ");
230   warnMsg.AppendInt(result);
231   warnMsg.Append(' ');
232   warnMsg.Append(message);
233   NS_WARNING(warnMsg.get());
234 #endif
235 
236   if (MOZ_LOG_TEST(gStorageLog, LogLevel::Error)) {
237     rv = aError->GetResult(&result);
238     NS_ENSURE_SUCCESS(rv, rv);
239     rv = aError->GetMessage(message);
240     NS_ENSURE_SUCCESS(rv, rv);
241     MOZ_LOG(gStorageLog, LogLevel::Error,
242             ("Vacuum failed with error: %d '%s'. Database was: '%s'", result,
243              message.get(), mDBFilename.get()));
244   }
245   return NS_OK;
246 }
247 
248 NS_IMETHODIMP
HandleResult(mozIStorageResultSet * aResultSet)249 Vacuumer::HandleResult(mozIStorageResultSet* aResultSet) {
250   MOZ_ASSERT_UNREACHABLE("Got a resultset from a vacuum?");
251   return NS_OK;
252 }
253 
254 NS_IMETHODIMP
HandleCompletion(uint16_t aReason)255 Vacuumer::HandleCompletion(uint16_t aReason) {
256   if (aReason == REASON_FINISHED) {
257     // Update last vacuum time.
258     int32_t now = static_cast<int32_t>(PR_Now() / PR_USEC_PER_SEC);
259     MOZ_ASSERT(!mDBFilename.IsEmpty(), "Database filename cannot be empty");
260     nsAutoCString prefName(PREF_VACUUM_BRANCH);
261     prefName += mDBFilename;
262     DebugOnly<nsresult> rv = Preferences::SetInt(prefName.get(), now);
263     MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to set a preference");
264   }
265 
266   notifyCompletion(aReason == REASON_FINISHED);
267 
268   return NS_OK;
269 }
270 
notifyCompletion(bool aSucceeded)271 nsresult Vacuumer::notifyCompletion(bool aSucceeded) {
272   nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
273   if (os) {
274     os->NotifyObservers(nullptr, OBSERVER_TOPIC_HEAVY_IO,
275                         OBSERVER_DATA_VACUUM_END);
276   }
277 
278   nsresult rv = mParticipant->OnEndVacuum(aSucceeded);
279   NS_ENSURE_SUCCESS(rv, rv);
280 
281   return NS_OK;
282 }
283 
284 }  // namespace
285 
286 ////////////////////////////////////////////////////////////////////////////////
287 //// VacuumManager
288 
289 NS_IMPL_ISUPPORTS(VacuumManager, nsIObserver)
290 
291 VacuumManager* VacuumManager::gVacuumManager = nullptr;
292 
getSingleton()293 already_AddRefed<VacuumManager> VacuumManager::getSingleton() {
294   // Don't allocate it in the child Process.
295   if (!XRE_IsParentProcess()) {
296     return nullptr;
297   }
298 
299   if (!gVacuumManager) {
300     auto manager = MakeRefPtr<VacuumManager>();
301     MOZ_ASSERT(gVacuumManager == manager.get());
302     return manager.forget();
303   }
304   return do_AddRef(gVacuumManager);
305 }
306 
VacuumManager()307 VacuumManager::VacuumManager() : mParticipants("vacuum-participant") {
308   MOZ_ASSERT(!gVacuumManager,
309              "Attempting to create two instances of the service!");
310   gVacuumManager = this;
311 }
312 
~VacuumManager()313 VacuumManager::~VacuumManager() {
314   // Remove the static reference to the service.  Check to make sure its us
315   // in case somebody creates an extra instance of the service.
316   MOZ_ASSERT(gVacuumManager == this,
317              "Deleting a non-singleton instance of the service");
318   if (gVacuumManager == this) {
319     gVacuumManager = nullptr;
320   }
321 }
322 
323 ////////////////////////////////////////////////////////////////////////////////
324 //// nsIObserver
325 
326 NS_IMETHODIMP
Observe(nsISupports * aSubject,const char * aTopic,const char16_t * aData)327 VacuumManager::Observe(nsISupports* aSubject, const char* aTopic,
328                        const char16_t* aData) {
329   if (strcmp(aTopic, OBSERVER_TOPIC_IDLE_DAILY) == 0) {
330     // Try to run vacuum on all registered entries.  Will stop at the first
331     // successful one.
332     nsCOMArray<mozIStorageVacuumParticipant> entries;
333     mParticipants.GetEntries(entries);
334     // If there are more entries than what a month can contain, we could end up
335     // skipping some, since we run daily.  So we use a starting index.
336     static const char* kPrefName = PREF_VACUUM_BRANCH "index";
337     int32_t startIndex = Preferences::GetInt(kPrefName, 0);
338     if (startIndex >= entries.Count()) {
339       startIndex = 0;
340     }
341     int32_t index;
342     for (index = startIndex; index < entries.Count(); ++index) {
343       RefPtr<Vacuumer> vacuum = new Vacuumer(entries[index]);
344       // Only vacuum one database per day.
345       if (vacuum->execute()) {
346         break;
347       }
348     }
349     DebugOnly<nsresult> rv = Preferences::SetInt(kPrefName, index);
350     MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to set a preference");
351   }
352 
353   return NS_OK;
354 }
355 
356 }  // namespace storage
357 }  // namespace mozilla
358