Skip to content

Commit bab839f

Browse files
committed
Change QueryPlanInit to support thresholdMicros per hash
Effectively means that we can provide a thresholdMicros per query plan hash [for the plans that we want collected]
1 parent dde2567 commit bab839f

File tree

4 files changed

+95
-17
lines changed

4 files changed

+95
-17
lines changed

.github/workflows/jdk-ea.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
strategy:
1717
fail-fast: false
1818
matrix:
19-
java_version: [GA,EA,23]
19+
java_version: [GA,EA]
2020
os: [ubuntu-latest]
2121

2222
steps:
Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package io.ebean.meta;
22

3-
import java.util.HashSet;
4-
import java.util.Set;
3+
import java.util.*;
4+
5+
import static java.util.Objects.requireNonNull;
56

67
/**
78
* Initiate query plan collection for plans by their hash or all query plans.
89
*/
910
public class QueryPlanInit {
1011

11-
private boolean all;
12+
private final Map<String,Long> hashes = new HashMap<>();
1213

13-
private Set<String> hashes = new HashSet<>();
14+
private boolean all;
1415

15-
private long thresholdMicros;
16+
private long defaultThresholdMicros;
1617

1718
/**
1819
* Return true if this initiates bind collection on all query plans.
@@ -29,39 +30,74 @@ public void setAll(boolean all) {
2930
}
3031

3132
/**
32-
* Return the query execution time threshold which must be exceeded to initiate
33+
* Return the default query execution time threshold which must be exceeded to initiate
3334
* query plan collection.
3435
*/
3536
public long thresholdMicros() {
36-
return thresholdMicros;
37+
return defaultThresholdMicros;
3738
}
3839

3940
/**
40-
* Set the query execution time threshold which must be exceeded to initiate
41+
* Set the default query execution time threshold which must be exceeded to initiate
4142
* query plan collection.
4243
*/
4344
public void thresholdMicros(long thresholdMicros) {
44-
this.thresholdMicros = thresholdMicros;
45+
this.defaultThresholdMicros = thresholdMicros;
4546
}
4647

4748
/**
4849
* Return true if the query plan should be initiated based on it's hash.
4950
*/
5051
public boolean includeHash(String hash) {
51-
return all || hashes.contains(hash);
52+
return all || hashes.containsKey(hash);
5253
}
5354

5455
/**
5556
* Return the specific hashes that we want to collect query plans on.
57+
*
58+
* @param hash The hash of the query plan.
59+
* @param thresholdMicros The threshold in micros to use.
60+
*/
61+
public void add(String hash, long thresholdMicros) {
62+
requireNonNull(hash);
63+
if (!"all".equals(hash)) {
64+
hashes.put(hash, thresholdMicros);
65+
} else {
66+
all = true;
67+
if (thresholdMicros > 0) {
68+
defaultThresholdMicros = thresholdMicros;
69+
}
70+
}
71+
}
72+
73+
/**
74+
* Remove a hash from this request.
5675
*/
57-
public Set<String> hashes() {
58-
return hashes;
76+
public void remove(String hash) {
77+
hashes.remove(hash);
5978
}
6079

6180
/**
62-
* Set the specific hashes that we want to collect query plans on.
81+
* Return the threshold in micros to use for the given hash.
6382
*/
64-
public void hashes(Set<String> hashes) {
65-
this.hashes = hashes;
83+
public long thresholdMicros(String hash) {
84+
long threshold = hashes.get(hash);
85+
return threshold < 1 ? defaultThresholdMicros : threshold;
86+
}
87+
88+
/**
89+
* Return true if there are no registered hashes and not collect <em>All</em> plans.
90+
*/
91+
public boolean isEmpty() {
92+
return !all && hashes.isEmpty();
93+
}
94+
95+
@Override
96+
public String toString() {
97+
return "QueryPlanInit{" +
98+
"all=" + all +
99+
", hashes=" + hashes +
100+
", thresholdMicros=" + defaultThresholdMicros +
101+
'}';
66102
}
67103
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.ebean.meta;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
class QueryPlanInitTest {
8+
9+
@Test
10+
void initialQueryPlanInit() {
11+
var init = new QueryPlanInit();
12+
assertThat(init.isEmpty()).isTrue();
13+
assertThat(init.isAll()).isFalse();
14+
assertThat(init.thresholdMicros()).isEqualTo(0L);
15+
}
16+
17+
@Test
18+
void add_all() {
19+
var init = new QueryPlanInit();
20+
init.add("all", 57L);
21+
22+
assertThat(init.isEmpty()).isFalse();
23+
assertThat(init.isAll()).isTrue();
24+
assertThat(init.thresholdMicros()).isEqualTo(57L);
25+
}
26+
27+
@Test
28+
void addWithThresholds() {
29+
var init = new QueryPlanInit();
30+
init.thresholdMicros(1000);
31+
init.add("xOne", 0);
32+
init.add("xTwo", 2000L);
33+
34+
assertThat(init.isEmpty()).isFalse();
35+
assertThat(init.isAll()).isFalse();
36+
assertThat(init.includeHash("xJunk")).isFalse();
37+
assertThat(init.includeHash("xOne")).isTrue();
38+
assertThat(init.includeHash("xTwo")).isTrue();
39+
assertThat(init.thresholdMicros("xOne")).isEqualTo(1000L);
40+
assertThat(init.thresholdMicros("xTwo")).isEqualTo(2000L);
41+
}
42+
}

ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@ public String convertOrmUpdateToSql(String ormUpdateStatement) {
14551455
void queryPlanInit(QueryPlanInit request, List<MetaQueryPlan> list) {
14561456
for (CQueryPlan queryPlan : queryPlanCache.values()) {
14571457
if (request.includeHash(queryPlan.hash())) {
1458-
queryPlan.queryPlanInit(request.thresholdMicros());
1458+
queryPlan.queryPlanInit(request.thresholdMicros(queryPlan.hash()));
14591459
list.add(queryPlan.createMeta(null, null));
14601460
}
14611461
}

0 commit comments

Comments
 (0)