|
| 1 | +/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 | +/* |
| 3 | + * Copyright 2019 Couchbase, Inc |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "durability_completion_task.h" |
| 19 | + |
| 20 | +#include "ep_engine.h" |
| 21 | +#include "executorpool.h" |
| 22 | +#include "vbucket.h" |
| 23 | + |
| 24 | +#include <climits> |
| 25 | + |
| 26 | +using namespace std::chrono_literals; |
| 27 | + |
| 28 | +DurabilityCompletionTask::DurabilityCompletionTask( |
| 29 | + EventuallyPersistentEngine& engine) |
| 30 | + : GlobalTask(&engine, TaskId::DurabilityCompletionTask), |
| 31 | + pendingVBs(engine.getConfiguration().getMaxVbuckets()), |
| 32 | + vbid(0) { |
| 33 | + for (auto& vb : pendingVBs) { |
| 34 | + vb.store(false); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +bool DurabilityCompletionTask::run() { |
| 39 | + if (engine->getEpStats().isShutdown) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + // Start by putting ourselves back to sleep once run() completes. |
| 44 | + // If a new VB is notified (or a VB is re-notified after it is processed in |
| 45 | + // the loop below) then that will cause the task to be re-awoken. |
| 46 | + snooze(INT_MAX); |
| 47 | + // Clear the wakeUpScheduled flag - that allows notifySyncWritesToComplete() |
| 48 | + // to wake up (re-schedule) this task if new vBuckets have SyncWrites which |
| 49 | + // need completing. |
| 50 | + wakeUpScheduled.store(false); |
| 51 | + |
| 52 | + const auto startTime = std::chrono::steady_clock::now(); |
| 53 | + |
| 54 | + // Loop for each vBucket, starting from where we previously left off. |
| 55 | + // For each vbucket, if the pending flag is set then clear it, and process |
| 56 | + // its resolved SyncWrites. |
| 57 | + for (size_t count = 0; count < pendingVBs.size(); |
| 58 | + count++, vbid = (vbid + 1) % pendingVBs.size()) { |
| 59 | + if (pendingVBs[vbid].exchange(false)) { |
| 60 | + engine->getVBucket(Vbid(vbid))->processResolvedSyncWrites(); |
| 61 | + } |
| 62 | + // Yield back to scheduler if we have exceeded the maximum runtime |
| 63 | + // for a single execution. |
| 64 | + auto runtime = std::chrono::steady_clock::now() - startTime; |
| 65 | + if (runtime > maxChunkDuration) { |
| 66 | + wakeUp(); |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return true; |
| 72 | +} |
| 73 | + |
| 74 | +void DurabilityCompletionTask::notifySyncWritesToComplete(Vbid vbid) { |
| 75 | + bool expected = false; |
| 76 | + if (!pendingVBs[vbid.get()].compare_exchange_strong(expected, true)) { |
| 77 | + // This VBucket transitioned from false -> true - wake ourselves up so |
| 78 | + // we can start to process the SyncWrites. |
| 79 | + expected = false; |
| 80 | + |
| 81 | + // Performance: Only wake up the task once (and don't repeatedly try to |
| 82 | + // wake if it's already scheduled to wake) - ExecutorPool::wake() isn't |
| 83 | + // super cheap so avoid it if already pending. |
| 84 | + if (wakeUpScheduled.compare_exchange_strong(expected, true)) { |
| 85 | + ExecutorPool::get()->wake(getId()); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +const std::chrono::steady_clock::duration |
| 91 | + DurabilityCompletionTask::maxChunkDuration = 25ms; |
0 commit comments