Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pre compute changes with sorted numeric doc values #23

Open
wants to merge 8 commits into
base: gbh-pre-211
Choose a base branch
from
Open
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 @@ -39,7 +39,7 @@
import org.opensearch.common.Nullable;
import org.opensearch.common.collect.MapBuilder;
import org.opensearch.index.IndexSettings;
import org.opensearch.index.codec.freshstartree.codec.StarTreeCodec;
import org.opensearch.index.codec.startree.codec.StarTreeCodec;
import org.opensearch.index.mapper.MapperService;

import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
import org.apache.lucene.codecs.PostingsFormat;
import org.apache.lucene.codecs.lucene95.Lucene95Codec;
import org.opensearch.common.lucene.Lucene;
import org.opensearch.index.codec.freshstartree.codec.StarTreeCodec;
import org.opensearch.index.codec.freshstartree.codec.StarTreeDocValuesFormat;
import org.opensearch.index.codec.startree.codec.StarTreeCodec;
import org.opensearch.index.codec.startree.codec.StarTreeDocValuesFormat;
import org.opensearch.index.mapper.CompletionFieldMapper;
import org.opensearch.index.mapper.MappedFieldType;
import org.opensearch.index.mapper.MapperService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opensearch.index.codec.freshstartree.aggregator;
package org.opensearch.index.codec.startree.aggregator;

import java.util.Comparator;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opensearch.index.codec.freshstartree.aggregator;
package org.opensearch.index.codec.startree.aggregator;

/** Aggregated function type */
public enum AggregationFunctionType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opensearch.index.codec.freshstartree.aggregator;
package org.opensearch.index.codec.startree.aggregator;

/** Count value aggregator */
public class CountValueAggregator implements ValueAggregator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opensearch.index.codec.freshstartree.aggregator;
package org.opensearch.index.codec.startree.aggregator;

/** Data type of doc values */
public enum DataType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opensearch.index.codec.freshstartree.aggregator;
package org.opensearch.index.codec.startree.aggregator;

/** Sum value aggregator */
public class SumValueAggregator implements ValueAggregator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opensearch.index.codec.freshstartree.aggregator;
package org.opensearch.index.codec.startree.aggregator;

/**
* A value aggregator that pre-aggregates on the input values for a specific type of aggregation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opensearch.index.codec.freshstartree.aggregator;
package org.opensearch.index.codec.startree.aggregator;

/** Value aggregator factory for a given aggregation type */
public class ValueAggregatorFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
* limitations under the License.
*/
/** Aggregator classes for star tree */
package org.opensearch.index.codec.freshstartree.aggregator;
package org.opensearch.index.codec.startree.aggregator;
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,39 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opensearch.index.codec.freshstartree.builder;
package org.opensearch.index.codec.startree.builder;

import java.time.temporal.ChronoField;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.codecs.CodecUtil;
import org.apache.lucene.codecs.DocValuesConsumer;
import org.apache.lucene.codecs.DocValuesProducer;
import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.index.DocsWithFieldSet;
import org.apache.lucene.index.EmptyDocValuesProducer;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.IndexFileNames;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.index.SegmentWriteState;
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.index.VectorEncoding;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.Counter;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.packed.PackedInts;
import org.apache.lucene.util.packed.PackedLongValues;
import org.opensearch.common.time.DateUtils;
import org.opensearch.index.codec.freshstartree.aggregator.AggregationFunctionColumnPair;
import org.opensearch.index.codec.freshstartree.aggregator.AggregationFunctionType;
import org.opensearch.index.codec.freshstartree.aggregator.ValueAggregator;
import org.opensearch.index.codec.freshstartree.aggregator.ValueAggregatorFactory;
import org.opensearch.index.codec.freshstartree.codec.StarTreeAggregatedValues;
import org.opensearch.index.codec.freshstartree.node.StarTreeNode;
import org.opensearch.index.codec.freshstartree.util.BufferedAggregatedDocValues;
import org.opensearch.index.codec.startree.aggregator.AggregationFunctionColumnPair;
import org.opensearch.index.codec.startree.aggregator.AggregationFunctionType;
import org.opensearch.index.codec.startree.aggregator.ValueAggregator;
import org.opensearch.index.codec.startree.aggregator.ValueAggregatorFactory;
import org.opensearch.index.codec.startree.codec.SortedNumericDocValuesWriter;
import org.opensearch.index.codec.startree.codec.StarTreeAggregatedValues;
import org.opensearch.index.codec.startree.node.StarTreeNode;
import org.opensearch.index.codec.startree.util.BufferedAggregatedDocValues;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -97,12 +99,14 @@ public abstract class BaseSingleTreeBuilder {
indexOutput = state.directory.createOutput(docFileName, state.context);
CodecUtil.writeIndexHeader(indexOutput, "STARTreeCodec", 0, state.segmentInfo.getId(), state.segmentSuffix);
dimensionsSplitOrder = new ArrayList<>();

dimensionsSplitOrder.add("minute");
dimensionsSplitOrder.add("hour");
dimensionsSplitOrder.add("day");
dimensionsSplitOrder.add("month");
// dimensionsSplitOrder.add("year");
dimensionsSplitOrder.add("status");
dimensionsSplitOrder.add("elb_status");
dimensionsSplitOrder.add("target_status");
_numDimensions = dimensionsSplitOrder.size();
_dimensionsSplitOrder = new String[_numDimensions];
_skipStarNodeCreationForDimensions = new HashSet<>();
Expand All @@ -111,10 +115,13 @@ public abstract class BaseSingleTreeBuilder {
List<String> functionColumnPairList = new ArrayList<>();

// TODO : pass function column pair - Remove hardcoding
functionColumnPairList.add("SUM__status");
functionColumnPairList.add("SUM__elb_status");
functionColumnPairList.add("SUM__target_status");
functionColumnPairList.add("COUNT__target_status");
List<AggregationFunctionColumnPair> aggregationSpecs = new ArrayList<>();
aggregationSpecs.add(AggregationFunctionColumnPair.fromColumnName("SUM__status"));
// aggregationSpecs.add(AggregationFunctionColumnPair.fromColumnName("COUNT__status"));
aggregationSpecs.add(AggregationFunctionColumnPair.fromColumnName("SUM__elb_status"));
aggregationSpecs.add(AggregationFunctionColumnPair.fromColumnName("SUM__target_status"));
//aggregationSpecs.add(AggregationFunctionColumnPair.fromColumnName("COUNT__elb_status"));

_dimensionReaders = new SortedNumericDocValues[_numDimensions];
Set<String> skipStarNodeCreationForDimensions = new HashSet<>();
Expand Down Expand Up @@ -147,7 +154,7 @@ public abstract class BaseSingleTreeBuilder {
}

// TODO : Removing hardcoding
_maxLeafRecords = 10000; // builderConfig.getMaxLeafRecords();
_maxLeafRecords = 100; // builderConfig.getMaxLeafRecords();
}

private void constructStarTree(StarTreeBuilderUtils.TreeNode node, int startDocId, int endDocId) throws IOException {
Expand Down Expand Up @@ -253,12 +260,118 @@ public void build(Iterator<Record> recordIterator, boolean isMerge) throws IOExc
logger.info("Finished creating aggregated documents, got aggregated records : {}", numAggregatedRecords);

// Create doc values indices in disk
createDocValuesIndices(_docValuesConsumer);
createSortedDocValuesIndices(_docValuesConsumer);

// Serialize and save in disk
StarTreeBuilderUtils.serializeTree(indexOutput, _rootNode, _dimensionsSplitOrder, _numNodes);
}

private void createSortedDocValuesIndices(DocValuesConsumer docValuesConsumer) throws IOException {
List<SortedNumericDocValuesWriter> dimWriterList = new ArrayList<>();
List<SortedNumericDocValuesWriter> metricWriterList = new ArrayList<>();
FieldInfo[] dimFieldInfoArr = new FieldInfo[_dimensionReaders.length];
FieldInfo[] metricFieldInfoArr = new FieldInfo[_metricReaders.length];
int fieldNum = 0;
for (int i = 0; i < _dimensionReaders.length; i++) {
final FieldInfo fi = new FieldInfo(
_dimensionsSplitOrder[i] + "_dim",
fieldNum,
false,
false,
true,
IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS,
DocValuesType.SORTED_NUMERIC,
-1,
Collections.emptyMap(),
0,
0,
0,
0,
VectorEncoding.FLOAT32,
VectorSimilarityFunction.EUCLIDEAN,
false
);
dimFieldInfoArr[i] = fi;
final SortedNumericDocValuesWriter w = new SortedNumericDocValuesWriter(fi, Counter.newCounter());
dimWriterList.add(w);
fieldNum++;
}
for (int i = 0; i < _metricReaders.length; i++) {
FieldInfo fi = new FieldInfo(
_metrics[i] + "_metric",
fieldNum,
false,
false,
true,
IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS,
DocValuesType.SORTED_NUMERIC,
-1,
Collections.emptyMap(),
0,
0,
0,
0,
VectorEncoding.FLOAT32,
VectorSimilarityFunction.EUCLIDEAN,
false
);
final SortedNumericDocValuesWriter w = new SortedNumericDocValuesWriter(fi, Counter.newCounter());
metricWriterList.add(w);
metricFieldInfoArr[i] = fi;
fieldNum++;
}

for (int docId = 0; docId < _numDocs; docId++) {
Record record = getStarTreeRecord(docId);
for (int i = 0; i < record._dimensions.length; i++) {
long val = record._dimensions[i];
dimWriterList.get(i).addValue(docId, val);
}
for (int i = 0; i < record._metrics.length; i++) {
switch (_valueAggregators[i].getAggregatedValueType()) {
case LONG:
long val = (long) record._metrics[i];
metricWriterList.get(i).addValue(docId, val);
break;
// TODO: support this
case DOUBLE:
// double doubleval = (double) record._metrics[i];
// break;
case FLOAT:
case INT:
default:
throw new IllegalStateException("Unsupported value type");
}
}
}

for (int i = 0; i < _dimensionReaders.length; i++) {
final int finalI = i;
DocValuesProducer a1 = new EmptyDocValuesProducer() {
@Override
public SortedNumericDocValues getSortedNumeric(FieldInfo field) throws IOException {

return dimWriterList.get(finalI).getSortedNumericDocValues();
}
};
docValuesConsumer.addSortedNumericField(dimFieldInfoArr[i], a1);
}

for (int i = 0; i < _metricReaders.length; i++) {
final int finalI = i;
DocValuesProducer a1 = new EmptyDocValuesProducer() {
@Override
public SortedNumericDocValues getSortedNumeric(FieldInfo field) throws IOException {

return metricWriterList.get(finalI).getSortedNumericDocValues();
}
};
docValuesConsumer.addSortedNumericField(metricFieldInfoArr[i], a1);
}


}

private void createDocValuesIndices(DocValuesConsumer docValuesConsumer) throws IOException {
PackedLongValues.Builder[] pendingDimArr = new PackedLongValues.Builder[_dimensionReaders.length];
PackedLongValues.Builder[] pendingMetricArr = new PackedLongValues.Builder[_metricReaders.length];
Expand All @@ -276,7 +389,7 @@ private void createDocValuesIndices(DocValuesConsumer docValuesConsumer) throws
false,
true,
IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS,
DocValuesType.NUMERIC,
DocValuesType.SORTED_NUMERIC,
-1,
Collections.emptyMap(),
0,
Expand All @@ -299,7 +412,7 @@ private void createDocValuesIndices(DocValuesConsumer docValuesConsumer) throws
false,
true,
IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS,
DocValuesType.NUMERIC,
DocValuesType.SORTED_NUMERIC,
-1,
Collections.emptyMap(),
0,
Expand Down Expand Up @@ -343,24 +456,24 @@ private void createDocValuesIndices(DocValuesConsumer docValuesConsumer) throws
final int finalI = i;
DocValuesProducer a1 = new EmptyDocValuesProducer() {
@Override
public NumericDocValues getNumeric(FieldInfo field) throws IOException {
public SortedNumericDocValues getSortedNumeric(FieldInfo field) throws IOException {

return new BufferedAggregatedDocValues(pendingDimArr[finalI].build(), docsWithField.iterator());
return DocValues.singleton(new BufferedAggregatedDocValues(pendingDimArr[finalI].build(), docsWithField.iterator()));
}
};
docValuesConsumer.addNumericField(dimFieldInfoArr[i], a1);
docValuesConsumer.addSortedNumericField(dimFieldInfoArr[i], a1);
}

for (int i = 0; i < _metricReaders.length; i++) {
final int finalI = i;
DocValuesProducer a1 = new EmptyDocValuesProducer() {
@Override
public NumericDocValues getNumeric(FieldInfo field) throws IOException {
public SortedNumericDocValues getSortedNumeric(FieldInfo field) throws IOException {

return new BufferedAggregatedDocValues(pendingMetricArr[finalI].build(), docsWithField.iterator());
return DocValues.singleton(new BufferedAggregatedDocValues(pendingDimArr[finalI].build(), docsWithField.iterator()));
}
};
docValuesConsumer.addNumericField(metricFieldInfoArr[i], a1);
docValuesConsumer.addSortedNumericField(metricFieldInfoArr[i], a1);
}
}

Expand Down Expand Up @@ -560,7 +673,12 @@ Record getNextSegmentRecord() throws IOException {
for (int i = 0; i < _numMetrics; i++) {
// Ignore the column for COUNT aggregation function
if (_metricReaders[i] != null) {
_metricReaders[i].nextDoc();
try {
_metricReaders[i].nextDoc();
} catch (Exception e) {
// TODO : handle null values in columns
logger.info(e);
}
metrics[i] = _metricReaders[i].nextValue();
}
}
Expand Down Expand Up @@ -617,8 +735,13 @@ private long getTimeStampVal(final String fieldName, final long val) {
long[] getNextSegmentRecordDimensions() throws IOException {
long[] dimensions = new long[_numDimensions];
for (int i = 0; i < _numDimensions; i++) {
_dimensionReaders[i].nextDoc();
try {
_dimensionReaders[i].nextDoc();
} catch(Exception e) {
logger.info(e);
}
dimensions[i] = getTimeStampVal(_dimensionsSplitOrder[i], _dimensionReaders[i].nextValue());
//logger.info("Dim : {} , DimValue : {}", _dimensionsSplitOrder[i], _dimensionReaders[i].nextValue());
}
return dimensions;
}
Expand Down
Loading