Skip to content
This repository was archived by the owner on Apr 7, 2019. It is now read-only.

Commit

Permalink
log query
Browse files Browse the repository at this point in the history
  • Loading branch information
naturefreshmilk committed Jul 16, 2018
1 parent 1967243 commit fc518ee
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 10 deletions.
15 changes: 14 additions & 1 deletion src/main/java/io/rudin/minetest/tileserver/TileRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public byte[] render(int tileX, int tileY, int zoom) throws IllegalArgumentExcep

int z1 = mapblockInfo.z;
int z2 = mapblockInfo.z + ((int)mapblockInfo.height * -1);


long start = System.currentTimeMillis();

Integer count = ctx
.select(DSL.count())
.from(BLOCKS)
Expand All @@ -110,6 +112,12 @@ public byte[] render(int tileX, int tileY, int zoom) throws IllegalArgumentExcep
.and(yCondition)
)
.fetchOne(DSL.count());

long diff = System.currentTimeMillis() - start;

if (diff > 250 && cfg.logQueryPerformance()){
logger.warn("white-count-query took {} ms", diff);
}

if (count == 0) {
logger.debug("Fail-fast, got zero mapblock count for x={}-{} z={}-{}", x1,x2, z1,z2);
Expand Down Expand Up @@ -297,6 +305,11 @@ public BufferedImage renderImage(int tileX, int tileY, int zoom) throws IllegalA

now = System.currentTimeMillis();
long timingZeroCountCheck = now - start;

if (timingZeroCountCheck > 250 && cfg.logQueryPerformance()){
logger.warn("count-zero-check took {} ms", timingZeroCountCheck);
}

start = now;

long timingRender = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import io.rudin.minetest.tileserver.config.TileServerConfig;
import jdk.nashorn.internal.ir.Block;
import org.jooq.DSLContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Singleton;
Expand All @@ -23,15 +25,19 @@
@Singleton
public class BlocksRecordAccessor extends CacheLoader<Coordinate, Optional<BlocksRecord>> {

private static final Logger logger = LoggerFactory.getLogger(BlocksRecordAccessor.class);

@Inject
public BlocksRecordAccessor(DSLContext ctx){
public BlocksRecordAccessor(DSLContext ctx, TileServerConfig cfg){
this.ctx = ctx;
this.cfg = cfg;
this.cache = CacheBuilder.newBuilder()
.maximumSize(5000)
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(this);
}

private final TileServerConfig cfg;

private final LoadingCache<Coordinate, Optional<BlocksRecord>> cache;

Expand All @@ -46,6 +52,9 @@ public Optional<BlocksRecord> get(Coordinate coords){
}

public List<BlocksRecord> getTopyDownYStride(int x, int z, int minY, int maxY) {

long start = System.currentTimeMillis();

List<BlocksRecord> list = ctx
.selectFrom(BLOCKS)
.where(BLOCKS.POSX.eq(x))
Expand All @@ -55,6 +64,12 @@ public List<BlocksRecord> getTopyDownYStride(int x, int z, int minY, int maxY) {
.orderBy(BLOCKS.POSY.desc())
.fetch();

long diff = System.currentTimeMillis() - start;

if (diff > 500 && cfg.logQueryPerformance()){
logger.warn("getTopyDownYStride took {} ms", diff);
}

for (BlocksRecord record: list){
update(record);
}
Expand All @@ -69,11 +84,23 @@ public void update(BlocksRecord block){

@Override
public Optional<BlocksRecord> load(Coordinate coordinate) throws Exception {
return ctx

long start = System.currentTimeMillis();


Optional<BlocksRecord> record = ctx
.selectFrom(BLOCKS)
.where(BLOCKS.POSX.eq(coordinate.x))
.and(BLOCKS.POSY.eq(coordinate.y))
.and(BLOCKS.POSZ.eq(coordinate.z))
.fetchOptional();


long diff = System.currentTimeMillis() - start;

if (diff > 500 && cfg.logQueryPerformance()){
logger.warn("load took {} ms", diff);
}
return record;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public interface TileServerConfig extends Config {
Logging stuff
*/

@Key("log.query.performance")
@DefaultValue("false")
boolean logQueryPerformance();

@Key("log.tile.updatetimings")
@DefaultValue("false")
boolean logTileUpdateTimings();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.rudin.minetest.tileserver.module;

import org.jooq.ExecuteContext;
import org.jooq.impl.DefaultExecuteListener;
import org.jooq.tools.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoggingExecuteListener extends DefaultExecuteListener {

private static final Logger logger = LoggerFactory.getLogger(LoggingExecuteListener.class);

StopWatch watch;

class SQLPerformanceWarning extends Exception {}

@Override
public void executeStart(ExecuteContext ctx) {
super.executeStart(ctx);
watch = new StopWatch();
}

@Override
public void executeEnd(ExecuteContext ctx) {
super.executeEnd(ctx);
if (watch.split() > 5_000_000_000L)
logger.warn(
"Slow SQL",
new SQLPerformanceWarning());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import javax.inject.Provider;
import javax.inject.Singleton;

import io.rudin.minetest.tileserver.config.TileServerConfig;
import io.rudin.minetest.tileserver.module.LoggingExecuteListener;
import org.jooq.DSLContext;
import org.jooq.ExecuteListener;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;

Expand All @@ -14,15 +17,23 @@
public class DSLContextProvider implements Provider<DSLContext> {

@Inject
public DSLContextProvider(HikariDataSource ds) {
public DSLContextProvider(HikariDataSource ds, TileServerConfig cfg) {
this.ds = ds;
this.cfg = cfg;
}


private final TileServerConfig cfg;

private final HikariDataSource ds;

@Override
public DSLContext get() {
return DSL.using(ds, SQLDialect.POSTGRES);
DSLContext ctx = DSL.using(ds, SQLDialect.POSTGRES);

if (cfg.logQueryPerformance())
ctx.configuration().set(new LoggingExecuteListener());

return ctx;
}

}
23 changes: 20 additions & 3 deletions src/main/java/io/rudin/minetest/tileserver/route/PlayerRoute.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import javax.inject.Inject;
import javax.inject.Singleton;

import io.rudin.minetest.tileserver.accessor.BlocksRecordAccessor;
import io.rudin.minetest.tileserver.blockdb.tables.pojos.PlayerMetadata;
import io.rudin.minetest.tileserver.config.TileServerConfig;
import io.rudin.minetest.tileserver.entity.PlayerInfo;
import org.jooq.DSLContext;

import io.rudin.minetest.tileserver.blockdb.tables.pojos.Player;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spark.Request;
import spark.Response;
import spark.Route;
Expand All @@ -22,17 +26,24 @@
@Singleton
public class PlayerRoute implements Route {

private static final Logger logger = LoggerFactory.getLogger(PlayerRoute.class);

@Inject
public PlayerRoute(DSLContext ctx) {
public PlayerRoute(DSLContext ctx, TileServerConfig cfg) {
this.ctx = ctx;
this.cfg = cfg;
}


private final TileServerConfig cfg;

private final DSLContext ctx;

@Override
public Object handle(Request request, Response response) throws Exception {
response.header("Content-Type", "application/json");

long start = System.currentTimeMillis();

Timestamp ts = new Timestamp(System.currentTimeMillis() - (3600L*1000L));

List<Player> players = ctx
Expand All @@ -43,7 +54,7 @@ public Object handle(Request request, Response response) throws Exception {

List<PlayerInfo> list = new ArrayList<>();

for (Player player: players){
for (Player player: players) {

List<PlayerMetadata> metadata = ctx
.selectFrom(PLAYER_METADATA)
Expand All @@ -53,6 +64,12 @@ public Object handle(Request request, Response response) throws Exception {
list.add(new PlayerInfo(player, metadata));
}

long diff = System.currentTimeMillis() - start;

if (diff > 250 && cfg.logQueryPerformance()){
logger.warn("getTopyDownYStride took {} ms", diff);
}

return list;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</encoder>
</appender>

<logger name="org.jooq.Constants" level="WARN"></logger>
<logger name="org.jooq" level="WARN"/>
<logger name="io.rudin.minetest.tileserver.TileRenderer" level="INFO"/>

<root level="info">
Expand Down

0 comments on commit fc518ee

Please sign in to comment.