Documentation
¶
Overview ¶
Package engine implements the core.Engine interface and orchestration pipeline.
Index ¶
- Constants
- func New() core.Engine
- func NewScheduler(...) *scheduler
- type CoreCEngine
- func (e *CoreCEngine) AddDriver(config core.DriverConfig) error
- func (e *CoreCEngine) AddTransport(config core.TransportConfig) error
- func (e *CoreCEngine) DataAgeHistogram() core.LatencySnapshot
- func (e *CoreCEngine) DeadLetterEntries() []core.DeadLetterEntry
- func (e *CoreCEngine) FlushBatchesDropped() uint64
- func (e *CoreCEngine) GetDriver(name string) (core.Driver, bool)
- func (e *CoreCEngine) GetRuleStats() []core.RuleStat
- func (e *CoreCEngine) GetTransport(name string) (core.Transport, bool)
- func (e *CoreCEngine) LatestValues(driver string) map[string]core.DataPoint
- func (e *CoreCEngine) ListDrivers() []core.DriverStatus
- func (e *CoreCEngine) ListTransports() []core.TransportStatus
- func (e *CoreCEngine) OfflineBufferStats() (pending int, drained, pushed uint64)
- func (e *CoreCEngine) OnAlert(handler func(point core.DataPoint, rule core.Rule))
- func (e *CoreCEngine) PublishLatency() *metrics.LatencyHistogram
- func (e *CoreCEngine) PublishLatencyHistogram() core.LatencySnapshot
- func (e *CoreCEngine) ReadLatency() *metrics.LatencyHistogram
- func (e *CoreCEngine) ReadLatencyHistogram() core.LatencySnapshot
- func (e *CoreCEngine) ReadTag(ctx context.Context, driver, tag string) (*core.TagValue, error)
- func (e *CoreCEngine) Reload(config *core.Config) error
- func (e *CoreCEngine) RemoveDriver(name string) error
- func (e *CoreCEngine) RemoveTransport(name string) error
- func (e *CoreCEngine) Resume() error
- func (e *CoreCEngine) SetRuleDisabled(index int, disabled bool) error
- func (e *CoreCEngine) SetRules(rules []core.RuleConfig) error
- func (e *CoreCEngine) StaleThreshold() time.Duration
- func (e *CoreCEngine) Start(ctx context.Context, config *core.Config) error
- func (e *CoreCEngine) Stats() core.EngineStats
- func (e *CoreCEngine) Status() core.EngineStatus
- func (e *CoreCEngine) Stop() error
- func (e *CoreCEngine) Subscribe(filter string) (ch <-chan core.DataPoint, unsub func())
- func (e *CoreCEngine) SubscribeWithBuffer(filter string, size int) (ch <-chan core.DataPoint, unsub func())
- func (e *CoreCEngine) Suspend() error
- func (e *CoreCEngine) WriteTag(ctx context.Context, cmd core.WriteCommand) (*core.WriteResult, error)
- type DataBus
- func (b *DataBus) Broadcast(point core.DataPoint)
- func (b *DataBus) Channel() <-chan core.DataPoint
- func (b *DataBus) Close()
- func (b *DataBus) Dropped() int64
- func (b *DataBus) HighPriorityChannel() <-chan core.DataPoint
- func (b *DataBus) HighPriorityPushDropped() int64
- func (b *DataBus) Push(point core.DataPoint)
- func (b *DataBus) PushDropped() int64
- func (b *DataBus) PushHighPriority(point core.DataPoint)
- func (b *DataBus) Subscribe(filter string) (events <-chan core.DataPoint, unsub func())
- func (b *DataBus) SubscribeWithBuffer(filter string, size int) (events <-chan core.DataPoint, unsub func())
- type Discovery
- type LatestCache
- type OfflineBuffer
- func (ob *OfflineBuffer) Close() error
- func (ob *OfflineBuffer) Drain(tryPublish func([]core.DataPoint) error) (int, error)
- func (ob *OfflineBuffer) DrainCount() uint64
- func (ob *OfflineBuffer) Len() int
- func (ob *OfflineBuffer) PendingCount() int
- func (ob *OfflineBuffer) Push(points []core.DataPoint, source string) error
Constants ¶
const DefaultSubscriberBuffer = 256
DefaultSubscriberBuffer is the default buffer size for DataBus subscriber channels.
Variables ¶
This section is empty.
Functions ¶
func NewScheduler ¶
func NewScheduler( readFunc func(ctx context.Context, driver string, tags []string) ([]core.TagValue, error), onData func(driver string, values []core.TagValue, priority int), errorThrottleWindow time.Duration, statManager *statistic.Manager, ) *scheduler
NewScheduler creates a new scheduler. readFunc is called to read tags from a driver. onData is called when data is received, with the task's priority so the engine can route fast-interval data to a dedicated high-priority channel. errorThrottleWindow is the time window for suppressing repeated error logs; <=0 falls back to core.DefaultErrorThrottleWindow. statManager collects throughput counters; if nil, a new Manager is created (but callers should pass the engine's instance for isolation).
Types ¶
type CoreCEngine ¶
type CoreCEngine struct {
// contains filtered or unexported fields
}
CoreCEngine implements core.Engine. This is the central orchestrator.
func (*CoreCEngine) AddDriver ¶
func (e *CoreCEngine) AddDriver(config core.DriverConfig) error
func (*CoreCEngine) AddTransport ¶
func (e *CoreCEngine) AddTransport(config core.TransportConfig) error
func (*CoreCEngine) DataAgeHistogram ¶ added in v0.0.3
func (e *CoreCEngine) DataAgeHistogram() core.LatencySnapshot
DataAgeHistogram returns a point-in-time snapshot of the data-age (freshness) histogram, satisfying the core.DataAgeProvider interface. Data age is the elapsed time from DataPoint.Timestamp (collection/source time) to the publish attempt, measured at each publish site.
func (*CoreCEngine) DeadLetterEntries ¶ added in v0.0.2
func (e *CoreCEngine) DeadLetterEntries() []core.DeadLetterEntry
DeadLetterEntries returns a copy of the current dead letter queue.
func (*CoreCEngine) FlushBatchesDropped ¶ added in v0.0.3
func (e *CoreCEngine) FlushBatchesDropped() uint64
FlushBatchesDropped returns the total number of full batches that were evicted or dropped from batcher flush queues because the async flush consumer fell behind a sustained burst (IMPROVEMENTS #1: async batcher backpressure observability). It aggregates the per-batcher flushBatchesDropped counters. Intended for Prometheus exposure as corec_flush_batches_dropped_total.
func (*CoreCEngine) GetRuleStats ¶
func (e *CoreCEngine) GetRuleStats() []core.RuleStat
func (*CoreCEngine) GetTransport ¶
func (e *CoreCEngine) GetTransport(name string) (core.Transport, bool)
func (*CoreCEngine) LatestValues ¶
func (e *CoreCEngine) LatestValues(driver string) map[string]core.DataPoint
func (*CoreCEngine) ListDrivers ¶
func (e *CoreCEngine) ListDrivers() []core.DriverStatus
func (*CoreCEngine) ListTransports ¶
func (e *CoreCEngine) ListTransports() []core.TransportStatus
func (*CoreCEngine) OfflineBufferStats ¶ added in v0.0.3
func (e *CoreCEngine) OfflineBufferStats() (pending int, drained, pushed uint64)
OfflineBufferStats aggregates offline-buffer observability counters across all transport batchers for Prometheus exposure:
- pending: batches currently waiting on disk for replay (gauge).
- drained: total batches successfully replayed since startup (counter).
- pushed: total batches persisted to the offline buffer since startup (counter).
The offline buffer is a single shared instance (e.offlineBuffer) handed to every batcher in newTransportBatcher, so pending and drained are read once from it rather than summed per batcher — summing the per-batcher OfflineBufferPending/OfflineBufferDrained values would multiply the real count by the number of batchers, since they all delegate to the same buffer. pushed, by contrast, is a genuine per-batcher counter (offlineBufferPushes, incremented in publishWithRetryAndBuffer) and must be summed across batchers.
The batchers map is snapshotted under RLock and then released before reading the counters, mirroring the Stop() pattern, so a slow Len() (disk readdir) can never block management API calls that take the write lock.
func (*CoreCEngine) OnAlert ¶
func (e *CoreCEngine) OnAlert(handler func(point core.DataPoint, rule core.Rule))
func (*CoreCEngine) PublishLatency ¶ added in v0.0.3
func (e *CoreCEngine) PublishLatency() *metrics.LatencyHistogram
PublishLatency returns the histogram tracking transport publish latency (time spent in Transport.Publish / batcher.publish, including fallback attempts), for Prometheus histogram exposure. The returned pointer is always non-nil for an engine created via New().
func (*CoreCEngine) PublishLatencyHistogram ¶ added in v0.0.3
func (e *CoreCEngine) PublishLatencyHistogram() core.LatencySnapshot
PublishLatencyHistogram returns a point-in-time snapshot of the publish latency histogram, satisfying the core.LatencyProvider interface.
func (*CoreCEngine) ReadLatency ¶ added in v0.0.3
func (e *CoreCEngine) ReadLatency() *metrics.LatencyHistogram
ReadLatency returns the histogram tracking driver read latency (time spent in Driver.Read), for Prometheus histogram exposure. The returned pointer is always non-nil for an engine created via New().
func (*CoreCEngine) ReadLatencyHistogram ¶ added in v0.0.3
func (e *CoreCEngine) ReadLatencyHistogram() core.LatencySnapshot
ReadLatencyHistogram returns a point-in-time snapshot of the read latency histogram, satisfying the core.LatencyProvider interface.
func (*CoreCEngine) RemoveDriver ¶
func (e *CoreCEngine) RemoveDriver(name string) error
func (*CoreCEngine) RemoveTransport ¶
func (e *CoreCEngine) RemoveTransport(name string) error
func (*CoreCEngine) Resume ¶
func (e *CoreCEngine) Resume() error
func (*CoreCEngine) SetRuleDisabled ¶
func (e *CoreCEngine) SetRuleDisabled(index int, disabled bool) error
func (*CoreCEngine) SetRules ¶
func (e *CoreCEngine) SetRules(rules []core.RuleConfig) error
func (*CoreCEngine) StaleThreshold ¶ added in v0.0.2
func (e *CoreCEngine) StaleThreshold() time.Duration
func (*CoreCEngine) Stats ¶
func (e *CoreCEngine) Stats() core.EngineStats
func (*CoreCEngine) Status ¶
func (e *CoreCEngine) Status() core.EngineStatus
func (*CoreCEngine) Stop ¶
func (e *CoreCEngine) Stop() error
func (*CoreCEngine) Subscribe ¶
func (e *CoreCEngine) Subscribe(filter string) (ch <-chan core.DataPoint, unsub func())
func (*CoreCEngine) SubscribeWithBuffer ¶
func (e *CoreCEngine) SubscribeWithBuffer(filter string, size int) (ch <-chan core.DataPoint, unsub func())
SubscribeWithBuffer is like Subscribe but lets the caller choose the subscriber channel buffer size.
func (*CoreCEngine) Suspend ¶
func (e *CoreCEngine) Suspend() error
func (*CoreCEngine) WriteTag ¶
func (e *CoreCEngine) WriteTag(ctx context.Context, cmd core.WriteCommand) (*core.WriteResult, error)
type DataBus ¶
type DataBus struct {
// contains filtered or unexported fields
}
DataBus is the internal data channel that connects drivers to the processing pipeline. It uses Go channels for high-performance, lock-free data flow.
DataBus supports two priority tiers (IMPROVEMENTS #5): a main channel for normal-priority data and a dedicated high-priority channel for fast-interval tasks. The engine runs separate worker goroutines for each tier so a burst of low-frequency bulk reads cannot starve high-frequency collection.
func NewDataBus ¶
NewDataBus creates a new DataBus with the given buffer size. The high-priority channel uses a smaller buffer (1/4 of the main, capped at 1024) because high-frequency data is less voluminous per tick.
func (*DataBus) Broadcast ¶
Broadcast sends a DataPoint to all subscribers. Subscribers with a non-empty filter only receive points whose Driver matches the filter; an empty filter receives all points.
func (*DataBus) Dropped ¶
Dropped returns the total number of messages dropped due to slow subscribers.
func (*DataBus) HighPriorityChannel ¶ added in v0.0.3
HighPriorityChannel returns the high-priority data channel for consumption by dedicated high-priority workers.
func (*DataBus) HighPriorityPushDropped ¶ added in v0.0.3
HighPriorityPushDropped returns the number of messages evicted from the high-priority channel because its buffer was full.
func (*DataBus) Push ¶
Push sends a DataPoint into the bus. It is safe to call Push concurrently with Close; a Push that races with Close is silently dropped instead of panicking.
func (*DataBus) PushDropped ¶
PushDropped returns the number of messages evicted from the main channel because the bus buffer was full (producer faster than consumer).
func (*DataBus) PushHighPriority ¶ added in v0.0.3
PushHighPriority sends a DataPoint into the high-priority channel. It uses the same drop-oldest semantics as Push so the newest high-frequency data is always retained. Safe to call concurrently with Close (IMPROVEMENTS #5: priority-tiered data routing).
type Discovery ¶ added in v0.0.2
type Discovery struct {
// contains filtered or unexported fields
}
Discovery manages topology auto-discovery via MQTT heartbeats. Each broker gets its own lightweight MQTT client that publishes the node's identity and subscribes to other nodes' heartbeats.
func NewDiscovery ¶ added in v0.0.2
func NewDiscovery(node core.NodeConfig, brokers []brokerEndpoint, addTransport func(core.TransportConfig) error) *Discovery
NewDiscovery creates a discovery module. brokers lists each broker this node connects to and what endpoints it advertises on each.
func (*Discovery) GetDiscoveredNodes ¶ added in v0.0.2
func (d *Discovery) GetDiscoveredNodes() []nodeInfo
GetDiscoveredNodes returns a snapshot of all known nodes.
type LatestCache ¶
type LatestCache struct {
// contains filtered or unexported fields
}
LatestCache maintains the most recent value for each tag. It is sharded by driver name to reduce mutex contention when multiple processing workers update the cache concurrently.
func (*LatestCache) Get ¶
func (c *LatestCache) Get(driver, tag string) (core.DataPoint, bool)
Get retrieves the latest value for a specific driver and tag.
func (*LatestCache) GetAll ¶ added in v0.0.3
func (c *LatestCache) GetAll() map[string]core.DataPoint
GetAll retrieves the latest values for every driver, keyed by tag name. When two drivers share a tag name, the last one visited wins (the DataPoint value carries its own Driver field, so the source is not lost). This is the backing implementation for the GET /tags endpoint.
func (*LatestCache) GetByDriver ¶
func (c *LatestCache) GetByDriver(driver string) map[string]core.DataPoint
GetByDriver retrieves all latest values for a driver. It returns an atomic snapshot that is reused across calls when the data hasn't changed, avoiding a full map copy.
func (*LatestCache) Update ¶
func (c *LatestCache) Update(point core.DataPoint)
Update stores or updates a DataPoint in the cache.
type OfflineBuffer ¶ added in v0.0.2
type OfflineBuffer struct {
// contains filtered or unexported fields
}
OfflineBuffer stores failed publish batches to disk so they can be replayed after the transport recovers. Each batch is written as a numbered JSON file inside the configured directory, making the storage crash-safe (each file is atomically renamed into place) and easy to inspect or purge manually.
The buffer is bounded by maxEntries; when full, the oldest entries are evicted to make room for newer ones (newer data is more valuable in real-time industrial monitoring).
func NewOfflineBuffer ¶ added in v0.0.2
func NewOfflineBuffer(dir string, maxEntries int) (*OfflineBuffer, error)
NewOfflineBuffer creates an OfflineBuffer rooted at dir. If dir does not exist it is created. maxEntries <= 0 defaults to core.DefaultOfflineBufferMaxEntries.
func (*OfflineBuffer) Close ¶ added in v0.0.2
func (ob *OfflineBuffer) Close() error
Close is a no-op for the file-based buffer; files remain on disk for replay after restart.
func (*OfflineBuffer) Drain ¶ added in v0.0.2
Drain attempts to replay all buffered batches. For each batch, it calls tryPublish; if that returns nil the batch is deleted from disk, otherwise draining stops (the transport is likely still down). Returns the number of batches successfully drained.
func (*OfflineBuffer) DrainCount ¶ added in v0.0.3
func (ob *OfflineBuffer) DrainCount() uint64
DrainCount returns the total number of batches successfully replayed by Drain since the buffer was created. It is a monotonically increasing counter, read atomically and lock-free, suitable for exposure as a Prometheus counter (e.g. corec_offline_buffer_drained_total).
func (*OfflineBuffer) Len ¶ added in v0.0.2
func (ob *OfflineBuffer) Len() int
Len returns the number of buffered batches currently on disk.
func (*OfflineBuffer) PendingCount ¶ added in v0.0.3
func (ob *OfflineBuffer) PendingCount() int
PendingCount returns the number of buffered batches currently awaiting replay. It is a thread-safe alias for Len, exposed under a name that describes its role for observability metrics (a Prometheus gauge of "pending offline batches"). It is safe for concurrent use.