github.com/weaviate/weaviate@v1.24.6/usecases/scaler/distribution.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package scaler
    13  
    14  import (
    15  	"fmt"
    16  
    17  	"github.com/weaviate/weaviate/usecases/sharding"
    18  )
    19  
    20  type (
    21  	// ShardDist shard distribution over nodes
    22  	ShardDist map[string][]string
    23  	// nodeShardDist map a node its shard distribution
    24  	nodeShardDist map[string]ShardDist
    25  )
    26  
    27  // distributions returns shard distribution for local node as well as remote nodes
    28  func distributions(before, after *sharding.State) (ShardDist, nodeShardDist) {
    29  	localDist := make(ShardDist, len(before.Physical))
    30  	nodeDist := make(map[string]ShardDist)
    31  	for name := range before.Physical {
    32  		newNodes := difference(after.Physical[name].BelongsToNodes, before.Physical[name].BelongsToNodes)
    33  		if before.IsLocalShard(name) {
    34  			localDist[name] = newNodes
    35  		} else {
    36  			belongsTo := before.Physical[name].BelongsToNode()
    37  			dist := nodeDist[belongsTo]
    38  			if dist == nil {
    39  				dist = make(map[string][]string)
    40  				nodeDist[belongsTo] = dist
    41  			}
    42  			dist[name] = newNodes
    43  		}
    44  	}
    45  	return localDist, nodeDist
    46  }
    47  
    48  // nodes return node names
    49  func (m nodeShardDist) nodes() []string {
    50  	ns := make([]string, 0, len(m))
    51  	for node := range m {
    52  		ns = append(ns, node)
    53  	}
    54  	return ns
    55  }
    56  
    57  // hosts resolve node names into host addresses
    58  func hosts(nodes []string, resolver cluster) ([]string, error) {
    59  	hs := make([]string, len(nodes))
    60  	for i, node := range nodes {
    61  		host, ok := resolver.NodeHostname(node)
    62  		if !ok {
    63  			return nil, fmt.Errorf("%w, %q", ErrUnresolvedName, node)
    64  		}
    65  		hs[i] = host
    66  	}
    67  	return hs, nil
    68  }
    69  
    70  // shards return names of all shards
    71  func (m ShardDist) shards() []string {
    72  	ns := make([]string, 0, len(m))
    73  	for node := range m {
    74  		ns = append(ns, node)
    75  	}
    76  	return ns
    77  }
    78  
    79  // difference returns elements in xs which doesn't exists in ys
    80  func difference(xs, ys []string) []string {
    81  	m := make(map[string]struct{}, len(ys))
    82  	for _, y := range ys {
    83  		m[y] = struct{}{}
    84  	}
    85  	rs := make([]string, 0, len(ys))
    86  	for _, x := range xs {
    87  		if _, ok := m[x]; !ok {
    88  			rs = append(rs, x)
    89  		}
    90  	}
    91  	return rs
    92  }