Loading...
Searching...
No Matches
duplicate_manager.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
24#include "common/utils/utils.h"
25namespace resdb {
27 : config_(config) {
28 if (config.GetConfigData().duplicate_check_frequency_useconds() > 0) {
29 frequency_useconds_ =
30 config.GetConfigData().duplicate_check_frequency_useconds();
31 }
32 stop_ = false;
33 update_thread_ = std::thread(&DuplicateManager::UpdateRecentHash, this);
34}
35
37 stop_ = true;
38 if (update_thread_.joinable()) {
39 update_thread_.join();
40 }
41}
42
43bool DuplicateManager::IsStop() { return stop_; }
44
45bool DuplicateManager::CheckIfProposed(const std::string& hash) {
46 std::lock_guard<std::mutex> lk(prop_mutex_);
47 return proposed_hash_set_.find(hash) != proposed_hash_set_.end();
48}
49
50uint64_t DuplicateManager::CheckIfExecuted(const std::string& hash) {
51 std::lock_guard<std::mutex> lk(exec_mutex_);
52 if (executed_hash_set_.find(hash) != executed_hash_set_.end()) {
53 return executed_hash_seq_[hash];
54 } else {
55 return 0;
56 }
57}
58
59void DuplicateManager::AddProposed(const std::string& hash) {
60 std::lock_guard<std::mutex> lk(prop_mutex_);
61 proposed_hash_time_queue_.push(std::make_pair(hash, GetCurrentTime()));
62 proposed_hash_set_.insert(hash);
63}
64
65void DuplicateManager::AddExecuted(const std::string& hash, uint64_t seq) {
66 std::lock_guard<std::mutex> lk(exec_mutex_);
67 executed_hash_time_queue_.push(std::make_pair(hash, GetCurrentTime()));
68 executed_hash_set_.insert(hash);
69 executed_hash_seq_[hash] = seq;
70}
71
72bool DuplicateManager::CheckAndAddProposed(const std::string& hash) {
73 std::lock_guard<std::mutex> lk(prop_mutex_);
74 if (proposed_hash_set_.find(hash) != proposed_hash_set_.end()) {
75 return true;
76 }
77 proposed_hash_time_queue_.push(std::make_pair(hash, GetCurrentTime()));
78 proposed_hash_set_.insert(hash);
79 return false;
80}
81
82bool DuplicateManager::CheckAndAddExecuted(const std::string& hash,
83 uint64_t seq) {
84 std::lock_guard<std::mutex> lk(exec_mutex_);
85 if (executed_hash_set_.find(hash) != executed_hash_set_.end()) {
86 return true;
87 }
88 executed_hash_time_queue_.push(std::make_pair(hash, GetCurrentTime()));
89 executed_hash_set_.insert(hash);
90 executed_hash_seq_[hash] = seq;
91 return false;
92}
93
94void DuplicateManager::EraseProposed(const std::string& hash) {
95 std::lock_guard<std::mutex> lk(prop_mutex_);
96 proposed_hash_set_.erase(hash);
97}
98
99void DuplicateManager::EraseExecuted(const std::string& hash) {
100 std::lock_guard<std::mutex> lk(exec_mutex_);
101 executed_hash_set_.erase(hash);
102 executed_hash_seq_.erase(hash);
103}
104
106 uint64_t time = GetCurrentTime();
107 while (!IsStop()) {
108 time = time + frequency_useconds_;
109 auto sleep_time = time - GetCurrentTime();
110 usleep(sleep_time);
111 while (true) {
112 std::lock_guard<std::mutex> lk(prop_mutex_);
113 if (!proposed_hash_time_queue_.empty()) {
114 auto it = proposed_hash_time_queue_.front();
115 if (it.second + window_useconds_ < time) {
116 proposed_hash_time_queue_.pop();
117 proposed_hash_set_.erase(it.first);
118 } else {
119 break;
120 }
121 } else {
122 break;
123 }
124 }
125
126 while (true) {
127 std::lock_guard<std::mutex> lk(exec_mutex_);
128 if (!executed_hash_time_queue_.empty()) {
129 auto it = executed_hash_time_queue_.front();
130 if (it.second + window_useconds_ < time) {
131 executed_hash_time_queue_.pop();
132 executed_hash_set_.erase(it.first);
133 executed_hash_seq_.erase(it.first);
134 } else {
135 break;
136 }
137 } else {
138 break;
139 }
140 }
141 }
142}
143} // namespace resdb
bool CheckAndAddExecuted(const std::string &hash, uint64_t seq)
void AddProposed(const std::string &hash)
void AddExecuted(const std::string &hash, uint64_t seq)
DuplicateManager(const ResDBConfig &config)
bool CheckAndAddProposed(const std::string &hash)
void EraseProposed(const std::string &hash)
bool CheckIfProposed(const std::string &hash)
uint64_t CheckIfExecuted(const std::string &hash)
void EraseExecuted(const std::string &hash)
ResConfigData GetConfigData() const
uint64_t GetCurrentTime()
Definition utils.cpp:36
std::unique_ptr< ResDBConfig > config_