Skip to content

Commit b98dc14

Browse files
committed
fix(storage): allow a deleted calendar in the Mail restore census
Use the account owner change feed after a verified empty calendar listing. Reject created and updated objects while accepting tombstones without loading the calendar event cache.
1 parent 534c210 commit b98dc14

2 files changed

Lines changed: 70 additions & 17 deletions

File tree

‎scripts/verify-email-data-recovery/census.go‎

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,40 +41,46 @@ func (c *mailboxClient) count(ctx context.Context, method string) (int, error) {
4141
return queryTotal(result)
4242
}
4343

44-
func (c *mailboxClient) collectionState(ctx context.Context, collection string) (string, error) {
44+
type collectionChanges struct {
45+
state string
46+
liveChanges bool
47+
}
48+
49+
func (c *mailboxClient) collectionChanges(ctx context.Context, collection string) (collectionChanges, error) {
4550
changes, err := c.call(ctx, collection+"/changes", object{"sinceState": "n"})
4651
if err != nil {
47-
return "", err
52+
return collectionChanges{}, err
4853
}
49-
state := text(changes, "newState")
50-
if text(changes, "oldState") != "n" || state == "" || changes["hasMoreChanges"] != false {
51-
return "", fmt.Errorf("census %s: incomplete change state", collection)
54+
result := collectionChanges{state: text(changes, "newState")}
55+
if text(changes, "oldState") != "n" || result.state == "" || changes["hasMoreChanges"] != false {
56+
return collectionChanges{}, fmt.Errorf("census %s: incomplete change state", collection)
5257
}
53-
if state == "n" {
54-
for _, field := range []string{"created", "updated", "destroyed"} {
55-
rows, ok := changes[field].([]any)
56-
if !ok || len(rows) != 0 {
57-
return "", fmt.Errorf("census %s: ambiguous initial state", collection)
58-
}
58+
for _, field := range []string{"created", "updated", "destroyed"} {
59+
rows, ok := changes[field].([]any)
60+
if !ok || (result.state == "n" && len(rows) != 0) {
61+
return collectionChanges{}, fmt.Errorf("census %s: inconsistent changes", collection)
62+
}
63+
if field != "destroyed" && len(rows) != 0 {
64+
result.liveChanges = true
5965
}
6066
}
61-
return state, nil
67+
return result, nil
6268
}
6369

6470
// listAll avoids initializing an untouched collection. An initialized
6571
// collection must retain its state while listed, so regenerated entries
6672
// cannot pass as restored data.
6773
func (c *mailboxClient) listAll(ctx context.Context, collection string) (int, error) {
68-
state, err := c.collectionState(ctx, collection)
69-
if err != nil || state == "n" {
74+
changes, err := c.collectionChanges(ctx, collection)
75+
if err != nil || changes.state == "n" {
7076
return 0, err
7177
}
7278
result, err := c.call(ctx, collection+"/get", object{"ids": nil, "properties": []any{"id"}})
7379
if err != nil {
7480
return 0, err
7581
}
7682
list, ok := result["list"].([]any)
77-
if text(result, "state") != state {
83+
if text(result, "state") != changes.state {
7884
return 0, fmt.Errorf("census %s: collection changed during listing", collection)
7985
}
8086
if !ok || len(items(result, "list")) != len(list) || !noneMissing(result) {
@@ -175,8 +181,15 @@ func (c *mailboxClient) census(ctx context.Context) (storeCensus, error) {
175181
if s.events, s.tasks, err = c.calendarObjects(ctx); err != nil {
176182
return s, err
177183
}
178-
} else if state, err := c.collectionState(ctx, "CalendarEvent"); err != nil || state != "n" {
179-
return s, errors.New("census-calendar-objects-without-calendar")
184+
} else {
185+
// Calendar/get established that no calendar exists. A calendar event
186+
// query can create a default calendar when the server rebuilds its
187+
// cache. Use the owner's change feed here to reject surviving events
188+
// or tasks while allowing a collection whose last objects were deleted.
189+
changes, err := c.collectionChanges(ctx, "CalendarEvent")
190+
if err != nil || changes.liveChanges {
191+
return s, errors.New("census-calendar-objects-without-calendar")
192+
}
180193
}
181194
if s.identities, err = c.listAll(ctx, "Identity"); err != nil {
182195
return s, err

‎scripts/verify-email-data-recovery/census_test.go‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,46 @@ func TestCensusLeavesUninitializedCollectionsUntouched(t *testing.T) {
118118
}
119119
}
120120

121+
func TestCensusChecksInitializedEventsAfterLastCalendarRemoved(t *testing.T) {
122+
for _, test := range []struct {
123+
name string
124+
liveField string
125+
}{
126+
{name: "empty after deletion"},
127+
{name: "created item without calendar", liveField: "created"},
128+
{name: "updated item without calendar", liveField: "updated"},
129+
} {
130+
t.Run(test.name, func(t *testing.T) {
131+
store := censusFixture()
132+
store.calendars = 0
133+
store.objects = nil
134+
store.mutate = func(method string, _, result object) {
135+
if method == "CalendarEvent/changes" {
136+
result["destroyed"] = []any{"deleted-item"}
137+
if test.liveField != "" {
138+
result[test.liveField] = []any{"surviving-item"}
139+
}
140+
}
141+
}
142+
census, err := store.serve(t).census(context.Background())
143+
for _, method := range store.methods {
144+
if method == "CalendarEvent/query" || method == "CalendarEvent/get" {
145+
t.Fatalf("%s can initialize a default calendar", method)
146+
}
147+
}
148+
if test.liveField == "" {
149+
if err != nil || census.calendars != 0 || census.events != 0 || census.tasks != 0 {
150+
t.Fatalf("census of an initialized empty event collection: %+v, %v", census, err)
151+
}
152+
return
153+
}
154+
if err == nil || !strings.Contains(err.Error(), "census-calendar-objects-without-calendar") {
155+
t.Fatalf("orphaned calendar content did not fail the census: %v", err)
156+
}
157+
})
158+
}
159+
}
160+
121161
func TestCensusRechecksAnEmptyCalendarEventQuery(t *testing.T) {
122162
store := censusFixture()
123163
store.objects = nil

0 commit comments

Comments
 (0)