Skip to content

Commit

Permalink
Use a Map<String, List<Integer> instead of Map<String, int[]> for sub…
Browse files Browse the repository at this point in the history
…plans to facilitate serialization and comparison
  • Loading branch information
billoley committed Sep 29, 2023
1 parent 6e7b695 commit 92d12dd
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package datawave.microservice.querymetric;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.protobuf.Internal;
import datawave.marking.MarkingFunctions;
import datawave.webservice.query.Query;
import datawave.webservice.query.QueryImpl.Parameter;
Expand Down Expand Up @@ -684,8 +683,8 @@ public int getFieldNumber(String name) {
protected Set<Prediction> predictions = new HashSet<>();

@XmlElement(name = "subplans")
@XmlJavaTypeAdapter(StringMapAdapter.class)
protected Map<String,int[]> subPlans = new HashMap<>();
@XmlJavaTypeAdapter(StringIntegerListMapAdapter.class)
protected Map<String,List<Integer>> subPlans = new HashMap<>();

public static final String DATAWAVE = "DATAWAVE";
protected static final Map<String,String> discoveredVersionMap = BaseQueryMetric.getVersionsFromClasspath();
Expand All @@ -696,15 +695,15 @@ public enum Lifecycle {
NONE, DEFINED, INITIALIZED, RESULTS, CLOSED, CANCELLED, MAXRESULTS, NEXTTIMEOUT, TIMEOUT, SHUTDOWN, MAXWORK
}

public void addSubPlan(String plan, int[] rangeCounts) {
public void addSubPlan(String plan, List<Integer> rangeCounts) {
subPlans.put(plan, rangeCounts);
}

public Map<String,int[]> getSubPlans() {
public Map<String,List<Integer>> getSubPlans() {
return subPlans;
}

public void setSubPlans(Map<String,int[]> subPlans) {
public void setSubPlans(Map<String,List<Integer>> subPlans) {
this.subPlans = subPlans;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlTransient;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
Expand Down Expand Up @@ -133,15 +134,15 @@ public String getMainContent() {
builder.append("<tr><th>Range</th><th>Sub Plan</th></tr>");
if (metric.getSubPlans() != null && !metric.getSubPlans().isEmpty()) {
int s = 0;
for (Map.Entry<String, int[]> e : metric.getSubPlans().entrySet()) {
for (Map.Entry<String, List<Integer>> e : metric.getSubPlans().entrySet()) {
// highlight alternating rows
if (s % 2 == 0) {
builder.append("<tr class=\"highlight\">");
} else {
builder.append("<tr>");
}
builder.append("<td>").append(e.getKey()).append("</td>");
builder.append("<td>").append(Arrays.toString(e.getValue())).append("</td>");
builder.append("<td>").append(e.getValue()).append("</td>");
builder.append("\n</tr>\n");
s++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

Expand Down Expand Up @@ -410,8 +411,8 @@ public void writeTo(Output output, QueryMetric message) throws IOException {
}

if (message.subPlans != null) {
for (Map.Entry<String,int[]> entry : message.subPlans.entrySet()) {
output.writeString(39, StringUtils.join(Arrays.asList(entry.getKey(), entry.getValue()), "\0"), true);
for (Map.Entry<String,List<Integer>> entry : message.subPlans.entrySet()) {
output.writeString(39, StringUtils.join(Arrays.asList(entry.getKey(), StringUtils.join(entry.getValue(), ",")), "\0"), true);
}
}

Expand Down Expand Up @@ -570,11 +571,10 @@ public void mergeFrom(Input input, QueryMetric message) throws IOException {
String encodedPlans = input.readString();
String[] splitPlans = StringUtils.split(encodedPlans, "\0");
if (splitPlans.length == 2) {
int[] rangeCounts = new int[2];
int index = 0;
for (String count : splitPlans[1].substring(1, splitPlans[1].length() - 1).split(", ")) {
rangeCounts[index] = Integer.parseInt(count);
index++;
List<Integer> rangeCounts = new ArrayList<>();
String[] rangeCountSplit = StringUtils.split(splitPlans[1], ",");
for (String count : rangeCountSplit) {
rangeCounts.add(Integer.parseInt(count));
}
message.subPlans.put(splitPlans[0], rangeCounts);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package datawave.microservice.querymetric;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlValue;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Provides JAX-B marshalling/unmarshalling of {@link Map} of String to List of Integer. This allows the marshalled type to be in our own namespace rather than
* in the "default" one, which when triggered will then assume the "" prefix and push all of our own elements into the "ns2" prefix.
*/
public class StringIntegerListMapAdapter extends XmlAdapter<StringIntegerListMapAdapter.StringIntegerListMap,Map<String,List<Integer>>> {

@Override
public Map<String,List<Integer>> unmarshal(StringIntegerListMap v) throws Exception {
HashMap<String,List<Integer>> map = new HashMap<>();
for (StringIntegerListMapEntry entry : v.entries) {
map.put(entry.key, entry.value);
}
return map;
}

@Override
public StringIntegerListMap marshal(Map<String,List<Integer>> v) throws Exception {
StringIntegerListMap map = new StringIntegerListMap();
for (Map.Entry<String,List<Integer>> entry : v.entrySet()) {
map.entries.add(new StringIntegerListMapEntry(entry.getKey(), entry.getValue()));
}
return map;
}

public static class StringIntegerListMap {
@XmlElement(name = "entry")
private List<StringIntegerListMapEntry> entries = new ArrayList<>();
}

public static class StringIntegerListMapEntry {
@XmlAttribute(name = "name")
private String key;
@XmlValue
private List<Integer> value;

public StringIntegerListMapEntry() {}

public StringIntegerListMapEntry(String key, List<Integer> value) {
this.key = key;
this.value = value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,12 @@ public Multimap<String,String> getEventFieldsToWrite(T updated, T stored) {
if (isChanged(updated.getSourceCount(), stored == null ? -1 : stored.getSourceCount())) {
fields.put("SOURCE_COUNT", Long.toString(updated.getSourceCount()));
}
Map<String,int[]> updatedSubPlans = updated.getSubPlans();
Map<String,List<Integer>> updatedSubPlans = updated.getSubPlans();
if (updatedSubPlans != null && !updatedSubPlans.isEmpty()) {
Map<String,int[]> storedSubPlans = stored == null ? null : stored.getSubPlans();
for (Map.Entry<String,int[]> entry : updatedSubPlans.entrySet()) {
Map<String,List<Integer>> storedSubPlans = stored == null ? null : stored.getSubPlans();
for (Map.Entry<String,List<Integer>> entry : updatedSubPlans.entrySet()) {
if (storedSubPlans == null || !storedSubPlans.containsKey(entry.getKey())) {
fields.put("SUBPLAN", entry.getKey() + " : " + Arrays.toString(entry.getValue()));
fields.put("SUBPLAN", entry.getKey() + " : " + StringUtils.join(entry.getValue(), ","));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public T toMetric(EventBase event) {
List<FieldBase> field = event.getFields();
m.setMarkings(event.getMarkings());
TreeMap<Long,PageMetric> pageMetrics = Maps.newTreeMap();
Map<String,int[]> subplans = new HashMap<>();
Map<String,List<Integer>> subplans = new HashMap<>();

boolean createDateSet = false;
for (FieldBase f : field) {
Expand Down Expand Up @@ -635,13 +635,12 @@ public T toMetric(EventBase event) {
} else if (fieldName.equals("SUBPLAN")) {
if (fieldValue != null) {
String[] arr = fieldValue.split(" : ", 2);
int[] rangeCounts = new int[2];
int index = 0;
for (String count : arr[1].substring(1, arr[1].length() - 1).split(", ")) {
rangeCounts[index] = Integer.parseInt(count);
index++;
}
if (arr.length >= 2) {
if (arr.length == 2) {
List<Integer> rangeCounts = new ArrayList<>();
String[] rangeCountSplit = StringUtils.split(arr[1], ",");
for (String count : rangeCountSplit) {
rangeCounts.add(Integer.parseInt(count));
}
subplans.put(arr[0], rangeCounts);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
Expand Down Expand Up @@ -88,8 +89,7 @@ public void PageMetricTest() throws Exception {
assertNoDuplicateFields(queryId);
}

public void updateRangeCounts(Map<String, Integer> shardCounts, Map<String, Integer> documentCounts,
String subplan, Range range) {
public void updateRangeCounts(Map<String,Integer> shardCounts, Map<String,Integer> documentCounts, String subplan, Range range) {
Key key = range.getStartKey();
String cf = key.getColumnFamily().toString();
if (cf.length() > 0) {
Expand All @@ -106,21 +106,21 @@ public void updateRangeCounts(Map<String, Integer> shardCounts, Map<String, Inte
}
}
}

@Test
public void SubPlanTest() throws Exception {
String queryId = createQueryId();
BaseQueryMetric m = createMetric(queryId);
Map<String, Integer> shardCounts = new HashMap<>();
Map<String, Integer> documentCounts = new HashMap<>();
Map<String,Integer> shardCounts = new HashMap<>();
Map<String,Integer> documentCounts = new HashMap<>();
List<String> plans = new ArrayList<>();
plans.add("F1 == 'value1' || F2 == 'value2'");
plans.add("F3 == 'value3' || F4 == 'value4'");
plans.add("F2 == 'value2' || F3 == 'value3'");
plans.add("F1 == 'value1' || F6 == 'value6'");
plans.add("F1 == 'value1' || F5 == 'value5'");
Random r = new Random(System.currentTimeMillis());

Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String date = sdf.format(now);
Expand All @@ -144,9 +144,9 @@ public void SubPlanTest() throws Exception {
for (String p : plans) {
Integer shardCount = shardCounts.getOrDefault(p, 0);
Integer documentCount = documentCounts.getOrDefault(p, 0);
m.addSubPlan(p, new int[] {shardCount, documentCount});
m.addSubPlan(p, Arrays.asList(shardCount, documentCount));
}

// @formatter:off
client.submit(new QueryMetricClient.Request.Builder()
.withMetric(m)
Expand Down

0 comments on commit 92d12dd

Please sign in to comment.