|
| 1 | +#include "special_cleaner.h" |
| 2 | + |
| 3 | +#include <ydb/core/tx/columnshard/columnshard_private_events.h> |
| 4 | + |
| 5 | +namespace NKikimr::NOlap::NNormalizer::NSpecialColumns { |
| 6 | + |
| 7 | +namespace { |
| 8 | + |
| 9 | +class TChanges: public INormalizerChanges { |
| 10 | +public: |
| 11 | + TChanges(TDeleteTrashImpl::TKeyBatch&& keys) |
| 12 | + : Keys(keys) { |
| 13 | + } |
| 14 | + bool ApplyOnExecute(NTabletFlatExecutor::TTransactionContext& txc, const TNormalizationController& /*normController*/) const override { |
| 15 | + using namespace NColumnShard; |
| 16 | + NIceDb::TNiceDb db(txc.DB); |
| 17 | + for (const auto& k : Keys) { |
| 18 | + db.Table<Schema::IndexColumns>().Key(k.Index, k.Granule, k.ColumnIdx, k.PlanStep, k.TxId, k.Portion, k.Chunk).Delete(); |
| 19 | + } |
| 20 | + AFL_NOTICE(NKikimrServices::TX_COLUMNSHARD)("normalizer", "TDeleteTrash")("message", TStringBuilder() << GetSize() << " rows deleted"); |
| 21 | + return true; |
| 22 | + } |
| 23 | + |
| 24 | + ui64 GetSize() const override { |
| 25 | + return Keys.size(); |
| 26 | + } |
| 27 | + |
| 28 | +private: |
| 29 | + const TDeleteTrashImpl::TKeyBatch Keys; |
| 30 | +}; |
| 31 | + |
| 32 | +} //namespace |
| 33 | + |
| 34 | +TConclusion<std::vector<INormalizerTask::TPtr>> TDeleteTrashImpl::DoInit( |
| 35 | + const TNormalizationController& /*controller*/, NTabletFlatExecutor::TTransactionContext& txc) { |
| 36 | + using namespace NColumnShard; |
| 37 | + NIceDb::TNiceDb db(txc.DB); |
| 38 | + const size_t MaxBatchSize = 10000; |
| 39 | + auto keysToDelete = KeysToDelete(txc, MaxBatchSize); |
| 40 | + if (!keysToDelete) { |
| 41 | + return TConclusionStatus::Fail("Not ready"); |
| 42 | + } |
| 43 | + ui32 removeCount = 0; |
| 44 | + for (auto&& i : *keysToDelete) { |
| 45 | + removeCount += i.size(); |
| 46 | + } |
| 47 | + AFL_NOTICE(NKikimrServices::TX_COLUMNSHARD)("normalizer", "TDeleteTrash")( |
| 48 | + "message", TStringBuilder() << "found " << removeCount << " rows to delete grouped in " << keysToDelete->size() << " batches"); |
| 49 | + |
| 50 | + std::vector<INormalizerTask::TPtr> result; |
| 51 | + for (auto&& batch : *keysToDelete) { |
| 52 | + AFL_VERIFY(!batch.empty()); |
| 53 | + result.emplace_back(std::make_shared<TTrivialNormalizerTask>(std::make_shared<TChanges>(std::move(batch)))); |
| 54 | + } |
| 55 | + return result; |
| 56 | +} |
| 57 | + |
| 58 | +std::optional<std::vector<TDeleteTrashImpl::TKeyBatch>> TDeleteTrashImpl::KeysToDelete( |
| 59 | + NTabletFlatExecutor::TTransactionContext& txc, const size_t maxBatchSize) { |
| 60 | + NIceDb::TNiceDb db(txc.DB); |
| 61 | + using namespace NColumnShard; |
| 62 | + if (!Schema::Precharge<Schema::IndexColumns>(db, txc.DB.GetScheme())) { |
| 63 | + return std::nullopt; |
| 64 | + } |
| 65 | + const std::set<ui64> columnIdsToDelete = GetColumnIdsToDelete(); |
| 66 | + std::vector<TKeyBatch> result; |
| 67 | + TKeyBatch currentBatch; |
| 68 | + auto rowset = |
| 69 | + db.Table<Schema::IndexColumns>() |
| 70 | + .Select<Schema::IndexColumns::Index, Schema::IndexColumns::Granule, Schema::IndexColumns::ColumnIdx, Schema::IndexColumns::PlanStep, |
| 71 | + Schema::IndexColumns::TxId, Schema::IndexColumns::Portion, Schema::IndexColumns::Chunk>(); |
| 72 | + if (!rowset.IsReady()) { |
| 73 | + return std::nullopt; |
| 74 | + } |
| 75 | + while (!rowset.EndOfSet()) { |
| 76 | + if (columnIdsToDelete.contains(rowset.GetValue<Schema::IndexColumns::ColumnIdx>())) { |
| 77 | + auto key = TKey{ |
| 78 | + .Index = rowset.GetValue<Schema::IndexColumns::Index>(), |
| 79 | + .Granule = rowset.GetValue<Schema::IndexColumns::Granule>(), |
| 80 | + .ColumnIdx = rowset.GetValue<Schema::IndexColumns::ColumnIdx>(), |
| 81 | + .PlanStep = rowset.GetValue<Schema::IndexColumns::PlanStep>(), |
| 82 | + .TxId = rowset.GetValue<Schema::IndexColumns::TxId>(), |
| 83 | + .Portion = rowset.GetValue<Schema::IndexColumns::Portion>(), |
| 84 | + .Chunk = rowset.GetValue<Schema::IndexColumns::Chunk>() }; |
| 85 | + currentBatch.emplace_back(std::move(key)); |
| 86 | + if (currentBatch.size() == maxBatchSize) { |
| 87 | + result.emplace_back(std::move(currentBatch)); |
| 88 | + currentBatch = TKeyBatch{}; |
| 89 | + } |
| 90 | + } |
| 91 | + if (!rowset.Next()) { |
| 92 | + return std::nullopt; |
| 93 | + } |
| 94 | + } |
| 95 | + if (!currentBatch.empty()) { |
| 96 | + result.emplace_back(std::move(currentBatch)); |
| 97 | + } |
| 98 | + |
| 99 | + return result; |
| 100 | +} |
| 101 | + |
| 102 | +} // namespace NKikimr::NOlap::NNormalizer::NSpecialColumns |
0 commit comments