Skip to content

Commit 1f9c436

Browse files
authored
fix: do not add system MID to fraction distribution (#256)
1 parent c305c8c commit 1f9c436

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

frac/active_sealing_source.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,12 @@ func (src *ActiveSealingSource) LastError() error {
147147
func (src *ActiveSealingSource) prepareInfo() {
148148
src.info.MetaOnDisk = 0
149149
src.info.SealingTime = uint64(src.created.UnixMilli())
150-
src.info.BuildDistribution(src.mids.vals)
150+
mids := src.mids.vals
151+
if len(mids) > 1 {
152+
// skip system MID
153+
mids = mids[1:]
154+
}
155+
src.info.BuildDistribution(mids)
151156
}
152157

153158
// Info returns index metadata information.

frac/fraction_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,33 @@ func (s *FractionTestSuite) TestSearchLargeFrac() {
10261026
s.AssertSearch(s.query("level:5", withLimit(100)), docs, level5Indexes[:100])
10271027
}
10281028

1029+
func (s *FractionTestSuite) TestMIDDistribution() {
1030+
now := time.Now().Truncate(time.Minute)
1031+
docs := []string{
1032+
fmt.Sprintf(`{"timestamp":%q,"message":"apple juice"}`, now.Add(-60*time.Minute).Format(time.RFC3339Nano)),
1033+
fmt.Sprintf(`{"timestamp":%q,"message":"orange juice"}`, now.Add(-61*time.Minute).Format(time.RFC3339Nano)),
1034+
fmt.Sprintf(`{"timestamp":%q,"message":"cider"}`, now.Add(-65*time.Minute).Format(time.RFC3339Nano)),
1035+
fmt.Sprintf(`{"timestamp":%q,"message":"red wine"}`, now.Add(-120*time.Minute).Format(time.RFC3339Nano)),
1036+
fmt.Sprintf(`{"timestamp":%q,"message":"coca cola"}`, now.Add(-360*time.Minute).Format(time.RFC3339Nano)),
1037+
}
1038+
1039+
s.insertDocuments(docs)
1040+
1041+
_, ok := s.fraction.(*Active)
1042+
if ok {
1043+
s.Require().Nil(s.fraction.Info().Distribution, "active fraction has MID distribution")
1044+
return
1045+
}
1046+
1047+
dist := s.fraction.Info().Distribution.GetDist()
1048+
s.Require().Equal(5, len(dist))
1049+
s.Require().Equal(now.Add(-360*time.Minute).UTC(), dist[0])
1050+
s.Require().Equal(now.Add(-120*time.Minute).UTC(), dist[1])
1051+
s.Require().Equal(now.Add(-65*time.Minute).UTC(), dist[2])
1052+
s.Require().Equal(now.Add(-61*time.Minute).UTC(), dist[3])
1053+
s.Require().Equal(now.Add(-60*time.Minute).UTC(), dist[4])
1054+
}
1055+
10291056
func (s *FractionTestSuite) TestFractionInfo() {
10301057
docs := []string{
10311058
`{"timestamp":"2000-01-01T13:00:25Z","service":"service_a","message":"first message some text", "container":"gateway"}`,

seq/seq.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/binary"
55
"encoding/hex"
66
"fmt"
7+
"math"
78
"time"
89

910
"github.com/ozontech/seq-db/util"
@@ -19,7 +20,12 @@ type RID uint64 // random part of ID
1920
type LID uint32 // local id for a fraction
2021

2122
func (m MID) Time() time.Time {
22-
return time.UnixMilli(int64(m))
23+
if uint64(m) <= math.MaxInt64 {
24+
return time.UnixMilli(int64(m))
25+
} else {
26+
// since MaxInt64 is 292278994 year in milliseconds, so we assume this MID is "infinite future"
27+
return time.UnixMilli(math.MaxInt64)
28+
}
2329
}
2430

2531
func (d ID) String() string {
@@ -105,7 +111,7 @@ func DurationToMID(d time.Duration) MID {
105111
}
106112

107113
func MIDToTime(t MID) time.Time {
108-
return time.Unix(0, 0).Add(MIDToDuration(t))
114+
return t.Time()
109115
}
110116

111117
func MIDToDuration(t MID) time.Duration {

seq/seq_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package seq
22

33
import (
4+
"math"
45
"testing"
6+
"time"
57

68
"github.com/stretchr/testify/assert"
79
)
@@ -10,3 +12,22 @@ func TestFromString(t *testing.T) {
1012
_, err := FromString("abaf05877b010000-2402dc02d60615cc")
1113
assert.NoError(t, err)
1214
}
15+
16+
func TestTimeToMIDConversion(t *testing.T) {
17+
timestampNow := time.Now()
18+
assert.EqualExportedValues(t, timestampNow, MID(timestampNow.UnixNano()).Time())
19+
20+
timestamp2 := MID(1763984556395).Time().UTC()
21+
assert.Equal(t, 2025, timestamp2.Year())
22+
assert.Equal(t, time.Month(11), timestamp2.Month())
23+
assert.Equal(t, 24, timestamp2.Day())
24+
assert.Equal(t, 11, timestamp2.Hour())
25+
assert.Equal(t, 42, timestamp2.Minute())
26+
assert.Equal(t, 36, timestamp2.Second())
27+
assert.Equal(t, 395000000, timestamp2.Nanosecond())
28+
29+
// check that we do not overflow on huge values
30+
maxMID := MID(math.MaxUint64)
31+
assert.Equal(t, 292278994, maxMID.Time().Year())
32+
assert.Equal(t, 292278994, MIDToTime(maxMID).Year())
33+
}

0 commit comments

Comments
 (0)