-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ttl: notify tidb nodes through etcd notification (#40705)
close #40365
- Loading branch information
Showing
7 changed files
with
183 additions
and
19 deletions.
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
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 |
---|---|---|
@@ -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) | ||
} | ||
|
||
// 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 | ||
} |
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
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