-
Notifications
You must be signed in to change notification settings - Fork 67
fix consul engine bug when calling Set and List #283
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
Changes from all commits
6ad5557
77a23a7
f1113c7
8455cc5
c0e760d
40afc4e
0680532
3b365b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -162,6 +162,7 @@ func (c *Consul) IsReady(ctx context.Context) bool { | |
| } | ||
|
|
||
| func (c *Consul) Get(ctx context.Context, key string) ([]byte, error) { | ||
| key = sanitizeKey(key) | ||
| rsp, _, err := c.client.KV().Get(key, nil) | ||
| if err != nil { | ||
| return nil, err | ||
|
|
@@ -173,6 +174,7 @@ func (c *Consul) Get(ctx context.Context, key string) ([]byte, error) { | |
| } | ||
|
|
||
| func (c *Consul) Exists(ctx context.Context, key string) (bool, error) { | ||
| key = sanitizeKey(key) | ||
| _, err := c.Get(ctx, key) | ||
| if err != nil { | ||
| if errors.Is(err, consts.ErrNotFound) { | ||
|
|
@@ -184,6 +186,7 @@ func (c *Consul) Exists(ctx context.Context, key string) (bool, error) { | |
| } | ||
|
|
||
| func (c *Consul) Set(ctx context.Context, key string, value []byte) error { | ||
| key = sanitizeKey(key) | ||
| kvPair := &api.KVPair{ | ||
| Key: key, | ||
| Value: value, | ||
|
|
@@ -193,11 +196,13 @@ func (c *Consul) Set(ctx context.Context, key string, value []byte) error { | |
| } | ||
|
|
||
| func (c *Consul) Delete(ctx context.Context, key string) error { | ||
| key = sanitizeKey(key) | ||
| _, err := c.client.KV().Delete(key, nil) | ||
| return err | ||
| } | ||
|
|
||
| func (c *Consul) List(ctx context.Context, prefix string) ([]engine.Entry, error) { | ||
| prefix = sanitizeKey(prefix) | ||
| rsp, _, err := c.client.KV().List(prefix, nil) | ||
| if err != nil { | ||
| return nil, err | ||
|
|
@@ -209,7 +214,7 @@ func (c *Consul) List(ctx context.Context, prefix string) ([]engine.Entry, error | |
| if string(kv.Key) == prefix { | ||
| continue | ||
| } | ||
| key := strings.TrimLeft(string(kv.Key[prefixLen+1]), "/") | ||
| key := strings.TrimLeft(string(kv.Key[prefixLen+1:]), "/") | ||
| if strings.ContainsRune(key, '/') { | ||
| continue | ||
| } | ||
|
|
@@ -236,7 +241,6 @@ func (c *Consul) electLoop() { | |
| TTL: fmt.Sprintf("%v", sessionTTL), | ||
| LockDelay: lockDelay, | ||
| }, nil) | ||
|
|
||
| if err != nil { | ||
| logger.Get().With( | ||
| zap.Error(err), | ||
|
|
@@ -312,3 +316,10 @@ func (c *Consul) Close() error { | |
| c.client = nil | ||
| return nil | ||
| } | ||
|
|
||
| func sanitizeKey(key string) string { | ||
| if len(key) > 0 && key[0] == '/' { | ||
| key = strings.TrimPrefix(key, "/") | ||
| } | ||
|
Comment on lines
+321
to
+323
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. trimming the leading |
||
| return key | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,15 +47,15 @@ func TestBasicOperations(t *testing.T) { | |
| }() | ||
|
|
||
| ctx := context.Background() | ||
| keys := []string{"a/b/c0", "a/b/c1", "a/b/c2"} | ||
| keys := []string{"/a/b/c0", "/a/b/c1", "/a/b/c2"} | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the error really happened because the |
||
| value := []byte("v") | ||
| for _, key := range keys { | ||
| require.NoError(t, persist.Set(ctx, key, value)) | ||
| gotValue, err := persist.Get(ctx, key) | ||
| require.NoError(t, err) | ||
| require.Equal(t, value, gotValue) | ||
| } | ||
| entries, err := persist.List(ctx, "a/b") | ||
| entries, err := persist.List(ctx, "/a/b") | ||
| require.NoError(t, err) | ||
| require.Equal(t, len(keys), len(entries)) | ||
| for _, key := range keys { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug 2 fix. I noticed that this was missing the
:which I could find in the etcd and other implementations