Skip to content

Commit e6ad7c8

Browse files
authored
replication rules - actual delete by db cleaner dfs 2353 (#9024)
Signed-off-by: Amit Prinz Setter <alphaprinz@gmail.com>
1 parent c7ecdb9 commit e6ad7c8

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

src/server/bg_services/db_cleaner.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const config = require('../../../config');
88
const MDStore = require('../object_services/md_store').MDStore;
99
const system_store = require('../system_services/system_store').get_instance();
1010
const nodes_store = require('../node_services/nodes_store').NodesStore.instance();
11+
const replication_store = require('../system_services/replication_store').instance();
1112
const system_utils = require('../utils/system_utils');
1213
const db_client = require('../../util/db_client');
1314
const md_aggregator = require('./md_aggregator');
@@ -47,6 +48,7 @@ async function background_worker() {
4748
await clean_md_store(last_date_to_remove);
4849
await clean_nodes_store(last_date_to_remove);
4950
await clean_system_store(last_date_to_remove);
51+
await clean_replication_store(last_date_to_remove);
5052
dbg.log0('DB_CLEANER:', 'END');
5153
return config.DB_CLEANER_CYCLE;
5254
}
@@ -107,6 +109,25 @@ async function clean_nodes_store(last_date_to_remove) {
107109
dbg.log0(`DB_CLEANER: removed ${filtered_nodes.length} documents from nodes-store`);
108110
}
109111

112+
async function clean_replication_store(last_date_to_remove) {
113+
const total_replication_rules_count = await replication_store.count_total_replication_rules();
114+
if (total_replication_rules_count < config.DB_CLEANER_MAX_TOTAL_DOCS) {
115+
dbg.log0(`DB_CLEANER: found less than ${config.DB_CLEANER_MAX_TOTAL_DOCS} replication rules in replication-store
116+
${total_replication_rules_count} replication rules - Skipping...`);
117+
return;
118+
}
119+
dbg.log0('DB_CLEANER: checking replication-store for replication rules deleted before', new Date(last_date_to_remove));
120+
const replication_rules = await replication_store.find_deleted_rules(last_date_to_remove, config.DB_CLEANER_DOCS_LIMIT);
121+
if (replication_rules.length === 0) {
122+
dbg.log0("No replication rules to delete.");
123+
return;
124+
}
125+
const rr_ids = db_client.instance().uniq_ids(replication_rules, '_id');
126+
dbg.log0('DB_CLEANER: found deleted replication rules:', rr_ids);
127+
await replication_store.actual_delete_replication_by_id(rr_ids);
128+
dbg.log0(`DB_CLEANER: removed ${rr_ids.length} replication rules from replication-store`);
129+
}
130+
110131
async function clean_system_store(last_date_to_remove) {
111132
const total_accounts_count = await system_store.count_total_docs('accounts');
112133
const total_buckets_count = await system_store.count_total_docs('buckets');

src/server/system_services/bucket_server.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ async function delete_bucket_and_objects(req) {
963963

964964
if (bucket.replication_policy_id) {
965965
// delete replication from replication collection
966-
await replication_store.instance().delete_replication_by_id(bucket.replication_policy_id);
966+
await replication_store.instance().mark_deleted_replication_by_id(bucket.replication_policy_id);
967967
}
968968

969969
const req_account = req.account &&
@@ -1027,7 +1027,7 @@ async function delete_bucket(req) {
10271027
});
10281028
if (bucket.replication_policy_id) {
10291029
// delete replication from replication collection
1030-
await replication_store.instance().delete_replication_by_id(bucket.replication_policy_id);
1030+
await replication_store.instance().mark_deleted_replication_by_id(bucket.replication_policy_id);
10311031
}
10321032

10331033
await BucketStatsStore.instance().delete_stats({
@@ -2079,7 +2079,7 @@ async function delete_bucket_replication(req) {
20792079
});
20802080

20812081
// delete replication from replication collection
2082-
await replication_store.instance().delete_replication_by_id(replication_id);
2082+
await replication_store.instance().mark_deleted_replication_by_id(replication_id);
20832083
}
20842084

20852085
async function validate_replication(req) {

src/server/system_services/replication_store.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,23 @@ class ReplicationStore {
5959
return repl;
6060
}
6161

62+
async find_deleted_rules(last_date_to_remove, limit) {
63+
const repl = this._replicationconfigs.find({
64+
deleted: {
65+
$lt: last_date_to_remove
66+
},
67+
}, { limit, projection: { _id: 1, deleted: 1 } });
68+
return repl;
69+
}
70+
6271
async get_replication_by_id(replication_id) {
6372
dbg.log1('get_replication_by_id: ', replication_id);
6473
const repl = await this._replicationconfigs.findOne({ _id: db_client.instance().parse_object_id(replication_id), deleted: null });
6574
return repl && repl.rules;
6675
}
6776

68-
async delete_replication_by_id(_id) {
69-
dbg.log1('delete_replication_by_id: ', _id);
77+
async mark_deleted_replication_by_id(_id) {
78+
dbg.log0('mark_deleted_replication_by_id: ', _id);
7079
const ans = await this._replicationconfigs.updateOne({
7180
_id: db_client.instance().parse_object_id(_id),
7281
deleted: null
@@ -78,6 +87,17 @@ class ReplicationStore {
7887
return ans;
7988
}
8089

90+
async actual_delete_replication_by_id(ids) {
91+
dbg.log0('actual_delete_replication_by_ids: ', ids);
92+
if (!ids || !ids.length) return;
93+
return this._replicationconfigs.deleteMany({
94+
_id: {
95+
$in: ids
96+
},
97+
deleted: { $exists: true }
98+
});
99+
}
100+
81101
async update_replication_status_by_id(_id, rule_id, status) {
82102
dbg.log1('update_replication_status_by_id: ', _id, rule_id, status);
83103
const parsed_id = db_client.instance().parse_object_id(_id);
@@ -153,6 +173,10 @@ class ReplicationStore {
153173
return reduced_replications;
154174
}
155175

176+
async count_total_replication_rules() {
177+
return this._replicationconfigs.countDocuments({});
178+
}
179+
156180
}
157181

158182
// EXPORTS

0 commit comments

Comments
 (0)