Skip to content

Commit

Permalink
fixup! core: reuse remote transcoder for stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Reuben Rodrigues committed May 4, 2021
1 parent afb9ab6 commit f45fb1b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
52 changes: 47 additions & 5 deletions core/orch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ func TestRemoveFromRemoteTranscoders(t *testing.T) {
m := NewRemoteTranscoderManager()
strm := &StubTranscoderServer{manager: m}
strm2 := &StubTranscoderServer{manager: m}
strm3 := &StubTranscoderServer{manager: m}

// sanity check that transcoder is not in liveTranscoders or remoteTranscoders
assert := assert.New(t)
Expand All @@ -402,22 +403,63 @@ func TestRemoveFromRemoteTranscoders(t *testing.T) {
time.Sleep(1 * time.Millisecond) // allow time for first stream to register
go func() { m.Manage(strm2, 1) }()
time.Sleep(1 * time.Millisecond) // allow time for second stream to register
go func() { m.Manage(strm3, 1) }()
time.Sleep(1 * time.Millisecond) // allow time for second stream to register

// assert two transcoders added
assert.Len(m.remoteTranscoders, 2)
// assert three transcoders added
assert.Len(m.remoteTranscoders, 3)
t1 := m.liveTranscoders[strm]
t2 := m.liveTranscoders[strm2]

t3 := m.liveTranscoders[strm3]
assert.Equal(m.remoteTranscoders[0], t1)
assert.Equal(m.remoteTranscoders[1], t2)
assert.Equal(m.remoteTranscoders[2], t3)

// Remove transcoders and assert size reduces
// Remove transcoder froms head of the list
m.remoteTranscoders = removeFromRemoteTranscoders(t1, m.remoteTranscoders)
assert.Equal(m.remoteTranscoders[0], t2)
assert.Len(m.remoteTranscoders, 1)
assert.Equal(m.remoteTranscoders[1], t3)
assert.Len(m.remoteTranscoders, 2)

// add one back
go func() { m.Manage(strm, 1) }()
time.Sleep(1 * time.Millisecond) // allow time for first stream to register
t1 = m.liveTranscoders[strm]
assert.Equal(m.remoteTranscoders[0], t2)
assert.Equal(m.remoteTranscoders[1], t3)
assert.Equal(m.remoteTranscoders[2], t1)
assert.Len(m.remoteTranscoders, 3)

// Remove transcoder from the end of the list
m.remoteTranscoders = removeFromRemoteTranscoders(t3, m.remoteTranscoders)
assert.Equal(m.remoteTranscoders[0], t2)
assert.Equal(m.remoteTranscoders[1], t1)
assert.Len(m.remoteTranscoders, 2)

// add one back
go func() { m.Manage(strm, 1) }()
time.Sleep(1 * time.Millisecond) // allow time for first stream to register
t3 = m.liveTranscoders[strm]
assert.Equal(m.remoteTranscoders[0], t2)
assert.Equal(m.remoteTranscoders[1], t1)
assert.Equal(m.remoteTranscoders[2], t3)
assert.Len(m.remoteTranscoders, 3)

// Remove transcoder from the middle of the list
m.remoteTranscoders = removeFromRemoteTranscoders(t1, m.remoteTranscoders)
assert.Equal(m.remoteTranscoders[0], t2)
assert.Equal(m.remoteTranscoders[1], t3)
assert.Len(m.remoteTranscoders, 2)

// Remove all the remaining transcoders
m.remoteTranscoders = removeFromRemoteTranscoders(t2, m.remoteTranscoders)
m.remoteTranscoders = removeFromRemoteTranscoders(t3, m.remoteTranscoders)
assert.Len(m.remoteTranscoders, 0)

// Removing a transcoder when list is empty
m.remoteTranscoders = removeFromRemoteTranscoders(t3, m.remoteTranscoders)
emptyTList := []*RemoteTranscoder{}
assert.Equal(m.remoteTranscoders, emptyTList)
}

func TestTranscoderManagerTranscoding(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions core/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,11 @@ func (rtm *RemoteTranscoderManager) Manage(stream net.Transcoder_RegisterTransco
}

func removeFromRemoteTranscoders(rt *RemoteTranscoder, remoteTranscoders []*RemoteTranscoder) []*RemoteTranscoder {
if len(remoteTranscoders) == 0 {
// No transocerds to remove, return
return remoteTranscoders
}

lastIndex := len(remoteTranscoders) - 1
last := remoteTranscoders[lastIndex]
if rt == last {
Expand Down

0 comments on commit f45fb1b

Please sign in to comment.