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

Upgrades to Helidon 4.1.5 #9556

Merged
merged 12 commits into from
Jan 23, 2025
Merged
25 changes: 13 additions & 12 deletions frameworks/Java/helidon/nima/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>io.helidon.applications</groupId>
<artifactId>helidon-se</artifactId>
<version>4.1.2</version>
<version>4.1.5</version>
<relativePath/>
</parent>

Expand All @@ -38,6 +38,7 @@
<rocker.version>1.3.0</rocker.version>
<vertx-pg-client.version>4.5.3</vertx-pg-client.version>
<jsoniter.version>0.9.23</jsoniter.version>
<jte.version>3.1.15</jte.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -78,9 +79,9 @@
<version>42.6.1</version>
</dependency>
<dependency>
<groupId>com.fizzed</groupId>
<artifactId>rocker-runtime</artifactId>
<version>${rocker.version}</version>
<groupId>gg.jte</groupId>
<artifactId>jte</artifactId>
<version>${jte.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.common.testing</groupId>
Expand All @@ -98,7 +99,6 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand All @@ -125,20 +125,21 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>com.fizzed</groupId>
<artifactId>rocker-maven-plugin</artifactId>
<version>${rocker.version}</version>
<groupId>gg.jte</groupId>
<artifactId>jte-maven-plugin</artifactId>
<version>${jte.version}</version>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/views</sourceDirectory>
<contentType>Html</contentType>
</configuration>
<executions>
<execution>
<id>generate-rocker-templates</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<templateDirectory>src/main/resources</templateDirectory>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.List;
import java.util.Map;

import com.jsoniter.output.JsonStream;
import com.jsoniter.output.JsonStreamPool;
Expand All @@ -15,7 +15,7 @@ private JsonSerializer() {
}

/**
* Serialize an instance into a JSON object and return it as a byte array.
* Serialize an instance into a byte array.
*
* @param obj the instance
* @return the byte array
Expand All @@ -28,19 +28,31 @@ public static byte[] serialize(Object obj) {
return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail());
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonStreamPool.returnJsonStream(stream);
}
}

/**
* Serialize a map of strings into a JSON object and return it as a byte array.
* Serialize an instance into a JSON stream.
*
* @param obj the instance
* @param stream the JSON stream
*/
public static void serialize(Object obj, JsonStream stream) {
try {
stream.reset(null);
stream.writeVal(obj.getClass(), obj);
} catch (IOException e) {
throw new JsonException(e);
}
}

/**
* Serialize a map of strings into a JSON stream.
*
* @param map the map
* @return the byte array
* @param stream the JSON stream
*/
public static byte[] serialize(Map<String, String> map) {
JsonStream stream = JsonStreamPool.borrowJsonStream();
public static void serialize(Map<String, String> map, JsonStream stream) {
try {
stream.reset(null);
stream.writeObjectStart();
Expand All @@ -53,22 +65,18 @@ public static byte[] serialize(Map<String, String> map) {
}
});
stream.writeObjectEnd();
return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail());
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonStreamPool.returnJsonStream(stream);
}
}

/**
* Serialize a list of objects into a JSON array and return it as a byte array.
* Serialize a list of objects into a JSON stream.
*
* @param objs the list of objects
* @return the byte array
* @param stream the JSON stream
*/
public static byte[] serialize(List<?> objs) {
JsonStream stream = JsonStreamPool.borrowJsonStream();
public static void serialize(List<?> objs, JsonStream stream) {
try {
stream.reset(null);
stream.writeArrayStart();
Expand All @@ -82,11 +90,8 @@ public static byte[] serialize(List<?> objs) {

}
stream.writeArrayEnd();
return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail());
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonStreamPool.returnJsonStream(stream);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,16 +19,18 @@
import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;

import com.jsoniter.output.JsonStream;
import com.jsoniter.output.JsonStreamPool;
import io.helidon.benchmark.nima.models.DbRepository;
import io.helidon.benchmark.nima.models.HikariJdbcRepository;
import io.helidon.benchmark.nima.models.PgClientRepository;
import io.helidon.benchmark.nima.services.DbService;
import io.helidon.benchmark.nima.services.FortuneHandler;
import io.helidon.config.Config;
import io.helidon.config.ConfigException;
import io.helidon.http.Header;
import io.helidon.http.HeaderNames;
import io.helidon.http.HeaderValues;
import io.helidon.config.Config;
import io.helidon.config.ConfigException;
import io.helidon.logging.common.LogConfig;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.http.Handler;
Expand Down Expand Up @@ -93,7 +95,7 @@ static void routing(HttpRules rules) {

static class PlaintextHandler implements Handler {
static final Header CONTENT_TYPE = HeaderValues.createCached(HeaderNames.CONTENT_TYPE,
"text/plain; charset=UTF-8");
"text/plain; charset=UTF-8");
static final Header CONTENT_LENGTH = HeaderValues.createCached(HeaderNames.CONTENT_LENGTH, "13");
private static final byte[] RESPONSE_BYTES = "Hello, World!".getBytes(StandardCharsets.UTF_8);

Expand All @@ -110,14 +112,20 @@ static class JsonHandler implements Handler {
private static final String MESSAGE = "Hello, World!";
private static final int JSON_LENGTH = serialize(new Message(MESSAGE)).length;
static final Header CONTENT_LENGTH = HeaderValues.createCached(HeaderNames.CONTENT_LENGTH,
String.valueOf(JSON_LENGTH));
String.valueOf(JSON_LENGTH));

@Override
public void handle(ServerRequest req, ServerResponse res) {
res.header(CONTENT_LENGTH);
res.header(HeaderValues.CONTENT_TYPE_JSON);
res.header(Main.SERVER);
res.send(serialize(new Message(MESSAGE)));
JsonStream stream = JsonStreamPool.borrowJsonStream();
try {
res.header(CONTENT_LENGTH);
res.header(HeaderValues.CONTENT_TYPE_JSON);
res.header(Main.SERVER);
serialize(new Message(MESSAGE), stream);
res.send(stream.buffer().data(), 0, stream.buffer().tail());
} finally {
JsonStreamPool.returnJsonStream(stream);
}
}
}

Expand Down Expand Up @@ -147,4 +155,4 @@ public String getMessage() {
return message;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

package io.helidon.benchmark.nima.models;

public final class Fortune {
public final class Fortune implements Comparable<Fortune> {
public int id;
public String message;

Expand All @@ -17,4 +17,8 @@ public int getId() {
public String getMessage() {
return message;
}
@Override
public int compareTo(Fortune other) {
return message.compareTo(other.message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@

package io.helidon.benchmark.nima.models;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

import io.vertx.core.Vertx;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.pgclient.PgConnection;
import io.vertx.sqlclient.PreparedQuery;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;

class PgClientConnectionPool implements AutoCloseable {

private final Vertx vertx;
private final PgConnectOptions options;
private final ReentrantLock lock = new ReentrantLock();
private final Map<String, PgClientConnection> connectionMap = new HashMap<>();

public PgClientConnectionPool(Vertx vertx, PgConnectOptions options) {
this.vertx = vertx;
this.options = options;
}

public PgClientConnection clientConnection() {
String carrierThread = carrierThread();
PgClientConnection connection = connectionMap.get(carrierThread);
if (connection == null) {
try {
lock.lock();
connection = connectionMap.get(carrierThread);
if (connection == null) {
connection = newConnection();
connectionMap.put(carrierThread, connection);
}
} finally {
lock.unlock();
}
}
return connection;
}

@Override
public void close() {
try {
for (PgClientConnection connection : connectionMap.values()) {
connection.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private PgClientConnection newConnection() {
try {
PgConnection conn = PgConnection.connect(vertx, options)
.toCompletionStage().toCompletableFuture().get();
PgClientConnection clientConn = new PgClientConnection(conn);
clientConn.prepare();
return clientConn;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

static String carrierThread() {
String threadName = Thread.currentThread().toString();
return threadName.substring(threadName.indexOf('@') + 1);
}

public static class PgClientConnection implements AutoCloseable {
static final int UPDATE_QUERIES = 500;
private static String SELECT_WORLD = "SELECT id, randomnumber from WORLD where id=$1";
private static String SELECT_FORTUNE = "SELECT * from FORTUNE";

private PreparedQuery<RowSet<Row>> worldQuery;
private PreparedQuery<RowSet<Row>> fortuneQuery;
private PreparedQuery<RowSet<Row>>[] updateQuery;

private final PgConnection conn;

PgClientConnection(PgConnection conn) {
this.conn = conn;
}

public PgConnection pgConnection() {
return conn;
}

@Override
public void close() {
conn.close();
}

public PreparedQuery<RowSet<Row>> worldQuery() {
return worldQuery;
}

public PreparedQuery<RowSet<Row>> fortuneQuery() {
return fortuneQuery;
}

public PreparedQuery<RowSet<Row>> updateQuery(int queryCount) {
return updateQuery[queryCount - 1];
}

@SuppressWarnings("unchecked")
void prepare() {
try {
worldQuery = conn.prepare(SELECT_WORLD)
.toCompletionStage().toCompletableFuture().get().query();
fortuneQuery = conn.prepare(SELECT_FORTUNE)
.toCompletionStage().toCompletableFuture().get().query();
updateQuery = (PreparedQuery<RowSet<Row>>[]) new PreparedQuery<?>[UPDATE_QUERIES];
for (int i = 0; i < UPDATE_QUERIES; i++) {
updateQuery[i] = conn.prepare(singleUpdate(i + 1))
.toCompletionStage().toCompletableFuture().get().query();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private static String singleUpdate(int count) {
StringBuilder sql = new StringBuilder();
sql.append("UPDATE WORLD SET RANDOMNUMBER = CASE ID");
for (int i = 0; i < count; i++) {
int k = i * 2 + 1;
sql.append(" WHEN $").append(k).append(" THEN $").append(k + 1);
}
sql.append(" ELSE RANDOMNUMBER");
sql.append(" END WHERE ID IN ($1");
for (int i = 1; i < count; i++) {
int k = i * 2 + 1;
sql.append(",$").append(k);
}
sql.append(")");
return sql.toString();
}
}
}
Loading
Loading