2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
package peerstream
|
2022-05-19 19:21:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/consul/lib/retry"
|
2023-02-17 21:14:46 +00:00
|
|
|
"github.com/hashicorp/consul/proto/private/pbservice"
|
2022-05-19 19:21:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// This file contains direct state store functions that need additional
|
|
|
|
// management to have them emit events. Ideally these would go through
|
|
|
|
// streaming machinery instead to be cheaper.
|
|
|
|
|
|
|
|
func (m *subscriptionManager) notifyExportedServicesForPeerID(ctx context.Context, state *subscriptionState, peerID string) {
|
2022-07-15 20:03:40 +00:00
|
|
|
// Wait until this is subscribed-to.
|
|
|
|
select {
|
|
|
|
case <-m.serviceSubReady:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
// syncSubscriptionsAndBlock ensures that the subscriptions to the subscription backend
|
|
|
|
// match the list of services exported to the peer.
|
2022-07-08 17:01:13 +00:00
|
|
|
m.syncViaBlockingQuery(ctx, "exported-services", func(ctx context.Context, store StateStore, ws memdb.WatchSet) (interface{}, error) {
|
2022-05-19 19:21:44 +00:00
|
|
|
// Get exported services for peer id
|
2022-06-27 19:37:18 +00:00
|
|
|
_, list, err := store.ExportedServicesForPeer(ws, peerID, m.config.Datacenter)
|
2022-05-19 19:21:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to watch exported services for peer %q: %w", peerID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}, subExportedServiceList, state.updateCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add a new streaming subscription type to list-by-kind-and-partition since we're getting evictions
|
|
|
|
func (m *subscriptionManager) notifyMeshGatewaysForPartition(ctx context.Context, state *subscriptionState, partition string) {
|
2022-07-15 20:03:40 +00:00
|
|
|
// Wait until this is subscribed-to.
|
|
|
|
select {
|
|
|
|
case <-m.serviceSubReady:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
m.syncViaBlockingQuery(ctx, "mesh-gateways", func(ctx context.Context, store StateStore, ws memdb.WatchSet) (interface{}, error) {
|
2022-05-19 19:21:44 +00:00
|
|
|
// Fetch our current list of all mesh gateways.
|
|
|
|
entMeta := structs.DefaultEnterpriseMetaInPartition(partition)
|
|
|
|
idx, nodes, err := store.ServiceDump(ws, structs.ServiceKindMeshGateway, true, entMeta, structs.DefaultPeerKeyword)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to watch mesh gateways services for partition %q: %w", partition, err)
|
|
|
|
}
|
|
|
|
if idx == 0 {
|
|
|
|
idx = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert back to a protobuf flavor
|
|
|
|
result := &pbservice.IndexedCheckServiceNodes{
|
|
|
|
Index: idx,
|
|
|
|
Nodes: make([]*pbservice.CheckServiceNode, len(nodes)),
|
|
|
|
}
|
|
|
|
for i, csn := range nodes {
|
|
|
|
result.Nodes[i] = pbservice.NewCheckServiceNodeFromStructs(&csn)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}, subMeshGateway+partition, state.updateCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *subscriptionManager) syncViaBlockingQuery(
|
|
|
|
ctx context.Context,
|
|
|
|
queryType string,
|
2022-07-08 17:01:13 +00:00
|
|
|
queryFn func(ctx context.Context, store StateStore, ws memdb.WatchSet) (interface{}, error),
|
2022-05-19 19:21:44 +00:00
|
|
|
correlationID string,
|
|
|
|
updateCh chan<- cache.UpdateEvent,
|
|
|
|
) {
|
|
|
|
waiter := &retry.Waiter{
|
|
|
|
MinFailures: 1,
|
|
|
|
Factor: 500 * time.Millisecond,
|
|
|
|
MaxWait: 60 * time.Second,
|
|
|
|
Jitter: retry.NewJitter(100),
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := m.logger
|
|
|
|
if queryType != "" {
|
|
|
|
logger = m.logger.With("queryType", queryType)
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
store := m.getStore()
|
2022-05-19 19:21:44 +00:00
|
|
|
|
2022-10-10 20:14:26 +00:00
|
|
|
for ctx.Err() == nil {
|
2022-05-19 19:21:44 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
ws.Add(store.AbandonCh())
|
|
|
|
ws.Add(ctx.Done())
|
|
|
|
|
2022-10-10 20:14:26 +00:00
|
|
|
if result, err := queryFn(ctx, store, ws); err != nil {
|
|
|
|
// Return immediately if the context was cancelled.
|
|
|
|
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
return
|
|
|
|
}
|
2022-05-19 19:21:44 +00:00
|
|
|
logger.Error("failed to sync from query", "error", err)
|
2022-10-10 20:14:26 +00:00
|
|
|
waiter.Wait(ctx)
|
2022-05-19 19:21:44 +00:00
|
|
|
} else {
|
2022-09-21 15:55:19 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case updateCh <- cache.UpdateEvent{CorrelationID: correlationID, Result: result}:
|
2022-10-10 20:14:26 +00:00
|
|
|
waiter.Reset()
|
2022-05-19 19:21:44 +00:00
|
|
|
}
|
2022-09-21 15:55:19 +00:00
|
|
|
// Block for any changes to the state store.
|
|
|
|
ws.WatchCtx(ctx)
|
2022-05-19 19:21:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|