github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/msg/producer/writer/message.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package writer 22 23 import ( 24 "github.com/m3db/m3/src/msg/generated/proto/msgpb" 25 "github.com/m3db/m3/src/msg/producer" 26 "github.com/m3db/m3/src/msg/protocol/proto" 27 28 "go.uber.org/atomic" 29 ) 30 31 type message struct { 32 *producer.RefCountedMessage 33 34 pb msgpb.Message 35 meta metadata 36 initNanos int64 37 retryAtNanos int64 38 // updated by the writing goroutine and read by the acking goroutine. 39 expectedProcessAtNanos atomic.Int64 40 retried int 41 // NB(cw) isAcked could be accessed concurrently by the background thread 42 // in message writer and acked by consumer service writers. 43 // Safe to store value inside struct, as message is never copied by value 44 isAcked atomic.Bool 45 } 46 47 func newMessage() *message { 48 return &message{} 49 } 50 51 // Set sets the message. 52 func (m *message) Set(meta metadata, rm *producer.RefCountedMessage, initNanos int64) { 53 m.initNanos = initNanos 54 m.meta = meta 55 m.RefCountedMessage = rm 56 m.ToProto(&m.pb) 57 } 58 59 // Close resets the states of the message. 60 func (m *message) Close() { 61 m.retryAtNanos = 0 62 m.retried = 0 63 m.isAcked.Store(false) 64 m.ResetProto(&m.pb) 65 } 66 67 // InitNanos returns the nanosecond when the message was initiated. 68 func (m *message) InitNanos() int64 { 69 return m.initNanos 70 } 71 72 // ExpectedProcessAtNanos returns the nanosecond when the message should be processed. Used to calculate processing lag 73 // in the system. 74 func (m *message) ExpectedProcessAtNanos() int64 { 75 return m.expectedProcessAtNanos.Load() 76 } 77 78 // RetryAtNanos returns the timestamp for next retry in nano seconds. 79 func (m *message) RetryAtNanos() int64 { 80 return m.retryAtNanos 81 } 82 83 // SetRetryAtNanos sets the next retry nanos. 84 func (m *message) SetRetryAtNanos(value int64) { 85 if m.retryAtNanos > 0 { 86 m.expectedProcessAtNanos.Store(m.retryAtNanos) 87 } else { 88 m.expectedProcessAtNanos.Store(m.initNanos) 89 } 90 m.retryAtNanos = value 91 } 92 93 // WriteTimes returns the times the message has been written. 94 func (m *message) WriteTimes() int { 95 return m.retried 96 } 97 98 // IncWriteTimes increments the times the message has been written. 99 func (m *message) IncWriteTimes() { 100 m.retried++ 101 } 102 103 // IsAcked returns true if the message has been acked. 104 func (m *message) IsAcked() bool { 105 return m.isAcked.Load() 106 } 107 108 // Ack acknowledges the message. Duplicated acks on the same message might cause panic. 109 func (m *message) Ack() { 110 m.isAcked.Store(true) 111 m.RefCountedMessage.DecRef() 112 } 113 114 func (m *message) ShardID() uint64 { 115 return m.meta.shard 116 } 117 118 // Metadata returns the metadata. 119 func (m *message) Metadata() metadata { 120 return m.meta 121 } 122 123 // SetSentAt sets the sentAtNanos on the metadata proto. 124 func (m *message) SetSentAt(nanos int64) { 125 m.pb.Metadata.SentAtNanos = uint64(nanos) 126 } 127 128 // Marshaler returns the marshaler and a bool to indicate whether the marshaler is valid. 129 func (m *message) Marshaler() (proto.Marshaler, bool) { 130 return &m.pb, !m.RefCountedMessage.IsDroppedOrConsumed() 131 } 132 133 func (m *message) ToProto(pb *msgpb.Message) { 134 m.meta.ToProto(&pb.Metadata) 135 pb.Value = m.RefCountedMessage.Bytes() 136 } 137 138 func (m *message) ResetProto(pb *msgpb.Message) { 139 pb.Value = nil 140 }