-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Seperate liveness of task list into a dedicated entity (#5105)
- Loading branch information
Showing
7 changed files
with
331 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Modifications Copyright (c) 2020 Uber Technologies Inc. | ||
|
||
// Copyright (c) 2020 Temporal Technologies, Inc. | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
package matching | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/uber/cadence/common" | ||
"github.com/uber/cadence/common/clock" | ||
) | ||
|
||
type ( | ||
liveness struct { | ||
status int32 | ||
timeSource clock.TimeSource | ||
ttl time.Duration | ||
// internal shutdown channel | ||
shutdownChan chan struct{} | ||
|
||
// broadcast shutdown functions | ||
broadcastShutdownFn func() | ||
|
||
sync.Mutex | ||
lastEventTime time.Time | ||
} | ||
) | ||
|
||
var _ common.Daemon = (*liveness)(nil) | ||
|
||
func newLiveness( | ||
timeSource clock.TimeSource, | ||
ttl time.Duration, | ||
broadcastShutdownFn func(), | ||
) *liveness { | ||
return &liveness{ | ||
status: common.DaemonStatusInitialized, | ||
timeSource: timeSource, | ||
ttl: ttl, | ||
shutdownChan: make(chan struct{}), | ||
|
||
broadcastShutdownFn: broadcastShutdownFn, | ||
|
||
lastEventTime: timeSource.Now().UTC(), | ||
} | ||
} | ||
|
||
func (l *liveness) Start() { | ||
if !atomic.CompareAndSwapInt32( | ||
&l.status, | ||
common.DaemonStatusInitialized, | ||
common.DaemonStatusStarted, | ||
) { | ||
return | ||
} | ||
|
||
go l.eventLoop() | ||
} | ||
|
||
func (l *liveness) Stop() { | ||
if !atomic.CompareAndSwapInt32( | ||
&l.status, | ||
common.DaemonStatusStarted, | ||
common.DaemonStatusStopped, | ||
) { | ||
return | ||
} | ||
|
||
close(l.shutdownChan) | ||
l.broadcastShutdownFn() | ||
} | ||
|
||
func (l *liveness) eventLoop() { | ||
ttlTimer := time.NewTicker(l.ttl) | ||
defer ttlTimer.Stop() | ||
|
||
for { | ||
select { | ||
case <-ttlTimer.C: | ||
if !l.isAlive() { | ||
l.Stop() | ||
} | ||
|
||
case <-l.shutdownChan: | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (l *liveness) isAlive() bool { | ||
l.Lock() | ||
defer l.Unlock() | ||
return l.lastEventTime.Add(l.ttl).After(l.timeSource.Now()) | ||
} | ||
|
||
func (l *liveness) markAlive( | ||
now time.Time, | ||
) { | ||
l.Lock() | ||
defer l.Unlock() | ||
if l.lastEventTime.Before(now) { | ||
l.lastEventTime = now.UTC() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Modifications Copyright (c) 2020 Uber Technologies Inc. | ||
|
||
// Copyright (c) 2020 Temporal Technologies, Inc. | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
package matching | ||
|
||
import ( | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/uber/cadence/common/clock" | ||
) | ||
|
||
type ( | ||
livenessSuite struct { | ||
suite.Suite | ||
*require.Assertions | ||
|
||
timeSource *clock.EventTimeSource | ||
ttl time.Duration | ||
shutdownFlag int32 | ||
} | ||
) | ||
|
||
func TestLivenessSuite(t *testing.T) { | ||
s := new(livenessSuite) | ||
suite.Run(t, s) | ||
} | ||
|
||
func (s *livenessSuite) SetupSuite() { | ||
} | ||
|
||
func (s *livenessSuite) TearDownSuite() { | ||
} | ||
|
||
func (s *livenessSuite) SetupTest() { | ||
s.Assertions = require.New(s.T()) | ||
|
||
s.ttl = 2 * time.Second | ||
s.timeSource = clock.NewEventTimeSource() | ||
s.timeSource.Update(time.Now()) | ||
s.shutdownFlag = 0 | ||
} | ||
|
||
func (s *livenessSuite) TearDownTest() { | ||
|
||
} | ||
|
||
func (s *livenessSuite) TestIsAlive_No() { | ||
liveness := newLiveness(s.timeSource, s.ttl, func() { atomic.CompareAndSwapInt32(&s.shutdownFlag, 0, 1) }) | ||
s.timeSource.Update(time.Now().Add(s.ttl * 2)) | ||
alive := liveness.isAlive() | ||
s.False(alive) | ||
} | ||
|
||
func (s *livenessSuite) TestIsAlive_Yes() { | ||
liveness := newLiveness(s.timeSource, s.ttl, func() { atomic.CompareAndSwapInt32(&s.shutdownFlag, 0, 1) }) | ||
s.timeSource.Update(time.Now().Add(s.ttl / 2)) | ||
alive := liveness.isAlive() | ||
s.True(alive) | ||
} | ||
|
||
func (s *livenessSuite) TestMarkAlive_Noop() { | ||
liveness := newLiveness(s.timeSource, s.ttl, func() { atomic.CompareAndSwapInt32(&s.shutdownFlag, 0, 1) }) | ||
lastEventTime := liveness.lastEventTime | ||
newEventTime := s.timeSource.Now().Add(-1) | ||
liveness.markAlive(newEventTime) | ||
s.True(lastEventTime.Equal(liveness.lastEventTime)) | ||
} | ||
|
||
func (s *livenessSuite) TestMarkAlive_Updated() { | ||
liveness := newLiveness(s.timeSource, s.ttl, func() { atomic.CompareAndSwapInt32(&s.shutdownFlag, 0, 1) }) | ||
newEventTime := s.timeSource.Now().Add(1) | ||
liveness.markAlive(newEventTime) | ||
s.True(newEventTime.Equal(liveness.lastEventTime)) | ||
} | ||
|
||
func (s *livenessSuite) TestEventLoop_Noop() { | ||
liveness := newLiveness(s.timeSource, s.ttl, func() { atomic.CompareAndSwapInt32(&s.shutdownFlag, 0, 1) }) | ||
liveness.Start() | ||
|
||
now := time.Now().Add(s.ttl * 4) | ||
s.timeSource.Update(now) | ||
liveness.markAlive(now) | ||
|
||
timer := time.NewTimer(s.ttl * 2) | ||
select { | ||
case <-liveness.shutdownChan: | ||
s.Fail("should not shutdown") | ||
case <-timer.C: | ||
s.Equal(int32(0), atomic.LoadInt32(&s.shutdownFlag)) | ||
} | ||
} | ||
|
||
func (s *livenessSuite) TestEventLoop_Shutdown() { | ||
liveness := newLiveness(s.timeSource, s.ttl, func() { atomic.CompareAndSwapInt32(&s.shutdownFlag, 0, 1) }) | ||
liveness.Start() | ||
|
||
s.timeSource.Update(time.Now().Add(s.ttl * 4)) | ||
<-liveness.shutdownChan | ||
s.Equal(int32(1), atomic.LoadInt32(&s.shutdownFlag)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.