Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

server: buffer expired storage warning backfills

Neeraj Gupta747760a4c62e
diff --git a/server/pkg/controller/email/storage_warning.go b/server/pkg/controller/email/storage_warning.go
index 938f9586a1..edcfb01f56 100644
--- a/server/pkg/controller/email/storage_warning.go
+++ b/server/pkg/controller/email/storage_warning.go
@@ -59,6 +59,7 @@ type storageWarningSnapshot struct {
Bucket storageWarningBucket
ActiveOverageStage activeOverageWarningStage
ExpiredStage expiredWarningStage
+ ExpiredBufferedCycle bool
AutoDeleteDate int64
IsFamilyPlan bool
WarningCycleStart int64
@@ -414,13 +415,15 @@ func (c *EmailNotificationController) decorateStorageWarningSnapshot(snapshot st
return storageWarningSnapshot{}, err
}
snapshot.NotificationHistory = history
- snapshot.WarningCycleStart = snapshot.ExpiredWarningAnchor
- snapshot.ExpiredStage = resolveExpiredWarningStage(snapshot.ExpiredWarningAnchor, now, history)
+ expiredResolution := resolveExpiredWarning(snapshot.ExpiredWarningAnchor, now, history)
+ snapshot.WarningCycleStart = expiredResolution.CycleStart
+ snapshot.ExpiredStage = expiredResolution.Stage
+ snapshot.ExpiredBufferedCycle = expiredResolution.BufferedCycle
+ snapshot.AutoDeleteDate = expiredResolution.AutoDeleteDate
if snapshot.ExpiredStage == expiredWarningStageNone {
snapshot.Bucket = storageWarningBucketNone
return snapshot, nil
}
- snapshot.AutoDeleteDate = expiredWarningAutoDeleteDate(snapshot.ExpiredWarningAnchor, snapshot.ExpiredStage, now)
case storageWarningBucketActiveOverage:
history, err := c.NotificationHistoryRepo.GetLastNotificationTimes(snapshot.RecipientID, activeOverageWarningTemplateIDs())
if err != nil {
@@ -658,10 +661,16 @@ func storageWarningPreviousStage(snapshot storageWarningSnapshot) (templateID st
case expiredWarningStage30:
return storageWarningExpired0TemplateID, string(expiredWarningStage0), true
case expiredWarningStage60:
+ if snapshot.ExpiredBufferedCycle {
+ return storageWarningExpired0TemplateID, string(expiredWarningStage0), true
+ }
return storageWarningExpired30TemplateID, string(expiredWarningStage30), true
case expiredWarningStage90:
return storageWarningExpired60TemplateID, string(expiredWarningStage60), true
case expiredWarningStage119:
+ if snapshot.ExpiredBufferedCycle {
+ return storageWarningExpired60TemplateID, string(expiredWarningStage60), true
+ }
return storageWarningExpired90TemplateID, string(expiredWarningStage90), true
case expiredWarningStageScheduledDeletion:
return storageWarningExpired119TemplateID, string(expiredWarningStage119), true
diff --git a/server/pkg/controller/email/storage_warning_expired.go b/server/pkg/controller/email/storage_warning_expired.go
index ebcdfc5ff7..b687c871f5 100644
--- a/server/pkg/controller/email/storage_warning_expired.go
+++ b/server/pkg/controller/email/storage_warning_expired.go
@@ -9,13 +9,15 @@ const (
storageWarningExpiredTemplate = "storage-warning/storage_warning_expired.html"
storageWarningExpiredScheduledDeletionTemplate = "storage-warning/storage_warning_expired_scheduled_deletion.html"
- storageWarningExpiredAnchorDelay = 30 * 24 * time.MicroSecondsInOneHour
- storageWarningExpiredDeletionDelay = 120 * 24 * time.MicroSecondsInOneHour
- storageWarningExpiredWarning0Delay = 0
- storageWarningExpiredWarning30Delay = 30 * 24 * time.MicroSecondsInOneHour
- storageWarningExpiredWarning60Delay = 60 * 24 * time.MicroSecondsInOneHour
- storageWarningExpiredWarning90Delay = 90 * 24 * time.MicroSecondsInOneHour
- storageWarningExpiredWarning119Delay = 119 * 24 * time.MicroSecondsInOneHour
+ storageWarningExpiredAnchorDelay = 30 * 24 * time.MicroSecondsInOneHour
+ storageWarningExpiredDeletionDelay = 120 * 24 * time.MicroSecondsInOneHour
+ storageWarningExpiredWarning0Delay = 0
+ storageWarningExpiredWarning30Delay = 30 * 24 * time.MicroSecondsInOneHour
+ storageWarningExpiredWarning60Delay = 60 * 24 * time.MicroSecondsInOneHour
+ storageWarningExpiredWarning90Delay = 90 * 24 * time.MicroSecondsInOneHour
+ storageWarningExpiredWarning119Delay = 119 * 24 * time.MicroSecondsInOneHour
+ storageWarningExpiredBackfillThreshold = storageWarningExpiredWarning30Delay
+ storageWarningExpiredBackfillMinRecoveryDelay = 30 * 24 * time.MicroSecondsInOneHour
storageWarningExpired0TemplateID = "storage_warning_expired_0d"
storageWarningExpired30TemplateID = "storage_warning_expired_30d"
@@ -43,6 +45,13 @@ const (
expiredWarningStageScheduledDeletion expiredWarningStage = "expired_scheduled_deletion"
)
+type expiredWarningResolution struct {
+ Stage expiredWarningStage
+ CycleStart int64
+ AutoDeleteDate int64
+ BufferedCycle bool
+}
+
func expiredWarningTemplateDetails(stage expiredWarningStage) (templateID string, templateName string, subject string, ok bool) {
switch stage {
case expiredWarningStage0:
@@ -107,8 +116,109 @@ func resolveExpiredWarningStage(expiredWarningAnchor int64, now int64, history m
return expiredWarningStageNone
}
-func expiredWarningAutoDeleteDate(expiredWarningAnchor int64, stage expiredWarningStage, now int64) int64 {
- autoDeleteDate := expiredWarningAnchor + storageWarningExpiredDeletionDelay
+func resolveExpiredWarning(expiredWarningAnchor int64, now int64, history map[string]int64) expiredWarningResolution {
+ if expiredWarningAnchor <= 0 || expiredWarningAnchor > now {
+ return expiredWarningResolution{}
+ }
+
+ standardAutoDeleteDate := expiredWarningAnchor + storageWarningExpiredDeletionDelay
+ if cycleStart, ok := expiredBufferedCycleStart(expiredWarningAnchor, history); ok {
+ autoDeleteDate := cycleStart + storageWarningExpiredBackfillMinRecoveryDelay
+ if standardAutoDeleteDate > autoDeleteDate {
+ autoDeleteDate = standardAutoDeleteDate
+ }
+ stage := resolveExpiredBufferedWarningStage(cycleStart, autoDeleteDate, now, history)
+ return expiredWarningResolution{
+ Stage: stage,
+ CycleStart: cycleStart,
+ AutoDeleteDate: expiredWarningAutoDeleteDate(autoDeleteDate, stage, now),
+ BufferedCycle: true,
+ }
+ }
+
+ if shouldUseExpiredBufferedCycle(expiredWarningAnchor, now, history) {
+ cycleStart := now
+ autoDeleteDate := cycleStart + storageWarningExpiredBackfillMinRecoveryDelay
+ if standardAutoDeleteDate > autoDeleteDate {
+ autoDeleteDate = standardAutoDeleteDate
+ }
+
+ stage := resolveExpiredBufferedWarningStage(cycleStart, autoDeleteDate, now, history)
+ return expiredWarningResolution{
+ Stage: stage,
+ CycleStart: cycleStart,
+ AutoDeleteDate: expiredWarningAutoDeleteDate(autoDeleteDate, stage, now),
+ BufferedCycle: true,
+ }
+ }
+
+ stage := resolveExpiredWarningStage(expiredWarningAnchor, now, history)
+ return expiredWarningResolution{
+ Stage: stage,
+ CycleStart: expiredWarningAnchor,
+ AutoDeleteDate: expiredWarningAutoDeleteDate(standardAutoDeleteDate, stage, now),
+ }
+}
+
+func shouldUseExpiredBufferedCycle(expiredWarningAnchor int64, now int64, history map[string]int64) bool {
+ if expiredWarningAnchor <= 0 || now < expiredWarningAnchor+storageWarningExpiredBackfillThreshold {
+ return false
+ }
+ return !hasExpiredWarningHistoryInCycle(history, expiredWarningAnchor)
+}
+
+func expiredBufferedCycleStart(expiredWarningAnchor int64, history map[string]int64) (int64, bool) {
+ sentAt := history[storageWarningExpired0TemplateID]
+ if sentAt <= 0 || sentAt < expiredWarningAnchor+storageWarningExpiredBackfillThreshold {
+ return 0, false
+ }
+ return sentAt, true
+}
+
+func hasExpiredWarningHistoryInCycle(history map[string]int64, cycleStart int64) bool {
+ for _, templateID := range expiredWarningTemplateIDs() {
+ if storageWarningTemplateSentInCycle(history, templateID, cycleStart) {
+ return true
+ }
+ }
+ return false
+}
+
+func resolveExpiredBufferedWarningStage(cycleStart int64, autoDeleteDate int64, now int64, history map[string]int64) expiredWarningStage {
+ if cycleStart <= 0 || cycleStart > now {
+ return expiredWarningStageNone
+ }
+
+ if !storageWarningTemplateSentInCycle(history, storageWarningExpired0TemplateID, cycleStart) {
+ return expiredWarningStage0
+ }
+ if now >= expiredBufferedWarning60At(cycleStart, autoDeleteDate) &&
+ !storageWarningTemplateSentInCycle(history, storageWarningExpired60TemplateID, cycleStart) {
+ return expiredWarningStage60
+ }
+ if now >= expiredBufferedWarning119At(autoDeleteDate) &&
+ !storageWarningTemplateSentInCycle(history, storageWarningExpired119TemplateID, cycleStart) {
+ return expiredWarningStage119
+ }
+ if now >= autoDeleteDate &&
+ !storageWarningTemplateSentInCycle(history, repo.StorageWarningExpiredScheduledDeletionTemplateID, cycleStart) {
+ return expiredWarningStageScheduledDeletion
+ }
+ return expiredWarningStageNone
+}
+
+func expiredBufferedWarning60At(cycleStart int64, autoDeleteDate int64) int64 {
+ if autoDeleteDate <= cycleStart {
+ return cycleStart
+ }
+ return cycleStart + ((autoDeleteDate - cycleStart) / 2)
+}
+
+func expiredBufferedWarning119At(autoDeleteDate int64) int64 {
+ return autoDeleteDate - storageWarningOneDayInMicroseconds
+}
+
+func expiredWarningAutoDeleteDate(autoDeleteDate int64, stage expiredWarningStage, now int64) int64 {
if stage == expiredWarningStage119 && autoDeleteDate <= now {
return now + storageWarningOneDayInMicroseconds
}
diff --git a/server/pkg/controller/email/storage_warning_integration_test.go b/server/pkg/controller/email/storage_warning_integration_test.go
index f9f7bc669f..36c6380866 100644
--- a/server/pkg/controller/email/storage_warning_integration_test.go
+++ b/server/pkg/controller/email/storage_warning_integration_test.go
@@ -1,6 +1,7 @@
package email
import (
+ "context"
"database/sql"
"testing"
@@ -26,7 +27,7 @@ func TestSendStorageWarningMailsActiveOverageIntegration(t *testing.T) {
now := timeutil.Microseconds()
userID := testutil.InsertUser(t, db, testutil.UserFixture{
- UserID: storageWarningIntegrationTestUserID,
+ UserID: storageWarningIntegrationTestUserID,
CreationTime: now - timeutil.MicroSecondsInOneHour,
})
@@ -56,7 +57,7 @@ func TestSendStorageWarningMailsExpiredIntegration(t *testing.T) {
now := timeutil.Microseconds()
userID := testutil.InsertUser(t, db, testutil.UserFixture{
- UserID: storageWarningIntegrationTestUserID,
+ UserID: storageWarningIntegrationTestUserID,
CreationTime: now - timeutil.MicroSecondsInOneHour,
})
@@ -75,6 +76,227 @@ func TestSendStorageWarningMailsExpiredIntegration(t *testing.T) {
assertStorageWarningNotificationCount(t, db, userID, storageWarningExpired0TemplateID, 1)
}
+func TestBuildIndividualStorageWarningSnapshotExpiredBackfillUsesBufferedRecoveryIntegration(t *testing.T) {
+ testutil.WithServerRoot(t)
+
+ db := testutil.RequireTestDB(t)
+ testutil.ResetTables(t, db)
+ t.Cleanup(func() {
+ testutil.ResetTables(t, db)
+ })
+
+ now := timeutil.Microseconds()
+ userID := testutil.InsertUser(t, db, testutil.UserFixture{
+ UserID: storageWarningIntegrationTestUserID,
+ Email: "[email protected]",
+ CreationTime: now - timeutil.MicroSecondsInOneHour,
+ })
+ testutil.InsertUsage(t, db, userID, 26*storageWarningIntegrationTestGiB)
+ testutil.InsertSubscription(t, db, testutil.SubscriptionFixture{
+ UserID: userID,
+ Storage: 10 * storageWarningIntegrationTestGiB,
+ ExpiryTime: now - storageWarningExpiredAnchorDelay - storageWarningExpiredDeletionDelay - (10 * 24 * timeutil.MicroSecondsInOneHour),
+ ProductID: "photos_yearly",
+ PaymentProvider: ente.Stripe,
+ })
+
+ controller := newStorageWarningIntegrationController(db)
+ snapshot, err := controller.buildIndividualStorageWarningSnapshot(context.Background(), userID, now)
+ if err != nil {
+ t.Fatalf("buildIndividualStorageWarningSnapshot() error = %v", err)
+ }
+
+ if snapshot.ExpiredStage != expiredWarningStage0 {
+ t.Fatalf("unexpected expired stage: got %q want %q", snapshot.ExpiredStage, expiredWarningStage0)
+ }
+ if !snapshot.ExpiredBufferedCycle {
+ t.Fatal("expected expired backfill snapshot to use buffered recovery")
+ }
+ if snapshot.WarningCycleStart != now {
+ t.Fatalf("unexpected warning cycle start: got %d want %d", snapshot.WarningCycleStart, now)
+ }
+ wantAutoDeleteDate := now + storageWarningExpiredBackfillMinRecoveryDelay
+ if snapshot.AutoDeleteDate != wantAutoDeleteDate {
+ t.Fatalf("unexpected auto delete date: got %d want %d", snapshot.AutoDeleteDate, wantAutoDeleteDate)
+ }
+}
+
+func TestBuildIndividualStorageWarningSnapshotExpiredBackfillContinuesBufferedCycleIntegration(t *testing.T) {
+ testutil.WithServerRoot(t)
+
+ db := testutil.RequireTestDB(t)
+ testutil.ResetTables(t, db)
+ t.Cleanup(func() {
+ testutil.ResetTables(t, db)
+ })
+
+ anchorNow := timeutil.Microseconds()
+ cycleStart := anchorNow - (16 * 24 * timeutil.MicroSecondsInOneHour)
+ userID := testutil.InsertUser(t, db, testutil.UserFixture{
+ UserID: storageWarningIntegrationTestUserID,
+ Email: "[email protected]",
+ CreationTime: anchorNow - timeutil.MicroSecondsInOneHour,
+ })
+ testutil.InsertUsage(t, db, userID, 26*storageWarningIntegrationTestGiB)
+ testutil.InsertSubscription(t, db, testutil.SubscriptionFixture{
+ UserID: userID,
+ Storage: 10 * storageWarningIntegrationTestGiB,
+ ExpiryTime: cycleStart - storageWarningExpiredAnchorDelay - storageWarningExpiredDeletionDelay - (10 * 24 * timeutil.MicroSecondsInOneHour),
+ ProductID: "photos_yearly",
+ PaymentProvider: ente.Stripe,
+ })
+ testutil.InsertNotificationHistory(t, db, testutil.NotificationHistoryFixture{
+ UserID: userID,
+ TemplateID: storageWarningExpired0TemplateID,
+ SentTime: cycleStart,
+ NotificationGroup: storageWarningExpiredNotificationGroup,
+ })
+
+ controller := newStorageWarningIntegrationController(db)
+ snapshot, err := controller.buildIndividualStorageWarningSnapshot(context.Background(), userID, anchorNow)
+ if err != nil {
+ t.Fatalf("buildIndividualStorageWarningSnapshot() error = %v", err)
+ }
+
+ if snapshot.ExpiredStage != expiredWarningStage60 {
+ t.Fatalf("unexpected expired stage: got %q want %q", snapshot.ExpiredStage, expiredWarningStage60)
+ }
+ if !snapshot.ExpiredBufferedCycle {
+ t.Fatal("expected persisted late first-contact history to keep buffered recovery")
+ }
+ if snapshot.WarningCycleStart != cycleStart {
+ t.Fatalf("unexpected warning cycle start: got %d want %d", snapshot.WarningCycleStart, cycleStart)
+ }
+ wantAutoDeleteDate := cycleStart + storageWarningExpiredBackfillMinRecoveryDelay
+ if snapshot.AutoDeleteDate != wantAutoDeleteDate {
+ t.Fatalf("unexpected auto delete date: got %d want %d", snapshot.AutoDeleteDate, wantAutoDeleteDate)
+ }
+}
+
+func TestBuildIndividualStorageWarningSnapshotExpiredBackfillWithExisting0dAnd30dSkipsImmediateSend(t *testing.T) {
+ testutil.WithServerRoot(t)
+
+ db := testutil.RequireTestDB(t)
+ testutil.ResetTables(t, db)
+ t.Cleanup(func() {
+ testutil.ResetTables(t, db)
+ })
+
+ day := int64(24 * timeutil.MicroSecondsInOneHour)
+ t0 := int64(200 * day)
+ now := t0 + (2 * day)
+ anchor := t0 - (40 * day)
+ userID := setupExpiredBackfillWithOld0dAnd30dHistory(t, db, anchor-(30*day), t0)
+
+ controller := newStorageWarningIntegrationController(db)
+ snapshot, err := controller.buildIndividualStorageWarningSnapshot(context.Background(), userID, now)
+ if err != nil {
+ t.Fatalf("buildIndividualStorageWarningSnapshot() error = %v", err)
+ }
+
+ if snapshot.CurrentBucket != storageWarningBucketExpired {
+ t.Fatalf("unexpected current bucket: got %q want %q", snapshot.CurrentBucket, storageWarningBucketExpired)
+ }
+ if snapshot.Bucket != storageWarningBucketNone {
+ t.Fatalf("unexpected decorated bucket: got %q want %q", snapshot.Bucket, storageWarningBucketNone)
+ }
+ if snapshot.ExpiredStage != expiredWarningStageNone {
+ t.Fatalf("unexpected expired stage: got %q want %q", snapshot.ExpiredStage, expiredWarningStageNone)
+ }
+ if !snapshot.ExpiredBufferedCycle {
+ t.Fatal("expected persisted old 0d send to establish buffered cycle")
+ }
+ if snapshot.WarningCycleStart != t0 {
+ t.Fatalf("unexpected warning cycle start: got %d want %d", snapshot.WarningCycleStart, t0)
+ }
+ wantAutoDeleteDate := maxInt64(t0+storageWarningExpiredBackfillMinRecoveryDelay, anchor+storageWarningExpiredDeletionDelay)
+ if snapshot.AutoDeleteDate != wantAutoDeleteDate {
+ t.Fatalf("unexpected auto delete date: got %d want %d", snapshot.AutoDeleteDate, wantAutoDeleteDate)
+ }
+}
+
+func TestBuildIndividualStorageWarningSnapshotExpiredBackfillWithExisting0dAnd30dTransitionsTo60dLater(t *testing.T) {
+ testutil.WithServerRoot(t)
+
+ db := testutil.RequireTestDB(t)
+ testutil.ResetTables(t, db)
+ t.Cleanup(func() {
+ testutil.ResetTables(t, db)
+ })
+
+ day := int64(24 * timeutil.MicroSecondsInOneHour)
+ t0 := int64(200 * day)
+ anchor := t0 - (40 * day)
+ standardAutoDeleteDate := anchor + storageWarningExpiredDeletionDelay
+ now := expiredBufferedWarning60At(t0, standardAutoDeleteDate) + day
+ userID := setupExpiredBackfillWithOld0dAnd30dHistory(t, db, anchor-(30*day), t0)
+
+ controller := newStorageWarningIntegrationController(db)
+ snapshot, err := controller.buildIndividualStorageWarningSnapshot(context.Background(), userID, now)
+ if err != nil {
+ t.Fatalf("buildIndividualStorageWarningSnapshot() error = %v", err)
+ }
+
+ if snapshot.CurrentBucket != storageWarningBucketExpired {
+ t.Fatalf("unexpected current bucket: got %q want %q", snapshot.CurrentBucket, storageWarningBucketExpired)
+ }
+ if snapshot.Bucket != storageWarningBucketExpired {
+ t.Fatalf("unexpected decorated bucket: got %q want %q", snapshot.Bucket, storageWarningBucketExpired)
+ }
+ if snapshot.ExpiredStage != expiredWarningStage60 {
+ t.Fatalf("unexpected expired stage: got %q want %q", snapshot.ExpiredStage, expiredWarningStage60)
+ }
+ if !snapshot.ExpiredBufferedCycle {
+ t.Fatal("expected persisted old 0d send to keep buffered cycle")
+ }
+ if snapshot.WarningCycleStart != t0 {
+ t.Fatalf("unexpected warning cycle start: got %d want %d", snapshot.WarningCycleStart, t0)
+ }
+ wantAutoDeleteDate := maxInt64(t0+storageWarningExpiredBackfillMinRecoveryDelay, standardAutoDeleteDate)
+ if snapshot.AutoDeleteDate != wantAutoDeleteDate {
+ t.Fatalf("unexpected auto delete date: got %d want %d", snapshot.AutoDeleteDate, wantAutoDeleteDate)
+ }
+}
+
+func setupExpiredBackfillWithOld0dAnd30dHistory(t *testing.T, db *sql.DB, subscriptionExpiry int64, firstSendAt int64) int64 {
+ t.Helper()
+
+ userID := testutil.InsertUser(t, db, testutil.UserFixture{
+ UserID: storageWarningIntegrationTestUserID,
+ Email: "[email protected]",
+ CreationTime: firstSendAt - timeutil.MicroSecondsInOneHour,
+ })
+ testutil.InsertUsage(t, db, userID, 26*storageWarningIntegrationTestGiB)
+ testutil.InsertSubscription(t, db, testutil.SubscriptionFixture{
+ UserID: userID,
+ Storage: 10 * storageWarningIntegrationTestGiB,
+ ExpiryTime: subscriptionExpiry,
+ ProductID: "photos_yearly",
+ PaymentProvider: ente.Stripe,
+ })
+ testutil.InsertNotificationHistory(t, db, testutil.NotificationHistoryFixture{
+ UserID: userID,
+ TemplateID: storageWarningExpired0TemplateID,
+ SentTime: firstSendAt,
+ NotificationGroup: storageWarningExpiredNotificationGroup,
+ })
+ testutil.InsertNotificationHistory(t, db, testutil.NotificationHistoryFixture{
+ UserID: userID,
+ TemplateID: storageWarningExpired30TemplateID,
+ SentTime: firstSendAt + (2 * 24 * timeutil.MicroSecondsInOneHour),
+ NotificationGroup: storageWarningExpiredNotificationGroup,
+ })
+
+ return userID
+}
+
+func maxInt64(a int64, b int64) int64 {
+ if a >= b {
+ return a
+ }
+ return b
+}
+
func newStorageWarningIntegrationController(db *sql.DB) *EmailNotificationController {
userRepo := &repo.UserRepository{
DB: db,
diff --git a/server/pkg/controller/email/storage_warning_test.go b/server/pkg/controller/email/storage_warning_test.go
index 7929adbfad..41ffffae0e 100644
--- a/server/pkg/controller/email/storage_warning_test.go
+++ b/server/pkg/controller/email/storage_warning_test.go
@@ -281,12 +281,6 @@ func TestResolveExpiredWarningStage(t *testing.T) {
},
want: expiredWarningStageNone,
},
- {
- name: "long expired backfill without history starts at first reminder",
- now: expiredWarningAnchor + storageWarningExpiredWarning119Delay + 10,
- history: map[string]int64{},
- want: expiredWarningStage0,
- },
{
name: "old cycle reminder ignored after renewal",
now: expiredWarningAnchor,
@@ -307,41 +301,64 @@ func TestResolveExpiredWarningStage(t *testing.T) {
}
}
-func TestResolveExpiredWarningStageLateBackfillSequence(t *testing.T) {
+func TestResolveExpiredWarningUsesBufferedCycleForLateBackfill(t *testing.T) {
expiredWarningAnchor := int64(100)
now := expiredWarningAnchor + storageWarningExpiredWarning119Delay + 10
- history := map[string]int64{}
+ got := resolveExpiredWarning(expiredWarningAnchor, now, map[string]int64{})
- if got := resolveExpiredWarningStage(expiredWarningAnchor, now, history); got != expiredWarningStage0 {
- t.Fatalf("expected first late-backfill stage %q, got %q", expiredWarningStage0, got)
+ if got.Stage != expiredWarningStage0 {
+ t.Fatalf("unexpected stage: got %q want %q", got.Stage, expiredWarningStage0)
}
-
- history[storageWarningExpired0TemplateID] = expiredWarningAnchor
- if got := resolveExpiredWarningStage(expiredWarningAnchor, now, history); got != expiredWarningStage30 {
- t.Fatalf("expected second late-backfill stage %q, got %q", expiredWarningStage30, got)
+ if !got.BufferedCycle {
+ t.Fatal("expected late backfill to use a buffered cycle")
+ }
+ if got.CycleStart != now {
+ t.Fatalf("unexpected cycle start: got %d want %d", got.CycleStart, now)
+ }
+ wantAutoDeleteDate := now + storageWarningExpiredBackfillMinRecoveryDelay
+ if got.AutoDeleteDate != wantAutoDeleteDate {
+ t.Fatalf("unexpected auto delete date: got %d want %d", got.AutoDeleteDate, wantAutoDeleteDate)
}
+}
+
+func TestResolveExpiredWarningBufferedCycleKeepsLaterHistoricalDeleteDate(t *testing.T) {
+ expiredWarningAnchor := int64(100)
+ now := expiredWarningAnchor + storageWarningExpiredBackfillThreshold + 10
+
+ got := resolveExpiredWarning(expiredWarningAnchor, now, map[string]int64{})
- history[storageWarningExpired30TemplateID] = expiredWarningAnchor + storageWarningExpiredWarning30Delay
- if got := resolveExpiredWarningStage(expiredWarningAnchor, now, history); got != expiredWarningStage60 {
- t.Fatalf("expected third late-backfill stage %q, got %q", expiredWarningStage60, got)
+ if got.Stage != expiredWarningStage0 {
+ t.Fatalf("unexpected stage: got %q want %q", got.Stage, expiredWarningStage0)
}
+ if !got.BufferedCycle {
+ t.Fatal("expected late first contact to use buffered cycle")
+ }
+ wantAutoDeleteDate := expiredWarningAnchor + storageWarningExpiredDeletionDelay
+ if got.AutoDeleteDate != wantAutoDeleteDate {
+ t.Fatalf("unexpected auto delete date: got %d want %d", got.AutoDeleteDate, wantAutoDeleteDate)
+ }
+}
- history[storageWarningExpired60TemplateID] = expiredWarningAnchor + storageWarningExpiredWarning60Delay
- if got := resolveExpiredWarningStage(expiredWarningAnchor, now, history); got != expiredWarningStage90 {
- t.Fatalf("expected fourth late-backfill stage %q, got %q", expiredWarningStage90, got)
+func TestResolveExpiredWarningBufferedCycleProgressesFromPersistedHistory(t *testing.T) {
+ cycleStart := int64(100)
+ autoDeleteDate := cycleStart + storageWarningExpiredBackfillMinRecoveryDelay
+ now := expiredBufferedWarning119At(autoDeleteDate) + 10
+ history := map[string]int64{
+ storageWarningExpired0TemplateID: cycleStart,
+ storageWarningExpired60TemplateID: expiredBufferedWarning60At(cycleStart, autoDeleteDate),
}
- history[storageWarningExpired90TemplateID] = expiredWarningAnchor + storageWarningExpiredWarning90Delay
- if got := resolveExpiredWarningStage(expiredWarningAnchor, now, history); got != expiredWarningStage119 {
- t.Fatalf("expected final late-backfill reminder %q, got %q", expiredWarningStage119, got)
+ got := resolveExpiredBufferedWarningStage(cycleStart, autoDeleteDate, now, history)
+ if got != expiredWarningStage119 {
+ t.Fatalf("unexpected buffered stage: got %q want %q", got, expiredWarningStage119)
}
}
func TestExpiredWarningAutoDeleteDateClampsOverdueFinalStage(t *testing.T) {
now := int64(1000)
- expiredWarningAnchor := now - storageWarningExpiredDeletionDelay - 10
+ autoDeleteDate := now - 10
- got := expiredWarningAutoDeleteDate(expiredWarningAnchor, expiredWarningStage119, now)
+ got := expiredWarningAutoDeleteDate(autoDeleteDate, expiredWarningStage119, now)
want := now + storageWarningOneDayInMicroseconds
if got != want {
t.Fatalf("unexpected auto delete date: got %d want %d", got, want)
@@ -615,6 +632,33 @@ func TestStorageWarningCadenceBroken(t *testing.T) {
},
wantBroken: false,
},
+ {
+ name: "buffered expired stage 60 requires initial expired notice",
+ snapshot: storageWarningSnapshot{
+ Bucket: storageWarningBucketExpired,
+ ExpiredStage: expiredWarningStage60,
+ ExpiredBufferedCycle: true,
+ EvaluatedAt: now,
+ WarningCycleStart: now - (storageWarningExpiredBackfillMinRecoveryDelay / 2),
+ NotificationHistory: map[string]int64{},
+ },
+ wantBroken: true,
+ wantStage: string(expiredWarningStage0),
+ },
+ {
+ name: "buffered expired final reminder uses the buffered midpoint stage",
+ snapshot: storageWarningSnapshot{
+ Bucket: storageWarningBucketExpired,
+ ExpiredStage: expiredWarningStage119,
+ ExpiredBufferedCycle: true,
+ EvaluatedAt: now,
+ WarningCycleStart: now - storageWarningExpiredBackfillMinRecoveryDelay + storageWarningOneDayInMicroseconds,
+ NotificationHistory: map[string]int64{
+ storageWarningExpired60TemplateID: now - storageWarningOneDayInMicroseconds,
+ },
+ },
+ wantBroken: false,
+ },
{
name: "terminal active overage stage requires final reminder",
snapshot: storageWarningSnapshot{