Skip to content

Commit

Permalink
fix(operations): fix get index sizes integer wrap (#9450)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanHolstien authored Dec 15, 2023
1 parent 0ea6145 commit 6a16935
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ public List<TimeseriesIndexSizeResult> getIndexSizes() {
elemResult.setEntityName(indexEntityAndAspect.get().getFirst());
elemResult.setAspectName(indexEntityAndAspect.get().getSecond());
}
int sizeBytes =
entry.getValue().get("primaries").get("store").get("size_in_bytes").asInt();
float sizeMb = (float) sizeBytes / 1000;
elemResult.setSizeMb(sizeMb);
long sizeBytes =
entry.getValue().get("primaries").get("store").get("size_in_bytes").asLong();
double sizeMb = (double) sizeBytes / 1000000;
elemResult.setSizeInMb(sizeMb);
res.add(elemResult);
});
return res;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.linkedin.metadata.timeseries.search;

import static org.mockito.Mockito.*;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.NumericNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.linkedin.metadata.models.registry.EntityRegistry;
import com.linkedin.metadata.search.elasticsearch.update.ESBulkProcessor;
import com.linkedin.metadata.timeseries.TimeseriesAspectService;
import com.linkedin.metadata.timeseries.elastic.ElasticSearchTimeseriesAspectService;
import com.linkedin.metadata.timeseries.elastic.indexbuilder.TimeseriesAspectIndexBuilders;
import com.linkedin.metadata.utils.elasticsearch.IndexConvention;
import com.linkedin.timeseries.TimeseriesIndexSizeResult;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.opensearch.client.Request;
import org.opensearch.client.Response;
import org.opensearch.client.RestClient;
import org.opensearch.client.RestHighLevelClient;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
* Test using mocks instead of integration for testing functionality not dependent on a real server
* response
*/
public class TimeseriesAspectServiceUnitTest {

private final RestHighLevelClient _searchClient = mock(RestHighLevelClient.class);
private final IndexConvention _indexConvention = mock(IndexConvention.class);
private final TimeseriesAspectIndexBuilders _timeseriesAspectIndexBuilders =
mock(TimeseriesAspectIndexBuilders.class);
private final EntityRegistry _entityRegistry = mock(EntityRegistry.class);
private final ESBulkProcessor _bulkProcessor = mock(ESBulkProcessor.class);
private final RestClient _restClient = mock(RestClient.class);
private final TimeseriesAspectService _timeseriesAspectService =
new ElasticSearchTimeseriesAspectService(
_searchClient,
_indexConvention,
_timeseriesAspectIndexBuilders,
_entityRegistry,
_bulkProcessor,
0);

private static final String INDEX_PATTERN = "indexPattern";

@Test
public void testGetIndicesIntegerWrap() throws IOException {
when(_indexConvention.getAllTimeseriesAspectIndicesPattern()).thenReturn(INDEX_PATTERN);
when(_searchClient.getLowLevelClient()).thenReturn(_restClient);
ObjectNode jsonNode = JsonNodeFactory.instance.objectNode();
ObjectNode indicesNode = JsonNodeFactory.instance.objectNode();
ObjectNode indexNode = JsonNodeFactory.instance.objectNode();
ObjectNode primariesNode = JsonNodeFactory.instance.objectNode();
ObjectNode storeNode = JsonNodeFactory.instance.objectNode();
NumericNode bytesNode = JsonNodeFactory.instance.numberNode(8078398031L);
storeNode.set("size_in_bytes", bytesNode);
primariesNode.set("store", storeNode);
indexNode.set("primaries", primariesNode);
indicesNode.set("someIndexName", indexNode);
jsonNode.set("indices", indicesNode);

Response response = mock(Response.class);
HttpEntity responseEntity = mock(HttpEntity.class);
when(response.getEntity()).thenReturn(responseEntity);
when(responseEntity.getContent())
.thenReturn(IOUtils.toInputStream(jsonNode.toString(), StandardCharsets.UTF_8));
when(_restClient.performRequest(any(Request.class))).thenReturn(response);

List<TimeseriesIndexSizeResult> results = _timeseriesAspectService.getIndexSizes();

Assert.assertEquals(results.get(0).getSizeInMb(), 8078.398031);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ record TimeseriesIndexSizeResult{
/**
* Size
*/
@deprecated = "use sizeInMb instead"
sizeMb: float = 0

sizeInMb: double = 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -3668,6 +3668,11 @@
"name" : "sizeMb",
"type" : "float",
"doc" : "Size",
"default" : 0.0,
"deprecated" : "use sizeInMb instead"
}, {
"name" : "sizeInMb",
"type" : "double",
"default" : 0.0
} ]
}, {
Expand Down

0 comments on commit 6a16935

Please sign in to comment.