-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding latency injector option to -dev mode for storage operations #3289
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,90 @@ | ||
package physical | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
|
||
log "github.com/mgutz/logxi/v1" | ||
) | ||
|
||
const ( | ||
// DefaultJitterPercent is used if no cache size is specified for NewCache | ||
DefaultJitterPercent = 20 | ||
) | ||
|
||
// LatencyInjector is used to add latency into underlying physical requests | ||
type LatencyInjector struct { | ||
backend Backend | ||
latency time.Duration | ||
jitterPercent int | ||
random *rand.Rand | ||
} | ||
|
||
// TransactionalLatencyInjector is the transactional version of the latency | ||
// injector | ||
type TransactionalLatencyInjector struct { | ||
*LatencyInjector | ||
Transactional | ||
} | ||
|
||
// NewLatencyInjector returns a wrapped physical backend to simulate latency | ||
func NewLatencyInjector(b Backend, latency time.Duration, jitter int, logger log.Logger) *LatencyInjector { | ||
if jitter < 0 || jitter > 100 { | ||
jitter = DefaultJitterPercent | ||
} | ||
logger.Info("physical/latency: creating latency injector") | ||
|
||
return &LatencyInjector{ | ||
backend: b, | ||
latency: latency, | ||
jitterPercent: jitter, | ||
random: rand.New(rand.NewSource(int64(time.Now().Nanosecond()))), | ||
} | ||
} | ||
|
||
// NewTransactionalLatencyInjector creates a new transactional LatencyInjector | ||
func NewTransactionalLatencyInjector(b Backend, latency time.Duration, jitter int, logger log.Logger) *TransactionalLatencyInjector { | ||
return &TransactionalLatencyInjector{ | ||
LatencyInjector: NewLatencyInjector(b, latency, jitter, logger), | ||
Transactional: b.(Transactional), | ||
} | ||
} | ||
|
||
func (l *LatencyInjector) addLatency() { | ||
// Calculate a value between -1 and 1 | ||
min := 100 - l.jitterPercent | ||
max := 100 + l.jitterPercent | ||
percent := float64(l.random.Intn(max-min)+min) / 100 | ||
latencyDuration := time.Duration(int64(float64(l.latency/time.Millisecond)*percent)) * time.Millisecond | ||
time.Sleep(latencyDuration) | ||
} | ||
|
||
// Put is a latent put request | ||
func (l *LatencyInjector) Put(entry *Entry) error { | ||
l.addLatency() | ||
return l.backend.Put(entry) | ||
} | ||
|
||
// Get is a latent get request | ||
func (l *LatencyInjector) Get(key string) (*Entry, error) { | ||
l.addLatency() | ||
return l.backend.Get(key) | ||
} | ||
|
||
// Delete is a latent delete request | ||
func (l *LatencyInjector) Delete(key string) error { | ||
l.addLatency() | ||
return l.backend.Delete(key) | ||
} | ||
|
||
// List is a latent list request | ||
func (l *LatencyInjector) List(prefix string) ([]string, error) { | ||
l.addLatency() | ||
return l.backend.List(prefix) | ||
} | ||
|
||
// Transaction is a latent transaction request | ||
func (l *TransactionalLatencyInjector) Transaction(txns []TxnEntry) error { | ||
l.addLatency() | ||
return l.Transactional.Transaction(txns) | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this and (
TransactionalLatencyInjector
) is really an implementation of thePhysical
interface, should we call thisLatencyBackend
to keep things uniform with other physical backend implementations?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not a backend itself and just wraps some other backend that is why I didn't call it a backend. It is more analogous to the
Cache
wrapper.