Skip to content

Implement load balancer#21

Merged
friedrichg merged 12 commits intomainfrom
load-balancer
May 5, 2023
Merged

Implement load balancer#21
friedrichg merged 12 commits intomainfrom
load-balancer

Conversation

@dogukanteber
Copy link
Copy Markdown
Collaborator

@dogukanteber dogukanteber commented May 1, 2023

This PR attempts to implement round robin load balancer

Signed-off-by: Doğukan Teber <dogukanteber1@hotmail.com>
Signed-off-by: Doğukan Teber <dogukanteber1@hotmail.com>
Signed-off-by: Doğukan Teber <dogukanteber1@hotmail.com>
Signed-off-by: Doğukan Teber <dogukanteber1@hotmail.com>
Signed-off-by: Doğukan Teber <dogukanteber1@hotmail.com>
Signed-off-by: Doğukan Teber <dogukanteber1@hotmail.com>
Signed-off-by: Doğukan Teber <dogukanteber1@hotmail.com>
Copy link
Copy Markdown
Member

@friedrichg friedrichg left a comment

Choose a reason for hiding this comment

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

Good start!

} else {
lb.Lock()
lb.ips = ipsToStrings(ips)
lb.currentIndex = 0
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am not sure about this, as resetting to zero means we will use the first resolved IP more than others.

But I think tests will give the final veredict: Include some tests that prove every ip address gets similar number of requests over many refresh dns.

Signed-off-by: Doğukan Teber <dogukanteber1@hotmail.com>
…ip address in the resolved ips

Signed-off-by: Doğukan Teber <dogukanteber1@hotmail.com>
…fter roundtrip

Signed-off-by: Doğukan Teber <dogukanteber1@hotmail.com>
Signed-off-by: Doğukan Teber <dogukanteber1@hotmail.com>
Signed-off-by: Doğukan Teber <dogukanteber1@hotmail.com>
@dogukanteber
Copy link
Copy Markdown
Collaborator Author

I have added the necessary test @friedrichg. I will handle the log issue in a separate PR. I also would like to refactor the other problem in a separate PR. Let me know if we need to expand the test cases for balancing the load for the resolved ips

@dogukanteber dogukanteber requested a review from friedrichg May 5, 2023 09:06
Copy link
Copy Markdown
Member

@friedrichg friedrichg left a comment

Choose a reason for hiding this comment

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

This is a first initial attempt. Let's merge it as is and let's work on improvements later

url, err := url.Parse(upstream.URL)
if err != nil {
// TODO: log the error with logrus
fmt.Println(err)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should exit the function with error. We don't want to continue if the url is not valid

resolver := DefaultDNSResolver{}
t := &CustomTransport{
Transport: *http.DefaultTransport.(*http.Transport).Clone(),
lb: newRoundRobinLoadBalancer(url.Hostname(), resolver.LookupIP),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

newRoundRobinLoadBalancer returns an error too. capture that and if it is nil and return.
We want to abort if we can't resolve the dns to ips.


reverseProxy := httputil.NewSingleHostReverseProxy(url)
reverseProxy.Transport = customTransport(component, timeouts)
reverseProxy.Transport = customTransport(component, upstream)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

capture the error and exit the function too

}

func (m mockDNSResolver) LookupIP(hostname string) ([]net.IP, error) {
return m.IPs, m.Err
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unfortunately this is not how it works. The DNS returns IPs in different order every time.

for example:

First try

$ nslookup cortexmetrics.io
Server:		10.128.4.247
Address:	10.128.4.247#53

Non-authoritative answer:
Name:	cortexmetrics.io
Address: 172.67.146.72
Name:	cortexmetrics.io
Address: 104.21.73.166

Second try

$ nslookup cortexmetrics.io
Server:		10.128.4.247
Address:	10.128.4.247#53

Non-authoritative answer:
Name:	cortexmetrics.io
Address: 104.21.73.166
Name:	cortexmetrics.io
Address: 172.67.146.72

I think if you randomize the order here, it can get pretty similar, though

tolerance float64
}{
{
name: "4 IPs, 1000 requests, 1 second refresh interval, 10% tolerance",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

10% tolerance is probably too much. We will improve it in a follow up PR

@friedrichg friedrichg merged commit bde969d into main May 5, 2023
@friedrichg friedrichg deleted the load-balancer branch May 5, 2023 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants