Add prev delta to cache

This commit is contained in:
Abhishek Pandey 2023-06-20 16:18:56 -07:00
parent 479f114514
commit 9af64f8c13
4 changed files with 30 additions and 19 deletions

View File

@ -323,6 +323,23 @@ func (c *Collections) Get(
"num_deltas_entries", numDeltas, "num_deltas_entries", numDeltas,
"delta_reset", delta.Reset) "delta_reset", delta.Reset)
numDriveItems := c.NumItems - numPrevItems
numPrevItems = c.NumItems
// Attach an url cache
if numDriveItems < urlCacheDriveItemThreshold {
logger.Ctx(ictx).Info("adding url cache for drive")
err = c.addURLCacheToDriveCollections(
ictx,
driveID,
prevDelta,
errs)
if err != nil {
return nil, false, err
}
}
// For both cases we don't need to do set difference on folder map if the // For both cases we don't need to do set difference on folder map if the
// delta token was valid because we should see all the changes. // delta token was valid because we should see all the changes.
if !delta.Reset { if !delta.Reset {
@ -379,22 +396,6 @@ func (c *Collections) Get(
c.CollectionMap[driveID][fldID] = col c.CollectionMap[driveID][fldID] = col
} }
numDriveItems := c.NumItems - numPrevItems
numPrevItems = c.NumItems
// Only create a drive cache if there are less than 300k items in the drive.
if numDriveItems < urlCacheDriveItemThreshold {
logger.Ctx(ictx).Info("adding url cache for drive")
err = c.addURLCacheToDriveCollections(
ictx,
driveID,
errs)
if err != nil {
return nil, false, err
}
}
} }
observe.Message(ctx, fmt.Sprintf("Discovered %d items to backup", c.NumItems)) observe.Message(ctx, fmt.Sprintf("Discovered %d items to backup", c.NumItems))
@ -461,11 +462,12 @@ func (c *Collections) Get(
// a drive. // a drive.
func (c *Collections) addURLCacheToDriveCollections( func (c *Collections) addURLCacheToDriveCollections(
ctx context.Context, ctx context.Context,
driveID string, driveID, prevDelta string,
errs *fault.Bus, errs *fault.Bus,
) error { ) error {
uc, err := newURLCache( uc, err := newURLCache(
driveID, driveID,
prevDelta,
urlCacheRefreshInterval, urlCacheRefreshInterval,
c.handler.NewItemPager(driveID, "", api.DriveItemSelectDefault()), c.handler.NewItemPager(driveID, "", api.DriveItemSelectDefault()),
errs) errs)

View File

@ -2742,6 +2742,7 @@ func (suite *OneDriveCollectionsUnitSuite) TestAddURLCacheToDriveCollections() {
err := c.addURLCacheToDriveCollections( err := c.addURLCacheToDriveCollections(
ctx, ctx,
driveID, driveID,
"",
fault.New(true)) fault.New(true))
require.NoError(t, err, clues.ToCore(err)) require.NoError(t, err, clues.ToCore(err))

View File

@ -37,6 +37,7 @@ var _ getItemPropertyer = &urlCache{}
// urlCache caches download URLs for drive items // urlCache caches download URLs for drive items
type urlCache struct { type urlCache struct {
driveID string driveID string
prevDelta string
idToProps map[string]itemProps idToProps map[string]itemProps
lastRefreshTime time.Time lastRefreshTime time.Time
refreshInterval time.Duration refreshInterval time.Duration
@ -53,7 +54,7 @@ type urlCache struct {
// newURLache creates a new URL cache for the specified drive ID // newURLache creates a new URL cache for the specified drive ID
func newURLCache( func newURLCache(
driveID string, driveID, prevDelta string,
refreshInterval time.Duration, refreshInterval time.Duration,
itemPager api.DriveItemEnumerator, itemPager api.DriveItemEnumerator,
errs *fault.Bus, errs *fault.Bus,
@ -70,6 +71,7 @@ func newURLCache(
idToProps: make(map[string]itemProps), idToProps: make(map[string]itemProps),
lastRefreshTime: time.Time{}, lastRefreshTime: time.Time{},
driveID: driveID, driveID: driveID,
prevDelta: prevDelta,
refreshInterval: refreshInterval, refreshInterval: refreshInterval,
itemPager: itemPager, itemPager: itemPager,
errs: errs, errs: errs,
@ -179,6 +181,8 @@ func (uc *urlCache) deltaQuery(
ctx context.Context, ctx context.Context,
) error { ) error {
logger.Ctx(ctx).Debug("starting delta query") logger.Ctx(ctx).Debug("starting delta query")
// Reset item pager to remove any previous state
uc.itemPager.Reset()
_, _, _, err := collectItems( _, _, _, err := collectItems(
ctx, ctx,
@ -187,7 +191,7 @@ func (uc *urlCache) deltaQuery(
"", "",
uc.updateCache, uc.updateCache,
map[string]string{}, map[string]string{},
"", uc.prevDelta,
uc.errs) uc.errs)
if err != nil { if err != nil {
return clues.Wrap(err, "delta query") return clues.Wrap(err, "delta query")

View File

@ -112,6 +112,7 @@ func (suite *URLCacheIntegrationSuite) TestURLCacheBasic() {
// Create a new URL cache with a long TTL // Create a new URL cache with a long TTL
cache, err := newURLCache( cache, err := newURLCache(
suite.driveID, suite.driveID,
"",
1*time.Hour, 1*time.Hour,
driveItemPager, driveItemPager,
fault.New(true)) fault.New(true))
@ -404,6 +405,7 @@ func (suite *URLCacheUnitSuite) TestGetItemProperties() {
cache, err := newURLCache( cache, err := newURLCache(
driveID, driveID,
"",
1*time.Hour, 1*time.Hour,
itemPager, itemPager,
fault.New(true)) fault.New(true))
@ -446,6 +448,7 @@ func (suite *URLCacheUnitSuite) TestNeedsRefresh() {
cache, err := newURLCache( cache, err := newURLCache(
driveID, driveID,
"",
refreshInterval, refreshInterval,
&mockItemPager{}, &mockItemPager{},
fault.New(true)) fault.New(true))
@ -519,6 +522,7 @@ func (suite *URLCacheUnitSuite) TestNewURLCache() {
t := suite.T() t := suite.T()
_, err := newURLCache( _, err := newURLCache(
test.driveID, test.driveID,
"",
test.refreshInt, test.refreshInt,
test.itemPager, test.itemPager,
test.errors) test.errors)