Skip to content
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

Query: always return a string in the lastError field #2809

Merged
merged 4 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ We use *breaking* word for marking changes that are not backward compatible (rel
- [#2728](https://github.com/thanos-io/thanos/pull/2728) Query: Fixed panics when using larger number of replica labels with short series label sets.
- [#2787](https://github.com/thanos-io/thanos/pull/2787) Update Prometheus mod to pull in prometheus/prometheus#7414.
- [#2807](https://github.com/thanos-io/thanos/pull/2807) Store: decreased memory allocations while querying block's index.
- [#2809](https://github.com/thanos-io/thanos/pull/2809) Query: `/api/v1/stores` now guarantees to return a string in the `lastError` field
GiedriusS marked this conversation as resolved.
Show resolved Hide resolved

### Changed

Expand Down
21 changes: 19 additions & 2 deletions pkg/query/storeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package query

import (
"context"
"encoding/json"
"fmt"
"sort"
"strings"
Expand Down Expand Up @@ -47,10 +48,26 @@ type RuleSpec interface {
Addr() string
}

// stringifiedError forces the error to be a string
// when marshalled into a JSON.
type stringifiedError struct {
GiedriusS marked this conversation as resolved.
Show resolved Hide resolved
originalErr error
}

// MarshalJSON marshals the error into a string form.
func (e *stringifiedError) MarshalJSON() ([]byte, error) {
return json.Marshal(e.originalErr.Error())
}

// Error returns the original underlying error.
func (e *stringifiedError) Error() string {
return e.originalErr.Error()
}

type StoreStatus struct {
Name string `json:"name"`
LastCheck time.Time `json:"lastCheck"`
LastError error `json:"lastError"`
LastError *stringifiedError `json:"lastError"`
LabelSets []storepb.LabelSet `json:"labelSets"`
StoreType component.StoreAPI `json:"-"`
MinTime int64 `json:"minTime"`
Expand Down Expand Up @@ -510,7 +527,7 @@ func (s *StoreSet) updateStoreStatus(store *storeRef, err error) {
status = *prev
}

status.LastError = err
status.LastError = &stringifiedError{originalErr: err}

if err == nil {
status.LastCheck = time.Now()
Expand Down
29 changes: 28 additions & 1 deletion pkg/query/storeset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ package query

import (
"context"
"encoding/json"
"fmt"
"math"
"net"
"testing"
"time"

"github.com/fortytw2/leaktest"
"github.com/pkg/errors"
"github.com/thanos-io/thanos/pkg/component"
"github.com/thanos-io/thanos/pkg/store"
"github.com/thanos-io/thanos/pkg/store/storepb"
Expand Down Expand Up @@ -660,7 +662,7 @@ func TestQuerierStrict(t *testing.T) {
testutil.Equals(t, 2, len(storeSet.stores), "two static clients must remain available")
testutil.Equals(t, curMin, storeSet.stores[staticStoreAddr].minTime, "minimum time reported by the store node is different")
testutil.Equals(t, curMax, storeSet.stores[staticStoreAddr].maxTime, "minimum time reported by the store node is different")
testutil.NotOk(t, storeSet.storeStatuses[staticStoreAddr].LastError)
testutil.NotOk(t, storeSet.storeStatuses[staticStoreAddr].LastError.originalErr)
}

func TestStoreSet_Update_Rules(t *testing.T) {
Expand Down Expand Up @@ -778,3 +780,28 @@ func TestStoreSet_Update_Rules(t *testing.T) {
})
}
}

type weirdError struct {
originalErr error
}

// MarshalJSON marshals the error and returns weird results.
func (e *weirdError) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{})
}

// Error returns the original, underlying string.
func (e *weirdError) Error() string {
return e.originalErr.Error()
}

func TestStringifiedError(t *testing.T) {
weirdError := &weirdError{originalErr: errors.New("test")}
properErr := &stringifiedError{originalErr: weirdError}
storestatusMock := map[string]error{}
storestatusMock["weird"] = weirdError
storestatusMock["proper"] = properErr
b, err := json.Marshal(storestatusMock)
testutil.Ok(t, err)
testutil.Equals(t, []byte(`{"proper":"test","weird":{}}`), b, "expected to get proper results")
}