-
Notifications
You must be signed in to change notification settings - Fork 18
/
tendermint-cleveldb-close-batch.patch
201 lines (184 loc) · 5.72 KB
/
tendermint-cleveldb-close-batch.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
diff --git a/libs/db/c_level_db.go b/libs/db/c_level_db.go
index 7f74b2a7..81d8be7c 100644
--- a/libs/db/c_level_db.go
+++ b/libs/db/c_level_db.go
@@ -21,17 +21,22 @@ func init() {
var _ DB = (*CLevelDB)(nil)
type CLevelDB struct {
- db *levigo.DB
- ro *levigo.ReadOptions
- wo *levigo.WriteOptions
- woSync *levigo.WriteOptions
+ db *levigo.DB
+ ro *levigo.ReadOptions
+ wo *levigo.WriteOptions
+ woSync *levigo.WriteOptions
+ cache *levigo.Cache
+ filterPolicy *levigo.FilterPolicy
}
func NewCLevelDB(name string, dir string) (*CLevelDB, error) {
dbPath := filepath.Join(dir, name+".db")
+ cache := levigo.NewLRUCache(500 * 1024 * 1024) // XXX(yumin): let's try use a smaller cache.
+ filter := levigo.NewBloomFilter(10)
opts := levigo.NewOptions()
- opts.SetCache(levigo.NewLRUCache(1 << 30))
+ opts.SetCache(cache)
+ opts.SetFilterPolicy(filter)
opts.SetCreateIfMissing(true)
db, err := levigo.Open(dbPath, opts)
if err != nil {
@@ -42,10 +47,12 @@ func NewCLevelDB(name string, dir string) (*CLevelDB, error) {
woSync := levigo.NewWriteOptions()
woSync.SetSync(true)
database := &CLevelDB{
- db: db,
- ro: ro,
- wo: wo,
- woSync: woSync,
+ db: db,
+ ro: ro,
+ wo: wo,
+ woSync: woSync,
+ cache: cache,
+ filterPolicy: filter,
}
return database, nil
}
@@ -113,6 +120,8 @@ func (db *CLevelDB) Close() {
db.ro.Close()
db.wo.Close()
db.woSync.Close()
+ db.cache.Close()
+ db.filterPolicy.Close()
}
// Implements DB.
@@ -179,6 +188,11 @@ func (mBatch *cLevelDBBatch) WriteSync() {
}
}
+// Implements Batch.
+func (mBatch *cLevelDBBatch) Close() {
+ mBatch.batch.Close()
+}
+
//----------------------------------------
// Iterator
// NOTE This is almost identical to db/go_level_db.Iterator
diff --git a/libs/db/debug_db.go b/libs/db/debug_db.go
index bb361a26..658cd055 100644
--- a/libs/db/debug_db.go
+++ b/libs/db/debug_db.go
@@ -250,3 +250,8 @@ func (dbch debugBatch) WriteSync() {
fmt.Printf("%v.batch.WriteSync()\n", dbch.label)
dbch.bch.WriteSync()
}
+
+// Implements Batch.
+func (dbch debugBatch) Close() {
+ dbch.bch.Close()
+}
diff --git a/libs/db/go_level_db.go b/libs/db/go_level_db.go
index fd487a4d..9a4358f6 100644
--- a/libs/db/go_level_db.go
+++ b/libs/db/go_level_db.go
@@ -184,6 +184,10 @@ func (mBatch *goLevelDBBatch) WriteSync() {
}
}
+// Implements Batch.
+// Close is no-op for goLevelDBBatch.
+func (mBatch *goLevelDBBatch) Close() {}
+
//----------------------------------------
// Iterator
// NOTE This is almost identical to db/c_level_db.Iterator
diff --git a/libs/db/mem_batch.go b/libs/db/mem_batch.go
index 5c5d0c13..ebba43f5 100644
--- a/libs/db/mem_batch.go
+++ b/libs/db/mem_batch.go
@@ -46,6 +46,10 @@ func (mBatch *memBatch) WriteSync() {
mBatch.write(true)
}
+func (mBatch *memBatch) Close() {
+ mBatch.ops = nil
+}
+
func (mBatch *memBatch) write(doSync bool) {
if mtx := mBatch.db.Mutex(); mtx != nil {
mtx.Lock()
diff --git a/libs/db/prefix_db.go b/libs/db/prefix_db.go
index 40d72560..0dd06ef9 100644
--- a/libs/db/prefix_db.go
+++ b/libs/db/prefix_db.go
@@ -248,6 +248,10 @@ func (pb prefixBatch) WriteSync() {
pb.source.WriteSync()
}
+func (pb prefixBatch) Close() {
+ pb.source.Close()
+}
+
//----------------------------------------
// prefixIterator
diff --git a/libs/db/remotedb/grpcdb/server.go b/libs/db/remotedb/grpcdb/server.go
index 3a9955dd..bfe65e61 100644
--- a/libs/db/remotedb/grpcdb/server.go
+++ b/libs/db/remotedb/grpcdb/server.go
@@ -180,6 +180,7 @@ func (s *server) BatchWriteSync(c context.Context, b *protodb.Batch) (*protodb.N
func (s *server) batchWrite(c context.Context, b *protodb.Batch, sync bool) (*protodb.Nothing, error) {
bat := s.db.NewBatch()
+ defer bat.Close()
for _, op := range b.Ops {
switch op.Type {
case protodb.Operation_SET:
diff --git a/libs/db/remotedb/remotedb.go b/libs/db/remotedb/remotedb.go
index 2b60d815..c70d54b9 100644
--- a/libs/db/remotedb/remotedb.go
+++ b/libs/db/remotedb/remotedb.go
@@ -260,3 +260,7 @@ func (bat *batch) WriteSync() {
panic(fmt.Sprintf("RemoteDB.BatchWriteSync: %v", err))
}
}
+
+func (bat *batch) Close() {
+ bat.ops = nil
+}
diff --git a/libs/db/types.go b/libs/db/types.go
index 9b9c6d0b..30f8afd1 100644
--- a/libs/db/types.go
+++ b/libs/db/types.go
@@ -57,10 +57,12 @@ type DB interface {
//----------------------------------------
// Batch
+// Batch Close must be called when the program no longer needs the object.
type Batch interface {
SetDeleter
Write()
WriteSync()
+ Close()
}
type SetDeleter interface {
diff --git a/lite/dbprovider.go b/lite/dbprovider.go
index ef1b2a59..9a3636d5 100644
--- a/lite/dbprovider.go
+++ b/lite/dbprovider.go
@@ -54,6 +54,7 @@ func (dbp *DBProvider) SaveFullCommit(fc FullCommit) error {
dbp.logger.Info("DBProvider.SaveFullCommit()...", "fc", fc)
batch := dbp.db.NewBatch()
+ defer batch.Close()
// Save the fc.validators.
// We might be overwriting what we already have, but
diff --git a/state/txindex/kv/kv.go b/state/txindex/kv/kv.go
index 93249b7f..84208b8c 100644
--- a/state/txindex/kv/kv.go
+++ b/state/txindex/kv/kv.go
@@ -78,6 +78,7 @@ func (txi *TxIndex) Get(hash []byte) (*types.TxResult, error) {
// AddBatch indexes a batch of transactions using the given list of tags.
func (txi *TxIndex) AddBatch(b *txindex.Batch) error {
storeBatch := txi.store.NewBatch()
+ defer storeBatch.Close()
for _, result := range b.Ops {
hash := result.Tx.Hash()
@@ -109,6 +110,7 @@ func (txi *TxIndex) AddBatch(b *txindex.Batch) error {
// Index indexes a single transaction using the given list of tags.
func (txi *TxIndex) Index(result *types.TxResult) error {
b := txi.store.NewBatch()
+ defer b.Close()
hash := result.Tx.Hash()