Debian Codegraph @

server/jetstream_events.go

raw
1 // Copyright 2020-2025 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package server
15
16 import (
17 "encoding/json"
18 "time"
19 )
20
21 // publishAdvisory sends the given advisory into the account. Returns true if
22 // it was sent, false if not (i.e. due to lack of interest or a marshal error).
23 func (s *Server) publishAdvisory(acc *Account, subject string, adv any) bool {
24 if acc == nil {
25 acc = s.SystemAccount()
26 if acc == nil {
27 return false
28 }
29 }
30
31 // If there is no one listening for this advisory then save ourselves the effort
32 // and don't bother encoding the JSON or sending it.
33 if sl := acc.sl; (sl != nil && !sl.HasInterest(subject)) && !s.hasGatewayInterest(acc.Name, subject) {
34 return false
35 }
36
37 ej, err := json.Marshal(adv)
38 if err == nil {
39 err = s.sendInternalAccountMsg(acc, subject, ej)
40 if err != nil {
41 s.Warnf("Advisory could not be sent for account %q: %v", acc.Name, err)
42 }
43 } else {
44 s.Warnf("Advisory could not be serialized for account %q: %v", acc.Name, err)
45 }
46 return err == nil
47 }
48
49 // JSAPIAudit is an advisory about administrative actions taken on JetStream
50 type JSAPIAudit struct {
51 TypedEvent
52 Server string `json:"server"`
53 Client *ClientInfo `json:"client"`
54 Subject string `json:"subject"`
55 Request string `json:"request,omitempty"`
56 Response string `json:"response"`
57 Domain string `json:"domain,omitempty"`
58 }
59
60 const JSAPIAuditType = "io.nats.jetstream.advisory.v1.api_audit"
61
62 // ActionAdvisoryType indicates which action against a stream, consumer or template triggered an advisory
63 type ActionAdvisoryType string
64
65 const (
66 CreateEvent ActionAdvisoryType = "create"
67 DeleteEvent ActionAdvisoryType = "delete"
68 ModifyEvent ActionAdvisoryType = "modify"
69 )
70
71 // JSStreamActionAdvisory indicates that a stream was created, edited or deleted
72 type JSStreamActionAdvisory struct {
73 TypedEvent
74 Stream string `json:"stream"`
75 Action ActionAdvisoryType `json:"action"`
76 Domain string `json:"domain,omitempty"`
77 }
78
79 const JSStreamActionAdvisoryType = "io.nats.jetstream.advisory.v1.stream_action"
80
81 // JSConsumerActionAdvisory indicates that a consumer was created or deleted
82 type JSConsumerActionAdvisory struct {
83 TypedEvent
84 Stream string `json:"stream"`
85 Consumer string `json:"consumer"`
86 Action ActionAdvisoryType `json:"action"`
87 Domain string `json:"domain,omitempty"`
88 }
89
90 const JSConsumerActionAdvisoryType = "io.nats.jetstream.advisory.v1.consumer_action"
91
92 // JSConsumerPauseAdvisory indicates that a consumer was paused or unpaused
93 type JSConsumerPauseAdvisory struct {
94 TypedEvent
95 Stream string `json:"stream"`
96 Consumer string `json:"consumer"`
97 Paused bool `json:"paused"`
98 PauseUntil time.Time `json:"pause_until,omitempty"`
99 Domain string `json:"domain,omitempty"`
100 }
101
102 const JSConsumerPauseAdvisoryType = "io.nats.jetstream.advisory.v1.consumer_pause"
103
104 // JSConsumerAckMetric is a metric published when a user acknowledges a message, the
105 // number of these that will be published is dependent on SampleFrequency
106 type JSConsumerAckMetric struct {
107 TypedEvent
108 Stream string `json:"stream"`
109 Consumer string `json:"consumer"`
110 ConsumerSeq uint64 `json:"consumer_seq"`
111 StreamSeq uint64 `json:"stream_seq"`
112 Delay int64 `json:"ack_time"`
113 Deliveries uint64 `json:"deliveries"`
114 Domain string `json:"domain,omitempty"`
115 }
116
117 // JSConsumerAckMetricType is the schema type for JSConsumerAckMetricType
118 const JSConsumerAckMetricType = "io.nats.jetstream.metric.v1.consumer_ack"
119
120 // JSConsumerDeliveryExceededAdvisory is an advisory informing that a message hit
121 // its MaxDeliver threshold and so might be a candidate for DLQ handling
122 type JSConsumerDeliveryExceededAdvisory struct {
123 TypedEvent
124 Stream string `json:"stream"`
125 Consumer string `json:"consumer"`
126 StreamSeq uint64 `json:"stream_seq"`
127 Deliveries uint64 `json:"deliveries"`
128 Domain string `json:"domain,omitempty"`
129 }
130
131 // JSConsumerDeliveryExceededAdvisoryType is the schema type for JSConsumerDeliveryExceededAdvisory
132 const JSConsumerDeliveryExceededAdvisoryType = "io.nats.jetstream.advisory.v1.max_deliver"
133
134 // JSConsumerDeliveryNakAdvisory is an advisory informing that a message was
135 // naked by the consumer
136 type JSConsumerDeliveryNakAdvisory struct {
137 TypedEvent
138 Stream string `json:"stream"`
139 Consumer string `json:"consumer"`
140 ConsumerSeq uint64 `json:"consumer_seq"`
141 StreamSeq uint64 `json:"stream_seq"`
142 Deliveries uint64 `json:"deliveries"`
143 Domain string `json:"domain,omitempty"`
144 }
145
146 // JSConsumerDeliveryNakAdvisoryType is the schema type for JSConsumerDeliveryNakAdvisory
147 const JSConsumerDeliveryNakAdvisoryType = "io.nats.jetstream.advisory.v1.nak"
148
149 // JSConsumerDeliveryTerminatedAdvisory is an advisory informing that a message was
150 // terminated by the consumer, so might be a candidate for DLQ handling
151 type JSConsumerDeliveryTerminatedAdvisory struct {
152 TypedEvent
153 Stream string `json:"stream"`
154 Consumer string `json:"consumer"`
155 ConsumerSeq uint64 `json:"consumer_seq"`
156 StreamSeq uint64 `json:"stream_seq"`
157 Deliveries uint64 `json:"deliveries"`
158 Reason string `json:"reason,omitempty"`
159 Domain string `json:"domain,omitempty"`
160 }
161
162 // JSConsumerDeliveryTerminatedAdvisoryType is the schema type for JSConsumerDeliveryTerminatedAdvisory
163 const JSConsumerDeliveryTerminatedAdvisoryType = "io.nats.jetstream.advisory.v1.terminated"
164
165 // JSSnapshotCreateAdvisory is an advisory sent after a snapshot is successfully started
166 type JSSnapshotCreateAdvisory struct {
167 TypedEvent
168 Stream string `json:"stream"`
169 State StreamState `json:"state"`
170 Client *ClientInfo `json:"client"`
171 Domain string `json:"domain,omitempty"`
172 }
173
174 // JSSnapshotCreatedAdvisoryType is the schema type for JSSnapshotCreateAdvisory
175 const JSSnapshotCreatedAdvisoryType = "io.nats.jetstream.advisory.v1.snapshot_create"
176
177 // JSSnapshotCompleteAdvisory is an advisory sent after a snapshot is successfully started
178 type JSSnapshotCompleteAdvisory struct {
179 TypedEvent
180 Stream string `json:"stream"`
181 Start time.Time `json:"start"`
182 End time.Time `json:"end"`
183 Client *ClientInfo `json:"client"`
184 Domain string `json:"domain,omitempty"`
185 }
186
187 // JSSnapshotCompleteAdvisoryType is the schema type for JSSnapshotCreateAdvisory
188 const JSSnapshotCompleteAdvisoryType = "io.nats.jetstream.advisory.v1.snapshot_complete"
189
190 // JSRestoreCreateAdvisory is an advisory sent after a snapshot is successfully started
191 type JSRestoreCreateAdvisory struct {
192 TypedEvent
193 Stream string `json:"stream"`
194 Client *ClientInfo `json:"client"`
195 Domain string `json:"domain,omitempty"`
196 }
197
198 // JSRestoreCreateAdvisoryType is the schema type for JSSnapshotCreateAdvisory
199 const JSRestoreCreateAdvisoryType = "io.nats.jetstream.advisory.v1.restore_create"
200
201 // JSRestoreCompleteAdvisory is an advisory sent after a snapshot is successfully started
202 type JSRestoreCompleteAdvisory struct {
203 TypedEvent
204 Stream string `json:"stream"`
205 Start time.Time `json:"start"`
206 End time.Time `json:"end"`
207 Bytes int64 `json:"bytes"`
208 Client *ClientInfo `json:"client"`
209 Domain string `json:"domain,omitempty"`
210 }
211
212 // JSRestoreCompleteAdvisoryType is the schema type for JSSnapshotCreateAdvisory
213 const JSRestoreCompleteAdvisoryType = "io.nats.jetstream.advisory.v1.restore_complete"
214
215 // Clustering specific.
216
217 // JSClusterLeaderElectedAdvisoryType is sent when the system elects a new meta leader.
218 const JSDomainLeaderElectedAdvisoryType = "io.nats.jetstream.advisory.v1.domain_leader_elected"
219
220 // JSClusterLeaderElectedAdvisory indicates that a domain has elected a new leader.
221 type JSDomainLeaderElectedAdvisory struct {
222 TypedEvent
223 Leader string `json:"leader"`
224 Replicas []*PeerInfo `json:"replicas"`
225 Cluster string `json:"cluster"`
226 Domain string `json:"domain,omitempty"`
227 }
228
229 // JSStreamLeaderElectedAdvisoryType is sent when the system elects a new leader for a stream.
230 const JSStreamLeaderElectedAdvisoryType = "io.nats.jetstream.advisory.v1.stream_leader_elected"
231
232 // JSStreamLeaderElectedAdvisory indicates that a stream has elected a new leader.
233 type JSStreamLeaderElectedAdvisory struct {
234 TypedEvent
235 Account string `json:"account,omitempty"`
236 Stream string `json:"stream"`
237 Leader string `json:"leader"`
238 Replicas []*PeerInfo `json:"replicas"`
239 Domain string `json:"domain,omitempty"`
240 }
241
242 // JSStreamQuorumLostAdvisoryType is sent when the system detects a clustered stream and
243 // its consumers are stalled and unable to make progress.
244 const JSStreamQuorumLostAdvisoryType = "io.nats.jetstream.advisory.v1.stream_quorum_lost"
245
246 // JSStreamQuorumLostAdvisory indicates that a stream has lost quorum and is stalled.
247 type JSStreamQuorumLostAdvisory struct {
248 TypedEvent
249 Account string `json:"account,omitempty"`
250 Stream string `json:"stream"`
251 Replicas []*PeerInfo `json:"replicas"`
252 Domain string `json:"domain,omitempty"`
253 }
254
255 // JSStreamBatchAbandonedAdvisoryType is sent when a stream's atomic batch is abandoned.
256 const JSStreamBatchAbandonedAdvisoryType = "io.nats.jetstream.advisory.v1.stream_batch_abandoned"
257
258 // JSStreamBatchAbandonedAdvisory indicates that a stream's batch was abandoned.
259 type JSStreamBatchAbandonedAdvisory struct {
260 TypedEvent
261 Account string `json:"account,omitempty"`
262 Stream string `json:"stream"`
263 Domain string `json:"domain,omitempty"`
264 BatchId string `json:"batch"`
265 Reason BatchAbandonReason `json:"reason"`
266 }
267
268 type BatchAbandonReason string
269
270 var (
271 BatchTimeout BatchAbandonReason = "timeout"
272 BatchLarge BatchAbandonReason = "large"
273 BatchIncomplete BatchAbandonReason = "incomplete"
274 BatchRequirementsNotMet BatchAbandonReason = "unsupported"
275 )
276
277 // JSConsumerLeaderElectedAdvisoryType is sent when the system elects a leader for a consumer.
278 const JSConsumerLeaderElectedAdvisoryType = "io.nats.jetstream.advisory.v1.consumer_leader_elected"
279
280 // JSConsumerLeaderElectedAdvisory indicates that a consumer has elected a new leader.
281 type JSConsumerLeaderElectedAdvisory struct {
282 TypedEvent
283 Account string `json:"account,omitempty"`
284 Stream string `json:"stream"`
285 Consumer string `json:"consumer"`
286 Leader string `json:"leader"`
287 Replicas []*PeerInfo `json:"replicas"`
288 Domain string `json:"domain,omitempty"`
289 }
290
291 // JSConsumerQuorumLostAdvisoryType is sent when the system detects a clustered consumer and
292 // is stalled and unable to make progress.
293 const JSConsumerQuorumLostAdvisoryType = "io.nats.jetstream.advisory.v1.consumer_quorum_lost"
294
295 // JSConsumerQuorumLostAdvisory indicates that a consumer has lost quorum and is stalled.
296 type JSConsumerQuorumLostAdvisory struct {
297 TypedEvent
298 Account string `json:"account,omitempty"`
299 Stream string `json:"stream"`
300 Consumer string `json:"consumer"`
301 Replicas []*PeerInfo `json:"replicas"`
302 Domain string `json:"domain,omitempty"`
303 }
304
305 const JSConsumerGroupPinnedAdvisoryType = "io.nats.jetstream.advisory.v1.consumer_group_pinned"
306
307 // JSConsumerGroupPinnedAdvisory that a group switched to a new pinned client
308 type JSConsumerGroupPinnedAdvisory struct {
309 TypedEvent
310 Account string `json:"account,omitempty"`
311 Stream string `json:"stream"`
312 Consumer string `json:"consumer"`
313 Domain string `json:"domain,omitempty"`
314 Group string `json:"group"`
315 PinnedClientId string `json:"pinned_id"`
316 }
317
318 const JSConsumerGroupUnpinnedAdvisoryType = "io.nats.jetstream.advisory.v1.consumer_group_unpinned"
319
320 // JSConsumerGroupUnpinnedAdvisory indicates that a pin was lost
321 type JSConsumerGroupUnpinnedAdvisory struct {
322 TypedEvent
323 Account string `json:"account,omitempty"`
324 Stream string `json:"stream"`
325 Consumer string `json:"consumer"`
326 Domain string `json:"domain,omitempty"`
327 Group string `json:"group"`
328 // one of "admin" or "timeout", could be an enum up to the implementor to decide
329 Reason string `json:"reason"`
330 }
331
332 // JSServerOutOfStorageAdvisoryType is sent when the server is out of storage space.
333 const JSServerOutOfStorageAdvisoryType = "io.nats.jetstream.advisory.v1.server_out_of_space"
334
335 // JSServerOutOfSpaceAdvisory indicates that a stream has lost quorum and is stalled.
336 type JSServerOutOfSpaceAdvisory struct {
337 TypedEvent
338 Server string `json:"server"`
339 ServerID string `json:"server_id"`
340 Stream string `json:"stream,omitempty"`
341 Cluster string `json:"cluster"`
342 Domain string `json:"domain,omitempty"`
343 }
344
345 // JSServerRemovedAdvisoryType is sent when the server has been removed and JS disabled.
346 const JSServerRemovedAdvisoryType = "io.nats.jetstream.advisory.v1.server_removed"
347
348 // JSServerRemovedAdvisory indicates that a stream has lost quorum and is stalled.
349 type JSServerRemovedAdvisory struct {
350 TypedEvent
351 Server string `json:"server"`
352 ServerID string `json:"server_id"`
353 Cluster string `json:"cluster"`
354 Domain string `json:"domain,omitempty"`
355 }
356
357 // JSAPILimitReachedAdvisoryType is sent when the JS API request queue limit is reached.
358 const JSAPILimitReachedAdvisoryType = "io.nats.jetstream.advisory.v1.api_limit_reached"
359
360 // JSAPILimitReachedAdvisory is a advisory published when JetStream hits the queue length limit.
361 type JSAPILimitReachedAdvisory struct {
362 TypedEvent
363 Server string `json:"server"` // Server that created the event, name or ID
364 Domain string `json:"domain,omitempty"` // Domain the server belongs to
365 Dropped int64 `json:"dropped"` // How many messages did we drop from the queue
366 }