Skip to content

Commit

Permalink
Cleanup of dead code (#14799)
Browse files Browse the repository at this point in the history
Signed-off-by: Dirkjan Bussink <[email protected]>
  • Loading branch information
dbussink authored Dec 19, 2023
1 parent c41d24b commit 08d48cf
Show file tree
Hide file tree
Showing 35 changed files with 4 additions and 1,783 deletions.
12 changes: 0 additions & 12 deletions go/cmd/vtctldclient/cli/pflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,11 @@ package cli
import (
"github.com/spf13/pflag"

"vitess.io/vitess/go/flagutil"
"vitess.io/vitess/go/vt/topo/topoproto"

topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)

// StringMapValue augments flagutil.StringMapValue so it can be used as a
// pflag.Value.
type StringMapValue struct {
flagutil.StringMapValue
}

// Type is part of the pflag.Value interface.
func (v *StringMapValue) Type() string {
return "cli.StringMapValue"
}

// KeyspaceTypeFlag adds the pflag.Value interface to a topodatapb.KeyspaceType.
type KeyspaceTypeFlag topodatapb.KeyspaceType

Expand Down
4 changes: 2 additions & 2 deletions go/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Unwrap(err error) []error {
return nil
}

// Unwrap unwraps an error created by errors.Join() in Go 1.20, into its components, recursively
// UnwrapAll unwraps an error created by errors.Join() in Go 1.20, into its components, recursively
func UnwrapAll(err error) (errs []error) {
if err == nil {
return nil
Expand All @@ -46,7 +46,7 @@ func UnwrapAll(err error) (errs []error) {
return []error{err}
}

// Unwrap unwraps an error created by errors.Join() in Go 1.20, into its components, recursively,
// UnwrapFirst unwraps an error created by errors.Join() in Go 1.20, into its components, recursively,
// and returns one (the first) unwrapped error
func UnwrapFirst(err error) error {
if err == nil {
Expand Down
72 changes: 0 additions & 72 deletions go/flagutil/deprecated_float64_seconds.go

This file was deleted.

11 changes: 0 additions & 11 deletions go/hack/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,10 @@ import (
"unsafe"
)

//go:noescape
//go:linkname memhash runtime.memhash
func memhash(p unsafe.Pointer, h, s uintptr) uintptr

//go:noescape
//go:linkname strhash runtime.strhash
func strhash(p unsafe.Pointer, h uintptr) uintptr

// RuntimeMemhash provides access to the Go runtime's default hash function for arbitrary bytes.
// This is an optimal hash function which takes an input seed and is potentially implemented in hardware
// for most architectures. This is the same hash function that the language's `map` uses.
func RuntimeMemhash(b []byte, seed uint64) uint64 {
return uint64(memhash(unsafe.Pointer(unsafe.SliceData(b)), uintptr(seed), uintptr(len(b))))
}

// RuntimeStrhash provides access to the Go runtime's default hash function for strings.
// This is an optimal hash function which takes an input seed and is potentially implemented in hardware
// for most architectures. This is the same hash function that the language's `map` uses.
Expand Down
4 changes: 0 additions & 4 deletions go/mysql/icuregex/internal/uset/unicode_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ func New() *UnicodeSet {
return &UnicodeSet{list: buf}
}

func FromRunes(list []rune) *UnicodeSet {
return &UnicodeSet{list: list}
}

func (u *UnicodeSet) ensureBufferCapacity(c int) {
if cap(u.buffer) < c {
u.buffer = make([]rune, c)
Expand Down
20 changes: 0 additions & 20 deletions go/mysql/replication/replication_position.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,23 +214,3 @@ func (rp *Position) MatchesFlavor(flavor string) bool {
}
return false
}

// Comparable returns whether the receiver is comparable to the supplied position, based on whether one
// of the two positions contains the other.
func (rp *Position) Comparable(other Position) bool {
return rp.GTIDSet.Contains(other.GTIDSet) || other.GTIDSet.Contains(rp.GTIDSet)
}

// AllPositionsComparable returns true if all positions in the supplied list are comparable with one another, and false
// if any are non-comparable.
func AllPositionsComparable(positions []Position) bool {
for i := 0; i < len(positions); i++ {
for j := i + 1; j < len(positions); j++ {
if !positions[i].Comparable(positions[j]) {
return false
}
}
}

return true
}
81 changes: 0 additions & 81 deletions go/netutil/netutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,71 +20,13 @@ package netutil
import (
"bytes"
"fmt"
"math/rand"
"net"
"os"
"sort"
"strconv"
"strings"
"time"
)

// byPriorityWeight sorts records by ascending priority and weight.
type byPriorityWeight []*net.SRV

func (addrs byPriorityWeight) Len() int { return len(addrs) }

func (addrs byPriorityWeight) Swap(i, j int) { addrs[i], addrs[j] = addrs[j], addrs[i] }

func (addrs byPriorityWeight) Less(i, j int) bool {
return addrs[i].Priority < addrs[j].Priority ||
(addrs[i].Priority == addrs[j].Priority && addrs[i].Weight < addrs[j].Weight)
}

// shuffleByWeight shuffles SRV records by weight using the algorithm
// described in RFC 2782.
// NOTE(msolo) This is disabled when the weights are zero.
func (addrs byPriorityWeight) shuffleByWeight(rand *rand.Rand) {
sum := 0
for _, addr := range addrs {
sum += int(addr.Weight)
}
for sum > 0 && len(addrs) > 1 {
s := 0
n := rand.Intn(sum)
for i := range addrs {
s += int(addrs[i].Weight)
if s > n {
if i > 0 {
t := addrs[i]
copy(addrs[1:i+1], addrs[0:i])
addrs[0] = t
}
break
}
}
sum -= int(addrs[0].Weight)
addrs = addrs[1:]
}
}

func (addrs byPriorityWeight) sortRfc2782(rand *rand.Rand) {
sort.Sort(addrs)
i := 0
for j := 1; j < len(addrs); j++ {
if addrs[i].Priority != addrs[j].Priority {
addrs[i:j].shuffleByWeight(rand)
i = j
}
}
addrs[i:].shuffleByWeight(rand)
}

// SortRfc2782 reorders SRV records as specified in RFC 2782.
func SortRfc2782(srvs []*net.SRV) {
byPriorityWeight(srvs).sortRfc2782(rand.New(rand.NewSource(time.Now().UTC().UnixNano())))
}

// SplitHostPort is an alternative to net.SplitHostPort that also parses the
// integer port. In addition, it is more tolerant of improperly escaped IPv6
// addresses, such as "::1:456", which should actually be "[::1]:456".
Expand Down Expand Up @@ -164,29 +106,6 @@ func FullyQualifiedHostnameOrPanic() string {
return hostname
}

// ResolveIPv4Addrs resolves the address:port part into IP address:port pairs
func ResolveIPv4Addrs(addr string) ([]string, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
ipAddrs, err := net.LookupIP(host)
if err != nil {
return nil, err
}
result := make([]string, 0, len(ipAddrs))
for _, ipAddr := range ipAddrs {
ipv4 := ipAddr.To4()
if ipv4 != nil {
result = append(result, net.JoinHostPort(ipv4.String(), port))
}
}
if len(result) == 0 {
return nil, fmt.Errorf("no IPv4addr for name %v", host)
}
return result, nil
}

func dnsLookup(host string) ([]net.IP, error) {
addrs, err := net.LookupHost(host)
if err != nil {
Expand Down
98 changes: 0 additions & 98 deletions go/netutil/netutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,9 @@ limitations under the License.
package netutil

import (
"fmt"
"math/rand"
"net"
"reflect"
"testing"
)

func checkDistribution(t *testing.T, rand *rand.Rand, data []*net.SRV, margin float64) {
sum := 0
for _, srv := range data {
sum += int(srv.Weight)
}

results := make(map[string]int)

count := 1000
for j := 0; j < count; j++ {
d := make([]*net.SRV, len(data))
copy(d, data)
byPriorityWeight(d).shuffleByWeight(rand)
key := d[0].Target
results[key] = results[key] + 1
}

actual := results[data[0].Target]
expected := float64(count) * float64(data[0].Weight) / float64(sum)
diff := float64(actual) - expected
t.Logf("actual: %v diff: %v e: %v m: %v", actual, diff, expected, margin)
if diff < 0 {
diff = -diff
}
if diff > (expected * margin) {
t.Errorf("missed target weight: expected %v, %v", expected, actual)
}
}

func testUniformity(t *testing.T, size int, margin float64) {
data := make([]*net.SRV, size)
for i := 0; i < size; i++ {
data[i] = &net.SRV{Target: fmt.Sprintf("%c", 'a'+i), Weight: 1}
}
checkDistribution(t, rand.New(rand.NewSource(1)), data, margin)
}

func TestUniformity(t *testing.T) {
testUniformity(t, 2, 0.05)
testUniformity(t, 3, 0.10)
testUniformity(t, 10, 0.20)
testWeighting(t, 0.05)
}

func testWeighting(t *testing.T, margin float64) {
data := []*net.SRV{
{Target: "a", Weight: 60},
{Target: "b", Weight: 30},
{Target: "c", Weight: 10},
}
checkDistribution(t, rand.New(rand.NewSource(1)), data, margin)
}

func TestWeighting(t *testing.T) {
testWeighting(t, 0.05)
}

func TestSplitHostPort(t *testing.T) {
type addr struct {
host string
Expand Down Expand Up @@ -133,43 +72,6 @@ func TestJoinHostPort(t *testing.T) {
}
}

func TestResolveIPv4Addrs(t *testing.T) {
cases := []struct {
address string
expected []string
expectedError bool
}{
{
address: "localhost:3306",
expected: []string{"127.0.0.1:3306"},
},
{
address: "127.0.0.256:3306",
expectedError: true,
},
{
address: "localhost",
expectedError: true,
},
{
address: "InvalidHost:3306",
expectedError: true,
},
}

for _, c := range cases {
t.Run(c.address, func(t *testing.T) {
got, err := ResolveIPv4Addrs(c.address)
if (err != nil) != c.expectedError {
t.Errorf("expected error but got: %v", err)
}
if !reflect.DeepEqual(got, c.expected) {
t.Errorf("expected: %v, got: %v", c.expected, got)
}
})
}
}

func TestNormalizeIP(t *testing.T) {
table := map[string]string{
"1.2.3.4": "1.2.3.4",
Expand Down
Loading

0 comments on commit 08d48cf

Please sign in to comment.