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
|
|
|
|
2020-09-28 22:52:31 +00:00
|
|
|
package subscribe
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-uuid"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/consul/stream"
|
2023-02-17 21:14:46 +00:00
|
|
|
"github.com/hashicorp/consul/proto/private/pbsubscribe"
|
2020-09-28 22:52:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// streamID is used in logs as a unique identifier for a subscription. The value
|
|
|
|
// is created lazily on the first call to String() so that we do not call it
|
|
|
|
// if trace logging is disabled.
|
|
|
|
// If a random UUID can not be created, defaults to the current time formatted
|
|
|
|
// as RFC3339Nano.
|
|
|
|
//
|
|
|
|
// TODO(banks) it might be nice one day to replace this with OpenTracing ID
|
|
|
|
// if one is set etc. but probably pointless until we support that properly
|
|
|
|
// in other places so it's actually propagated properly. For now this just
|
|
|
|
// makes lifetime of a stream more traceable in our regular server logs for
|
|
|
|
// debugging/dev.
|
|
|
|
type streamID struct {
|
|
|
|
once sync.Once
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *streamID) String() string {
|
|
|
|
s.once.Do(func() {
|
|
|
|
var err error
|
|
|
|
s.id, err = uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
s.id = time.Now().Format(time.RFC3339Nano)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return s.id
|
|
|
|
}
|
|
|
|
|
2020-10-21 20:08:33 +00:00
|
|
|
func newLoggerForRequest(l Logger, req *pbsubscribe.SubscribeRequest) Logger {
|
proxycfg: server-local config entry data sources
This is the OSS portion of enterprise PR 2056.
This commit provides server-local implementations of the proxycfg.ConfigEntry
and proxycfg.ConfigEntryList interfaces, that source data from streaming events.
It makes use of the LocalMaterializer type introduced for peering replication,
adding the necessary support for authorization.
It also adds support for "wildcard" subscriptions (within a topic) to the event
publisher, as this is needed to fetch service-resolvers for all services when
configuring mesh gateways.
Currently, events will be emitted for just the ingress-gateway, service-resolver,
and mesh config entry types, as these are the only entries required by proxycfg
— the events will be emitted on topics named IngressGateway, ServiceResolver,
and MeshConfig topics respectively.
Though these events will only be consumed "locally" for now, they can also be
consumed via the gRPC endpoint (confirmed using grpcurl) so using them from
client agents should be a case of swapping the LocalMaterializer for an
RPCMaterializer.
2022-07-01 15:09:47 +00:00
|
|
|
l = l.With(
|
2020-09-28 22:52:31 +00:00
|
|
|
"topic", req.Topic.String(),
|
|
|
|
"dc", req.Datacenter,
|
2020-11-13 20:05:16 +00:00
|
|
|
"request_index", req.Index,
|
proxycfg: server-local config entry data sources
This is the OSS portion of enterprise PR 2056.
This commit provides server-local implementations of the proxycfg.ConfigEntry
and proxycfg.ConfigEntryList interfaces, that source data from streaming events.
It makes use of the LocalMaterializer type introduced for peering replication,
adding the necessary support for authorization.
It also adds support for "wildcard" subscriptions (within a topic) to the event
publisher, as this is needed to fetch service-resolvers for all services when
configuring mesh gateways.
Currently, events will be emitted for just the ingress-gateway, service-resolver,
and mesh config entry types, as these are the only entries required by proxycfg
— the events will be emitted on topics named IngressGateway, ServiceResolver,
and MeshConfig topics respectively.
Though these events will only be consumed "locally" for now, they can also be
consumed via the gRPC endpoint (confirmed using grpcurl) so using them from
client agents should be a case of swapping the LocalMaterializer for an
RPCMaterializer.
2022-07-01 15:09:47 +00:00
|
|
|
"stream_id", &streamID{},
|
|
|
|
)
|
|
|
|
|
|
|
|
if req.GetWildcardSubject() {
|
|
|
|
return l.With("wildcard_subject", true)
|
|
|
|
}
|
|
|
|
|
|
|
|
named := req.GetNamedSubject()
|
|
|
|
if named == nil {
|
|
|
|
named = &pbsubscribe.NamedSubject{
|
|
|
|
Key: req.Key, // nolint:staticcheck // SA1019 intentional use of deprecated field
|
|
|
|
Partition: req.Partition, // nolint:staticcheck // SA1019 intentional use of deprecated field
|
|
|
|
Namespace: req.Namespace, // nolint:staticcheck // SA1019 intentional use of deprecated field
|
|
|
|
PeerName: req.PeerName, // nolint:staticcheck // SA1019 intentional use of deprecated field
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return l.With(
|
|
|
|
"peer", named.PeerName,
|
|
|
|
"key", named.Key,
|
|
|
|
"namespace", named.Namespace,
|
|
|
|
"partition", named.Partition,
|
|
|
|
)
|
2020-09-28 22:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type eventLogger struct {
|
|
|
|
logger Logger
|
|
|
|
snapshotDone bool
|
|
|
|
count uint64
|
|
|
|
}
|
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
func (l *eventLogger) Trace(e stream.Event) {
|
2020-09-28 22:52:31 +00:00
|
|
|
switch {
|
2020-10-05 16:38:38 +00:00
|
|
|
case e.IsEndOfSnapshot():
|
2020-09-28 22:52:31 +00:00
|
|
|
l.snapshotDone = true
|
2020-10-05 16:38:38 +00:00
|
|
|
l.logger.Trace("snapshot complete", "index", e.Index, "sent", l.count)
|
2020-11-05 22:57:25 +00:00
|
|
|
return
|
2020-10-05 16:38:38 +00:00
|
|
|
case e.IsNewSnapshotToFollow():
|
|
|
|
l.logger.Trace("starting new snapshot", "sent", l.count)
|
2020-10-02 17:55:41 +00:00
|
|
|
return
|
2020-09-28 22:52:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 22:57:25 +00:00
|
|
|
size := 1
|
|
|
|
if l, ok := e.Payload.(length); ok {
|
|
|
|
size = l.Len()
|
|
|
|
}
|
|
|
|
l.logger.Trace("sending events", "index", e.Index, "sent", l.count, "batch_size", size)
|
|
|
|
l.count += uint64(size)
|
|
|
|
}
|
|
|
|
|
|
|
|
type length interface {
|
|
|
|
Len() int
|
2020-09-28 22:52:31 +00:00
|
|
|
}
|