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

Fixed auto-purged cluster behaviour for databricks_library #1745

Merged
merged 7 commits into from
Nov 10, 2022
33 changes: 32 additions & 1 deletion libraries/libraries_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (a LibrariesAPI) WaitForLibrariesInstalled(wait Wait) (result *ClusterLibra
return resource.RetryableError(err)
}
if err != nil {
return resource.NonRetryableError(err)
return resource.NonRetryableError(wrapClusterStatusAPIError(err, wait.ClusterID))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, so please simplify the logic here between lines 81 and 87. the new logic should be something like: if we get error from a.ClusterStatus, return it as non-retriable.

you probably need to directly paste only these lines:

        apiErr, ok := err.(common.APIError)
	if !ok {
		return resource.NonRetryableError(err)
	}
	// fix non-compliant error code
	if apiErr.StatusCode != 404 && strings.Contains(apiErr.Message, 
                fmt.Sprintf("Cluster %s does not exist", id)) {
		apiErr.StatusCode = 404
	}
        return resource.NonRetryableError(apiErr)

then amount of unit tests will be smaller ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nfx Updated per your suggestion, please review and confirm if we are good.

}
if !wait.IsRunning {
log.Printf("[INFO] Cluster %s is currently not running, so just returning list of %d libraries",
Expand Down Expand Up @@ -355,3 +355,34 @@ func (cls ClusterLibraryStatuses) IsRetryNeeded(refresh bool) (bool, error) {
}
return false, nil
}

// wrapClusterStatusAPIError returns the wrapped error response when the requested cluster doesn't exist.
// Wraps up the error messages of cluster status API (/api/2.0/libraries/cluster-status) like INVALID_STATE and INVALID_PARAMETER_VALUE
func wrapClusterStatusAPIError(err error, id string) error {

apiErr, ok := err.(common.APIError)
if !ok {
return err
}
if apiErr.IsMissing() {
return err
}

if apiErr.ErrorCode == "INVALID_STATE" {
log.Printf("[WARN] assuming that cluster is removed on backend: %s", apiErr)
apiErr.StatusCode = 404
return apiErr
}
if apiErr.ErrorCode == "INVALID_PARAMETER_VALUE" {
log.Printf("[WARN] assuming that cluster is not created or removed on backend: %s", apiErr)
apiErr.StatusCode = 404
return apiErr
}
// fix non-compliant error code
if strings.Contains(apiErr.Message,
fmt.Sprintf("Cluster %s does not exist", id)) {
apiErr.StatusCode = 404
return apiErr
}
return err
}
18 changes: 18 additions & 0 deletions libraries/libraries_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package libraries

import (
"context"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -350,3 +351,20 @@ func TestNewLibraryFromInstanceState(t *testing.T) {
})
}
}

func TestWrapClusterStatusAPIError(t *testing.T) {
assert.EqualError(t, wrapClusterStatusAPIError(fmt.Errorf("x"), "abc"), "x")
assert.EqualError(t, wrapClusterStatusAPIError(common.APIError{
Message: "Cluster abc does not exist",
}, "abc"), "Cluster abc does not exist")
assert.EqualValues(t, common.APIError{Message: "Cluster abc does not exist",
StatusCode: 404, ErrorCode: "INVALID_PARAMETER_VALUE"},
wrapClusterStatusAPIError(common.APIError{Message: "Cluster abc does not exist",
StatusCode: 400, ErrorCode: "INVALID_PARAMETER_VALUE"}, "1232"))
assert.EqualValues(t, common.APIError{Message: "Cluster abc does not exist",
StatusCode: 404, ErrorCode: "INVALID_STATE"},
wrapClusterStatusAPIError(common.APIError{Message: "Cluster abc does not exist",
StatusCode: 400, ErrorCode: "INVALID_STATE"}, "1232"))
assert.EqualValues(t, common.APIError{StatusCode: 404},
wrapClusterStatusAPIError(common.APIError{StatusCode: 404}, "1232"))
}