Loading...
Searching...
No Matches
lock_free_collector_pool.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
21
22#include <glog/logging.h>
23
24namespace resdb {
25
26namespace {
27uint32_t GetCapacity(uint32_t size) {
28 int i = 0;
29 for (i = 0; (1u << i) <= size; ++i)
30 ;
31 return (1u << i);
32}
33} // namespace
34
36 uint32_t size,
37 TransactionExecutor* executor,
38 bool enable_viewchange)
39 : name_(name),
40 capacity_(GetCapacity(size * 2)),
41 mask_((capacity_ << 1) - 1),
42 executor_(executor),
43 enable_viewchange_(enable_viewchange) {
44 collector_.resize(capacity_ << 1);
45 for (size_t i = 0; i < (capacity_ << 1); ++i) {
46 collector_[i] = std::make_unique<TransactionCollector>(i, executor_,
47 enable_viewchange_);
48 }
49 LOG(ERROR) << "name:" << name_ << " create pool done. capacity:" << capacity_
50 << " enable viewchange:" << enable_viewchange_ << " done";
51}
52
54 uint32_t idx = seq & mask_;
55 if (collector_[idx]->Seq() != seq) {
56 LOG(ERROR) << "seq not match, skip update:" << seq;
57 return;
58 }
59 // LOG(ERROR)<<" update:"<<(idx^capacity_)<<" seq:"<<seq+capacity_<<
60 // " cap:"<<capacity_<<" update seq:"<<seq;
61 collector_[idx ^ capacity_] = std::make_unique<TransactionCollector>(
62 seq + capacity_, executor_, enable_viewchange_);
63}
64
66 uint32_t idx = seq & mask_;
67 return collector_[idx].get();
68}
69
70} // namespace resdb
TransactionCollector * GetCollector(uint64_t seq)
LockFreeCollectorPool(const std::string &name, uint32_t size, TransactionExecutor *executor, bool enable_viewchange=false)
ContractTransactionManager executor_