Skip to content

Commit

Permalink
Fix reindexing triggered by shares by using the event's SpaceOwner
Browse files Browse the repository at this point in the history
  • Loading branch information
aduffeck committed Oct 12, 2022
1 parent 7ed3134 commit 18394a1
Showing 1 changed file with 29 additions and 43 deletions.
72 changes: 29 additions & 43 deletions services/search/pkg/search/provider/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,31 @@ func (d *SpaceDebouncer) Debounce(id *provider.StorageSpaceId, userID *user.User
}

func (p *Provider) handleEvent(ev interface{}) {
var ref *provider.Reference
var owner *user.User
switch e := ev.(type) {
case events.ItemTrashed:
p.logger.Debug().Interface("event", ev).Msg("marking document as deleted")
ref = e.Ref
owner = &user.User{
Id: e.Executant,
}
err := p.indexClient.Delete(e.ID)
if err != nil {
p.logger.Error().Err(err).Interface("Id", e.ID).Msg("failed to remove item from index")
}
p.reindexSpace(ev, e.Ref, e.Executant, e.SpaceOwner)
case events.ItemRestored:
p.logger.Debug().Interface("event", ev).Msg("marking document as restored")
ref = e.Ref
owner = &user.User{
owner := &user.User{
Id: e.Executant,
}

ownerCtx, err := p.getAuthContext(owner)
if err != nil {
return
}
statRes, err := p.statResource(ownerCtx, ref, owner)
statRes, err := p.statResource(ownerCtx, e.Ref, owner)
if err != nil {
p.logger.Error().Err(err).
Str("storageid", ref.GetResourceId().GetStorageId()).
Str("spaceid", ref.GetResourceId().GetSpaceId()).
Str("opaqueid", ref.GetResourceId().GetOpaqueId()).
Str("path", ref.GetPath()).
Str("storageid", e.Ref.GetResourceId().GetStorageId()).
Str("spaceid", e.Ref.GetResourceId().GetSpaceId()).
Str("opaqueid", e.Ref.GetResourceId().GetOpaqueId()).
Str("path", e.Ref.GetPath()).
Msg("failed to make stat call for the restored resource")
return
}
Expand All @@ -87,32 +81,32 @@ func (p *Provider) handleEvent(ev interface{}) {
err = p.indexClient.Restore(statRes.Info.Id)
if err != nil {
p.logger.Error().Err(err).
Str("storageid", ref.GetResourceId().GetStorageId()).
Str("spaceid", ref.GetResourceId().GetSpaceId()).
Str("opaqueid", ref.GetResourceId().GetOpaqueId()).
Str("path", ref.GetPath()).
Str("storageid", e.Ref.GetResourceId().GetStorageId()).
Str("spaceid", e.Ref.GetResourceId().GetSpaceId()).
Str("opaqueid", e.Ref.GetResourceId().GetOpaqueId()).
Str("path", e.Ref.GetPath()).
Msg("failed to restore the changed resource in the index")
}
default:
p.logger.Error().Interface("statRes", statRes).
Str("storageid", ref.GetResourceId().GetStorageId()).
Str("spaceid", ref.GetResourceId().GetSpaceId()).
Str("opaqueid", ref.GetResourceId().GetOpaqueId()).
Str("path", ref.GetPath()).
Str("storageid", e.Ref.GetResourceId().GetStorageId()).
Str("spaceid", e.Ref.GetResourceId().GetSpaceId()).
Str("opaqueid", e.Ref.GetResourceId().GetOpaqueId()).
Str("path", e.Ref.GetPath()).
Msg("failed to stat the restored resource")
}
p.reindexSpace(ev, e.Ref, e.Executant, e.SpaceOwner)
case events.ItemMoved:
p.logger.Debug().Interface("event", ev).Msg("resource has been moved, updating the document")
ref = e.Ref
owner = &user.User{
owner := &user.User{
Id: e.Executant,
}

ownerCtx, err := p.getAuthContext(owner)
if err != nil {
return
}
statRes, err := p.statResource(ownerCtx, ref, owner)
statRes, err := p.statResource(ownerCtx, e.Ref, owner)
if err != nil {
p.logger.Error().Err(err).Msg("failed to stat the moved resource")
return
Expand All @@ -124,42 +118,34 @@ func (p *Provider) handleEvent(ev interface{}) {

gpRes, err := p.getPath(ownerCtx, statRes.Info.Id, owner)
if err != nil {
p.logger.Error().Err(err).Interface("ref", ref).Msg("failed to get path for moved resource")
p.logger.Error().Err(err).Interface("ref", e.Ref).Msg("failed to get path for moved resource")
return
}
if gpRes.Status.Code != rpcv1beta1.Code_CODE_OK {
p.logger.Error().Interface("status", gpRes.Status).Interface("ref", ref).Msg("failed to get path for moved resource")
p.logger.Error().Interface("status", gpRes.Status).Interface("ref", e.Ref).Msg("failed to get path for moved resource")
return
}

err = p.indexClient.Move(statRes.GetInfo().GetId(), statRes.GetInfo().GetParentId(), gpRes.Path)
if err != nil {
p.logger.Error().Err(err).Msg("failed to move the changed resource in the index")
}
p.reindexSpace(ev, e.Ref, e.Executant, e.SpaceOwner)
case events.ContainerCreated:
ref = e.Ref
owner = &user.User{
Id: e.Executant,
}
p.reindexSpace(ev, e.Ref, e.Executant, e.SpaceOwner)
case events.FileUploaded:
ref = e.Ref
owner = &user.User{
Id: e.Executant,
}
p.reindexSpace(ev, e.Ref, e.Executant, e.SpaceOwner)
case events.FileTouched:
ref = e.Ref
owner = &user.User{
Id: e.Executant,
}
p.reindexSpace(ev, e.Ref, e.Executant, e.SpaceOwner)
case events.FileVersionRestored:
ref = e.Ref
owner = &user.User{
Id: e.Executant,
}
p.reindexSpace(ev, e.Ref, e.Executant, e.SpaceOwner)
default:
// Not sure what to do here. Skip.
return
}
}

func (p *Provider) reindexSpace(ev interface{}, ref *provider.Reference, executant, owner *user.UserId) {
p.logger.Debug().Interface("event", ev).Msg("resource has been changed, scheduling a space resync")

spaceID := &provider.StorageSpaceId{
Expand All @@ -168,7 +154,7 @@ func (p *Provider) handleEvent(ev interface{}) {
SpaceId: ref.GetResourceId().GetSpaceId(),
}),
}
p.indexSpaceDebouncer.Debounce(spaceID, owner.Id)
p.indexSpaceDebouncer.Debounce(spaceID, owner)
}

func (p *Provider) statResource(ctx context.Context, ref *provider.Reference, owner *user.User) (*provider.StatResponse, error) {
Expand Down

0 comments on commit 18394a1

Please sign in to comment.