-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add query-frontend cache Signed-off-by: Joe Elliott <[email protected]> * docs and changelog Signed-off-by: Joe Elliott <[email protected]> * removed load and added test Signed-off-by: Joe Elliott <[email protected]> --------- Signed-off-by: Joe Elliott <[email protected]>
- Loading branch information
1 parent
f7fcfac
commit dd0992a
Showing
15 changed files
with
651 additions
and
118 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
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,69 @@ | ||
package frontend | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
"github.com/gogo/protobuf/jsonpb" | ||
"github.com/gogo/protobuf/proto" | ||
"github.com/grafana/tempo/pkg/cache" | ||
) | ||
|
||
type frontendCache struct { | ||
c cache.Cache | ||
} | ||
|
||
func newFrontendCache(cacheProvider cache.Provider, role cache.Role, logger log.Logger) *frontendCache { | ||
var c cache.Cache | ||
if cacheProvider != nil { | ||
c = cacheProvider.CacheFor(role) | ||
} | ||
|
||
level.Info(logger).Log("msg", "init frontend cache", "role", role, "enabled", c != nil) | ||
|
||
return &frontendCache{ | ||
c: c, | ||
} | ||
} | ||
|
||
// store stores the response body in the cache. the caller assumes the responsibility of closing the response body | ||
func (c *frontendCache) store(ctx context.Context, key string, buffer []byte) { | ||
if c.c == nil { | ||
return | ||
} | ||
|
||
if key == "" { | ||
return | ||
} | ||
|
||
if len(buffer) == 0 { | ||
return | ||
} | ||
|
||
c.c.Store(ctx, []string{key}, [][]byte{buffer}) | ||
} | ||
|
||
// fetch fetches the response body from the cache. the caller assumes the responsibility of closing the response body. | ||
func (c *frontendCache) fetch(key string, pb proto.Message) bool { | ||
if c.c == nil { | ||
return false | ||
} | ||
|
||
if len(key) == 0 { | ||
return false | ||
} | ||
|
||
_, bufs, _ := c.c.Fetch(context.Background(), []string{key}) | ||
if len(bufs) != 1 { | ||
return false | ||
} | ||
|
||
err := (&jsonpb.Unmarshaler{AllowUnknownFields: true}).Unmarshal(bytes.NewReader(bufs[0]), pb) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return 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,60 @@ | ||
package frontend | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/gogo/protobuf/jsonpb" | ||
"github.com/grafana/tempo/pkg/cache" | ||
"github.com/grafana/tempo/pkg/tempopb" | ||
"github.com/grafana/tempo/pkg/util/test" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNilProvider(t *testing.T) { | ||
testKey := "key" | ||
|
||
c := newFrontendCache(nil, cache.RoleBloom, log.NewNopLogger()) | ||
require.NotNil(t, c) | ||
|
||
rec := httptest.NewRecorder() | ||
|
||
bodyBuffer, err := io.ReadAll(rec.Result().Body) | ||
require.NoError(t, err) | ||
|
||
c.store(context.Background(), testKey, bodyBuffer) | ||
found := c.fetch(testKey, &tempopb.SearchResponse{}) | ||
|
||
require.False(t, found) | ||
} | ||
|
||
func TestCacheCaches(t *testing.T) { | ||
expected := &tempopb.SearchTagsResponse{ | ||
TagNames: []string{"foo", "bar"}, | ||
} | ||
|
||
// marshal mesage to bytes | ||
buf := bytes.NewBuffer([]byte{}) | ||
err := (&jsonpb.Marshaler{}).Marshal(buf, expected) | ||
require.NoError(t, err) | ||
|
||
testKey := "key" | ||
testData := buf.Bytes() | ||
|
||
p := test.NewMockProvider() | ||
c := newFrontendCache(p, cache.RoleBloom, log.NewNopLogger()) | ||
require.NotNil(t, c) | ||
|
||
// create response | ||
c.store(context.Background(), testKey, testData) | ||
|
||
actual := &tempopb.SearchTagsResponse{} | ||
found := c.fetch(testKey, actual) | ||
|
||
require.True(t, found) | ||
require.Equal(t, expected, actual) | ||
} |
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
Oops, something went wrong.