Fix handling of non-monotonic timestamps (#1304)
* Fix handling of non-monotonic timestamps Timed version is inspired by Hybrid Clock. We used to have a mixed behavior by using time.Time: * during local comparisons, it does increment monotonically * when deserializing remote timestamps, we lose that attribute So it's possible for two requests to be sent in the same microsecond, and for the latter one to be dropped. To fix that behavior, I'm switching it to keeping timestamps to consolidate that behavior, and accepting multiple updates in the same ms by incrementing ticks. Also using @paulwe's idea of a version generator.
This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/connectionquality"
|
||||
"github.com/livekit/livekit-server/pkg/telemetry"
|
||||
utils2 "github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/mediatransportutil/pkg/twcc"
|
||||
"github.com/livekit/protocol/auth"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
@@ -90,6 +91,7 @@ type ParticipantParams struct {
|
||||
GetParticipantInfo func(pID livekit.ParticipantID) *livekit.ParticipantInfo
|
||||
ReconnectOnPublicationError bool
|
||||
ReconnectOnSubscriptionError bool
|
||||
VersionGenerator utils2.TimedVersionGenerator
|
||||
}
|
||||
|
||||
type ParticipantImpl struct {
|
||||
@@ -1191,8 +1193,9 @@ func (p *ParticipantImpl) setupTransportManager() error {
|
||||
|
||||
func (p *ParticipantImpl) setupUpTrackManager() {
|
||||
p.UpTrackManager = NewUpTrackManager(UpTrackManagerParams{
|
||||
SID: p.params.SID,
|
||||
Logger: p.params.Logger,
|
||||
SID: p.params.SID,
|
||||
Logger: p.params.Logger,
|
||||
VersionGenerator: p.params.VersionGenerator,
|
||||
})
|
||||
|
||||
p.UpTrackManager.OnPublishedTrackUpdated(func(track types.MediaTrack, onlyIfReady bool) {
|
||||
|
||||
@@ -3,7 +3,6 @@ package rtc
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
@@ -17,10 +16,12 @@ var (
|
||||
)
|
||||
|
||||
type UpTrackManagerParams struct {
|
||||
SID livekit.ParticipantID
|
||||
Logger logger.Logger
|
||||
SID livekit.ParticipantID
|
||||
Logger logger.Logger
|
||||
VersionGenerator utils.TimedVersionGenerator
|
||||
}
|
||||
|
||||
// UpTrackManager manages all uptracks from a participant
|
||||
type UpTrackManager struct {
|
||||
params UpTrackManagerParams
|
||||
|
||||
@@ -203,6 +204,10 @@ func (u *UpTrackManager) UpdateSubscriptionPermission(
|
||||
) error {
|
||||
u.lock.Lock()
|
||||
if timedVersion != nil {
|
||||
// it's possible for permission updates to come from another node. In that case
|
||||
// they would be the authority for this participant's permissions
|
||||
// we do not want to initialize subscriptionPermissionVersion too early since if another machine is the
|
||||
// owner for the data, we'd prefer to use their TimedVersion
|
||||
if u.subscriptionPermissionVersion != nil {
|
||||
tv := utils.NewTimedVersionFromProto(timedVersion)
|
||||
// ignore older version
|
||||
@@ -221,16 +226,18 @@ func (u *UpTrackManager) UpdateSubscriptionPermission(
|
||||
u.lock.Unlock()
|
||||
return nil
|
||||
}
|
||||
u.subscriptionPermissionVersion.Update(time.UnixMicro(timedVersion.UnixMicro))
|
||||
u.subscriptionPermissionVersion.Update(tv)
|
||||
} else {
|
||||
u.subscriptionPermissionVersion = utils.NewTimedVersionFromProto(timedVersion)
|
||||
}
|
||||
} else {
|
||||
// for requests coming from the current node, use local versions
|
||||
tv := u.params.VersionGenerator.New()
|
||||
// use current time as the new/updated version
|
||||
if u.subscriptionPermissionVersion == nil {
|
||||
u.subscriptionPermissionVersion = utils.NewTimedVersion(time.Now(), 0)
|
||||
u.subscriptionPermissionVersion = tv
|
||||
} else {
|
||||
u.subscriptionPermissionVersion.Update(time.Now())
|
||||
u.subscriptionPermissionVersion.Update(tv)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
|
||||
@@ -13,7 +14,8 @@ import (
|
||||
)
|
||||
|
||||
var defaultUptrackManagerParams = UpTrackManagerParams{
|
||||
Logger: logger.GetLogger(),
|
||||
Logger: logger.GetLogger(),
|
||||
VersionGenerator: utils.NewDefaultTimedVersionGenerator(),
|
||||
}
|
||||
|
||||
func TestUpdateSubscriptionPermission(t *testing.T) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
|
||||
serverutils "github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/livekit-server/version"
|
||||
"github.com/livekit/protocol/auth"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
@@ -49,6 +50,7 @@ type RoomManager struct {
|
||||
telemetry telemetry.TelemetryService
|
||||
clientConfManager clientconfiguration.ClientConfigurationManager
|
||||
egressLauncher rtc.EgressLauncher
|
||||
versionGenerator serverutils.TimedVersionGenerator
|
||||
|
||||
rooms map[livekit.RoomName]*rtc.Room
|
||||
|
||||
@@ -63,8 +65,8 @@ func NewLocalRoomManager(
|
||||
telemetry telemetry.TelemetryService,
|
||||
clientConfManager clientconfiguration.ClientConfigurationManager,
|
||||
egressLauncher rtc.EgressLauncher,
|
||||
versionGenerator serverutils.TimedVersionGenerator,
|
||||
) (*RoomManager, error) {
|
||||
|
||||
rtcConf, err := rtc.NewWebRTCConfig(conf, currentNode.Ip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -79,6 +81,7 @@ func NewLocalRoomManager(
|
||||
telemetry: telemetry,
|
||||
clientConfManager: clientConfManager,
|
||||
egressLauncher: egressLauncher,
|
||||
versionGenerator: versionGenerator,
|
||||
|
||||
rooms: make(map[livekit.RoomName]*rtc.Room),
|
||||
|
||||
@@ -326,6 +329,7 @@ func (r *RoomManager) StartSession(
|
||||
},
|
||||
ReconnectOnPublicationError: reconnectOnPublicationError,
|
||||
ReconnectOnSubscriptionError: reconnectOnSubscriptionError,
|
||||
VersionGenerator: r.versionGenerator,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/service/rpc"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/protocol/auth"
|
||||
"github.com/livekit/protocol/egress"
|
||||
"github.com/livekit/protocol/ingress"
|
||||
@@ -62,6 +63,7 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
|
||||
NewLocalRoomManager,
|
||||
newTurnAuthHandler,
|
||||
newInProcessTurnServer,
|
||||
utils.NewDefaultTimedVersionGenerator,
|
||||
NewLivekitServer,
|
||||
)
|
||||
return &LivekitServer{}, nil
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/service/rpc"
|
||||
"github.com/livekit/livekit-server/pkg/telemetry"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/protocol/auth"
|
||||
"github.com/livekit/protocol/egress"
|
||||
"github.com/livekit/protocol/ingress"
|
||||
@@ -81,7 +82,8 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
|
||||
ingressService := NewIngressService(ingressConfig, nodeID, messageBus, ingressClient, ingressRPCClient, ingressStore, roomService, telemetryService)
|
||||
rtcService := NewRTCService(conf, roomAllocator, objectStore, router, currentNode, telemetryService)
|
||||
clientConfigurationManager := createClientConfiguration()
|
||||
roomManager, err := NewLocalRoomManager(conf, objectStore, currentNode, router, telemetryService, clientConfigurationManager, rtcEgressLauncher)
|
||||
timedVersionGenerator := utils.NewDefaultTimedVersionGenerator()
|
||||
roomManager, err := NewLocalRoomManager(conf, objectStore, currentNode, router, telemetryService, clientConfigurationManager, rtcEgressLauncher, timedVersionGenerator)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
+43
-17
@@ -7,33 +7,59 @@ import (
|
||||
"github.com/livekit/protocol/livekit"
|
||||
)
|
||||
|
||||
type TimedVersion struct {
|
||||
lock sync.RWMutex
|
||||
at time.Time
|
||||
type TimedVersionGenerator interface {
|
||||
New() *TimedVersion
|
||||
}
|
||||
|
||||
func NewDefaultTimedVersionGenerator() TimedVersionGenerator {
|
||||
return &timedVersionGenerator{}
|
||||
}
|
||||
|
||||
type timedVersionGenerator struct {
|
||||
lock sync.Mutex
|
||||
ts int64
|
||||
ticks int32
|
||||
}
|
||||
|
||||
func NewTimedVersion(at time.Time, ticks int32) *TimedVersion {
|
||||
func (g *timedVersionGenerator) New() *TimedVersion {
|
||||
ts := time.Now().UnixMicro()
|
||||
var ticks int32
|
||||
|
||||
g.lock.Lock()
|
||||
if ts <= g.ts {
|
||||
g.ticks++
|
||||
ts = g.ts
|
||||
ticks = g.ticks
|
||||
} else {
|
||||
g.ts = ts
|
||||
g.ticks = 0
|
||||
}
|
||||
g.lock.Unlock()
|
||||
|
||||
return &TimedVersion{
|
||||
at: at,
|
||||
ts: ts,
|
||||
ticks: ticks,
|
||||
}
|
||||
}
|
||||
|
||||
func NewTimedVersionFromProto(proto *livekit.TimedVersion) *TimedVersion {
|
||||
type TimedVersion struct {
|
||||
lock sync.RWMutex
|
||||
ts int64
|
||||
ticks int32
|
||||
}
|
||||
|
||||
func NewTimedVersionFromProto(ptv *livekit.TimedVersion) *TimedVersion {
|
||||
return &TimedVersion{
|
||||
at: time.UnixMicro(proto.UnixMicro),
|
||||
ticks: proto.Ticks,
|
||||
ts: ptv.UnixMicro,
|
||||
ticks: ptv.Ticks,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TimedVersion) Update(at time.Time) {
|
||||
func (t *TimedVersion) Update(other *TimedVersion) {
|
||||
t.lock.Lock()
|
||||
if at.After(t.at) {
|
||||
t.at = at
|
||||
t.ticks = 0
|
||||
} else {
|
||||
t.ticks++
|
||||
if other.After(t) {
|
||||
t.ts = other.ts
|
||||
t.ticks = other.ticks
|
||||
}
|
||||
t.lock.Unlock()
|
||||
}
|
||||
@@ -42,11 +68,11 @@ func (t *TimedVersion) After(other *TimedVersion) bool {
|
||||
t.lock.RLock()
|
||||
defer t.lock.RUnlock()
|
||||
|
||||
if t.at.Equal(other.at) {
|
||||
if t.ts == other.ts {
|
||||
return t.ticks > other.ticks
|
||||
}
|
||||
|
||||
return t.at.After(other.at)
|
||||
return t.ts > other.ts
|
||||
}
|
||||
|
||||
func (t *TimedVersion) ToProto() *livekit.TimedVersion {
|
||||
@@ -54,7 +80,7 @@ func (t *TimedVersion) ToProto() *livekit.TimedVersion {
|
||||
defer t.lock.RUnlock()
|
||||
|
||||
return &livekit.TimedVersion{
|
||||
UnixMicro: t.at.UnixMicro(),
|
||||
UnixMicro: t.ts,
|
||||
Ticks: t.ticks,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user