Skip to content

Commit 3a86f43

Browse files
committed
Upgrade to Inverno 1.4.1
1 parent c421a9f commit 3a86f43

File tree

5 files changed

+72
-135
lines changed

5 files changed

+72
-135
lines changed

frameworks/Java/inverno/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>io.inverno.dist</groupId>
88
<artifactId>inverno-parent</artifactId>
9-
<version>1.2.2</version>
9+
<version>1.4.1</version>
1010
</parent>
1111
<groupId>com.techempower</groupId>
1212
<artifactId>inverno-benchmark</artifactId>

frameworks/Java/inverno/src/main/java/com/techempower/inverno/benchmark/internal/Handler.java

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
import io.inverno.mod.http.base.Parameter;
2929
import io.inverno.mod.http.base.Status;
3030
import io.inverno.mod.http.server.Exchange;
31-
import io.inverno.mod.http.server.ExchangeHandler;
31+
import io.inverno.mod.http.server.ExchangeContext;
32+
import io.inverno.mod.http.server.RootExchangeHandler;
3233
import io.inverno.mod.sql.SqlClient;
34+
import io.inverno.mod.sql.UnsafeSqlOperations;
3335
import io.netty.buffer.ByteBuf;
3436
import io.netty.buffer.Unpooled;
3537
import io.netty.channel.EventLoopGroup;
@@ -40,7 +42,7 @@
4042
import reactor.core.publisher.Mono;
4143

4244
@Bean( visibility = Visibility.PRIVATE )
43-
public class Handler implements ExchangeHandler<Exchange> {
45+
public class Handler implements RootExchangeHandler<ExchangeContext, Exchange<ExchangeContext>> {
4446

4547
private static final String PATH_PLAINTEXT = "/plaintext";
4648
private static final String PATH_JSON = "/json";
@@ -49,26 +51,27 @@ public class Handler implements ExchangeHandler<Exchange> {
4951
private static final String PATH_UPDATES = "/updates";
5052
private static final String PATH_FORTUNES = "/fortunes";
5153

54+
public static final String DB_SELECT_WORLD = "SELECT id, randomnumber from WORLD where id = $1";
55+
public static final String DB_UPDATE_WORLD = "UPDATE world SET randomnumber=$1 WHERE id=$2";
56+
public static final String DB_SELECT_FORTUNE = "SELECT id, message from FORTUNE";
57+
5258
private static final CharSequence STATIC_SERVER = AsciiString.cached("inverno");
5359

5460
private final Reactor reactor;
5561
private final ObjectMapper mapper;
56-
private final ReactorScope<SqlClient> pooledClientSqlClient;
57-
private final ReactorScope<SqlClient> poolSqlClient;
62+
private final ReactorScope<Mono<SqlClient>> sqlClient;
5863

5964
private EventLoopGroup dateEventLoopGroup;
6065

6166
private CharSequence date;
6267

6368
public Handler(Reactor reactor,
6469
ObjectMapper mapper,
65-
ReactorScope<SqlClient> pooledClientSqlClient,
66-
ReactorScope<SqlClient> poolSqlClient
70+
ReactorScope<Mono<SqlClient>> sqlClient
6771
) {
6872
this.reactor = reactor;
6973
this.mapper = mapper;
70-
this.pooledClientSqlClient = pooledClientSqlClient;
71-
this.poolSqlClient = poolSqlClient;
74+
this.sqlClient = sqlClient;
7275
}
7376

7477
@Init
@@ -85,7 +88,7 @@ public void destroy() {
8588
}
8689

8790
@Override
88-
public void handle(Exchange exchange) throws HttpException {
91+
public void handle(Exchange<ExchangeContext> exchange) throws HttpException {
8992
switch(exchange.request().getPath()) {
9093
case PATH_PLAINTEXT: {
9194
this.handle_plaintext(exchange);
@@ -145,7 +148,7 @@ public ByteBuf get() {
145148

146149
private static final Mono<ByteBuf> PLAIN_TEXT_MONO = Mono.fromSupplier(new PlaintextSupplier());
147150

148-
public void handle_plaintext(Exchange exchange) throws HttpException {
151+
public void handle_plaintext(Exchange<ExchangeContext> exchange) throws HttpException {
149152
exchange.response()
150153
.headers(h -> h
151154
.add(HttpHeaderNames.SERVER, STATIC_SERVER)
@@ -158,7 +161,7 @@ public void handle_plaintext(Exchange exchange) throws HttpException {
158161
.stream(PLAIN_TEXT_MONO);
159162
}
160163

161-
public void handle_json(Exchange exchange) throws HttpException {
164+
public void handle_json(Exchange<ExchangeContext> exchange) throws HttpException {
162165
try {
163166
exchange.response()
164167
.headers(h -> h
@@ -175,21 +178,20 @@ public void handle_json(Exchange exchange) throws HttpException {
175178
}
176179
}
177180

178-
private static final String DB_SELECT_WORLD = "SELECT id, randomnumber from WORLD where id = $1";
179-
180181
private static int randomWorldId() {
181182
return 1 + ThreadLocalRandom.current().nextInt(10000);
182183
}
183184

184-
public void handle_db(Exchange exchange) throws HttpException {
185+
public void handle_db(Exchange<ExchangeContext> exchange) throws HttpException {
185186
exchange.response()
186187
.headers(h -> h
187188
.add(HttpHeaderNames.SERVER, STATIC_SERVER)
188189
.add(HttpHeaderNames.DATE, this.date)
189190
.add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON)
190191
)
191192
.body()
192-
.raw().stream(this.pooledClientSqlClient.get().queryForObject(
193+
.raw().stream(this.sqlClient.get().flatMap(client ->
194+
client.queryForObject(
193195
DB_SELECT_WORLD,
194196
row -> {
195197
try {
@@ -201,21 +203,21 @@ public void handle_db(Exchange exchange) throws HttpException {
201203
},
202204
randomWorldId()
203205
)
204-
);
206+
));
205207
}
206208

207209
private static final String PARAMETER_QUERIES = "queries";
208210

209-
private int extractQueriesParameter(Exchange exchange) {
211+
private int extractQueriesParameter(Exchange<ExchangeContext> exchange) {
210212
try {
211213
return Math.min(500, Math.max(1, exchange.request().queryParameters().get(PARAMETER_QUERIES).map(Parameter::asInteger).orElse(1)));
212214
}
213-
catch (ConverterException e) { // TODO
215+
catch (ConverterException e) {
214216
return 1;
215217
}
216218
}
217219

218-
public void handle_queries(Exchange exchange) throws HttpException {
220+
public void handle_queries(Exchange<ExchangeContext> exchange) throws HttpException {
219221
int queries = this.extractQueriesParameter(exchange);
220222
exchange.response()
221223
.headers(h -> h
@@ -224,12 +226,17 @@ public void handle_queries(Exchange exchange) throws HttpException {
224226
.add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON)
225227
)
226228
.body()
227-
.raw().stream(Flux.range(0, queries)
228-
.flatMap(ign -> this.pooledClientSqlClient.get().queryForObject(
229-
DB_SELECT_WORLD,
230-
row -> new World(row.getInteger(0), row.getInteger(1)),
231-
randomWorldId()
232-
))
229+
.raw().stream(this.sqlClient.get()
230+
.flatMapMany(client -> ((UnsafeSqlOperations)client)
231+
.batchQueries(ops ->
232+
Flux.range(0, queries)
233+
.map(ign -> ops.queryForObject(
234+
DB_SELECT_WORLD,
235+
row -> new World(row.getInteger(0), row.getInteger(1)),
236+
randomWorldId()
237+
))
238+
)
239+
)
233240
.collectList()
234241
.map(worlds -> {
235242
try {
@@ -242,27 +249,28 @@ public void handle_queries(Exchange exchange) throws HttpException {
242249
);
243250
}
244251

245-
private static final String DB_UPDATE_WORLD = "UPDATE world SET randomnumber=$1 WHERE id=$2";
246-
247-
public void handle_updates(Exchange exchange) throws HttpException {
252+
public void handle_updates(Exchange<ExchangeContext> exchange) throws HttpException {
248253
int queries = this.extractQueriesParameter(exchange);
249254

250255
exchange.response()
251-
.headers(h -> h
252-
.add(HttpHeaderNames.SERVER, STATIC_SERVER)
253-
.add(HttpHeaderNames.DATE, this.date)
254-
.add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON)
255-
)
256-
.body()
257-
.raw().stream(this.poolSqlClient.get().connection(ops -> Flux.range(0, queries)
258-
.flatMap(ign -> ops.queryForObject(
259-
DB_SELECT_WORLD,
260-
row -> new World(row.getInteger(0), randomWorldId()),
261-
randomWorldId()
262-
)
263-
)
256+
.headers(h -> h
257+
.add(HttpHeaderNames.SERVER, STATIC_SERVER)
258+
.add(HttpHeaderNames.DATE, this.date)
259+
.add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON)
260+
)
261+
.body()
262+
.raw().stream(this.sqlClient.get()
263+
.flatMapMany(client -> Flux.from(((UnsafeSqlOperations)client)
264+
.batchQueries(ops ->
265+
Flux.range(0, queries)
266+
.map(ign -> ops.queryForObject(
267+
DB_SELECT_WORLD,
268+
row -> new World(row.getInteger(0), randomWorldId()),
269+
randomWorldId()
270+
))
271+
))
264272
.collectSortedList()
265-
.delayUntil(worlds -> ops.batchUpdate(
273+
.delayUntil(worlds -> client.batchUpdate(
266274
DB_UPDATE_WORLD,
267275
worlds.stream().map(world -> new Object[] { world.getRandomNumber(), world.getId() })
268276
)
@@ -274,27 +282,29 @@ public void handle_updates(Exchange exchange) throws HttpException {
274282
catch (JsonProcessingException e) {
275283
throw new InternalServerErrorException(e);
276284
}
277-
}))
278-
);
285+
})
286+
)
287+
);
279288
}
280289

281-
private static final String DB_SELECT_FORTUNE = "SELECT id, message from FORTUNE";
282290
private static final CharSequence MEDIA_TEXT_HTML_UTF8 = AsciiString.cached("text/html; charset=utf-8");
283291

284292
private static final FortunesTemplate.Renderer<CompletableFuture<ByteBuf>> FORTUNES_RENDERER = FortunesTemplate.bytebuf(() -> Unpooled.unreleasableBuffer(Unpooled.buffer()));
285293

286-
public void handle_fortunes(Exchange exchange) throws HttpException {
294+
public void handle_fortunes(Exchange<ExchangeContext> exchange) throws HttpException {
287295
exchange.response()
288296
.headers(h -> h
289297
.add(HttpHeaderNames.SERVER, STATIC_SERVER)
290298
.add(HttpHeaderNames.DATE, this.date)
291299
.add(HttpHeaderNames.CONTENT_TYPE, MEDIA_TEXT_HTML_UTF8)
292300
)
293301
.body()
294-
.raw().stream(Flux.from(this.pooledClientSqlClient.get().query(
295-
DB_SELECT_FORTUNE,
296-
row -> new Fortune(row.getInteger(0), row.getString(1))
297-
))
302+
.raw().stream(this.sqlClient.get().flatMapMany(client ->
303+
client.query(
304+
DB_SELECT_FORTUNE,
305+
row -> new Fortune(row.getInteger(0), row.getString(1))
306+
)
307+
)
298308
.collectList()
299309
.flatMap(fortunes -> {
300310
fortunes.add(new Fortune(0, "Additional fortune added at request time."));

frameworks/Java/inverno/src/main/java/com/techempower/inverno/benchmark/internal/PooledClientSqlClientReactorScope.java

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,30 @@
33
import com.techempower.inverno.benchmark.AppConfiguration;
44

55
import io.inverno.core.annotation.Bean;
6+
import io.inverno.core.annotation.Bean.Visibility;
67
import io.inverno.core.annotation.Destroy;
78
import io.inverno.core.annotation.Init;
8-
import io.inverno.core.annotation.Bean.Visibility;
99
import io.inverno.mod.base.concurrent.Reactor;
1010
import io.inverno.mod.base.concurrent.ReactorScope;
1111
import io.inverno.mod.base.concurrent.VertxReactor;
1212
import io.inverno.mod.sql.SqlClient;
13-
import io.inverno.mod.sql.vertx.PoolSqlClient;
13+
import io.inverno.mod.sql.vertx.ConnectionSqlClient;
1414
import io.vertx.core.Vertx;
1515
import io.vertx.core.VertxOptions;
1616
import io.vertx.pgclient.PgConnectOptions;
17-
import io.vertx.pgclient.PgPool;
18-
import io.vertx.sqlclient.PoolOptions;
17+
import io.vertx.pgclient.PgConnection;
18+
import reactor.core.publisher.Mono;
1919

20-
@Bean( name = "poolSqlClient", visibility = Visibility.PRIVATE )
21-
public class PoolSqlClientReactorScope extends ReactorScope<SqlClient> {
20+
@Bean( name = "SqlClient", visibility = Visibility.PRIVATE )
21+
public class SqlClientReactorScope extends ReactorScope<Mono<SqlClient>> {
2222

2323
private final AppConfiguration configuration;
2424
private final Reactor reactor;
2525

2626
private Vertx vertx;
2727
private PgConnectOptions connectOptions;
28-
private PoolOptions poolOptions;
2928

30-
public PoolSqlClientReactorScope(AppConfiguration configuration, Reactor reactor) {
29+
public SqlClientReactorScope(AppConfiguration configuration, Reactor reactor) {
3130
this.configuration = configuration;
3231
this.reactor = reactor;
3332
}
@@ -47,9 +46,8 @@ public void init() {
4746
.setDatabase(this.configuration.db_database())
4847
.setUser(this.configuration.db_username())
4948
.setPassword(this.configuration.db_password())
50-
.setCachePreparedStatements(true);
51-
52-
this.poolOptions = new PoolOptions().setMaxSize(4);
49+
.setCachePreparedStatements(true)
50+
.setPipeliningLimit(100_100);
5351
}
5452

5553
@Destroy
@@ -60,8 +58,7 @@ public void destroy() {
6058
}
6159

6260
@Override
63-
protected SqlClient create() {
64-
return new PoolSqlClient(PgPool.pool(this.vertx, this.connectOptions, this.poolOptions));
61+
protected Mono<SqlClient> create() {
62+
return Mono.fromCompletionStage(PgConnection.connect(this.vertx, this.connectOptions).toCompletionStage()).map(pgConn -> (SqlClient)new ConnectionSqlClient(pgConn)).cache();
6563
}
66-
6764
}

frameworks/Java/inverno/src/main/java/module-info.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
@io.inverno.core.annotation.Module( excludes = { "io.inverno.mod.sql.vertx" } )
2-
3-
@io.inverno.core.annotation.Wire(beans="pooledClientSqlClient", into="handler:pooledClientSqlClient")
4-
@io.inverno.core.annotation.Wire(beans="poolSqlClient", into="handler:poolSqlClient")
52
module com.techempower.inverno.benchmark {
63
requires io.inverno.mod.boot;
74
requires io.inverno.mod.http.server;

0 commit comments

Comments
 (0)