//////////////////////////////////////////////////////////////////////////////// /// @brief Write-ahead log garbage collection thread /// /// @file /// /// DISCLAIMER /// /// Copyright 2004-2013 triAGENS GmbH, Cologne, Germany /// /// Licensed under the Apache License, Version 2.0 (the "License"); /// you may not use this file except in compliance with the License. /// You may obtain a copy of the License at /// /// http://www.apache.org/licenses/LICENSE-2.0 /// /// Unless required by applicable law or agreed to in writing, software /// distributed under the License is distributed on an "AS IS" BASIS, /// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. /// See the License for the specific language governing permissions and /// limitations under the License. /// /// Copyright holder is triAGENS GmbH, Cologne, Germany /// /// @author Jan Steemann /// @author Copyright 2011-2013, triAGENS GmbH, Cologne, Germany //////////////////////////////////////////////////////////////////////////////// #include "CollectorThread.h" #include "BasicsC/logging.h" #include "Basics/ConditionLocker.h" #include "Wal/Logfile.h" #include "Wal/LogfileManager.h" using namespace triagens::wal; // ----------------------------------------------------------------------------- // --SECTION-- class CollectorThread // ----------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// /// @brief wait interval for the collector thread when idle //////////////////////////////////////////////////////////////////////////////// const uint64_t CollectorThread::Interval = 1000000; // ----------------------------------------------------------------------------- // --SECTION-- constructors and destructors // ----------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// /// @brief create the collector thread //////////////////////////////////////////////////////////////////////////////// CollectorThread::CollectorThread (LogfileManager* logfileManager) : Thread("WalCollector"), _logfileManager(logfileManager), _condition(), _stop(0) { allowAsynchronousCancelation(); } //////////////////////////////////////////////////////////////////////////////// /// @brief destroy the collector thread //////////////////////////////////////////////////////////////////////////////// CollectorThread::~CollectorThread () { } // ----------------------------------------------------------------------------- // --SECTION-- public methods // ----------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// /// @brief stops the collector thread //////////////////////////////////////////////////////////////////////////////// void CollectorThread::stop () { if (_stop > 0) { return; } _stop = 1; _condition.signal(); while (_stop != 2) { usleep(1000); } } //////////////////////////////////////////////////////////////////////////////// /// @brief signal the thread that there is something to do //////////////////////////////////////////////////////////////////////////////// void CollectorThread::signal () { CONDITION_LOCKER(guard, _condition); guard.signal(); } // ----------------------------------------------------------------------------- // --SECTION-- Thread methods // ----------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// /// @brief main loop //////////////////////////////////////////////////////////////////////////////// void CollectorThread::run () { while (_stop == 0) { // collect a logfile if any qualifies bool worked = this->collectLogfiles(); // delete a logfile if any qualifies worked |= this->removeLogfiles(); CONDITION_LOCKER(guard, _condition); if (! worked) { // sleep only if there was nothing to do guard.wait(Interval); } } _stop = 2; } // ----------------------------------------------------------------------------- // --SECTION-- private methods // ----------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// /// @brief perform collection of a logfile (if any) //////////////////////////////////////////////////////////////////////////////// bool CollectorThread::collectLogfiles () { Logfile* logfile = _logfileManager->getCollectableLogfile(); if (logfile == nullptr) { return false; } _logfileManager->setCollectionRequested(logfile); // TODO: implement the actual logfile collection LOG_TRACE("collecting logfile %llu", (unsigned long long) logfile->id()); _logfileManager->setCollectionDone(logfile); return true; } //////////////////////////////////////////////////////////////////////////////// /// @brief perform removal of a logfile (if any) //////////////////////////////////////////////////////////////////////////////// bool CollectorThread::removeLogfiles () { Logfile* logfile = _logfileManager->getRemovableLogfile(); if (logfile == nullptr) { return false; } _logfileManager->removeLogfile(logfile, true); return true; } // Local Variables: // mode: outline-minor // outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|/// @page\\|// --SECTION--\\|/// @\\}" // End: