Skip to content

Commit

Permalink
fqdn check wait time (#8144)
Browse files Browse the repository at this point in the history
Signed-off-by: Sahiba3108 <sgoyal@progress.com>
  • Loading branch information
Sahiba3108 committed Aug 22, 2023
1 parent 8bbdf5b commit 76330e3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"
"net/http"
"strings"
"time"

"github.com/chef/automate/components/automate-cli/pkg/verifyserver/constants"
"github.com/chef/automate/components/automate-cli/pkg/verifyserver/models"
Expand Down Expand Up @@ -36,6 +37,6 @@ func (h *Handler) CheckFqdn(c *fiber.Ctx) error {
return fiber.NewError(http.StatusBadRequest, "node_type should be automate or chef-infra-server, Please provide node_type.")
}

res := h.FqdnService.CheckFqdnReachability(*req, constants.DEFAULT_HTTPS_PORT)
res := h.FqdnService.CheckFqdnReachability(*req, constants.DEFAULT_HTTPS_PORT, time.Minute*1)
return c.JSON(response.BuildSuccessResponse(res))
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/chef/automate/components/automate-cli/pkg/verifyserver/constants"
"github.com/chef/automate/components/automate-cli/pkg/verifyserver/models"
Expand All @@ -19,7 +20,7 @@ import (

func SetupMockFqdnService() fqdnservice.IFqdnService {
return &fqdnservice.MockFqdnService{
CheckFqdnReachabilityFunc: func(req models.FqdnRequest, port string) models.FqdnResponse {
CheckFqdnReachabilityFunc: func(req models.FqdnRequest, port string, duration time.Duration) models.FqdnResponse {
return models.FqdnResponse{
Passed: true,
Checks: []models.Checks{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

type IFqdnService interface {
CheckFqdnReachability(models.FqdnRequest, string) models.FqdnResponse
CheckFqdnReachability(models.FqdnRequest, string, time.Duration) models.FqdnResponse
}

type FqdnService struct {
Expand Down Expand Up @@ -111,35 +111,41 @@ func makeSet(reqNodes []string, isAfterDeployment bool) (map[string]int, error)
}

// makeConcurrentCalls function is making 50 concurrent calls for checking node reachability.
func (fq *FqdnService) MakeConcurrentCalls(url string, client *http.Client, setNodes map[string]int) error {
func (fq *FqdnService) MakeConcurrentCalls(url string, client *http.Client, setNodes map[string]int, duration time.Duration) error {
fq.log.Debug("Making Concurrent Calls...")
fqdnResultChan := make(chan string)
minNumberOfCalls := 5 * len(setNodes)
for i := 0; i < minNumberOfCalls; i++ {
go func(fqdnResultChan chan string) {
res, err := client.Get(url)
if err != nil {
fq.log.Error(err.Error())
fqdnResultChan <- constants.CHAN_RESULT_ERROR_MESSAGE
return
}
nodeIP := res.Header.Get(constants.SERVER_IP_HEADER_KEY)
fqdnResultChan <- nodeIP
}(fqdnResultChan)
}

for i := 0; i < minNumberOfCalls; i++ {
chanResult := <-fqdnResultChan
if chanResult == constants.CHAN_RESULT_ERROR_MESSAGE {
continue
timestamp := time.Now().Add(duration)
for time.Now().Before(timestamp) {
for i := 0; i < minNumberOfCalls; i++ {
go func(fqdnResultChan chan string) {
res, err := client.Get(url)
if err != nil {
fq.log.Error(err.Error())
fqdnResultChan <- constants.CHAN_RESULT_ERROR_MESSAGE
return
}
nodeIP := res.Header.Get(constants.SERVER_IP_HEADER_KEY)
fqdnResultChan <- nodeIP
}(fqdnResultChan)
}
delete(setNodes, chanResult)
//if setNodes becomes empty, that means we are able to reach all the nodes given in the request body.
if len(setNodes) == 0 {
fq.log.Debug("All nodes are reachable.")
return nil

for i := 0; i < minNumberOfCalls; i++ {
chanResult := <-fqdnResultChan
if chanResult == constants.CHAN_RESULT_ERROR_MESSAGE {
continue
}
delete(setNodes, chanResult)
//if setNodes becomes empty, that means we are able to reach all the nodes given in the request body.
if len(setNodes) == 0 {
fq.log.Debug("All nodes are reachable.")
return nil
}
}

time.Sleep(5 * time.Second)
}

return errors.New("nodes are not reachable")
}

Expand Down Expand Up @@ -181,7 +187,7 @@ func (fq *FqdnService) fqdnReachable(fqdn, rootCert, nodeType string, isAfterDep
}

// nodeReachable function will check that our load balancer will correctly redirecting to all the nodes or not.
func (fq *FqdnService) nodeReachable(fqdn, rootCert string, reqNodes []string, isAfterDeployment bool, port string) models.Checks {
func (fq *FqdnService) nodeReachable(fqdn, rootCert string, reqNodes []string, isAfterDeployment bool, port string, duration time.Duration) models.Checks {
fq.log.Debug("Checking Node Reachability...")
setNodes, err := makeSet(reqNodes, isAfterDeployment)
if err != nil {
Expand All @@ -199,7 +205,7 @@ func (fq *FqdnService) nodeReachable(fqdn, rootCert string, reqNodes []string, i

client := fq.createClient(rootCert)

err = fq.MakeConcurrentCalls(url, client, setNodes)
err = fq.MakeConcurrentCalls(url, client, setNodes, duration)
if err != nil {
fq.log.Debug("All nodes are not reachable")
errorMessage := createErrorMessage(setNodes, reqNodes, isAfterDeployment)
Expand Down Expand Up @@ -246,7 +252,7 @@ func createCheck(title string, passed bool, successMsg, errorMsg, resolutionMsg
}
}

func (fq *FqdnService) CheckFqdnReachability(req models.FqdnRequest, port string) models.FqdnResponse {
func (fq *FqdnService) CheckFqdnReachability(req models.FqdnRequest, port string, duration time.Duration) models.FqdnResponse {
var response = models.FqdnResponse{}
var check models.Checks

Expand All @@ -268,7 +274,7 @@ func (fq *FqdnService) CheckFqdnReachability(req models.FqdnRequest, port string
response.Checks = append(response.Checks, check)

if fqdnReachabilityCheck {
check = fq.nodeReachable(req.Fqdn, req.RootCert, req.Nodes, req.IsAfterDeployment, port)
check = fq.nodeReachable(req.Fqdn, req.RootCert, req.Nodes, req.IsAfterDeployment, port, duration)
} else {
check = createCheck(constants.NODE_TITLE, false, "", fmt.Sprintf(constants.NODE_ERROR_MESSAGE, req.Nodes), constants.NODE_RESOLUTION_MESSAGE)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ func TestCheckFqdnReachability(t *testing.T) {

for _, e := range tests {
t.Run(e.TestName, func(t *testing.T) {
res := fq.CheckFqdnReachability(e.ReqBody, e.Port)
res := fq.CheckFqdnReachability(e.ReqBody, e.Port, time.Second*2)
assert.Equal(t, e.ResponseBody, res)
})
}
Expand All @@ -744,6 +744,6 @@ func TestMakeConcurrentCalls(t *testing.T) {
Timeout: 2 * time.Second,
}
url := fmt.Sprintf("https://%v:6574", LOCALHOST2)
res := fq.MakeConcurrentCalls(url, client, make(map[string]int))
res := fq.MakeConcurrentCalls(url, client, make(map[string]int), time.Second*2)
assert.Equal(t, errors.New("nodes are not reachable"), res)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package fqdnservice

import "github.com/chef/automate/components/automate-cli/pkg/verifyserver/models"
import (
"time"

"github.com/chef/automate/components/automate-cli/pkg/verifyserver/models"
)

type MockFqdnService struct {
CheckFqdnReachabilityFunc func(models.FqdnRequest, string) models.FqdnResponse
CheckFqdnReachabilityFunc func(models.FqdnRequest, string, time.Duration) models.FqdnResponse
}

func (mfq *MockFqdnService) CheckFqdnReachability(req models.FqdnRequest, port string) models.FqdnResponse {
return mfq.CheckFqdnReachabilityFunc(req, port)
func (mfq *MockFqdnService) CheckFqdnReachability(req models.FqdnRequest, port string, duration time.Duration) models.FqdnResponse {
return mfq.CheckFqdnReachabilityFunc(req, port, duration)
}

0 comments on commit 76330e3

Please sign in to comment.