Loading...
Searching...
No Matches
transaction_executor.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#include <functional>
22#include <thread>
23
29#include "platform/proto/resdb.pb.h"
31
32namespace resdb {
33
34// Execute the requests that may contain system information or user requests.
36 public:
37 typedef std::function<void(std::unique_ptr<Request>,
38 std::unique_ptr<BatchUserResponse> resp)>
40 typedef std::function<void(Request*)> PreExecuteFunc;
41 typedef std::function<void(uint64_t seq)> SeqUpdateNotifyFunc;
42
43 TransactionExecutor(const ResDBConfig& config, PostExecuteFunc post_exec_func,
44 SystemInfo* system_info,
45 std::unique_ptr<TransactionManager> transaction_manager);
47
48 void Stop();
49
50 bool NeedResponse();
51
52 int Commit(std::unique_ptr<Request> request);
53
54 // The max seq S that can be executed (have received all the seq before S).
55 uint64_t GetMaxPendingExecutedSeq();
56
57 // When a transaction is ready to be executed (have received all the seq
58 // before Txn) PreExecute func will be called.
60
62
64
66
67 private:
68 void Execute(std::unique_ptr<Request> request, bool need_execute = true);
69 void OnlyExecute(std::unique_ptr<Request> request);
70
71 std::unique_ptr<std::string> DoExecute(const Request& request);
72 void OrderMessage();
73 void ExecuteMessage();
74 void ExecuteMessageOutOfOrder();
75
76 void AddNewData(std::unique_ptr<Request> message);
77 std::unique_ptr<Request> GetNextData();
78
79 bool IsStop();
80
81 void UpdateMaxExecutedSeq(uint64_t seq);
82
83 protected:
85
86 private:
87 std::atomic<uint64_t> next_execute_seq_ = 1;
88 PreExecuteFunc pre_exec_func_ = nullptr;
89 SeqUpdateNotifyFunc seq_update_notify_func_ = nullptr;
90 PostExecuteFunc post_exec_func_ = nullptr;
91 SystemInfo* system_info_ = nullptr;
92 std::unique_ptr<TransactionManager> transaction_manager_ = nullptr;
93 std::map<uint64_t, std::unique_ptr<Request>> candidates_;
94 std::thread ordering_thread_, execute_thread_, execute_OOO_thread_;
95 LockFreeQueue<Request> commit_queue_, execute_queue_, execute_OOO_queue_;
96 std::atomic<bool> stop_;
97 Stats* global_stats_ = nullptr;
98 DuplicateManager* duplicate_manager_;
99};
100
101} // namespace resdb
void SetDuplicateManager(DuplicateManager *manager)
std::function< void(uint64_t seq)> SeqUpdateNotifyFunc
void SetPreExecuteFunc(PreExecuteFunc func)
std::function< void(Request *)> PreExecuteFunc
void SetSeqUpdateNotifyFunc(SeqUpdateNotifyFunc func)
int Commit(std::unique_ptr< Request > request)
std::function< void(std::unique_ptr< Request >, std::unique_ptr< BatchUserResponse > resp)> PostExecuteFunc