mirror of
https://github.com/openziti/ziti.git
synced 2026-10-26 05:30:03 +00:00
af20635fd9
- applies controller lists, leader announcements and new controller addresses on one goroutine, in the order they arrive, so checking an update's index, applying it and recording it is one step - replaces the router's controller endpoints only from a leader's list at a newer index; a leader's list at the recorded index, or a follower's list at or above it, only adds endpoints - applies leader announcements on the same goroutine, against their own recorded index - keeps receive goroutines from waiting on an update being applied, and bounds the waiting updates by dropping follower lists and keeping each controller's newest leader list, announcement and address - ignores an empty new controller address - marks a controller's on-connect list as the leader's when it leads, so a connecting router still drops controllers the cluster removed - builds the handler once per router rather than in package-level singletons - adds NetworkControllers.GetEndpoints - tests list handling, ordering against index resets and announcements, and the bound on waiting updates
96 lines
2.7 KiB
Go
96 lines
2.7 KiB
Go
package controller
|
|
|
|
import (
|
|
"github.com/michaelquigley/pfxlog"
|
|
"github.com/openziti/channel/v4"
|
|
"github.com/openziti/channel/v4/protobufs"
|
|
"github.com/openziti/ziti/common/pb/ctrl_pb"
|
|
config2 "github.com/openziti/ziti/controller/config"
|
|
"github.com/openziti/ziti/controller/model"
|
|
"github.com/openziti/ziti/controller/raft"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// OnConnectSettingsHandler sends a ctrl_pb.ContentType_SettingsType message when routers connect if necessary
|
|
// Settings are a map of int32 -> []byte data. The type should be used to determine how the setting's []byte
|
|
// array is consumed.
|
|
type OnConnectSettingsHandler struct {
|
|
config *config2.Config
|
|
settings map[int32][]byte
|
|
}
|
|
|
|
func (o *OnConnectSettingsHandler) RouterDisconnected(r *model.Router) {
|
|
//do nothing, satisfy interface
|
|
}
|
|
|
|
func (o OnConnectSettingsHandler) RouterConnected(r *model.Router) {
|
|
if len(o.settings) > 0 {
|
|
settingsMsg := &ctrl_pb.Settings{
|
|
Data: map[int32][]byte{},
|
|
}
|
|
|
|
for k, v := range o.settings {
|
|
settingsMsg.Data[k] = v
|
|
}
|
|
|
|
if body, err := proto.Marshal(settingsMsg); err == nil {
|
|
msg := channel.NewMessage(int32(ctrl_pb.ContentType_SettingsType), body)
|
|
if err := r.Control.Send(msg); err == nil {
|
|
pfxlog.Logger().WithError(err).WithFields(map[string]interface{}{
|
|
"routerId": r.Id,
|
|
"channel": r.Control.LogicalName(),
|
|
}).Error("error sending settings on router connect")
|
|
}
|
|
}
|
|
|
|
} else {
|
|
pfxlog.Logger().WithFields(map[string]interface{}{
|
|
"routerId": r.Id,
|
|
"channel": r.Control.LogicalName(),
|
|
}).Info("no on connect settings to send")
|
|
}
|
|
}
|
|
|
|
type OnConnectCtrlAddressesUpdateHandler struct {
|
|
ctrlAddress string
|
|
raft *raft.Controller
|
|
}
|
|
|
|
func NewOnConnectCtrlAddressesUpdateHandler(ctrlAddress string, raft *raft.Controller) *OnConnectCtrlAddressesUpdateHandler {
|
|
return &OnConnectCtrlAddressesUpdateHandler{
|
|
ctrlAddress: ctrlAddress,
|
|
raft: raft,
|
|
}
|
|
}
|
|
|
|
func (o *OnConnectCtrlAddressesUpdateHandler) RouterDisconnected(r *model.Router) {
|
|
//do nothing, satisfy interface
|
|
}
|
|
|
|
func (o OnConnectCtrlAddressesUpdateHandler) RouterConnected(r *model.Router) {
|
|
index, data := o.raft.CtrlAddresses()
|
|
log := pfxlog.Logger().WithFields(map[string]interface{}{
|
|
"routerId": r.Id,
|
|
"channel": r.Control.LogicalName(),
|
|
"addresses": data,
|
|
"index": index,
|
|
})
|
|
|
|
if len(data) == 0 {
|
|
log.Error("no addresses to send")
|
|
return
|
|
}
|
|
|
|
log.Info("router connected, syncing ctrl addresses")
|
|
|
|
updMsg := &ctrl_pb.UpdateCtrlAddresses{
|
|
IsLeader: o.raft.IsLeader(),
|
|
Addresses: data,
|
|
Index: index,
|
|
}
|
|
|
|
if err := protobufs.MarshalTyped(updMsg).Send(r.Control); err != nil {
|
|
log.WithError(err).Error("error sending UpdateCtrlAddresses on router connect")
|
|
}
|
|
}
|