Skip to content

Commit

Permalink
morph: support reloading morph endpoints with SIGHUP
Browse files Browse the repository at this point in the history
Add a new function `Client.Reload` that passes the `WithEndpoints` option and
updates the `Client` endpoints.
Closes the current connection and attempts to
reconnect if there is no endpoint in the config to which the client is
connected. Node service can be interrupted in this case.
Add docs.

Closes #1871.

Signed-off-by: Andrey Butusov <[email protected]>
  • Loading branch information
End-rey committed Nov 7, 2024
1 parent 3b0bbc4 commit 51dacc9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ attribute, which is used for container domain name in NNS contracts (#2954)
- `--disable-auto-gen-tag` flag for gendoc command (#2983)
- Docs files for cli commands to the `docs/cli-commands` folder (#2983)
- `logger.encoding` config option (#2999)
- Reloading morph endpoints with SIGHUP (#2998)

### Fixed
- Do not search for tombstones when handling their expiration, use local indexes instead (#2929)
Expand Down
4 changes: 4 additions & 0 deletions cmd/neofs-node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,10 @@ func (c *cfg) configWatcher(ctx context.Context) {
continue
}

// Morph

c.cli.Reload(client.WithEndpoints(c.morph.endpoints))

Check warning on line 878 in cmd/neofs-node/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/config.go#L877-L878

Added lines #L877 - L878 were not covered by tests
c.log.Info("configuration has been reloaded successfully")
case <-ctx.Done():
return
Expand Down
6 changes: 6 additions & 0 deletions docs/sighup.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ comparing paths from `shard.blobstor` section. After this we have 3 sets:
| Changed section | Actions |
|-----------------|----------------------------------------------------------------------------------------------------------------------|
| `path` | If `path` is different, metabase is closed and opened with a new path. All other configuration will also be updated. |

### Morph

| Changed section | Actions |
|-----------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `endpoints` | Updates N3 endpoints.<br/>If new `endpoints` do not contain the endpoint client is connected to, it will reconnect to another endpoint from the new list. Node service can be interrupted in this case. |
4 changes: 3 additions & 1 deletion pkg/morph/client/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ type cfg struct {
autoSidechainScope bool
signer *transaction.Signer

endpoints []string
endpointsLock *sync.RWMutex
endpoints []string

singleCli *rpcclient.WSClient // neo-go client for single client mode

Expand All @@ -63,6 +64,7 @@ func defaultConfig() *cfg {
signer: &transaction.Signer{
Scopes: transaction.CalledByEntry,
},
endpointsLock: &sync.RWMutex{},

Check warning on line 67 in pkg/morph/client/constructor.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/client/constructor.go#L67

Added line #L67 was not covered by tests
reconnectionDelay: 5 * time.Second,
reconnectionRetries: 5,
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/morph/client/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func (c *Client) switchRPC() *connection {
}

func (c *Client) connEndpoints() *connection {
c.cfg.endpointsLock.RLock()
defer c.cfg.endpointsLock.RUnlock()

Check warning on line 38 in pkg/morph/client/multi.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/client/multi.go#L36-L38

Added lines #L36 - L38 were not covered by tests
// Iterate endpoints.
for _, e := range c.cfg.endpoints {

Check warning on line 40 in pkg/morph/client/multi.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/client/multi.go#L40

Added line #L40 was not covered by tests
conn, err := c.newConnection(e)
Expand Down
29 changes: 29 additions & 0 deletions pkg/morph/client/reload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package client

import "slices"

// Reload allows runtime reconfiguration for WithEndpoints parameter.
func (c *Client) Reload(opts ...Option) {
cfg := new(cfg)
for _, o := range opts {
o(cfg)
}

Check warning on line 10 in pkg/morph/client/reload.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/client/reload.go#L6-L10

Added lines #L6 - L10 were not covered by tests

c.cfg.endpointsLock.Lock()

c.cfg.endpoints = cfg.endpoints

c.cfg.endpointsLock.Unlock()

conn := c.conn.Load()
if conn == nil {
return
}

Check warning on line 21 in pkg/morph/client/reload.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/client/reload.go#L12-L21

Added lines #L12 - L21 were not covered by tests

// Close current connection and attempt to reconnect, if there is no endpoint
// in the config to which the client is connected.
// Node service can be interrupted in this case.
if slices.Contains(cfg.endpoints, conn.client.Endpoint()) {
conn.client.Close()
}

Check warning on line 28 in pkg/morph/client/reload.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/client/reload.go#L26-L28

Added lines #L26 - L28 were not covered by tests
}

0 comments on commit 51dacc9

Please sign in to comment.