Skip to content

Commit

Permalink
Refactor: change index key markers from slice to single byte (grafana…
Browse files Browse the repository at this point in the history
…#1786)

This is more idiomatic Go and more efficient because it doesn't have
to loop over the slice.

Signed-off-by: Bryan Boreham <[email protected]>
  • Loading branch information
bboreham authored Feb 4, 2020
1 parent 2dd8efd commit 14f8736
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 120 deletions.
9 changes: 5 additions & 4 deletions inmemory_storage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,11 @@ func (m *MockStorage) BatchWrite(ctx context.Context, batch WriteBatch) error {
} else {
// Return error if duplicate write and not metric name entry or series entry
itemComponents := decodeRangeKey(items[i].rangeValue)
if !bytes.Equal(itemComponents[3], metricNameRangeKeyV1) &&
!bytes.Equal(itemComponents[3], seriesRangeKeyV1) &&
!bytes.Equal(itemComponents[3], labelNamesRangeKeyV1) &&
!bytes.Equal(itemComponents[3], labelSeriesRangeKeyV1) {
keyType := itemComponents[3][0]
if keyType != metricNameRangeKeyV1 &&
keyType != seriesRangeKeyV1 &&
keyType != labelNamesRangeKeyV1 &&
keyType != labelSeriesRangeKeyV1 {
return fmt.Errorf("Dupe write")
}
}
Expand Down
80 changes: 41 additions & 39 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@ import (
"github.com/prometheus/prometheus/pkg/labels"
)

var (
chunkTimeRangeKeyV1a = []byte{1}
chunkTimeRangeKeyV1 = []byte{'1'}
chunkTimeRangeKeyV2 = []byte{'2'}
chunkTimeRangeKeyV3 = []byte{'3'}
chunkTimeRangeKeyV4 = []byte{'4'}
chunkTimeRangeKeyV5 = []byte{'5'}
metricNameRangeKeyV1 = []byte{'6'}
const (
chunkTimeRangeKeyV1a = 1
chunkTimeRangeKeyV1 = '1'
chunkTimeRangeKeyV2 = '2'
chunkTimeRangeKeyV3 = '3'
chunkTimeRangeKeyV4 = '4'
chunkTimeRangeKeyV5 = '5'
metricNameRangeKeyV1 = '6'

// For v9 schema
seriesRangeKeyV1 = []byte{'7'}
labelSeriesRangeKeyV1 = []byte{'8'}
seriesRangeKeyV1 = '7'
labelSeriesRangeKeyV1 = '8'
// For v11 schema
labelNamesRangeKeyV1 = []byte{'9'}
labelNamesRangeKeyV1 = '9'
)

var (
// ErrNotSupported when a schema doesn't support that particular lookup.
ErrNotSupported = errors.New("not supported")
)
Expand Down Expand Up @@ -247,7 +249,7 @@ func (originalEntries) GetWriteEntries(bucket Bucket, metricName string, labels
result = append(result, IndexEntry{
TableName: bucket.tableName,
HashValue: bucket.hashKey + ":" + metricName,
RangeValue: encodeRangeKey([]byte(v.Name), []byte(v.Value), chunkIDBytes),
RangeValue: rangeValuePrefix([]byte(v.Name), []byte(v.Value), chunkIDBytes),
})
}
return result, nil
Expand Down Expand Up @@ -275,7 +277,7 @@ func (originalEntries) GetReadMetricLabelQueries(bucket Bucket, metricName strin
{
TableName: bucket.tableName,
HashValue: bucket.hashKey + ":" + metricName,
RangeValuePrefix: encodeRangeKey([]byte(labelName)),
RangeValuePrefix: rangeValuePrefix([]byte(labelName)),
},
}, nil
}
Expand All @@ -288,7 +290,7 @@ func (originalEntries) GetReadMetricLabelValueQueries(bucket Bucket, metricName
{
TableName: bucket.tableName,
HashValue: bucket.hashKey + ":" + metricName,
RangeValuePrefix: encodeRangeKey([]byte(labelName), []byte(labelValue)),
RangeValuePrefix: rangeValuePrefix([]byte(labelName), []byte(labelValue)),
},
}, nil
}
Expand Down Expand Up @@ -320,7 +322,7 @@ func (base64Entries) GetWriteEntries(bucket Bucket, metricName string, labels la
result = append(result, IndexEntry{
TableName: bucket.tableName,
HashValue: bucket.hashKey + ":" + metricName,
RangeValue: encodeRangeKey([]byte(v.Name), encodedBytes, chunkIDBytes, chunkTimeRangeKeyV1),
RangeValue: encodeRangeKey(chunkTimeRangeKeyV1, []byte(v.Name), encodedBytes, chunkIDBytes),
})
}
return result, nil
Expand All @@ -339,7 +341,7 @@ func (base64Entries) GetReadMetricLabelValueQueries(bucket Bucket, metricName st
{
TableName: bucket.tableName,
HashValue: bucket.hashKey + ":" + metricName,
RangeValuePrefix: encodeRangeKey([]byte(labelName), encodedBytes),
RangeValuePrefix: rangeValuePrefix([]byte(labelName), encodedBytes),
},
}, nil
}
Expand All @@ -357,7 +359,7 @@ func (labelNameInHashKeyEntries) GetWriteEntries(bucket Bucket, metricName strin
{
TableName: bucket.tableName,
HashValue: bucket.hashKey + ":" + metricName,
RangeValue: encodeRangeKey(nil, nil, chunkIDBytes, chunkTimeRangeKeyV2),
RangeValue: encodeRangeKey(chunkTimeRangeKeyV2, nil, nil, chunkIDBytes),
},
}

Expand All @@ -369,7 +371,7 @@ func (labelNameInHashKeyEntries) GetWriteEntries(bucket Bucket, metricName strin
entries = append(entries, IndexEntry{
TableName: bucket.tableName,
HashValue: fmt.Sprintf("%s:%s:%s", bucket.hashKey, metricName, v.Name),
RangeValue: encodeRangeKey(nil, encodedBytes, chunkIDBytes, chunkTimeRangeKeyV1),
RangeValue: encodeRangeKey(chunkTimeRangeKeyV1, nil, encodedBytes, chunkIDBytes),
})
}

Expand Down Expand Up @@ -407,7 +409,7 @@ func (labelNameInHashKeyEntries) GetReadMetricLabelValueQueries(bucket Bucket, m
{
TableName: bucket.tableName,
HashValue: fmt.Sprintf("%s:%s:%s", bucket.hashKey, metricName, labelName),
RangeValuePrefix: encodeRangeKey(nil, encodedBytes),
RangeValuePrefix: rangeValuePrefix(nil, encodedBytes),
},
}, nil
}
Expand All @@ -433,7 +435,7 @@ func (v5Entries) GetWriteEntries(bucket Bucket, metricName string, labels labels
{
TableName: bucket.tableName,
HashValue: bucket.hashKey + ":" + metricName,
RangeValue: encodeRangeKey(encodedThroughBytes, nil, chunkIDBytes, chunkTimeRangeKeyV3),
RangeValue: encodeRangeKey(chunkTimeRangeKeyV3, encodedThroughBytes, nil, chunkIDBytes),
},
}

Expand All @@ -445,7 +447,7 @@ func (v5Entries) GetWriteEntries(bucket Bucket, metricName string, labels labels
entries = append(entries, IndexEntry{
TableName: bucket.tableName,
HashValue: fmt.Sprintf("%s:%s:%s", bucket.hashKey, metricName, v.Name),
RangeValue: encodeRangeKey(encodedThroughBytes, encodedValueBytes, chunkIDBytes, chunkTimeRangeKeyV4),
RangeValue: encodeRangeKey(chunkTimeRangeKeyV4, encodedThroughBytes, encodedValueBytes, chunkIDBytes),
})
}

Expand Down Expand Up @@ -506,7 +508,7 @@ func (v6Entries) GetWriteEntries(bucket Bucket, metricName string, labels labels
{
TableName: bucket.tableName,
HashValue: bucket.hashKey + ":" + metricName,
RangeValue: encodeRangeKey(encodedThroughBytes, nil, chunkIDBytes, chunkTimeRangeKeyV3),
RangeValue: encodeRangeKey(chunkTimeRangeKeyV3, encodedThroughBytes, nil, chunkIDBytes),
},
}

Expand All @@ -517,7 +519,7 @@ func (v6Entries) GetWriteEntries(bucket Bucket, metricName string, labels labels
entries = append(entries, IndexEntry{
TableName: bucket.tableName,
HashValue: fmt.Sprintf("%s:%s:%s", bucket.hashKey, metricName, v.Name),
RangeValue: encodeRangeKey(encodedThroughBytes, nil, chunkIDBytes, chunkTimeRangeKeyV5),
RangeValue: encodeRangeKey(chunkTimeRangeKeyV5, encodedThroughBytes, nil, chunkIDBytes),
Value: []byte(v.Value),
})
}
Expand All @@ -538,7 +540,7 @@ func (v6Entries) GetReadMetricQueries(bucket Bucket, metricName string) ([]Index
{
TableName: bucket.tableName,
HashValue: bucket.hashKey + ":" + metricName,
RangeValueStart: encodeRangeKey(encodedFromBytes),
RangeValueStart: rangeValuePrefix(encodedFromBytes),
},
}, nil
}
Expand All @@ -549,7 +551,7 @@ func (v6Entries) GetReadMetricLabelQueries(bucket Bucket, metricName string, lab
{
TableName: bucket.tableName,
HashValue: fmt.Sprintf("%s:%s:%s", bucket.hashKey, metricName, labelName),
RangeValueStart: encodeRangeKey(encodedFromBytes),
RangeValueStart: rangeValuePrefix(encodedFromBytes),
},
}, nil
}
Expand All @@ -560,7 +562,7 @@ func (v6Entries) GetReadMetricLabelValueQueries(bucket Bucket, metricName string
{
TableName: bucket.tableName,
HashValue: fmt.Sprintf("%s:%s:%s", bucket.hashKey, metricName, labelName),
RangeValueStart: encodeRangeKey(encodedFromBytes),
RangeValueStart: rangeValuePrefix(encodedFromBytes),
ValueEqual: []byte(labelValue),
},
}, nil
Expand Down Expand Up @@ -590,7 +592,7 @@ func (v9Entries) GetLabelWriteEntries(bucket Bucket, metricName string, labels l
{
TableName: bucket.tableName,
HashValue: bucket.hashKey + ":" + metricName,
RangeValue: encodeRangeKey(seriesID, nil, nil, seriesRangeKeyV1),
RangeValue: encodeRangeKey(seriesRangeKeyV1, seriesID, nil, nil),
},
}

Expand All @@ -604,7 +606,7 @@ func (v9Entries) GetLabelWriteEntries(bucket Bucket, metricName string, labels l
entries = append(entries, IndexEntry{
TableName: bucket.tableName,
HashValue: fmt.Sprintf("%s:%s:%s", bucket.hashKey, metricName, v.Name),
RangeValue: encodeRangeKey(valueHash, seriesID, nil, labelSeriesRangeKeyV1),
RangeValue: encodeRangeKey(labelSeriesRangeKeyV1, valueHash, seriesID, nil),
Value: []byte(v.Value),
})
}
Expand All @@ -621,7 +623,7 @@ func (v9Entries) GetChunkWriteEntries(bucket Bucket, metricName string, labels l
{
TableName: bucket.tableName,
HashValue: bucket.hashKey + ":" + string(seriesID),
RangeValue: encodeRangeKey(encodedThroughBytes, nil, []byte(chunkID), chunkTimeRangeKeyV3),
RangeValue: encodeRangeKey(chunkTimeRangeKeyV3, encodedThroughBytes, nil, []byte(chunkID)),
},
}

Expand Down Expand Up @@ -652,7 +654,7 @@ func (v9Entries) GetReadMetricLabelValueQueries(bucket Bucket, metricName string
{
TableName: bucket.tableName,
HashValue: fmt.Sprintf("%s:%s:%s", bucket.hashKey, metricName, labelName),
RangeValueStart: encodeRangeKey(valueHash),
RangeValueStart: rangeValuePrefix(valueHash),
ValueEqual: []byte(labelValue),
},
}, nil
Expand All @@ -664,7 +666,7 @@ func (v9Entries) GetChunksForSeries(bucket Bucket, seriesID []byte) ([]IndexQuer
{
TableName: bucket.tableName,
HashValue: bucket.hashKey + ":" + string(seriesID),
RangeValueStart: encodeRangeKey(encodedFromBytes),
RangeValueStart: rangeValuePrefix(encodedFromBytes),
},
}, nil
}
Expand Down Expand Up @@ -693,7 +695,7 @@ func (s v10Entries) GetLabelWriteEntries(bucket Bucket, metricName string, label
{
TableName: bucket.tableName,
HashValue: fmt.Sprintf("%02d:%s:%s", shard, bucket.hashKey, metricName),
RangeValue: encodeRangeKey(seriesID, nil, nil, seriesRangeKeyV1),
RangeValue: encodeRangeKey(seriesRangeKeyV1, seriesID, nil, nil),
},
}

Expand All @@ -707,7 +709,7 @@ func (s v10Entries) GetLabelWriteEntries(bucket Bucket, metricName string, label
entries = append(entries, IndexEntry{
TableName: bucket.tableName,
HashValue: fmt.Sprintf("%02d:%s:%s:%s", shard, bucket.hashKey, metricName, v.Name),
RangeValue: encodeRangeKey(valueHash, seriesID, nil, labelSeriesRangeKeyV1),
RangeValue: encodeRangeKey(labelSeriesRangeKeyV1, valueHash, seriesID, nil),
Value: []byte(v.Value),
})
}
Expand All @@ -724,7 +726,7 @@ func (v10Entries) GetChunkWriteEntries(bucket Bucket, metricName string, labels
{
TableName: bucket.tableName,
HashValue: bucket.hashKey + ":" + string(seriesID),
RangeValue: encodeRangeKey(encodedThroughBytes, nil, []byte(chunkID), chunkTimeRangeKeyV3),
RangeValue: encodeRangeKey(chunkTimeRangeKeyV3, encodedThroughBytes, nil, []byte(chunkID)),
},
}

Expand Down Expand Up @@ -760,7 +762,7 @@ func (s v10Entries) GetReadMetricLabelValueQueries(bucket Bucket, metricName str
result = append(result, IndexQuery{
TableName: bucket.tableName,
HashValue: fmt.Sprintf("%02d:%s:%s:%s", i, bucket.hashKey, metricName, labelName),
RangeValueStart: encodeRangeKey(valueHash),
RangeValueStart: rangeValuePrefix(valueHash),
ValueEqual: []byte(labelValue),
})
}
Expand All @@ -773,7 +775,7 @@ func (v10Entries) GetChunksForSeries(bucket Bucket, seriesID []byte) ([]IndexQue
{
TableName: bucket.tableName,
HashValue: bucket.hashKey + ":" + string(seriesID),
RangeValueStart: encodeRangeKey(encodedFromBytes),
RangeValueStart: rangeValuePrefix(encodedFromBytes),
},
}, nil
}
Expand Down Expand Up @@ -809,13 +811,13 @@ func (s v11Entries) GetLabelWriteEntries(bucket Bucket, metricName string, label
{
TableName: bucket.tableName,
HashValue: fmt.Sprintf("%02d:%s:%s", shard, bucket.hashKey, metricName),
RangeValue: encodeRangeKey(seriesID, nil, nil, seriesRangeKeyV1),
RangeValue: encodeRangeKey(seriesRangeKeyV1, seriesID, nil, nil),
},
// Entry for seriesID -> label names
{
TableName: bucket.tableName,
HashValue: string(seriesID),
RangeValue: encodeRangeKey(nil, nil, nil, labelNamesRangeKeyV1),
RangeValue: encodeRangeKey(labelNamesRangeKeyV1, nil, nil, nil),
Value: data,
},
}
Expand All @@ -830,7 +832,7 @@ func (s v11Entries) GetLabelWriteEntries(bucket Bucket, metricName string, label
entries = append(entries, IndexEntry{
TableName: bucket.tableName,
HashValue: fmt.Sprintf("%02d:%s:%s:%s", shard, bucket.hashKey, metricName, v.Name),
RangeValue: encodeRangeKey(valueHash, seriesID, nil, labelSeriesRangeKeyV1),
RangeValue: encodeRangeKey(labelSeriesRangeKeyV1, valueHash, seriesID, nil),
Value: []byte(v.Value),
})
}
Expand Down
39 changes: 20 additions & 19 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,32 +147,33 @@ func parseRangeValueType(rangeValue []byte) (int, error) {
return ChunkTimeRangeValue, nil

// chunk time range values
case bytes.Equal(components[3], chunkTimeRangeKeyV1):
return ChunkTimeRangeValue, nil
case len(components[3]) == 1:
switch components[3][0] {
case chunkTimeRangeKeyV1:
return ChunkTimeRangeValue, nil

case bytes.Equal(components[3], chunkTimeRangeKeyV2):
return ChunkTimeRangeValue, nil
case chunkTimeRangeKeyV2:
return ChunkTimeRangeValue, nil

case bytes.Equal(components[3], chunkTimeRangeKeyV3):
return ChunkTimeRangeValue, nil
case chunkTimeRangeKeyV3:
return ChunkTimeRangeValue, nil

case bytes.Equal(components[3], chunkTimeRangeKeyV4):
return ChunkTimeRangeValue, nil
case chunkTimeRangeKeyV4:
return ChunkTimeRangeValue, nil

case bytes.Equal(components[3], chunkTimeRangeKeyV5):
return ChunkTimeRangeValue, nil

// metric name range values
case bytes.Equal(components[3], metricNameRangeKeyV1):
return MetricNameRangeValue, nil
case chunkTimeRangeKeyV5:
return ChunkTimeRangeValue, nil

// series range values
case bytes.Equal(components[3], seriesRangeKeyV1):
return SeriesRangeValue, nil
// metric name range values
case metricNameRangeKeyV1:
return MetricNameRangeValue, nil

default:
return 0, fmt.Errorf("unrecognised range value type. version: %q", string(components[3]))
// series range values
case seriesRangeKeyV1:
return SeriesRangeValue, nil
}
}
return 0, fmt.Errorf("unrecognised range value type. version: %q", string(components[3]))
}

func TestSchemaRangeKey(t *testing.T) {
Expand Down
Loading

0 comments on commit 14f8736

Please sign in to comment.