Skip to content

Commit c5baee0

Browse files
rahulKQLigorbernstein2
authored andcommitted
chore: add filter size check on read row request (#166)
* chore: add filter size check on read row request Currently, bigtable could only serve read row request with filter size of 20MB only. So adding this check to stop server request. * adding unit test case * adding unit test for filter size
1 parent 8d69936 commit c5baee0

File tree

2 files changed

+33
-1
lines changed
  • google-cloud-bigtable/src
    • main/java/com/google/cloud/bigtable/data/v2/models
    • test/java/com/google/cloud/bigtable/data/v2/models

2 files changed

+33
-1
lines changed

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/Query.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.google.api.core.InternalApi;
1919
import com.google.bigtable.v2.ReadRowsRequest;
20+
import com.google.bigtable.v2.RowFilter;
2021
import com.google.bigtable.v2.RowRange;
2122
import com.google.bigtable.v2.RowSet;
2223
import com.google.cloud.bigtable.data.v2.internal.ByteStringComparator;
@@ -42,6 +43,9 @@
4243
public final class Query implements Serializable {
4344
private static final long serialVersionUID = -316972783499434755L;
4445

46+
// bigtable can server the largest filter size of 20MB.
47+
private static final int MAX_FILTER_SIZE = 20 * 1024 * 1024;
48+
4549
private final String tableId;
4650
private transient ReadRowsRequest.Builder builder = ReadRowsRequest.newBuilder();
4751

@@ -162,7 +166,13 @@ public Query range(ByteStringRange range) {
162166
* filters, please use {@link Filters#interleave()} or {@link Filters#chain()}.
163167
*/
164168
public Query filter(Filters.Filter filter) {
165-
builder.setFilter(filter.toProto());
169+
Preconditions.checkNotNull(filter, "filter can't be null");
170+
171+
RowFilter rowFilter = filter.toProto();
172+
Preconditions.checkArgument(
173+
rowFilter.getSerializedSize() < MAX_FILTER_SIZE, "filter size can't be more than 20MB");
174+
175+
builder.setFilter(rowFilter);
166176
return this;
167177
}
168178

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/QueryTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,28 @@ public void rowRangeTest() {
112112
assertThat(actualProto).isEqualTo(expectedProto.build());
113113
}
114114

115+
@Test
116+
public void filterTestWithExceptions() {
117+
Exception actualException = null;
118+
try {
119+
Query.create(TABLE_ID).filter(null);
120+
} catch (Exception ex) {
121+
actualException = ex;
122+
}
123+
assertThat(actualException).isInstanceOf(NullPointerException.class);
124+
125+
actualException = null;
126+
int maxFilterSize = 20 * 1024 * 1024;
127+
ByteString largeValue = ByteString.copyFrom(new byte[maxFilterSize + 1]);
128+
129+
try {
130+
Query.create(TABLE_ID).filter(FILTERS.value().exactMatch(largeValue));
131+
} catch (Exception ex) {
132+
actualException = ex;
133+
}
134+
assertThat(actualException).hasMessageThat().contains("filter size can't be more than 20MB");
135+
}
136+
115137
@Test
116138
public void filterTest() {
117139
Query query = Query.create(TABLE_ID).filter(FILTERS.key().regex(".*"));

0 commit comments

Comments
 (0)