1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2010 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 "config.h"
19 
20 #include "ep_engine.h"
21 #include "htresizer.h"
22 #include "kv_bucket_iface.h"
23 
24 #include <phosphor/phosphor.h>
25 #include <platform/make_unique.h>
26 
27 #include <memory>
28 
29 /**
30  * Look at all the hash tables and make sure they're sized appropriately.
31  */
32 class ResizingVisitor : public VBucketVisitor {
33 public:
ResizingVisitor()34     ResizingVisitor() { }
35 
36     void visitBucket(VBucketPtr &vb) override {
37         vb->ht.resize();
38     }
39 };
40 
HashtableResizerTask(KVBucketIface* s, double sleepTime)41 HashtableResizerTask::HashtableResizerTask(KVBucketIface* s, double sleepTime)
42     : GlobalTask(&s->getEPEngine(),
43                  TaskId::HashtableResizerTask,
44                  sleepTime,
45                  false),
46       store(s) {
47 }
48 
run(void)49 bool HashtableResizerTask::run(void) {
50     TRACE_EVENT0("ep-engine/task", "HashtableResizerTask");
51     auto pv = std::make_unique<ResizingVisitor>();
52 
53     // [per-VBucket Task] While a Hashtable is resizing no user
54     // requests can be performed (the resizing process needs to
55     // acquire all HT locks). As such we are sensitive to the duration
56     // of this task - we want to log anything which has a
57     // non-negligible impact on frontend operations.
58     const auto maxExpectedDuration = std::chrono::milliseconds(100);
59 
60     store->visit(std::move(pv),
61                  "Hashtable resizer",
62                  TaskId::HashtableResizerVisitorTask,
63                  /*sleepTime*/ 0,
64                  maxExpectedDuration);
65 
66     snooze(engine->getConfiguration().getHtResizeInterval());
67     return true;
68 }
69