Skip to content

Commit

Permalink
refactor: Removed meters from Query
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Ruaux committed Feb 24, 2023
1 parent 2b54983 commit 1a230cb
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 836 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.redis.smartcache.core;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Timer;
import io.trino.sql.tree.Statement;

public class Query {
Expand All @@ -12,49 +10,12 @@ public class Query {
private final String id;
private final String sql;
private final Statement statement;
private final Timer timer;
private final Timer backendTimer;
private final Timer cacheGetTimer;
private final Timer cachePutTimer;
private final Counter cacheHitCounter;
private final Counter cacheMissCounter;
private long ttl = TTL_NO_CACHING;

public Query(String id, String sql, Statement statement, Timer timer, Timer backendTimer, Timer cacheGetTimer,
Timer cachePutTimer, Counter cacheHitCounter, Counter cacheMissCounter) {
public Query(String id, String sql, Statement statement) {
this.id = id;
this.sql = sql;
this.statement = statement;
this.timer = timer;
this.backendTimer = backendTimer;
this.cacheGetTimer = cacheGetTimer;
this.cachePutTimer = cachePutTimer;
this.cacheHitCounter = cacheHitCounter;
this.cacheMissCounter = cacheMissCounter;
}

public Counter getCacheHitCounter() {
return cacheHitCounter;
}

public Counter getCacheMissCounter() {
return cacheMissCounter;
}

public Timer getTimer() {
return timer;
}

public Timer getBackendTimer() {
return backendTimer;
}

public Timer getCacheGetTimer() {
return cacheGetTimer;
}

public Timer getCachePutTimer() {
return cachePutTimer;
}

public boolean hasStatement() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.Timer;
import io.trino.sql.parser.ParsingException;
import io.trino.sql.parser.ParsingOptions;
Expand Down Expand Up @@ -360,17 +361,58 @@ public Query getQuery(String sql) {
if (queryCache.containsKey(id)) {
return queryCache.get(id);
}
return new Query(id, sql, parse(sql), timer(METER_QUERY, id), timer(METER_BACKEND, id),
timer(METER_CACHE_GET, id), timer(METER_CACHE_PUT, id),
counter(METER_CACHE_GET, id, TAG_RESULT, TAG_HIT), counter(METER_CACHE_GET, id, TAG_RESULT, TAG_MISS));
Query query = new Query(id, sql, parse(sql));
createTimer(METER_QUERY, query);
createTimer(METER_BACKEND, query);
createTimer(METER_CACHE_GET, query);
createTimer(METER_CACHE_PUT, query);
createCounter(METER_CACHE_GET, query, TAG_RESULT, TAG_HIT);
createCounter(METER_CACHE_GET, query, TAG_RESULT, TAG_MISS);
return query;
}

public Timer getQueryTimer(Query query) {
return getTimer(METER_QUERY, query);
}

public Timer getBackendTimer(Query query) {
return getTimer(METER_BACKEND, query);
}

public Timer getCacheGetTimer(Query query) {
return getTimer(METER_CACHE_GET, query);
}

public Timer getCachePutTimer(Query query) {
return getTimer(METER_CACHE_PUT, query);
}

public Counter getCacheHitCounter(Query query) {
return getCounter(METER_CACHE_GET, query, TAG_RESULT, TAG_HIT);
}

public Counter getCacheMissCounter(Query query) {
return getCounter(METER_CACHE_GET, query, TAG_RESULT, TAG_MISS);
}

private Counter getCounter(String name, Query query, String... tags) {
return meterRegistry.get(name).tags(tags(query)).tags(tags).counter();
}

private Timer getTimer(String name, Query query) {
return meterRegistry.get(name).tags(tags(query)).timer();
}

private Timer createTimer(String name, Query query) {
return Timer.builder(name).tags(tags(query)).publishPercentiles(0.9, 0.99).register(meterRegistry);
}

private Timer timer(String name, String queryId) {
return Timer.builder(name).tag(TAG_QUERY, queryId).publishPercentiles(0.9, 0.99).register(meterRegistry);
private Tags tags(Query query) {
return Tags.of(TAG_QUERY, query.getId());
}

private Counter counter(String name, String queryId, String... tags) {
return Counter.builder(name).tag(TAG_QUERY, queryId).tags(tags).register(meterRegistry);
private Counter createCounter(String name, Query query, String... tags) {
return Counter.builder(name).tags(tags(query)).tags(tags).register(meterRegistry);
}

private io.trino.sql.tree.Statement parse(String sql) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ protected String id(Query query) {
protected boolean execute(String sql, Callable<Boolean> executable) throws SQLException {
Query query = connection.fireRules(sql);
try {
return query.getTimer().recordCallable(() -> {
return connection.getQueryTimer(query).recordCallable(() -> {
queryExecution = getCachedExecution(query);
if (queryExecution.hasResultSet()) {
return true;
}
return query.getBackendTimer().recordCallable(executable);
return connection.getBackendTimer(query).recordCallable(executable);
});
} catch (SQLException e) {
throw e;
Expand All @@ -65,12 +65,12 @@ protected boolean execute(String sql, Callable<Boolean> executable) throws SQLEx
protected ResultSet executeQuery(String sql, Callable<ResultSet> callable) throws SQLException {
Query query = connection.fireRules(sql);
try {
return query.getTimer().recordCallable(() -> {
return connection.getQueryTimer(query).recordCallable(() -> {
QueryExecution execution = getCachedExecution(query);
if (execution.hasResultSet()) {
return execution.getResultSet();
}
return cacheResultSet(execution.getQuery(), query.getBackendTimer().recordCallable(callable));
return cacheResultSet(execution.getQuery(), connection.getBackendTimer(query).recordCallable(callable));
});
} catch (SQLException e) {
throw e;
Expand All @@ -90,7 +90,7 @@ private ResultSet cacheResultSet(Query query, ResultSet resultSet) throws SQLExc
CachedRowSet cachedRowSet = connection.createCachedRowSet();
cachedRowSet.populate(resultSet);
cachedRowSet.beforeFirst();
query.getCachePutTimer()
connection.getCachePutTimer(query)
.record(() -> connection.getResultSetCache().put(id(query), query.getTtl(), cachedRowSet));
cachedRowSet.beforeFirst();
return cachedRowSet;
Expand Down Expand Up @@ -128,12 +128,12 @@ public ResultSet getResultSet() {

private QueryExecution getCachedExecution(Query query) throws Exception {
if (query.isCaching()) {
ResultSet resultSet = query.getCacheGetTimer()
ResultSet resultSet = connection.getCacheGetTimer(query)
.recordCallable(() -> connection.getResultSetCache().get(id(query)));
if (resultSet == null) {
query.getCacheMissCounter().increment();
connection.getCacheMissCounter(query).increment();
} else {
query.getCacheHitCounter().increment();
connection.getCacheHitCounter(query).increment();
}
return new QueryExecution(query, resultSet);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ void testRegex() {
}

private Query query(String sql) {
return new Query(SmartConnection.crc32(sql), sql, PARSER.createStatement(sql, PARSING_OPTIONS), null, null,
null, null, null, null);
return new Query(SmartConnection.crc32(sql), sql, PARSER.createStatement(sql, PARSING_OPTIONS));
}

}
Loading

0 comments on commit 1a230cb

Please sign in to comment.