forked from mtusi2026/mtusi2026lab4
feat: two-store reconciler with safe repair and cost measurement (grade 5, track 2)
This commit is contained in:
@@ -28,31 +28,37 @@ type Orphan struct {
|
||||
}
|
||||
|
||||
type Missing struct {
|
||||
ID string `json:"id"`
|
||||
ObjectKey string `json:"object_key"`
|
||||
ChatID string `json:"chat_id"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
ID string `json:"id"`
|
||||
ObjectKey string `json:"object_key"`
|
||||
ChatID string `json:"chat_id"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
Referenced bool `json:"referenced_by_message"`
|
||||
}
|
||||
|
||||
type Report struct {
|
||||
Prefix string `json:"prefix"`
|
||||
GraceSeconds int64 `json:"grace_seconds"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
ScannedObjects int `json:"scanned_objects"`
|
||||
ScannedRows int `json:"scanned_rows"`
|
||||
Truncated bool `json:"truncated"`
|
||||
Orphans []Orphan `json:"orphans"`
|
||||
Missing []Missing `json:"missing"`
|
||||
Deleted int `json:"deleted_objects"`
|
||||
DeleteFailures int `json:"delete_failures"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
Prefix string `json:"prefix"`
|
||||
GraceSeconds int64 `json:"grace_seconds"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
ScannedObjects int `json:"scanned_objects"`
|
||||
ScannedRows int `json:"scanned_rows"`
|
||||
Truncated bool `json:"truncated"`
|
||||
Orphans []Orphan `json:"orphans"`
|
||||
Missing []Missing `json:"missing"`
|
||||
DeletedObjects int `json:"deleted_objects"`
|
||||
DeletedMetadata int `json:"deleted_metadata"`
|
||||
RetainedMissing int `json:"retained_missing"`
|
||||
DeleteFailures int `json:"delete_failures"`
|
||||
S3Requests int64 `json:"s3_requests"`
|
||||
DBQueries int `json:"db_queries"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
ObjectsPerSecond float64 `json:"objects_per_second"`
|
||||
}
|
||||
|
||||
func Plan(objects []Object, rows []Metadata, prefix string, now time.Time, grace time.Duration) ([]Orphan, []Missing) {
|
||||
referenced := make(map[string]struct{}, len(rows))
|
||||
func Plan(objects []Object, rows []Metadata, referenced map[uuid.UUID]bool, prefix string, now time.Time, grace time.Duration) ([]Orphan, []Missing) {
|
||||
known := make(map[string]struct{}, len(rows))
|
||||
existing := make(map[string]struct{}, len(objects))
|
||||
for _, row := range rows {
|
||||
referenced[row.ObjectKey] = struct{}{}
|
||||
known[row.ObjectKey] = struct{}{}
|
||||
}
|
||||
for _, object := range objects {
|
||||
existing[object.Key] = struct{}{}
|
||||
@@ -63,7 +69,7 @@ func Plan(objects []Object, rows []Metadata, prefix string, now time.Time, grace
|
||||
if !strings.HasPrefix(object.Key, prefix) {
|
||||
continue
|
||||
}
|
||||
if _, ok := referenced[object.Key]; ok {
|
||||
if _, ok := known[object.Key]; ok {
|
||||
continue
|
||||
}
|
||||
age := now.Sub(object.LastModified)
|
||||
@@ -80,14 +86,18 @@ func Plan(objects []Object, rows []Metadata, prefix string, now time.Time, grace
|
||||
|
||||
missing := make([]Missing, 0)
|
||||
for _, row := range rows {
|
||||
if !strings.HasPrefix(row.ObjectKey, prefix) {
|
||||
continue
|
||||
}
|
||||
if _, ok := existing[row.ObjectKey]; ok {
|
||||
continue
|
||||
}
|
||||
missing = append(missing, Missing{
|
||||
ID: row.ID.String(),
|
||||
ObjectKey: row.ObjectKey,
|
||||
ChatID: row.ChatID.String(),
|
||||
CreatedAt: row.CreatedAt.UTC().Format(time.RFC3339),
|
||||
ID: row.ID.String(),
|
||||
ObjectKey: row.ObjectKey,
|
||||
ChatID: row.ChatID.String(),
|
||||
CreatedAt: row.CreatedAt.UTC().Format(time.RFC3339),
|
||||
Referenced: referenced[row.ID],
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user