Skip to content

Commit 4dfdf17

Browse files
authored
leveldb cleaner (keybase#16033)
* leveldb cleaner * fixes * cleanup * optimize iterator option * mobile bkg, lrusize, whitelist * clean ui button * cleanup * cleanup * store mobile in struct * release iterator first * protocol * rename * old test style * Revert "rename" This reverts commit 3ba1c7b. * rename * review comments * Cleanup db tests (keybase#16263) * Revert "old test style" This reverts commit e993c49f74b6faaf374a8dd8e53ce926a5130c2c. * fix t.Fatal in go routine
1 parent 235043f commit 4dfdf17

24 files changed

Lines changed: 787 additions & 229 deletions

File tree

‎go/chat/search/indexer.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (idx *Indexer) Start(ctx context.Context, uid gregor1.UID) {
114114
}
115115

116116
func (idx *Indexer) Stop(ctx context.Context) chan struct{} {
117-
defer idx.Trace(ctx, func() error { return nil }, "Start")()
117+
defer idx.Trace(ctx, func() error { return nil }, "Stop")()
118118
idx.Lock()
119119
defer idx.Unlock()
120120

‎go/client/cmd_db.go‎

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,70 @@ package client
66
import (
77
"encoding/base64"
88
"errors"
9+
"strconv"
10+
911
"github.com/keybase/cli"
1012
"github.com/keybase/client/go/libcmdline"
1113
"github.com/keybase/client/go/libkb"
1214
keybase1 "github.com/keybase/client/go/protocol/keybase1"
1315
"golang.org/x/net/context"
14-
"strconv"
1516
)
1617

18+
type CmdDbClean struct {
19+
libkb.Contextified
20+
dbType keybase1.DbType
21+
force bool
22+
}
23+
24+
func (c *CmdDbClean) ParseArgv(ctx *cli.Context) error {
25+
c.dbType = keybase1.DbType_MAIN
26+
if ctx.Bool("chat") {
27+
c.dbType = keybase1.DbType_CHAT
28+
}
29+
c.force = ctx.Bool("force")
30+
return nil
31+
}
32+
33+
func (c *CmdDbClean) Run() error {
34+
cli, err := GetCtlClient(c.G())
35+
if err != nil {
36+
return err
37+
}
38+
if err = RegisterProtocolsWithContext(nil, c.G()); err != nil {
39+
return err
40+
}
41+
return cli.DbClean(context.TODO(), keybase1.DbCleanArg{
42+
Force: c.force,
43+
DbType: c.dbType,
44+
})
45+
}
46+
47+
func NewCmdDbClean(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
48+
return cli.Command{
49+
Name: "clean",
50+
Usage: "Clean the local database of stale entries",
51+
Action: func(c *cli.Context) {
52+
cl.ChooseCommand(NewCmdDbCleanRunner(g), "clean", c)
53+
},
54+
Flags: []cli.Flag{
55+
cli.BoolFlag{
56+
Name: "chat, c",
57+
Usage: "Refer to the chat database.",
58+
},
59+
cli.BoolFlag{
60+
Name: "force, f",
61+
Usage: "Force clean, skip over configuration checks for db size",
62+
},
63+
},
64+
}
65+
}
66+
67+
func NewCmdDbCleanRunner(g *libkb.GlobalContext) *CmdDbClean {
68+
return &CmdDbClean{
69+
Contextified: libkb.NewContextified(g),
70+
}
71+
}
72+
1773
type CmdDbNuke struct {
1874
libkb.Contextified
1975
force bool
@@ -260,13 +316,21 @@ func NewCmdDb(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
260316
Name: "db",
261317
Subcommands: []cli.Command{
262318
NewCmdDbNuke(cl, g),
319+
NewCmdDbClean(cl, g),
263320
NewCmdDbDelete(cl, g),
264321
NewCmdDbGet(cl, g),
265322
NewCmdDbPut(cl, g),
266323
},
267324
}
268325
}
269326

327+
func (c *CmdDbClean) GetUsage() libkb.Usage {
328+
return libkb.Usage{
329+
Config: true,
330+
API: true,
331+
}
332+
}
333+
270334
func (c *CmdDbNuke) GetUsage() libkb.Usage {
271335
return libkb.Usage{
272336
Config: true,

‎go/libkb/db.go‎

Lines changed: 111 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,112 @@ import (
1414
jsonw "github.com/keybase/go-jsonw"
1515
)
1616

17+
const (
18+
DBUser = 0x00
19+
DBSig = 0x0f
20+
DBTeamChain = 0x10
21+
DBUserPlusAllKeysV1 = 0x19
22+
23+
DBOfflineRPC = 0xbe
24+
DBChatCollapses = 0xbf
25+
DBMerkleAudit = 0xca
26+
DBUnfurler = 0xcb
27+
DBStellarDisclaimer = 0xcc
28+
DBFTLStorage = 0xcd
29+
DBTeamAuditor = 0xce
30+
DBAttachmentUploader = 0xcf
31+
DBHasRandomPW = 0xd0
32+
DBDiskLRUEntries = 0xda
33+
DBDiskLRUIndex = 0xdb
34+
DBImplicitTeamConflictInfo = 0xdc
35+
DBUidToFullName = 0xdd
36+
DBUidToUsername = 0xde
37+
DBUserPlusKeysVersioned = 0xdf
38+
DBLink = 0xe0
39+
DBLocalTrack = 0xe1
40+
DBPGPKey = 0xe3
41+
DBSigHints = 0xe4
42+
DBProofCheck = 0xe5
43+
DBUserSecretKeys = 0xe6
44+
DBSigChainTailPublic = 0xe7
45+
DBSigChainTailSemiprivate = 0xe8
46+
DBSigChainTailEncrypted = 0xe9
47+
DBChatActive = 0xea
48+
DBUserEKBox = 0xeb
49+
DBTeamEKBox = 0xec
50+
DBChatIndex = 0xed
51+
DBMerkleRoot = 0xf0
52+
DBTrackers = 0xf1
53+
DBGregor = 0xf2
54+
DBTrackers2 = 0xf3
55+
DBTrackers2Reverse = 0xf4
56+
DBNotificationDismiss = 0xf5
57+
DBChatBlockIndex = 0xf6
58+
DBChatBlocks = 0xf7
59+
DBChatOutbox = 0xf8
60+
DBChatInbox = 0xf9
61+
DBIdentify = 0xfa
62+
DBResolveUsernameToUID = 0xfb
63+
DBChatBodyHashIndex = 0xfc
64+
DBMerkleStore = 0xfd
65+
DBChatConvFailures = 0xfe
66+
DBTeamList = 0xff
67+
)
68+
69+
const (
70+
DBLookupUsername = 0x00
71+
DBLookupMerkleRoot = 0x01
72+
)
73+
74+
func DbKeyUID(t ObjType, uid keybase1.UID) DbKey {
75+
return DbKey{Typ: t, Key: uid.String()}
76+
}
77+
78+
func DbKeyNotificationDismiss(prefix string, username NormalizedUsername) DbKey {
79+
return DbKey{
80+
Typ: DBNotificationDismiss,
81+
Key: fmt.Sprintf("%s:%s", prefix, username),
82+
}
83+
}
84+
85+
// IsPermDbKey returns true for keys ignored by the leveldb cleaner and always
86+
// persisted to disk. Ideally these keys handling some cleanup/size bounding
87+
// themselves.
88+
func IsPermDbKey(typ ObjType) bool {
89+
switch typ {
90+
case DBDiskLRUEntries,
91+
DBDiskLRUIndex,
92+
DBOfflineRPC,
93+
DBChatCollapses,
94+
DBHasRandomPW,
95+
DBStellarDisclaimer:
96+
return true
97+
default:
98+
return false
99+
}
100+
}
101+
102+
type ObjType byte
103+
104+
type DbKey struct {
105+
Typ ObjType
106+
Key string
107+
}
108+
109+
// tablePrefix builds a key prefix for the given table for use in `util.Range`
110+
// or `util.BytesPrefix`
111+
func tablePrefix(table string) []byte {
112+
return []byte(fmt.Sprintf("%s:", table))
113+
}
114+
17115
func (k DbKey) ToString(table string) string {
18116
return fmt.Sprintf("%s:%02x:%s", table, k.Typ, k.Key)
19117
}
20118

21119
func (k DbKey) ToBytes(table string) []byte {
120+
if IsPermDbKey(k.Typ) {
121+
table = levelDbTablePerm
122+
}
22123
return []byte(k.ToString(table))
23124
}
24125

@@ -56,6 +157,7 @@ func jsonLocalDbGet(ops LocalDbOps, id DbKey) (*jsonw.Wrapper, error) {
56157
}
57158
return ret, err
58159
}
160+
59161
func jsonLocalDbGetInto(ops LocalDbOps, obj interface{}, id DbKey) (found bool, err error) {
60162
var buf []byte
61163
buf, found, err = ops.Get(id)
@@ -68,6 +170,7 @@ func jsonLocalDbGetInto(ops LocalDbOps, obj interface{}, id DbKey) (found bool,
68170
}
69171
return found, err
70172
}
173+
71174
func jsonLocalDbPutObj(ops LocalDbOps, id DbKey, aliases []DbKey, obj interface{}) (err error) {
72175
var bytes []byte
73176
bytes, err = json.Marshal(obj)
@@ -76,6 +179,7 @@ func jsonLocalDbPutObj(ops LocalDbOps, id DbKey, aliases []DbKey, obj interface{
76179
}
77180
return err
78181
}
182+
79183
func jsonLocalDbLookup(ops LocalDbOps, id DbKey) (*jsonw.Wrapper, error) {
80184
bytes, found, err := ops.Lookup(id)
81185
var ret *jsonw.Wrapper
@@ -107,12 +211,13 @@ type JSONLocalDb struct {
107211
engine LocalDb
108212
}
109213

110-
func NewJSONLocalDb(e LocalDb) *JSONLocalDb { return &JSONLocalDb{e} }
111-
func (j *JSONLocalDb) Open() error { return j.engine.Open() }
112-
func (j *JSONLocalDb) ForceOpen() error { return j.engine.ForceOpen() }
113-
func (j *JSONLocalDb) Close() error { return j.engine.Close() }
114-
func (j *JSONLocalDb) Nuke() (string, error) { return j.engine.Nuke() }
115-
func (j *JSONLocalDb) Stats() string { return j.engine.Stats() }
214+
func NewJSONLocalDb(e LocalDb) *JSONLocalDb { return &JSONLocalDb{e} }
215+
func (j *JSONLocalDb) Open() error { return j.engine.Open() }
216+
func (j *JSONLocalDb) ForceOpen() error { return j.engine.ForceOpen() }
217+
func (j *JSONLocalDb) Close() error { return j.engine.Close() }
218+
func (j *JSONLocalDb) Nuke() (string, error) { return j.engine.Nuke() }
219+
func (j *JSONLocalDb) Clean(force bool) error { return j.engine.Clean(force) }
220+
func (j *JSONLocalDb) Stats() string { return j.engine.Stats() }
116221

117222
func (j *JSONLocalDb) PutRaw(id DbKey, b []byte) error { return j.engine.Put(id, nil, b) }
118223
func (j *JSONLocalDb) GetRaw(id DbKey) ([]byte, bool, error) { return j.engine.Get(id) }
@@ -196,71 +301,3 @@ func (j JSONLocalDbTransaction) Commit() error {
196301
func (j JSONLocalDbTransaction) Discard() {
197302
j.tr.Discard()
198303
}
199-
200-
const (
201-
DBUser = 0x00
202-
DBSig = 0x0f
203-
DBTeamChain = 0x10
204-
DBUserPlusAllKeysV1 = 0x19
205-
206-
DBOfflineRPC = 0xbe
207-
DBChatCollapses = 0xbf
208-
DBMerkleAudit = 0xca
209-
DBUnfurler = 0xcb
210-
DBStellarDisclaimer = 0xcc
211-
DBFTLStorage = 0xcd
212-
DBTeamAuditor = 0xce
213-
DBAttachmentUploader = 0xcf
214-
DBHasRandomPW = 0xd0
215-
DBDiskLRUEntries = 0xda
216-
DBDiskLRUIndex = 0xdb
217-
DBImplicitTeamConflictInfo = 0xdc
218-
DBUidToFullName = 0xdd
219-
DBUidToUsername = 0xde
220-
DBUserPlusKeysVersioned = 0xdf
221-
DBLink = 0xe0
222-
DBLocalTrack = 0xe1
223-
DBPGPKey = 0xe3
224-
DBSigHints = 0xe4
225-
DBProofCheck = 0xe5
226-
DBUserSecretKeys = 0xe6
227-
DBSigChainTailPublic = 0xe7
228-
DBSigChainTailSemiprivate = 0xe8
229-
DBSigChainTailEncrypted = 0xe9
230-
DBChatActive = 0xea
231-
DBUserEKBox = 0xeb
232-
DBTeamEKBox = 0xec
233-
DBChatIndex = 0xed
234-
DBMerkleRoot = 0xf0
235-
DBTrackers = 0xf1
236-
DBGregor = 0xf2
237-
DBTrackers2 = 0xf3
238-
DBTrackers2Reverse = 0xf4
239-
DBNotificationDismiss = 0xf5
240-
DBChatBlockIndex = 0xf6
241-
DBChatBlocks = 0xf7
242-
DBChatOutbox = 0xf8
243-
DBChatInbox = 0xf9
244-
DBIdentify = 0xfa
245-
DBResolveUsernameToUID = 0xfb
246-
DBChatBodyHashIndex = 0xfc
247-
DBMerkleStore = 0xfd
248-
DBChatConvFailures = 0xfe
249-
DBTeamList = 0xff
250-
)
251-
252-
const (
253-
DBLookupUsername = 0x00
254-
DBLookupMerkleRoot = 0x01
255-
)
256-
257-
func DbKeyUID(t ObjType, uid keybase1.UID) DbKey {
258-
return DbKey{Typ: t, Key: uid.String()}
259-
}
260-
261-
func DbKeyNotificationDismiss(prefix string, username NormalizedUsername) DbKey {
262-
return DbKey{
263-
Typ: DBNotificationDismiss,
264-
Key: fmt.Sprintf("%s:%s", prefix, username),
265-
}
266-
}

‎go/libkb/globals.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,14 @@ func (g *GlobalContext) GetAppType() AppType {
644644
return g.Env.GetAppType()
645645
}
646646

647+
func (g *GlobalContext) IsMobileAppType() bool {
648+
return g.Env.GetAppType() == MobileAppType
649+
}
650+
651+
func (g *GlobalContext) IsDesktopAppType() bool {
652+
return g.Env.GetAppType() == DesktopAppType
653+
}
654+
647655
func (g *GlobalContext) ConfigureExportedStreams() error {
648656
g.XStreams = NewExportedStreams()
649657
return nil

‎go/libkb/interfaces.go‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,6 @@ type CommandLine interface {
124124
type Server interface {
125125
}
126126

127-
type ObjType byte
128-
129-
type DbKey struct {
130-
Typ ObjType
131-
Key string
132-
}
133-
134127
type LocalDbOps interface {
135128
Put(id DbKey, aliases []DbKey, value []byte) error
136129
Delete(id DbKey) error
@@ -151,6 +144,7 @@ type LocalDb interface {
151144
ForceOpen() error
152145
Close() error
153146
Nuke() (string, error)
147+
Clean(force bool) error
154148
OpenTransaction() (LocalDbTransaction, error)
155149
}
156150

0 commit comments

Comments
 (0)