mrproliu commented on code in PR #1180: URL: https://github.com/apache/skywalking-banyandb/pull/1180#discussion_r3424928971
########## test/cases/lifecycle/orphan.go: ########## @@ -0,0 +1,495 @@ +// 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_test + +import ( + "bytes" + "compress/gzip" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "io/fs" + "os" + "path/filepath" + "strings" + "time" + + "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/protobuf/types/known/timestamppb" + + commonv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/common/v1" + databasev1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/database/v1" + measurev1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/measure/v1" + modelv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/model/v1" + streamv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/stream/v1" + "github.com/apache/skywalking-banyandb/banyand/backup/lifecycle" + "github.com/apache/skywalking-banyandb/pkg/grpchelper" + "github.com/apache/skywalking-banyandb/pkg/test/flags" +) + +// orphanRec mirrors one archived JSONL line (the subset this suite asserts on). +// The archive writer lives in banyand/backup/lifecycle (different package); this +// is a read-side view of its self-describing record. +type orphanRec struct { + Fields map[string]orphanVal `json:"fields"` + Tags map[string]orphanVal `json:"tags"` + Group string `json:"group"` + Catalog string `json:"catalog"` + Measure string `json:"measure"` + Timestamp string `json:"timestamp"` + ElementID string `json:"element_id"` + Entity []string `json:"entity"` + TimeNanos int64 `json:"timestamp_unix_nano"` + Version int64 `json:"version"` +} + +type orphanVal struct { + Value interface{} `json:"value"` + Type string `json:"type"` +} + +type orphanManifest struct { + Measures []struct { + Measure string `json:"measure"` + Rows int `json:"rows"` + } `json:"measures"` + TotalRows int `json:"total_rows"` +} + +// dayInterval/coprime grid clone of sw_cross_segment: a 2-day source segment +// straddles the 3-day warm boundary, so crossSegmentTimestamps() lands rows on +// the row-replay path — the only path where orphan archiving happens. +func orphanStages() *commonv1.ResourceOpts { + return &commonv1.ResourceOpts{ + ShardNum: 1, + SegmentInterval: &commonv1.IntervalRule{Unit: commonv1.IntervalRule_UNIT_DAY, Num: 2}, + Ttl: &commonv1.IntervalRule{Unit: commonv1.IntervalRule_UNIT_DAY, Num: 5}, + Stages: []*commonv1.LifecycleStage{{ + Name: "warm", + ShardNum: 1, + SegmentInterval: &commonv1.IntervalRule{Unit: commonv1.IntervalRule_UNIT_DAY, Num: 3}, + Ttl: &commonv1.IntervalRule{Unit: commonv1.IntervalRule_UNIT_DAY, Num: 10}, + NodeSelector: "type=warm", + }}, + } +} + +// drainWriteResult closes a client-streaming write and returns an error on any +// non-success ack or non-EOF stream error (the error-returning sibling of +// drainWriteAcks, so the whole write can be retried under Eventually until a +// freshly-created schema has propagated to the liaison). +func drainWriteResult[R interface{ GetStatus() string }](recv func() (R, error), closeSend func() error) error { + if err := closeSend(); err != nil { + return err + } + for { + resp, recvErr := recv() + if errors.Is(recvErr, io.EOF) { + return nil + } + if recvErr != nil { + return recvErr + } + if s := resp.GetStatus(); s != "" && s != "STATUS_SUCCEED" { + return fmt.Errorf("write ack status: %s", s) + } + } +} + +// runLifecycleMigrationWithArchive runs one real hot->warm lifecycle migration +// (the actual lifecycle command) with the orphan archive policy enabled. archiveSubdir +// is the relative subdir (under each catalog's root path) the archive lands in. +func runLifecycleMigrationWithArchive(progressFile, reportDir, archiveSubdir string) { + lifecycleCmd := lifecycle.NewCommand() + args := []string{ + "--grpc-addr", SharedContext.DataAddr, + "--stream-root-path", SharedContext.SrcDir, + "--measure-root-path", SharedContext.SrcDir, + "--trace-root-path", SharedContext.SrcDir, + "--progress-file", progressFile, + "--report-dir", reportDir, + "--migration-orphan-policy", "archive", + "--migration-orphan-archive-subdir", archiveSubdir, + } + args = append(args, SharedContext.MetadataFlags...) + lifecycleCmd.SetArgs(args) + gomega.Expect(lifecycleCmd.Execute()).To(gomega.Succeed()) +} + +// readOrphanArchive gunzips every part-*.jsonl.gz under groupArchiveDir (the +// <catalog-root>/<subdir>/<group> directory) and returns the decoded records plus +// the summed manifest row count. +func readOrphanArchive(groupArchiveDir string) (recs []orphanRec, manifestRows int) { + root := groupArchiveDir + err := filepath.WalkDir(root, func(path string, d fs.DirEntry, walkErr error) error { + if walkErr != nil { + return walkErr + } + if d.IsDir() { + return nil + } + switch { + case strings.HasSuffix(d.Name(), ".jsonl.gz"): + f, openErr := os.Open(path) + gomega.Expect(openErr).NotTo(gomega.HaveOccurred()) + defer f.Close() + gr, gzErr := gzip.NewReader(f) + gomega.Expect(gzErr).NotTo(gomega.HaveOccurred(), "archive file %s must be a valid gzip", path) + defer gr.Close() + data, readErr := io.ReadAll(gr) + gomega.Expect(readErr).NotTo(gomega.HaveOccurred()) + dec := json.NewDecoder(bytes.NewReader(data)) + for dec.More() { + var rec orphanRec + gomega.Expect(dec.Decode(&rec)).To(gomega.Succeed(), "archive line in %s must be valid JSON", path) + recs = append(recs, rec) + } Review Comment: Wrong judgment. `Decoder.More()` supports reading a stream of concatenated top-level JSON values. -- 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]
