deleteTenant will delete all of a tenant's objects if there is not a tenant index present.
(ctx context.Context, tenantID string)
| 531 | |
| 532 | // deleteTenant will delete all of a tenant's objects if there is not a tenant index present. |
| 533 | func (p *Poller) deleteTenant(ctx context.Context, tenantID string) error { |
| 534 | // If we have not enabled empty tenant deletion, do nothing. |
| 535 | if !p.cfg.EmptyTenantDeletionEnabled { |
| 536 | return nil |
| 537 | } |
| 538 | |
| 539 | level.Info(p.logger).Log("msg", "deleting tenant", "tenant", tenantID) |
| 540 | |
| 541 | if p.cfg.EmptyTenantDeletionAge == 0 { |
| 542 | return fmt.Errorf("empty tenant deletion age must be greater than 0") |
| 543 | } |
| 544 | |
| 545 | var ( |
| 546 | foundObjects []string |
| 547 | recentObjects int |
| 548 | ) |
| 549 | err := p.reader.Find(ctx, backend.KeyPath{tenantID}, func(opts backend.FindMatch) { |
| 550 | level.Info(p.logger).Log("msg", "checking object for deletion", "object", opts.Key, "modified", opts.Modified) |
| 551 | |
| 552 | if time.Since(opts.Modified) > p.cfg.EmptyTenantDeletionAge { |
| 553 | foundObjects = append(foundObjects, opts.Key) |
| 554 | } else { |
| 555 | recentObjects++ |
| 556 | } |
| 557 | }) |
| 558 | if err != nil { |
| 559 | return err |
| 560 | } |
| 561 | |
| 562 | // do nothing if there are recent objects for this tenant. |
| 563 | if recentObjects > 0 { |
| 564 | return nil |
| 565 | } |
| 566 | |
| 567 | // do nothing if the tenant index has appeared. |
| 568 | _, err = p.reader.TenantIndex(ctx, tenantID) |
| 569 | // If we have any error other than that which indicates that the tenant index |
| 570 | // call was made successfully, and that it does not exist, do nothing. Only |
| 571 | // proceed if we know that the index does not exist. |
| 572 | if !errors.Is(err, backend.ErrDoesNotExist) { |
| 573 | return nil |
| 574 | } |
| 575 | |
| 576 | for _, object := range foundObjects { |
| 577 | dir, name := path.Split(object) |
| 578 | level.Info(p.logger).Log("msg", "deleting", "tenant", tenantID, "object", object) |
| 579 | err = p.writer.Delete(ctx, name, backend.KeyPath{dir}) |
| 580 | if err != nil { |
| 581 | return err |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | return nil |
| 586 | } |
| 587 | |
| 588 | type backendMetaMetrics struct { |
| 589 | blockMetaTotalObjects int |
no test coverage detected