Skip to content

Commit

Permalink
lock names usage correction (ydb-platform#2591)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanmorozov333 authored Mar 11, 2024
1 parent 2c454f8 commit 3a6ff80
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 54 deletions.
24 changes: 15 additions & 9 deletions ydb/core/tx/columnshard/data_locks/locks/abstract.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#pragma once
#include <ydb/library/accessor/accessor.h>
#include <vector>

#include <util/generic/string.h>

#include <optional>
#include <memory>
#include <vector>

namespace NKikimr::NOlap {
class TPortionInfo;
Expand All @@ -12,29 +16,31 @@ namespace NKikimr::NOlap::NDataLocks {

class ILock {
private:
YDB_READONLY_DEF(TString, LockName);
YDB_READONLY_FLAG(ReadOnly, false);
protected:
virtual bool DoIsLocked(const TPortionInfo& portion) const = 0;
virtual bool DoIsLocked(const TGranuleMeta& granule) const = 0;
virtual std::optional<TString> DoIsLocked(const TPortionInfo& portion) const = 0;
virtual std::optional<TString> DoIsLocked(const TGranuleMeta& granule) const = 0;
virtual bool DoIsEmpty() const = 0;
public:
ILock(const bool isReadOnly = false)
: ReadOnlyFlag(isReadOnly)
ILock(const TString& lockName, const bool isReadOnly = false)
: LockName(lockName)
, ReadOnlyFlag(isReadOnly)
{

}

virtual ~ILock() = default;

bool IsLocked(const TPortionInfo& portion, const bool readOnly = false) const {
std::optional<TString> IsLocked(const TPortionInfo& portion, const bool readOnly = false) const {
if (IsReadOnly() && readOnly) {
return false;
return {};
}
return DoIsLocked(portion);
}
bool IsLocked(const TGranuleMeta& g, const bool readOnly = false) const {
std::optional<TString> IsLocked(const TGranuleMeta& g, const bool readOnly = false) const {
if (IsReadOnly() && readOnly) {
return false;
return {};
}
return DoIsLocked(g);
}
Expand Down
24 changes: 12 additions & 12 deletions ydb/core/tx/columnshard/data_locks/locks/composite.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ class TCompositeLock: public ILock {
using TBase = ILock;
std::vector<std::shared_ptr<ILock>> Locks;
protected:
virtual bool DoIsLocked(const TPortionInfo& portion) const override {
virtual std::optional<TString> DoIsLocked(const TPortionInfo& portion) const override {
for (auto&& i : Locks) {
if (i->IsLocked(portion)) {
return true;
if (auto lockName = i->IsLocked(portion)) {
return lockName;
}
}
return false;
return {};
}
virtual bool DoIsLocked(const TGranuleMeta& granule) const override {
virtual std::optional<TString> DoIsLocked(const TGranuleMeta& granule) const override {
for (auto&& i : Locks) {
if (i->IsLocked(granule)) {
return true;
if (auto lockName = i->IsLocked(granule)) {
return lockName;
}
}
return false;
return {};
}
bool DoIsEmpty() const override {
return Locks.empty();
}
public:
TCompositeLock(const std::vector<std::shared_ptr<ILock>>& locks, const bool readOnly = false)
: TBase(readOnly)
TCompositeLock(const TString& lockName, const std::vector<std::shared_ptr<ILock>>& locks, const bool readOnly = false)
: TBase(lockName, readOnly)
{
for (auto&& l : locks) {
if (!l || l->IsEmpty()) {
Expand All @@ -39,8 +39,8 @@ class TCompositeLock: public ILock {
}
}

TCompositeLock(std::initializer_list<std::shared_ptr<ILock>> locks, const bool readOnly = false)
: TBase(readOnly)
TCompositeLock(const TString& lockName, std::initializer_list<std::shared_ptr<ILock>> locks, const bool readOnly = false)
: TBase(lockName, readOnly)
{
for (auto&& l : locks) {
if (!l || l->IsEmpty()) {
Expand Down
30 changes: 18 additions & 12 deletions ydb/core/tx/columnshard/data_locks/locks/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,42 @@ class TListPortionsLock: public ILock {
THashSet<TPortionAddress> Portions;
THashSet<TTabletId> Granules;
protected:
virtual bool DoIsLocked(const TPortionInfo& portion) const override {
return Portions.contains(portion.GetAddress());
virtual std::optional<TString> DoIsLocked(const TPortionInfo& portion) const override {
if (Portions.contains(portion.GetAddress())) {
return GetLockName();
}
return {};
}
virtual bool DoIsLocked(const TGranuleMeta& granule) const override {
return Granules.contains((TTabletId)granule.GetPathId());
virtual std::optional<TString> DoIsLocked(const TGranuleMeta& granule) const override {
if (Granules.contains((TTabletId)granule.GetPathId())) {
return GetLockName();
}
return {};
}
bool DoIsEmpty() const override {
return Portions.empty();
}
public:
TListPortionsLock(const std::vector<std::shared_ptr<TPortionInfo>>& portions, const bool readOnly = false)
: TBase(readOnly)
TListPortionsLock(const TString& lockName, const std::vector<std::shared_ptr<TPortionInfo>>& portions, const bool readOnly = false)
: TBase(lockName, readOnly)
{
for (auto&& p : portions) {
Portions.emplace(p->GetAddress());
Granules.emplace((TTabletId)p->GetPathId());
}
}

TListPortionsLock(const std::vector<TPortionInfo>& portions, const bool readOnly = false)
: TBase(readOnly) {
TListPortionsLock(const TString& lockName, const std::vector<TPortionInfo>& portions, const bool readOnly = false)
: TBase(lockName, readOnly) {
for (auto&& p : portions) {
Portions.emplace(p.GetAddress());
Granules.emplace((TTabletId)p.GetPathId());
}
}

template <class T, class TGetter>
TListPortionsLock(const std::vector<T>& portions, const TGetter& g, const bool readOnly = false)
: TBase(readOnly) {
TListPortionsLock(const TString& lockName, const std::vector<T>& portions, const TGetter& g, const bool readOnly = false)
: TBase(lockName, readOnly) {
for (auto&& p : portions) {
const auto address = g(p);
Portions.emplace(address);
Expand All @@ -49,8 +55,8 @@ class TListPortionsLock: public ILock {
}

template <class T>
TListPortionsLock(const THashMap<TPortionAddress, T>& portions, const bool readOnly = false)
: TBase(readOnly) {
TListPortionsLock(const TString& lockName, const THashMap<TPortionAddress, T>& portions, const bool readOnly = false)
: TBase(lockName, readOnly) {
for (auto&& p : portions) {
const auto address = p.first;
Portions.emplace(address);
Expand Down
18 changes: 12 additions & 6 deletions ydb/core/tx/columnshard/data_locks/locks/snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ class TSnapshotLock: public ILock {
const TSnapshot SnapshotBarrier;
const THashSet<TTabletId> PathIds;
protected:
virtual bool DoIsLocked(const TPortionInfo& portion) const override {
return PathIds.contains((TTabletId)portion.GetPathId()) && portion.RecordSnapshotMin() <= SnapshotBarrier;
virtual std::optional<TString> DoIsLocked(const TPortionInfo& portion) const override {
if (PathIds.contains((TTabletId)portion.GetPathId()) && portion.RecordSnapshotMin() <= SnapshotBarrier) {
return GetLockName();
}
return {};
}
virtual bool DoIsLocked(const TGranuleMeta& granule) const override {
return PathIds.contains((TTabletId)granule.GetPathId());
virtual std::optional<TString> DoIsLocked(const TGranuleMeta& granule) const override {
if (PathIds.contains((TTabletId)granule.GetPathId())) {
return GetLockName();
}
return {};
}
public:
TSnapshotLock(const TSnapshot& snapshotBarrier, const THashSet<TTabletId>& pathIds, const bool readOnly = false)
: TBase(readOnly)
TSnapshotLock(const TString& lockName, const TSnapshot& snapshotBarrier, const THashSet<TTabletId>& pathIds, const bool readOnly = false)
: TBase(lockName, readOnly)
, SnapshotBarrier(snapshotBarrier)
, PathIds(pathIds)
{
Expand Down
13 changes: 7 additions & 6 deletions ydb/core/tx/columnshard/data_locks/manager/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

namespace NKikimr::NOlap::NDataLocks {

void TManager::RegisterLock(const TString& processId, const std::shared_ptr<ILock>& lock) {
AFL_VERIFY(ProcessLocks.emplace(processId, lock).second)("process_id", processId);
void TManager::RegisterLock(const std::shared_ptr<ILock>& lock) {
AFL_VERIFY(lock);
AFL_VERIFY(ProcessLocks.emplace(lock->GetLockName(), lock).second)("process_id", lock->GetLockName());
}

void TManager::UnregisterLock(const TString& processId) {
Expand All @@ -13,17 +14,17 @@ void TManager::UnregisterLock(const TString& processId) {

std::optional<TString> TManager::IsLocked(const TPortionInfo& portion) const {
for (auto&& i : ProcessLocks) {
if (i.second->IsLocked(portion)) {
return i.first;
if (auto lockName = i.second->IsLocked(portion)) {
return lockName;
}
}
return {};
}

std::optional<TString> TManager::IsLocked(const TGranuleMeta& granule) const {
for (auto&& i : ProcessLocks) {
if (i.second->IsLocked(granule)) {
return i.first;
if (auto lockName = i.second->IsLocked(granule)) {
return lockName;
}
}
return {};
Expand Down
6 changes: 3 additions & 3 deletions ydb/core/tx/columnshard/data_locks/manager/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class TManager {
public:
TManager() = default;

void RegisterLock(const TString& processId, const std::shared_ptr<ILock>& lock);
void RegisterLock(const std::shared_ptr<ILock>& lock);
template <class TLock, class ...Args>
void RegisterLock(const TString& processId, Args&&... args) {
RegisterLock(processId, std::make_shared<TLock>(args...));
void RegisterLock(Args&&... args) {
RegisterLock(std::make_shared<TLock>(args...));
}
void UnregisterLock(const TString& processId);
std::optional<TString> IsLocked(const TPortionInfo& portion) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void TColumnEngineChanges::Abort(NColumnShard::TColumnShard& self, TChangesFinis
}

void TColumnEngineChanges::Start(NColumnShard::TColumnShard& self) {
self.DataLocksManager->RegisterLock(TypeString() + "::" + GetTaskIdentifier(), BuildDataLock());
self.DataLocksManager->RegisterLock(BuildDataLock());
Y_ABORT_UNLESS(Stage == EStage::Created);
DoStart(self);
Stage = EStage::Started;
Expand Down
2 changes: 1 addition & 1 deletion ydb/core/tx/columnshard/engines/changes/cleanup.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TCleanupColumnEngineChanges: public TColumnEngineChanges {
return 0;
}
virtual std::shared_ptr<NDataLocks::ILock> DoBuildDataLock() const override {
return std::make_shared<NDataLocks::TListPortionsLock>(PortionsToDrop);
return std::make_shared<NDataLocks::TListPortionsLock>(TypeString() + "::" + GetTaskIdentifier(), PortionsToDrop);
}

public:
Expand Down
2 changes: 1 addition & 1 deletion ydb/core/tx/columnshard/engines/changes/compaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TCompactColumnEngineChanges: public TChangesWithAppend {
NeedGranuleStatusProvide = false;
}
virtual std::shared_ptr<NDataLocks::ILock> DoBuildDataLockImpl() const override {
return std::make_shared<NDataLocks::TListPortionsLock>(SwitchedPortions);
return std::make_shared<NDataLocks::TListPortionsLock>(TypeString() + "::" + GetTaskIdentifier(), SwitchedPortions);
}

public:
Expand Down
2 changes: 1 addition & 1 deletion ydb/core/tx/columnshard/engines/changes/ttl.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class TTTLColumnEngineChanges: public TChangesWithAppend {
const auto pred = [](const TPortionForEviction& p) {
return p.GetPortionInfo().GetAddress();
};
return std::make_shared<NDataLocks::TListPortionsLock>(PortionsToEvict, pred);
return std::make_shared<NDataLocks::TListPortionsLock>(TypeString() + "::" + GetTaskIdentifier(), PortionsToEvict, pred);
}
public:
class TMemoryPredictorSimplePolicy: public IMemoryPredictor {
Expand Down
9 changes: 7 additions & 2 deletions ydb/core/tx/columnshard/engines/changes/with_appended.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ class TChangesWithAppend: public TColumnEngineChanges {

virtual std::shared_ptr<NDataLocks::ILock> DoBuildDataLock() const override final {
auto actLock = DoBuildDataLockImpl();
auto selfLock = std::make_shared<NDataLocks::TListPortionsLock>(PortionsToRemove);
return std::make_shared<NDataLocks::TCompositeLock>(std::vector<std::shared_ptr<NDataLocks::ILock>>({actLock, selfLock}));
if (actLock) {
auto selfLock = std::make_shared<NDataLocks::TListPortionsLock>(TypeString() + "::" + GetTaskIdentifier() + "::REMOVE", PortionsToRemove);
return std::make_shared<NDataLocks::TCompositeLock>(TypeString() + "::" + GetTaskIdentifier(), std::vector<std::shared_ptr<NDataLocks::ILock>>({actLock, selfLock}));
} else {
auto selfLock = std::make_shared<NDataLocks::TListPortionsLock>(TypeString() + "::" + GetTaskIdentifier(), PortionsToRemove);
return selfLock;
}
}

public:
Expand Down

0 comments on commit 3a6ff80

Please sign in to comment.