-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
224 additions
and
4 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...tlin/com/wavesenterprise/we/tx/observer/core/spring/executor/ScheduledPartitionCleaner.kt
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,29 @@ | ||
package com.wavesenterprise.we.tx.observer.core.spring.executor | ||
|
||
import com.wavesenterprise.we.tx.observer.core.spring.properties.PartitionCleanerConfig | ||
import com.wavesenterprise.we.tx.observer.jpa.repository.TxQueuePartitionJpaRepository | ||
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
|
||
open class ScheduledPartitionCleaner( | ||
val txQueuePartitionJpaRepository: TxQueuePartitionJpaRepository, | ||
val partitionCleanerConfig: PartitionCleanerConfig, | ||
) { | ||
val logger: Logger = LoggerFactory.getLogger(ScheduledPartitionCleaner::class.java) | ||
|
||
@SchedulerLock( | ||
name = "cleanEmptyPartitions_task", | ||
) | ||
open fun cleanEmptyPartitions() { | ||
logger.info("Cleaning empty partitions...") | ||
var totalDeletedCount = 0 | ||
do { | ||
val deletedCount = txQueuePartitionJpaRepository.deleteEmptyPartitions( | ||
limit = partitionCleanerConfig.batchSize, | ||
) | ||
totalDeletedCount += deletedCount | ||
} while (deletedCount > 0) | ||
logger.info("Deleted $totalDeletedCount empty partitions") | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...otlin/com/wavesenterprise/we/tx/observer/core/spring/properties/PartitionCleanerConfig.kt
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,9 @@ | ||
package com.wavesenterprise.we.tx.observer.core.spring.properties | ||
|
||
import java.time.Duration | ||
|
||
interface PartitionCleanerConfig { | ||
var enabled: Boolean | ||
var fixedDelay: Duration | ||
var batchSize: Int | ||
} |
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
27 changes: 27 additions & 0 deletions
27
...rter/src/main/kotlin/com/wavesenterprise/we/tx/observer/starter/PartitionCleanerConfig.kt
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,27 @@ | ||
package com.wavesenterprise.we.tx.observer.starter | ||
|
||
import com.wavesenterprise.we.tx.observer.core.spring.executor.ScheduledPartitionCleaner | ||
import com.wavesenterprise.we.tx.observer.core.spring.properties.PartitionCleanerConfig | ||
import com.wavesenterprise.we.tx.observer.jpa.config.TxObserverJpaConfig | ||
import com.wavesenterprise.we.tx.observer.jpa.repository.TxQueuePartitionJpaRepository | ||
import com.wavesenterprise.we.tx.observer.starter.properties.PartitionCleanerProperties | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Import | ||
|
||
@Configuration | ||
@Import(TxObserverJpaConfig::class) | ||
@EnableConfigurationProperties(PartitionCleanerProperties::class) | ||
class PartitionCleanerConfig { | ||
|
||
@Bean | ||
fun scheduledPartitionCleaner( | ||
txQueuePartitionJpaRepository: TxQueuePartitionJpaRepository, | ||
partitionCleanerConfig: PartitionCleanerConfig, | ||
) = | ||
ScheduledPartitionCleaner( | ||
txQueuePartitionJpaRepository = txQueuePartitionJpaRepository, | ||
partitionCleanerConfig = partitionCleanerConfig, | ||
) | ||
} |
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
18 changes: 18 additions & 0 deletions
18
...otlin/com/wavesenterprise/we/tx/observer/starter/properties/PartitionCleanerProperties.kt
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,18 @@ | ||
package com.wavesenterprise.we.tx.observer.starter.properties | ||
|
||
import com.wavesenterprise.we.tx.observer.core.spring.properties.PartitionCleanerConfig | ||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.boot.context.properties.ConstructorBinding | ||
import org.springframework.boot.context.properties.bind.DefaultValue | ||
import java.time.Duration | ||
|
||
@ConfigurationProperties("tx-observer.partition-cleaner") | ||
@ConstructorBinding | ||
data class PartitionCleanerProperties( | ||
@DefaultValue("true") | ||
override var enabled: Boolean, | ||
@DefaultValue("5m") | ||
override var fixedDelay: Duration, | ||
@DefaultValue("100") | ||
override var batchSize: Int, | ||
) : PartitionCleanerConfig |
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
92 changes: 92 additions & 0 deletions
92
...wavesenterprise/we/tx/observer/starter/observer/executor/ScheduledPartitionCleanerTest.kt
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,92 @@ | ||
package com.wavesenterprise.we.tx.observer.starter.observer.executor | ||
|
||
import com.wavesenterprise.sdk.node.client.http.tx.AtomicInnerTxDto.Companion.toDto | ||
import com.wavesenterprise.sdk.node.test.data.TestDataFactory | ||
import com.wavesenterprise.we.flyway.starter.FlywaySchemaConfiguration | ||
import com.wavesenterprise.we.tx.observer.domain.TxQueuePartition | ||
import com.wavesenterprise.we.tx.observer.jpa.TxObserverJpaAutoConfig | ||
import com.wavesenterprise.we.tx.observer.jpa.config.TxObserverJpaConfig | ||
import com.wavesenterprise.we.tx.observer.jpa.repository.EnqueuedTxJpaRepository | ||
import com.wavesenterprise.we.tx.observer.jpa.repository.TxQueuePartitionJpaRepository | ||
import com.wavesenterprise.we.tx.observer.starter.TxObserverStarterConfig | ||
import com.wavesenterprise.we.tx.observer.starter.observer.config.NodeBlockingServiceFactoryMockConfiguration | ||
import com.wavesenterprise.we.tx.observer.starter.observer.config.ObjectMapperConfig | ||
import com.wavesenterprise.we.tx.observer.starter.observer.util.ModelFactory | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertFalse | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest | ||
import org.springframework.test.context.ActiveProfiles | ||
import org.springframework.test.context.ContextConfiguration | ||
import javax.persistence.EntityManager | ||
import javax.persistence.PersistenceContext | ||
|
||
@DataJpaTest | ||
@ActiveProfiles("test") | ||
@ContextConfiguration( | ||
classes = [ | ||
ObjectMapperConfig::class, | ||
DataSourceAutoConfiguration::class, | ||
NodeBlockingServiceFactoryMockConfiguration::class, | ||
TxObserverJpaAutoConfig::class, | ||
TxObserverStarterConfig::class, | ||
FlywaySchemaConfiguration::class, | ||
TxObserverJpaConfig::class, | ||
] | ||
) | ||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) | ||
class ScheduledPartitionCleanerTest { | ||
|
||
@Autowired | ||
lateinit var txQueuePartitionJpaRepository: TxQueuePartitionJpaRepository | ||
|
||
@Autowired | ||
lateinit var enqueuedTxJpaRepository: EnqueuedTxJpaRepository | ||
|
||
@PersistenceContext | ||
lateinit var em: EntityManager | ||
|
||
@Test | ||
fun `should delete empty partitions`() { | ||
val batchSize = 1 | ||
val activePartition = | ||
TxQueuePartition( | ||
id = "activePartitionId", | ||
priority = 0, | ||
) | ||
val inactivePartition = | ||
TxQueuePartition( | ||
id = "inactivePartitionId", | ||
priority = 0, | ||
) | ||
val tx1 = ModelFactory.enqueuedTx( | ||
tx = TestDataFactory.callContractTx().toDto(), | ||
partition = activePartition, | ||
) | ||
val tx2 = ModelFactory.enqueuedTx( | ||
tx = TestDataFactory.callContractTx().toDto(), | ||
partition = activePartition, | ||
) | ||
txQueuePartitionJpaRepository.saveAndFlush(activePartition) | ||
txQueuePartitionJpaRepository.saveAndFlush(inactivePartition) | ||
enqueuedTxJpaRepository.saveAndFlush(tx1) | ||
enqueuedTxJpaRepository.saveAndFlush(tx2) | ||
em.flushAndClear() | ||
|
||
val deletedCount = txQueuePartitionJpaRepository.deleteEmptyPartitions(limit = batchSize) | ||
em.flushAndClear() | ||
|
||
assertEquals(1, deletedCount) | ||
assertTrue(txQueuePartitionJpaRepository.existsById(activePartition.id)) | ||
assertFalse(txQueuePartitionJpaRepository.existsById(inactivePartition.id)) | ||
} | ||
|
||
private fun EntityManager.flushAndClear() { | ||
flush() | ||
clear() | ||
} | ||
} |