This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from johanneswuerbach/dependencies-prefetch
feat: dependencies prefetch
- Loading branch information
Showing
10 changed files
with
173 additions
and
13 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,79 @@ | ||
package s3spanstore | ||
|
||
import ( | ||
"context" | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/hashicorp/go-hclog" | ||
"github.com/jaegertracing/jaeger/model" | ||
) | ||
|
||
type DependenciesPrefetch struct { | ||
logger hclog.Logger | ||
reader ReaderWithDependencies | ||
ticker *time.Ticker | ||
enabled bool | ||
done chan bool | ||
ctx context.Context | ||
sleepDuration time.Duration | ||
} | ||
|
||
type ReaderWithDependencies interface { | ||
GetDependencies(ctx context.Context, endTs time.Time, lookback time.Duration) ([]model.DependencyLink, error) | ||
} | ||
|
||
func NewDependenciesPrefetch(ctx context.Context, logger hclog.Logger, reader ReaderWithDependencies, interval time.Duration, enabled bool) *DependenciesPrefetch { | ||
s1 := rand.NewSource(time.Now().UnixNano()) | ||
r1 := rand.New(s1) | ||
|
||
return &DependenciesPrefetch{ | ||
logger: logger, | ||
reader: reader, | ||
ticker: time.NewTicker(interval), | ||
enabled: enabled, | ||
done: make(chan bool), | ||
ctx: ctx, | ||
sleepDuration: time.Second * time.Duration(r1.Intn(180)), | ||
} | ||
} | ||
|
||
func (d *DependenciesPrefetch) Start() { | ||
if !d.enabled { | ||
return | ||
} | ||
|
||
go func() { | ||
// Do an initial prefetch | ||
d.prefetchDependencies() | ||
|
||
// Schedule background prefetches | ||
for { | ||
select { | ||
case <-d.done: | ||
return | ||
case <-d.ticker.C: | ||
d.prefetchDependencies() | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (d *DependenciesPrefetch) prefetchDependencies() { | ||
// Ensure different readers don't refresh at the same time | ||
time.Sleep(d.sleepDuration) | ||
|
||
// GetDependencies to ensure the result is cached | ||
if _, err := d.reader.GetDependencies(d.ctx, time.Now(), time.Hour*24*7); err != nil { | ||
d.logger.Error("failed to get dependencies", err) | ||
} | ||
} | ||
|
||
func (d *DependenciesPrefetch) Stop() { | ||
if !d.enabled { | ||
return | ||
} | ||
|
||
d.ticker.Stop() | ||
d.done <- true | ||
} |
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,74 @@ | ||
package s3spanstore | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/hashicorp/go-hclog" | ||
"github.com/jaegertracing/jaeger/model" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func NewTestDependencyPrefetch(ctx context.Context, assert *assert.Assertions, reader ReaderWithDependencies, enabled bool) *DependenciesPrefetch { | ||
loggerName := "jaeger-s3" | ||
|
||
logLevel := os.Getenv("GRPC_STORAGE_PLUGIN_LOG_LEVEL") | ||
if logLevel == "" { | ||
logLevel = hclog.Debug.String() | ||
} | ||
|
||
logger := hclog.New(&hclog.LoggerOptions{ | ||
Level: hclog.LevelFromString(logLevel), | ||
Name: loggerName, | ||
JSONFormat: true, | ||
}) | ||
|
||
prefetch := NewDependenciesPrefetch(ctx, logger, reader, 100*time.Millisecond, enabled) | ||
prefetch.sleepDuration = time.Millisecond * 1 | ||
|
||
return prefetch | ||
} | ||
|
||
type testReader struct { | ||
called int | ||
} | ||
|
||
func (r *testReader) GetDependencies(ctx context.Context, endTs time.Time, lookback time.Duration) ([]model.DependencyLink, error) { | ||
r.called++ | ||
return nil, nil | ||
} | ||
|
||
func TestDependenciesPrefetchEnabled(t *testing.T) { | ||
assert := assert.New(t) | ||
ctx := context.Background() | ||
|
||
testReader := &testReader{called: 0} | ||
prefetch := NewTestDependencyPrefetch(ctx, assert, testReader, true) | ||
prefetch.Start() | ||
time.Sleep(10 * time.Millisecond) | ||
|
||
assert.Equal(1, testReader.called) | ||
|
||
time.Sleep(150 * time.Millisecond) | ||
|
||
assert.Equal(2, testReader.called) | ||
|
||
prefetch.Stop() | ||
} | ||
|
||
func TestDependenciesPrefetchDisabled(t *testing.T) { | ||
assert := assert.New(t) | ||
ctx := context.Background() | ||
|
||
testReader := &testReader{called: 0} | ||
prefetch := NewTestDependencyPrefetch(ctx, assert, testReader, false) | ||
prefetch.Start() | ||
|
||
time.Sleep(150 * time.Millisecond) | ||
|
||
assert.Equal(0, testReader.called) | ||
|
||
prefetch.Stop() | ||
} |
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
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