From 0e42a6a5658280ea72a3858d10dd147b66fa6b48 Mon Sep 17 00:00:00 2001 From: Xin Li <59580070+xin-hedera@users.noreply.github.com> Date: Tue, 20 Aug 2024 13:55:08 -0500 Subject: [PATCH] Use owner in backfill ethereum transaction hash migration (#9080) - Use owner mirror_node in the migration Signed-off-by: Xin Li --- ...kfillEthereumTransactionHashMigration.java | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/hedera-mirror-importer/src/main/java/com/hedera/mirror/importer/migration/BackfillEthereumTransactionHashMigration.java b/hedera-mirror-importer/src/main/java/com/hedera/mirror/importer/migration/BackfillEthereumTransactionHashMigration.java index 25dc0c443a2..3a85af61acd 100644 --- a/hedera-mirror-importer/src/main/java/com/hedera/mirror/importer/migration/BackfillEthereumTransactionHashMigration.java +++ b/hedera-mirror-importer/src/main/java/com/hedera/mirror/importer/migration/BackfillEthereumTransactionHashMigration.java @@ -22,12 +22,13 @@ import com.hedera.mirror.common.domain.entity.EntityId; import com.hedera.mirror.common.domain.transaction.TransactionHash; import com.hedera.mirror.importer.ImporterProperties; +import com.hedera.mirror.importer.config.Owner; import com.hedera.mirror.importer.parser.record.entity.EntityProperties; import com.hedera.mirror.importer.parser.record.ethereum.EthereumTransactionParser; -import com.hedera.mirror.importer.repository.TransactionHashRepository; import jakarta.inject.Named; import java.io.IOException; import java.util.List; +import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; import lombok.Data; import org.apache.commons.lang3.ArrayUtils; @@ -37,11 +38,18 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter; import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.support.TransactionOperations; +import org.springframework.transaction.support.TransactionTemplate; @Named public class BackfillEthereumTransactionHashMigration extends RepeatableMigration { + private static final String INSERT_TRANSACTION_HASH_SQL = + """ + insert into transaction_hash (consensus_timestamp, distribution_id, hash, payer_account_id) + values (?, ?, ?, ?) + """; private static final ParameterizedPreparedStatementSetter PSS = (ps, transaction) -> { ps.setBytes(1, transaction.getHash()); ps.setLong(2, transaction.getConsensusTimestamp()); @@ -84,23 +92,17 @@ insert into contract_transaction_hash (consensus_timestamp, entity_id, hash, pay private final EntityProperties entityProperties; private final EthereumTransactionParser ethereumTransactionParser; private final JdbcTemplate jdbcTemplate; - private final TransactionHashRepository transactionHashRepository; - private final TransactionOperations transactionOperations; @Lazy public BackfillEthereumTransactionHashMigration( EntityProperties entityProperties, EthereumTransactionParser ethereumTransactionParser, ImporterProperties importerProperties, - JdbcTemplate jdbcTemplate, - TransactionHashRepository transactionHashRepository, - TransactionOperations transactionOperations) { + @Owner JdbcTemplate jdbcTemplate) { super(importerProperties.getMigration()); this.entityProperties = entityProperties; this.ethereumTransactionParser = ethereumTransactionParser; this.jdbcTemplate = jdbcTemplate; - this.transactionHashRepository = transactionHashRepository; - this.transactionOperations = transactionOperations; } @Override @@ -114,7 +116,7 @@ protected void doMigrate() throws IOException { var patched = new AtomicInteger(); var stopwatch = Stopwatch.createStarted(); - transactionOperations.executeWithoutResult(s -> { + getTransactionOperations().executeWithoutResult(s -> { long consensusTimestamp = -1; for (; ; ) { var transactions = jdbcTemplate.query(SELECT_ETHEREUM_TRANSACTION_SQL, ROW_MAPPER, consensusTimestamp); @@ -173,10 +175,21 @@ private void backfillTables(List patchedTransactio .payerAccountId(t.getPayerAccountId()) .build()) .toList(); - transactionHashRepository.saveAll(transactionHashes); + jdbcTemplate.batchUpdate( + INSERT_TRANSACTION_HASH_SQL, transactionHashes, transactionHashes.size(), (ps, transactionHash) -> { + ps.setLong(1, transactionHash.getConsensusTimestamp()); + ps.setShort(2, transactionHash.getDistributionId()); + ps.setBytes(3, transactionHash.getHash()); + ps.setLong(4, transactionHash.getPayerAccountId()); + }); } } + private TransactionOperations getTransactionOperations() { + var transactionManager = new DataSourceTransactionManager(Objects.requireNonNull(jdbcTemplate.getDataSource())); + return new TransactionTemplate(transactionManager); + } + @Data private static class MigrationEthereumTransaction { private byte[] callData;