Skip to content

Commit

Permalink
Merge pull request #1538 from tronprotocol/fastPop
Browse files Browse the repository at this point in the history
fast pop
  • Loading branch information
nanfengpo authored Sep 17, 2018
2 parents e41acc6 + a6b1b57 commit 0a9c357
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
24 changes: 15 additions & 9 deletions src/main/java/org/tron/core/db/AbstractRevokingStore.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.tron.core.db;

import static org.tron.core.db2.core.SnapshotManager.simpleDecode;

import com.google.common.collect.Maps;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
Expand All @@ -12,8 +15,6 @@
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand All @@ -27,13 +28,9 @@
import org.tron.core.config.args.Args;
import org.tron.core.db2.common.IRevokingDB;
import org.tron.core.db2.core.ISession;
import org.tron.core.db2.core.RevokingDBWithCachingNewValue;
import org.tron.core.db2.core.RevokingDBWithCachingOldValue;
import org.tron.core.db2.core.SnapshotManager;
import org.tron.core.exception.RevokingStoreIllegalStateException;

import static org.tron.core.db2.core.SnapshotManager.simpleDecode;

@Slf4j
@Getter // only for unit test
public abstract class AbstractRevokingStore implements RevokingDatabase {
Expand Down Expand Up @@ -247,6 +244,15 @@ public synchronized void commit() {

@Override
public synchronized void pop() {
prune(writeOptions);
}

@Override
public synchronized void fastPop() {
prune(new WriteOptions());
}

private synchronized void prune(WriteOptions options) {
if (activeDialog != 0) {
throw new RevokingStoreIllegalStateException("activeDialog has to be equal 0");
}
Expand All @@ -259,9 +265,9 @@ public synchronized void pop() {

try {
RevokingState state = stack.peekLast();
state.oldValues.forEach((k, v) -> k.database.putData(k.key, v, writeOptions));
state.newIds.forEach(e -> e.database.deleteData(e.key, writeOptions));
state.removed.forEach((k, v) -> k.database.putData(k.key, v, writeOptions));
state.oldValues.forEach((k, v) -> k.database.putData(k.key, v, options));
state.newIds.forEach(e -> e.database.deleteData(e.key, options));
state.removed.forEach((k, v) -> k.database.putData(k.key, v, options));
stack.pollLast();
} finally {
disabled = false;
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/tron/core/db/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ public synchronized void eraseBlock() {
getDynamicPropertiesStore().getLatestBlockHeaderHash());
logger.info("begin to erase block:" + oldHeadBlock);
khaosDb.pop();
revokingStore.pop();
revokingStore.fastPop();
logger.info("end to erase block:" + oldHeadBlock);
popedTransactions.addAll(oldHeadBlock.getTransactions());

Expand Down Expand Up @@ -1025,13 +1025,17 @@ public synchronized BlockCapsule generateBlock(
UnLinkedBlockException, ValidateScheduleException, AccountResourceInsufficientException {

//check that the first block after the maintenance period has just been processed
if (lastHeadBlockIsMaintenanceBefore != lastHeadBlockIsMaintenance()) {
// if (lastHeadBlockIsMaintenanceBefore != lastHeadBlockIsMaintenance()) {
if (!witnessController.validateWitnessSchedule(witnessCapsule.getAddress(), when)) {
logger.info("It's not my turn, "
+ "and the first block after the maintenance period has just been processed");

logger.info("when:{},lastHeadBlockIsMaintenanceBefore:{},lastHeadBlockIsMaintenanceAfter:{}",
when, lastHeadBlockIsMaintenanceBefore,lastHeadBlockIsMaintenance() );

return null;
}
}
// }

final long timestamp = this.dynamicPropertiesStore.getLatestBlockHeaderTimestamp();
final long number = this.dynamicPropertiesStore.getLatestBlockHeaderNumber();
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/tron/core/db/RevokingDatabase.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.tron.core.db;

import org.tron.core.db.AbstractRevokingStore.Dialog;
import org.tron.core.db.AbstractRevokingStore.RevokingState;
import org.tron.core.db.AbstractRevokingStore.RevokingTuple;
import org.tron.core.db2.common.IRevokingDB;
import org.tron.core.db2.core.ISession;
import org.tron.core.exception.RevokingStoreIllegalStateException;
Expand All @@ -23,6 +20,8 @@ public interface RevokingDatabase {

void pop() throws RevokingStoreIllegalStateException;

void fastPop() throws RevokingStoreIllegalStateException;

void enable();

int size();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/tron/core/db2/core/SnapshotManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ public synchronized void pop() {
}
}

@Override
public void fastPop() throws RevokingStoreIllegalStateException {
pop();
}

public synchronized void enable() {
disabled = false;
}
Expand Down

0 comments on commit 0a9c357

Please sign in to comment.