Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring the 'suspicion protocol' to a generic module for timebase…
…d state transitions.
- Loading branch information
Sever Banesiu
authored and
Nils Dijk
committed
Mar 4, 2016
1 parent
6affd85
commit 99b1219
Showing
7 changed files
with
368 additions
and
308 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
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,195 @@ | ||
// Copyright (c) 2015 Uber 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 swim | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/uber-common/bark" | ||
"github.com/uber/ringpop-go/logging" | ||
"github.com/uber/ringpop-go/util" | ||
) | ||
|
||
// subject is an interface to define the subject (eg. member) to transition state for. This interface allows to pass in both a Member and a Change struct to the schedule function. | ||
type subject interface { | ||
address() string | ||
incarnation() int64 | ||
} | ||
|
||
type transitionTimer struct { | ||
*time.Timer | ||
|
||
// state represents the state the subject was in when the transition was scheduled | ||
state string | ||
} | ||
|
||
// stateTransitions handles the timers for state transitions in SWIM | ||
type stateTransitions struct { | ||
sync.Mutex | ||
|
||
node *Node | ||
|
||
timeouts StateTimeouts | ||
timers map[string]*transitionTimer | ||
enabled bool | ||
logger bark.Logger | ||
} | ||
|
||
// StateTimeouts contains the configured timeouts for every state before transitioning to the new state | ||
type StateTimeouts struct { | ||
// Suspect is the timeout it takes a node in suspect mode to transition to faulty | ||
Suspect time.Duration | ||
} | ||
|
||
func mergeStateTimeouts(one StateTimeouts, two StateTimeouts) StateTimeouts { | ||
return StateTimeouts{ | ||
Suspect: util.SelectDuration(one.Suspect, two.Suspect), | ||
} | ||
} | ||
|
||
// newStateTransitions returns a new state transition controller that can be used to schedule state transitions for nodes | ||
func newStateTransitions(n *Node, timeouts StateTimeouts) *stateTransitions { | ||
return &stateTransitions{ | ||
node: n, | ||
timeouts: timeouts, | ||
timers: make(map[string]*transitionTimer), | ||
enabled: true, | ||
logger: logging.Logger("stateTransitions").WithField("local", n.Address()), | ||
} | ||
} | ||
|
||
// ScheduleSuspectToFaulty starts the suspect timer. After the Suspect timeout the node will be declared faulty | ||
func (s *stateTransitions) ScheduleSuspectToFaulty(subject subject) { | ||
s.Lock() | ||
s.schedule(subject, Suspect, s.timeouts.Suspect, func() { | ||
// transition the subject to faulty | ||
s.node.memberlist.MakeFaulty(subject.address(), subject.incarnation()) | ||
}) | ||
s.Unlock() | ||
} | ||
|
||
func (s *stateTransitions) schedule(subject subject, state string, timeout time.Duration, transition func()) { | ||
if !s.enabled { | ||
s.logger.WithField("member", subject.address()).Warn("cannot schedule a state transition while disabled") | ||
return | ||
} | ||
|
||
if s.node.Address() == subject.address() { | ||
s.logger.WithField("member", subject.address()).Warn("cannot schedule a state transition for the local member") | ||
return | ||
} | ||
|
||
if timer, ok := s.timers[subject.address()]; ok { | ||
if timer.state == state { | ||
s.logger.WithFields(bark.Fields{ | ||
"member": subject.address(), | ||
"state": state, | ||
}).Warn("redundant call to schedule a state transition for memer, ignored") | ||
return | ||
} | ||
// cancel the previously scheduled transition for the subject | ||
timer.Stop() | ||
} | ||
|
||
timer := time.AfterFunc(timeout, func() { | ||
s.logger.WithFields(bark.Fields{ | ||
"member": subject.address(), | ||
"state": state, | ||
}).Info("executing scheduled transition for member") | ||
// execute the transition | ||
transition() | ||
}) | ||
|
||
s.timers[subject.address()] = &transitionTimer{ | ||
Timer: timer, | ||
state: state, | ||
} | ||
|
||
s.logger.WithFields(bark.Fields{ | ||
"member": subject.address(), | ||
"state": state, | ||
}).Debug("scheduled state transition for member") | ||
} | ||
|
||
// Cancel cancels the scheduled transition for the subject | ||
func (s *stateTransitions) Cancel(subject subject) { | ||
s.Lock() | ||
|
||
if timer, ok := s.timers[subject.address()]; ok { | ||
timer.Stop() | ||
delete(s.timers, subject.address()) | ||
s.logger.WithFields(bark.Fields{ | ||
"member": subject.address(), | ||
"state": timer.state, | ||
}).Debug("stopped scheduled state transition for member") | ||
} | ||
|
||
s.Unlock() | ||
} | ||
|
||
// Enable enables state transition controller. The transition controller needs to be in enabled state to allow transitions to be scheduled. | ||
func (s *stateTransitions) Enable() { | ||
s.Lock() | ||
|
||
if s.enabled { | ||
s.logger.Warn("state transition controller already enabled") | ||
s.Unlock() | ||
return | ||
} | ||
|
||
s.enabled = true | ||
s.Unlock() | ||
s.logger.Info("enabled state transition controller") | ||
} | ||
|
||
// Disable cancels all scheduled state transitions and disables the state transition controller for further use | ||
func (s *stateTransitions) Disable() { | ||
s.Lock() | ||
|
||
if !s.enabled { | ||
s.logger.Warn("state transition controller already disabled") | ||
s.Unlock() | ||
return | ||
} | ||
|
||
s.enabled = false | ||
|
||
numTimers := len(s.timers) | ||
for address, timer := range s.timers { | ||
timer.Stop() | ||
delete(s.timers, address) | ||
} | ||
|
||
s.Unlock() | ||
s.logger.WithField("timersStopped", numTimers).Info("disabled state transition controller") | ||
} | ||
|
||
// timer is a testing func to avoid data races | ||
func (s *stateTransitions) timer(address string) *time.Timer { | ||
s.Lock() | ||
t, ok := s.timers[address] | ||
s.Unlock() | ||
if !ok { | ||
return nil | ||
} | ||
return t.Timer | ||
} |
Oops, something went wrong.