-
Notifications
You must be signed in to change notification settings - Fork 0
/
shard_controller_test.go
148 lines (140 loc) · 3.87 KB
/
shard_controller_test.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
package zongzi
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestShardControllerDefault(t *testing.T) {
t.Run("reconcile", func(t *testing.T) {
state := newFsmStateRadix()
testHelperFillHosts(state, 3)
state = state.withTxn(true)
state.shardPut(Shard{ID: 0, Name: "zongzi"})
state.metaSetIndex(1)
state.commit()
shard := Shard{
ID: 1,
Updated: 2,
Status: ShardStatus_New,
Type: "test",
Tags: map[string]string{},
}
t.Run("members", func(t *testing.T) {
state = state.withTxn(true)
defer state.commit()
require.Nil(t, WithName("test-1")(&shard))
require.Nil(t, WithPlacementMembers(3, "geo:region=us-west")(&shard))
state.shardPut(shard)
agent, err := NewAgent("test", nil)
require.Nil(t, err)
ctrl := newShardControllerDefault(agent)
controls := &mockControls{
mockCreate: func(hostID string, shardID uint64, isNonVoting bool) (id uint64, err error) {
i := state.Index()
id = state.replicaIncr()
state.metaSetIndex(i + 1)
state.replicaPut(Replica{
ID: id,
Updated: i,
HostID: hostID,
ShardID: shardID,
IsNonVoting: isNonVoting,
Tags: map[string]string{},
})
state.shardTouch(shardID, i)
state.hostTouch(hostID, i)
return
},
}
err = ctrl.Reconcile(state, shard, controls)
require.Nil(t, err)
assert.True(t, controls.updated)
var n int
state.ReplicaIterateByShardID(shard.ID, func(r Replica) bool {
n++
// t.Logf(`%#v`, r)
return true
})
assert.Equal(t, 3, n)
})
testHelperFillHosts(state, 3)
t.Run("replicas", func(t *testing.T) {
state = state.withTxn(true)
defer state.commit()
require.Nil(t, WithName("test-1")(&shard))
require.Nil(t, WithPlacementReplicas("group1", 3, "geo:region=us-west")(&shard))
shard.Updated = state.Index() + 1
state.shardPut(shard)
agent, err := NewAgent("test", nil)
require.Nil(t, err)
controls := &mockControls{
mockCreate: func(hostID string, shardID uint64, isNonVoting bool) (id uint64, err error) {
i := state.Index()
id = state.replicaIncr()
state.metaSetIndex(i + 1)
state.replicaPut(Replica{
ID: id,
Updated: i,
HostID: hostID,
ShardID: shardID,
IsNonVoting: isNonVoting,
Tags: map[string]string{},
})
state.shardTouch(shardID, i)
state.hostTouch(hostID, i)
return
},
}
ctrl := newShardControllerDefault(agent)
err = ctrl.Reconcile(state, shard, controls)
require.Nil(t, err)
assert.True(t, controls.updated)
var n int
state.ReplicaIterateByShardID(shard.ID, func(r Replica) bool {
n++
// t.Logf(`%#v`, r)
return true
})
assert.Equal(t, 6, n)
})
})
}
var uuidIncr uint64
func testHelperFillHosts(state *State, n uint64) {
state = state.withTxn(true)
defer state.commit()
for i := uint64(1); i <= n; i++ {
state.hostPut(Host{
ID: fmt.Sprintf("8201349e-c113-4504-8d7e-25514c3c%04x", uuidIncr+i),
Tags: map[string]string{
"geo:region": "us-west",
"geo:zone": fmt.Sprintf("us-west-%d", i),
},
})
state.replicaPut(Replica{
ID: i,
HostID: fmt.Sprintf("8201349e-c113-4504-8d7e-25514c3c%04x", uuidIncr+i),
ShardID: 0,
Tags: map[string]string{},
})
uuidIncr++
}
}
type mockControls struct {
mockCreate func(hostID string, shardID uint64, isNonVoting bool) (id uint64, err error)
mockDelete func(replicaID uint64) (err error)
updated bool
}
func (m *mockControls) Create(hostID string, shardID uint64, isNonVoting bool) (id uint64, err error) {
if id, err = m.mockCreate(hostID, shardID, isNonVoting); err == nil {
m.updated = true
}
return
}
func (m *mockControls) Delete(replicaID uint64) (err error) {
if err = m.mockDelete(replicaID); err == nil {
m.updated = true
}
return
}