Skip to content

Commit 2feef4e

Browse files
committed
Use TSA macros.
1 parent 7c367e1 commit 2feef4e

2 files changed

Lines changed: 23 additions & 21 deletions

File tree

src/Interpreters/ContextTimeSeriesTagsCollector.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -831,11 +831,12 @@ void ContextTimeSeriesTagsCollector::extractTag(const std::vector<Group> & group
831831
template <typename IDType>
832832
void ContextTimeSeriesTagsCollector::storeTags(const IDType & id, const TagNamesAndValuesPtr & tags)
833833
{
834-
auto & groups_by_id = getIDMap<IDType>().groups_by_id;
835-
836834
{
837835
SharedLockGuard lock{mutex};
836+
837+
const auto & groups_by_id = getConstIDMap<IDType>().groups_by_id;
838838
auto it = groups_by_id.find(id);
839+
839840
if (it != groups_by_id.end())
840841
{
841842
Group existing_group = it->second;
@@ -849,7 +850,7 @@ void ContextTimeSeriesTagsCollector::storeTags(const IDType & id, const TagNames
849850
std::lock_guard lock{mutex};
850851

851852
Group group = tryAddGroupUnlocked(tags);
852-
853+
auto & groups_by_id = getIDMap<IDType>().groups_by_id;
853854
auto it = groups_by_id.try_emplace(id, group).first;
854855

855856
if (it->second != group)
@@ -862,14 +863,15 @@ template <typename IDType>
862863
void ContextTimeSeriesTagsCollector::storeTags(const std::vector<IDType> & ids, const std::vector<TagNamesAndValuesPtr> & tags_vector)
863864
{
864865
chassert(ids.size() == tags_vector.size());
865-
auto & groups_by_id = getIDMap<IDType>().groups_by_id;
866866

867867
std::vector<Group> found_groups;
868868
found_groups.resize(tags_vector.size(), INVALID_GROUP);
869869
size_t num_found_groups = 0;
870870

871871
{
872872
SharedLockGuard lock{mutex};
873+
const auto & groups_by_id = getConstIDMap<IDType>().groups_by_id;
874+
873875
for (size_t i = 0; i != tags_vector.size(); ++i)
874876
{
875877
const auto & id = ids[i];
@@ -891,6 +893,8 @@ void ContextTimeSeriesTagsCollector::storeTags(const std::vector<IDType> & ids,
891893

892894
{
893895
std::lock_guard lock{mutex};
896+
auto & groups_by_id = getIDMap<IDType>().groups_by_id;
897+
894898
for (size_t i = 0; i != tags_vector.size(); ++i)
895899
{
896900
if (found_groups[i] != INVALID_GROUP)
@@ -915,9 +919,8 @@ void ContextTimeSeriesTagsCollector::storeTags(const std::vector<IDType> & ids,
915919
template <typename IDType>
916920
Group ContextTimeSeriesTagsCollector::getGroupByID(const IDType & id) const
917921
{
918-
const auto & groups_by_id = getIDMap<IDType>().groups_by_id;
919-
920922
SharedLockGuard lock{mutex};
923+
const auto & groups_by_id = getConstIDMap<IDType>().groups_by_id;
921924

922925
auto it = groups_by_id.find(id);
923926
if (it == groups_by_id.end())
@@ -930,12 +933,12 @@ Group ContextTimeSeriesTagsCollector::getGroupByID(const IDType & id) const
930933
template <typename IDType>
931934
std::vector<Group> ContextTimeSeriesTagsCollector::getGroupByID(const std::vector<IDType> & ids) const
932935
{
933-
const auto & groups_by_id = getIDMap<IDType>().groups_by_id;
934-
935936
std::vector<Group> res;
936937
res.reserve(ids.size());
937938

938939
SharedLockGuard lock{mutex};
940+
const auto & groups_by_id = getConstIDMap<IDType>().groups_by_id;
941+
939942
for (const auto & id : ids)
940943
{
941944
auto it = groups_by_id.find(id);
@@ -951,9 +954,8 @@ std::vector<Group> ContextTimeSeriesTagsCollector::getGroupByID(const std::vecto
951954
template <typename IDType>
952955
TagNamesAndValuesPtr ContextTimeSeriesTagsCollector::getTagsByID(const IDType & id) const
953956
{
954-
const auto & groups_by_id = getIDMap<IDType>().groups_by_id;
955-
956957
SharedLockGuard lock{mutex};
958+
const auto & groups_by_id = getConstIDMap<IDType>().groups_by_id;
957959

958960
auto it = groups_by_id.find(id);
959961
if (it == groups_by_id.end())
@@ -965,12 +967,12 @@ TagNamesAndValuesPtr ContextTimeSeriesTagsCollector::getTagsByID(const IDType &
965967
template <typename IDType>
966968
std::vector<TagNamesAndValuesPtr> ContextTimeSeriesTagsCollector::getTagsByID(const std::vector<IDType> & ids) const
967969
{
968-
const auto & groups_by_id = getIDMap<IDType>().groups_by_id;
969-
970970
std::vector<TagNamesAndValuesPtr> res;
971971
res.reserve(ids.size());
972972

973973
SharedLockGuard lock{mutex};
974+
const auto & groups_by_id = getConstIDMap<IDType>().groups_by_id;
975+
974976
for (const auto & id : ids)
975977
{
976978
auto it = groups_by_id.find(id);
@@ -1243,9 +1245,9 @@ ContextTimeSeriesTagsCollector::IDMap<IDType> & ContextTimeSeriesTagsCollector::
12431245
}
12441246

12451247
template <typename IDType>
1246-
const ContextTimeSeriesTagsCollector::IDMap<IDType> & ContextTimeSeriesTagsCollector::getIDMap() const
1248+
const ContextTimeSeriesTagsCollector::IDMap<IDType> & ContextTimeSeriesTagsCollector::getConstIDMap() const
12471249
{
1248-
return const_cast<ContextTimeSeriesTagsCollector *>(this)->getIDMap<IDType>();
1250+
return TSA_SUPPRESS_WARNING_FOR_READ(const_cast<ContextTimeSeriesTagsCollector *>(this)->getIDMap<IDType>());
12491251
}
12501252

12511253

src/Interpreters/ContextTimeSeriesTagsCollector.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ class ContextTimeSeriesTagsCollector
156156

157157
/// Adds a group associated with a specified set of tags.
158158
/// If there is such a group already the function returns it.
159-
Group tryAddGroupUnlocked(const TagNamesAndValuesPtr & tags);
159+
Group tryAddGroupUnlocked(const TagNamesAndValuesPtr & tags) TSA_REQUIRES(mutex);
160160

161161
mutable SharedMutex mutex;
162162

163-
std::vector<TagNamesAndValuesPtr> groups;
163+
std::vector<TagNamesAndValuesPtr> groups TSA_GUARDED_BY(mutex);
164164

165165
struct Equal
166166
{
@@ -172,7 +172,7 @@ class ContextTimeSeriesTagsCollector
172172
size_t operator()(const TagNamesAndValuesPtr & ptr) const;
173173
};
174174

175-
std::unordered_map<TagNamesAndValuesPtr, Group, Hash, Equal> groups_for_tags;
175+
std::unordered_map<TagNamesAndValuesPtr, Group, Hash, Equal> groups_for_tags TSA_GUARDED_BY(mutex);
176176

177177
template <typename IDType>
178178
struct IDMap
@@ -181,13 +181,13 @@ class ContextTimeSeriesTagsCollector
181181
};
182182

183183
template <typename IDType>
184-
IDMap<IDType> & getIDMap();
184+
IDMap<IDType> & getIDMap() TSA_REQUIRES(mutex);
185185

186186
template <typename IDType>
187-
const IDMap<IDType> & getIDMap() const;
187+
const IDMap<IDType> & getConstIDMap() const TSA_REQUIRES_SHARED(mutex);
188188

189-
IDMap<UInt64> uint64_id_map;
190-
IDMap<UInt128> uint128_id_map;
189+
IDMap<UInt64> uint64_id_map TSA_GUARDED_BY(mutex);
190+
IDMap<UInt128> uint128_id_map TSA_GUARDED_BY(mutex);
191191
};
192192

193193
}

0 commit comments

Comments
 (0)