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
|
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
/* -*- mode: C; c-basic-offset: 4 -*- */
#ident "$Id$"
/*======
This file is part of TokuDB
Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
TokuDB is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2,
as published by the Free Software Foundation.
TokuDB 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with TokuDB. If not, see <http://www.gnu.org/licenses/>.
======= */
#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
#include "tokudb_background.h"
#include "tokudb_sysvars.h"
namespace tokudb {
namespace background {
std::atomic<uint64_t> job_manager_t::job_t::_next_id(1);
job_manager_t::job_t::job_t(bool user_scheduled) :
_running(false),
_cancelled(false),
_id(_next_id++),
_user_scheduled(user_scheduled),
_scheduled_time(::time(0)),
_started_time(0) {
}
job_manager_t::job_t::~job_t() {
}
void* job_manager_t::operator new(size_t sz) {
return tokudb::memory::malloc(sz, MYF(MY_WME|MY_ZEROFILL|MY_FAE));
}
void job_manager_t::operator delete(void* p) {
tokudb::memory::free(p);
}
job_manager_t::job_manager_t() :
_sem(0, 65535),
_shutdown(false) {
}
job_manager_t::~job_manager_t() {
}
void job_manager_t::initialize() {
int r = _thread.start(thread_func, this);
assert_always(r == 0);
}
void job_manager_t::destroy() {
assert_always(!_shutdown);
assert_always(_foreground_jobs.size() == 0);
_shutdown = true;
_sem.set_interrupt();
while (_background_jobs.size()) {
_mutex.lock();
job_t* job = _background_jobs.front();
if (!job->cancelled())
cancel(job);
_background_jobs.pop_front();
delete job;
_mutex.unlock();
}
void* result;
int r = _thread.join(&result);
assert_always(r == 0);
}
bool job_manager_t::run_job(job_t* newjob, bool background) {
bool ret = false;
const char* jobkey = newjob->key();
_mutex.lock();
assert_always(!_shutdown);
for (jobs_t::iterator it = _background_jobs.begin();
it != _background_jobs.end();
it++) {
job_t* job = *it;
if (!job->cancelled() && strcmp(job->key(), jobkey) == 0) {
// if this is a foreground job being run and
// there is an existing background job of the same type
// and it is not running yet, we can cancel the background job
// and just run this one in the foreground, might have different
// params, but that is up to the user to figure out.
if (!background && !job->running()) {
job->cancel();
} else {
// can't schedule or run another job on the same key
goto cleanup;
}
}
}
for (jobs_t::iterator it = _foreground_jobs.begin();
it != _foreground_jobs.end();
it++) {
job_t* job = *it;
if (strcmp(job->key(), jobkey) == 0) {
// can't schedule or run another job on the same key
// as an existing foreground job
goto cleanup;
}
}
if (background) {
_background_jobs.push_back(newjob);
_sem.signal();
ret = true;
} else {
_foreground_jobs.push_back(newjob);
run(newjob);
for (jobs_t::iterator it = _foreground_jobs.begin();
it != _foreground_jobs.end();
it++) {
job_t* job = *it;
if (job == newjob) {
_foreground_jobs.erase(it);
delete job;
break;
}
}
ret = true;
}
cleanup:
_mutex.unlock();
return ret;
}
bool job_manager_t::cancel_job(const char* key) {
bool ret = false;
_mutex.lock();
for (jobs_t::iterator it = _background_jobs.begin();
it != _background_jobs.end(); it++) {
job_t* job = *it;
if (!job->cancelled() && strcmp(job->key(), key) == 0) {
cancel(job);
ret = true;
}
}
_mutex.unlock();
return ret;
}
void job_manager_t::iterate_jobs(pfn_iterate_t callback, void* extra) const {
_mutex.lock();
for (jobs_t::const_iterator it = _background_jobs.begin();
it != _background_jobs.end();
it++) {
job_t* job = *it;
if (!job->cancelled()) {
callback(job, extra);
}
}
_mutex.unlock();
}
void* job_manager_t::thread_func(void* v) {
return ((tokudb::background::job_manager_t*)v)->real_thread_func();
}
void* job_manager_t::real_thread_func() {
while (_shutdown == false) {
tokudb::thread::semaphore_t::E_WAIT res = _sem.wait();
if (res == tokudb::thread::semaphore_t::E_INTERRUPTED || _shutdown) {
break;
} else if (res == tokudb::thread::semaphore_t::E_SIGNALLED) {
#if TOKUDB_DEBUG
if (TOKUDB_UNLIKELY(
tokudb::sysvars::debug_pause_background_job_manager)) {
_sem.signal();
tokudb::time::sleep_microsec(250000);
continue;
}
#endif // TOKUDB_DEBUG
_mutex.lock();
assert_debug(_background_jobs.size() > 0);
job_t* job = _background_jobs.front();
run(job);
_background_jobs.pop_front();
_mutex.unlock();
delete job;
}
}
return NULL;
}
void job_manager_t::run(job_t* job) {
assert_debug(_mutex.is_owned_by_me());
if (!job->cancelled()) {
_mutex.unlock();
// do job
job->run();
// done job
_mutex.lock();
}
if (!job->cancelled()) {
job->destroy();
}
}
void job_manager_t::cancel(job_t* job) {
assert_debug(_mutex.is_owned_by_me());
assert_always(!job->cancelled());
job->cancel();
}
job_manager_t* _job_manager = NULL;
bool initialize() {
assert_always(_job_manager == NULL);
_job_manager = new job_manager_t;
_job_manager->initialize();
return true;
}
bool destroy() {
_job_manager->destroy();
delete _job_manager;
_job_manager = NULL;
return true;
}
} // namespace background
} // namespace tokudb
|