From cab6cee3854e4d8ea45099da111a331dd8f44798 Mon Sep 17 00:00:00 2001 From: Kagashino Date: Fri, 10 Jun 2022 18:57:42 +0800 Subject: [PATCH] fix: backend/cos - remove cos object write lock either when unlock state file --- internal/backend/remote-state/cos/client.go | 39 +++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/internal/backend/remote-state/cos/client.go b/internal/backend/remote-state/cos/client.go index 4bd48069a5..818bc129f5 100644 --- a/internal/backend/remote-state/cos/client.go +++ b/internal/backend/remote-state/cos/client.go @@ -123,6 +123,11 @@ func (c *remoteClient) Unlock(check string) error { return c.lockError(err) } + err = c.cosUnlock(c.bucket, c.lockFile) + if err != nil { + return c.lockError(err) + } + return nil } @@ -362,6 +367,16 @@ func (c *remoteClient) cosUnlock(bucket, cosFile string) error { var err error for i := 0; i < 30; i++ { + tagExists, err := c.CheckTag(lockTagKey, lockTagValue) + + if err != nil { + return err + } + + if !tagExists { + return nil + } + err = c.DeleteTag(lockTagKey, lockTagValue) if err == nil { return nil @@ -372,6 +387,30 @@ func (c *remoteClient) cosUnlock(bucket, cosFile string) error { return err } +// CheckTag checks if tag key:value exists +func (c *remoteClient) CheckTag(key, value string) (exists bool, err error) { + request := tag.NewDescribeTagsRequest() + request.TagKey = &key + request.TagValue = &value + + response, err := c.tagClient.DescribeTags(request) + log.Printf("[DEBUG] create tag %s:%s: error: %v", key, value, err) + if err != nil { + return + } + + if len(response.Response.Tags) == 0 { + return + } + + tagKey := response.Response.Tags[0].TagKey + tagValue := response.Response.Tags[0].TagValue + + exists = key == *tagKey && value == *tagValue + + return +} + // CreateTag create tag by key and value func (c *remoteClient) CreateTag(key, value string) error { request := tag.NewCreateTagRequest()