Skip to content

Commit

Permalink
#XD-780 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
penemue committed Jun 25, 2019
1 parent bee9ca3 commit c4ec5bb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ public Log getLog() {

@Override
public void gc() {
gc.wake();
gc.wake(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ internal class BackgroundCleaner(private val gc: GarbageCollector) {

val isCurrentThread: Boolean get() = threadId == Thread.currentThread().id

val isActive: Boolean get() = processor.currentJob == backgroundCleaningJob

fun finish() {
(processor.currentJob as? GcJob)?.cancel()
backgroundCleaningJob.cancel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import jetbrains.exodus.core.dataStructures.hash.IntHashMap
import jetbrains.exodus.core.dataStructures.hash.PackedLongHashSet
import jetbrains.exodus.core.execution.Job
import jetbrains.exodus.core.execution.JobProcessorAdapter
import jetbrains.exodus.core.execution.LatchJob
import jetbrains.exodus.env.*
import jetbrains.exodus.io.Block
import jetbrains.exodus.io.DataReader
Expand Down Expand Up @@ -95,9 +96,14 @@ class GarbageCollector(internal val environment: EnvironmentImpl) {
}, Priority.highest)
}

fun wake() {
fun wake(estimateTotalUtilization: Boolean = false) {
if (ec.isGcEnabled) {
environment.executeTransactionSafeTask { cleaner.queueCleaningJob() }
environment.executeTransactionSafeTask {
if (estimateTotalUtilization) {
utilizationProfile.estimateTotalBytes()
}
cleaner.queueCleaningJob()
}
}
}

Expand Down Expand Up @@ -139,6 +145,17 @@ class GarbageCollector(internal val environment: EnvironmentImpl) {
newFiles = 0
}

/**
* For tests only!!!
*/
fun waitForPendingGC() {
cleaner.getJobProcessor().waitForLatchJob(object : LatchJob() {
override fun execute() {
release()
}
}, 100, Priority.lowest)
}

/**
* For tests only!!!
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package jetbrains.exodus.gc

import jetbrains.exodus.TestFor
import jetbrains.exodus.bindings.IntegerBinding
import jetbrains.exodus.bindings.StringBinding
import jetbrains.exodus.env.*
Expand Down Expand Up @@ -299,6 +300,7 @@ open class GarbageCollectorTest : EnvironmentTestsBase() {
}

@Test
@TestFor(issues = ["XD-780"])
fun `stackoverflow-com-questions-56662998`() {
env.environmentConfig.run {
gcStartIn = 0
Expand All @@ -309,26 +311,23 @@ open class GarbageCollectorTest : EnvironmentTestsBase() {
setLogFileSize(1)
val store = openStoreAutoCommit("store")
env.executeInExclusiveTransaction { txn ->
for (i in 1..1000) {
for (i in 1..500) {
store.putRight(txn, IntegerBinding.intToEntry(i), IntegerBinding.intToEntry(i))
}
}
Assert.assertTrue(env.log.numberOfFiles > 1L)
Assert.assertTrue(env.log.numberOfFiles > 3L)
env.executeInExclusiveTransaction { txn ->
store.openCursor(txn).use { cursor ->
cursor.forEach { deleteCurrent() }
}
}
env.gc()
env.gc.cleaner.getJobProcessor().run {
repeat(4) {
waitForJobs(100)
}
}
Assert.assertEquals(1L, env.log.numberOfFiles)
env.gc.waitForPendingGC()
Assert.assertTrue(env.log.numberOfFiles <= 3L)
}

@Test
@TestFor(issues = ["XD-780"])
fun `stackoverflow-com-questions-56662998+`() {
env.environmentConfig.run {
gcStartIn = 0
Expand All @@ -339,21 +338,17 @@ open class GarbageCollectorTest : EnvironmentTestsBase() {
setLogFileSize(1)
val store = openStoreAutoCommit("store")
env.executeInExclusiveTransaction { txn ->
for (i in 1..1000) {
for (i in 1..500) {
store.putRight(txn, IntegerBinding.intToEntry(i), IntegerBinding.intToEntry(i))
}
}
Assert.assertTrue(env.log.numberOfFiles > 1L)
Assert.assertTrue(env.log.numberOfFiles > 3L)
env.executeInExclusiveTransaction { txn ->
env.truncateStore("store", txn)
}
env.gc()
env.gc.cleaner.getJobProcessor().run {
repeat(4) {
waitForJobs(100)
}
}
Assert.assertEquals(1L, env.log.numberOfFiles)
env.gc.waitForPendingGC()
Assert.assertTrue(env.log.numberOfFiles <= 3L)
}

protected fun openStoreAutoCommit(name: String): StoreImpl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,16 @@ import java.util.concurrent.CyclicBarrier
open class GarbageCollectorTestInMemory : GarbageCollectorTest() {

private val rnd = Random()
private var memory: Memory? = null
private lateinit var memory: Memory

override fun createRW(): Pair<DataReader, DataWriter> {
memory = Memory()
return Pair(MemoryDataReader(memory!!), MemoryDataWriter(memory!!))
return Pair(MemoryDataReader(memory), MemoryDataWriter(memory))
}

override fun deleteRW() {
reader = null
writer = null
memory = null
}

@Test
Expand Down Expand Up @@ -78,7 +77,7 @@ open class GarbageCollectorTestInMemory : GarbageCollectorTest() {
Thread.sleep(0)
}
} catch (t: Throwable) {
memory!!.dump(File(System.getProperty("user.home"), "dump"))
memory.dump(File(System.getProperty("user.home"), "dump"))
logger.error("User code exception: ", t)
Assert.fail()
}
Expand Down Expand Up @@ -129,7 +128,7 @@ open class GarbageCollectorTestInMemory : GarbageCollectorTest() {
Thread.sleep(0)
}
} catch (t: Throwable) {
memory!!.dump(File(System.getProperty("user.home"), "dump"))
memory.dump(File(System.getProperty("user.home"), "dump"))
logger.error("User code exception: ", t)
Assert.fail()
}
Expand Down Expand Up @@ -225,7 +224,7 @@ open class GarbageCollectorTestInMemory : GarbageCollectorTest() {
}
val t = throwable
if (t != null) {
memory!!.dump(File(System.getProperty("user.home"), "dump"))
memory.dump(File(System.getProperty("user.home"), "dump"))
logger.error("User code exception: ", t)
Assert.fail()
}
Expand Down

0 comments on commit c4ec5bb

Please sign in to comment.