-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
util.go
64 lines (53 loc) · 1.41 KB
/
util.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
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation and Dapr Contributors.
// Licensed under the MIT License.
// ------------------------------------------------------------
package raft
import (
"bytes"
"os"
"github.com/hashicorp/go-msgpack/codec"
"github.com/pkg/errors"
)
const defaultDirPermission = 0755
func ensureDir(dirName string) error {
info, err := os.Stat(dirName)
if !os.IsNotExist(err) && !info.Mode().IsDir() {
return errors.New("file already existed")
}
err = os.Mkdir(dirName, defaultDirPermission)
if err == nil || os.IsExist(err) {
return nil
}
return err
}
func makeRaftLogCommand(t CommandType, member DaprHostMember) ([]byte, error) {
buf := bytes.NewBuffer(nil)
buf.WriteByte(uint8(t))
err := codec.NewEncoder(buf, &codec.MsgpackHandle{}).Encode(member)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func marshalMsgPack(in interface{}) ([]byte, error) {
buf := bytes.NewBuffer(nil)
enc := codec.NewEncoder(buf, &codec.MsgpackHandle{})
err := enc.Encode(in)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func unmarshalMsgPack(in []byte, out interface{}) error {
dec := codec.NewDecoderBytes(in, &codec.MsgpackHandle{})
return dec.Decode(out)
}
func raftAddressForID(id string, nodes []PeerInfo) string {
for _, node := range nodes {
if node.ID == id {
return node.Address
}
}
return ""
}