-
Notifications
You must be signed in to change notification settings - Fork 721
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
pkg: support service discovery #5911
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2023 TiKV Project 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 discovery | ||
|
||
import ( | ||
"github.com/tikv/pd/pkg/utils/etcdutil" | ||
"go.etcd.io/etcd/clientv3" | ||
) | ||
|
||
// Discover is used to get all the service instances of the specified service name. | ||
func Discover(cli *clientv3.Client, serviceName string) ([]string, error) { | ||
key := discoveryPath(serviceName) + "/" | ||
endKey := clientv3.GetPrefixRangeEnd(key) + "/" | ||
|
||
withRange := clientv3.WithRange(endKey) | ||
resp, err := etcdutil.EtcdKVGet(cli, key, withRange) | ||
if err != nil { | ||
return nil, err | ||
} | ||
values := make([]string, 0, len(resp.Kvs)) | ||
for _, item := range resp.Kvs { | ||
values = append(values, string(item.Value)) | ||
} | ||
return values, nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2023 TiKV Project 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 discovery | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/tikv/pd/pkg/utils/etcdutil" | ||
"go.etcd.io/etcd/clientv3" | ||
"go.etcd.io/etcd/embed" | ||
) | ||
|
||
func TestDiscover(t *testing.T) { | ||
re := require.New(t) | ||
cfg := etcdutil.NewTestSingleConfig(t) | ||
etcd, err := embed.StartEtcd(cfg) | ||
defer func() { | ||
etcd.Close() | ||
}() | ||
re.NoError(err) | ||
|
||
ep := cfg.LCUrls[0].String() | ||
client, err := clientv3.New(clientv3.Config{ | ||
Endpoints: []string{ep}, | ||
}) | ||
re.NoError(err) | ||
|
||
<-etcd.Server.ReadyNotify() | ||
sr1 := NewServiceRegister(context.Background(), client, "test_service", "127.0.0.1:1", "127.0.0.1:1", 1) | ||
err = sr1.Register() | ||
re.NoError(err) | ||
sr2 := NewServiceRegister(context.Background(), client, "test_service", "127.0.0.1:2", "127.0.0.1:2", 1) | ||
err = sr2.Register() | ||
re.NoError(err) | ||
|
||
endpoints, err := Discover(client, "test_service") | ||
re.NoError(err) | ||
re.Len(endpoints, 2) | ||
re.Equal("127.0.0.1:1", endpoints[0]) | ||
re.Equal("127.0.0.1:2", endpoints[1]) | ||
|
||
sr1.cancel() | ||
sr2.cancel() | ||
time.Sleep(3 * time.Second) | ||
endpoints, err = Discover(client, "test_service") | ||
re.NoError(err) | ||
re.Empty(endpoints) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2023 TiKV Project 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 discovery | ||
|
||
import "path" | ||
|
||
const ( | ||
registryPrefix = "/pd/microservice" | ||
registryKey = "registry" | ||
) | ||
|
||
func registryPath(serviceName, serviceAddr string) string { | ||
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. do we need tenant now? 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. service instance (node) registry path doesn't contain tenant/keyspace. The tenant/keyspace group primary discovery path does. |
||
return path.Join(registryPrefix, serviceName, registryKey, serviceAddr) | ||
} | ||
|
||
func discoveryPath(serviceName string) string { | ||
return path.Join(registryPrefix, serviceName, registryKey) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2023 TiKV Project 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 discovery | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/pingcap/log" | ||
"go.etcd.io/etcd/clientv3" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// ServiceRegister is used to register the service to etcd. | ||
type ServiceRegister struct { | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
cli *clientv3.Client | ||
key string | ||
value string | ||
ttl int64 | ||
} | ||
|
||
// NewServiceRegister creates a new ServiceRegister. | ||
func NewServiceRegister(ctx context.Context, cli *clientv3.Client, serviceName, serviceAddr, serializedValue string, ttl int64) *ServiceRegister { | ||
cctx, cancel := context.WithCancel(ctx) | ||
serviceKey := registryPath(serviceName, serviceAddr) | ||
return &ServiceRegister{ | ||
ctx: cctx, | ||
cancel: cancel, | ||
cli: cli, | ||
key: serviceKey, | ||
value: serializedValue, | ||
ttl: ttl, | ||
} | ||
} | ||
|
||
// Register registers the service to etcd. | ||
func (sr *ServiceRegister) Register() error { | ||
resp, err := sr.cli.Grant(sr.ctx, sr.ttl) | ||
if err != nil { | ||
sr.cancel() | ||
return fmt.Errorf("grant lease failed: %v", err) | ||
} | ||
|
||
if _, err := sr.cli.Put(sr.ctx, sr.key, sr.value, clientv3.WithLease(resp.ID)); err != nil { | ||
sr.cancel() | ||
return fmt.Errorf("put the key %s failed: %v", sr.key, err) | ||
} | ||
|
||
kresp, err := sr.cli.KeepAlive(sr.ctx, resp.ID) | ||
if err != nil { | ||
sr.cancel() | ||
return fmt.Errorf("keepalive failed: %v", err) | ||
} | ||
go func() { | ||
for { | ||
select { | ||
case <-sr.ctx.Done(): | ||
log.Info("exit register process", zap.String("key", sr.key)) | ||
return | ||
case _, ok := <-kresp: | ||
if !ok { | ||
log.Error("keep alive failed", zap.String("key", sr.key)) | ||
// retry | ||
t := time.NewTicker(time.Duration(sr.ttl) * time.Second / 2) | ||
for { | ||
<-t.C | ||
resp, err := sr.cli.Grant(sr.ctx, sr.ttl) | ||
if err != nil { | ||
log.Error("grant lease failed", zap.String("key", sr.key), zap.Error(err)) | ||
continue | ||
} | ||
|
||
if _, err := sr.cli.Put(sr.ctx, sr.key, sr.value, clientv3.WithLease(resp.ID)); err != nil { | ||
log.Error("put the key failed", zap.String("key", sr.key), zap.Error(err)) | ||
continue | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
// Deregister deregisters the service from etcd. | ||
func (sr *ServiceRegister) Deregister() error { | ||
sr.cancel() | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
_, err := sr.cli.Delete(ctx, sr.key) | ||
return err | ||
} |
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2023 TiKV Project 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 discovery | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/tikv/pd/pkg/utils/etcdutil" | ||
"go.etcd.io/etcd/clientv3" | ||
"go.etcd.io/etcd/embed" | ||
) | ||
|
||
func TestRegister(t *testing.T) { | ||
re := require.New(t) | ||
cfg := etcdutil.NewTestSingleConfig(t) | ||
etcd, err := embed.StartEtcd(cfg) | ||
defer func() { | ||
etcd.Close() | ||
}() | ||
re.NoError(err) | ||
|
||
ep := cfg.LCUrls[0].String() | ||
client, err := clientv3.New(clientv3.Config{ | ||
Endpoints: []string{ep}, | ||
}) | ||
re.NoError(err) | ||
|
||
<-etcd.Server.ReadyNotify() | ||
sr := NewServiceRegister(context.Background(), client, "test_service", "127.0.0.1:1", "127.0.0.1:1", 10) | ||
err = sr.Register() | ||
re.NoError(err) | ||
resp, err := client.Get(context.Background(), sr.key) | ||
re.NoError(err) | ||
re.Equal("127.0.0.1:1", string(resp.Kvs[0].Value)) | ||
|
||
err = sr.Deregister() | ||
re.NoError(err) | ||
resp, err = client.Get(context.Background(), sr.key) | ||
re.NoError(err) | ||
re.Empty(resp.Kvs) | ||
|
||
sr = NewServiceRegister(context.Background(), client, "test_service", "127.0.0.1:2", "127.0.0.1:2", 1) | ||
err = sr.Register() | ||
re.NoError(err) | ||
sr.cancel() | ||
// ensure that the lease is expired | ||
time.Sleep(3 * time.Second) | ||
resp, err = client.Get(context.Background(), sr.key) | ||
re.NoError(err) | ||
re.Empty(resp.Kvs) | ||
} |
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
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.
The design doc contains server register, service instance discovery and service discovery. This function is the second one. For tenant tso, in the third part "service discover", we need to get the server endpoint for a given key space group. Could you list which functionalities are provided by this PR? And we can implement the remaining functionalities in the next PRs.