mrproliu commented on code in PR #1168: URL: https://github.com/apache/skywalking-banyandb/pull/1168#discussion_r3386325761
########## banyand/backup/lifecycle/migration_error_test.go: ########## @@ -0,0 +1,296 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package lifecycle + +import ( + "encoding/json" + "fmt" + "sync" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + commonv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/common/v1" + "github.com/apache/skywalking-banyandb/banyand/internal/storage" + "github.com/apache/skywalking-banyandb/pkg/logger" + "github.com/apache/skywalking-banyandb/pkg/timestamp" +) + +// TestFormatIntervalRule pins the compact interval rendering for the report's +// interval field across day and hour granularities. +func TestFormatIntervalRule(t *testing.T) { + assert.Equal(t, "5d", formatIntervalRule(storage.IntervalRule{Unit: storage.DAY, Num: 5})) + assert.Equal(t, "1d", formatIntervalRule(storage.IntervalRule{Unit: storage.DAY, Num: 1})) + assert.Equal(t, "1h", formatIntervalRule(storage.IntervalRule{Unit: storage.HOUR, Num: 1})) + assert.Equal(t, "12h", formatIntervalRule(storage.IntervalRule{Unit: storage.HOUR, Num: 12})) +} + +// TestSegmentErrorLocation pins the source segment directory name + interval +// derivation, the inverse of storage.ParseSegmentTime, for day and hour units. +func TestSegmentErrorLocation(t *testing.T) { + start := time.Date(2026, 6, 1, 0, 0, 0, 0, time.UTC) + dayTR := ×tamp.TimeRange{Start: start, End: start.Add(5 * 24 * time.Hour)} + seg, interval := segmentErrorLocation(dayTR, storage.IntervalRule{Unit: storage.DAY, Num: 5}) + assert.Equal(t, "seg-20260601", seg) + assert.Equal(t, "5d", interval) + + hourTR := ×tamp.TimeRange{Start: start, End: start.Add(time.Hour)} + seg, interval = segmentErrorLocation(hourTR, storage.IntervalRule{Unit: storage.HOUR, Num: 1}) + assert.Equal(t, "seg-2026060100", seg) + assert.Equal(t, "1h", interval) Review Comment: No need to change; it's the original design. ########## banyand/internal/storage/segment.go: ########## @@ -733,6 +727,28 @@ func parseSegmentTime(value string, unit IntervalUnit) (time.Time, error) { panic("invalid interval unit") } +// ParseSegmentTime parses a segment-directory suffix back into its start time +// for the given interval unit, the inverse of the suffix formatting used when a +// segment directory is named. Callers outside this package use it to map the +// suffixes returned by VisitSegmentsInTimeRange to the segment instants a +// visitor observed. +func ParseSegmentTime(suffix string, rule IntervalRule) (time.Time, error) { + return parseSegmentTime(suffix, rule.Unit) +} + +// FormatSegmentTime formats a segment start time into its directory suffix for +// the given interval rule, the inverse of ParseSegmentTime. The returned suffix +// excludes the "seg-" directory prefix. +func FormatSegmentTime(t time.Time, rule IntervalRule) string { + switch rule.Unit { + case HOUR: + return t.Format(hourFormat) + case DAY: + return t.Format(dayFormat) + } + panic("invalid interval unit") +} Review Comment: No need to change; it's the original design. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
