1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2017 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 "mock_synchronous_ep_engine.h"
19 
20 #include <checkpoint_remover.h>
21 #include <programs/engine_testapp/mock_server.h>
22 #include "dcp/dcpconnmap.h"
23 #include "dcp/flow-control-manager.h"
24 #include "replicationthrottle.h"
25 
26 #include <string>
27 
SynchronousEPEngine(std::string extra_config)28 SynchronousEPEngine::SynchronousEPEngine(std::string extra_config)
29     : EventuallyPersistentEngine(get_mock_server_api) {
30     // Tests may need to create multiple failover table entries, so allow that
31     maxFailoverEntries = 5;
32 
33     // Merge any extra config into the main configuration.
34     if (extra_config.size() > 0) {
35         if (!configuration.parseConfiguration(extra_config.c_str(),
36                                               serverApi)) {
37             throw std::invalid_argument("Unable to parse config string: " +
38                                         extra_config);
39         }
40     }
41 
42     // workload is needed by EPStore's constructor (to construct the
43     // VBucketMap).
44     workload = new WorkLoadPolicy(/*workers*/ 1, /*shards*/ 1);
45 
46     // dcpConnMap_ is needed by EPStore's constructor.
47     dcpConnMap_ = std::make_unique<DcpConnMap>(*this);
48 
49     // checkpointConfig is needed by CheckpointManager (via EPStore).
50     checkpointConfig = new CheckpointConfig(*this);
51 
52     dcpFlowControlManager_ = std::make_unique<DcpFlowControlManager>(*this);
53 
54     enableTraffic(true);
55 
56     maxItemSize = configuration.getMaxItemSize();
57 
58     setCompressionMode(configuration.getCompressionMode());
59 }
60 
setKVBucket(std::unique_ptr<KVBucket> store)61 void SynchronousEPEngine::setKVBucket(std::unique_ptr<KVBucket> store) {
62     cb_assert(kvBucket == nullptr);
63     kvBucket = std::move(store);
64 }
65 
setDcpConnMap( std::unique_ptr<DcpConnMap> dcpConnMap)66 void SynchronousEPEngine::setDcpConnMap(
67         std::unique_ptr<DcpConnMap> dcpConnMap) {
68     dcpConnMap_ = std::move(dcpConnMap);
69 }
70 
build( const std::string& config)71 std::unique_ptr<SynchronousEPEngine> SynchronousEPEngine::build(
72         const std::string& config) {
73     std::unique_ptr<SynchronousEPEngine> engine(
74             new SynchronousEPEngine(config));
75 
76     // switch current thread to this new engine, so all sub-created objects
77     // are accounted in it's mem_used.
78     ObjectRegistry::onSwitchThread(engine.get());
79 
80     engine->setKVBucket(
81             engine->public_makeMockBucket(engine->getConfiguration()));
82 
83     // Ensure that EPEngine is told about necessary server callbacks
84     // (client disconnect, bucket delete).
85     engine->public_initializeEngineCallbacks();
86 
87     return engine;
88 }
89 
initializeConnmap()90 void SynchronousEPEngine::initializeConnmap() {
91     dcpConnMap_->initialize();
92 }
93 
public_makeMockBucket( Configuration& config)94 std::unique_ptr<KVBucket> SynchronousEPEngine::public_makeMockBucket(
95         Configuration& config) {
96     const auto bucketType = config.getBucketType();
97     if (bucketType == "persistent") {
98         return std::make_unique<MockEPBucket>(*this);
99     } else if (bucketType == "ephemeral") {
100         EphemeralBucket::reconfigureForEphemeral(configuration);
101         return std::make_unique<MockEphemeralBucket>(*this);
102     }
103     throw std::invalid_argument(bucketType +
104                                 " is not a recognized bucket "
105                                 "type");
106 }
107