Skip to content

Commit

Permalink
Add retry logic
Browse files Browse the repository at this point in the history
This is a blind retry for now, meaning we are not checking http status
code result. Could be future enhancement if needed
  • Loading branch information
mkobaly committed Feb 22, 2021
1 parent 3ba42cd commit 8882f6b
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.7
0.2.0
28 changes: 26 additions & 2 deletions elastic/schemaChange.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"path/filepath"
"strconv"
"strings"
)

Expand All @@ -15,6 +16,7 @@ type SchemaChange struct {
FileName string
ID string
Action Action
Retrys int
}

// NewSchemaChange will get keys (folder & filename)
Expand All @@ -27,11 +29,11 @@ func NewSchemaChange(file string) *SchemaChange {
s.Folder = folder
s.FileName = filename
s.ID = id
s.Action = getAction(file)
s.Action, s.Retrys = parseFile(file)
return s
}

func getAction(esFile string) Action {
func parseFile(esFile string) (Action, int) {
file, err := os.Open(esFile)
if err != nil {
log.Fatal(err)
Expand All @@ -45,6 +47,11 @@ func getAction(esFile string) Action {
scanner.Scan()
url := scanner.Text()

url, retry, err := parseURL(url)
if err != nil {
log.Fatal(err)
}

var body bytes.Buffer
for scanner.Scan() {
body.WriteString(scanner.Text())
Expand All @@ -57,5 +64,22 @@ func getAction(esFile string) Action {
HTTPVerb: verb,
URL: url,
JSON: body.String(),
}, retry
}

//parseURL will take a URL and look for the retry option
//Returns URL, retry count, error
func parseURL(url string) (string, int, error) {
parts := strings.Split(url, "retry=")
if len(parts) == 2 {
//see if ?retry=x or &retry=x present
u := strings.TrimSuffix(parts[0], "&")
u = strings.TrimSuffix(u, "?")
retry, err := strconv.Atoi(parts[1])
if err != nil {
return u, 0, err
}
return u, retry, nil
}
return parts[0], 0, nil
}
40 changes: 40 additions & 0 deletions elastic/schemaChange_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package elastic

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseUrlWithNonNumericRetry(t *testing.T) {
_, _, err := parseURL("idm_employee_v5/_update_by_query?retry=a")
assert.Error(t, err)
}

func TestParseUrlWithRetry(t *testing.T) {
url, retry, err := parseURL("idm_employee_v5/_update_by_query?retry=3")
assert.NoError(t, err)
assert.Equal(t, "idm_employee_v5/_update_by_query", url)
assert.Equal(t, 3, retry)
}

func TestParseUrlWithUppercaseRetry(t *testing.T) {
url, retry, err := parseURL("idm_employee_v5/_update_by_query?RETRY=3")
assert.NoError(t, err)
assert.Equal(t, "idm_employee_v5/_update_by_query?RETRY=3", url)
assert.Equal(t, 0, retry)
}

func TestParseUrlEndingWithRetry(t *testing.T) {
url, retry, err := parseURL("idm_employee_v5/_update_by_query?foo=bar&retry=2")
assert.NoError(t, err)
assert.Equal(t, "idm_employee_v5/_update_by_query?foo=bar", url)
assert.Equal(t, 2, retry)
}

func TestParseUrlNoRetry(t *testing.T) {
url, retry, err := parseURL("idm_employee_v5/_update_by_query?foo=bar")
assert.NoError(t, err)
assert.Equal(t, "idm_employee_v5/_update_by_query?foo=bar", url)
assert.Equal(t, 0, retry)
}
44 changes: 31 additions & 13 deletions elastic/schemaChanger.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,24 @@ func (s *EsSchemaChanger) Apply(sc *SchemaChange) error {
req.Header.Add("Content-Type", "application/json")
}

resp, err := s.HTTPClient.Do(req)
if err != nil || resp.StatusCode != 200 {
bodyBytes, err2 := ioutil.ReadAll(resp.Body)
if err2 != nil {
return err2
}
bodyString := string(bodyBytes)
return ErrSchemaChange{
Message: bodyString,
err := retry(sc.Retrys, time.Second, func() error {
resp, err := s.HTTPClient.Do(req)
if err != nil || resp.StatusCode != 200 {
bodyBytes, err2 := ioutil.ReadAll(resp.Body)
if err2 != nil {
return err2
}
bodyString := string(bodyBytes)
return ErrSchemaChange{
Message: bodyString,
}
}
defer resp.Body.Close()
return err
})
if err != nil {
return err
}
defer resp.Body.Close()

// successfully applied schema so now track its completed
return s.markScheamaChangeComplete(sc)
Expand Down Expand Up @@ -176,6 +182,18 @@ func (s *EsSchemaChanger) initialize() {

}

func retry(attempts int, sleep time.Duration, fn func() error) error {
if err := fn(); err != nil {

if attempts--; attempts > 0 {
time.Sleep(sleep)
return retry(attempts, 2*sleep, fn)
}
return err
}
return nil
}

const index = "esdeploy_v1"
const esType = "version_info"
const typeDefinition = `
Expand All @@ -184,9 +202,9 @@ const typeDefinition = `
"_id" : { "path" : "id" },
"properties": {
"id": { "type": "string", "index" : "not_analyzed" },
"folder": { "type": "string", "index" : "not_analyzed" },
"file": { "type": "string", "index" : "not_analyzed" },
"machine": { "type": "string", "index" : "not_analyzed" },
"folder": { "type": "string", "index" : "not_analyzed" },
"file": { "type": "string", "index" : "not_analyzed" },
"machine": { "type": "string", "index" : "not_analyzed" },
"dateRunUtc": {"type": "date", "format": "dateOptionalTime", "index" : "not_analyzed" }
}
}
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ go 1.14

require (
github.com/alecthomas/kingpin v2.2.6+incompatible
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/fatih/color v1.9.0
github.com/mattn/go-colorable v0.1.7
github.com/mattn/go-isatty v0.0.12
golang.org/x/sys v0.0.0-20200810151505-1b9f1253b3ed
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/stretchr/testify v1.4.0
golang.org/x/sys v0.0.0-20200810151505-1b9f1253b3ed // indirect
)
18 changes: 5 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,40 +1,32 @@
github.com/alecthomas/kingpin v2.2.4-0.20170217220530-bf17db2c43f5+incompatible h1:AqrnLgtsvJHbN+on/6wx+HOLYPy6pH7HzRBWqDsTAFQ=
github.com/alecthomas/kingpin v2.2.4-0.20170217220530-bf17db2c43f5+incompatible/go.mod h1:59OFYbFVLKQKq+mqrL6Rw5bR0c3ACQaawgXx0QYndlE=
github.com/alecthomas/kingpin v2.2.6+incompatible h1:5svnBTFgJjZvGKyYBtMB0+m5wvrbUHiqye8wRJMlnYI=
github.com/alecthomas/kingpin v2.2.6+incompatible/go.mod h1:59OFYbFVLKQKq+mqrL6Rw5bR0c3ACQaawgXx0QYndlE=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.4.0 h1:zw+qNRz7futKZsgbBmT5ffigcBJLFNu0TZAxbTxFr8U=
github.com/fatih/color v1.4.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/mattn/go-colorable v0.0.8-0.20170221004137-d898aa9fb31c h1:92u24RLD37W+Dz6UhUUmyu0KGChKGvN3WeO97ia7FQI=
github.com/mattn/go-colorable v0.0.8-0.20170221004137-d898aa9fb31c/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw=
github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.0-20170216235908-dda3de49cbfc h1:pK7tzC30erKOTfEDCYGvPZQCkmM9X5iSmmAR5m9x3Yc=
github.com/mattn/go-isatty v0.0.0-20170216235908-dda3de49cbfc/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/sys v0.0.0-20170223225100-e4594059fe4c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200810151505-1b9f1253b3ed h1:WBkVNH1zd9jg/dK4HCM4lNANnmd12EHC9z+LmcCG4ns=
golang.org/x/sys v0.0.0-20200810151505-1b9f1253b3ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func main() {
var cred elastic.Creds

switch kingpin.MustParse(app.Parse(os.Args[1:])) {

case versionCmd.FullCommand():
color.Cyan("version %v", version)
os.Exit(0)
Expand All @@ -70,6 +71,7 @@ func main() {

color.Cyan("Validation completed")
os.Exit(exit)

//Dry run
case drCmd.FullCommand():
if *drPath == "" {
Expand All @@ -89,6 +91,7 @@ func main() {
color.Green("%v", r)
}
color.Cyan("Dry Run completed")

//Full deployment
case deployCmd.FullCommand():
if *dPath == "" {
Expand Down

0 comments on commit 8882f6b

Please sign in to comment.