-
Notifications
You must be signed in to change notification settings - Fork 6
/
replica.go
51 lines (45 loc) · 1.29 KB
/
replica.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
package varlogpb
import (
"github.com/pkg/errors"
"github.com/kakao/varlog/pkg/util/container/set"
"github.com/kakao/varlog/pkg/verrors"
)
func EqualReplicas(xs []LogStreamReplica, ys []LogStreamReplica) bool {
if len(xs) != len(ys) {
return false
}
for idx := range xs {
x := &xs[idx]
y := &ys[idx]
// NOTE: To skip comparison of address, Equal method is not used.
/*
if !x.Equal(y) {
return false
}
*/
if x.StorageNode.StorageNodeID != y.StorageNode.StorageNodeID || x.LogStreamID != y.LogStreamID {
return false
}
}
return true
}
// ValidReplicas checks whether given replicas are valid. Valid replicas should contain at least one
// replica, and all replicas have the same LogStreamID. They also have different StorageNodeIDs.
func ValidReplicas(replicas []LogStreamReplica) error {
if len(replicas) < 1 {
return errors.Wrap(verrors.ErrInvalid, "no replica")
}
lsidSet := set.New(len(replicas))
snidSet := set.New(len(replicas))
for _, replica := range replicas {
lsidSet.Add(replica.LogStreamID)
snidSet.Add(replica.StorageNode.StorageNodeID)
}
if lsidSet.Size() != 1 {
return errors.Wrap(verrors.ErrInvalid, "LogStreamID mismatch")
}
if snidSet.Size() != len(replicas) {
return errors.Wrap(verrors.ErrInvalid, "StorageNodeID duplicated")
}
return nil
}