configureZoneAwareRouting configures zone-aware routing for memberlist.
(mlCfg *memberlist.Config)
| 563 | |
| 564 | // configureZoneAwareRouting configures zone-aware routing for memberlist. |
| 565 | func (m *KV) configureZoneAwareRouting(mlCfg *memberlist.Config) error { |
| 566 | // Parse the role from the config string. |
| 567 | var role NodeRole |
| 568 | switch m.cfg.ZoneAwareRouting.Role { |
| 569 | case NodeRoleMember.String(): |
| 570 | role = NodeRoleMember |
| 571 | case NodeRoleBridge.String(): |
| 572 | role = NodeRoleBridge |
| 573 | default: |
| 574 | return fmt.Errorf("invalid zone-aware routing role: %s (valid values: %s, %s)", m.cfg.ZoneAwareRouting.Role, NodeRoleMember.String(), NodeRoleBridge.String()) |
| 575 | } |
| 576 | |
| 577 | // Encode the local node metadata. |
| 578 | localMeta, err := EncodeNodeMetadata(role, m.cfg.ZoneAwareRouting.Zone) |
| 579 | if err != nil { |
| 580 | return fmt.Errorf("failed to encode node metadata: %w", err) |
| 581 | } |
| 582 | |
| 583 | // Store the encoded metadata so NodeMeta() can return it. |
| 584 | m.nodeMeta = localMeta |
| 585 | |
| 586 | // Set up the node selection delegate. |
| 587 | mlCfg.NodeSelection = newZoneAwareNodeSelectionDelegate(role, m.cfg.ZoneAwareRouting.Zone, m.logger, m.registerer) |
| 588 | |
| 589 | // The bridge always prefer another bridge as first node. If the bridge only push/pull to 1 node per interval, then |
| 590 | // it will only communicate to bridges, potentially leading to network partitioning if the gossiping is not |
| 591 | // working to propagate changes. To reduce the likelihood of network partitioning when gossiping is not |
| 592 | // working and periodic push/pull is enabled, we configure the bridge to push/pull to 2 nodes per interval |
| 593 | // (the first node is a bridge, and the second node is selected randomly). |
| 594 | if role == NodeRoleBridge { |
| 595 | mlCfg.PushPullNodes = 2 |
| 596 | } else { |
| 597 | mlCfg.PushPullNodes = 1 |
| 598 | } |
| 599 | |
| 600 | level.Info(m.logger).Log( |
| 601 | "msg", "zone-aware routing enabled", |
| 602 | "zone", m.cfg.ZoneAwareRouting.Zone, |
| 603 | "role", role.String(), |
| 604 | ) |
| 605 | |
| 606 | return nil |
| 607 | } |
| 608 | |
| 609 | func (m *KV) starting(ctx context.Context) error { |
| 610 | mlCfg, err := m.buildMemberlistConfig() |
no test coverage detected