Skip to content

Commit 74fd452

Browse files
authored
memory lru implicit team lookup (keybase#14990)
* disk lru implicit team lookup * nil cacher in engine tests * use in memory cache, logout hook
1 parent b74ffec commit 74fd452

9 files changed

Lines changed: 99 additions & 26 deletions

File tree

‎go/libkb/constants.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ const (
115115
UPAKCacheSize = 2000
116116
UIDMapFullNameCacheSize = 100000
117117
ImplicitTeamConflictInfoCacheSize = 10000
118+
ImplicitTeamCacheSize = 10000
118119

119120
PayloadCacheSize = 1000
120121

‎go/libkb/globals.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type GlobalContext struct {
8080
teamEKBoxStorage TeamEKBoxStorage // Store team ephemeral key boxes
8181
ekLib EKLib // Wrapper to call ephemeral key methods
8282
itciCacher LRUer // Cacher for implicit team conflict info
83+
iteamCacher MemLRUer // In memory cacher for implicit teams
8384
cardCache *UserCardCache // cache of keybase1.UserCard objects
8485
fullSelfer FullSelfer // a loader that gets the full self object
8586
pvlSource MerkleStore // a cache and fetcher for pvl
@@ -603,6 +604,18 @@ func (g *GlobalContext) SetImplicitTeamConflictInfoCacher(l LRUer) {
603604
g.itciCacher = l
604605
}
605606

607+
func (g *GlobalContext) GetImplicitTeamCacher() MemLRUer {
608+
g.cacheMu.RLock()
609+
defer g.cacheMu.RUnlock()
610+
return g.iteamCacher
611+
}
612+
613+
func (g *GlobalContext) SetImplicitTeamCacher(l MemLRUer) {
614+
g.cacheMu.RLock()
615+
defer g.cacheMu.RUnlock()
616+
g.iteamCacher = l
617+
}
618+
606619
func (g *GlobalContext) GetFullSelfer() FullSelfer {
607620
g.cacheMu.RLock()
608621
defer g.cacheMu.RUnlock()

‎go/libkb/interfaces.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,11 @@ type LRUer interface {
776776
Put(context.Context, LRUContext, LRUKeyer, interface{}) error
777777
}
778778

779+
type MemLRUer interface {
780+
Get(key interface{}) (interface{}, bool)
781+
Put(key, value interface{}) bool
782+
}
783+
779784
type ClockContext interface {
780785
GetClock() clockwork.Clock
781786
}

‎go/lru/lru.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package lru
22

33
import (
44
json "encoding/json"
5+
"reflect"
6+
"sync"
7+
58
lru "github.com/hashicorp/golang-lru"
69
libkb "github.com/keybase/client/go/libkb"
710
keybase1 "github.com/keybase/client/go/protocol/keybase1"
811
jsonw "github.com/keybase/go-jsonw"
912
context "golang.org/x/net/context"
10-
"reflect"
11-
"sync"
1213
)
1314

1415
type Cache struct {

‎go/protocol/keybase1/extras.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,8 +2395,8 @@ func (n ImplicitTeamDisplayName) String() string {
23952395
return name
23962396
}
23972397

2398-
func (c ImplicitTeamConflictInfo) IsConflict() bool {
2399-
return c.Generation > ConflictGeneration(0)
2398+
func (c *ImplicitTeamConflictInfo) IsConflict() bool {
2399+
return c != nil && c.Generation > ConflictGeneration(0)
24002400
}
24012401

24022402
const (

‎go/teams/conflicts.go‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package teams
22

33
import (
44
"fmt"
5+
56
"github.com/keybase/client/go/libkb"
67
"github.com/keybase/client/go/lru"
78
"github.com/keybase/client/go/protocol/keybase1"
@@ -50,7 +51,7 @@ func GetConflictInfo(ctx context.Context, g *libkb.GlobalContext, id keybase1.Te
5051
key := conflictID{name.IsPublic, id}
5152
cv, err := g.GetImplicitTeamConflictInfoCacher().Get(ctx, g, key)
5253
if err != nil {
53-
g.Log.CWarningf(ctx, "In fetching from cache: %s", err.Error())
54+
g.Log.CDebugf(ctx, "In fetching from cache: %s", err.Error())
5455
}
5556
if cv != nil {
5657
if p, ok := cv.(*keybase1.ImplicitTeamConflictInfo); ok {
@@ -59,7 +60,7 @@ func GetConflictInfo(ctx context.Context, g *libkb.GlobalContext, id keybase1.Te
5960
}
6061
return ret, nil
6162
}
62-
g.Log.CWarningf(ctx, "Bad element of wrong type from cache: %T", cv)
63+
g.Log.CDebugf(ctx, "Bad element of wrong type from cache: %T", cv)
6364
}
6465

6566
displayName, err := FormatImplicitTeamDisplayName(ctx, g, name)
@@ -85,23 +86,14 @@ func GetConflictInfo(ctx context.Context, g *libkb.GlobalContext, id keybase1.Te
8586
ci := raw.ConflictInfo
8687
ret.ConflictInfo = ci
8788

88-
// The server will return an empty conflict info if there is no conflict info
89-
// for this teamID/displayName. But we still want to cache a value in the DB
90-
// even if there is no conflict. So by convention, cache the "trivial" conflict
91-
// which is a generation of 0.
92-
if ci == nil {
93-
// Note that now, ci.IsConflict() is false!
94-
ci = &keybase1.ImplicitTeamConflictInfo{}
95-
}
96-
9789
// If the team is not fully resolved, and there isn't a conflict, there might
9890
// still become a conflict in the future, so we decide not to cache it.
9991
// Otherwise, the answer stays true indefinitely, so we can cache the value
10092
// without fear of staleness.
10193
if isFullyResolved || ci.IsConflict() {
10294
tmpErr := g.GetImplicitTeamConflictInfoCacher().Put(ctx, g, key, ci)
10395
if tmpErr != nil {
104-
g.Log.CWarningf(ctx, "Failed to cached implicit team conflict info: %s", tmpErr.Error())
96+
g.Log.CDebugf(ctx, "Failed to cached implicit team conflict info: %s", tmpErr.Error())
10597
}
10698
}
10799

‎go/teams/implicit.go‎

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package teams
33
import (
44
"fmt"
55
"sort"
6+
"strings"
67

8+
lru "github.com/hashicorp/golang-lru"
79
"github.com/keybase/client/go/kbfs"
810
"github.com/keybase/client/go/libkb"
911
"github.com/keybase/client/go/protocol/keybase1"
@@ -37,7 +39,6 @@ func (i *implicitTeam) GetAppStatus() *libkb.AppStatus {
3739
// Resolves social assertions.
3840
func LookupImplicitTeam(ctx context.Context, g *libkb.GlobalContext, displayName string, public bool) (
3941
team *Team, teamName keybase1.TeamName, impTeamName keybase1.ImplicitTeamDisplayName, err error) {
40-
4142
team, teamName, impTeamName, _, err = LookupImplicitTeamAndConflicts(ctx, g, displayName, public)
4243
return team, teamName, impTeamName, err
4344
}
@@ -55,13 +56,36 @@ func LookupImplicitTeamAndConflicts(ctx context.Context, g *libkb.GlobalContext,
5556

5657
func LookupImplicitTeamIDUntrusted(ctx context.Context, g *libkb.GlobalContext, displayName string,
5758
public bool) (res keybase1.TeamID, err error) {
58-
imp, err := loadImpteamFromServer(ctx, g, displayName, public)
59+
imp, err := loadImpteam(ctx, g, displayName, public)
5960
if err != nil {
6061
return res, err
6162
}
6263
return imp.TeamID, nil
6364
}
6465

66+
func loadImpteam(ctx context.Context, g *libkb.GlobalContext, displayName string, public bool) (imp implicitTeam, err error) {
67+
cacheKey := impTeamCacheKey(displayName, public)
68+
cacher := g.GetImplicitTeamCacher()
69+
if cacher != nil {
70+
if cv, ok := cacher.Get(cacheKey); ok {
71+
if imp, ok := cv.(implicitTeam); ok {
72+
g.Log.CDebugf(ctx, "using cached iteam")
73+
return imp, nil
74+
}
75+
g.Log.CDebugf(ctx, "Bad element of wrong type from cache: %T", cv)
76+
}
77+
}
78+
imp, err = loadImpteamFromServer(ctx, g, displayName, public)
79+
if err != nil {
80+
return imp, err
81+
}
82+
// If the team has any assertions just skip caching
83+
if cacher != nil && !strings.Contains(imp.DisplayName, "@") {
84+
cacher.Put(cacheKey, imp)
85+
}
86+
return imp, nil
87+
}
88+
6589
func loadImpteamFromServer(ctx context.Context, g *libkb.GlobalContext, displayName string, public bool) (imp implicitTeam, err error) {
6690
arg := libkb.NewAPIArgWithNetContext(ctx, "team/implicit")
6791
arg.SessionType = libkb.APISessionTypeOPTIONAL
@@ -104,7 +128,7 @@ func lookupImplicitTeamAndConflicts(ctx context.Context, g *libkb.GlobalContext,
104128
if err != nil {
105129
return team, teamName, impTeamName, conflicts, err
106130
}
107-
imp, err := loadImpteamFromServer(ctx, g, lookupNameWithoutConflict, impTeamName.IsPublic)
131+
imp, err := loadImpteam(ctx, g, lookupNameWithoutConflict, impTeamName.IsPublic)
108132
if err != nil {
109133
return team, teamName, impTeamName, conflicts, err
110134
}
@@ -120,7 +144,7 @@ func lookupImplicitTeamAndConflicts(ctx context.Context, g *libkb.GlobalContext,
120144

121145
if err != nil {
122146
// warn, don't fail
123-
g.Log.CWarningf(ctx, "LookupImplicitTeam got conflict suffix: %v", err)
147+
g.Log.CDebugf(ctx, "LookupImplicitTeam got conflict suffix: %v", err)
124148
err = nil
125149
continue
126150
}
@@ -270,7 +294,7 @@ func formatImplicitTeamDisplayNameCommon(ctx context.Context, g *libkb.GlobalCon
270294
}
271295

272296
var suffix string
273-
if impTeamName.ConflictInfo != nil && impTeamName.ConflictInfo.IsConflict() {
297+
if impTeamName.ConflictInfo.IsConflict() {
274298
suffix = libkb.FormatImplicitTeamDisplayNameSuffix(*impTeamName.ConflictInfo)
275299
}
276300

@@ -295,3 +319,42 @@ func sortStringsFront(ss []string, front string) {
295319
return a < b
296320
})
297321
}
322+
323+
func impTeamCacheKey(displayName string, public bool) string {
324+
return fmt.Sprintf("%s-%v", displayName, public)
325+
}
326+
327+
type implicitTeamCache struct {
328+
cache *lru.Cache
329+
}
330+
331+
func newImplicitTeamCache(g *libkb.GlobalContext) *implicitTeamCache {
332+
cache, err := lru.New(libkb.ImplicitTeamCacheSize)
333+
if err != nil {
334+
panic(err)
335+
}
336+
return &implicitTeamCache{
337+
cache: cache,
338+
}
339+
}
340+
341+
func (i *implicitTeamCache) Get(key interface{}) (interface{}, bool) {
342+
return i.cache.Get(key)
343+
}
344+
345+
func (i *implicitTeamCache) Put(key, value interface{}) bool {
346+
return i.cache.Add(key, value)
347+
}
348+
349+
func (i *implicitTeamCache) OnLogout(m libkb.MetaContext) error {
350+
i.cache.Purge()
351+
return nil
352+
}
353+
354+
var _ libkb.MemLRUer = &implicitTeamCache{}
355+
356+
func NewImplicitTeamCacheAndInstall(g *libkb.GlobalContext) {
357+
cache := newImplicitTeamCache(g)
358+
g.SetImplicitTeamCacher(cache)
359+
g.AddLogoutHook(cache)
360+
}

‎go/teams/init.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ func ServiceInit(g *libkb.GlobalContext) {
99
NewFastTeamLoaderAndInstall(g)
1010
NewAuditorAndInstall(g)
1111
NewImplicitTeamConflictInfoCacheAndInstall(g)
12+
NewImplicitTeamCacheAndInstall(g)
1213
}

‎go/teams/resolve.go‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,10 @@ func ResolveImplicitTeamDisplayName(ctx context.Context, g *libkb.GlobalContext,
100100
}
101101

102102
var resolvedAssertions []libkb.ResolvedAssertion
103-
104-
err = ResolveImplicitTeamSetUntrusted(ctx, g, writerAssertions, &res.Writers, &resolvedAssertions)
105-
if err != nil {
103+
if err = ResolveImplicitTeamSetUntrusted(ctx, g, writerAssertions, &res.Writers, &resolvedAssertions); err != nil {
106104
return res, err
107105
}
108-
err = ResolveImplicitTeamSetUntrusted(ctx, g, readerAssertions, &res.Readers, &resolvedAssertions)
109-
if err != nil {
106+
if err = ResolveImplicitTeamSetUntrusted(ctx, g, readerAssertions, &res.Readers, &resolvedAssertions); err != nil {
110107
return res, err
111108
}
112109

0 commit comments

Comments
 (0)