Loading...
Searching...
No Matches
transaction_collector.h
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
20#pragma once
21
22#include <bitset>
23
26#include "platform/proto/resdb.pb.h"
28
29namespace resdb {
30
39
41 std::unique_ptr<Request> request;
42 SignatureInfo signature;
43};
44
45template <typename T>
47 public:
48 AtomicUniquePtr() : v_(0) {}
49 bool Set(std::unique_ptr<T>& new_ptr) {
50 int old_v = 0;
51 bool res = v_.compare_exchange_strong(old_v, 1, std::memory_order_acq_rel,
52 std::memory_order_acq_rel);
53 if (!res) {
54 return false;
55 }
56 ptr_ = std::move(new_ptr);
57 v_ = 2;
58 return true;
59 }
60
61 T* Reference() {
62 int v = v_.load(std::memory_order_acq_rel);
63 if (v <= 1) {
64 return nullptr;
65 }
66 return ptr_.get();
67 }
68
69 void Clear() {
70 v_ = 0;
71 ptr_ = nullptr;
72 }
73
74 private:
75 std::unique_ptr<T> ptr_;
76 std::atomic<int> v_;
77};
78
80 public:
82 bool enable_viewchange = false)
83 : seq_(seq),
84 executor_(executor),
85 status_(TransactionStatue::None),
86 enable_viewchange_(enable_viewchange),
87 view_(0) {}
88
90
91 // TODO split the context list.
92 // context contains the client channel used for sending back the response.
93 int SetContextList(uint64_t seq,
94 std::vector<std::unique_ptr<Context>> context);
95 bool HasClientContextList(uint64_t seq) const;
96 std::vector<std::unique_ptr<Context>> FetchContextList(uint64_t seq);
97
98 typedef std::list<std::unique_ptr<RequestInfo>> CollectorDataType;
99
100 // Add a message and count by its hash value.
101 // After it is done call_back will be triggered.
102 int AddRequest(
103 std::unique_ptr<Request> request, const SignatureInfo& signature,
104 bool is_main_request,
105 std::function<void(const Request&, int received_count,
107 std::atomic<TransactionStatue>* status, bool force)>
108 call_back);
109
110 std::vector<RequestInfo> GetPreparedProof();
112
113 uint64_t Seq();
114
115 bool IsPrepared();
116
117 std::vector<std::string> GetAllStoredHash();
118
119 private:
120 int Commit();
121
122 private:
123 uint64_t seq_;
124 TransactionExecutor* executor_;
125 std::atomic<bool> is_committed_ = false;
126 std::atomic<bool> is_prepared_ = false;
127 std::vector<std::unique_ptr<Context>> context_list_;
128 std::map<std::string, std::list<std::unique_ptr<RequestInfo>>>
129 data_[Request::NUM_OF_TYPE];
130 std::vector<std::unique_ptr<RequestInfo>> prepared_proof_;
131 AtomicUniquePtr<RequestInfo> atomic_mian_request_;
132 std::atomic<TransactionStatue> status_ = TransactionStatue::None;
133 bool enable_viewchange_;
134 std::mutex mutex_;
135 std::vector<SignatureInfo> commit_certs_;
136 std::map<std::string, std::bitset<128>> senders_[Request::NUM_OF_TYPE];
137 std::set<std::unique_ptr<RequestInfo>> other_main_request_;
138 uint64_t view_;
139};
140
141} // namespace resdb
std::string data
bool Set(std::unique_ptr< T > &new_ptr)
std::vector< RequestInfo > GetPreparedProof()
int SetContextList(uint64_t seq, std::vector< std::unique_ptr< Context > > context)
TransactionCollector(uint64_t seq, TransactionExecutor *executor, bool enable_viewchange=false)
std::vector< std::unique_ptr< Context > > FetchContextList(uint64_t seq)
int AddRequest(std::unique_ptr< Request > request, const SignatureInfo &signature, bool is_main_request, std::function< void(const Request &, int received_count, CollectorDataType *data, std::atomic< TransactionStatue > *status, bool force)> call_back)
std::list< std::unique_ptr< RequestInfo > > CollectorDataType
bool HasClientContextList(uint64_t seq) const
std::vector< std::string > GetAllStoredHash()
TransactionStatue GetStatus() const
std::unique_ptr< Request > request