-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from anyproto/GO-2061-space-deletion
GO-2061 deletion log, space delete
- Loading branch information
Showing
13 changed files
with
409 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package deletelog | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"github.com/anyproto/any-sync/app" | ||
"github.com/anyproto/any-sync/app/logger" | ||
"github.com/anyproto/any-sync/coordinator/coordinatorclient" | ||
"github.com/anyproto/any-sync/coordinator/coordinatorproto" | ||
"github.com/anyproto/any-sync/util/periodicsync" | ||
"github.com/go-redsync/redsync/v4" | ||
"github.com/go-redsync/redsync/v4/redis/goredis/v9" | ||
"github.com/redis/go-redis/v9" | ||
"go.uber.org/zap" | ||
|
||
"github.com/anyproto/any-sync-filenode/index" | ||
"github.com/anyproto/any-sync-filenode/redisprovider" | ||
) | ||
|
||
const CName = "filenode.deletionLog" | ||
|
||
const lastKey = "deletionLastId.{system}" | ||
|
||
const recordsLimit = 1000 | ||
|
||
var log = logger.NewNamed(CName) | ||
|
||
func New() app.ComponentRunnable { | ||
return new(deleteLog) | ||
} | ||
|
||
type deleteLog struct { | ||
redis redis.UniversalClient | ||
coordinatorClient coordinatorclient.CoordinatorClient | ||
redsync *redsync.Redsync | ||
ticker periodicsync.PeriodicSync | ||
index index.Index | ||
disableTicker bool | ||
} | ||
|
||
func (d *deleteLog) Init(a *app.App) (err error) { | ||
d.redis = a.MustComponent(redisprovider.CName).(redisprovider.RedisProvider).Redis() | ||
d.coordinatorClient = a.MustComponent(coordinatorclient.CName).(coordinatorclient.CoordinatorClient) | ||
d.redsync = redsync.New(goredis.NewPool(d.redis)) | ||
d.index = a.MustComponent(index.CName).(index.Index) | ||
return | ||
} | ||
|
||
func (d *deleteLog) Name() (name string) { | ||
return CName | ||
} | ||
|
||
func (d *deleteLog) Run(ctx context.Context) (err error) { | ||
if !d.disableTicker { | ||
d.ticker = periodicsync.NewPeriodicSync(60, time.Hour, d.checkLog, log) | ||
d.ticker.Run() | ||
} | ||
return | ||
} | ||
|
||
func (d *deleteLog) checkLog(ctx context.Context) (err error) { | ||
mu := d.redsync.NewMutex("_lock:deletion", redsync.WithExpiry(time.Minute*10)) | ||
if err = mu.LockContext(ctx); err != nil { | ||
return | ||
} | ||
defer func() { | ||
_, _ = mu.Unlock() | ||
}() | ||
st := time.Now() | ||
lastId, err := d.redis.Get(ctx, lastKey).Result() | ||
if err != nil && !errors.Is(err, redis.Nil) { | ||
return | ||
} | ||
|
||
recs, err := d.coordinatorClient.DeletionLog(ctx, lastId, recordsLimit) | ||
if err != nil { | ||
return | ||
} | ||
var handledCount, deletedCount int | ||
var ok bool | ||
for _, rec := range recs { | ||
if rec.Status == coordinatorproto.DeletionLogRecordStatus_Remove && rec.FileGroup != "" { | ||
ok, err = d.index.SpaceDelete(ctx, index.Key{ | ||
GroupId: rec.FileGroup, | ||
SpaceId: rec.SpaceId, | ||
}) | ||
if err != nil { | ||
return | ||
} | ||
handledCount++ | ||
if ok { | ||
deletedCount++ | ||
} | ||
} | ||
if err = d.redis.Set(ctx, lastKey, rec.Id, 0).Err(); err != nil { | ||
return | ||
} | ||
} | ||
log.Info("processing deletion log", | ||
zap.Int("records", len(recs)), | ||
zap.Int("handled", handledCount), | ||
zap.Int("deleted", deletedCount), | ||
zap.Duration("dur", time.Since(st)), | ||
) | ||
return | ||
} | ||
|
||
func (d *deleteLog) Close(ctx context.Context) (err error) { | ||
if d.ticker != nil { | ||
d.ticker.Close() | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package deletelog | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/anyproto/any-sync/app" | ||
"github.com/anyproto/any-sync/coordinator/coordinatorclient" | ||
"github.com/anyproto/any-sync/coordinator/coordinatorclient/mock_coordinatorclient" | ||
"github.com/anyproto/any-sync/coordinator/coordinatorproto" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/mock/gomock" | ||
|
||
"github.com/anyproto/any-sync-filenode/index" | ||
"github.com/anyproto/any-sync-filenode/index/mock_index" | ||
"github.com/anyproto/any-sync-filenode/redisprovider/testredisprovider" | ||
) | ||
|
||
var ctx = context.Background() | ||
|
||
func TestDeleteLog_checkLog(t *testing.T) { | ||
t.Run("no records", func(t *testing.T) { | ||
fx := newFixture(t) | ||
defer fx.finish(t) | ||
fx.coord.EXPECT().DeletionLog(ctx, "", recordsLimit).Return(nil, nil) | ||
require.NoError(t, fx.checkLog(ctx)) | ||
}) | ||
t.Run("success", func(t *testing.T) { | ||
fx := newFixture(t) | ||
defer fx.finish(t) | ||
now := time.Now().Unix() | ||
fx.coord.EXPECT().DeletionLog(ctx, "", recordsLimit).Return([]*coordinatorproto.DeletionLogRecord{ | ||
{ | ||
Id: "1", | ||
SpaceId: "s1", | ||
Status: coordinatorproto.DeletionLogRecordStatus_Ok, | ||
Timestamp: now, | ||
FileGroup: "f1", | ||
}, | ||
{ | ||
Id: "2", | ||
SpaceId: "s2", | ||
Status: coordinatorproto.DeletionLogRecordStatus_Remove, | ||
Timestamp: now, | ||
FileGroup: "f2", | ||
}, | ||
}, nil) | ||
fx.index.EXPECT().SpaceDelete(ctx, index.Key{ | ||
GroupId: "f2", | ||
SpaceId: "s2", | ||
}) | ||
require.NoError(t, fx.checkLog(ctx)) | ||
lastId, err := fx.redis.Get(ctx, lastKey).Result() | ||
require.NoError(t, err) | ||
assert.Equal(t, "2", lastId) | ||
}) | ||
|
||
} | ||
|
||
func newFixture(t *testing.T) *fixture { | ||
ctrl := gomock.NewController(t) | ||
fx := &fixture{ | ||
ctrl: ctrl, | ||
a: new(app.App), | ||
coord: mock_coordinatorclient.NewMockCoordinatorClient(ctrl), | ||
index: mock_index.NewMockIndex(ctrl), | ||
deleteLog: New().(*deleteLog), | ||
} | ||
fx.disableTicker = true | ||
fx.coord.EXPECT().Name().Return(coordinatorclient.CName).AnyTimes() | ||
fx.coord.EXPECT().Init(gomock.Any()).AnyTimes() | ||
fx.index.EXPECT().Name().Return(index.CName).AnyTimes() | ||
fx.index.EXPECT().Init(gomock.Any()).AnyTimes() | ||
fx.index.EXPECT().Run(gomock.Any()).AnyTimes() | ||
fx.index.EXPECT().Close(gomock.Any()).AnyTimes() | ||
|
||
fx.a.Register(testredisprovider.NewTestRedisProviderNum(7)).Register(fx.coord).Register(fx.index).Register(fx.deleteLog) | ||
require.NoError(t, fx.a.Start(ctx)) | ||
|
||
return fx | ||
} | ||
|
||
type fixture struct { | ||
ctrl *gomock.Controller | ||
a *app.App | ||
coord *mock_coordinatorclient.MockCoordinatorClient | ||
index *mock_index.MockIndex | ||
*deleteLog | ||
} | ||
|
||
func (fx *fixture) finish(t *testing.T) { | ||
require.NoError(t, fx.a.Close(ctx)) | ||
fx.ctrl.Finish() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.