Skip to content

Commit ee5d2b2

Browse files
authored
vertx-web-kotlin-dsljson updates (#9192)
* minor performance improvements and warning suppression * vertx-web-kotlin-dsljson updates * formatting * update postgres socket connection config
1 parent 8d801c8 commit ee5d2b2

File tree

13 files changed

+45
-43
lines changed

13 files changed

+45
-43
lines changed

frameworks/Kotlin/vertx-web-kotlin-dsljson/configuration/scripts/server.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
#!/bin/bash
22

3-
NUM_PROCESSORS=$((`grep --count ^processor /proc/cpuinfo`))
4-
53
JVM_OPTS="-server \
64
-Xms2G \
75
-Xmx2G \
6+
-XX:+AlwaysPreTouch \
87
-XX:+UseParallelGC \
8+
-XX:+PreserveFramePointer \
9+
-XX:+EnableDynamicAgentLoading \
910
-XX:InitialCodeCacheSize=512m \
1011
-XX:ReservedCodeCacheSize=512m \
1112
-XX:MaxInlineLevel=20 \
12-
-XX:+AlwaysPreTouch \
1313
-XX:+UseNUMA \
14+
-Djava.lang.Integer.IntegerCache.high=10000 \
1415
-Dvertx.disableMetrics=true \
1516
-Dvertx.disableH2c=true \
1617
-Dvertx.disableWebsockets=true \
@@ -19,15 +20,14 @@ JVM_OPTS="-server \
1920
-Dvertx.disableContextTimings=true \
2021
-Dvertx.disableTCCL=true \
2122
-Dvertx.disableHttpHeadersValidation=true \
23+
-Dvertx.eventLoopPoolSize=$((`grep --count ^processor /proc/cpuinfo`)) \
2224
-Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector \
2325
-Dio.netty.buffer.checkBounds=false \
2426
-Dio.netty.buffer.checkAccessible=false \
2527
-Dtfb.hasDB=false"
2628

2729
JAR_PATH="./build/libs/vertx-web-kotlin-dsljson-benchmark-1.0.0-SNAPSHOT-fat.jar"
2830

29-
VERTX_ARGS="-instances 1"
30-
3131
cleanup() {
3232
echo "Caught SIGINT signal. Stopping the Java program..."
3333
if [ ! -z "$JAVA_PID" ]; then
@@ -39,7 +39,7 @@ cleanup() {
3939

4040
trap cleanup SIGINT
4141

42-
java $JVM_OPTS -jar $JAR_PATH $VERTX_ARGS &
42+
java $JVM_OPTS -jar $JAR_PATH &
4343
JAVA_PID=$!
4444

4545
echo "Server PID: $JAVA_PID"

frameworks/Kotlin/vertx-web-kotlin-dsljson/src/main/kotlin/com/example/starter/App.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.example.starter
33
import com.example.starter.utils.PeriodicDateResolver
44
import com.example.starter.utils.block
55
import io.vertx.core.Vertx
6-
import io.vertx.core.impl.cpu.CpuCoreSensor
76
import io.vertx.kotlin.core.deploymentOptionsOf
87
import io.vertx.kotlin.core.vertxOptionsOf
98
import kotlin.time.Duration.Companion.seconds
@@ -14,11 +13,12 @@ object App : Logging {
1413

1514
@JvmStatic
1615
fun main(args: Array<out String?>?) {
17-
val numCores = CpuCoreSensor.availableProcessors()
16+
val eventLoopPoolSize = System.getProperty("vertx.eventLoopPoolSize")?.toInt()
17+
?: Runtime.getRuntime().availableProcessors()
1818

1919
val vertx = Vertx.vertx(
2020
vertxOptionsOf(
21-
eventLoopPoolSize = numCores,
21+
eventLoopPoolSize = eventLoopPoolSize,
2222
preferNativeTransport = true,
2323
)
2424
)
@@ -41,7 +41,7 @@ object App : Logging {
4141
vertx.deployVerticle(
4242
{ if (hasDb) PostgresVerticle() else BasicVerticle() },
4343
deploymentOptionsOf(
44-
instances = numCores,
44+
instances = eventLoopPoolSize,
4545
)
4646
)
4747

frameworks/Kotlin/vertx-web-kotlin-dsljson/src/main/kotlin/com/example/starter/PostgresVerticle.kt

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import com.example.starter.db.WorldRepository
55
import com.example.starter.handlers.FortuneHandler
66
import com.example.starter.handlers.WorldHandler
77
import com.example.starter.io.JsonResource
8-
import com.example.starter.utils.array
98
import com.example.starter.utils.isConnectionReset
109
import io.vertx.core.AbstractVerticle
11-
import io.vertx.core.Future
1210
import io.vertx.core.Promise
1311
import io.vertx.core.http.HttpServerOptions
1412
import io.vertx.ext.web.Router
@@ -18,15 +16,10 @@ import org.apache.logging.log4j.kotlin.Logging
1816

1917
class PostgresVerticle : AbstractVerticle() {
2018
override fun start(startPromise: Promise<Void>) {
21-
Future.all(
22-
PgConnection.connect(vertx, PG_CONNECT_OPTIONS),
23-
PgConnection.connect(vertx, PG_CONNECT_OPTIONS),
24-
)
25-
.onSuccess { cf ->
26-
val pool = cf.array<PgConnection>()
27-
28-
val fortuneHandler = FortuneHandler(FortuneRepository(pool))
29-
val worldHandler = WorldHandler(WorldRepository(pool))
19+
PgConnection.connect(vertx, PG_CONNECT_OPTIONS)
20+
.onSuccess { conn ->
21+
val fortuneHandler = FortuneHandler(FortuneRepository(conn))
22+
val worldHandler = WorldHandler(WorldRepository(conn))
3023

3124
val router = Router.router(vertx)
3225

frameworks/Kotlin/vertx-web-kotlin-dsljson/src/main/kotlin/com/example/starter/db/AbstractRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package com.example.starter.db
33
import io.vertx.pgclient.PgConnection
44

55
abstract class AbstractRepository<T>(
6-
protected val pool: Array<PgConnection>
6+
protected val conn: PgConnection
77
)

frameworks/Kotlin/vertx-web-kotlin-dsljson/src/main/kotlin/com/example/starter/db/FortuneRepository.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import io.vertx.core.Future
77
import io.vertx.pgclient.PgConnection
88
import io.vertx.sqlclient.Row
99

10-
class FortuneRepository(pool: Array<PgConnection>) : AbstractRepository<Fortune>(pool) {
11-
private val selectFortuneQuery = this.pool[0].preparedQuery(SELECT_FORTUNE_SQL)
10+
class FortuneRepository(conn: PgConnection) : AbstractRepository<Fortune>(conn) {
11+
private val selectFortuneQuery = this.conn.preparedQuery(SELECT_FORTUNE_SQL)
1212

1313
fun selectFortunes(): Future<Array<Fortune>> = selectFortuneQuery
1414
.execute()

frameworks/Kotlin/vertx-web-kotlin-dsljson/src/main/kotlin/com/example/starter/db/WorldRepository.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import io.vertx.sqlclient.impl.SqlClientInternal
1313
import java.util.concurrent.ThreadLocalRandom
1414
import java.util.concurrent.atomic.AtomicInteger
1515

16-
@Suppress("NOTHING_TO_INLINE")
17-
class WorldRepository(pool: Array<PgConnection>) : AbstractRepository<World>(pool) {
18-
private val selectWorldQuery = this.pool[0].preparedQuery(SELECT_WORLD_SQL)
19-
private val updateWorldQueries = generateQueries(this.pool[1])
16+
@Suppress("NOTHING_TO_INLINE", "UNCHECKED_CAST")
17+
class WorldRepository(conn: PgConnection) : AbstractRepository<World>(conn) {
18+
private val selectWorldQuery = this.conn.preparedQuery(SELECT_WORLD_SQL)
19+
private val updateWorldQueries = generateQueries(this.conn)
2020

2121
fun selectRandomWorld(): Future<World> = selectWorldQuery
2222
.execute(Tuple.of(randomWorld()))
@@ -26,7 +26,7 @@ class WorldRepository(pool: Array<PgConnection>) : AbstractRepository<World>(poo
2626
val promise = Promise.promise<Array<World>>()
2727
val arr = arrayOfNulls<World>(numWorlds)
2828
val count = AtomicInteger(0)
29-
(this.pool[0] as SqlClientInternal).group { c ->
29+
(this.conn as SqlClientInternal).group { c ->
3030
repeat(numWorlds) {
3131
c.preparedQuery(SELECT_WORLD_SQL).execute(Tuple.of(randomWorld())) { ar ->
3232
val index = count.getAndIncrement()

frameworks/Kotlin/vertx-web-kotlin-dsljson/src/main/kotlin/com/example/starter/utils/JsonExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ inline fun <T> T.serialize(initialSizeHint: Int = 0): Buffer {
1616
val output = BufferOutputStream(initialSizeHint)
1717
DSL_JSON.serialize(this, output)
1818
return output
19-
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package com.example.starter.utils
22

33
import io.vertx.core.Vertx
4+
import io.vertx.core.http.HttpHeaders
45
import java.time.ZonedDateTime
56
import java.time.format.DateTimeFormatter
67

78
object PeriodicDateResolver {
8-
var current: String = next()
9+
var current: CharSequence = next()
910

1011
fun init(vertx: Vertx) {
1112
vertx.setPeriodic(1000L) { current = next() }
1213
}
1314

1415
@Suppress("NOTHING_TO_INLINE")
15-
private inline fun next(): String = DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now())
16+
private inline fun next(): CharSequence = HttpHeaders.createOptimized(
17+
DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now())
18+
)
1619
}

frameworks/Kotlin/vertx-web-kotlin-dsljson/src/main/kotlin/com/example/starter/utils/RowSetExtensions.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import io.vertx.sqlclient.RowSet
55

66
// This extension relies on the assumption the mapper never returns null, as it is defined. Otherwise,
77
// we prevent the overhead from having to do another iteration over the loop for a `filterNotNull` check.
8+
@Suppress("UNCHECKED_CAST")
89
inline fun <reified U> RowSet<Row>.mapToArray(mapper: (Row) -> U): Array<U> {
910
val arr = arrayOfNulls<U>(this.size())
1011
val iterator = this.iterator()
1112
var index = 0
1213
while (iterator.hasNext()) arr[index++] = mapper(iterator.next())
1314
return arr as Array<U>
14-
}
15+
}

frameworks/Kotlin/vertx-web-kotlin-dsljson/src/main/kotlin/com/example/starter/utils/ThrowableExtensions.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import java.net.SocketException
66

77
const val CONNECTION_RESET_MESSAGE = "Connection reset"
88

9-
fun Throwable.isConnectionReset(): Boolean {
10-
return (this is NativeIoException && this.expectedErr() == Errors.ERRNO_ECONNRESET_NEGATIVE)
11-
|| (this is SocketException && this.message == CONNECTION_RESET_MESSAGE)
12-
}
9+
@Suppress("NOTHING_TO_INLINE")
10+
inline fun Throwable.isConnectionReset(): Boolean {
11+
return when {
12+
this is NativeIoException && this.expectedErr() == Errors.ERRNO_ECONNRESET_NEGATIVE -> true
13+
this is SocketException && this.message == CONNECTION_RESET_MESSAGE -> true
14+
else -> false
15+
}
16+
}

0 commit comments

Comments
 (0)