Files
ziti/controller/handler_peer_ctrl/bind.go
T
Paul Lorenz 7f2f619e88 Reconcile the raft advertise address with the controller config on startup. Fixes #3588
- adds a startup reconcile that asks the leader to move this node's stored raft address to its configured advertise address, retrying with jittered backoff until the change is visible locally
- wakes the running reconcile on leader events instead of starting a second one, and jitters the first attempt and each wake so controllers reconciling after the same event spread their requests
- keeps checking a node whose copy of the configuration doesn't list it yet, as after a restart before any of the log reached it, instead of treating it as reconciled
- adds an UpdatePeerAddressRequest peer message carrying the member id, the address the node believes is stored and the new address; the leader accepts it only from the node it names and does not forward it
- adds updateMemberAddressAsLeader, which changes a member's address in place, keeping its suffrage, after refusing non-members, addresses held by another member and stale from addresses
- probes the new address as a sanity check, refusing the change if a different controller answers and making it with a warning if the address can't be reached, since some controllers only dial out
- conditions the update on a configuration index read from the raft log and snapshot stores, since GetConfiguration().Index() is always zero in the pinned raft version
- routes AddPeer requests that find an existing member at a new address to the in-place update instead of remove and re-add, identifying the member through GetPeerInfo rather than the request id
- falls back to the member id when the raft transport dials a stored address with no peer connected at it
- adds a probe hello header so probe connections are closed without registering a peer
- excludes self by server id in GetPeerAddresses
- corrects the inverted voter flag in the peer AddPeer log and removes the unused ctrlAddress field
- tests single-node, follower, non-voter and leader address changes, refused and unverified addresses, requests from the wrong sender, membership changes made while a member is offline or during the probe, and configuration lookup across snapshots and log gaps
- tests the reconcile starting on an empty log and waiting until the configuration lists the node
- waits in the HA test harness for a restarted peer's own configuration to list it before restarting it
- adds a changelog entry describing the address update and the leader version requirement
2026-10-28 20:54:56 -04:00

124 lines
4.3 KiB
Go

/*
Copyright NetFoundry 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
https://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 handler_peer_ctrl
import (
"sync/atomic"
"time"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/channel/v5"
"github.com/openziti/channel/v5/latency"
"github.com/openziti/foundation/v2/concurrenz"
"github.com/openziti/metrics"
"github.com/openziti/ziti/v2/controller/network"
"github.com/openziti/ziti/v2/controller/raft"
"github.com/sirupsen/logrus"
)
func NewBindHandler(n *network.Network, raftCtrl *raft.Controller, heartbeatOptions *channel.HeartbeatOptions) channel.BindHandler {
bindHandler := func(binding channel.Binding) error {
channel.AddReceiveHandlers(binding, newCommandHandler(raftCtrl))
channel.AddReceiveHandlers(binding, newAddPeerHandler(raftCtrl))
channel.AddReceiveHandlers(binding, newRemovePeerHandler(raftCtrl))
channel.AddReceiveHandlers(binding, newUpdatePeerAddressHandler(raftCtrl))
channel.AddReceiveHandlers(binding, newTransferLeadershipHandler(raftCtrl))
channel.AddReceiveHandlers(binding, newInspectHandler(n))
roundTripHistogram := n.GetMetricsRegistry().Histogram("peer.latency:" + binding.GetChannel().Id())
queueTimeHistogram := n.GetMetricsRegistry().Histogram("peer.queue_time:" + binding.GetChannel().Id())
binding.AddCloseHandler(channel.CloseHandlerF(func(ch channel.Channel) {
roundTripHistogram.Dispose()
queueTimeHistogram.Dispose()
}))
cb := &heartbeatCallback{
latencyMetric: roundTripHistogram,
queueTimeMetric: queueTimeHistogram,
ch: binding.GetChannel(),
latencySemaphore: concurrenz.NewSemaphore(2),
closeUnresponsiveTimeout: heartbeatOptions.CloseUnresponsiveTimeout,
}
cb.lastResponse.Store(time.Now().Add(heartbeatOptions.CloseUnresponsiveTimeout * 2).UnixMilli()) // wait at least 2x timeout before closing
channel.ConfigureHeartbeat(binding, heartbeatOptions.SendInterval, heartbeatOptions.CheckInterval, cb)
return nil
}
return channel.BindHandlerF(bindHandler)
}
type heartbeatCallback struct {
latencyMetric metrics.Histogram
queueTimeMetric metrics.Histogram
lastResponse atomic.Int64
ch channel.Channel
latencySemaphore concurrenz.Semaphore
closeUnresponsiveTimeout time.Duration
}
func (self *heartbeatCallback) HeartbeatTx(int64) {}
func (self *heartbeatCallback) HeartbeatRx(int64) {}
func (self *heartbeatCallback) HeartbeatRespTx(int64) {}
func (self *heartbeatCallback) HeartbeatRespRx(ts int64) {
now := time.Now()
self.lastResponse.Store(now.UnixMilli())
self.latencyMetric.Update(now.UnixNano() - ts)
}
func (self *heartbeatCallback) timeSinceLastResponse(nowUnixMillis int64) time.Duration {
return time.Duration(nowUnixMillis-self.lastResponse.Load()) * time.Millisecond
}
func (self *heartbeatCallback) CheckHeartBeat() {
now := time.Now().UnixMilli()
if self.timeSinceLastResponse(now) > self.closeUnresponsiveTimeout {
log := self.logger()
log.Error("heartbeat not received in time, closing link")
if err := self.ch.Close(); err != nil {
log.WithError(err).Error("error while closing link")
}
}
go self.checkQueueTime()
}
func (self *heartbeatCallback) checkQueueTime() {
if !self.latencySemaphore.TryAcquire() {
self.logger().Warn("unable to check queue time, too many check already running")
return
}
defer self.latencySemaphore.Release()
sendTracker := &latency.SendTimeTracker{
Handler: func(latencyType latency.Type, latency time.Duration) {
self.queueTimeMetric.Update(latency.Nanoseconds())
},
StartTime: time.Now(),
}
if err := self.ch.Send(sendTracker); err != nil && !self.ch.IsClosed() {
self.logger().WithError(err).Error("unable to send queue time tracer")
}
}
func (self *heartbeatCallback) logger() *logrus.Entry {
return pfxlog.Logger().WithField("channelType", "peer").WithField("channelId", self.ch.Id())
}