Skip to content

Commit 7ebee45

Browse files
committed
remove putflushtoroot
1 parent 85a85cf commit 7ebee45

9 files changed

Lines changed: 142 additions & 131 deletions

File tree

‎src/main/java/org/tron/core/db2/common/DB.java‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,9 @@ public interface DB<K, V> extends Iterable<Map.Entry<K, V>> {
77

88
void put(K k, V v);
99

10-
void putAll(Map<K, V> map);
11-
1210
long size();
1311

1412
boolean isEmpty();
1513

1614
void remove(K k);
17-
18-
void clear();
19-
20-
Map<K, V> asMap();
2115
}

‎src/main/java/org/tron/core/db2/common/HashDB.java‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.tron.core.db2.common;
22

3-
import com.google.common.collect.ImmutableMap;
43
import java.util.HashMap;
54
import java.util.Iterator;
65
import java.util.Map;
@@ -18,11 +17,6 @@ public void put(Key key, Value value) {
1817
db.put(key, value);
1918
}
2019

21-
@Override
22-
public void putAll(Map<Key, Value> map) {
23-
db.putAll(map);
24-
}
25-
2620
@Override
2721
public long size() {
2822
return db.size();
@@ -38,16 +32,6 @@ public void remove(Key key) {
3832
db.remove(key);
3933
}
4034

41-
@Override
42-
public void clear() {
43-
db.clear();
44-
}
45-
46-
@Override
47-
public Map<Key, Value> asMap() {
48-
return ImmutableMap.copyOf(db);
49-
}
50-
5135
@Override
5236
public Iterator<Map.Entry<Key,Value>> iterator() {
5337
return db.entrySet().iterator();

‎src/main/java/org/tron/core/db2/common/LevelDB.java‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ public void put(byte[] key, byte[] value) {
3131
db.putData(key, value);
3232
}
3333

34-
@Override
35-
public void putAll(Map<byte[], byte[]> map) {
36-
db.updateByBatch(map, writeOptions);
37-
}
38-
3934
@Override
4035
public long size() {
4136
return db.getTotal();
@@ -51,16 +46,6 @@ public void remove(byte[] key) {
5146
db.deleteData(key);
5247
}
5348

54-
@Override
55-
public void clear() {
56-
reset();
57-
}
58-
59-
@Override
60-
public Map<byte[], byte[]> asMap() {
61-
throw new UnsupportedOperationException();
62-
}
63-
6449
@Override
6550
public DBIterator iterator() {
6651
return db.iterator();
@@ -71,6 +56,7 @@ public void flush(Map<WrappedByteArray, WrappedByteArray> batch) {
7156
.map(e -> Maps.immutableEntry(e.getKey().getBytes(), e.getValue().getBytes()))
7257
.collect(HashMap::new, (m, k) -> m.put(k.getKey(), k.getValue()), HashMap::putAll);
7358
db.updateByBatch(rows, writeOptions);
59+
// db.reOpen();
7460
}
7561

7662
public void close() {

‎src/main/java/org/tron/core/db2/core/RevokingDBWithCachingNewValue.java‎

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@
33
import com.google.common.collect.Maps;
44
import com.google.common.collect.Streams;
55
import java.util.Collections;
6-
import java.util.Comparator;
76
import java.util.HashMap;
87
import java.util.HashSet;
98
import java.util.Iterator;
10-
import java.util.List;
119
import java.util.Map;
12-
import java.util.Map.Entry;
1310
import java.util.Set;
1411
import java.util.stream.Collectors;
1512
import lombok.Getter;
1613
import org.tron.common.utils.ByteUtil;
1714
import org.tron.core.config.args.Args;
1815
import org.tron.core.db.common.WrappedByteArray;
1916
import org.tron.core.db2.common.IRevokingDB;
20-
import org.tron.core.db2.common.Key;
2117
import org.tron.core.db2.common.LevelDB;
2218
import org.tron.core.db2.common.Value;
2319
import org.tron.core.exception.ItemNotFoundException;
@@ -132,21 +128,6 @@ private synchronized Set<byte[]> getlatestValues(Snapshot head, long limit) {
132128
}
133129
}
134130

135-
if (snapshot.getPrevious() == null && tmp != 0) {
136-
List<Entry<Key, Value>> cache = Streams.stream(((SnapshotRoot) snapshot).getCache())
137-
.sorted(
138-
Comparator.comparing(
139-
(Entry<Key, Value> e) -> e.getKey().getBytes(), ByteUtil::compare)
140-
.reversed())
141-
.collect(Collectors.toList());
142-
for (Entry<Key, Value> e : cache) {
143-
if (tmp > 0) {
144-
result.add(e.getValue().getBytes());
145-
}
146-
--tmp;
147-
}
148-
}
149-
150131
if (snapshot.getPrevious() == null && tmp != 0) {
151132
result.addAll(((LevelDB) ((SnapshotRoot) snapshot).db).getDb().getlatestValues(tmp));
152133
}
@@ -166,10 +147,6 @@ private Set<byte[]> getValuesNext(Snapshot head, byte[] key, long limit) {
166147
}
167148

168149
Map<WrappedByteArray, WrappedByteArray> levelDBMap = new HashMap<>();
169-
((SnapshotRoot) head.getRoot()).getCache().asMap().entrySet().stream()
170-
.map(e -> Maps.immutableEntry(WrappedByteArray.of(e.getKey().getBytes()),
171-
WrappedByteArray.of(e.getValue().getBytes())))
172-
.forEach(e -> levelDBMap.put(e.getKey(), e.getValue()));
173150

174151
((LevelDB) ((SnapshotRoot) head.getRoot()).db).getDb().getNext(key, limit).entrySet().stream()
175152
.map(e -> Maps.immutableEntry(WrappedByteArray.of(e.getKey()), WrappedByteArray.of(e.getValue())))

‎src/main/java/org/tron/core/db2/core/Snapshot.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ static boolean isImpl(Snapshot snapshot) {
3939
void close();
4040

4141
void reset();
42+
43+
void resetSolidity();
44+
45+
void updateSolidity();
4246
}

‎src/main/java/org/tron/core/db2/core/SnapshotImpl.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.tron.core.db2.common.Key;
1111
import org.tron.core.db2.common.Value;
1212

13+
import java.util.Arrays;
1314
import java.util.HashMap;
1415
import java.util.HashSet;
1516
import java.util.Iterator;
@@ -184,4 +185,13 @@ public void reset() {
184185
getRoot().reset();
185186
}
186187

188+
@Override
189+
public void resetSolidity() {
190+
root.resetSolidity();
191+
}
192+
193+
@Override
194+
public void updateSolidity() {
195+
root.updateSolidity();
196+
}
187197
}

‎src/main/java/org/tron/core/db2/core/SnapshotManager.java‎

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
public class SnapshotManager implements RevokingDatabase {
3838

3939
private static final int DEFAULT_STACK_MAX_SIZE = 256;
40-
public static final int DEFAULT_MAX_FLUSH_COUNT = 1000;
40+
public static final int DEFAULT_MAX_FLUSH_COUNT = 500;
4141
public static final int DEFAULT_MIN_FLUSH_COUNT = 1;
4242
@Getter
4343
private List<RevokingDBWithCachingNewValue> dbs = new ArrayList<>();
@@ -80,7 +80,7 @@ public synchronized ISession buildSession(boolean forceEnable) {
8080

8181
if (size > maxSize.get()) {
8282
flushCount = flushCount + (size - maxSize.get());
83-
merge(size - maxSize.get());
83+
updateSolidity(size - maxSize.get());
8484
size = maxSize.get();
8585
flush();
8686
}
@@ -90,11 +90,6 @@ public synchronized ISession buildSession(boolean forceEnable) {
9090
return new Session(this, disableOnExit);
9191
}
9292

93-
@Override
94-
public void setMode(boolean mode) {
95-
dbs.forEach(db -> db.setMode(mode));
96-
}
97-
9893
@Override
9994
public void add(IRevokingDB db) {
10095
RevokingDBWithCachingNewValue revokingDB = (RevokingDBWithCachingNewValue) db;
@@ -222,18 +217,10 @@ public void shutdown() {
222217
System.err.println("******** end to pop revokingDb ********");
223218
}
224219

225-
private void merge(int hops) {
220+
public void updateSolidity(int hops) {
226221
for (int i = 0; i < hops; i++) {
227222
for (RevokingDBWithCachingNewValue db : dbs) {
228-
Snapshot root = db.getHead().getRoot();
229-
Snapshot next = root.getNext();
230-
root.merge(root.getNext());
231-
if (db.getHead() == next) {
232-
db.setHead(root);
233-
} else {
234-
next.getNext().setPrevious(root);
235-
root.setNext(next.getNext());
236-
}
223+
db.getHead().updateSolidity();
237224
}
238225
}
239226
}
@@ -258,7 +245,28 @@ private void refresh() {
258245
}
259246

260247
private void refreshOne(RevokingDBWithCachingNewValue db) {
261-
((SnapshotRoot) db.getHead().getRoot()).flush();
248+
if (Snapshot.isRoot(db.getHead())) {
249+
return;
250+
}
251+
252+
List<Snapshot> snapshots = new ArrayList<>();
253+
254+
SnapshotRoot root = (SnapshotRoot) db.getHead().getRoot();
255+
Snapshot next = root;
256+
for (int i = 0; i < flushCount; ++i) {
257+
next = next.getNext();
258+
snapshots.add(next);
259+
}
260+
261+
root.merge(snapshots);
262+
263+
root.resetSolidity();
264+
if (db.getHead() == next) {
265+
db.setHead(root);
266+
} else {
267+
next.getNext().setPrevious(root);
268+
root.setNext(next.getNext());
269+
}
262270
}
263271

264272
public void flush() {
@@ -285,14 +293,22 @@ private void createCheckPoint() {
285293
Map<WrappedByteArray, WrappedByteArray> batch = new HashMap<>();
286294
for (RevokingDBWithCachingNewValue db : dbs) {
287295
Snapshot head = db.getHead();
296+
if (Snapshot.isRoot(head)) {
297+
return;
298+
}
299+
288300
String dbName = db.getDbName();
289-
SnapshotRoot snapshot = (SnapshotRoot) head.getRoot();
290-
DB<Key, Value> keyValueDB = snapshot.getCache();
291-
for (Map.Entry<Key, Value> e : keyValueDB) {
292-
Key k = e.getKey();
293-
Value v = e.getValue();
294-
batch.put(WrappedByteArray.of(Bytes.concat(simpleEncode(dbName), k.getBytes())),
295-
WrappedByteArray.of(v.encode()));
301+
Snapshot next = head.getRoot();
302+
for (int i = 0; i < flushCount; ++i) {
303+
next = next.getNext();
304+
SnapshotImpl snapshot = (SnapshotImpl) next;
305+
DB<Key, Value> keyValueDB = snapshot.getDb();
306+
for (Map.Entry<Key, Value> e : keyValueDB) {
307+
Key k = e.getKey();
308+
Value v = e.getValue();
309+
batch.put(WrappedByteArray.of(Bytes.concat(simpleEncode(dbName), k.getBytes())),
310+
WrappedByteArray.of(v.encode()));
311+
}
296312
}
297313
}
298314

@@ -346,7 +362,7 @@ public void check() {
346362

347363
}
348364

349-
dbs.forEach(db -> ((SnapshotRoot) db.getHead().getRoot()).merge(db.getHead(), true));
365+
dbs.forEach(db -> db.getHead().getRoot().merge(db.getHead()));
350366
retreat();
351367
}
352368

0 commit comments

Comments
 (0)