forked from apache/rocketmq-clients
/
message_id_codec.go
161 lines (126 loc) · 4.79 KB
/
message_id_codec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package golang
import (
"bytes"
"crypto/rand"
"encoding/binary"
"encoding/hex"
"os"
"strings"
"sync/atomic"
"time"
uberatomic "go.uber.org/atomic"
"github.com/apache/rocketmq-clients/golang/v5/pkg/utils"
)
/**
The codec for the message-id.
Codec here provides the following two functions:
1. Provide decoding function of message-id of all versions above v0.
2. Provide a generator of message-id of v1 version.
The message-id of versions above V1 consists of 17 bytes in total. The first two bytes represent the version
number. For V1, these two bytes are 0x0001.
V1 message id example
┌──┬────────────┬────┬────────┬────────┐
│01│56F7E71C361B│21BC│024CCDBE│00000000│
└──┴────────────┴────┴────────┴────────┘
V1 version message id generation rules
process id(lower 2bytes)
▲
mac address(lower 6bytes) │ sequence number(big endian)
▲ │ ▲ (4bytes)
│ │ │
┌─────┴─────┐ ┌┴┐ ┌───┐ ┌─┴─┐
0x01+ │ 6 │ │2│ │ 4 │ │ 4 │
└───────────┘ └─┘ └─┬─┘ └───┘
│
▼
seconds since 2021-01-01 00:00:00(UTC+0)
(lower 4bytes)
*/
type MessageIdCodec interface {
NextMessageId() MessageId
Decode(messageId string) MessageId
}
var _ MessageIdCodec = &messageIdCodec{}
const (
MESSAGE_ID_LENGTH_FOR_V1_OR_LATER = 34
MESSAGE_ID_VERSION_V0 string = "00"
MESSAGE_ID_VERSION_V1 string = "01"
)
var (
messageIdCodecInstance MessageIdCodec
processFixedStringV1 string
secondsSinceCustomEpoch int64
secondsStartTimestamp int64
seconds uberatomic.Int64
sequence int32
)
func init() {
var buffer bytes.Buffer
prefix0 := utils.GetMacAddress()
if len(prefix0) < 6 {
prefix0 = make([]byte, 6)
_, err := rand.Read(prefix0)
if err != nil {
sugarBaseLogger.Fatalf("failed to get mac address: %v", err)
}
}
if err := binary.Write(&buffer, binary.BigEndian, prefix0); err != nil {
sugarBaseLogger.Fatalf("failed to write buffer: %v", err)
}
pid := os.Getpid()
if err := binary.Write(&buffer, binary.BigEndian, uint16(pid)); err != nil {
sugarBaseLogger.Fatalf("failed to write pid: %v", err)
}
encodedStr := hex.EncodeToString(buffer.Bytes())
processFixedStringV1 = strings.ToUpper(encodedStr)
secondsSinceCustomEpoch = time.Now().Unix() - time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
// TODO Implement System.nanoTime() in golang, see https://github.com/golang/go/issues/16658
secondsStartTimestamp = time.Now().Unix()
seconds.Store(deltaSeconds())
sequence = -1
messageIdCodecInstance = &messageIdCodec{}
}
func GetMessageIdCodecInstance() MessageIdCodec {
return messageIdCodecInstance
}
type messageIdCodec struct{}
func deltaSeconds() int64 {
return time.Now().Unix() - secondsStartTimestamp + secondsSinceCustomEpoch
}
func (mic *messageIdCodec) NextMessageId() MessageId {
var buffer bytes.Buffer
deltaSeconds := deltaSeconds()
if seconds.Load() != deltaSeconds {
seconds.Store(deltaSeconds)
}
if err := binary.Write(&buffer, binary.BigEndian, uint32(deltaSeconds)); err != nil {
return nil
}
if err := binary.Write(&buffer, binary.BigEndian, uint32(atomic.AddInt32(&sequence, 1))); err != nil {
return nil
}
suffix := processFixedStringV1 + strings.ToUpper(hex.EncodeToString(buffer.Bytes()))
return NewMessageId(MESSAGE_ID_VERSION_V1, suffix)
}
func (mic *messageIdCodec) Decode(messageId string) MessageId {
if MESSAGE_ID_LENGTH_FOR_V1_OR_LATER != len(messageId) {
return NewMessageId(MESSAGE_ID_VERSION_V0, messageId)
}
return NewMessageId(messageId[0:2], messageId[2:])
}