github.com/m3db/m3@v1.5.0/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 retryAtNanos: 0, 50 retried: 0, 51 } 52 } 53 54 // Set sets the message. 55 func (m *message) Set(meta metadata, rm *producer.RefCountedMessage, initNanos int64) { 56 m.initNanos = initNanos 57 m.meta = meta 58 m.RefCountedMessage = rm 59 m.ToProto(&m.pb) 60 } 61 62 // Close resets the states of the message. 63 func (m *message) Close() { 64 m.retryAtNanos = 0 65 m.retried = 0 66 m.isAcked.Store(false) 67 m.ResetProto(&m.pb) 68 } 69 70 // InitNanos returns the nanosecond when the message was initiated. 71 func (m *message) InitNanos() int64 { 72 return m.initNanos 73 } 74 75 // ExpectedProcessAtNanos returns the nanosecond when the message should be processed. Used to calculate processing lag 76 // in the system. 77 func (m *message) ExpectedProcessAtNanos() int64 { 78 return m.expectedProcessAtNanos.Load() 79 } 80 81 // RetryAtNanos returns the timestamp for next retry in nano seconds. 82 func (m *message) RetryAtNanos() int64 { 83 return m.retryAtNanos 84 } 85 86 // SetRetryAtNanos sets the next retry nanos. 87 func (m *message) SetRetryAtNanos(value int64) { 88 if m.retryAtNanos > 0 { 89 m.expectedProcessAtNanos.Store(m.retryAtNanos) 90 } else { 91 m.expectedProcessAtNanos.Store(m.initNanos) 92 } 93 m.retryAtNanos = value 94 } 95 96 // WriteTimes returns the times the message has been written. 97 func (m *message) WriteTimes() int { 98 return m.retried 99 } 100 101 // IncWriteTimes increments the times the message has been written. 102 func (m *message) IncWriteTimes() { 103 m.retried++ 104 } 105 106 // IsAcked returns true if the message has been acked. 107 func (m *message) IsAcked() bool { 108 return m.isAcked.Load() 109 } 110 111 // Ack acknowledges the message. Duplicated acks on the same message might cause panic. 112 func (m *message) Ack() { 113 m.isAcked.Store(true) 114 m.RefCountedMessage.DecRef() 115 } 116 117 func (m *message) ShardID() uint64 { 118 return m.meta.shard 119 } 120 121 // Metadata returns the metadata. 122 func (m *message) Metadata() metadata { 123 return m.meta 124 } 125 126 // SetSentAt sets the sentAtNanos on the metadata proto. 127 func (m *message) SetSentAt(nanos int64) { 128 m.pb.Metadata.SentAtNanos = uint64(nanos) 129 } 130 131 // Marshaler returns the marshaler and a bool to indicate whether the marshaler is valid. 132 func (m *message) Marshaler() (proto.Marshaler, bool) { 133 return &m.pb, !m.RefCountedMessage.IsDroppedOrConsumed() 134 } 135 136 func (m *message) ToProto(pb *msgpb.Message) { 137 m.meta.ToProto(&pb.Metadata) 138 pb.Value = m.RefCountedMessage.Bytes() 139 } 140 141 func (m *message) ResetProto(pb *msgpb.Message) { 142 pb.Value = nil 143 }