Signed-off-by: Andrew Mason <amason@slack-corp.com>
This commit is contained in:
Andrew Mason 2021-09-19 19:16:44 -04:00
Родитель a938f67f30
Коммит 76db205e66
4 изменённых файлов: 36 добавлений и 53 удалений

Просмотреть файл

@ -189,3 +189,17 @@ func ResolveIPv4Addrs(addr string) ([]string, error) {
}
return result, nil
}
// NormalizeIP normalizes loopback addresses to avoid spurious errors when
// communicating to different representations of the loopback.
//
// Note: this also maps IPv6 localhost to IPv4 localhost, as
// TabletManagerClient.GetReplicas() (the only place this function is used on)
// will return only IPv4 addresses.
func NormalizeIP(s string) string {
if ip := net.ParseIP(s); ip != nil && ip.IsLoopback() {
return "127.0.0.1"
}
return s
}

Просмотреть файл

@ -171,3 +171,20 @@ func TestResolveIPv4Addrs(t *testing.T) {
})
}
}
func TestNormalizeIP(t *testing.T) {
table := map[string]string{
"1.2.3.4": "1.2.3.4",
"127.0.0.1": "127.0.0.1",
"127.0.1.1": "127.0.0.1",
// IPv6 must be mapped to IPv4.
"::1": "127.0.0.1",
// An unparseable IP should be returned as is.
"127.": "127.",
}
for input, want := range table {
if got := NormalizeIP(input); got != want {
t.Errorf("NormalizeIP(%#v) = %#v, want %#v", input, got, want)
}
}
}

Просмотреть файл

@ -19,11 +19,11 @@ package wrangler
import (
"errors"
"fmt"
"net"
"sync"
"context"
"vitess.io/vitess/go/netutil"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/topoproto"
@ -176,16 +176,6 @@ func (wr *Wrangler) validateShard(ctx context.Context, keyspace, shard string, p
}
}
func normalizeIP(ip string) string {
// Normalize loopback to avoid spurious validation errors.
if parsedIP := net.ParseIP(ip); parsedIP != nil && parsedIP.IsLoopback() {
// Note that this also maps IPv6 localhost to IPv4 localhost
// as GetReplicas() will return only IPv4 addresses.
return "127.0.0.1"
}
return ip
}
func (wr *Wrangler) validateReplication(ctx context.Context, shardInfo *topo.ShardInfo, tabletMap map[string]*topo.TabletInfo, results chan<- error) {
if shardInfo.PrimaryAlias == nil {
results <- fmt.Errorf("no primary in shard record %v/%v", shardInfo.Keyspace(), shardInfo.ShardName())
@ -217,15 +207,15 @@ func (wr *Wrangler) validateReplication(ctx context.Context, shardInfo *topo.Sha
results <- fmt.Errorf("could not resolve IP for tablet %s: %v", tablet.Tablet.MysqlHostname, err)
continue
}
tabletIPMap[normalizeIP(ip)] = tablet.Tablet
tabletIPMap[netutil.NormalizeIP(ip)] = tablet.Tablet
}
// See if every replica is in the replication graph.
for _, replicaAddr := range replicaList {
if tabletIPMap[normalizeIP(replicaAddr)] == nil {
if tabletIPMap[netutil.NormalizeIP(replicaAddr)] == nil {
results <- fmt.Errorf("replica %v not in replication graph for shard %v/%v (mysql instance without vttablet?)", replicaAddr, shardInfo.Keyspace(), shardInfo.ShardName())
}
replicaIPMap[normalizeIP(replicaAddr)] = true
replicaIPMap[netutil.NormalizeIP(replicaAddr)] = true
}
// See if every entry in the replication graph is connected to the primary.
@ -239,7 +229,7 @@ func (wr *Wrangler) validateReplication(ctx context.Context, shardInfo *topo.Sha
results <- fmt.Errorf("could not resolve IP for tablet %s: %v", tablet.Tablet.MysqlHostname, err)
continue
}
if !replicaIPMap[normalizeIP(ip)] {
if !replicaIPMap[netutil.NormalizeIP(ip)] {
results <- fmt.Errorf("replica %v not replicating: %v replica list: %q", topoproto.TabletAliasString(tablet.Alias), ip, replicaList)
}
}

Просмотреть файл

@ -1,38 +0,0 @@
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package wrangler
import (
"testing"
)
func TestNormalizeIP(t *testing.T) {
table := map[string]string{
"1.2.3.4": "1.2.3.4",
"127.0.0.1": "127.0.0.1",
"127.0.1.1": "127.0.0.1",
// IPv6 must be mapped to IPv4.
"::1": "127.0.0.1",
// An unparseable IP should be returned as is.
"127.": "127.",
}
for input, want := range table {
if got := normalizeIP(input); got != want {
t.Errorf("normalizeIP(%#v) = %#v, want %#v", input, got, want)
}
}
}