ButterBright commented on code in PR #967: URL: https://github.com/apache/skywalking-banyandb/pull/967#discussion_r2776926068
########## banyand/internal/snapshot/snapshot.go: ########## @@ -0,0 +1,219 @@ +// 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 snapshot provides generic transaction coordination for snapshot-based systems. +// It enables atomic updates across multiple heterogeneous snapshot managers using Go generics. +package snapshot + +import ( + "reflect" + "sync" + + "github.com/apache/skywalking-banyandb/pkg/pool" +) + +// Snapshot is a type constraint for snapshot types that support reference counting. +// Any type implementing this interface can participate in atomic transactions. +type Snapshot interface { + // IncRef increments the reference count. + IncRef() + // DecRef decrements the reference count and releases resources when zero. + DecRef() +} + +// Manager is a generic interface for managing snapshots of type S. +// Both trace and sidx implement this interface for their respective snapshot types. +type Manager[S Snapshot] interface { + // CurrentSnapshot returns the current snapshot with incremented ref count. + // Returns nil if no snapshot exists. + CurrentSnapshot() S + // ReplaceSnapshot atomically replaces the current snapshot with next. + // The old snapshot's DecRef is called automatically. + ReplaceSnapshot(next S) +} + +// Transition represents a prepared but uncommitted snapshot change. +// It holds both the current and next snapshots, allowing atomic commit or rollback. +type Transition[S Snapshot] struct { + manager Manager[S] + current S + next S + committed bool +} + +// getTransitionPool returns or creates a pool for the given snapshot type. +func getTransitionPool[S Snapshot]() *pool.Synced[any] { Review Comment: It might register duplicated pools when there are concurrent calls. ########## banyand/internal/sidx/sidx.go: ########## @@ -248,6 +248,79 @@ func (s *sidx) currentSnapshot() *snapshot { return nil } +// CurrentSnapshot returns the current snapshot with incremented reference count. +// Implements snapshot.Manager[*Snapshot] interface. +func (s *sidx) CurrentSnapshot() *Snapshot { Review Comment: Duplicated with currentSnapshot above. -- 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]
