github.com/nats-io/nats-server/v2@v2.11.0-preview.2/server/jetstream_events.go (about)

     1  // Copyright 2020-2021 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  func (s *Server) publishAdvisory(acc *Account, subject string, adv any) {
    22  	if acc == nil {
    23  		acc = s.SystemAccount()
    24  		if acc == nil {
    25  			return
    26  		}
    27  	}
    28  	ej, err := json.Marshal(adv)
    29  	if err == nil {
    30  		err = s.sendInternalAccountMsg(acc, subject, ej)
    31  		if err != nil {
    32  			s.Warnf("Advisory could not be sent for account %q: %v", acc.Name, err)
    33  		}
    34  	} else {
    35  		s.Warnf("Advisory could not be serialized for account %q: %v", acc.Name, err)
    36  	}
    37  }
    38  
    39  // JSAPIAudit is an advisory about administrative actions taken on JetStream
    40  type JSAPIAudit struct {
    41  	TypedEvent
    42  	Server   string      `json:"server"`
    43  	Client   *ClientInfo `json:"client"`
    44  	Subject  string      `json:"subject"`
    45  	Request  string      `json:"request,omitempty"`
    46  	Response string      `json:"response"`
    47  	Domain   string      `json:"domain,omitempty"`
    48  }
    49  
    50  const JSAPIAuditType = "io.nats.jetstream.advisory.v1.api_audit"
    51  
    52  // ActionAdvisoryType indicates which action against a stream, consumer or template triggered an advisory
    53  type ActionAdvisoryType string
    54  
    55  const (
    56  	CreateEvent ActionAdvisoryType = "create"
    57  	DeleteEvent ActionAdvisoryType = "delete"
    58  	ModifyEvent ActionAdvisoryType = "modify"
    59  )
    60  
    61  // JSStreamActionAdvisory indicates that a stream was created, edited or deleted
    62  type JSStreamActionAdvisory struct {
    63  	TypedEvent
    64  	Stream   string             `json:"stream"`
    65  	Action   ActionAdvisoryType `json:"action"`
    66  	Template string             `json:"template,omitempty"`
    67  	Domain   string             `json:"domain,omitempty"`
    68  }
    69  
    70  const JSStreamActionAdvisoryType = "io.nats.jetstream.advisory.v1.stream_action"
    71  
    72  // JSConsumerActionAdvisory indicates that a consumer was created or deleted
    73  type JSConsumerActionAdvisory struct {
    74  	TypedEvent
    75  	Stream   string             `json:"stream"`
    76  	Consumer string             `json:"consumer"`
    77  	Action   ActionAdvisoryType `json:"action"`
    78  	Domain   string             `json:"domain,omitempty"`
    79  }
    80  
    81  const JSConsumerActionAdvisoryType = "io.nats.jetstream.advisory.v1.consumer_action"
    82  
    83  // JSConsumerPauseAdvisory indicates that a consumer was paused or unpaused
    84  type JSConsumerPauseAdvisory struct {
    85  	TypedEvent
    86  	Stream     string    `json:"stream"`
    87  	Consumer   string    `json:"consumer"`
    88  	Paused     bool      `json:"paused"`
    89  	PauseUntil time.Time `json:"pause_until,omitempty"`
    90  	Domain     string    `json:"domain,omitempty"`
    91  }
    92  
    93  const JSConsumerPauseAdvisoryType = "io.nats.jetstream.advisory.v1.consumer_pause"
    94  
    95  // JSConsumerAckMetric is a metric published when a user acknowledges a message, the
    96  // number of these that will be published is dependent on SampleFrequency
    97  type JSConsumerAckMetric struct {
    98  	TypedEvent
    99  	Stream      string `json:"stream"`
   100  	Consumer    string `json:"consumer"`
   101  	ConsumerSeq uint64 `json:"consumer_seq"`
   102  	StreamSeq   uint64 `json:"stream_seq"`
   103  	Delay       int64  `json:"ack_time"`
   104  	Deliveries  uint64 `json:"deliveries"`
   105  	Domain      string `json:"domain,omitempty"`
   106  }
   107  
   108  // JSConsumerAckMetricType is the schema type for JSConsumerAckMetricType
   109  const JSConsumerAckMetricType = "io.nats.jetstream.metric.v1.consumer_ack"
   110  
   111  // JSConsumerDeliveryExceededAdvisory is an advisory informing that a message hit
   112  // its MaxDeliver threshold and so might be a candidate for DLQ handling
   113  type JSConsumerDeliveryExceededAdvisory struct {
   114  	TypedEvent
   115  	Stream     string `json:"stream"`
   116  	Consumer   string `json:"consumer"`
   117  	StreamSeq  uint64 `json:"stream_seq"`
   118  	Deliveries uint64 `json:"deliveries"`
   119  	Domain     string `json:"domain,omitempty"`
   120  }
   121  
   122  // JSConsumerDeliveryExceededAdvisoryType is the schema type for JSConsumerDeliveryExceededAdvisory
   123  const JSConsumerDeliveryExceededAdvisoryType = "io.nats.jetstream.advisory.v1.max_deliver"
   124  
   125  // JSConsumerDeliveryNakAdvisory is an advisory informing that a message was
   126  // naked by the consumer
   127  type JSConsumerDeliveryNakAdvisory struct {
   128  	TypedEvent
   129  	Stream      string `json:"stream"`
   130  	Consumer    string `json:"consumer"`
   131  	ConsumerSeq uint64 `json:"consumer_seq"`
   132  	StreamSeq   uint64 `json:"stream_seq"`
   133  	Deliveries  uint64 `json:"deliveries"`
   134  	Domain      string `json:"domain,omitempty"`
   135  }
   136  
   137  // JSConsumerDeliveryNakAdvisoryType is the schema type for JSConsumerDeliveryNakAdvisory
   138  const JSConsumerDeliveryNakAdvisoryType = "io.nats.jetstream.advisory.v1.nak"
   139  
   140  // JSConsumerDeliveryTerminatedAdvisory is an advisory informing that a message was
   141  // terminated by the consumer, so might be a candidate for DLQ handling
   142  type JSConsumerDeliveryTerminatedAdvisory struct {
   143  	TypedEvent
   144  	Stream      string `json:"stream"`
   145  	Consumer    string `json:"consumer"`
   146  	ConsumerSeq uint64 `json:"consumer_seq"`
   147  	StreamSeq   uint64 `json:"stream_seq"`
   148  	Deliveries  uint64 `json:"deliveries"`
   149  	Reason      string `json:"reason,omitempty"`
   150  	Domain      string `json:"domain,omitempty"`
   151  }
   152  
   153  // JSConsumerDeliveryTerminatedAdvisoryType is the schema type for JSConsumerDeliveryTerminatedAdvisory
   154  const JSConsumerDeliveryTerminatedAdvisoryType = "io.nats.jetstream.advisory.v1.terminated"
   155  
   156  // JSSnapshotCreateAdvisory is an advisory sent after a snapshot is successfully started
   157  type JSSnapshotCreateAdvisory struct {
   158  	TypedEvent
   159  	Stream string      `json:"stream"`
   160  	State  StreamState `json:"state"`
   161  	Client *ClientInfo `json:"client"`
   162  	Domain string      `json:"domain,omitempty"`
   163  }
   164  
   165  // JSSnapshotCreatedAdvisoryType is the schema type for JSSnapshotCreateAdvisory
   166  const JSSnapshotCreatedAdvisoryType = "io.nats.jetstream.advisory.v1.snapshot_create"
   167  
   168  // JSSnapshotCompleteAdvisory is an advisory sent after a snapshot is successfully started
   169  type JSSnapshotCompleteAdvisory struct {
   170  	TypedEvent
   171  	Stream string      `json:"stream"`
   172  	Start  time.Time   `json:"start"`
   173  	End    time.Time   `json:"end"`
   174  	Client *ClientInfo `json:"client"`
   175  	Domain string      `json:"domain,omitempty"`
   176  }
   177  
   178  // JSSnapshotCompleteAdvisoryType is the schema type for JSSnapshotCreateAdvisory
   179  const JSSnapshotCompleteAdvisoryType = "io.nats.jetstream.advisory.v1.snapshot_complete"
   180  
   181  // JSRestoreCreateAdvisory is an advisory sent after a snapshot is successfully started
   182  type JSRestoreCreateAdvisory struct {
   183  	TypedEvent
   184  	Stream string      `json:"stream"`
   185  	Client *ClientInfo `json:"client"`
   186  	Domain string      `json:"domain,omitempty"`
   187  }
   188  
   189  // JSRestoreCreateAdvisoryType is the schema type for JSSnapshotCreateAdvisory
   190  const JSRestoreCreateAdvisoryType = "io.nats.jetstream.advisory.v1.restore_create"
   191  
   192  // JSRestoreCompleteAdvisory is an advisory sent after a snapshot is successfully started
   193  type JSRestoreCompleteAdvisory struct {
   194  	TypedEvent
   195  	Stream string      `json:"stream"`
   196  	Start  time.Time   `json:"start"`
   197  	End    time.Time   `json:"end"`
   198  	Bytes  int64       `json:"bytes"`
   199  	Client *ClientInfo `json:"client"`
   200  	Domain string      `json:"domain,omitempty"`
   201  }
   202  
   203  // JSRestoreCompleteAdvisoryType is the schema type for JSSnapshotCreateAdvisory
   204  const JSRestoreCompleteAdvisoryType = "io.nats.jetstream.advisory.v1.restore_complete"
   205  
   206  // Clustering specific.
   207  
   208  // JSClusterLeaderElectedAdvisoryType is sent when the system elects a new meta leader.
   209  const JSDomainLeaderElectedAdvisoryType = "io.nats.jetstream.advisory.v1.domain_leader_elected"
   210  
   211  // JSClusterLeaderElectedAdvisory indicates that a domain has elected a new leader.
   212  type JSDomainLeaderElectedAdvisory struct {
   213  	TypedEvent
   214  	Leader   string      `json:"leader"`
   215  	Replicas []*PeerInfo `json:"replicas"`
   216  	Cluster  string      `json:"cluster"`
   217  	Domain   string      `json:"domain,omitempty"`
   218  }
   219  
   220  // JSStreamLeaderElectedAdvisoryType is sent when the system elects a new leader for a stream.
   221  const JSStreamLeaderElectedAdvisoryType = "io.nats.jetstream.advisory.v1.stream_leader_elected"
   222  
   223  // JSStreamLeaderElectedAdvisory indicates that a stream has elected a new leader.
   224  type JSStreamLeaderElectedAdvisory struct {
   225  	TypedEvent
   226  	Account  string      `json:"account,omitempty"`
   227  	Stream   string      `json:"stream"`
   228  	Leader   string      `json:"leader"`
   229  	Replicas []*PeerInfo `json:"replicas"`
   230  	Domain   string      `json:"domain,omitempty"`
   231  }
   232  
   233  // JSStreamQuorumLostAdvisoryType is sent when the system detects a clustered stream and
   234  // its consumers are stalled and unable to make progress.
   235  const JSStreamQuorumLostAdvisoryType = "io.nats.jetstream.advisory.v1.stream_quorum_lost"
   236  
   237  // JSStreamQuorumLostAdvisory indicates that a stream has lost quorum and is stalled.
   238  type JSStreamQuorumLostAdvisory struct {
   239  	TypedEvent
   240  	Account  string      `json:"account,omitempty"`
   241  	Stream   string      `json:"stream"`
   242  	Replicas []*PeerInfo `json:"replicas"`
   243  	Domain   string      `json:"domain,omitempty"`
   244  }
   245  
   246  // JSConsumerLeaderElectedAdvisoryType is sent when the system elects a leader for a consumer.
   247  const JSConsumerLeaderElectedAdvisoryType = "io.nats.jetstream.advisory.v1.consumer_leader_elected"
   248  
   249  // JSConsumerLeaderElectedAdvisory indicates that a consumer has elected a new leader.
   250  type JSConsumerLeaderElectedAdvisory struct {
   251  	TypedEvent
   252  	Account  string      `json:"account,omitempty"`
   253  	Stream   string      `json:"stream"`
   254  	Consumer string      `json:"consumer"`
   255  	Leader   string      `json:"leader"`
   256  	Replicas []*PeerInfo `json:"replicas"`
   257  	Domain   string      `json:"domain,omitempty"`
   258  }
   259  
   260  // JSConsumerQuorumLostAdvisoryType is sent when the system detects a clustered consumer and
   261  // is stalled and unable to make progress.
   262  const JSConsumerQuorumLostAdvisoryType = "io.nats.jetstream.advisory.v1.consumer_quorum_lost"
   263  
   264  // JSConsumerQuorumLostAdvisory indicates that a consumer has lost quorum and is stalled.
   265  type JSConsumerQuorumLostAdvisory struct {
   266  	TypedEvent
   267  	Account  string      `json:"account,omitempty"`
   268  	Stream   string      `json:"stream"`
   269  	Consumer string      `json:"consumer"`
   270  	Replicas []*PeerInfo `json:"replicas"`
   271  	Domain   string      `json:"domain,omitempty"`
   272  }
   273  
   274  // JSServerOutOfStorageAdvisoryType is sent when the server is out of storage space.
   275  const JSServerOutOfStorageAdvisoryType = "io.nats.jetstream.advisory.v1.server_out_of_space"
   276  
   277  // JSServerOutOfSpaceAdvisory indicates that a stream has lost quorum and is stalled.
   278  type JSServerOutOfSpaceAdvisory struct {
   279  	TypedEvent
   280  	Server   string `json:"server"`
   281  	ServerID string `json:"server_id"`
   282  	Stream   string `json:"stream,omitempty"`
   283  	Cluster  string `json:"cluster"`
   284  	Domain   string `json:"domain,omitempty"`
   285  }
   286  
   287  // JSServerRemovedAdvisoryType is sent when the server has been removed and JS disabled.
   288  const JSServerRemovedAdvisoryType = "io.nats.jetstream.advisory.v1.server_removed"
   289  
   290  // JSServerRemovedAdvisory indicates that a stream has lost quorum and is stalled.
   291  type JSServerRemovedAdvisory struct {
   292  	TypedEvent
   293  	Server   string `json:"server"`
   294  	ServerID string `json:"server_id"`
   295  	Cluster  string `json:"cluster"`
   296  	Domain   string `json:"domain,omitempty"`
   297  }