-
Notifications
You must be signed in to change notification settings - Fork 717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
etcdutil: add dial keep alive params to switch connect as soon as possible #6059
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a716a9f
etcdutil: add dial keep alive params to switch connect as soon as pos…
lhy1024 7924ed7
address comments
lhy1024 f782325
address comments
lhy1024 0e5e7a6
direct return
lhy1024 d2f41d1
add rand
lhy1024 8c89c58
remove parallel
lhy1024 975a400
refactor
lhy1024 7556e8e
Merge branch 'master' into fix-etcd
ti-chi-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,13 +18,19 @@ import ( | |
"context" | ||
"crypto/tls" | ||
"fmt" | ||
"io" | ||
"net" | ||
"strings" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/pingcap/failpoint" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tikv/pd/pkg/utils/tempurl" | ||
"go.etcd.io/etcd/clientv3" | ||
"go.etcd.io/etcd/embed" | ||
"go.etcd.io/etcd/etcdserver/etcdserverpb" | ||
"go.etcd.io/etcd/pkg/types" | ||
) | ||
|
||
|
@@ -54,43 +60,15 @@ func TestMemberHelpers(t *testing.T) { | |
re.Equal(uint64(etcd1.Server.ID()), listResp1.Members[0].ID) | ||
|
||
// Test AddEtcdMember | ||
// Make a new etcd config. | ||
cfg2 := NewTestSingleConfig(t) | ||
cfg2.Name = "etcd2" | ||
cfg2.InitialCluster = cfg1.InitialCluster + fmt.Sprintf(",%s=%s", cfg2.Name, &cfg2.LPUrls[0]) | ||
cfg2.ClusterState = embed.ClusterStateFlagExisting | ||
|
||
// Add it to the cluster above. | ||
peerURL := cfg2.LPUrls[0].String() | ||
addResp, err := AddEtcdMember(client1, []string{peerURL}) | ||
re.NoError(err) | ||
|
||
etcd2, err := embed.StartEtcd(cfg2) | ||
defer func() { | ||
etcd2.Close() | ||
}() | ||
re.NoError(err) | ||
re.Equal(uint64(etcd2.Server.ID()), addResp.Member.ID) | ||
|
||
etcd2 := checkAddEtcdMember(t, cfg1, client1) | ||
cfg2 := etcd2.Config() | ||
defer etcd2.Close() | ||
ep2 := cfg2.LCUrls[0].String() | ||
client2, err := clientv3.New(clientv3.Config{ | ||
Endpoints: []string{ep2}, | ||
}) | ||
re.NoError(err) | ||
|
||
<-etcd2.Server.ReadyNotify() | ||
|
||
listResp2, err := ListEtcdMembers(client2) | ||
re.NoError(err) | ||
re.Len(listResp2.Members, 2) | ||
for _, m := range listResp2.Members { | ||
switch m.ID { | ||
case uint64(etcd1.Server.ID()): | ||
case uint64(etcd2.Server.ID()): | ||
default: | ||
t.Fatalf("unknown member: %v", m) | ||
} | ||
} | ||
checkMembers(re, client2, []*embed.Etcd{etcd1, etcd2}) | ||
|
||
// Test CheckClusterID | ||
urlsMap, err := types.NewURLsMap(cfg2.InitialCluster) | ||
|
@@ -252,45 +230,145 @@ func TestEtcdClientSync(t *testing.T) { | |
<-etcd1.Server.ReadyNotify() | ||
|
||
// Add a new member. | ||
etcd2 := checkAddEtcdMember(t, cfg1, client1) | ||
defer etcd2.Close() | ||
checkMembers(re, client1, []*embed.Etcd{etcd1, etcd2}) | ||
|
||
// Remove the first member and close the etcd1. | ||
_, err = RemoveEtcdMember(client1, uint64(etcd1.Server.ID())) | ||
re.NoError(err) | ||
time.Sleep(20 * time.Millisecond) // wait for etcd client sync endpoints and client will be connected to etcd2 | ||
etcd1.Close() | ||
|
||
// Check the client can get the new member with the new endpoints. | ||
listResp3, err := ListEtcdMembers(client1) | ||
re.NoError(err) | ||
re.Len(listResp3.Members, 1) | ||
re.Equal(uint64(etcd2.Server.ID()), listResp3.Members[0].ID) | ||
|
||
require.NoError(t, failpoint.Disable("github.com/tikv/pd/pkg/utils/etcdutil/autoSyncInterval")) | ||
} | ||
|
||
func TestEtcdWithHangLeader(t *testing.T) { | ||
t.Parallel() | ||
re := require.New(t) | ||
// Start a etcd server. | ||
cfg1 := NewTestSingleConfig(t) | ||
etcd1, err := embed.StartEtcd(cfg1) | ||
re.NoError(err) | ||
ep1 := cfg1.LCUrls[0].String() | ||
<-etcd1.Server.ReadyNotify() | ||
|
||
// Create a proxy to etcd1. | ||
proxyAddr := tempurl.Alloc() | ||
var enableDiscard atomic.Bool | ||
go proxyWithDiscard(re, ep1, proxyAddr, &enableDiscard) | ||
|
||
// Create a etcd client with etcd1 as endpoint. | ||
urls, err := types.NewURLs([]string{proxyAddr}) | ||
re.NoError(err) | ||
client1, err := createEtcdClient(nil, urls) | ||
re.NoError(err) | ||
|
||
// Add a new member and set the client endpoints to etcd1 and etcd2. | ||
etcd2 := checkAddEtcdMember(t, cfg1, client1) | ||
defer etcd2.Close() | ||
checkMembers(re, client1, []*embed.Etcd{etcd1, etcd2}) | ||
etcd2Addr := etcd2.Config().LCUrls[0].String() | ||
client1.SetEndpoints(proxyAddr, etcd2Addr) | ||
|
||
// Hang the etcd1 and wait for the client to connect to etcd2. | ||
enableDiscard.Store(true) | ||
time.Sleep(defaultDialKeepAliveTime + defaultDialKeepAliveTimeout*2) | ||
_, err = EtcdKVGet(client1, "test/key1") | ||
re.NoError(err) | ||
} | ||
|
||
func checkAddEtcdMember(t *testing.T, cfg1 *embed.Config, client *clientv3.Client) *embed.Etcd { | ||
re := require.New(t) | ||
cfg2 := NewTestSingleConfig(t) | ||
cfg2.Name = "etcd2" | ||
cfg2.InitialCluster = cfg1.InitialCluster + fmt.Sprintf(",%s=%s", cfg2.Name, &cfg2.LPUrls[0]) | ||
cfg2.ClusterState = embed.ClusterStateFlagExisting | ||
peerURL := cfg2.LPUrls[0].String() | ||
addResp, err := AddEtcdMember(client1, []string{peerURL}) | ||
addResp, err := AddEtcdMember(client, []string{peerURL}) | ||
re.NoError(err) | ||
etcd2, err := embed.StartEtcd(cfg2) | ||
defer func() { | ||
etcd2.Close() | ||
}() | ||
re.NoError(err) | ||
re.Equal(uint64(etcd2.Server.ID()), addResp.Member.ID) | ||
<-etcd2.Server.ReadyNotify() | ||
return etcd2 | ||
} | ||
|
||
func checkMembers(re *require.Assertions, client *clientv3.Client, etcds []*embed.Etcd) { | ||
// Check the client can get the new member. | ||
listResp2, err := ListEtcdMembers(client1) | ||
re.NoError(err) | ||
re.Len(listResp2.Members, 2) | ||
for _, m := range listResp2.Members { | ||
switch m.ID { | ||
case uint64(etcd1.Server.ID()): | ||
case uint64(etcd2.Server.ID()): | ||
default: | ||
t.Fatalf("unknown member: %v", m) | ||
listResp, err := ListEtcdMembers(client) | ||
re.NoError(err) | ||
re.Len(listResp.Members, len(etcds)) | ||
inList := func(m *etcdserverpb.Member) bool { | ||
for _, etcd := range etcds { | ||
if m.ID == uint64(etcd.Server.ID()) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
for _, m := range listResp.Members { | ||
re.True(inList(m)) | ||
} | ||
} | ||
|
||
// Remove the first member and close the etcd1. | ||
_, err = RemoveEtcdMember(client1, uint64(etcd1.Server.ID())) | ||
func proxyWithDiscard(re *require.Assertions, server, proxy string, enableDiscard *atomic.Bool) { | ||
server = strings.TrimPrefix(server, "http://") | ||
proxy = strings.TrimPrefix(proxy, "http://") | ||
l, err := net.Listen("tcp", proxy) | ||
re.NoError(err) | ||
time.Sleep(20 * time.Millisecond) // wait for etcd client sync endpoints and client will be connected to etcd2 | ||
etcd1.Close() | ||
for { | ||
connect, err := l.Accept() | ||
re.NoError(err) | ||
go func(connect net.Conn) { | ||
serverConnect, err := net.Dial("tcp", server) | ||
re.NoError(err) | ||
pipe(connect, serverConnect, enableDiscard) | ||
}(connect) | ||
} | ||
} | ||
|
||
// Check the client can get the new member with the new endpoints. | ||
listResp3, err := ListEtcdMembers(client1) | ||
re.NoError(err) | ||
re.Len(listResp3.Members, 1) | ||
re.Equal(uint64(etcd2.Server.ID()), listResp3.Members[0].ID) | ||
func pipe(src net.Conn, dst net.Conn, enableDiscard *atomic.Bool) { | ||
errChan := make(chan error, 1) | ||
go func() { | ||
err := ioCopy(src, dst, enableDiscard) | ||
errChan <- err | ||
}() | ||
go func() { | ||
err := ioCopy(dst, src, enableDiscard) | ||
errChan <- err | ||
}() | ||
<-errChan | ||
dst.Close() | ||
src.Close() | ||
} | ||
|
||
require.NoError(t, failpoint.Disable("github.com/tikv/pd/pkg/utils/etcdutil/autoSyncInterval")) | ||
func ioCopy(dst io.Writer, src io.Reader, enableDiscard *atomic.Bool) (err error) { | ||
buffer := make([]byte, 32*1024) | ||
for { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a dead loop when src.Read(buffer) returns non-zero, EOF then next Read returns 0, EOF There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just direct return |
||
if enableDiscard.Load() { | ||
io.Copy(io.Discard, src) | ||
} | ||
readNum, errRead := src.Read(buffer) | ||
if readNum > 0 { | ||
writeNum, errWrite := dst.Write(buffer[0:readNum]) | ||
lhy1024 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if errWrite != nil { | ||
return errWrite | ||
} | ||
if readNum != writeNum { | ||
return io.ErrShortWrite | ||
} | ||
} | ||
if errRead != nil { | ||
lhy1024 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
err = errRead | ||
break | ||
} | ||
} | ||
return err | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the default value if we don't set them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
zero