Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions src/server/bg_services/buckets_reclaimer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class BucketsReclaimer {
}

let has_errors = false;
this._reset_delete_lists();
dbg.log0('bucket_reclaimer: starting batch work on buckets: ', deleting_buckets.map(b => b.name).join(', '));
await P.all(deleting_buckets.map(async bucket => {
try {
Expand All @@ -43,13 +44,7 @@ class BucketsReclaimer {
});
if (is_empty) {
dbg.log0(`bucket ${bucket.name} is empty. calling delete_bucket`);
await this.client.bucket.delete_bucket({ name: bucket.name, internal_call: true }, {
auth_token: auth_server.make_auth_token({
system_id: system._id,
account_id: system.owner._id,
role: 'admin'
})
});
this._add_bucket_to_delete_lists(bucket);
} else {
dbg.log0(`bucket ${bucket.name} is not empty yet`);
}
Expand All @@ -58,6 +53,26 @@ class BucketsReclaimer {
has_errors = true;
}
}));
if (this.buckets_to_delete.length > 0) {
const changes = {
remove: {
buckets: this.buckets_to_delete,
tieringpolicies: this.tiering_to_delete,
tiers: this.tiers_to_delete,
},
};
if (this.account_updates.length > 0) {
changes.update = {
accounts: this.account_updates,
};
}
try {
await system_store.make_changes(changes);
} catch (err) {
dbg.error(`got error when trying to update DB :`, err);
has_errors = true;
}
}

if (has_errors) {
return config.BUCKET_RECLAIMER_ERROR_DELAY;
Expand All @@ -83,6 +98,30 @@ class BucketsReclaimer {
return system_store.data.buckets.filter(bucket => Boolean(bucket.deleting));
}

_add_bucket_to_delete_lists(bucket) {
this.buckets_to_delete.push(bucket._id);
const tiering_policy = bucket.tiering;
this.tiering_to_delete.push(tiering_policy._id);
this.tiers_to_delete.push(...tiering_policy.tiers.map(t => t.tier._id));
system_store.data.accounts.forEach(account => {
if (!account.allowed_buckets || (account.allowed_buckets && account.allowed_buckets.full_permission)) return;
if (!account.allowed_buckets.permission_list.includes(bucket)) return;
this.account_updates.push({
_id: account._id,
$pullAll: {
'allowed_buckets.permission_list': [bucket._id]
}
});
});
}

_reset_delete_lists() {
this.buckets_to_delete = [];
this.tiering_to_delete = [];
this.tiers_to_delete = [];
this.account_updates = [];
}

}


Expand Down
6 changes: 5 additions & 1 deletion src/server/system_services/system_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,10 @@ class SystemStore extends EventEmitter {
data.check_indexes(col, item);
let dont_change_last_update = Boolean(item.dont_change_last_update);
let updates = _.omit(item, '_id', '$find', 'dont_change_last_update');
let finds = item.$find || _.pick(item, '_id');
let find_id = _.pick(item, '_id');
let finds = item.$find || (mongo_utils.is_object_id(find_id._id) && find_id);
if (_.isEmpty(updates)) return;
if (!finds) throw new Error(`SystemStore: make_changes id is not of type object_id: ${find_id._id}`);
let keys = _.keys(updates);

if (_.first(keys)[0] === '$') {
Expand Down Expand Up @@ -768,6 +770,7 @@ class SystemStore extends EventEmitter {
_.each(changes.remove, (list, name) => {
get_collection(name);
_.each(list, id => {
if (!mongo_utils.is_object_id(id)) throw new Error(`SystemStore: make_changes id is not of type object_id: ${id}`);
any_news = true;
get_bulk(name)
.find({
Expand All @@ -785,6 +788,7 @@ class SystemStore extends EventEmitter {
_.each(changes.db_delete, (list, name) => {
get_collection(name);
_.each(list, id => {
if (!mongo_utils.is_object_id(id)) throw new Error(`SystemStore: make_changes id is not of type object_id: ${id}`);
get_bulk(name)
.find({
_id: id,
Expand Down