Skip to content

Commit dec8c0e

Browse files
cyriltovenagouthamve
authored andcommitted
Finishing touches
fix lint issue Signed-off-by: Cyril Tovena <[email protected]> removes useless loop Signed-off-by: Cyril Tovena <[email protected]> This should be on v11 not v10. Signed-off-by: Cyril Tovena <[email protected]> s/metricConstRangeKeyV1/labelNamesRangeKeyV1/ The code was first written to store the entire series, but now changed to do just labelNames. Signed-off-by: Goutham Veeramachaneni <[email protected]> Add note about v11 being experimental. Signed-off-by: Goutham Veeramachaneni <[email protected]>
1 parent 6d08e49 commit dec8c0e

File tree

5 files changed

+59
-28
lines changed

5 files changed

+59
-28
lines changed

docs/architecture.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,4 @@ The interface works somewhat differently across the supported databases:
145145

146146
A set of schemas are used to map the matchers and label sets used on reads and writes to the chunk store into appropriate operations on the index. Schemas have been added as Cortex has evolved, mainly in an attempt to better load balance writes and improve query performance.
147147

148-
> The current schema recommendation is the **v10 schema**.
148+
> The current schema recommendation is the **v10 schema**. v11 schema is an experimental schema.

docs/single-process-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ schema:
5454
- from: 2019-07-29
5555
store: boltdb
5656
object_store: filesystem
57-
schema: v11
57+
schema: v10
5858
index:
5959
prefix: index_
6060
period: 168h

pkg/chunk/inmemory_storage_client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (m *MockStorage) BatchWrite(ctx context.Context, batch WriteBatch) error {
165165
itemComponents := decodeRangeKey(items[i].rangeValue)
166166
if !bytes.Equal(itemComponents[3], metricNameRangeKeyV1) &&
167167
!bytes.Equal(itemComponents[3], seriesRangeKeyV1) &&
168-
!bytes.Equal(itemComponents[3], metricConstRangeKeyV1) &&
168+
!bytes.Equal(itemComponents[3], labelNamesRangeKeyV1) &&
169169
!bytes.Equal(itemComponents[3], labelSeriesRangeKeyV1) {
170170
return fmt.Errorf("Dupe write")
171171
}

pkg/chunk/schema.go

+53-20
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var (
2424
seriesRangeKeyV1 = []byte{'7'}
2525
labelSeriesRangeKeyV1 = []byte{'8'}
2626
// For v11 schema
27-
metricConstRangeKeyV1 = []byte{'9'}
27+
labelNamesRangeKeyV1 = []byte{'9'}
2828

2929
// ErrNotSupported when a schema doesn't support that particular lookup.
3030
ErrNotSupported = errors.New("not supported")
@@ -48,7 +48,7 @@ type Schema interface {
4848

4949
// If the query resulted in series IDs, use this method to find chunks.
5050
GetChunksForSeries(from, through model.Time, userID string, seriesID []byte) ([]IndexQuery, error)
51-
// Returns queries to retrieve all labels of multiple series by id.
51+
// Returns queries to retrieve all label names of multiple series by id.
5252
GetLabelNamesForSeries(from, through model.Time, userID string, seriesID []byte) ([]IndexQuery, error)
5353
}
5454

@@ -687,31 +687,13 @@ func (s v10Entries) GetLabelWriteEntries(bucket Bucket, metricName string, label
687687
// read first 32 bits of the hash and use this to calculate the shard
688688
shard := binary.BigEndian.Uint32(seriesID) % s.rowShards
689689

690-
labelNames := make([]string, 0, len(labels))
691-
for _, l := range labels {
692-
if l.Name == model.MetricNameLabel {
693-
continue
694-
}
695-
labelNames = append(labelNames, l.Name)
696-
}
697-
data, err := jsoniter.ConfigFastest.Marshal(labelNames)
698-
if err != nil {
699-
return nil, err
700-
}
701690
entries := []IndexEntry{
702691
// Entry for metricName -> seriesID
703692
{
704693
TableName: bucket.tableName,
705694
HashValue: fmt.Sprintf("%02d:%s:%s", shard, bucket.hashKey, metricName),
706695
RangeValue: encodeRangeKey(seriesID, nil, nil, seriesRangeKeyV1),
707696
},
708-
// Entry for seriesID -> labels
709-
{
710-
TableName: bucket.tableName,
711-
HashValue: string(seriesID),
712-
RangeValue: encodeRangeKey(nil, nil, nil, metricConstRangeKeyV1),
713-
Value: data,
714-
},
715697
}
716698

717699
// Entries for metricName:labelName -> hash(value):seriesID
@@ -804,6 +786,57 @@ type v11Entries struct {
804786
v10Entries
805787
}
806788

789+
func (s v11Entries) GetLabelWriteEntries(bucket Bucket, metricName string, labels labels.Labels, chunkID string) ([]IndexEntry, error) {
790+
seriesID := labelsSeriesID(labels)
791+
792+
// read first 32 bits of the hash and use this to calculate the shard
793+
shard := binary.BigEndian.Uint32(seriesID) % s.rowShards
794+
795+
labelNames := make([]string, 0, len(labels))
796+
for _, l := range labels {
797+
if l.Name == model.MetricNameLabel {
798+
continue
799+
}
800+
labelNames = append(labelNames, l.Name)
801+
}
802+
data, err := jsoniter.ConfigFastest.Marshal(labelNames)
803+
if err != nil {
804+
return nil, err
805+
}
806+
entries := []IndexEntry{
807+
// Entry for metricName -> seriesID
808+
{
809+
TableName: bucket.tableName,
810+
HashValue: fmt.Sprintf("%02d:%s:%s", shard, bucket.hashKey, metricName),
811+
RangeValue: encodeRangeKey(seriesID, nil, nil, seriesRangeKeyV1),
812+
},
813+
// Entry for seriesID -> label names
814+
{
815+
TableName: bucket.tableName,
816+
HashValue: string(seriesID),
817+
RangeValue: encodeRangeKey(nil, nil, nil, labelNamesRangeKeyV1),
818+
Value: data,
819+
},
820+
}
821+
822+
// Entries for metricName:labelName -> hash(value):seriesID
823+
// We use a hash of the value to limit its length.
824+
for _, v := range labels {
825+
if v.Name == model.MetricNameLabel {
826+
continue
827+
}
828+
valueHash := sha256bytes(v.Value)
829+
entries = append(entries, IndexEntry{
830+
TableName: bucket.tableName,
831+
HashValue: fmt.Sprintf("%02d:%s:%s:%s", shard, bucket.hashKey, metricName, v.Name),
832+
RangeValue: encodeRangeKey(valueHash, seriesID, nil, labelSeriesRangeKeyV1),
833+
Value: []byte(v.Value),
834+
})
835+
}
836+
837+
return entries, nil
838+
}
839+
807840
func (v11Entries) GetLabelNamesForSeries(bucket Bucket, seriesID []byte) ([]IndexQuery, error) {
808841
return []IndexQuery{
809842
{

pkg/chunk/series_store.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ func (c *seriesStore) lookupLabelNamesBySeries(ctx context.Context, from, throug
406406
return nil, err
407407
}
408408
level.Debug(log).Log("entries", len(entries))
409-
uniqueLabelNames := map[string]struct{}{model.MetricNameLabel: struct{}{}}
409+
result := []string{model.MetricNameLabel}
410+
uniqueLabelNames := map[string]struct{}{model.MetricNameLabel: {}}
410411
for _, entry := range entries {
411412
lbs := []string{}
412413
err := jsoniter.ConfigFastest.Unmarshal(entry.Value, &lbs)
@@ -416,13 +417,10 @@ func (c *seriesStore) lookupLabelNamesBySeries(ctx context.Context, from, throug
416417
for _, l := range lbs {
417418
if _, ok := uniqueLabelNames[l]; !ok {
418419
uniqueLabelNames[l] = struct{}{}
420+
result = append(result, l)
419421
}
420422
}
421423
}
422-
result := make([]string, 0, len(uniqueLabelNames))
423-
for name := range uniqueLabelNames {
424-
result = append(result, name)
425-
}
426424
sort.Strings(result)
427425
return result, nil
428426
}

0 commit comments

Comments
 (0)