Skip to content

Commit dda5f9b

Browse files
committed
Compaction: Create a new sync instead of copying the first sync and compacting everything on it. This is slower, but it means the compacted sync does not have the same sync ID as any other sync.
1 parent a5299b4 commit dda5f9b

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

pkg/synccompactor/compactor.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,32 +156,30 @@ func (c *Compactor) Compact(ctx context.Context) (*CompactableSync, error) {
156156
fileName := fmt.Sprintf("compacted-%s.c1z", c.entries[0].SyncID)
157157
destFilePath := path.Join(c.destDir, fileName)
158158

159-
// Copy the last entry into the destination file.
160-
err = cpFile(c.entries[len(c.entries)-1].FilePath, destFilePath)
161-
if err != nil {
162-
return nil, fmt.Errorf("failed to copy file: %w", err)
163-
}
164159
c.compactedC1z, err = dotc1z.NewC1ZFile(ctx, destFilePath, opts...)
165160
if err != nil {
166161
l.Error("doOneCompaction failed: could not create c1z file", zap.Error(err))
167162
return nil, err
168163
}
169164
defer func() { _ = c.compactedC1z.Close() }()
165+
newSyncId, err := c.compactedC1z.StartNewSync(ctx, connectorstore.SyncTypePartial, "")
166+
if err != nil {
167+
return nil, fmt.Errorf("failed to start new sync: %w", err)
168+
}
169+
err = c.compactedC1z.EndSync(ctx)
170+
if err != nil {
171+
return nil, fmt.Errorf("failed to end sync: %w", err)
172+
}
173+
l.Debug("new empty partial sync created", zap.String("sync_id", newSyncId))
170174

171175
// Base sync is c.entries[0], so compact in reverse order. That way we compact the biggest sync last.
172-
for i := len(c.entries) - 2; i >= 0; i-- {
176+
for i := len(c.entries) - 1; i >= 0; i-- {
173177
err = c.doOneCompaction(ctx, c.entries[i])
174178
if err != nil {
175179
return nil, fmt.Errorf("failed to compact sync %s: %w", c.entries[i].SyncID, err)
176180
}
177181
}
178182

179-
newSyncId, err := c.compactedC1z.LatestSyncID(ctx, connectorstore.SyncTypeAny)
180-
if err != nil {
181-
return nil, err
182-
}
183-
l.Debug("latest sync id", zap.String("sync_id", newSyncId))
184-
185183
// Grant expansion doesn't use the connector interface at all, so giving syncer an empty connector is safe... for now.
186184
// If that ever changes, we should implement a file connector that is a wrapper around the reader.
187185
emptyConnector, err := sdk.NewEmptyConnector()

0 commit comments

Comments
 (0)