Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add Maglev hashing LB algorithm #554

Merged
merged 18 commits into from
May 16, 2023
Merged

Conversation

shawnh2
Copy link
Contributor

@shawnh2 shawnh2 commented Mar 30, 2023

  • add Maglev hash load balancing algorithm
  • rename original consistent hash to ring-hash

@codecov-commenter
Copy link

codecov-commenter commented Mar 31, 2023

Codecov Report

Merging #554 (b80db56) into develop (52728f8) will increase coverage by 0.04%.
The diff coverage is 76.43%.

❗ Current head b80db56 differs from pull request most recent head f0022c6. Consider uploading reports for the commit f0022c6 to get more accurate results

@@             Coverage Diff             @@
##           develop     #554      +/-   ##
===========================================
+ Coverage    54.71%   54.75%   +0.04%     
===========================================
  Files          667      669       +2     
  Lines        78143    78323     +180     
===========================================
+ Hits         42756    42886     +130     
- Misses       31733    31778      +45     
- Partials      3654     3659       +5     
Impacted Files Coverage Δ
pixiu/pkg/server/cluster_manager.go 36.84% <0.00%> (ø)
...xiu/pkg/cluster/loadbalancer/maglev/maglev_hash.go 62.96% <62.96%> (ø)
...xiu/pkg/cluster/loadbalancer/maglev/permutation.go 80.71% <80.71%> (ø)
...xiu/pkg/cluster/loadbalancer/ringhash/ring_hash.go 71.42% <84.21%> (ø)

... and 19 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@mark4z mark4z self-requested a review April 2, 2023 12:11
@shawnh2 shawnh2 requested a review from baerwang April 5, 2023 04:15
@shawnh2 shawnh2 requested a review from baerwang April 5, 2023 13:45
@baerwang
Copy link
Member

baerwang commented Apr 6, 2023

分配好像不均衡,result map[1:6 2:2 3:4 4:5 5:3]

func TestMaglevHash(t *testing.T) {

	nodeCount := 5

	nodes := make([]*model.Endpoint, 0, nodeCount)

	for i := 1; i <= nodeCount; i++ {
		name := strconv.Itoa(i)
		nodes = append(nodes, &model.Endpoint{ID: name, Name: name,
			Address: model.SocketAddress{Address: "192.168.1." + name, Port: 1000 + i}})
	}

	cluster := &model.ClusterConfig{
		Name:           "test-cluster",
		Endpoints:      nodes,
		LbStr:          model.LoadBalanceMaglevHashing,
		ConsistentHash: model.ConsistentHash{ReplicaFactor: 10},
	}
	cluster.CreateConsistentHash()

	var (
		hashing = MaglevHash{}
		path    string
	)

	record := make(map[string]int, nodeCount)

	for i := 1; i <= 20; i++ {
		path = fmt.Sprintf("/pixiu?total=%d", i)
		handler := hashing.Handler(cluster, &http.HttpContext{Request: &stdHttp.Request{Method: stdHttp.MethodGet, RequestURI: path}})
		if _, ok := record[handler.ID]; ok {
			record[handler.ID]++
			continue
		}
		record[handler.ID] = 1
	}

	t.Log(record)
}

@shawnh2
Copy link
Contributor Author

shawnh2 commented Apr 6, 2023

分配好像不均衡,result map[1:6 2:2 3:4 4:5 5:3]

maglev hash 算法要求的是 LookUpTable 的表长度(也就是其结构体内 size 字段的值)必须是个质数,这样可以保证均衡。

但在目前的实现中,这一点我没有去保证,我来补充一下。

@shawnh2
Copy link
Contributor Author

shawnh2 commented Apr 7, 2023

分配好像不均衡,result map[1:6 2:2 3:4 4:5 5:3]

maglev hash 算法要求的是 LookUpTable 的表长度(也就是其结构体内 size 字段的值)必须是个质数,这样可以保证均衡。

但在目前的实现中,这一点我没有去保证,我来补充一下。

fixed, @baerwang PTAL

@mark4z
Copy link
Member

mark4z commented Apr 9, 2023

Using a simple benchmark testing tool https://github.com/mark4z/hey to test the effect of load balancing, 1 of 5 endpoints always can not receive requests, and starts to get 503 frequently after a period of time.

image

@mark4z
Copy link
Member

mark4z commented Apr 9, 2023

---
static_resources:
  listeners:
    - name: "net/http"
      protocol_type: "HTTP"
      address:
        socket_address:
          address: "0.0.0.0"
          port: 8888
      filter_chains:
        filters:
          - name: dgp.filter.httpconnectionmanager
            config:
              route_config:
                routes:
                  - match:
                      prefix: "/count"
                    route:
                      cluster: "count"
                      cluster_not_found_response_code: 505
              http_filters:
                - name: dgp.filter.http.httpproxy
                  config:
      config:
        idle_timeout: 5s
        read_timeout: 5s
        write_timeout: 5s
  clusters:
    - name: "count"
      lb_policy: "MaglevHashing"
      endpoints:
        - id: 1
          socket_address:
            address: localhost
            port: 8080
        - id: 2
          socket_address:
            address: localhost
            port: 8081
        - id: 3
          socket_address:
            address: localhost
            port: 8082
        - id: 4
          socket_address:
            address: localhost
            port: 8083
        - id: 5
          socket_address:
            address: localhost
            port: 8084
  shutdown_config:
    timeout: "60s"
    step_timeout: "10s"
    reject_policy: "immediacy"

@shawnh2
Copy link
Contributor Author

shawnh2 commented Apr 9, 2023

Using a simple benchmark testing tool https://github.com/mark4z/hey to test the effect of load balancing, 1 of 5 endpoints always can not receive requests, and starts to get 503 frequently after a period of time.

此次测试,由于该版代码并没有在 pluginregistry 中注册,所以压测的实质上是默认的 rand 策略。至于为什么总有一个节点接收不到请求,我看了一下是 rand 的随机区间设置的有问题,目前已更正。

@shawnh2
Copy link
Contributor Author

shawnh2 commented Apr 9, 2023

new benchmark result on Maglev hash, using same config as #554 (comment)

image

endpoints 1 recv: 5847
endpoints 2 recv: 5840
endpoints 3 recv: 5770
endpoints 4 recv: 5782
endpoints 5 recv: 5675

@shawnh2 shawnh2 requested a review from mark4z April 9, 2023 15:50
@baerwang baerwang self-requested a review April 10, 2023 11:56
@sonarcloud
Copy link

sonarcloud bot commented Apr 11, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 7 Code Smells

No Coverage information No Coverage information
1.9% 1.9% Duplication

@mark4z
Copy link
Member

mark4z commented Apr 16, 2023

new benchmark result on Maglev hash, using same config as #554 (comment)

image

endpoints 1 recv: 5847 endpoints 2 recv: 5840 endpoints 3 recv: 5770 endpoints 4 recv: 5782 endpoints 5 recv: 5675

nice job

@mark4z mark4z requested a review from ma642 May 7, 2023 12:16
@AlexStocks AlexStocks merged commit 8a01f68 into apache:develop May 16, 2023
@shawnh2 shawnh2 deleted the lb-maglev branch May 16, 2023 07:29
@mark4z mark4z added this to the v1.0.0 milestone Aug 27, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants