forked from treeverse/lakeFS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CR] Fail "lakefs expire" if errors occur during expiration
Tested by using IAM to fail CreateJob and seeing an appropriately FATAL report. Former-commit-id: a1e4ca93d287e7dc16fabad3134e31f3ae90cab8
- Loading branch information
1 parent
dd78fbc
commit 3ac0877
Showing
5 changed files
with
266 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package retention | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/treeverse/lakefs/logging" | ||
) | ||
|
||
// Fields is a string-keyed map with a nice printed representation. | ||
type Fields map[string]interface{} | ||
|
||
func (f Fields) String() string { | ||
keys := make([]string, 0, len(f)) | ||
for k := range f { | ||
keys = append(keys, k) | ||
} | ||
sort.Strings(keys) | ||
|
||
var b strings.Builder | ||
sep := "" | ||
for _, k := range keys { | ||
b.WriteString(fmt.Sprintf("%s%s=%v", sep, k, f[k])) | ||
if sep == "" { | ||
sep = ", " | ||
} | ||
} | ||
return b.String() | ||
} | ||
|
||
func copyFields(f Fields) Fields { | ||
ret := Fields{} | ||
for k, v := range f { | ||
ret[k] = v | ||
} | ||
return ret | ||
} | ||
|
||
// WithField augments fields with another field. | ||
func (f Fields) WithField(key string, value interface{}) Fields { | ||
ret := copyFields(f) | ||
ret[key] = value | ||
return ret | ||
} | ||
|
||
// WithFields merges two Fields. | ||
func (f Fields) WithFields(g Fields) Fields { | ||
ret := copyFields(f) | ||
for k, v := range g { | ||
ret[k] = v | ||
} | ||
return ret | ||
} | ||
|
||
// FromLoggerContext returns Fields using logging keys from ctx. This is not stealing: logging | ||
// exports the field key. | ||
func FromLoggerContext(ctx context.Context) Fields { | ||
ret := Fields{} | ||
loggerFields := ctx.Value(logging.LogFieldsContextKey) | ||
if loggerFields != nil { | ||
for k, v := range loggerFields.(logging.Fields) { | ||
ret[k] = v | ||
} | ||
} | ||
return ret | ||
} | ||
|
||
// MapError wraps an error and adds multiple keyed additional Fields of string-keyed | ||
// information. | ||
type MapError struct { | ||
Fields Fields | ||
WrappedError error | ||
} | ||
|
||
func (m MapError) Unwrap() error { | ||
return m.WrappedError | ||
} | ||
|
||
func (m MapError) Error() string { | ||
return fmt.Sprintf("%+v %s", m.Fields, m.WrappedError) | ||
} |
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,62 @@ | ||
package retention_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/treeverse/lakefs/retention" | ||
) | ||
|
||
func TestFields_WithField(t *testing.T) { | ||
fields := retention.Fields{}.WithField("a", 1) | ||
if fields["a"].(int) != 1 { | ||
t.Errorf("expected to set field \"a\" to 1, got %v", fields) | ||
} | ||
moreFields := fields.WithField("b", "two") | ||
if moreFields["b"].(string) != "two" { | ||
t.Errorf("expected to set field \"b\" to \"two\", got %v", moreFields) | ||
} | ||
if moreFields["a"].(int) != 1 { | ||
t.Errorf("expected to keep field \"a\" at 1 after WithFields(\"b\", ...), got %v", moreFields) | ||
} | ||
if _, ok := fields["b"]; ok { | ||
t.Errorf("expected WithFields(\"b\", ...) not to change original fields, got %v", fields) | ||
} | ||
} | ||
|
||
func TestField_WithFields(t *testing.T) { | ||
fieldsA := retention.Fields{}.WithField("a", 1) | ||
fieldsB := retention.Fields{}.WithField("b", "two") | ||
fields := fieldsA.WithFields(fieldsB) | ||
|
||
if _, ok := fields["a"]; !ok { | ||
t.Errorf("expected field \"a\" on merged fields, got %v", fields) | ||
} | ||
if _, ok := fields["b"]; !ok { | ||
t.Errorf("expected field \"b\" on merged fields, got %v", fields) | ||
} | ||
if _, ok := fieldsA["b"]; ok { | ||
t.Errorf("expected WithFields(...) not to change original fields, got %v", fieldsA) | ||
} | ||
if _, ok := fieldsB["a"]; ok { | ||
t.Errorf("expected WithFields(...) not to change argument fields, got %v", fieldsB) | ||
} | ||
} | ||
|
||
func TestField_String(t *testing.T) { | ||
fields := retention.Fields{}.WithField("foo", "xyzzy").WithField("bar", 22) | ||
s := fields.String() | ||
if s != "bar=22, foo=xyzzy" { | ||
t.Errorf("unexpected string representation of %v: %s", fields, s) | ||
} | ||
} | ||
|
||
var wErr error = fmt.Errorf("error for testing") | ||
|
||
func TestMapError(t *testing.T) { | ||
err := retention.MapError{Fields: retention.Fields{"a": 1, "b": 2}, WrappedError: wErr} | ||
if !errors.Is(err, wErr) { | ||
t.Errorf("error %s failed to wrap its base %s", err, wErr) | ||
} | ||
} |
Oops, something went wrong.