-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
ttl: notify tidb nodes through etcd notification #40705
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2023 PingCAP, Inc. | ||
// | ||
// 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 client | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pingcap/tidb/ddl/util" | ||
clientv3 "go.etcd.io/etcd/client/v3" | ||
) | ||
|
||
const ttlNotificationPrefix string = "/tidb/ttl/notification/" | ||
|
||
// NotificationClient is a client to notify other TTL workers | ||
type NotificationClient interface { | ||
// Notify sends a notification | ||
Notify(ctx context.Context, typ string, data string) error | ||
// WatchNotification opens a channel, in which we could receive all notifications | ||
WatchNotification(ctx context.Context, typ string) clientv3.WatchChan | ||
} | ||
|
||
// NewNotificationClient creates a notification client with etcd | ||
func NewNotificationClient(etcdCli *clientv3.Client) NotificationClient { | ||
return &etcdClient{ | ||
etcdCli: etcdCli, | ||
} | ||
} | ||
|
||
// Notify stores the corresponding K-V in the etcd | ||
func (c *etcdClient) Notify(ctx context.Context, typ string, data string) error { | ||
return util.PutKVToEtcd(ctx, c.etcdCli, 1, ttlNotificationPrefix+typ, data) | ||
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. It is strange that a common function is placed in the 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. I think it's because this function is only used by ddl (to notify after inserting ddl jobs). We could consider to extract this function to a more proper place (especially after implementing notification for the unified job/worker framework 🤔 ). 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. I couldn’t agree more. |
||
} | ||
|
||
// WatchNotification returns a go channel to get notification | ||
func (c *etcdClient) WatchNotification(ctx context.Context, typ string) clientv3.WatchChan { | ||
return c.etcdCli.Watch(ctx, ttlNotificationPrefix+typ) | ||
} | ||
|
||
// NewMockNotificationClient creates a mock notification client | ||
func NewMockNotificationClient() NotificationClient { | ||
return &mockClient{ | ||
store: make(map[string]interface{}), | ||
commandWatchers: make([]chan *CmdRequest, 0, 1), | ||
notificationWatchers: make(map[string][]chan clientv3.WatchResponse), | ||
} | ||
} | ||
|
||
// Notify implements the NotificationClient | ||
func (c *mockClient) Notify(_ context.Context, typ string, data string) error { | ||
c.Lock() | ||
defer c.Unlock() | ||
|
||
for _, ch := range c.notificationWatchers[typ] { | ||
ch <- clientv3.WatchResponse{} | ||
} | ||
return nil | ||
} | ||
|
||
// WatchNotification implements the NotificationClient | ||
func (c *mockClient) WatchNotification(_ context.Context, typ string) clientv3.WatchChan { | ||
c.Lock() | ||
defer c.Unlock() | ||
|
||
ch := make(chan clientv3.WatchResponse, 1) | ||
c.notificationWatchers[typ] = append(c.notificationWatchers[typ], ch) | ||
return ch | ||
} |
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.
Is it better to merge interface
NotificationClient
andCommandClient
?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.
I don't think it's good to merge these two interfaces. They are actually doing different things, and the requirement of
CommandClient
is much higher than theNotificationClient
(if the command somehow disappeared, the sending one will be blocked).Also we just want to use the
CommandClient
in the test, but theNotificationClient
is used in production codes (as we don't expect it to be 100% transfered, and the message is not important).