Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use single threaded coroutine context for database transactions #282

Merged
merged 3 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.github.quiltservertools.ledger.network.packet.handshake.HandshakeS2CP
import com.github.quiltservertools.ledger.network.packet.response.ResponseS2CPacket
import com.github.quiltservertools.ledger.registry.ActionRegistry
import com.uchuhimo.konf.Config
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.TimeoutCancellationException
Expand Down Expand Up @@ -61,7 +62,7 @@ object Ledger : DedicatedServerModInitializer, CoroutineScope {
@JvmField // Required for mixin access
val previewCache = ConcurrentHashMap<UUID, Preview>()

override val coroutineContext: CoroutineContext = Dispatchers.IO
override val coroutineContext: CoroutineContext = Dispatchers.Default + CoroutineName("Ledger")

override fun onInitializeServer() {
val version = FabricLoader.getInstance().getModContainer(MOD_ID).get().metadata.version
Expand Down Expand Up @@ -116,7 +117,7 @@ object Ledger : DedicatedServerModInitializer, CoroutineScope {
runBlocking {
try {
withTimeout(config[DatabaseSpec.queueTimeoutMin].minutes) {
Ledger.launch(Dispatchers.Default) {
Ledger.launch {
while (ActionQueueService.size > 0) {
logInfo(
"Database is still busy. If you exit now data WILL be lost. " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.github.quiltservertools.ledger.utility.LiteralNode
import com.github.quiltservertools.ledger.utility.MessageUtils
import com.github.quiltservertools.ledger.utility.TextColorPallet
import com.mojang.brigadier.arguments.IntegerArgumentType
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.minecraft.server.command.CommandManager.argument
import net.minecraft.server.command.CommandManager.literal
Expand All @@ -28,7 +27,7 @@ object PageCommand : BuildableCommand {

val params = Ledger.searchCache[source.name]
if (params != null) {
Ledger.launch(Dispatchers.IO) {
Ledger.launch {
MessageUtils.warnBusy(source)
val results = DatabaseManager.searchActions(params, page)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.github.quiltservertools.ledger.database.DatabaseManager
import com.github.quiltservertools.ledger.utility.Context
import com.github.quiltservertools.ledger.utility.LiteralNode
import com.github.quiltservertools.ledger.utility.MessageUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.lucko.fabric.api.permissions.v0.Permissions
import net.minecraft.server.command.CommandManager
Expand Down Expand Up @@ -55,7 +54,7 @@ object PreviewCommand : BuildableCommand {
val source = context.source
val player = source.playerOrThrow
params.ensureSpecific()
Ledger.launch(Dispatchers.IO) {
Ledger.launch {
MessageUtils.warnBusy(source)
val actions = DatabaseManager.previewActions(params, type)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.github.quiltservertools.ledger.utility.MessageUtils
import com.github.quiltservertools.ledger.utility.TextColorPallet
import com.github.quiltservertools.ledger.utility.launchMain
import com.github.quiltservertools.ledger.utility.literal
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.lucko.fabric.api.permissions.v0.Permissions
import net.minecraft.server.command.CommandManager
Expand All @@ -32,7 +31,7 @@ object RestoreCommand : BuildableCommand {
fun restore(context: Context, params: ActionSearchParams): Int {
val source = context.source
params.ensureSpecific()
Ledger.launch(Dispatchers.IO) {
Ledger.launch {
MessageUtils.warnBusy(source)
val actions = DatabaseManager.selectRestore(params)

Expand Down Expand Up @@ -61,7 +60,7 @@ object RestoreCommand : BuildableCommand {
actionIds.add(action.id)
}
}
Ledger.launch(Dispatchers.IO) {
Ledger.launch {
DatabaseManager.restoreActions(actionIds)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.github.quiltservertools.ledger.utility.MessageUtils
import com.github.quiltservertools.ledger.utility.TextColorPallet
import com.github.quiltservertools.ledger.utility.launchMain
import com.github.quiltservertools.ledger.utility.literal
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.lucko.fabric.api.permissions.v0.Permissions
import net.minecraft.server.command.CommandManager
Expand All @@ -32,7 +31,7 @@ object RollbackCommand : BuildableCommand {
fun rollback(context: Context, params: ActionSearchParams): Int {
val source = context.source
params.ensureSpecific()
Ledger.launch(Dispatchers.IO) {
Ledger.launch {
MessageUtils.warnBusy(source)
val actions = DatabaseManager.selectRollback(params)

Expand Down Expand Up @@ -61,7 +60,7 @@ object RollbackCommand : BuildableCommand {
actionIds.add(action.id)
}
}
Ledger.launch(Dispatchers.IO) {
Ledger.launch {
DatabaseManager.rollbackActions(actionIds)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object ActionQueueService {
if (queue.size < Ledger.config[DatabaseSpec.batchSize]) {
delay(Ledger.config[DatabaseSpec.batchDelay].ticks)
}
drainBatch()
if (queue.isNotEmpty()) drainBatch()
prepareNextBatch()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import com.github.quiltservertools.ledger.utility.Negatable
import com.github.quiltservertools.ledger.utility.PlayerResult
import com.google.common.cache.Cache
import com.mojang.authlib.GameProfile
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.newSingleThreadContext
import net.minecraft.util.Identifier
import net.minecraft.util.math.BlockPos
import org.jetbrains.exposed.dao.Entity
Expand Down Expand Up @@ -75,10 +80,16 @@ object DatabaseManager {
get() = database.dialect.name

private val cache = DatabaseCacheService
private var databaseContext = Dispatchers.IO + CoroutineName("Ledger Database")

@OptIn(ExperimentalCoroutinesApi::class, DelicateCoroutinesApi::class)
fun setup(dataSource: DataSource?) {
val source = dataSource ?: getDefaultDatasource()
database = Database.connect(source)
if (dataSource == null) {
database = Database.connect(getDefaultDatasource())
databaseContext = newSingleThreadContext("Ledger Database")
} else {
database = Database.connect(dataSource)
}
}

private fun getDefaultDatasource(): DataSource {
Expand Down Expand Up @@ -402,7 +413,7 @@ object DatabaseManager {
delay(timeMillis = 1000)
}

return newSuspendedTransaction(db = database) {
return newSuspendedTransaction(context = databaseContext, db = database) {
repetitionAttempts = MAX_QUERY_RETRIES
minRepetitionDelay = MIN_RETRY_DELAY
maxRepetitionDelay = MAX_RETRY_DELAY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.github.quiltservertools.ledger.network.packet.response.ResponseConten
import com.github.quiltservertools.ledger.network.packet.response.ResponseS2CPacket
import com.github.quiltservertools.ledger.utility.MessageUtils
import com.github.quiltservertools.ledger.utility.launchMain
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.lucko.fabric.api.permissions.v0.Permissions
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking
Expand Down Expand Up @@ -53,7 +52,7 @@ data class SearchC2SPacket(val restore: Boolean, val args: String) : CustomPaylo
sender
)

Ledger.launch(Dispatchers.IO) {
Ledger.launch {
MessageUtils.warnBusy(source)
if (payload.restore) {
val actions = DatabaseManager.selectRestore(params)
Expand All @@ -66,7 +65,7 @@ data class SearchC2SPacket(val restore: Boolean, val args: String) : CustomPaylo
actionIds.add(action.id)
}
}
Ledger.launch(Dispatchers.IO) {
Ledger.launch {
DatabaseManager.restoreActions(actionIds)
}

Expand All @@ -89,7 +88,7 @@ data class SearchC2SPacket(val restore: Boolean, val args: String) : CustomPaylo
actionIds.add(action.id)
}
}
Ledger.launch(Dispatchers.IO) {
Ledger.launch {
DatabaseManager.rollbackActions(actionIds)
}
ResponseS2CPacket.sendResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.github.quiltservertools.ledger.Ledger
import com.github.quiltservertools.ledger.actionutils.ActionSearchParams
import com.github.quiltservertools.ledger.actionutils.SearchResults
import com.github.quiltservertools.ledger.database.DatabaseManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.minecraft.block.BedBlock
import net.minecraft.block.BlockState
Expand Down Expand Up @@ -56,7 +55,7 @@ fun PlayerEntity.inspectOff(): Int {
fun ServerCommandSource.inspectBlock(pos: BlockPos) {
val source = this

Ledger.launch(Dispatchers.IO) {
Ledger.launch {
var area = BlockBox(pos)

val state = source.world.getBlockState(pos)
Expand Down