generated from B1NARY-GR0UP/.github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
123 lines (104 loc) · 2.97 KB
/
utils.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
// Copyright 2024 BINARY Members
//
// Licensed 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 raft
import (
"crypto/rand"
"math/big"
"sync"
rt "github.com/B1NARY-GR0UP/raft/raftthrift"
"github.com/apache/thrift/lib/go/thrift"
"github.com/cloudwego/frugal"
)
func IsEmptySnapshot(snapshot rt.Snapshot) bool {
return snapshot.Metadata == nil || snapshot.Metadata.LastIndex == 0
}
var clientMessage = map[rt.MessageType]struct{}{
rt.MessageType_Propose: {},
}
func isClientMessage(t rt.MessageType) bool {
_, ok := clientMessage[t]
return ok
}
// internal messages are different from local messages
// internal messages should only be used inside node itself
// local messages are messages which dst is node itself
var internalMessage = map[rt.MessageType]struct{}{
rt.MessageType_Campaign: {},
rt.MessageType_Beat: {},
rt.MessageType_StorageAppend: {},
rt.MessageType_StorageApply: {},
}
func isInternalMessage(t rt.MessageType) bool {
_, ok := internalMessage[t]
return ok
}
var rpcReqMessage = map[rt.MessageType]struct{}{
rt.MessageType_AppendEntries: {},
rt.MessageType_RequestVote: {},
rt.MessageType_InstallSnapshot: {},
}
func isRPCReqMessage(t rt.MessageType) bool {
_, ok := rpcReqMessage[t]
return ok
}
var rpcRespMessage = map[rt.MessageType]struct{}{
rt.MessageType_AppendEntriesResp: {},
rt.MessageType_RequestVoteResp: {},
rt.MessageType_InstallSnapshotResp: {},
}
func isRPCRespMessage(t rt.MessageType) bool {
_, ok := rpcRespMessage[t]
return ok
}
var globalRand = &syncRand{}
type syncRand struct {
mu sync.Mutex
}
// Intn return a random value in [0, n)
func (r *syncRand) Intn(n int) int {
r.mu.Lock()
// higher security and wider range compare to rand.Intn
v, _ := rand.Int(rand.Reader, big.NewInt(int64(n)))
r.mu.Unlock()
return int(v.Int64())
}
func EntryValues(entries []*rt.Entry) []rt.Entry {
res := make([]rt.Entry, len(entries))
for i, entry := range entries {
if entry != nil {
res[i] = *entry
}
}
return res
}
func EntryPointers(entries []rt.Entry) []*rt.Entry {
res := make([]*rt.Entry, len(entries))
for i, entry := range entries {
res[i] = &entry
}
return res
}
func TMarshal(data thrift.TStruct) ([]byte, error) {
buf := make([]byte, frugal.EncodedSize(data))
if _, err := frugal.EncodeObject(buf, nil, data); err != nil {
return nil, err
}
return buf, nil
}
func TUnmarshal(data []byte, v thrift.TStruct) error {
if _, err := frugal.DecodeObject(data, v); err != nil {
return err
}
return nil
}