You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ha...@apache.org on 2025/03/04 12:14:56 UTC
(skywalking-banyandb) branch lm-api updated: Add nodes labels
This is an automated email from the ASF dual-hosted git repository.
hanahmily pushed a commit to branch lm-api
in repository https://gitbox.apache.org/repos/asf/skywalking-banyandb.git
The following commit(s) were added to refs/heads/lm-api by this push:
new fa8bd32f Add nodes labels
fa8bd32f is described below
commit fa8bd32f9aa9062a58785f567f674465d2915f68
Author: Gao Hongtao <ha...@gmail.com>
AuthorDate: Tue Mar 4 20:13:18 2025 +0800
Add nodes labels
Signed-off-by: Gao Hongtao <ha...@gmail.com>
---
CHANGES.md | 1 +
api/common/id.go | 17 ++
api/proto/banyandb/common/v1/common.proto | 9 +-
api/proto/banyandb/database/v1/database.proto | 6 +
api/proto/banyandb/measure/v1/query.proto | 2 +
api/proto/banyandb/property/v1/rpc.proto | 2 +
api/proto/banyandb/stream/v1/query.proto | 2 +
banyand/internal/storage/rotation_test.go | 89 ++++++-
banyand/internal/storage/segment.go | 66 +++--
banyand/internal/storage/tsdb.go | 9 +-
banyand/liaison/grpc/discovery.go | 52 ++--
banyand/liaison/grpc/measure.go | 15 +-
banyand/liaison/grpc/stream.go | 14 +-
banyand/measure/metadata.go | 1 +
banyand/metadata/client.go | 62 ++++-
banyand/metadata/metadata.go | 2 +
banyand/metadata/schema/node.go | 11 +
banyand/metadata/schema/register_test.go | 25 ++
banyand/metadata/schema/schema.go | 1 +
banyand/queue/pub/client.go | 45 ++--
banyand/queue/pub/client_test.go | 68 +++++
banyand/queue/pub/pub.go | 45 +++-
banyand/queue/pub/pub_suite_test.go | 8 +
banyand/queue/pub/pub_test.go | 281 +++++++++++++++++++++
banyand/queue/pub/selector.go | 232 +++++++++++++++++
banyand/queue/pub/selector_test.go | 347 ++++++++++++++++++++++++++
banyand/stream/metadata.go | 1 +
docs/api-reference.md | 214 ++++++++++------
pkg/bus/bus.go | 27 +-
pkg/cmdsetup/root.go | 1 +
pkg/timestamp/range.go | 30 +++
31 files changed, 1525 insertions(+), 160 deletions(-)
diff --git a/CHANGES.md b/CHANGES.md
index 8ba53c15..cf8cda64 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -30,6 +30,7 @@ Release Notes.
- Add restore command line tool to restore the data files.
- Add concurrent barrier to partition merge to improve the performance of the partition merge.
- Improve the write performance.
+- Add node labels to classify the nodes.
### Bug Fixes
diff --git a/api/common/id.go b/api/common/id.go
index 668df583..36c6692f 100644
--- a/api/common/id.go
+++ b/api/common/id.go
@@ -60,6 +60,7 @@ type contextPositionKey struct{}
// The logger could attach it for debugging.
type Position struct {
Module string
+ Stage string
Database string
Shard string
Segment string
@@ -142,6 +143,7 @@ func (e Error) Error() string {
// Node contains the node id and address.
type Node struct {
+ Labels map[string]string
NodeID string
GrpcAddress string
HTTPAddress string
@@ -152,6 +154,8 @@ var (
FlagNodeHost string
// FlagNodeHostProvider is the node id provider from flag.
FlagNodeHostProvider NodeHostProvider
+ // FlagNodeLabels is the node labels from flag.
+ FlagNodeLabels []string
)
// NodeHostProvider is the provider of node id.
@@ -218,9 +222,22 @@ func GenerateNode(grpcPort, httpPort *uint32) (node Node, err error) {
if httpPort != nil {
node.HTTPAddress = net.JoinHostPort(nodeHost, strconv.FormatUint(uint64(*httpPort), 10))
}
+ node.Labels = parseNodeFlags()
return node, nil
}
+func parseNodeFlags() map[string]string {
+ labels := make(map[string]string)
+ for _, label := range FlagNodeLabels {
+ parts := strings.Split(label, "=")
+ if len(parts) != 2 {
+ continue
+ }
+ labels[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
+ }
+ return labels
+}
+
// ContextNodeKey is a context key to store the node id.
var ContextNodeKey = contextNodeKey{}
diff --git a/api/proto/banyandb/common/v1/common.proto b/api/proto/banyandb/common/v1/common.proto
index 3db00dd6..24846c56 100644
--- a/api/proto/banyandb/common/v1/common.proto
+++ b/api/proto/banyandb/common/v1/common.proto
@@ -92,12 +92,9 @@ message ResourceOpts {
// ttl indicates time to live, how long the data will be cached
IntervalRule ttl = 3;
// stages defines the ordered lifecycle stages. Data progresses through these stages sequentially.
- repeated LifecycleStage stages = 4 [
- (validate.rules).repeated = {
- min_items: 1,
- items: {message: {required: true}}
- }
- ];
+ repeated LifecycleStage stages = 4;
+ // default_node_selector is the default node selector for queries if node_selector is not specified
+ string default_node_selector = 5;
}
// Group is an internal object for Group management
diff --git a/api/proto/banyandb/database/v1/database.proto b/api/proto/banyandb/database/v1/database.proto
index 8ae7ffc3..a5caddba 100644
--- a/api/proto/banyandb/database/v1/database.proto
+++ b/api/proto/banyandb/database/v1/database.proto
@@ -20,6 +20,7 @@ syntax = "proto3";
package banyandb.database.v1;
import "banyandb/common/v1/common.proto";
+import "banyandb/model/v1/query.proto";
import "google/protobuf/timestamp.proto";
option go_package = "github.com/apache/skywalking-banyandb/api/proto/banyandb/database/v1";
@@ -38,6 +39,11 @@ message Node {
string grpc_address = 3;
string http_address = 4;
google.protobuf.Timestamp created_at = 5;
+ // labels is a set of key-value pairs to describe the node.
+ map<string, string> labels = 6;
+ // data_segments_boundary is the time range of the data segments that the node is responsible for.
+ // [start, end) is the time range.
+ map<string, model.v1.TimeRange> data_segments_boundary = 7;
}
message Shard {
diff --git a/api/proto/banyandb/measure/v1/query.proto b/api/proto/banyandb/measure/v1/query.proto
index 11f30f68..4537bcf5 100644
--- a/api/proto/banyandb/measure/v1/query.proto
+++ b/api/proto/banyandb/measure/v1/query.proto
@@ -111,4 +111,6 @@ message QueryRequest {
model.v1.QueryOrder order_by = 12;
// trace is used to enable trace for the query
bool trace = 13;
+ // node_selector is used to specify the target node for the query
+ string node_selector = 14;
}
diff --git a/api/proto/banyandb/property/v1/rpc.proto b/api/proto/banyandb/property/v1/rpc.proto
index 0a16da9a..ffc50167 100644
--- a/api/proto/banyandb/property/v1/rpc.proto
+++ b/api/proto/banyandb/property/v1/rpc.proto
@@ -78,6 +78,8 @@ message QueryRequest {
uint32 limit = 6;
// trace is used to enable trace for the query
bool trace = 7;
+ // node_selector is used to select the node to query
+ string node_selector = 8;
}
// QueryResponse is the response for a query to the Query module.
diff --git a/api/proto/banyandb/stream/v1/query.proto b/api/proto/banyandb/stream/v1/query.proto
index 250b332a..4fe18f9e 100644
--- a/api/proto/banyandb/stream/v1/query.proto
+++ b/api/proto/banyandb/stream/v1/query.proto
@@ -77,4 +77,6 @@ message QueryRequest {
model.v1.TagProjection projection = 8 [(validate.rules).message.required = true];
// trace is used to enable trace for the query
bool trace = 9;
+ // node_selector is used to select the node to query
+ string node_selector = 10;
}
diff --git a/banyand/internal/storage/rotation_test.go b/banyand/internal/storage/rotation_test.go
index f25576b4..a0aed52e 100644
--- a/banyand/internal/storage/rotation_test.go
+++ b/banyand/internal/storage/rotation_test.go
@@ -19,6 +19,7 @@ package storage
import (
"context"
+ "sync"
"testing"
"time"
@@ -26,6 +27,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/apache/skywalking-banyandb/api/common"
+ modelv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/model/v1"
"github.com/apache/skywalking-banyandb/banyand/observability"
"github.com/apache/skywalking-banyandb/pkg/fs"
"github.com/apache/skywalking-banyandb/pkg/logger"
@@ -34,9 +36,76 @@ import (
"github.com/apache/skywalking-banyandb/pkg/timestamp"
)
+type boundaryUpdateTracker struct {
+ updates []*modelv1.TimeRange
+ called int
+ mu sync.Mutex
+}
+
+func (t *boundaryUpdateTracker) recordUpdate(_, _ string, boundary *modelv1.TimeRange) {
+ t.mu.Lock()
+ defer t.mu.Unlock()
+ t.updates = append(t.updates, boundary)
+ t.called++
+}
+
+func TestSegmentBoundaryUpdateFn(t *testing.T) {
+ t.Run("called when a segment is created", func(t *testing.T) {
+ tsdb, c, _, tracker, dfFn := setUpDB(t)
+ defer dfFn()
+
+ // Initial segment creation should have triggered it once
+ require.Equal(t, 1, tracker.called)
+
+ // Create new segment
+ ts := c.Now().Add(23*time.Hour + time.Second)
+ tsdb.Tick(ts.UnixNano())
+
+ // Verify the function was called again
+ assert.Eventually(t, func() bool {
+ tracker.mu.Lock()
+ defer tracker.mu.Unlock()
+ return tracker.called >= 2
+ }, flags.EventuallyTimeout, time.Millisecond, "boundary update function should be called")
+ })
+
+ t.Run("called when a segment is deleted", func(t *testing.T) {
+ tsdb, c, segCtrl, tracker, dfFn := setUpDB(t)
+ defer dfFn()
+
+ ts := c.Now()
+ for i := 0; i < 4; i++ {
+ ts = ts.Add(23 * time.Hour)
+ c.Set(ts)
+ tsdb.Tick(ts.UnixNano())
+ t.Logf("current time: %s", ts.Format(time.RFC3339))
+ expected := i + 2
+ require.EventuallyWithTf(t, func(ct *assert.CollectT) {
+ if len(segCtrl.segments()) != expected {
+ ct.Errorf("expect %d segments, got %d", expected, len(segCtrl.segments()))
+ }
+ }, flags.EventuallyTimeout, time.Millisecond, "wait for %d segment to be created", expected)
+ ts = ts.Add(time.Hour)
+ }
+
+ tracker.mu.Lock()
+ called := tracker.called
+ tracker.mu.Unlock()
+
+ c.Set(ts)
+ tsdb.Tick(ts.UnixNano())
+
+ assert.Eventually(t, func() bool {
+ tracker.mu.Lock()
+ defer tracker.mu.Unlock()
+ return tracker.called > called
+ }, flags.EventuallyTimeout, time.Millisecond, "boundary update function should be called after deletion")
+ })
+}
+
func TestForwardRotation(t *testing.T) {
t.Run("create a new segment when the time is up", func(t *testing.T) {
- tsdb, c, segCtrl, dfFn := setUpDB(t)
+ tsdb, c, segCtrl, _, dfFn := setUpDB(t)
defer dfFn()
ts := c.Now().Add(23*time.Hour + time.Second)
t.Logf("current time: %s", ts.Format(time.RFC3339))
@@ -47,7 +116,7 @@ func TestForwardRotation(t *testing.T) {
})
t.Run("no new segment created when the time is not up", func(t *testing.T) {
- tsdb, c, segCtrl, dfFn := setUpDB(t)
+ tsdb, c, segCtrl, _, dfFn := setUpDB(t)
defer dfFn()
ts := c.Now().Add(22*time.Hour + 59*time.Minute + 59*time.Second)
t.Logf("current time: %s", ts.Format(time.RFC3339))
@@ -60,7 +129,7 @@ func TestForwardRotation(t *testing.T) {
func TestRetention(t *testing.T) {
t.Run("delete the segment and index when the TTL is up", func(t *testing.T) {
- tsdb, c, segCtrl, dfFn := setUpDB(t)
+ tsdb, c, segCtrl, _, dfFn := setUpDB(t)
defer dfFn()
ts := c.Now()
for i := 0; i < 4; i++ {
@@ -87,7 +156,7 @@ func TestRetention(t *testing.T) {
})
t.Run("keep the segment volume stable", func(t *testing.T) {
- tsdb, c, segCtrl, dfFn := setUpDB(t)
+ tsdb, c, segCtrl, _, dfFn := setUpDB(t)
defer dfFn()
ts := c.Now()
for i := 0; i < 10; i++ {
@@ -133,14 +202,22 @@ func TestRetention(t *testing.T) {
})
}
-func setUpDB(t *testing.T) (*database[*MockTSTable, any], timestamp.MockClock, *segmentController[*MockTSTable, any], func()) {
+func setUpDB(t *testing.T) (*database[*MockTSTable, any], timestamp.MockClock, *segmentController[*MockTSTable, any], *boundaryUpdateTracker, func()) {
dir, defFn := test.Space(require.New(t))
+
+ // Create tracker for boundary updates
+ tracker := &boundaryUpdateTracker{
+ updates: make([]*modelv1.TimeRange, 0),
+ }
+
TSDBOpts := TSDBOpts[*MockTSTable, any]{
Location: dir,
SegmentInterval: IntervalRule{Unit: DAY, Num: 1},
TTL: IntervalRule{Unit: DAY, Num: 3},
ShardNum: 1,
TSTableCreator: MockTSTableCreator,
+ // Add the boundary update function
+ SegmentBoundaryUpdateFn: tracker.recordUpdate,
}
ctx := context.Background()
mc := timestamp.NewMockClock()
@@ -157,7 +234,7 @@ func setUpDB(t *testing.T) (*database[*MockTSTable, any], timestamp.MockClock, *
db := tsdb.(*database[*MockTSTable, any])
require.Equal(t, len(db.segmentController.segments()), 1)
- return db, mc, db.segmentController, func() {
+ return db, mc, db.segmentController, tracker, func() {
tsdb.Close()
defFn()
}
diff --git a/banyand/internal/storage/segment.go b/banyand/internal/storage/segment.go
index 8233dba6..dfbae4dc 100644
--- a/banyand/internal/storage/segment.go
+++ b/banyand/internal/storage/segment.go
@@ -30,9 +30,11 @@ import (
"time"
"github.com/pkg/errors"
+ "google.golang.org/protobuf/types/known/timestamppb"
"github.com/apache/skywalking-banyandb/api/common"
commonv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/common/v1"
+ modelv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/model/v1"
"github.com/apache/skywalking-banyandb/pkg/convert"
"github.com/apache/skywalking-banyandb/pkg/index/inverted"
"github.com/apache/skywalking-banyandb/pkg/logger"
@@ -204,31 +206,39 @@ func (s *segment[T, O]) String() string {
}
type segmentController[T TSTable, O any] struct {
- clock timestamp.Clock
- metrics Metrics
- l *logger.Logger
- indexMetrics *inverted.Metrics
- opts *TSDBOpts[T, O]
- position common.Position
- location string
- lst []*segment[T, O]
- deadline atomic.Int64
- optsMutex sync.RWMutex
+ clock timestamp.Clock
+ metrics Metrics
+ opts *TSDBOpts[T, O]
+ l *logger.Logger
+ indexMetrics *inverted.Metrics
+ segmentBoundaryUpdateFn SegmentBoundaryUpdateFn
+ position common.Position
+ db string
+ stage string
+ location string
+ lst []*segment[T, O]
+ deadline atomic.Int64
+ optsMutex sync.RWMutex
sync.RWMutex
}
func newSegmentController[T TSTable, O any](ctx context.Context, location string,
l *logger.Logger, opts TSDBOpts[T, O], indexMetrics *inverted.Metrics, metrics Metrics,
+ segmentsBoundaryUpdateFn SegmentBoundaryUpdateFn,
) *segmentController[T, O] {
clock, _ := timestamp.GetClock(ctx)
+ p := common.GetPosition(ctx)
return &segmentController[T, O]{
- location: location,
- opts: &opts,
- l: l,
- clock: clock,
- position: common.GetPosition(ctx),
- metrics: metrics,
- indexMetrics: indexMetrics,
+ location: location,
+ opts: &opts,
+ l: l,
+ clock: clock,
+ position: common.GetPosition(ctx),
+ metrics: metrics,
+ indexMetrics: indexMetrics,
+ segmentBoundaryUpdateFn: segmentsBoundaryUpdateFn,
+ stage: p.Stage,
+ db: p.Database,
}
}
@@ -278,9 +288,30 @@ func (sc *segmentController[T, O]) createSegment(ts time.Time) (*segment[T, O],
return nil, err
}
s.incRef()
+ sc.notifySegmentBoundaryUpdate()
return s, nil
}
+func (sc *segmentController[T, O]) notifySegmentBoundaryUpdate() {
+ if sc.segmentBoundaryUpdateFn == nil {
+ return
+ }
+ segs := sc.segments()
+ defer func() {
+ for i := range segs {
+ segs[i].DecRef()
+ }
+ }()
+ var tr *modelv1.TimeRange
+ if len(segs) > 0 {
+ tr = &modelv1.TimeRange{
+ Begin: timestamppb.New(segs[0].Start),
+ End: timestamppb.New(segs[len(segs)-1].End),
+ }
+ }
+ sc.segmentBoundaryUpdateFn(sc.stage, sc.db, tr)
+}
+
func (sc *segmentController[T, O]) segments() (ss []*segment[T, O]) {
sc.RLock()
defer sc.RUnlock()
@@ -427,6 +458,7 @@ func (sc *segmentController[T, O]) remove(deadline time.Time) (hasSegment bool,
}
s.DecRef()
}
+ sc.notifySegmentBoundaryUpdate()
return hasSegment, err
}
diff --git a/banyand/internal/storage/tsdb.go b/banyand/internal/storage/tsdb.go
index 676ea38f..67ae1280 100644
--- a/banyand/internal/storage/tsdb.go
+++ b/banyand/internal/storage/tsdb.go
@@ -29,6 +29,7 @@ import (
"github.com/apache/skywalking-banyandb/api/common"
commonv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/common/v1"
+ modelv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/model/v1"
"github.com/apache/skywalking-banyandb/banyand/observability"
"github.com/apache/skywalking-banyandb/pkg/fs"
"github.com/apache/skywalking-banyandb/pkg/index/inverted"
@@ -49,18 +50,22 @@ const (
lockFilename = "lock"
)
+// SegmentBoundaryUpdateFn is a callback function to update the segment boundary.
+type SegmentBoundaryUpdateFn func(stage, group string, boundary *modelv1.TimeRange)
+
// TSDBOpts wraps options to create a tsdb.
type TSDBOpts[T TSTable, O any] struct {
Option O
TableMetrics Metrics
TSTableCreator TSTableCreator[T, O]
StorageMetricsFactory *observability.Factory
+ SegmentBoundaryUpdateFn SegmentBoundaryUpdateFn
Location string
SegmentInterval IntervalRule
TTL IntervalRule
SeriesIndexFlushTimeoutSeconds int64
- ShardNum uint32
SeriesIndexCacheMaxBytes int
+ ShardNum uint32
}
type (
@@ -130,7 +135,7 @@ func OpenTSDB[T TSTable, O any](ctx context.Context, opts TSDBOpts[T, O]) (TSDB[
tsEventCh: make(chan int64),
p: p,
segmentController: newSegmentController[T](ctx, location,
- l, opts, indexMetrics, opts.TableMetrics),
+ l, opts, indexMetrics, opts.TableMetrics, opts.SegmentBoundaryUpdateFn),
metrics: newMetrics(opts.StorageMetricsFactory),
}
db.logger.Info().Str("path", opts.Location).Msg("initialized")
diff --git a/banyand/liaison/grpc/discovery.go b/banyand/liaison/grpc/discovery.go
index 661deb01..d266acf1 100644
--- a/banyand/liaison/grpc/discovery.go
+++ b/banyand/liaison/grpc/discovery.go
@@ -39,17 +39,17 @@ var errNotExist = errors.New("the object doesn't exist")
type discoveryService struct {
metadataRepo metadata.Repo
nodeRegistry NodeRegistry
- shardRepo *shardRepo
+ groupRepo *groupRepo
entityRepo *entityRepo
log *logger.Logger
kind schema.Kind
}
func newDiscoveryService(kind schema.Kind, metadataRepo metadata.Repo, nodeRegistry NodeRegistry) *discoveryService {
- sr := &shardRepo{shardEventsMap: make(map[identity]uint32)}
+ sr := &groupRepo{resourceOpts: make(map[string]*commonv1.ResourceOpts)}
er := &entityRepo{entitiesMap: make(map[identity]partition.EntityLocator)}
return &discoveryService{
- shardRepo: sr,
+ groupRepo: sr,
entityRepo: er,
kind: kind,
metadataRepo: metadataRepo,
@@ -58,21 +58,19 @@ func newDiscoveryService(kind schema.Kind, metadataRepo metadata.Repo, nodeRegis
}
func (ds *discoveryService) initialize() error {
- ds.metadataRepo.RegisterHandler("liaison", schema.KindGroup, ds.shardRepo)
+ ds.metadataRepo.RegisterHandler("liaison", schema.KindGroup, ds.groupRepo)
ds.metadataRepo.RegisterHandler("liaison", ds.kind, ds.entityRepo)
return nil
}
func (ds *discoveryService) SetLogger(log *logger.Logger) {
ds.log = log
- ds.shardRepo.log = log
+ ds.groupRepo.log = log
ds.entityRepo.log = log
}
func (ds *discoveryService) navigate(metadata *commonv1.Metadata, tagFamilies []*modelv1.TagFamilyForWrite) (pbv1.Entity, pbv1.EntityValues, common.ShardID, error) {
- shardNum, existed := ds.shardRepo.shardNum(getID(&commonv1.Metadata{
- Name: metadata.Group,
- }))
+ shardNum, existed := ds.groupRepo.shardNum(metadata.Group)
if !existed {
return nil, nil, common.ShardID(0), errors.Wrapf(errNotExist, "finding the shard num by: %v", metadata)
}
@@ -92,16 +90,16 @@ func (i identity) String() string {
return fmt.Sprintf("%s/%s", i.group, i.name)
}
-var _ schema.EventHandler = (*shardRepo)(nil)
+var _ schema.EventHandler = (*groupRepo)(nil)
-type shardRepo struct {
+type groupRepo struct {
schema.UnimplementedOnInitHandler
- log *logger.Logger
- shardEventsMap map[identity]uint32
+ log *logger.Logger
+ resourceOpts map[string]*commonv1.ResourceOpts
sync.RWMutex
}
-func (s *shardRepo) OnAddOrUpdate(schemaMetadata schema.Metadata) {
+func (s *groupRepo) OnAddOrUpdate(schemaMetadata schema.Metadata) {
if schemaMetadata.Kind != schema.KindGroup {
return
}
@@ -109,16 +107,15 @@ func (s *shardRepo) OnAddOrUpdate(schemaMetadata schema.Metadata) {
if group.ResourceOpts == nil || group.Catalog == commonv1.Catalog_CATALOG_UNSPECIFIED {
return
}
- idx := getID(group.GetMetadata())
if le := s.log.Debug(); le.Enabled() {
- le.Stringer("id", idx).Uint32("total", group.ResourceOpts.ShardNum).Msg("shard added or updated")
+ le.Stringer("id", group.Metadata).Uint32("total", group.ResourceOpts.ShardNum).Msg("shard added or updated")
}
s.RWMutex.Lock()
defer s.RWMutex.Unlock()
- s.shardEventsMap[idx] = group.ResourceOpts.ShardNum
+ s.resourceOpts[group.Metadata.GetName()] = group.ResourceOpts
}
-func (s *shardRepo) OnDelete(schemaMetadata schema.Metadata) {
+func (s *groupRepo) OnDelete(schemaMetadata schema.Metadata) {
if schemaMetadata.Kind != schema.KindGroup {
return
}
@@ -126,23 +123,32 @@ func (s *shardRepo) OnDelete(schemaMetadata schema.Metadata) {
if group.ResourceOpts == nil || group.Catalog == commonv1.Catalog_CATALOG_UNSPECIFIED {
return
}
- idx := getID(group.GetMetadata())
if le := s.log.Debug(); le.Enabled() {
- le.Stringer("id", idx).Msg("shard deleted")
+ le.Stringer("id", group.Metadata).Msg("shard deleted")
}
s.RWMutex.Lock()
defer s.RWMutex.Unlock()
- delete(s.shardEventsMap, idx)
+ delete(s.resourceOpts, group.Metadata.GetName())
}
-func (s *shardRepo) shardNum(idx identity) (uint32, bool) {
+func (s *groupRepo) shardNum(groupName string) (uint32, bool) {
s.RWMutex.RLock()
defer s.RWMutex.RUnlock()
- sn, ok := s.shardEventsMap[idx]
+ r, ok := s.resourceOpts[groupName]
if !ok {
return 0, false
}
- return sn, true
+ return r.ShardNum, true
+}
+
+func (s *groupRepo) getNodeSelector(groupName string) (string, bool) {
+ s.RWMutex.RLock()
+ defer s.RWMutex.RUnlock()
+ r, ok := s.resourceOpts[groupName]
+ if !ok {
+ return "", false
+ }
+ return r.DefaultNodeSelector, true
}
func getID(metadata *commonv1.Metadata) identity {
diff --git a/banyand/liaison/grpc/measure.go b/banyand/liaison/grpc/measure.go
index 7dd38590..a6761208 100644
--- a/banyand/liaison/grpc/measure.go
+++ b/banyand/liaison/grpc/measure.go
@@ -215,7 +215,20 @@ func (ms *measureService) Query(ctx context.Context, req *measurev1.QueryRequest
span.Stop()
}()
}
- feat, err := ms.broadcaster.Publish(ctx, data.TopicMeasureQuery, bus.NewMessage(bus.MessageID(now.UnixNano()), req))
+ nodeSelectors := make(map[string]string)
+ for _, g := range req.Groups {
+ if req.NodeSelector != "" {
+ nodeSelectors[g] = req.NodeSelector
+ continue
+ }
+ if ns, exist := ms.groupRepo.getNodeSelector(g); exist {
+ nodeSelectors[g] = ns
+ continue
+ }
+ nodeSelectors[g] = ""
+ }
+
+ feat, err := ms.broadcaster.Publish(ctx, data.TopicMeasureQuery, bus.NewMessageWithNodeSelectors(bus.MessageID(now.UnixNano()), nodeSelectors, req.TimeRange, req))
if err != nil {
return nil, err
}
diff --git a/banyand/liaison/grpc/stream.go b/banyand/liaison/grpc/stream.go
index c7c0c245..0bbbef5c 100644
--- a/banyand/liaison/grpc/stream.go
+++ b/banyand/liaison/grpc/stream.go
@@ -213,7 +213,19 @@ func (s *streamService) Query(ctx context.Context, req *streamv1.QueryRequest) (
span.Stop()
}()
}
- message := bus.NewMessage(bus.MessageID(now.UnixNano()), req)
+ nodeSelectors := make(map[string]string)
+ for _, g := range req.Groups {
+ if req.NodeSelector != "" {
+ nodeSelectors[g] = req.NodeSelector
+ continue
+ }
+ if ns, exist := s.groupRepo.getNodeSelector(g); exist {
+ nodeSelectors[g] = ns
+ continue
+ }
+ nodeSelectors[g] = ""
+ }
+ message := bus.NewMessageWithNodeSelectors(bus.MessageID(now.UnixNano()), nodeSelectors, req.TimeRange, req)
feat, errQuery := s.broadcaster.Publish(ctx, data.TopicStreamQuery, message)
if errQuery != nil {
if errors.Is(errQuery, io.EOF) {
diff --git a/banyand/measure/metadata.go b/banyand/measure/metadata.go
index b5b8f1cf..1c37d41e 100644
--- a/banyand/measure/metadata.go
+++ b/banyand/measure/metadata.go
@@ -372,6 +372,7 @@ func (s *supplier) OpenDB(groupSchema *commonv1.Group) (resourceSchema.DB, error
SeriesIndexFlushTimeoutSeconds: s.option.flushTimeout.Nanoseconds() / int64(time.Second),
SeriesIndexCacheMaxBytes: int(s.option.seriesCacheMaxSize),
StorageMetricsFactory: factory,
+ SegmentBoundaryUpdateFn: s.metadata.UpdateSegmentsBoundary,
}
return storage.OpenTSDB(
common.SetPosition(context.Background(), func(_ common.Position) common.Position {
diff --git a/banyand/metadata/client.go b/banyand/metadata/client.go
index f3bcf0c1..1f46a0ff 100644
--- a/banyand/metadata/client.go
+++ b/banyand/metadata/client.go
@@ -21,6 +21,8 @@ import (
"context"
"os"
"os/signal"
+ "strings"
+ "sync"
"syscall"
"time"
@@ -31,6 +33,7 @@ import (
"github.com/apache/skywalking-banyandb/api/common"
commonv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/common/v1"
databasev1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/database/v1"
+ modelv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/model/v1"
"github.com/apache/skywalking-banyandb/banyand/metadata/schema"
"github.com/apache/skywalking-banyandb/pkg/logger"
"github.com/apache/skywalking-banyandb/pkg/run"
@@ -64,15 +67,17 @@ func NewClient(forceRegisterNode bool) (Service, error) {
type clientService struct {
schemaRegistry schema.Registry
closer *run.Closer
- namespace string
- etcdUsername string
+ nodeInfo *databasev1.Node
+ etcdTLSCertFile string
etcdPassword string
etcdTLSCAFile string
- etcdTLSCertFile string
+ etcdUsername string
etcdTLSKeyFile string
+ namespace string
endpoints []string
registryTimeout time.Duration
etcdFullSyncInterval time.Duration
+ nodeInfoMux sync.Mutex
forceRegisterNode bool
}
@@ -164,6 +169,7 @@ func (s *clientService) PreRun(ctx context.Context) error {
GrpcAddress: node.GrpcAddress,
HttpAddress: node.HTTPAddress,
Roles: nodeRoles,
+ Labels: node.Labels,
CreatedAt: timestamppb.Now(),
}
for {
@@ -189,6 +195,9 @@ func (s *clientService) PreRun(ctx context.Context) error {
}
if err == nil {
l.Info().Stringer("info", nodeInfo).Msg("register node successfully")
+ s.nodeInfoMux.Lock()
+ s.nodeInfo = nodeInfo
+ s.nodeInfoMux.Unlock()
}
return err
}
@@ -339,3 +348,50 @@ func contains(s []string, e string) bool {
}
return false
}
+
+func (s *clientService) UpdateSegmentsBoundary(stage, group string, boundary *modelv1.TimeRange) {
+ s.nodeInfoMux.Lock()
+ defer s.nodeInfoMux.Unlock()
+ nodeInfo := s.nodeInfo
+ b := nodeInfo.DataSegmentsBoundary
+ if b == nil {
+ b = make(map[string]*modelv1.TimeRange)
+ }
+ key := getNodeBoundaryKey(group, stage)
+ if boundary == nil {
+ delete(b, key)
+ } else {
+ b[key] = boundary
+ }
+ nodeInfo.DataSegmentsBoundary = b
+ s.nodeInfo = nodeInfo
+ if err := s.schemaRegistry.UpdateNode(context.Background(), nodeInfo); err != nil {
+ logger.GetLogger(s.Name()).Error().Err(err).Msg("failed to update node")
+ }
+}
+
+func getNodeBoundaryKey(items ...string) string {
+ return strings.Join(items, "|")
+}
+
+// FindSegmentsBoundary finds the segments boundary for the given group.
+func FindSegmentsBoundary(nodeInfo *databasev1.Node, group string) *modelv1.TimeRange {
+ b := nodeInfo.DataSegmentsBoundary
+ if len(b) == 0 {
+ return nil
+ }
+ result := &modelv1.TimeRange{}
+ for g, tr := range b {
+ segments := strings.Split(g, "|")
+ if segments[0] != group {
+ continue
+ }
+ if result.Begin == nil || result.Begin.AsTime().After(tr.Begin.AsTime()) {
+ result.Begin = tr.Begin
+ }
+ if result.End == nil || result.End.AsTime().Before(tr.End.AsTime()) {
+ result.End = tr.End
+ }
+ }
+ return result
+}
diff --git a/banyand/metadata/metadata.go b/banyand/metadata/metadata.go
index adb6a1da..e0b23fa0 100644
--- a/banyand/metadata/metadata.go
+++ b/banyand/metadata/metadata.go
@@ -24,6 +24,7 @@ import (
commonv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/common/v1"
databasev1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/database/v1"
+ modelv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/model/v1"
"github.com/apache/skywalking-banyandb/banyand/metadata/schema"
"github.com/apache/skywalking-banyandb/pkg/run"
)
@@ -46,6 +47,7 @@ type Repo interface {
GroupRegistry() schema.Group
TopNAggregationRegistry() schema.TopNAggregation
RegisterHandler(string, schema.Kind, schema.EventHandler)
+ UpdateSegmentsBoundary(stage, group string, boundary *modelv1.TimeRange)
}
// Service is the metadata repository.
diff --git a/banyand/metadata/schema/node.go b/banyand/metadata/schema/node.go
index a9d8ae77..aacf6d2f 100644
--- a/banyand/metadata/schema/node.go
+++ b/banyand/metadata/schema/node.go
@@ -59,6 +59,17 @@ func (e *etcdSchemaRegistry) RegisterNode(ctx context.Context, node *databasev1.
}, forced)
}
+func (e *etcdSchemaRegistry) UpdateNode(ctx context.Context, node *databasev1.Node) error {
+ _, err := e.update(ctx, Metadata{
+ TypeMeta: TypeMeta{
+ Kind: KindNode,
+ Name: node.Metadata.Name,
+ },
+ Spec: node,
+ })
+ return err
+}
+
func formatNodeKey(name string) string {
return path.Join(nodeKeyPrefix, name)
}
diff --git a/banyand/metadata/schema/register_test.go b/banyand/metadata/schema/register_test.go
index 1e9ceef8..8280e150 100644
--- a/banyand/metadata/schema/register_test.go
+++ b/banyand/metadata/schema/register_test.go
@@ -26,9 +26,11 @@ import (
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/onsi/gomega/gleak"
+ "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"
+ modelv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/model/v1"
"github.com/apache/skywalking-banyandb/banyand/metadata/embeddedetcd"
"github.com/apache/skywalking-banyandb/banyand/metadata/schema"
"github.com/apache/skywalking-banyandb/pkg/test"
@@ -118,4 +120,27 @@ var _ = ginkgo.Describe("etcd_register", func() {
return err
}, flags.EventuallyTimeout).ShouldNot(gomega.HaveOccurred())
})
+
+ ginkgo.It("should update node's data segments boundary", func() {
+ gomega.Expect(r.Register(context.Background(), md, false)).ShouldNot(gomega.HaveOccurred())
+
+ n, err := r.GetNode(context.Background(), node)
+ gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
+
+ newBoundary := &modelv1.TimeRange{
+ Begin: timestamppb.New(time.Unix(1000, 0)),
+ End: timestamppb.New(time.Unix(2000, 0)),
+ }
+ n.DataSegmentsBoundary = map[string]*modelv1.TimeRange{"default": newBoundary}
+
+ gomega.Expect(r.UpdateNode(context.Background(), n)).ShouldNot(gomega.HaveOccurred())
+
+ updatedNode, err := r.GetNode(context.Background(), node)
+ gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
+ gomega.Expect(updatedNode.DataSegmentsBoundary).ShouldNot(gomega.BeNil())
+ gomega.Expect(updatedNode.DataSegmentsBoundary).Should(gomega.HaveLen(1))
+ gomega.Expect(updatedNode.DataSegmentsBoundary["default"]).ShouldNot(gomega.BeNil())
+ gomega.Expect(updatedNode.DataSegmentsBoundary["default"].Begin.Seconds).Should(gomega.Equal(int64(1000)))
+ gomega.Expect(updatedNode.DataSegmentsBoundary["default"].End.Seconds).Should(gomega.Equal(int64(2000)))
+ })
})
diff --git a/banyand/metadata/schema/schema.go b/banyand/metadata/schema/schema.go
index 8f4a8e35..95d7e824 100644
--- a/banyand/metadata/schema/schema.go
+++ b/banyand/metadata/schema/schema.go
@@ -198,4 +198,5 @@ type Node interface {
ListNode(ctx context.Context, role databasev1.Role) ([]*databasev1.Node, error)
RegisterNode(ctx context.Context, node *databasev1.Node, forced bool) error
GetNode(ctx context.Context, node string) (*databasev1.Node, error)
+ UpdateNode(ctx context.Context, node *databasev1.Node) error
}
diff --git a/banyand/queue/pub/client.go b/banyand/queue/pub/client.go
index cf818cc5..c180c086 100644
--- a/banyand/queue/pub/client.go
+++ b/banyand/queue/pub/client.go
@@ -100,24 +100,7 @@ func (p *pub) OnAddOrUpdate(md schema.Metadata) {
p.mu.Lock()
defer p.mu.Unlock()
- if n, ok := p.registered[name]; ok {
- if n.GrpcAddress == address {
- return
- }
- if en, ok := p.evictable[name]; ok {
- close(en.c)
- delete(p.evictable, name)
- p.log.Info().Str("node", name).Str("status", p.dump()).Msg("node is removed from evict queue by the new gRPC address updated event")
- }
-
- if client, ok := p.active[name]; ok {
- _ = client.conn.Close()
- delete(p.active, name)
- p.deleteClient(client.md)
- p.log.Info().Str("status", p.dump()).Str("node", name).Msg("node is removed from active queue by the new gRPC address updated event")
- }
- }
- p.registered[name] = node
+ p.registerNode(node)
if _, ok := p.active[name]; ok {
return
@@ -142,6 +125,32 @@ func (p *pub) OnAddOrUpdate(md schema.Metadata) {
p.log.Info().Str("status", p.dump()).Stringer("node", node).Msg("new node is healthy, add it to active queue")
}
+func (p *pub) registerNode(node *databasev1.Node) {
+ name := node.Metadata.GetName()
+ defer func() {
+ p.registered[name] = node
+ }()
+
+ n, ok := p.registered[name]
+ if !ok {
+ return
+ }
+ if n.GrpcAddress == node.GrpcAddress {
+ return
+ }
+ if en, ok := p.evictable[name]; ok {
+ close(en.c)
+ delete(p.evictable, name)
+ p.log.Info().Str("node", name).Str("status", p.dump()).Msg("node is removed from evict queue by the new gRPC address updated event")
+ }
+ if client, ok := p.active[name]; ok {
+ _ = client.conn.Close()
+ delete(p.active, name)
+ p.deleteClient(client.md)
+ p.log.Info().Str("status", p.dump()).Str("node", name).Msg("node is removed from active queue by the new gRPC address updated event")
+ }
+}
+
func (p *pub) OnDelete(md schema.Metadata) {
if md.Kind != schema.KindNode {
return
diff --git a/banyand/queue/pub/client_test.go b/banyand/queue/pub/client_test.go
index 12678b6e..a96d7da8 100644
--- a/banyand/queue/pub/client_test.go
+++ b/banyand/queue/pub/client_test.go
@@ -26,6 +26,7 @@ import (
"github.com/onsi/gomega/gleak"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/health/grpc_health_v1"
+ "google.golang.org/protobuf/types/known/timestamppb"
"github.com/apache/skywalking-banyandb/api/common"
"github.com/apache/skywalking-banyandb/api/data"
@@ -35,6 +36,8 @@ import (
"github.com/apache/skywalking-banyandb/pkg/test/flags"
)
+const svc = "service"
+
var _ = ginkgo.Describe("publish clients register/unregister", func() {
var goods []gleak.Goroutine
ginkgo.BeforeEach(func() {
@@ -144,6 +147,71 @@ var _ = ginkgo.Describe("publish clients register/unregister", func() {
verifyClientsWithGomega(g, p, data.TopicStreamWrite, 1, 0, 2, 1)
}, flags.EventuallyTimeout).Should(gomega.Succeed())
})
+
+ ginkgo.It("should update node when labels or data boundaries change", func() {
+ addr1 := getAddress()
+ closeFn := setup(addr1, codes.OK, 200*time.Millisecond)
+ defer closeFn()
+ p := newPub()
+ defer p.GracefulStop()
+
+ // Replace hard-coded "service" with the Service constant
+ group1 := svc
+ now := time.Now()
+ timeRange1 := &modelv1.TimeRange{
+ Begin: timestamppb.New(now.Add(-3 * time.Hour)),
+ End: timestamppb.New(now.Add(-2 * time.Hour)),
+ }
+
+ initialLabels := map[string]string{
+ "role": "ingest",
+ "zone": "east",
+ }
+ initialBoundaries := map[string]*modelv1.TimeRange{
+ group1: timeRange1,
+ }
+
+ node1 := getDataNodeWithLabels("node1", addr1, initialLabels, initialBoundaries)
+ p.OnAddOrUpdate(node1)
+ verifyClients(p, 1, 0, 1, 0)
+
+ p.mu.RLock()
+ registeredNode := p.registered["node1"]
+ gomega.Expect(registeredNode.Labels).Should(gomega.Equal(initialLabels))
+ gomega.Expect(registeredNode.DataSegmentsBoundary).Should(gomega.Equal(initialBoundaries))
+ p.mu.RUnlock()
+
+ updatedLabels := map[string]string{
+ "role": "query",
+ "zone": "east",
+ "env": "prod",
+ }
+ updatedNode1 := getDataNodeWithLabels("node1", addr1, updatedLabels, initialBoundaries)
+ p.OnAddOrUpdate(updatedNode1)
+
+ p.mu.RLock()
+ registeredNode = p.registered["node1"]
+ gomega.Expect(registeredNode.Labels).Should(gomega.Equal(updatedLabels))
+ gomega.Expect(len(p.active)).Should(gomega.Equal(1))
+ p.mu.RUnlock()
+
+ timeRange2 := &modelv1.TimeRange{
+ Begin: timestamppb.New(now.Add(-1 * time.Hour)),
+ End: timestamppb.New(now),
+ }
+ updatedBoundaries := map[string]*modelv1.TimeRange{
+ group1: timeRange1,
+ "inventory": timeRange2,
+ }
+ updatedNode2 := getDataNodeWithLabels("node1", addr1, updatedLabels, updatedBoundaries)
+ p.OnAddOrUpdate(updatedNode2)
+
+ p.mu.RLock()
+ registeredNode = p.registered["node1"]
+ gomega.Expect(registeredNode.DataSegmentsBoundary).Should(gomega.Equal(updatedBoundaries))
+ gomega.Expect(len(p.active)).Should(gomega.Equal(1))
+ p.mu.RUnlock()
+ })
})
func verifyClients(p *pub, active, evict, onAdd, onDelete int) {
diff --git a/banyand/queue/pub/pub.go b/banyand/queue/pub/pub.go
index 8640264d..98208dfc 100644
--- a/banyand/queue/pub/pub.go
+++ b/banyand/queue/pub/pub.go
@@ -43,6 +43,7 @@ import (
"github.com/apache/skywalking-banyandb/pkg/bus"
"github.com/apache/skywalking-banyandb/pkg/logger"
"github.com/apache/skywalking-banyandb/pkg/run"
+ "github.com/apache/skywalking-banyandb/pkg/timestamp"
)
var (
@@ -86,16 +87,54 @@ func (p *pub) Serve() run.StopNotify {
return p.closer.CloseNotify()
}
+func bypassMatches(_ map[string]string) bool { return true }
+
func (p *pub) Broadcast(timeout time.Duration, topic bus.Topic, messages bus.Message) ([]bus.Future, error) {
- var names []string
+ var nodes []*databasev1.Node
p.mu.RLock()
for k := range p.active {
- names = append(names, k)
+ nodes = append(nodes, p.registered[k])
}
p.mu.RUnlock()
+ if len(nodes) == 0 {
+ return nil, errors.New("no active nodes")
+ }
+ names := make(map[string]struct{})
+ if len(messages.NodeSelectors()) == 0 {
+ for _, n := range nodes {
+ names[n.Metadata.Name] = struct{}{}
+ }
+ } else {
+ for g, sel := range messages.NodeSelectors() {
+ var matches func(map[string]string) bool
+ if sel == "" {
+ matches = bypassMatches
+ } else {
+ selector, err := parseLabelSelector(sel)
+ if err != nil {
+ return nil, fmt.Errorf("failed to parse node selector: %w", err)
+ }
+ matches = selector.matches
+ }
+ for _, n := range nodes {
+ tr := metadata.FindSegmentsBoundary(n, g)
+ if tr == nil {
+ continue
+ }
+ if matches(n.Labels) && timestamp.PbHasOverlap(messages.TimeRange(), tr) {
+ names[n.Metadata.Name] = struct{}{}
+ }
+ }
+ }
+ }
+
+ if l := p.log.Debug(); l.Enabled() {
+ l.Msgf("broadcasting message to %s nodes", names)
+ }
+
futureCh := make(chan publishResult, len(names))
var wg sync.WaitGroup
- for _, n := range names {
+ for n := range names {
wg.Add(1)
go func(n string) {
defer wg.Done()
diff --git a/banyand/queue/pub/pub_suite_test.go b/banyand/queue/pub/pub_suite_test.go
index b20a8574..50ccfcc8 100644
--- a/banyand/queue/pub/pub_suite_test.go
+++ b/banyand/queue/pub/pub_suite_test.go
@@ -237,3 +237,11 @@ func getDataNode(name string, address string) schema.Metadata {
},
}
}
+
+func getDataNodeWithLabels(name string, address string, labels map[string]string, dataBoundary map[string]*modelv1.TimeRange) schema.Metadata {
+ node := getDataNode(name, address)
+ nodeInfo := node.Spec.(*databasev1.Node)
+ nodeInfo.Labels = labels
+ nodeInfo.DataSegmentsBoundary = dataBoundary
+ return node
+}
diff --git a/banyand/queue/pub/pub_test.go b/banyand/queue/pub/pub_test.go
index 2e90d132..9abb59f2 100644
--- a/banyand/queue/pub/pub_test.go
+++ b/banyand/queue/pub/pub_test.go
@@ -28,6 +28,7 @@ import (
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
+ "google.golang.org/protobuf/types/known/timestamppb"
"github.com/apache/skywalking-banyandb/api/common"
"github.com/apache/skywalking-banyandb/api/data"
@@ -286,5 +287,285 @@ var _ = ginkgo.Describe("Publish and Broadcast", func() {
}
ginkgo.Fail("should not reach here")
})
+
+ ginkgo.It("should broadcast messages to nodes with empty selector", func() {
+ addr1 := getAddress()
+ addr2 := getAddress()
+ closeFn1 := setup(addr1, codes.OK, 200*time.Millisecond)
+ closeFn2 := setup(addr2, codes.OK, 10*time.Millisecond)
+ p := newPub()
+ defer func() {
+ p.GracefulStop()
+ closeFn1()
+ closeFn2()
+ }()
+
+ group1 := svc
+ timeRange := &modelv1.TimeRange{
+ Begin: timestamppb.New(time.Now().Add(-time.Hour)),
+ End: timestamppb.New(time.Now()),
+ }
+
+ node1Labels := map[string]string{
+ "role": "ingest",
+ "zone": "east",
+ }
+ node1Boundaries := map[string]*modelv1.TimeRange{
+ group1: timeRange,
+ }
+
+ node2Labels := map[string]string{
+ "role": "query",
+ "zone": "west",
+ }
+ node2Boundaries := map[string]*modelv1.TimeRange{
+ group1: timeRange,
+ }
+
+ node1 := getDataNodeWithLabels("node1", addr1, node1Labels, node1Boundaries)
+ p.OnAddOrUpdate(node1)
+ node2 := getDataNodeWithLabels("node2", addr2, node2Labels, node2Boundaries)
+ p.OnAddOrUpdate(node2)
+
+ nodeSelectors := map[string]string{
+ group1: "",
+ }
+
+ ff, err := p.Broadcast(3*time.Second, data.TopicStreamQuery,
+ bus.NewMessageWithNodeSelectors(bus.MessageID(1), nodeSelectors, timeRange, &streamv1.QueryRequest{}))
+
+ gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
+ // Both nodes should be selected
+ gomega.Expect(ff).Should(gomega.HaveLen(2))
+
+ for _, f := range ff {
+ messages, err := f.GetAll()
+ gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
+ gomega.Expect(messages).Should(gomega.HaveLen(1))
+ }
+ })
+
+ ginkgo.It("should broadcast messages to nodes with specific label selector", func() {
+ addr1 := getAddress()
+ addr2 := getAddress()
+ closeFn1 := setup(addr1, codes.OK, 200*time.Millisecond)
+ closeFn2 := setup(addr2, codes.OK, 10*time.Millisecond)
+ p := newPub()
+ defer func() {
+ p.GracefulStop()
+ closeFn1()
+ closeFn2()
+ }()
+
+ group1 := svc
+ timeRange := &modelv1.TimeRange{
+ Begin: timestamppb.New(time.Now().Add(-time.Hour)),
+ End: timestamppb.New(time.Now()),
+ }
+
+ node1Labels := map[string]string{
+ "role": "ingest",
+ "zone": "east",
+ }
+ node1Boundaries := map[string]*modelv1.TimeRange{
+ group1: timeRange,
+ }
+
+ node2Labels := map[string]string{
+ "role": "query",
+ "zone": "west",
+ }
+ node2Boundaries := map[string]*modelv1.TimeRange{
+ group1: timeRange,
+ }
+
+ node1 := getDataNodeWithLabels("node1", addr1, node1Labels, node1Boundaries)
+ p.OnAddOrUpdate(node1)
+ node2 := getDataNodeWithLabels("node2", addr2, node2Labels, node2Boundaries)
+ p.OnAddOrUpdate(node2)
+
+ nodeSelectors := map[string]string{
+ group1: "role=ingest",
+ }
+
+ ff, err := p.Broadcast(3*time.Second, data.TopicStreamQuery,
+ bus.NewMessageWithNodeSelectors(bus.MessageID(1), nodeSelectors, timeRange, &streamv1.QueryRequest{}))
+
+ gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
+ gomega.Expect(ff).Should(gomega.HaveLen(1))
+
+ messages, err := ff[0].GetAll()
+ gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
+ gomega.Expect(messages).Should(gomega.HaveLen(1))
+ })
+
+ ginkgo.It("should broadcast messages based on time range overlap", func() {
+ addr1 := getAddress()
+ addr2 := getAddress()
+ addr3 := getAddress()
+ closeFn1 := setup(addr1, codes.OK, 200*time.Millisecond)
+ closeFn2 := setup(addr2, codes.OK, 10*time.Millisecond)
+ closeFn3 := setup(addr3, codes.OK, 10*time.Millisecond)
+ p := newPub()
+ defer func() {
+ p.GracefulStop()
+ closeFn1()
+ closeFn2()
+ closeFn3()
+ }()
+
+ group1 := svc
+
+ now := time.Now()
+ timeRange1 := &modelv1.TimeRange{
+ Begin: timestamppb.New(now.Add(-3 * time.Hour)),
+ End: timestamppb.New(now.Add(-2 * time.Hour)),
+ }
+
+ timeRange2 := &modelv1.TimeRange{
+ Begin: timestamppb.New(now.Add(-2 * time.Hour)),
+ End: timestamppb.New(now.Add(-1 * time.Hour)),
+ }
+
+ timeRange3 := &modelv1.TimeRange{
+ Begin: timestamppb.New(now.Add(-1 * time.Hour)),
+ End: timestamppb.New(now),
+ }
+
+ node1Labels := map[string]string{"zone": "east"}
+ node1Boundaries := map[string]*modelv1.TimeRange{
+ group1: timeRange1,
+ }
+
+ node2Labels := map[string]string{"zone": "east"}
+ node2Boundaries := map[string]*modelv1.TimeRange{
+ group1: timeRange2,
+ }
+
+ node3Labels := map[string]string{"zone": "east"}
+ node3Boundaries := map[string]*modelv1.TimeRange{
+ group1: timeRange3,
+ }
+
+ node1 := getDataNodeWithLabels("node1", addr1, node1Labels, node1Boundaries)
+ p.OnAddOrUpdate(node1)
+ node2 := getDataNodeWithLabels("node2", addr2, node2Labels, node2Boundaries)
+ p.OnAddOrUpdate(node2)
+ node3 := getDataNodeWithLabels("node3", addr3, node3Labels, node3Boundaries)
+ p.OnAddOrUpdate(node3)
+
+ queryTimeRange := &modelv1.TimeRange{
+ Begin: timestamppb.New(now.Add(-2 * time.Hour).Add(-30 * time.Minute)),
+ End: timestamppb.New(now.Add(-1 * time.Hour).Add(-30 * time.Minute)),
+ }
+
+ nodeSelectors := map[string]string{
+ group1: "",
+ }
+
+ ff, err := p.Broadcast(3*time.Second, data.TopicStreamQuery,
+ bus.NewMessageWithNodeSelectors(bus.MessageID(1), nodeSelectors, queryTimeRange, &streamv1.QueryRequest{}))
+
+ gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
+ gomega.Expect(ff).Should(gomega.HaveLen(2))
+
+ for _, f := range ff {
+ messages, err := f.GetAll()
+ gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
+ gomega.Expect(messages).Should(gomega.HaveLen(1))
+ }
+ })
+
+ ginkgo.It("should broadcast messages with both label selector and time range filter", func() {
+ addr1 := getAddress()
+ addr2 := getAddress()
+ addr3 := getAddress()
+ addr4 := getAddress()
+ closeFn1 := setup(addr1, codes.OK, 200*time.Millisecond)
+ closeFn2 := setup(addr2, codes.OK, 10*time.Millisecond)
+ closeFn3 := setup(addr3, codes.OK, 10*time.Millisecond)
+ closeFn4 := setup(addr4, codes.OK, 10*time.Millisecond)
+ p := newPub()
+ defer func() {
+ p.GracefulStop()
+ closeFn1()
+ closeFn2()
+ closeFn3()
+ closeFn4()
+ }()
+
+ group1 := svc
+
+ now := time.Now()
+ timeRange1 := &modelv1.TimeRange{
+ Begin: timestamppb.New(now.Add(-2 * time.Hour)),
+ End: timestamppb.New(now.Add(-1 * time.Hour)),
+ }
+
+ timeRange2 := &modelv1.TimeRange{
+ Begin: timestamppb.New(now.Add(-1 * time.Hour)),
+ End: timestamppb.New(now),
+ }
+
+ node1Labels := map[string]string{
+ "env": "prod",
+ "zone": "east",
+ }
+ node1Boundaries := map[string]*modelv1.TimeRange{
+ group1: timeRange1,
+ }
+
+ node2Labels := map[string]string{
+ "env": "prod",
+ "zone": "west",
+ }
+ node2Boundaries := map[string]*modelv1.TimeRange{
+ group1: timeRange1,
+ }
+
+ node3Labels := map[string]string{
+ "env": "dev",
+ "zone": "east",
+ }
+ node3Boundaries := map[string]*modelv1.TimeRange{
+ group1: timeRange2,
+ }
+
+ node4Labels := map[string]string{
+ "env": "prod",
+ "zone": "east",
+ }
+ node4Boundaries := map[string]*modelv1.TimeRange{
+ group1: timeRange2,
+ }
+
+ node1 := getDataNodeWithLabels("node1", addr1, node1Labels, node1Boundaries)
+ p.OnAddOrUpdate(node1)
+ node2 := getDataNodeWithLabels("node2", addr2, node2Labels, node2Boundaries)
+ p.OnAddOrUpdate(node2)
+ node3 := getDataNodeWithLabels("node3", addr3, node3Labels, node3Boundaries)
+ p.OnAddOrUpdate(node3)
+ node4 := getDataNodeWithLabels("node4", addr4, node4Labels, node4Boundaries)
+ p.OnAddOrUpdate(node4)
+
+ queryTimeRange := &modelv1.TimeRange{
+ Begin: timestamppb.New(now.Add(-30 * time.Minute)),
+ End: timestamppb.New(now.Add(30 * time.Minute)),
+ }
+
+ nodeSelectors := map[string]string{
+ group1: "env=prod",
+ }
+
+ ff, err := p.Broadcast(3*time.Second, data.TopicStreamQuery,
+ bus.NewMessageWithNodeSelectors(bus.MessageID(1), nodeSelectors, queryTimeRange, &streamv1.QueryRequest{}))
+
+ gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
+ gomega.Expect(ff).Should(gomega.HaveLen(1))
+
+ messages, err := ff[0].GetAll()
+ gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
+ gomega.Expect(messages).Should(gomega.HaveLen(1))
+ })
})
})
diff --git a/banyand/queue/pub/selector.go b/banyand/queue/pub/selector.go
new file mode 100644
index 00000000..b49edac1
--- /dev/null
+++ b/banyand/queue/pub/selector.go
@@ -0,0 +1,232 @@
+// 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 pub
+
+import (
+ "errors"
+ "fmt"
+ "strings"
+ "unicode"
+)
+
+type labelSelector struct {
+ criteria []condition
+}
+
+type condition struct {
+ Key string
+ Values []string
+ Op operator
+}
+
+type operator int
+
+const (
+ opEquals operator = iota
+ opNotEquals
+ opIn
+ opNotIn
+ opExists
+ opNotExists
+)
+
+func parseLabelSelector(selector string) (*labelSelector, error) {
+ if selector == "" {
+ return &labelSelector{}, nil
+ }
+
+ var conditions []condition
+ for _, expr := range splitSelector(selector) {
+ expr = strings.TrimSpace(expr)
+ if expr == "" {
+ continue
+ }
+
+ cond, err := parseCondition(expr)
+ if err != nil {
+ return nil, fmt.Errorf("invalid selector %q: %w", selector, err)
+ }
+ conditions = append(conditions, cond)
+ }
+
+ return &labelSelector{conditions}, nil
+}
+
+func splitSelector(selector string) []string {
+ var exprs []string
+ var current []rune
+ parenLevel := 0
+ for _, r := range selector {
+ switch r {
+ case '(':
+ parenLevel++
+ case ')':
+ if parenLevel > 0 {
+ parenLevel--
+ }
+ case ',':
+ if parenLevel == 0 {
+ exprs = append(exprs, strings.TrimSpace(string(current)))
+ current = []rune{}
+ continue
+ }
+ }
+ current = append(current, r)
+ }
+ if len(current) > 0 {
+ exprs = append(exprs, strings.TrimSpace(string(current)))
+ }
+ return exprs
+}
+
+func parseCondition(expr string) (condition, error) {
+ if strings.HasPrefix(expr, "!") {
+ key := strings.TrimSpace(expr[1:])
+ if err := validateLabelKey(key); err != nil {
+ return condition{}, err
+ }
+ return condition{Key: key, Op: opNotExists}, nil
+ }
+
+ var op operator
+ var key, valuesStr string
+
+ switch {
+ case strings.Contains(expr, "!="):
+ parts := strings.SplitN(expr, "!=", 2)
+ key = strings.TrimSpace(parts[0])
+ valuesStr = strings.TrimSpace(parts[1])
+ op = opNotEquals
+ case strings.Contains(expr, "="):
+ parts := strings.SplitN(expr, "=", 2)
+ key = strings.TrimSpace(parts[0])
+ valuesStr = strings.TrimSpace(parts[1])
+ op = opEquals
+ case strings.Contains(expr, " notin "):
+ parts := strings.SplitN(expr, " notin ", 2)
+ key = strings.TrimSpace(parts[0])
+ valuesStr = strings.TrimSpace(parts[1])
+ op = opNotIn
+ case strings.Contains(expr, " in "):
+ parts := strings.SplitN(expr, " in ", 2)
+ key = strings.TrimSpace(parts[0])
+ valuesStr = strings.TrimSpace(parts[1])
+ op = opIn
+ default:
+ if err := validateLabelKey(expr); err != nil {
+ return condition{}, err
+ }
+ return condition{Key: expr, Op: opExists}, nil
+ }
+
+ if err := validateLabelKey(key); err != nil {
+ return condition{}, err
+ }
+
+ values, err := parseValues(valuesStr, op)
+ if err != nil {
+ return condition{}, err
+ }
+
+ return condition{
+ Key: key,
+ Op: op,
+ Values: values,
+ }, nil
+}
+
+func parseValues(valuesStr string, op operator) ([]string, error) {
+ if op == opIn || op == opNotIn {
+ if !strings.HasPrefix(valuesStr, "(") || !strings.HasSuffix(valuesStr, ")") {
+ return nil, fmt.Errorf("set operations require parenthesized values")
+ }
+ valuesStr = strings.Trim(valuesStr, "()")
+ }
+
+ var values []string
+ for _, v := range strings.Split(valuesStr, ",") {
+ v = strings.TrimSpace(v)
+ if v == "" {
+ continue
+ }
+ values = append(values, v)
+ }
+
+ if len(values) == 0 {
+ return nil, errors.New("empty value list")
+ }
+ return values, nil
+}
+
+func validateLabelKey(key string) error {
+ if key == "" {
+ return errors.New("empty key")
+ }
+ for _, c := range key {
+ if !unicode.IsLower(c) && !unicode.IsDigit(c) && c != '-' && c != '_' && c != '.' {
+ return fmt.Errorf("invalid key %q: must match [a-z0-9-_.]", key)
+ }
+ }
+ return nil
+}
+
+func (s *labelSelector) matches(labels map[string]string) bool {
+ for _, req := range s.criteria {
+ if !req.matches(labels) {
+ return false
+ }
+ }
+ return true
+}
+
+func (r condition) matches(labels map[string]string) bool {
+ value, exists := labels[r.Key]
+
+ switch r.Op {
+ case opExists:
+ return exists
+ case opNotExists:
+ return !exists
+ case opEquals:
+ return exists && value == r.Values[0]
+ case opNotEquals:
+ return !exists || value != r.Values[0]
+ case opIn:
+ if !exists {
+ return false
+ }
+ for _, v := range r.Values {
+ if v == value {
+ return true
+ }
+ }
+ return false
+ case opNotIn:
+ if !exists {
+ return true
+ }
+ for _, v := range r.Values {
+ if v == value {
+ return false
+ }
+ }
+ return true
+ default:
+ return false
+ }
+}
diff --git a/banyand/queue/pub/selector_test.go b/banyand/queue/pub/selector_test.go
new file mode 100644
index 00000000..382f4886
--- /dev/null
+++ b/banyand/queue/pub/selector_test.go
@@ -0,0 +1,347 @@
+// 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 pub
+
+import (
+ "testing"
+)
+
+func TestParseLabelSelector(t *testing.T) {
+ tests := []struct {
+ want *labelSelector
+ name string
+ input string
+ wantErr bool
+ }{
+ {
+ name: "empty selector",
+ input: "",
+ want: &labelSelector{},
+ },
+ {
+ name: "single equality",
+ input: "key=value",
+ want: &labelSelector{
+ criteria: []condition{
+ {Key: "key", Op: opEquals, Values: []string{"value"}},
+ },
+ },
+ },
+ {
+ name: "not equals",
+ input: "key != value",
+ want: &labelSelector{
+ criteria: []condition{
+ {Key: "key", Op: opNotEquals, Values: []string{"value"}},
+ },
+ },
+ },
+ {
+ name: "in clause",
+ input: "key in (v1, v2)",
+ want: &labelSelector{
+ criteria: []condition{
+ {Key: "key", Op: opIn, Values: []string{"v1", "v2"}},
+ },
+ },
+ },
+ {
+ name: "not in clause",
+ input: "key notin (v1,v2)",
+ want: &labelSelector{
+ criteria: []condition{
+ {Key: "key", Op: opNotIn, Values: []string{"v1", "v2"}},
+ },
+ },
+ },
+ {
+ name: "exists",
+ input: "key",
+ want: &labelSelector{
+ criteria: []condition{
+ {Key: "key", Op: opExists},
+ },
+ },
+ },
+ {
+ name: "not exists",
+ input: "!key",
+ want: &labelSelector{
+ criteria: []condition{
+ {Key: "key", Op: opNotExists},
+ },
+ },
+ },
+ {
+ name: "multiple conditions",
+ input: "key1=value1,key2 in (v1, v2),!key3",
+ want: &labelSelector{
+ criteria: []condition{
+ {Key: "key1", Op: opEquals, Values: []string{"value1"}},
+ {Key: "key2", Op: opIn, Values: []string{"v1", "v2"}},
+ {Key: "key3", Op: opNotExists},
+ },
+ },
+ },
+ {
+ name: "invalid key",
+ input: "INVALID=value",
+ wantErr: true,
+ },
+ {
+ name: "invalid operator",
+ input: "key~value",
+ wantErr: true,
+ },
+ {
+ name: "in without parentheses",
+ input: "key in v1,v2",
+ wantErr: true,
+ },
+ {
+ name: "empty value list",
+ input: "key in ()",
+ wantErr: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := parseLabelSelector(tt.input)
+ if (err != nil) != tt.wantErr {
+ t.Fatalf("parseLabelSelector() error = %v, wantErr %v", err, tt.wantErr)
+ }
+ if tt.wantErr {
+ return
+ }
+ if len(got.criteria) != len(tt.want.criteria) {
+ t.Fatalf("expected %d conditions, got %d", len(tt.want.criteria), len(got.criteria))
+ }
+ for i, cond := range got.criteria {
+ wantCond := tt.want.criteria[i]
+ if cond.Key != wantCond.Key || cond.Op != wantCond.Op {
+ t.Errorf("condition %d: got (%s, %d), want (%s, %d)", i, cond.Key, cond.Op, wantCond.Key, wantCond.Op)
+ }
+ if len(cond.Values) != len(wantCond.Values) {
+ t.Errorf("condition %d: values length mismatch: got %d, want %d", i, len(cond.Values), len(wantCond.Values))
+ continue
+ }
+ for j, v := range cond.Values {
+ if v != wantCond.Values[j] {
+ t.Errorf("condition %d value %d: got %s, want %s", i, j, v, wantCond.Values[j])
+ }
+ }
+ }
+ })
+ }
+}
+
+func TestValidateLabelKey(t *testing.T) {
+ tests := []struct {
+ key string
+ wantErr bool
+ }{
+ {"valid-key", false},
+ {"key.with.dots", false},
+ {"key_with_underscore", false},
+ {"123key", false},
+ {"", true},
+ {"InvalidKey", true}, // uppercase
+ {"key/with/slash", true}, // invalid character
+ {"key with space", true},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.key, func(t *testing.T) {
+ err := validateLabelKey(tt.key)
+ if (err != nil) != tt.wantErr {
+ t.Errorf("validateLabelKey(%q) error = %v, wantErr %v", tt.key, err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestConditionMatches(t *testing.T) {
+ tests := []struct {
+ labels map[string]string
+ name string
+ condition condition
+ want bool
+ }{
+ {
+ name: "exists true",
+ condition: condition{Key: "k", Op: opExists},
+ labels: map[string]string{"k": "v"},
+ want: true,
+ },
+ {
+ name: "exists false",
+ condition: condition{Key: "k", Op: opExists},
+ labels: map[string]string{"other": "v"},
+ want: false,
+ },
+ {
+ name: "not exists true",
+ condition: condition{Key: "k", Op: opNotExists},
+ labels: map[string]string{"other": "v"},
+ want: true,
+ },
+ {
+ name: "not exists false",
+ condition: condition{Key: "k", Op: opNotExists},
+ labels: map[string]string{"k": "v"},
+ want: false,
+ },
+ {
+ name: "equals match",
+ condition: condition{Key: "k", Op: opEquals, Values: []string{"v"}},
+ labels: map[string]string{"k": "v"},
+ want: true,
+ },
+ {
+ name: "equals no match",
+ condition: condition{Key: "k", Op: opEquals, Values: []string{"v"}},
+ labels: map[string]string{"k": "x"},
+ want: false,
+ },
+ {
+ name: "equals key missing",
+ condition: condition{Key: "k", Op: opEquals, Values: []string{"v"}},
+ labels: map[string]string{"other": "v"},
+ want: false,
+ },
+ {
+ name: "not equals match",
+ condition: condition{Key: "k", Op: opNotEquals, Values: []string{"v"}},
+ labels: map[string]string{"k": "x"},
+ want: true,
+ },
+ {
+ name: "not equals key missing",
+ condition: condition{Key: "k", Op: opNotEquals, Values: []string{"v"}},
+ labels: map[string]string{"other": "v"},
+ want: true,
+ },
+ {
+ name: "not equals no match",
+ condition: condition{Key: "k", Op: opNotEquals, Values: []string{"v"}},
+ labels: map[string]string{"k": "v"},
+ want: false,
+ },
+ {
+ name: "in match",
+ condition: condition{Key: "k", Op: opIn, Values: []string{"v1", "v2"}},
+ labels: map[string]string{"k": "v1"},
+ want: true,
+ },
+ {
+ name: "in no match",
+ condition: condition{Key: "k", Op: opIn, Values: []string{"v1", "v2"}},
+ labels: map[string]string{"k": "v3"},
+ want: false,
+ },
+ {
+ name: "in key missing",
+ condition: condition{Key: "k", Op: opIn, Values: []string{"v1", "v2"}},
+ labels: map[string]string{"other": "v1"},
+ want: false,
+ },
+ {
+ name: "not in match",
+ condition: condition{Key: "k", Op: opNotIn, Values: []string{"v1", "v2"}},
+ labels: map[string]string{"k": "v3"},
+ want: true,
+ },
+ {
+ name: "not in no match",
+ condition: condition{Key: "k", Op: opNotIn, Values: []string{"v1", "v2"}},
+ labels: map[string]string{"k": "v1"},
+ want: false,
+ },
+ {
+ name: "not in key missing",
+ condition: condition{Key: "k", Op: opNotIn, Values: []string{"v1", "v2"}},
+ labels: map[string]string{"other": "v1"},
+ want: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := tt.condition.matches(tt.labels)
+ if got != tt.want {
+ t.Errorf("matches() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestLabelSelectorMatches(t *testing.T) {
+ tests := []struct {
+ selector *labelSelector
+ labels map[string]string
+ name string
+ want bool
+ }{
+ {
+ name: "all conditions met",
+ selector: &labelSelector{
+ criteria: []condition{
+ {Key: "k1", Op: opExists},
+ {Key: "k2", Op: opEquals, Values: []string{"v2"}},
+ {Key: "k3", Op: opNotIn, Values: []string{"v3"}},
+ },
+ },
+ labels: map[string]string{
+ "k1": "v1",
+ "k2": "v2",
+ "k3": "v4",
+ },
+ want: true,
+ },
+ {
+ name: "one condition fails",
+ selector: &labelSelector{
+ criteria: []condition{
+ {Key: "k1", Op: opExists},
+ {Key: "k2", Op: opEquals, Values: []string{"v2"}},
+ },
+ },
+ labels: map[string]string{
+ "k1": "v1",
+ "k2": "v3",
+ },
+ want: false,
+ },
+ {
+ name: "empty selector",
+ selector: &labelSelector{},
+ labels: map[string]string{"any": "value"},
+ want: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := tt.selector.matches(tt.labels)
+ if got != tt.want {
+ t.Errorf("matches() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
diff --git a/banyand/stream/metadata.go b/banyand/stream/metadata.go
index 8d1bea4a..7f5bd596 100644
--- a/banyand/stream/metadata.go
+++ b/banyand/stream/metadata.go
@@ -285,6 +285,7 @@ func (s *supplier) OpenDB(groupSchema *commonv1.Group) (resourceSchema.DB, error
SeriesIndexFlushTimeoutSeconds: s.option.flushTimeout.Nanoseconds() / int64(time.Second),
SeriesIndexCacheMaxBytes: int(s.option.seriesCacheMaxSize),
StorageMetricsFactory: s.omr.With(storageScope.ConstLabels(meter.ToLabelPairs(common.DBLabelNames(), p.DBLabelValues()))),
+ SegmentBoundaryUpdateFn: s.metadata.UpdateSegmentsBoundary,
}
return storage.OpenTSDB(
common.SetPosition(context.Background(), func(_ common.Position) common.Position {
diff --git a/docs/api-reference.md b/docs/api-reference.md
index 80554405..09e8eabb 100644
--- a/docs/api-reference.md
+++ b/docs/api-reference.md
@@ -17,6 +17,7 @@
- [banyandb/common/v1/common.proto](#banyandb_common_v1_common-proto)
- [Group](#banyandb-common-v1-Group)
- [IntervalRule](#banyandb-common-v1-IntervalRule)
+ - [LifecycleStage](#banyandb-common-v1-LifecycleStage)
- [Metadata](#banyandb-common-v1-Metadata)
- [ResourceOpts](#banyandb-common-v1-ResourceOpts)
@@ -35,12 +36,6 @@
- [Tag](#banyandb-common-v1-Tag)
- [Trace](#banyandb-common-v1-Trace)
-- [banyandb/database/v1/database.proto](#banyandb_database_v1_database-proto)
- - [Node](#banyandb-database-v1-Node)
- - [Shard](#banyandb-database-v1-Shard)
-
- - [Role](#banyandb-database-v1-Role)
-
- [banyandb/model/v1/common.proto](#banyandb_model_v1_common-proto)
- [FieldValue](#banyandb-model-v1-FieldValue)
- [Float](#banyandb-model-v1-Float)
@@ -70,6 +65,14 @@
- [LogicalExpression.LogicalOp](#banyandb-model-v1-LogicalExpression-LogicalOp)
- [Sort](#banyandb-model-v1-Sort)
+- [banyandb/database/v1/database.proto](#banyandb_database_v1_database-proto)
+ - [Node](#banyandb-database-v1-Node)
+ - [Node.DataSegmentsBoundaryEntry](#banyandb-database-v1-Node-DataSegmentsBoundaryEntry)
+ - [Node.LabelsEntry](#banyandb-database-v1-Node-LabelsEntry)
+ - [Shard](#banyandb-database-v1-Shard)
+
+ - [Role](#banyandb-database-v1-Role)
+
- [banyandb/database/v1/schema.proto](#banyandb_database_v1_schema-proto)
- [Entity](#banyandb-database-v1-Entity)
- [FieldSpec](#banyandb-database-v1-FieldSpec)
@@ -407,6 +410,26 @@ IntervalRule is a structured duration
+<a name="banyandb-common-v1-LifecycleStage"></a>
+
+### LifecycleStage
+
+
+
+| Field | Type | Label | Description |
+| ----- | ---- | ----- | ----------- |
+| name | [string](#string) | | The stage name (e.g., "warm", "cold"). This should be a non-empty string. |
+| shard_num | [uint32](#uint32) | | Number of shards allocated for this stage. Must be greater than zero. |
+| segment_interval | [IntervalRule](#banyandb-common-v1-IntervalRule) | | Defines the interval for data segmentation in this stage. This is a required field and uses the IntervalRule structure. |
+| ttl | [IntervalRule](#banyandb-common-v1-IntervalRule) | | Specifies the time-to-live for data in this stage before moving to the next. This is also a required field using the IntervalRule structure. |
+| node_selector | [string](#string) | | Node selector specifying target nodes for this stage. Optional; if provided, it must be a non-empty string. |
+| close | [bool](#bool) | | Indicates whether segments that are no longer live should be closed. |
+
+
+
+
+
+
<a name="banyandb-common-v1-Metadata"></a>
### Metadata
@@ -437,6 +460,8 @@ Metadata is for multi-tenant, multi-model use
| shard_num | [uint32](#uint32) | | shard_num is the number of shards |
| segment_interval | [IntervalRule](#banyandb-common-v1-IntervalRule) | | segment_interval indicates the length of a segment |
| ttl | [IntervalRule](#banyandb-common-v1-IntervalRule) | | ttl indicates time to live, how long the data will be cached |
+| stages | [LifecycleStage](#banyandb-common-v1-LifecycleStage) | repeated | stages defines the ordered lifecycle stages. Data progresses through these stages sequentially. |
+| default_node_selector | [string](#string) | | default_node_selector is the default node selector for queries if node_selector is not specified |
@@ -618,76 +643,6 @@ Trace is the top level message of a trace.
-<a name="banyandb_database_v1_database-proto"></a>
-<p align="right"><a href="#top">Top</a></p>
-
-## banyandb/database/v1/database.proto
-
-
-
-<a name="banyandb-database-v1-Node"></a>
-
-### Node
-
-
-
-| Field | Type | Label | Description |
-| ----- | ---- | ----- | ----------- |
-| metadata | [banyandb.common.v1.Metadata](#banyandb-common-v1-Metadata) | | |
-| roles | [Role](#banyandb-database-v1-Role) | repeated | |
-| grpc_address | [string](#string) | | |
-| http_address | [string](#string) | | |
-| created_at | [google.protobuf.Timestamp](#google-protobuf-Timestamp) | | |
-
-
-
-
-
-
-<a name="banyandb-database-v1-Shard"></a>
-
-### Shard
-
-
-
-| Field | Type | Label | Description |
-| ----- | ---- | ----- | ----------- |
-| id | [uint64](#uint64) | | |
-| metadata | [banyandb.common.v1.Metadata](#banyandb-common-v1-Metadata) | | |
-| catalog | [banyandb.common.v1.Catalog](#banyandb-common-v1-Catalog) | | |
-| node | [string](#string) | | |
-| total | [uint32](#uint32) | | |
-| updated_at | [google.protobuf.Timestamp](#google-protobuf-Timestamp) | | |
-| created_at | [google.protobuf.Timestamp](#google-protobuf-Timestamp) | | |
-
-
-
-
-
-
-
-
-<a name="banyandb-database-v1-Role"></a>
-
-### Role
-
-
-| Name | Number | Description |
-| ---- | ------ | ----------- |
-| ROLE_UNSPECIFIED | 0 | |
-| ROLE_META | 1 | |
-| ROLE_DATA | 2 | |
-| ROLE_LIAISON | 3 | |
-
-
-
-
-
-
-
-
-
-
<a name="banyandb_model_v1_common-proto"></a>
<p align="right"><a href="#top">Top</a></p>
@@ -1101,6 +1056,110 @@ Each item in a string array is seen as a token instead of a query expression.
+<a name="banyandb_database_v1_database-proto"></a>
+<p align="right"><a href="#top">Top</a></p>
+
+## banyandb/database/v1/database.proto
+
+
+
+<a name="banyandb-database-v1-Node"></a>
+
+### Node
+
+
+
+| Field | Type | Label | Description |
+| ----- | ---- | ----- | ----------- |
+| metadata | [banyandb.common.v1.Metadata](#banyandb-common-v1-Metadata) | | |
+| roles | [Role](#banyandb-database-v1-Role) | repeated | |
+| grpc_address | [string](#string) | | |
+| http_address | [string](#string) | | |
+| created_at | [google.protobuf.Timestamp](#google-protobuf-Timestamp) | | |
+| labels | [Node.LabelsEntry](#banyandb-database-v1-Node-LabelsEntry) | repeated | labels is a set of key-value pairs to describe the node. |
+| data_segments_boundary | [Node.DataSegmentsBoundaryEntry](#banyandb-database-v1-Node-DataSegmentsBoundaryEntry) | repeated | data_segments_boundary is the time range of the data segments that the node is responsible for. [start, end) is the time range. |
+
+
+
+
+
+
+<a name="banyandb-database-v1-Node-DataSegmentsBoundaryEntry"></a>
+
+### Node.DataSegmentsBoundaryEntry
+
+
+
+| Field | Type | Label | Description |
+| ----- | ---- | ----- | ----------- |
+| key | [string](#string) | | |
+| value | [banyandb.model.v1.TimeRange](#banyandb-model-v1-TimeRange) | | |
+
+
+
+
+
+
+<a name="banyandb-database-v1-Node-LabelsEntry"></a>
+
+### Node.LabelsEntry
+
+
+
+| Field | Type | Label | Description |
+| ----- | ---- | ----- | ----------- |
+| key | [string](#string) | | |
+| value | [string](#string) | | |
+
+
+
+
+
+
+<a name="banyandb-database-v1-Shard"></a>
+
+### Shard
+
+
+
+| Field | Type | Label | Description |
+| ----- | ---- | ----- | ----------- |
+| id | [uint64](#uint64) | | |
+| metadata | [banyandb.common.v1.Metadata](#banyandb-common-v1-Metadata) | | |
+| catalog | [banyandb.common.v1.Catalog](#banyandb-common-v1-Catalog) | | |
+| node | [string](#string) | | |
+| total | [uint32](#uint32) | | |
+| updated_at | [google.protobuf.Timestamp](#google-protobuf-Timestamp) | | |
+| created_at | [google.protobuf.Timestamp](#google-protobuf-Timestamp) | | |
+
+
+
+
+
+
+
+
+<a name="banyandb-database-v1-Role"></a>
+
+### Role
+
+
+| Name | Number | Description |
+| ---- | ------ | ----------- |
+| ROLE_UNSPECIFIED | 0 | |
+| ROLE_META | 1 | |
+| ROLE_DATA | 2 | |
+| ROLE_LIAISON | 3 | |
+
+
+
+
+
+
+
+
+
+
<a name="banyandb_database_v1_schema-proto"></a>
<p align="right"><a href="#top">Top</a></p>
@@ -2653,6 +2712,7 @@ QueryRequest is the request contract for query.
| limit | [uint32](#uint32) | | limit is used to impose a boundary on the number of records being returned. If top is specified, limit processes the dataset based on top's output |
| order_by | [banyandb.model.v1.QueryOrder](#banyandb-model-v1-QueryOrder) | | order_by is given to specify the sort for a tag. |
| trace | [bool](#bool) | | trace is used to enable trace for the query |
+| node_selector | [string](#string) | | node_selector is used to specify the target node for the query |
@@ -3145,6 +3205,7 @@ QueryRequest is the request contract for query.
| tag_projection | [string](#string) | repeated | tag_projection can be used to select tags of the data points in the response |
| limit | [uint32](#uint32) | | |
| trace | [bool](#bool) | | trace is used to enable trace for the query |
+| node_selector | [string](#string) | | node_selector is used to select the node to query |
@@ -3244,6 +3305,7 @@ QueryRequest is the request contract for query.
| criteria | [banyandb.model.v1.Criteria](#banyandb-model-v1-Criteria) | | tag_families are indexed. |
| projection | [banyandb.model.v1.TagProjection](#banyandb-model-v1-TagProjection) | | projection can be used to select the key names of the element in the response |
| trace | [bool](#bool) | | trace is used to enable trace for the query |
+| node_selector | [string](#string) | | node_selector is used to select the node to query |
diff --git a/pkg/bus/bus.go b/pkg/bus/bus.go
index 0966ed66..ccbd390a 100644
--- a/pkg/bus/bus.go
+++ b/pkg/bus/bus.go
@@ -29,6 +29,7 @@ import (
"go.uber.org/multierr"
"github.com/apache/skywalking-banyandb/api/common"
+ modelv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/model/v1"
)
type (
@@ -46,10 +47,12 @@ type (
// Message is send on the bus to all subscribed listeners.
type Message struct {
- payload payload
- node string
- id MessageID
- batchMode bool
+ payload payload
+ nodeSelectors map[string]string
+ timeRange *modelv1.TimeRange
+ node string
+ id MessageID
+ batchMode bool
}
// ID outputs the MessageID of the Message.
@@ -67,6 +70,16 @@ func (m Message) Node() string {
return m.node
}
+// NodeSelectors returns the node selectors of the Message.
+func (m Message) NodeSelectors() map[string]string {
+ return m.nodeSelectors
+}
+
+// TimeRange returns the time range of the Message.
+func (m Message) TimeRange() *modelv1.TimeRange {
+ return m.timeRange
+}
+
// BatchModeEnabled returns whether the Message is sent in batch mode.
func (m Message) BatchModeEnabled() bool {
return m.batchMode
@@ -87,6 +100,12 @@ func NewMessageWithNode(id MessageID, node string, data interface{}) Message {
return Message{id: id, node: node, payload: data}
}
+// NewMessageWithNodeSelectors returns a new Message with a MessageID and NodeSelectors and embed data.
+// Nodes matching any of the selectors will receive the message.
+func NewMessageWithNodeSelectors(id MessageID, nodeSelectors map[string]string, timeRange *modelv1.TimeRange, data interface{}) Message {
+ return Message{id: id, nodeSelectors: nodeSelectors, timeRange: timeRange, payload: data}
+}
+
// MessageListener is the signature of functions that can handle an EventMessage.
type MessageListener interface {
Rev(ctx context.Context, message Message) Message
diff --git a/pkg/cmdsetup/root.go b/pkg/cmdsetup/root.go
index 02e1eb4d..f0be0608 100644
--- a/pkg/cmdsetup/root.go
+++ b/pkg/cmdsetup/root.go
@@ -67,6 +67,7 @@ BanyanDB, as an observability database, aims to ingest, analyze and store Metric
cmd.PersistentFlags().Var(&nodeIDProviderValue{&common.FlagNodeHostProvider},
"node-host-provider", "the node host provider, can be hostname, ip or flag, default is hostname")
cmd.PersistentFlags().StringVar(&common.FlagNodeHost, "node-host", "", "the node host of the server only used when node-host-provider is \"flag\"")
+ cmd.PersistentFlags().StringSliceVar(&common.FlagNodeLabels, "node-labels", nil, "the node labels. e.g. key1=value1,key2=value2")
cmd.PersistentFlags().StringVar(&logging.Env, "logging-env", "prod", "the logging")
cmd.PersistentFlags().StringVar(&logging.Level, "logging-level", "info", "the root level of logging")
cmd.PersistentFlags().StringSliceVar(&logging.Modules, "logging-modules", nil, "the specific module")
diff --git a/pkg/timestamp/range.go b/pkg/timestamp/range.go
index f8db1652..6fc12460 100644
--- a/pkg/timestamp/range.go
+++ b/pkg/timestamp/range.go
@@ -19,6 +19,8 @@ package timestamp
import (
"time"
+
+ modelv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/model/v1"
)
// TimeRange is a range of periods into which data can be written or retrieved.
@@ -188,3 +190,31 @@ func Find(timestamps []int64, target int64) int {
}
return -1
}
+
+// PbHasOverlap checks if two TimeRanges have an overlap.
+// Returns true if they have at least one point in common, false otherwise.
+// Note: this follows [begin, end] (inclusive on both ends)
+//
+// other follows [begin, end) (inclusive begin, exclusive end)
+func PbHasOverlap(this, other *modelv1.TimeRange) bool {
+ if this == nil || other == nil {
+ return false
+ }
+
+ if this.Begin == nil || this.End == nil || other.Begin == nil || other.End == nil {
+ return false
+ }
+
+ thisBegin := this.Begin.GetSeconds()*1e9 + int64(this.Begin.GetNanos())
+ thisEnd := this.End.GetSeconds()*1e9 + int64(this.End.GetNanos())
+ otherBegin := other.Begin.GetSeconds()*1e9 + int64(other.Begin.GetNanos())
+ otherEnd := other.End.GetSeconds()*1e9 + int64(other.End.GetNanos())
+
+ // Check for overlap:
+ // - For x [begin, end] (inclusive on both ends)
+ // - For other [begin, end) (inclusive begin, exclusive end)
+ // They overlap when:
+ // thisBegin <= otherEnd-1 && thisEnd >= otherBegin
+ // Simplified to: thisBegin < otherEnd && thisEnd >= otherBegin
+ return thisBegin < otherEnd && thisEnd >= otherBegin
+}