Skip to content

Commit a0d7213

Browse files
committed
ads mode support consistent hash
Signed-off-by: 1640528278@qq.com <1640528278@qq.com>
1 parent 749707a commit a0d7213

File tree

6 files changed

+10
-24
lines changed

6 files changed

+10
-24
lines changed

pkg/bpf/ads/loader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import (
3232

3333
"kmesh.net/kmesh/daemon/options"
3434
"kmesh.net/kmesh/pkg/bpf/restart"
35-
"kmesh.net/kmesh/pkg/logger"
3635
"kmesh.net/kmesh/pkg/consistenthash/maglev"
36+
"kmesh.net/kmesh/pkg/logger"
3737
)
3838

3939
var log = logger.NewLoggerScope("bpf_ads")

pkg/cache/v2/cluster.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import (
2929
core_v2 "kmesh.net/kmesh/api/v2/core"
3030
bpfads "kmesh.net/kmesh/pkg/bpf/ads"
3131
maps_v2 "kmesh.net/kmesh/pkg/cache/v2/maps"
32-
"kmesh.net/kmesh/pkg/utils"
3332
"kmesh.net/kmesh/pkg/consistenthash/maglev"
33+
"kmesh.net/kmesh/pkg/utils"
3434
)
3535

3636
type ClusterStatsKey struct {
@@ -181,8 +181,8 @@ func (cache *ClusterCache) Flush() {
181181
err := maps_v2.ClusterUpdate(name, cluster)
182182
if cluster.GetLbPolicy() == cluster_v2.Cluster_MAGLEV {
183183
// create consistent lb here and update table to bpf map
184-
if err := maglev.CreateLB(cluster);err != nil {
185-
log.Errorf("maglev lb update %v cluster failed: %v",name, err)
184+
if err := maglev.CreateLB(cluster); err != nil {
185+
log.Errorf("maglev lb update %v cluster failed: %v", name, err)
186186
}
187187
}
188188
if err == nil {

pkg/consistenthash/maglev/maglev.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"unsafe"
2424

2525
"github.com/cilium/ebpf"
26-
26+
2727
cluster_v2 "kmesh.net/kmesh/api/v2/cluster"
2828
"kmesh.net/kmesh/api/v2/endpoint"
2929
"kmesh.net/kmesh/pkg/utils/hash"

pkg/consistenthash/maglev/maglev_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
"github.com/cilium/ebpf"
2626
"github.com/stretchr/testify/suite"
27-
27+
2828
cluster_v2 "kmesh.net/kmesh/api/v2/cluster"
2929
"kmesh.net/kmesh/api/v2/core"
3030
"kmesh.net/kmesh/api/v2/endpoint"

pkg/controller/ads/cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,10 @@ func newApiRouteAction(action *config_route_v3.RouteAction) *route_v2.RouteActio
474474
return nil
475475
}
476476
// current only support http header based hash policy
477-
apiHashPolicys := make([]*route_v2.RouteAction_HashPolicy,0)
477+
apiHashPolicys := make([]*route_v2.RouteAction_HashPolicy, 0)
478478
for _, hp := range action.GetHashPolicy() {
479479
apiHashPolicy := &route_v2.RouteAction_HashPolicy{
480-
PolicySpecifier: nil,
480+
PolicySpecifier: nil,
481481
}
482482
switch hp.GetPolicySpecifier().(type) {
483483
case *config_route_v3.RouteAction_HashPolicy_Header_:

pkg/utils/hash/murmur3.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
17-
package hash
1816

17+
package hash
1918

2019
import (
2120
"unsafe"
2221
)
2322

24-
2523
// Hash128 calculates a 128 bits hash for the given data. It returns different
2624
// results when running on big-endian and little-endian machines.
2725
//
@@ -33,20 +31,16 @@ func Hash128(data []byte, seed uint32) (uint64, uint64) {
3331
c2 = uint64(0x4cf5ad432745937f)
3432
)
3533

36-
3734
nblocks := len(data) / 16
3835

39-
4036
h1 := uint64(seed)
4137
h2 := uint64(seed)
4238

43-
4439
for i := 0; i < nblocks; i++ {
4540
tmp := (*[2]uint64)(unsafe.Pointer(&data[i*16]))
4641
k1 := tmp[0]
4742
k2 := tmp[1]
4843

49-
5044
k1 *= c1
5145
k1 = rotl64(k1, 31)
5246
k1 *= c2
@@ -63,14 +57,11 @@ func Hash128(data []byte, seed uint32) (uint64, uint64) {
6357
h2 = h2*5 + 0x38495ab5
6458
}
6559

66-
6760
k1 := uint64(0)
6861
k2 := uint64(0)
6962

70-
7163
tail := data[nblocks*16:]
7264

73-
7465
switch len(tail) & 15 {
7566
case 15:
7667
k2 ^= uint64(tail[14]) << 48
@@ -126,7 +117,6 @@ func Hash128(data []byte, seed uint32) (uint64, uint64) {
126117
h1 ^= k1
127118
}
128119

129-
130120
h1 ^= uint64(len(data))
131121
h2 ^= uint64(len(data))
132122
h1 += h2
@@ -136,23 +126,19 @@ func Hash128(data []byte, seed uint32) (uint64, uint64) {
136126
h1 += h2
137127
h2 += h1
138128

139-
140129
return h1, h2
141130
}
142131

143-
144132
func rotl64(x uint64, r int8) uint64 {
145133
return (x << r) | (x >> (64 - r))
146134
}
147135

148-
149136
func fmix64(k uint64) uint64 {
150137
k ^= k >> 33
151138
k *= 0xff51afd7ed558ccd
152139
k ^= k >> 33
153140
k *= 0xc4ceb9fe1a85ec53
154141
k ^= k >> 33
155142

156-
157143
return k
158-
}
144+
}

0 commit comments

Comments
 (0)