Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.pinot.common.datatable;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Preconditions;
import java.io.DataInput;
Expand All @@ -26,8 +27,10 @@
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nullable;
import org.apache.pinot.spi.utils.JsonUtils;
Expand Down Expand Up @@ -149,6 +152,24 @@ public StatMap<K> merge(K key, String value) {
return this;
}

public Set<String> getStringSet(K key) {
Preconditions.checkArgument(key.getType() == Type.STRING_SET, "Key %s is of type %s, not STRING_SET", key,
key.getType());
Object o = _map.get(key);
return o == null ? Set.of() : (Set<String>) o;
}

public StatMap<K> merge(K key, Set<String> value) {
Set<String> oldValue = getStringSet(key);
Set<String> newValue = key.merge(oldValue, value);
if (newValue.isEmpty()) {
_map.remove(key);
} else {
_map.put(key, Collections.unmodifiableSet(new LinkedHashSet<>(newValue)));
}
return this;
}

/**
* Returns the value associated with the key.
* <p>
Expand All @@ -164,6 +185,8 @@ public Object getAny(K key) {
return getLong(key);
case STRING:
return getString(key);
case STRING_SET:
return getStringSet(key);
default:
throw new IllegalArgumentException("Unsupported type: " + key.getType());
}
Expand Down Expand Up @@ -216,6 +239,9 @@ public StatMap<K> merge(StatMap<K> other) {
case STRING:
merge(key, (String) value);
break;
case STRING_SET:
merge(key, (Set<String>) value);
break;
default:
throw new IllegalArgumentException("Unsupported type: " + key.getType());
}
Expand Down Expand Up @@ -261,6 +287,14 @@ public StatMap<K> merge(DataInput input)
case STRING:
merge(key, input.readUTF());
break;
case STRING_SET:
int size = input.readInt();
LinkedHashSet<String> values = new LinkedHashSet<>(size);
for (int j = 0; j < size; j++) {
values.add(input.readUTF());
}
merge(key, values);
break;
default:
throw new IllegalStateException("Unknown type " + key.getType());
}
Expand Down Expand Up @@ -311,6 +345,18 @@ public ObjectNode asJson() {
node.put(key.getStatName(), (String) value);
}
break;
case STRING_SET:
if (value == null) {
if (key.includeDefaultInJson()) {
node.putArray(key.getStatName());
}
} else {
ArrayNode arrayNode = node.putArray(key.getStatName());
for (String stringValue : (Set<String>) value) {
arrayNode.add(stringValue);
}
}
break;
default:
throw new IllegalArgumentException("Unsupported type: " + key.getType());
}
Expand Down Expand Up @@ -366,6 +412,18 @@ public void serialize(DataOutput output)
}
break;
}
case STRING_SET: {
Set<String> value = getStringSet(key);
if (!value.isEmpty()) {
writtenKeys++;
output.writeByte(ordinal);
output.writeInt(value.size());
for (String stringValue : value) {
output.writeUTF(stringValue);
}
}
break;
}
default:
throw new IllegalStateException("Unknown type " + key.getType());
}
Expand Down Expand Up @@ -398,6 +456,12 @@ private boolean checkContainsNoDefault() {
throw new IllegalStateException("String value must be non-null but null is stored for key " + key);
}
break;
case STRING_SET:
if (value == null || ((Set<String>) value).isEmpty()) {
throw new IllegalStateException("String set value must be non-empty but " + value + " is stored for key "
+ key);
}
break;
default:
throw new IllegalArgumentException("Unsupported type: " + key.getType());
}
Expand Down Expand Up @@ -493,6 +557,12 @@ default String merge(@Nullable String value1, @Nullable String value2) {
return value2 != null ? value2 : value1;
}

default Set<String> merge(Set<String> value1, Set<String> value2) {
LinkedHashSet<String> merged = new LinkedHashSet<>(value1);
merged.addAll(value2);
return merged;
}

/**
* The type of the values associated to this key.
*/
Expand Down Expand Up @@ -540,6 +610,7 @@ public enum Type {
BOOLEAN,
INT,
LONG,
STRING
STRING,
STRING_SET
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
*/
@JsonPropertyOrder({
"resultTable", "numRowsResultSet", "partialResult", "exceptions", "numGroupsLimitReached",
"numGroupsWarningLimitReached", "numGroups", "maxRowsInJoinReached", "maxRowsInJoin",
"maxRowsInWindowReached", "maxRowsInWindow", "timeUsedMs", "stageStats",
"numGroupsWarningLimitReached", "numGroups", "earlyTerminationReasons", "maxRowsInJoinReached",
"maxRowsInJoin", "maxRowsInWindowReached", "maxRowsInWindow", "timeUsedMs", "stageStats",
"maxRowsInOperator", "requestId", "clientRequestId", "brokerId", "numDocsScanned", "totalDocs",
"numEntriesScannedInFilter", "numEntriesScannedPostFilter", "numServersQueried", "numServersResponded",
"numSegmentsQueried", "numSegmentsProcessed", "numSegmentsMatched", "numConsumingSegmentsQueried",
Expand Down Expand Up @@ -112,7 +112,8 @@ public int getNumRowsResultSet() {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@Override
public boolean isPartialResult() {
return getExceptionsSize() > 0 || isNumGroupsLimitReached() || isMaxRowsInJoinReached();
return getExceptionsSize() > 0 || isNumGroupsLimitReached() || !getEarlyTerminationReasons().isEmpty()
|| isMaxRowsInJoinReached();
}

@Override
Expand Down Expand Up @@ -163,6 +164,11 @@ public void mergeNumGroupsWarningLimitReached(boolean numGroupsWarningLimitReach
_brokerStats.merge(StatKey.NUM_GROUPS_WARNING_LIMIT_REACHED, numGroupsWarningLimitReached);
}

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public List<String> getEarlyTerminationReasons() {
return List.copyOf(_brokerStats.getStringSet(StatKey.EARLY_TERMINATION_REASONS));
}

@Override
public boolean isMaxRowsInJoinReached() {
return _maxRowsInJoinReached;
Expand Down Expand Up @@ -487,7 +493,8 @@ public long merge(long value1, long value2) {
public long merge(long value1, long value2) {
return Math.max(value1, value2);
}
};
},
EARLY_TERMINATION_REASONS(StatMap.Type.STRING_SET);

private final StatMap.Type _type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.apache.pinot.common.response.broker.BrokerResponseNativeV2;
import org.testng.Assert;
import org.testng.SkipException;
Expand Down Expand Up @@ -70,6 +73,15 @@ public void dynamicTypeCheckPutString(MyStats stat) {
statMap.merge(stat, "foo");
}

@Test(dataProvider = "allTypeStats", expectedExceptions = IllegalArgumentException.class)
public void dynamicTypeCheckPutStringSet(MyStats stat) {
if (stat.getType() == StatMap.Type.STRING_SET) {
throw new SkipException("Skipping STRING_SET test");
}
StatMap<MyStats> statMap = new StatMap<>(MyStats.class);
statMap.merge(stat, Set.of("foo"));
}

@Test(dataProvider = "allTypeStats")
public void singleEncodeDecode(MyStats stat)
throws IOException {
Expand All @@ -87,6 +99,9 @@ public void singleEncodeDecode(MyStats stat)
case STRING:
statMap.merge(stat, "foo");
break;
case STRING_SET:
statMap.merge(stat, Set.of("foo"));
break;
default:
throw new IllegalStateException();
}
Expand All @@ -111,13 +126,28 @@ public void encodeDecodeAll()
case STRING:
statMap.merge(stat, "foo");
break;
case STRING_SET:
statMap.merge(stat, Set.of("foo"));
break;
default:
throw new IllegalStateException();
}
}
testSerializeDeserialize(statMap);
}

@Test
public void stringSetPreservesOrderAndDeduplicates() {
StatMap<MyStats> statMap = new StatMap<>(MyStats.class)
.merge(MyStats.STR_SET_KEY, stringSet("foo", "bar"))
.merge(MyStats.STR_SET_KEY, stringSet("foo", "baz"));

Assert.assertEquals(List.copyOf(statMap.getStringSet(MyStats.STR_SET_KEY)), List.of("foo", "bar", "baz"));
Assert.assertEquals(statMap.asJson().get("strSetKey").get(0).asText(), "foo");
Assert.assertEquals(statMap.asJson().get("strSetKey").get(1).asText(), "bar");
Assert.assertEquals(statMap.asJson().get("strSetKey").get(2).asText(), "baz");
}

private <K extends Enum<K> & StatMap.Key> void testSerializeDeserialize(StatMap<K> statMap)
throws IOException {
ByteArrayDataOutput output = ByteStreams.newDataOutput();
Expand Down Expand Up @@ -153,22 +183,26 @@ static StatMap<?>[] complexStats() {
.merge(MyStats.BOOL_KEY, true)
.merge(MyStats.LONG_KEY, 1L)
.merge(MyStats.INT_KEY, 1)
.merge(MyStats.STR_KEY, "foo"),
.merge(MyStats.STR_KEY, "foo")
.merge(MyStats.STR_SET_KEY, stringSet("foo", "bar")),
new StatMap<>(MyStats.class)
.merge(MyStats.BOOL_KEY, false)
.merge(MyStats.LONG_KEY, 1L)
.merge(MyStats.INT_KEY, 1)
.merge(MyStats.STR_KEY, "foo"),
.merge(MyStats.STR_KEY, "foo")
.merge(MyStats.STR_SET_KEY, stringSet("foo", "bar")),
new StatMap<>(MyStats.class)
.merge(MyStats.BOOL_KEY, true)
.merge(MyStats.LONG_KEY, 0L)
.merge(MyStats.INT_KEY, 1)
.merge(MyStats.STR_KEY, "foo"),
.merge(MyStats.STR_KEY, "foo")
.merge(MyStats.STR_SET_KEY, stringSet("foo", "bar")),
new StatMap<>(MyStats.class)
.merge(MyStats.BOOL_KEY, false)
.merge(MyStats.LONG_KEY, 1L)
.merge(MyStats.INT_KEY, 0)
.merge(MyStats.STR_KEY, "foo"),
.merge(MyStats.STR_KEY, "foo")
.merge(MyStats.STR_SET_KEY, stringSet("foo", "bar")),
new StatMap<>(MyStats.class)
.merge(MyStats.BOOL_KEY, false)
.merge(MyStats.LONG_KEY, 1L)
Expand All @@ -188,11 +222,16 @@ static MyStats[] allTypeStats() {
return MyStats.values();
}

private static Set<String> stringSet(String... values) {
return new LinkedHashSet<>(List.of(values));
}

public enum MyStats implements StatMap.Key {
BOOL_KEY(StatMap.Type.BOOLEAN),
LONG_KEY(StatMap.Type.LONG),
INT_KEY(StatMap.Type.INT),
STR_KEY(StatMap.Type.STRING);
STR_KEY(StatMap.Type.STRING),
STR_SET_KEY(StatMap.Type.STRING_SET);

private final StatMap.Type _type;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pinot.common.response.broker;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.apache.pinot.common.datatable.StatMap;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;


public class BrokerResponseNativeV2Test {
@Test
public void testEarlyTerminationReasonsMarkPartialResponse() {
BrokerResponseNativeV2 brokerResponse = new BrokerResponseNativeV2();
assertTrue(brokerResponse.getEarlyTerminationReasons().isEmpty());
assertFalse(brokerResponse.isPartialResult());

brokerResponse.addBrokerStats(new StatMap<>(BrokerResponseNativeV2.StatKey.class)
.merge(BrokerResponseNativeV2.StatKey.EARLY_TERMINATION_REASONS,
stringSet("DISTINCT_MAX_ROWS", "DISTINCT_MAX_ROWS_WITHOUT_CHANGE"))
.merge(BrokerResponseNativeV2.StatKey.EARLY_TERMINATION_REASONS,
stringSet("DISTINCT_MAX_ROWS", "DISTINCT_MAX_EXECUTION_TIME")));

assertEquals(brokerResponse.getEarlyTerminationReasons(),
List.of("DISTINCT_MAX_ROWS", "DISTINCT_MAX_ROWS_WITHOUT_CHANGE", "DISTINCT_MAX_EXECUTION_TIME"));
assertTrue(brokerResponse.isPartialResult());
}

private static Set<String> stringSet(String... values) {
return new LinkedHashSet<>(List.of(values));
}
}
Loading
Loading