-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #225 add transaction pruning service
- Loading branch information
Sotatek-HuyLe3a
committed
Apr 23, 2024
1 parent
6471f0c
commit 3a9e6ee
Showing
9 changed files
with
158 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...src/main/java/com/bloxbean/cardano/yaci/store/transaction/TransactionStoreProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.bloxbean.cardano.yaci.store.transaction; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public class TransactionStoreProperties { | ||
|
||
@Builder.Default | ||
private boolean pruningEnabled = false; | ||
|
||
@Builder.Default | ||
private int pruningInterval = 1440; //In minutes | ||
|
||
@Builder.Default | ||
private int pruningSafeSlot = 43200; // 2160 blocks | ||
} |
84 changes: 84 additions & 0 deletions
84
...java/com/bloxbean/cardano/yaci/store/transaction/scheduler/TransactionPruningService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.bloxbean.cardano.yaci.store.transaction.scheduler; | ||
|
||
|
||
import com.bloxbean.cardano.yaci.store.common.service.CursorService; | ||
import com.bloxbean.cardano.yaci.store.events.EpochChangeEvent; | ||
import com.bloxbean.cardano.yaci.store.transaction.TransactionStoreProperties; | ||
import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionStorage; | ||
import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionWitnessStorage; | ||
import jakarta.annotation.PostConstruct; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
@Component | ||
@ConditionalOnProperty( | ||
value = "store.transaction.pruning-enabled", | ||
havingValue = "true" | ||
) | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class TransactionPruningService { | ||
private final TransactionStorage transactionStorage; | ||
private final TransactionWitnessStorage transactionWitnessStorage; | ||
private final CursorService cursorService; | ||
private final TransactionStoreProperties transactionStoreProperties; | ||
|
||
private final AtomicBoolean isPruning = new AtomicBoolean(false); | ||
|
||
@PostConstruct | ||
public void init() { | ||
log.info("<< Transaction Pruning Service Enabled >>"); | ||
} | ||
|
||
@Scheduled(fixedRateString = "${store.transaction.pruning-interval:1440}", timeUnit = TimeUnit.MINUTES) | ||
public void handleTransactionPruning() { | ||
if (isPruning.get()) { | ||
log.info("Transaction pruning is already in progress. Skipping this run !!!"); | ||
return; | ||
} | ||
|
||
Thread.startVirtualThread(this::deleteOldTransactions); | ||
} | ||
|
||
@EventListener | ||
@Transactional | ||
public void handleEpochChangeEvent(EpochChangeEvent epochChangeEvent) { | ||
if (isPruning.get()) { | ||
log.info("Transaction pruning is already in progress. Skipping this run !!!"); | ||
return; | ||
} | ||
|
||
Thread.startVirtualThread(this::deleteOldTransactions); | ||
} | ||
|
||
private void deleteOldTransactions() { | ||
isPruning.set(true); | ||
try { | ||
cursorService.getCursor().ifPresent(cursor -> { | ||
log.info("Current cursor: {}", cursor.getBlock()); | ||
|
||
var slot = cursor.getSlot() - transactionStoreProperties.getPruningSafeSlot(); | ||
if (slot > 0) { | ||
long t1 = System.currentTimeMillis(); | ||
var deleteTxCount = | ||
transactionStorage.deleteBySlotLessThan(slot); | ||
var deleteTxWitnessCount = transactionWitnessStorage.deleteBySlotLessThan(slot); | ||
// skip pruning invalid_transaction and withdrawn because these 2 tables do not have too much data | ||
long t2 = System.currentTimeMillis(); | ||
log.info("Deleted {} transactions and {} transaction witnesses before slot {}, Time taken: {} ms", | ||
deleteTxCount, deleteTxWitnessCount, slot, (t2 - t1)); | ||
} | ||
}); | ||
} finally { | ||
isPruning.set(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters