-
Notifications
You must be signed in to change notification settings - Fork 0
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
batcher for performance improvements #12
Open
Topru333
wants to merge
10
commits into
dev
Choose a base branch
from
batcher
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3052f77
batcher for performance improvements
Topru333 f90a58d
raw message batcher into resource
Topru333 8a91849
LOG4J version update
Topru333 4bf914e
LOG4J version update
Topru333 10efdda
fixed error with published message
Topru333 f6d0a0b
fixed kotlin compile error, updated dependency
Topru333 a98f14e
temporary changes due log4j issues
Topru333 0fb53a7
common and bom version downgrade
Topru333 e45dbc9
updated back to last common
Topru333 e7807f5
updated common-utils lib to master version 0.0.1
Topru333 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
kotlin.code.style=official | ||
kotlin_version=1.4.10 | ||
release_version=0.3.1 | ||
kotlin_version=1.6.21 | ||
release_version=0.3.2 | ||
description='Websocket Client' | ||
vcs_url=https://github.com/th2-net/th2-conn-ws-client |
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -19,6 +19,7 @@ | |||||||
package com.exactpro.th2.ws.client | ||||||||
|
||||||||
import com.exactpro.th2.common.event.Event | ||||||||
import com.exactpro.th2.common.event.EventUtils | ||||||||
import com.exactpro.th2.common.grpc.ConnectionID | ||||||||
import com.exactpro.th2.common.grpc.Direction | ||||||||
import com.exactpro.th2.common.grpc.EventBatch | ||||||||
|
@@ -29,6 +30,10 @@ import com.exactpro.th2.common.schema.message.MessageListener | |||||||
import com.exactpro.th2.common.schema.message.MessageRouter | ||||||||
import com.exactpro.th2.common.schema.message.QueueAttribute | ||||||||
import com.exactpro.th2.common.schema.message.storeEvent | ||||||||
import com.exactpro.th2.common.utils.event.EventBatcher | ||||||||
import com.exactpro.th2.common.utils.message.RAW_DIRECTION_SELECTOR | ||||||||
import com.exactpro.th2.common.utils.message.RawMessageBatcher | ||||||||
import com.exactpro.th2.common.utils.message.direction | ||||||||
import com.exactpro.th2.ws.client.Settings.FrameType.TEXT | ||||||||
import com.exactpro.th2.ws.client.api.IClient | ||||||||
import com.exactpro.th2.ws.client.api.IHandler | ||||||||
|
@@ -37,16 +42,18 @@ import com.exactpro.th2.ws.client.api.IHandlerSettingsTypeProvider | |||||||
import com.exactpro.th2.ws.client.api.impl.DefaultHandler | ||||||||
import com.exactpro.th2.ws.client.api.impl.DefaultHandlerSettingsTypeProvider | ||||||||
import com.exactpro.th2.ws.client.api.impl.WebSocketClient | ||||||||
import com.exactpro.th2.ws.client.util.toBatch | ||||||||
import com.exactpro.th2.ws.client.util.toPrettyString | ||||||||
import com.exactpro.th2.ws.client.util.toRawMessage | ||||||||
import com.fasterxml.jackson.databind.json.JsonMapper | ||||||||
import com.fasterxml.jackson.databind.module.SimpleModule | ||||||||
import com.fasterxml.jackson.module.kotlin.KotlinModule | ||||||||
import mu.KotlinLogging | ||||||||
import org.apache.commons.lang3.exception.ExceptionUtils | ||||||||
import java.net.URI | ||||||||
import java.time.Instant | ||||||||
import java.util.ServiceLoader | ||||||||
import java.util.concurrent.ConcurrentLinkedDeque | ||||||||
import java.util.concurrent.Executors | ||||||||
import java.util.concurrent.TimeUnit.SECONDS | ||||||||
import java.util.concurrent.atomic.AtomicLong | ||||||||
import java.util.concurrent.locks.ReentrantLock | ||||||||
|
@@ -121,16 +128,36 @@ fun run( | |||||||
val incomingSequence = createSequence() | ||||||||
val outgoingSequence = createSequence() | ||||||||
|
||||||||
//TODO: add batching (by size or time) | ||||||||
val scheduledExecutorService = Executors.newScheduledThreadPool(1).also { | ||||||||
registerResource("Batcher scheduled executor", it::shutdownNow) | ||||||||
} | ||||||||
|
||||||||
val batcher = RawMessageBatcher(settings.maxBatchSize, settings.maxFlushTime, RAW_DIRECTION_SELECTOR, scheduledExecutorService, { throwable: Throwable -> | ||||||||
LOGGER.error(throwable) { "Can't send message group batch due inner error" } | ||||||||
}) { | ||||||||
when (it.groupsList.first().direction) { | ||||||||
Direction.FIRST -> messageRouter.send(it, QueueAttribute.FIRST.value) | ||||||||
Direction.SECOND -> messageRouter.send(it, QueueAttribute.SECOND.value) | ||||||||
else -> error("Unrecognized direction") | ||||||||
} | ||||||||
}.also { | ||||||||
registerResource("Raw message batcher", it::close) | ||||||||
} | ||||||||
|
||||||||
val onMessage = { message: ByteArray, _: Boolean, direction: Direction -> | ||||||||
val sequence = if (direction == Direction.FIRST) incomingSequence else outgoingSequence | ||||||||
val attribute = if (direction == Direction.FIRST) QueueAttribute.FIRST else QueueAttribute.SECOND | ||||||||
messageRouter.send(message.toBatch(connectionId, direction, sequence()), attribute.toString()) | ||||||||
batcher.onMessage(message.toRawMessage( | ||||||||
connectionId, | ||||||||
direction, | ||||||||
(if (direction == Direction.FIRST) incomingSequence else outgoingSequence)() | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
For readability |
||||||||
)) | ||||||||
} | ||||||||
|
||||||||
val eventBatcher = EventBatcher(settings.maxBatchSize, settings.maxFlushTime, scheduledExecutorService, eventRouter::send).also { | ||||||||
registerResource("Event batcher", it::close) | ||||||||
} | ||||||||
|
||||||||
val onEvent = { cause: Throwable?, message: () -> String -> | ||||||||
val type = if (cause != null) "Error" else "Info" | ||||||||
eventRouter.storeEvent(rootEventId, message(), type, cause) | ||||||||
eventBatcher.storeEvent(message(), cause, rootEventId) | ||||||||
} | ||||||||
|
||||||||
val client = WebSocketClient( | ||||||||
|
@@ -190,7 +217,9 @@ data class Settings( | |||||||
val handlerSettings: IHandlerSettings? = null, | ||||||||
val grpcStartControl: Boolean = false, | ||||||||
val autoStart: Boolean = true, | ||||||||
val autoStopAfter: Int = 0 | ||||||||
val autoStopAfter: Int = 0, | ||||||||
val maxBatchSize: Int = 1000, | ||||||||
val maxFlushTime: Long = 1000 | ||||||||
) { | ||||||||
enum class FrameType { | ||||||||
TEXT { | ||||||||
|
@@ -218,3 +247,22 @@ private inline fun <reified T> load(defaultImpl: Class<out T>): T { | |||||||
private fun createSequence(): () -> Long = Instant.now().run { | ||||||||
AtomicLong(epochSecond * SECONDS.toNanos(1) + nano) | ||||||||
}::incrementAndGet | ||||||||
|
||||||||
fun EventBatcher.storeEvent(name: String, cause: Throwable?, parentEventId: String) { | ||||||||
val event = createEvent(name, cause) | ||||||||
onEvent(event.toProtoEvent(parentEventId)) | ||||||||
} | ||||||||
|
||||||||
fun createEvent( | ||||||||
name: String, | ||||||||
cause: Throwable? = null | ||||||||
): Event = Event.start().apply { | ||||||||
endTimestamp() | ||||||||
name(name) | ||||||||
type(if (cause != null) "Error" else "Info") | ||||||||
status(if (cause != null) Event.Status.FAILED else Event.Status.PASSED) | ||||||||
|
||||||||
generateSequence(cause, Throwable::cause).forEach { error -> | ||||||||
bodyData(EventUtils.createMessageBean(ExceptionUtils.getMessage(error))) | ||||||||
} | ||||||||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bump minor version instead since there was an extension of functionality