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"
|
2023-02-22 13:04:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
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-04-05 13:58:32 +00:00
|
|
|
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))
|
|
|
|
|
2023-04-05 13:58:32 +00:00
|
|
|
subs, ok := c.restoreSubscriptions(ctx, cli, newEndpoint, false)
|
2023-02-22 13:04:58 +00:00
|
|
|
if !ok {
|
2022-06-29 13:02:07 +00:00
|
|
|
// new WS client does not allow
|
|
|
|
// restoring subscription, client
|
|
|
|
// could not work correctly =>
|
|
|
|
// closing connection to RPC node
|
|
|
|
// to switch to another one
|
|
|
|
cli.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-08-22 16:36:41 +00:00
|
|
|
c.client = cli
|
2022-11-03 12:44:59 +00:00
|
|
|
c.setActor(act)
|
2023-02-22 13:04:58 +00:00
|
|
|
c.subsInfo = subs
|
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
|
|
|
|
|
|
|
return false
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 13:58:32 +00:00
|
|
|
func (c *Client) notificationLoop(ctx context.Context) {
|
2023-02-22 13:04:58 +00:00
|
|
|
var e any
|
|
|
|
var ok bool
|
|
|
|
|
2021-02-09 17:52:10 +00:00
|
|
|
for {
|
2022-08-22 16:36:41 +00:00
|
|
|
c.switchLock.RLock()
|
2023-02-22 13:04:58 +00:00
|
|
|
bChan := c.blockRcv
|
|
|
|
nChan := c.notificationRcv
|
|
|
|
nrChan := c.notaryReqRcv
|
2022-08-22 16:36:41 +00:00
|
|
|
c.switchLock.RUnlock()
|
|
|
|
|
2021-02-09 17:52:10 +00:00
|
|
|
select {
|
2023-04-05 13:58:32 +00:00
|
|
|
case <-ctx.Done():
|
2021-02-09 17:52:10 +00:00
|
|
|
_ = c.UnsubscribeAll()
|
|
|
|
c.close()
|
|
|
|
|
|
|
|
return
|
|
|
|
case <-c.closeChan:
|
|
|
|
_ = c.UnsubscribeAll()
|
|
|
|
c.close()
|
|
|
|
|
|
|
|
return
|
2023-02-22 13:04:58 +00:00
|
|
|
case e, ok = <-bChan:
|
|
|
|
case e, ok = <-nChan:
|
|
|
|
case e, ok = <-nrChan:
|
|
|
|
}
|
2022-06-09 19:22:02 +00:00
|
|
|
|
2023-02-22 13:04:58 +00:00
|
|
|
if ok {
|
2023-04-05 13:58:32 +00:00
|
|
|
c.routeEvent(ctx, e)
|
2023-02-22 13:04:58 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-02-09 17:52:10 +00:00
|
|
|
|
2023-04-05 13:58:32 +00:00
|
|
|
if !c.reconnect(ctx) {
|
2023-02-22 13:04:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 17:52:10 +00:00
|
|
|
|
2023-04-05 13:58:32 +00:00
|
|
|
func (c *Client) routeEvent(ctx context.Context, e any) {
|
2023-02-22 13:04:58 +00:00
|
|
|
typedNotification := rpcclient.Notification{Value: e}
|
2021-02-09 17:52:10 +00:00
|
|
|
|
2023-02-22 13:04:58 +00:00
|
|
|
switch e.(type) {
|
|
|
|
case *block.Block:
|
|
|
|
typedNotification.Type = neorpc.BlockEventID
|
|
|
|
case *state.ContainedNotificationEvent:
|
|
|
|
typedNotification.Type = neorpc.NotificationEventID
|
|
|
|
case *result.NotaryRequestEvent:
|
|
|
|
typedNotification.Type = neorpc.NotaryRequestEventID
|
|
|
|
}
|
2021-02-09 17:52:10 +00:00
|
|
|
|
2023-02-22 13:04:58 +00:00
|
|
|
select {
|
|
|
|
case c.notifications <- typedNotification:
|
2023-04-05 13:58:32 +00:00
|
|
|
case <-ctx.Done():
|
2023-02-22 13:04:58 +00:00
|
|
|
_ = c.UnsubscribeAll()
|
|
|
|
c.close()
|
|
|
|
case <-c.closeChan:
|
|
|
|
_ = c.UnsubscribeAll()
|
|
|
|
c.close()
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 17:52:10 +00:00
|
|
|
|
2023-04-05 13:58:32 +00:00
|
|
|
func (c *Client) reconnect(ctx context.Context) bool {
|
2023-02-22 13:04:58 +00:00
|
|
|
if closeErr := c.client.GetError(); closeErr != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.logger.Warn(logs.ClientSwitchingToTheNextRPCNode,
|
2023-02-22 13:04:58 +00:00
|
|
|
zap.String("reason", closeErr.Error()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// neo-go client was closed by calling `Close`
|
|
|
|
// method, that happens only when a client has
|
|
|
|
// switched to the more prioritized RPC
|
|
|
|
return true
|
|
|
|
}
|
2023-01-27 15:12:03 +00:00
|
|
|
|
2023-04-05 13:58:32 +00:00
|
|
|
if !c.switchRPC(ctx) {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.logger.Error(logs.ClientCouldNotEstablishConnectionToAnyRPCNode)
|
2023-01-27 15:12:03 +00:00
|
|
|
|
2023-02-22 13:04:58 +00:00
|
|
|
// could not connect to all endpoints =>
|
|
|
|
// switch client to inactive mode
|
|
|
|
c.inactiveMode()
|
|
|
|
|
|
|
|
return false
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|
2023-02-22 13:04:58 +00:00
|
|
|
|
|
|
|
// TODO(@carpawell): call here some callback retrieved in constructor
|
|
|
|
// of the client to allow checking chain state since during switch
|
|
|
|
// process some notification could be lost
|
|
|
|
|
|
|
|
return true
|
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-04-05 13:58:32 +00:00
|
|
|
if subs, ok := c.restoreSubscriptions(ctx, cli, tryE, true); ok {
|
2022-08-22 16:36:41 +00:00
|
|
|
c.switchLock.Lock()
|
|
|
|
|
|
|
|
// higher priority node could have been
|
|
|
|
// connected in the other goroutine
|
|
|
|
if e.Priority >= c.endpoints.list[c.endpoints.curr].Priority {
|
|
|
|
cli.Close()
|
|
|
|
c.switchLock.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.client.Close()
|
|
|
|
c.cache.invalidate()
|
|
|
|
c.client = cli
|
2022-11-03 12:44:59 +00:00
|
|
|
c.setActor(act)
|
2023-02-22 13:04:58 +00:00
|
|
|
c.subsInfo = subs
|
2022-08-22 16:36:41 +00:00
|
|
|
c.endpoints.curr = i
|
|
|
|
|
|
|
|
c.switchLock.Unlock()
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
c.logger.Info(logs.ClientSwitchedToTheHigherPriorityRPC,
|
2022-08-22 16:36:41 +00:00
|
|
|
zap.String("endpoint", tryE))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
c.logger.Warn(logs.ClientCouldNotRestoreSideChainSubscriptionsUsingNode,
|
2022-08-22 16:36:41 +00:00
|
|
|
zap.String("endpoint", tryE),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
close(c.notifications)
|
|
|
|
c.client.Close()
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|