2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-09-11 18:49:08 +00:00
|
|
|
package physical
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2017-09-11 18:49:08 +00:00
|
|
|
"math/rand"
|
2019-06-05 05:37:40 +00:00
|
|
|
"sync"
|
2017-09-11 18:49:08 +00:00
|
|
|
"time"
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2021-06-16 15:48:39 +00:00
|
|
|
uberAtomic "go.uber.org/atomic"
|
2017-09-11 18:49:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2019-03-04 18:35:22 +00:00
|
|
|
logger log.Logger
|
2017-09-11 18:49:08 +00:00
|
|
|
backend Backend
|
2021-06-16 15:48:39 +00:00
|
|
|
latency *uberAtomic.Duration
|
2017-09-11 18:49:08 +00:00
|
|
|
jitterPercent int
|
2019-06-05 05:37:40 +00:00
|
|
|
randomLock *sync.Mutex
|
2017-09-11 18:49:08 +00:00
|
|
|
random *rand.Rand
|
|
|
|
}
|
|
|
|
|
|
|
|
// TransactionalLatencyInjector is the transactional version of the latency
|
|
|
|
// injector
|
|
|
|
type TransactionalLatencyInjector struct {
|
|
|
|
*LatencyInjector
|
|
|
|
Transactional
|
|
|
|
}
|
|
|
|
|
2018-01-20 01:44:24 +00:00
|
|
|
// Verify LatencyInjector satisfies the correct interfaces
|
2021-04-08 16:43:39 +00:00
|
|
|
var (
|
|
|
|
_ Backend = (*LatencyInjector)(nil)
|
|
|
|
_ Transactional = (*TransactionalLatencyInjector)(nil)
|
|
|
|
)
|
2018-01-20 01:44:24 +00:00
|
|
|
|
2017-09-11 18:49:08 +00:00
|
|
|
// 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
|
|
|
|
}
|
2018-04-03 00:46:59 +00:00
|
|
|
logger.Info("creating latency injector")
|
2017-09-11 18:49:08 +00:00
|
|
|
|
|
|
|
return &LatencyInjector{
|
2019-03-04 18:35:22 +00:00
|
|
|
logger: logger,
|
2017-09-11 18:49:08 +00:00
|
|
|
backend: b,
|
2021-06-16 15:48:39 +00:00
|
|
|
latency: uberAtomic.NewDuration(latency),
|
2017-09-11 18:49:08 +00:00
|
|
|
jitterPercent: jitter,
|
2019-06-05 05:37:40 +00:00
|
|
|
randomLock: new(sync.Mutex),
|
2017-09-11 18:49:08 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 18:35:22 +00:00
|
|
|
func (l *LatencyInjector) SetLatency(latency time.Duration) {
|
|
|
|
l.logger.Info("Changing backend latency", "latency", latency)
|
2021-06-16 15:48:39 +00:00
|
|
|
l.latency.Store(latency)
|
2019-03-04 18:35:22 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 18:49:08 +00:00
|
|
|
func (l *LatencyInjector) addLatency() {
|
|
|
|
// Calculate a value between 1 +- jitter%
|
2018-10-15 21:06:45 +00:00
|
|
|
percent := 100
|
|
|
|
if l.jitterPercent > 0 {
|
|
|
|
min := 100 - l.jitterPercent
|
|
|
|
max := 100 + l.jitterPercent
|
2019-06-05 05:37:40 +00:00
|
|
|
l.randomLock.Lock()
|
2018-10-15 21:06:45 +00:00
|
|
|
percent = l.random.Intn(max-min) + min
|
2019-06-05 05:37:40 +00:00
|
|
|
l.randomLock.Unlock()
|
2018-10-15 21:06:45 +00:00
|
|
|
}
|
2021-06-16 15:48:39 +00:00
|
|
|
latencyDuration := time.Duration(int(l.latency.Load()) * percent / 100)
|
2017-09-11 18:49:08 +00:00
|
|
|
time.Sleep(latencyDuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put is a latent put request
|
2018-01-19 06:44:44 +00:00
|
|
|
func (l *LatencyInjector) Put(ctx context.Context, entry *Entry) error {
|
2017-09-11 18:49:08 +00:00
|
|
|
l.addLatency()
|
2018-01-19 06:44:44 +00:00
|
|
|
return l.backend.Put(ctx, entry)
|
2017-09-11 18:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get is a latent get request
|
2018-01-19 06:44:44 +00:00
|
|
|
func (l *LatencyInjector) Get(ctx context.Context, key string) (*Entry, error) {
|
2017-09-11 18:49:08 +00:00
|
|
|
l.addLatency()
|
2018-01-19 06:44:44 +00:00
|
|
|
return l.backend.Get(ctx, key)
|
2017-09-11 18:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete is a latent delete request
|
2018-01-19 06:44:44 +00:00
|
|
|
func (l *LatencyInjector) Delete(ctx context.Context, key string) error {
|
2017-09-11 18:49:08 +00:00
|
|
|
l.addLatency()
|
2018-01-19 06:44:44 +00:00
|
|
|
return l.backend.Delete(ctx, key)
|
2017-09-11 18:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List is a latent list request
|
2018-01-19 06:44:44 +00:00
|
|
|
func (l *LatencyInjector) List(ctx context.Context, prefix string) ([]string, error) {
|
2017-09-11 18:49:08 +00:00
|
|
|
l.addLatency()
|
2018-01-19 06:44:44 +00:00
|
|
|
return l.backend.List(ctx, prefix)
|
2017-09-11 18:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transaction is a latent transaction request
|
2018-01-19 06:44:44 +00:00
|
|
|
func (l *TransactionalLatencyInjector) Transaction(ctx context.Context, txns []*TxnEntry) error {
|
2017-09-11 18:49:08 +00:00
|
|
|
l.addLatency()
|
2018-01-19 06:44:44 +00:00
|
|
|
return l.Transactional.Transaction(ctx, txns)
|
2017-09-11 18:49:08 +00:00
|
|
|
}
|