-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CORE-18913 extract the group allocator logic in the mediator to its o…
…wn service (#5257) This allows for better unit testing of the allocator logic and cleaner code in the mediator.
- Loading branch information
Showing
13 changed files
with
221 additions
and
40 deletions.
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
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
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
51 changes: 51 additions & 0 deletions
51
libs/messaging/messaging-impl/src/main/kotlin/net/corda/messaging/mediator/GroupAllocator.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,51 @@ | ||
package net.corda.messaging.mediator | ||
|
||
import net.corda.messaging.api.mediator.config.EventMediatorConfig | ||
import net.corda.messaging.api.records.Record | ||
import kotlin.math.ceil | ||
import kotlin.math.min | ||
|
||
/** | ||
* Helper class to use in the mediator to divide polled records into groups for processing. | ||
*/ | ||
class GroupAllocator { | ||
|
||
/** | ||
* Allocate events into groups based on their keys, a configured minimum group size and thread count. | ||
* This allows for more efficient multi-threaded processing. | ||
* The threshold record count to establish a new group is [config.minGroupSize]. | ||
* If the number of groups exceeds the number of threads then the group count is set to the number of [config.threads] | ||
* Records of the same key are always placed into the same group regardless of group size and count. | ||
* @param events Events to allocate to groups | ||
* @param config Mediator config | ||
* @return Records allocated to groups. | ||
*/ | ||
fun <K : Any, S : Any, E : Any> allocateGroups( | ||
events: List<Record<K, E>>, | ||
config: EventMediatorConfig<K, S, E> | ||
): List<Map<K, List<Record<K, E>>>> { | ||
val groups = setUpGroups(config, events) | ||
val buckets = events | ||
.groupBy { it.key }.toList() | ||
.sortedByDescending { it.second.size } | ||
|
||
buckets.forEach { (key, records) -> | ||
val leastFilledGroup = groups.minByOrNull { it.values.flatten().size } | ||
leastFilledGroup?.put(key, records) | ||
} | ||
|
||
return groups.filter { it.values.isNotEmpty() } | ||
} | ||
|
||
private fun <E : Any, S: Any, K : Any> setUpGroups( | ||
config: EventMediatorConfig<K, S, E>, | ||
events: List<Record<K, E>> | ||
): MutableList<MutableMap<K, List<Record<K, E>>>> { | ||
val numGroups = min( | ||
ceil(events.size.toDouble() / config.minGroupSize).toInt(), | ||
config.threads | ||
) | ||
|
||
return MutableList(numGroups) { mutableMapOf() } | ||
} | ||
} |
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
Oops, something went wrong.