|
| 1 | +#include "clean_empty.h" |
| 2 | +#include <ydb/core/tx/columnshard/columnshard_schema.h> |
| 3 | + |
| 4 | + |
| 5 | +namespace NKikimr::NOlap { |
| 6 | + |
| 7 | +namespace { |
| 8 | +std::optional<THashSet<TPortionAddress>> GetColumnPortionAddresses(NTabletFlatExecutor::TTransactionContext& txc) { |
| 9 | + using namespace NColumnShard; |
| 10 | + NIceDb::TNiceDb db(txc.DB); |
| 11 | + if (!Schema::Precharge<Schema::IndexColumns>(db, txc.DB.GetScheme())) { |
| 12 | + return std::nullopt; |
| 13 | + } |
| 14 | + THashSet<TPortionAddress> usedPortions; |
| 15 | + auto rowset = db.Table<Schema::IndexColumns>().Select< |
| 16 | + Schema::IndexColumns::PathId, |
| 17 | + Schema::IndexColumns::Portion |
| 18 | + >(); |
| 19 | + if (!rowset.IsReady()) { |
| 20 | + return std::nullopt; |
| 21 | + } |
| 22 | + while (!rowset.EndOfSet()) { |
| 23 | + usedPortions.emplace( |
| 24 | + rowset.GetValue<Schema::IndexColumns::PathId>(), |
| 25 | + rowset.GetValue<Schema::IndexColumns::Portion>() |
| 26 | + ); |
| 27 | + if (!rowset.Next()) { |
| 28 | + return std::nullopt; |
| 29 | + } |
| 30 | + } |
| 31 | + return usedPortions; |
| 32 | +} |
| 33 | + |
| 34 | +using TBatch = std::vector<TPortionAddress>; |
| 35 | + |
| 36 | +std::optional<std::vector<TBatch>> GetPortionsToDelete(NTabletFlatExecutor::TTransactionContext& txc) { |
| 37 | + using namespace NColumnShard; |
| 38 | + const auto usedPortions = GetColumnPortionAddresses(txc); |
| 39 | + if (!usedPortions) { |
| 40 | + return std::nullopt; |
| 41 | + } |
| 42 | + const size_t MaxBatchSize = 10000; |
| 43 | + NIceDb::TNiceDb db(txc.DB); |
| 44 | + if (!Schema::Precharge<Schema::IndexPortions>(db, txc.DB.GetScheme())) { |
| 45 | + return std::nullopt; |
| 46 | + } |
| 47 | + auto rowset = db.Table<Schema::IndexPortions>().Select< |
| 48 | + Schema::IndexPortions::PathId, |
| 49 | + Schema::IndexPortions::PortionId |
| 50 | + >(); |
| 51 | + if (!rowset.IsReady()) { |
| 52 | + return std::nullopt; |
| 53 | + } |
| 54 | + std::vector<TBatch> result; |
| 55 | + TBatch portionsToDelete; |
| 56 | + while (!rowset.EndOfSet()) { |
| 57 | + TPortionAddress addr( |
| 58 | + rowset.GetValue<Schema::IndexPortions::PathId>(), |
| 59 | + rowset.GetValue<Schema::IndexPortions::PortionId>() |
| 60 | + ); |
| 61 | + if (!usedPortions->contains(addr)) { |
| 62 | + ACFL_WARN("normalizer", "TCleanEmptyPortionsNormalizer")("message", TStringBuilder() << addr.DebugString() << " marked for deletion"); |
| 63 | + portionsToDelete.emplace_back(std::move(addr)); |
| 64 | + if (portionsToDelete.size() == MaxBatchSize) { |
| 65 | + result.emplace_back(std::move(portionsToDelete)); |
| 66 | + portionsToDelete = TBatch{}; |
| 67 | + } |
| 68 | + } |
| 69 | + if (!rowset.Next()) { |
| 70 | + return std::nullopt; |
| 71 | + } |
| 72 | + } |
| 73 | + if (!portionsToDelete.empty()) { |
| 74 | + result.emplace_back(std::move(portionsToDelete)); |
| 75 | + } |
| 76 | + return result; |
| 77 | +} |
| 78 | + |
| 79 | +class TChanges : public INormalizerChanges { |
| 80 | +public: |
| 81 | + TChanges(TBatch&& addresses) |
| 82 | + : Addresses(addresses) |
| 83 | + {} |
| 84 | + bool ApplyOnExecute(NTabletFlatExecutor::TTransactionContext& txc, const TNormalizationController&) const override { |
| 85 | + using namespace NColumnShard; |
| 86 | + NIceDb::TNiceDb db(txc.DB); |
| 87 | + for(const auto& a: Addresses) { |
| 88 | + db.Table<Schema::IndexPortions>().Key( |
| 89 | + a.GetPathId(), |
| 90 | + a.GetPortionId() |
| 91 | + ).Delete(); |
| 92 | + } |
| 93 | + ACFL_WARN("normalizer", "TCleanEmptyPortionsNormalizer")("message", TStringBuilder() << GetSize() << " portions deleted"); |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + ui64 GetSize() const override { |
| 98 | + return Addresses.size(); |
| 99 | + } |
| 100 | +private: |
| 101 | + const TBatch Addresses; |
| 102 | +}; |
| 103 | + |
| 104 | +} //namespace |
| 105 | + |
| 106 | +TConclusion<std::vector<INormalizerTask::TPtr>> TCleanEmptyPortionsNormalizer::DoInit(const TNormalizationController&, NTabletFlatExecutor::TTransactionContext& txc) { |
| 107 | + using namespace NColumnShard; |
| 108 | + auto batchesToDelete = GetPortionsToDelete(txc); |
| 109 | + if (!batchesToDelete) { |
| 110 | + return TConclusionStatus::Fail("Not ready"); |
| 111 | + } |
| 112 | + |
| 113 | + std::vector<INormalizerTask::TPtr> result; |
| 114 | + for (auto&& b: *batchesToDelete) { |
| 115 | + result.emplace_back(std::make_shared<TTrivialNormalizerTask>(std::make_shared<TChanges>(std::move(b)))); |
| 116 | + } |
| 117 | + return result; |
| 118 | +} |
| 119 | + |
| 120 | +} //namespace NKikimr::NOlap |
0 commit comments