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
7 changes: 7 additions & 0 deletions changelog/unreleased/SOLR-18239-fix-npe-size-estimator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
title: Fixed NPE in size estimator for null valued fields
type: fixed
authors:
- name: Jalaz Kumar
links:
- name: SOLR-18239
url: https://issues.apache.org/jira/browse/SOLR-18239
Comment thread
epugh marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ static long estimate(Object obj) {
}

private static long primitiveEstimate(Object obj, long def) {
if (obj == null) return def;
Class<?> clazz = obj.getClass();
if (clazz.isPrimitive()) {
return primitiveSizes.get(clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public AddUpdateCommand getUpdate(int sizeBytes) {

@Test
public void testEstimateObjectSize() {
assertEquals(estimate(null), 0);
assertEquals(estimate("abc"), 6);
assertEquals(estimate("abcdefgh"), 16);
List<String> keys = List.of("int", "long", "double", "float", "str");
Expand All @@ -99,7 +100,8 @@ public void testEstimateObjectSize() {
map.put("double", 12.0);
map.put("float", 5.0f);
map.put("str", "duck");
assertEquals(estimate(map), 50);
map.put("short", null);
assertEquals(estimate(map), 60);

SolrInputDocument document = new SolrInputDocument();
for (Map.Entry<String, Object> entry : map.entrySet()) {
Expand Down Expand Up @@ -134,7 +136,8 @@ public void testEstimateObjectSizeWithSingleChild() {
mapWChild.put("double", 12.0);
mapWChild.put("float", 5.0f);
mapWChild.put("str", "duck");
assertEquals(estimate(mapWChild), 50);
mapWChild.put("short", null);
assertEquals(estimate(mapWChild), 60);
Map<String, Object> childMap = new HashMap<>(mapWChild);

SolrInputDocument document = new SolrInputDocument();
Expand Down Expand Up @@ -175,7 +178,8 @@ public void testEstimateObjectSizeWithChildList() {
mapWChild.put("double", 12.0);
mapWChild.put("float", 5.0f);
mapWChild.put("str", "duck");
assertEquals(estimate(mapWChild), 50);
mapWChild.put("short", null);
assertEquals(estimate(mapWChild), 60);
Map<String, Object> childMap = new HashMap<>(mapWChild);

SolrInputDocument document = new SolrInputDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ static long estimate(Object obj) {
}

private static long primitiveEstimate(Object obj, long def) {
if (obj == null) return def;
Class<?> clazz = obj.getClass();
if (clazz.isPrimitive()) {
return primitiveSizes.get(clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.solr.crossdc.update.processor;

import static org.apache.solr.crossdc.update.processor.MirroringUpdateProcessor.ObjectSizeEstimator.estimate;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.doAnswer;
Expand All @@ -27,6 +28,8 @@

import io.opentelemetry.api.common.Attributes;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -570,6 +573,40 @@ public void testProcessDBQResults() throws Exception {
assertEquals(1, counters.get("submittedDeleteByQuery").get());
}

@Test
public void testEstimateObjectSize() {
assertEquals(estimate(null), 0);
assertEquals(estimate("abc"), 6);
assertEquals(estimate("abcdefgh"), 16);
List<String> keys = List.of("int", "long", "double", "float", "str");
assertEquals(estimate(keys), 42);
List<Object> values = List.of(12, 5L, 12.0, 5.0, "duck");
assertEquals(estimate(values), 8);

Map<String, Object> map = new HashMap<>();
map.put("int", 12);
map.put("long", 5L);
map.put("double", 12.0);
map.put("float", 5.0f);
map.put("str", "duck");
map.put("short", null);
assertEquals(estimate(map), 60);

SolrInputDocument document = new SolrInputDocument();
for (Map.Entry<String, Object> entry : map.entrySet()) {
document.addField(entry.getKey(), entry.getValue());
}
assertEquals(MirroringUpdateProcessor.ObjectSizeEstimator.estimate(document), estimate(map));

SolrInputDocument childDocument = new SolrInputDocument();
for (Map.Entry<String, Object> entry : map.entrySet()) {
childDocument.addField(entry.getKey(), entry.getValue());
}
document.addChildDocument(childDocument);
assertEquals(
MirroringUpdateProcessor.ObjectSizeEstimator.estimate(document), estimate(map) * 2);
}

@Test
public void testFinish() throws IOException {
processor.finish();
Expand Down
Loading