-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* replace Cancel with Terminate * Code optimized based on code review result * Update doc about command: terminate * Code optimized
- Loading branch information
Lifei Chen
authored and
Marc Rooding
committed
Apr 12, 2019
1 parent
d5a9c82
commit 706b264
Showing
20 changed files
with
245 additions
and
75 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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,60 @@ | ||
package flink | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
type TerminateJobErrorResponse struct { | ||
ErrInfo string `json:"error"` | ||
} | ||
|
||
// Terminate terminates a running job specified by job ID | ||
func (c FlinkRestClient) Terminate(jobID string, mode string) error { | ||
var path string | ||
if len(mode) > 0 { | ||
path = fmt.Sprintf("jobs/%v?mode=%v", jobID, mode) | ||
} else { | ||
path = fmt.Sprintf("jobs/%v", jobID) | ||
} | ||
|
||
c.Client.CheckRetry = RetryPolicy | ||
req, err := c.newRequest("PATCH", c.constructURL(path), nil) | ||
res, err := c.Client.Do(req) | ||
|
||
defer res.Body.Close() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if res.StatusCode != 202 { | ||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return fmt.Errorf("Unexpected response status %v with body %v", res.StatusCode, string(body[:])) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Do not retry when status code is 500. (indicating the job is not stoppable) | ||
func RetryPolicy(ctx context.Context, resp *http.Response, err error) (bool, error) { | ||
if ctx.Err() != nil { | ||
return false, ctx.Err() | ||
} | ||
|
||
if err != nil { | ||
return true, err | ||
} | ||
|
||
if resp.StatusCode == 0 || resp.StatusCode > 500 { | ||
return true, nil | ||
} | ||
|
||
return false, nil | ||
} |
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,65 @@ | ||
package flink | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-retryablehttp" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTerminateWithModeCancelAndStatusSuccess(t *testing.T) { | ||
server := createTestServerWithBodyCheck(t, "/jobs/id?mode=cancel", "", http.StatusAccepted, "") | ||
defer server.Close() | ||
|
||
api := FlinkRestClient{ | ||
BaseURL: server.URL, | ||
Client: retryablehttp.NewClient(), | ||
} | ||
|
||
err := api.Terminate("id", "cancel") | ||
|
||
assert.Nil(t, err) | ||
} | ||
|
||
func TestTerminateWithModeCancelAndStatus404(t *testing.T) { | ||
server := createTestServerWithBodyCheck(t, "/jobs/id?mode=cancel", "", http.StatusNotFound, "not found") | ||
defer server.Close() | ||
|
||
api := FlinkRestClient{ | ||
BaseURL: server.URL, | ||
Client: retryablehttp.NewClient(), | ||
} | ||
|
||
err := api.Terminate("id", "cancel") | ||
|
||
assert.EqualError(t, err, "Unexpected response status 404 with body not found") | ||
} | ||
|
||
func TestTerminateWithModeStopAndStatusSuccess(t *testing.T) { | ||
server := createTestServerWithBodyCheck(t, "/jobs/id?mode=stop", "", http.StatusAccepted, "OK") | ||
defer server.Close() | ||
|
||
api := FlinkRestClient{ | ||
BaseURL: server.URL, | ||
Client: retryablehttp.NewClient(), | ||
} | ||
|
||
err := api.Terminate("id", "stop") | ||
|
||
assert.Nil(t, err) | ||
} | ||
|
||
func TestTerminateWithModeStopAndStatusFailure(t *testing.T) { | ||
server := createTestServerWithBodyCheck(t, "/jobs/id?mode=stop", "", http.StatusInternalServerError, "error") | ||
defer server.Close() | ||
|
||
api := FlinkRestClient{ | ||
BaseURL: server.URL, | ||
Client: retryablehttp.NewClient(), | ||
} | ||
|
||
err := api.Terminate("id", "stop") | ||
|
||
assert.EqualError(t, err, "Unexpected response status 500 with body error") | ||
} |
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
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
Oops, something went wrong.