Provides common functionality that enhance usability of standard jvm.
Named executors helps to monitor threads state, tasks latency and throughput.
All pools can be dynamically reconfigured
@PropertyId("dao.thread.pool.size")
lateinit var daoPoolSize: DynamicProperty<Integer>
lateinit var profiler: Profiler
...
val executor = NamedExecutors.newDynamicPool(
"dao-pool",
daoPoolSize,
profiler)
Scheduler is based on ScheduledThreadPoolExecutor
but it can change it's rate dynamically.
@PropertyId("work.rate")
lateinit var rate: DynamicProperty<Long>
@PropertyId("pool.size")
lateinit var poolSize: DynamicProperty<Int>
lateinit var profiler: Profiler
...
val scheduler = NamedExecutors.newScheduler(
"regular-work-pool",
poolSize,
profiler)
scheduler.schedule(
Schedule.withRate(rate),
0,//delay
Runnable {
//do work here
})
Common metrics that will work out of the box:
pool.<poolName>.queue
- size of pending tasks submitted to poolpool.<poolName>.activeThreads
- count of currently running threadspool.<poolName>.await
- how many ms spent task in pending state before pool took task for executionpool.<poolName>.run
- how many ms task executedpool.<poolName>.poolSize
- current size of the pool
Special case is Common Fork Join Pool that uses different set of metrics:
lateinit var profiler: Profiler
...
//Enable Common Fork Join Pool profiling
NamedExecutors.profileCommonPool(profiler)
pool.commonPool.poolSize
- current size of the poolpool.commonPool.activeThread
- count of threads in the poolpool.commonPool.runningThread
- count of currently active not blocking threadspool.commonPool.queue
- size of pending tasks submitted to poolpool.commonPool.steal
- count of stolen tasks
CommonThreadPoolGuard, ThreadPoolGuard allows you to watch for queue size of the thread pool. If it outgrows threshold guard will invoke user handler and print stack trace of all threads.
val guard = CommonThreadPoolGuard(
profiler,
checkRate,
threshold) { queueSize, dump ->
log.error("Queue size $queueSize is too big. Current threads state: $dump")
}
Provides RateLimiter and RateLimitedDispatcher.
RateLimiter
interface is defined here as abstraction for RateLimitedDispatcher
.
You can wrap whatever rate limiter implementation you want into it.
Out of the box there is one implementation - ConfigurableRateLimiter
. Under the hood it uses AtomicRateLimiter
of resilience4j-ratelimiter preconfigured it to acquire limits smoothly.
AtomicRateLimiter does not distribute events inside refresh period, it releases all available permits immediately on
interval start if there is a demand for them. To distribute event in our interval of 1 second ConfigurableRateLimiter
divides it into chunks so that 1 chunk of time is limited to 1 permit.The drawback is that actual rate will be lower
than configured - depending on requests' distribution.
Enables async usage of rate limiter. Submitted tasks are executed in the order of submission.
Provides following metrics:
RateLimiterDispatcher.<dispatcherName>.queue_size
– incoming tasks queue sizeRateLimiterDispatcher.<dispatcherName>.queue_wait
– task's wait time in the queue before executionRateLimiterDispatcher.<dispatcherName>.acquire_limit
- time to acquire limitRateLimiterDispatcher.<dispatcherName>.supplied_operation
- supplied task execution duration
Provides SocketChecker.
Allows to check port availability by its port number. Allows to get random free port number in range from 30000 to 60000.
Hot to run jmh tests for id generator:
cd jfix-stdlib-id-generator-jmh
gradle --stop
gradle --no-daemon clean build jmh