2021-08-26 07:59:02 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2023-04-05 13:58:32 +00:00
|
|
|
"context"
|
2022-07-18 13:41:35 +00:00
|
|
|
"sort"
|
2022-08-22 16:36:41 +00:00
|
|
|
"time"
|
2022-07-18 13:41:35 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2021-08-26 07:59:02 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-07-18 13:41:35 +00:00
|
|
|
// Endpoint represents morph endpoint together with its priority.
|
|
|
|
type Endpoint struct {
|
|
|
|
Address string
|
|
|
|
Priority int
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:52:10 +00:00
|
|
|
type endpoints struct {
|
|
|
|
curr int
|
2022-07-18 13:41:35 +00:00
|
|
|
list []Endpoint
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
2021-08-26 07:59:02 +00:00
|
|
|
|
2022-07-18 13:41:35 +00:00
|
|
|
func (e *endpoints) init(ee []Endpoint) {
|
|
|
|
sort.SliceStable(ee, func(i, j int) bool {
|
2022-08-02 11:31:21 +00:00
|
|
|
return ee[i].Priority < ee[j].Priority
|
2022-07-18 13:41:35 +00:00
|
|
|
})
|
|
|
|
|
2022-06-29 13:02:07 +00:00
|
|
|
e.curr = 0
|
|
|
|
e.list = ee
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
// SwitchRPC performs reconnection and returns true if it was successful.
|
|
|
|
func (c *Client) SwitchRPC(ctx context.Context) bool {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.Lock()
|
|
|
|
defer c.switchLock.Unlock()
|
|
|
|
|
|
|
|
c.client.Close()
|
|
|
|
|
2022-07-18 13:41:35 +00:00
|
|
|
// Iterate endpoints in the order of decreasing priority.
|
|
|
|
for c.endpoints.curr = range c.endpoints.list {
|
|
|
|
newEndpoint := c.endpoints.list[c.endpoints.curr].Address
|
2023-04-05 13:58:32 +00:00
|
|
|
cli, act, err := c.newCli(ctx, newEndpoint)
|
2021-02-09 17:52:10 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.logger.Warn(logs.ClientCouldNotEstablishConnectionToTheSwitchedRPCNode,
|
2021-02-09 17:52:10 +00:00
|
|
|
zap.String("endpoint", newEndpoint),
|
|
|
|
zap.Error(err),
|
2021-08-26 07:59:02 +00:00
|
|
|
)
|
|
|
|
|
2021-02-09 17:52:10 +00:00
|
|
|
continue
|
2021-08-31 10:27:12 +00:00
|
|
|
}
|
2021-08-26 07:59:02 +00:00
|
|
|
|
2022-03-31 13:12:42 +00:00
|
|
|
c.cache.invalidate()
|
2021-02-09 17:52:10 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
c.logger.Info(logs.ClientConnectionToTheNewRPCNodeHasBeenEstablished,
|
2022-06-29 13:02:07 +00:00
|
|
|
zap.String("endpoint", newEndpoint))
|
|
|
|
|
2022-08-22 16:36:41 +00:00
|
|
|
c.client = cli
|
2022-11-03 12:44:59 +00:00
|
|
|
c.setActor(act)
|
2022-08-22 16:36:41 +00:00
|
|
|
|
2022-10-12 16:47:33 +00:00
|
|
|
if c.cfg.switchInterval != 0 && !c.switchIsActive.Load() &&
|
2022-08-22 16:36:41 +00:00
|
|
|
c.endpoints.list[c.endpoints.curr].Priority != c.endpoints.list[0].Priority {
|
|
|
|
c.switchIsActive.Store(true)
|
2023-04-05 13:58:32 +00:00
|
|
|
go c.switchToMostPrioritized(ctx)
|
2022-08-22 16:36:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 17:52:10 +00:00
|
|
|
return true
|
|
|
|
}
|
2022-07-18 13:41:35 +00:00
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
c.inactive = true
|
2022-06-09 19:22:02 +00:00
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
if c.cfg.inactiveModeCb != nil {
|
|
|
|
c.cfg.inactiveModeCb()
|
2023-02-22 13:04:58 +00:00
|
|
|
}
|
2023-05-11 14:05:24 +00:00
|
|
|
return false
|
2023-02-22 13:04:58 +00:00
|
|
|
}
|
2021-02-09 17:52:10 +00:00
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
func (c *Client) closeWaiter(ctx context.Context) {
|
2023-10-27 12:02:14 +00:00
|
|
|
c.wg.Add(1)
|
|
|
|
defer c.wg.Done()
|
2023-02-22 13:04:58 +00:00
|
|
|
select {
|
2023-04-05 13:58:32 +00:00
|
|
|
case <-ctx.Done():
|
2023-02-22 13:04:58 +00:00
|
|
|
case <-c.closeChan:
|
|
|
|
}
|
2023-05-11 14:05:24 +00:00
|
|
|
_ = c.UnsubscribeAll()
|
|
|
|
c.close()
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
2021-08-26 07:59:02 +00:00
|
|
|
|
2023-04-05 13:58:32 +00:00
|
|
|
func (c *Client) switchToMostPrioritized(ctx context.Context) {
|
2022-10-12 17:12:35 +00:00
|
|
|
t := time.NewTicker(c.cfg.switchInterval)
|
2022-08-22 16:36:41 +00:00
|
|
|
defer t.Stop()
|
|
|
|
defer c.switchIsActive.Store(false)
|
|
|
|
|
|
|
|
mainLoop:
|
|
|
|
for {
|
|
|
|
select {
|
2023-04-05 13:58:32 +00:00
|
|
|
case <-ctx.Done():
|
2022-08-22 16:36:41 +00:00
|
|
|
return
|
|
|
|
case <-t.C:
|
|
|
|
c.switchLock.RLock()
|
2023-02-22 13:04:58 +00:00
|
|
|
|
2022-08-22 16:36:41 +00:00
|
|
|
endpointsCopy := make([]Endpoint, len(c.endpoints.list))
|
|
|
|
copy(endpointsCopy, c.endpoints.list)
|
|
|
|
currPriority := c.endpoints.list[c.endpoints.curr].Priority
|
|
|
|
highestPriority := c.endpoints.list[0].Priority
|
2023-02-22 13:04:58 +00:00
|
|
|
|
2022-08-22 16:36:41 +00:00
|
|
|
c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if currPriority == highestPriority {
|
|
|
|
// already connected to
|
|
|
|
// the most prioritized
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, e := range endpointsCopy {
|
|
|
|
if currPriority == e.Priority {
|
|
|
|
// a switch will not increase the priority
|
|
|
|
continue mainLoop
|
|
|
|
}
|
|
|
|
|
|
|
|
tryE := e.Address
|
|
|
|
|
2023-04-05 13:58:32 +00:00
|
|
|
cli, act, err := c.newCli(ctx, tryE)
|
2022-08-22 16:36:41 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.logger.Warn(logs.ClientCouldNotCreateClientToTheHigherPriorityNode,
|
2022-08-22 16:36:41 +00:00
|
|
|
zap.String("endpoint", tryE),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
c.switchLock.Lock()
|
2022-08-22 16:36:41 +00:00
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
// higher priority node could have been
|
|
|
|
// connected in the other goroutine
|
|
|
|
if e.Priority >= c.endpoints.list[c.endpoints.curr].Priority {
|
|
|
|
cli.Close()
|
2022-08-22 16:36:41 +00:00
|
|
|
c.switchLock.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
c.client.Close()
|
|
|
|
c.cache.invalidate()
|
|
|
|
c.client = cli
|
|
|
|
c.setActor(act)
|
|
|
|
c.endpoints.curr = i
|
|
|
|
|
|
|
|
c.switchLock.Unlock()
|
|
|
|
|
|
|
|
c.logger.Info(logs.ClientSwitchedToTheHigherPriorityRPC,
|
|
|
|
zap.String("endpoint", tryE))
|
|
|
|
|
|
|
|
return
|
2022-08-22 16:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:03:55 +00:00
|
|
|
// close closes notification channel and wrapped WS client.
|
2021-02-09 17:52:10 +00:00
|
|
|
func (c *Client) close() {
|
2023-05-11 14:05:24 +00:00
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
2021-02-09 17:52:10 +00:00
|
|
|
c.client.Close()
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|