github.com/matrixorigin/matrixone@v1.2.0/pkg/gossip/delegate_test.go (about) 1 // Copyright 2021 - 2023 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package gossip 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/matrixorigin/matrixone/pkg/pb/gossip" 22 "github.com/matrixorigin/matrixone/pkg/pb/query" 23 "github.com/matrixorigin/matrixone/pkg/pb/statsinfo" 24 "github.com/stretchr/testify/assert" 25 "go.uber.org/zap" 26 ) 27 28 func TestDelegate_NodeMeta(t *testing.T) { 29 d := newDelegate(&zap.Logger{}, "127.0.0.1:8889") 30 m := d.NodeMeta(100) 31 assert.NotNil(t, m) 32 assert.Equal(t, "127.0.0.1:8889", string(m)) 33 } 34 35 func TestDelegate_DataCache_GetBroadcastsAndNotify(t *testing.T) { 36 d := newDelegate(&zap.Logger{}, "127.0.0.1:8889") 37 data := d.GetBroadcasts(4, 32*1024) 38 assert.Equal(t, 0, len(data)) 39 40 for i := 0; i < 10; i++ { 41 ck := query.CacheKey{ 42 Path: fmt.Sprintf("p%d", i), 43 Offset: int64(10 * i), 44 Sz: int64(10 * i), 45 } 46 d.getDataCacheKey().AddItem(gossip.CommonItem{ 47 Operation: gossip.Operation_Set, 48 Key: &gossip.CommonItem_CacheKey{ 49 CacheKey: &ck, 50 }, 51 }) 52 assert.Equal(t, i+1, len(d.dataCacheKey.queueMu.itemQueue)) 53 } 54 55 data = d.GetBroadcasts(4, 32*1024) 56 assert.NotNil(t, data) 57 58 t.Run("self", func(t *testing.T) { 59 for _, single := range data { 60 d.NotifyMsg(single) 61 } 62 assert.Equal(t, 10, len(d.dataCacheKey.mu.keyTarget)) 63 for i := 0; i < 15; i++ { 64 target := d.getDataCacheKey().Target(query.CacheKey{ 65 Path: fmt.Sprintf("p%d", i), 66 Offset: int64(10 * i), 67 Sz: int64(10 * i), 68 }) 69 assert.Equal(t, "", target) 70 } 71 }) 72 73 t.Run("other", func(t *testing.T) { 74 d1 := newDelegate(&zap.Logger{}, "127.0.0.1:7779") 75 for _, single := range data { 76 d1.NotifyMsg(single) 77 } 78 assert.Equal(t, 10, len(d1.dataCacheKey.mu.keyTarget)) 79 for i := 0; i < 15; i++ { 80 target := d1.getDataCacheKey().Target(query.CacheKey{ 81 Path: fmt.Sprintf("p%d", i), 82 Offset: int64(10 * i), 83 Sz: int64(10 * i), 84 }) 85 if i < 10 { 86 assert.Equal(t, "127.0.0.1:8889", target) 87 } else { 88 assert.Equal(t, "", target) 89 } 90 } 91 }) 92 } 93 94 func TestDelegate_StatsInfo_GetBroadcastsAndNotify(t *testing.T) { 95 d := newDelegate(&zap.Logger{}, "127.0.0.1:8889") 96 data := d.GetBroadcasts(4, 32*1024) 97 assert.Equal(t, 0, len(data)) 98 99 var i uint64 100 for i = 0; i < 10; i++ { 101 ck := statsinfo.StatsInfoKey{ 102 DatabaseID: i * 2, 103 TableID: i * 3, 104 } 105 d.getStatsInfoKey().AddItem(gossip.CommonItem{ 106 Operation: gossip.Operation_Set, 107 Key: &gossip.CommonItem_StatsInfoKey{ 108 StatsInfoKey: &ck, 109 }, 110 }) 111 assert.Equal(t, int(i+1), len(d.statsInfoKey.queueMu.itemQueue)) 112 } 113 114 data = d.GetBroadcasts(4, 32*1024) 115 assert.NotNil(t, data) 116 117 t.Run("self", func(t *testing.T) { 118 for _, single := range data { 119 d.NotifyMsg(single) 120 } 121 assert.Equal(t, 10, len(d.statsInfoKey.mu.keyTarget)) 122 var i uint64 123 for i = 0; i < 15; i++ { 124 target := d.getStatsInfoKey().Target(statsinfo.StatsInfoKey{ 125 DatabaseID: i * 2, 126 TableID: i * 3, 127 }) 128 assert.Equal(t, "", target) 129 } 130 }) 131 132 t.Run("other", func(t *testing.T) { 133 d1 := newDelegate(&zap.Logger{}, "127.0.0.1:7779") 134 for _, single := range data { 135 d1.NotifyMsg(single) 136 } 137 assert.Equal(t, 10, len(d1.statsInfoKey.mu.keyTarget)) 138 var i uint64 139 for i = 0; i < 15; i++ { 140 target := d1.getStatsInfoKey().Target(statsinfo.StatsInfoKey{ 141 DatabaseID: i * 2, 142 TableID: i * 3, 143 }) 144 if i < 10 { 145 assert.Equal(t, "127.0.0.1:8889", target) 146 } else { 147 assert.Equal(t, "", target) 148 } 149 } 150 }) 151 } 152 153 func TestDelegate_DataCache_LocalStateAndMergeRemoteState(t *testing.T) { 154 l, err := zap.NewDevelopment() 155 assert.NoError(t, err) 156 d1 := newDelegate(l, "127.0.0.1:8888") 157 d2 := newDelegate(l, "127.0.0.1:8889") 158 159 for i := 0; i < 10; i++ { 160 ck := query.CacheKey{ 161 Path: fmt.Sprintf("p%d", i), 162 Offset: int64(10 * i), 163 Sz: int64(10 * i), 164 } 165 d1.getDataCacheKey().AddItem(gossip.CommonItem{ 166 Operation: gossip.Operation_Set, 167 Key: &gossip.CommonItem_CacheKey{ 168 CacheKey: &ck, 169 }, 170 }) 171 assert.Equal(t, i+1, len(d1.dataCacheKey.queueMu.itemQueue)) 172 } 173 174 data := d1.GetBroadcasts(4, 32*1024) 175 assert.NotNil(t, data) 176 177 for _, single := range data { 178 d2.NotifyMsg(single) 179 } 180 assert.Equal(t, 10, len(d2.dataCacheKey.mu.keyTarget)) 181 182 buf := d2.LocalState(false) 183 d2.MergeRemoteState(buf, true) 184 for i := 0; i < 15; i++ { 185 target := d2.getDataCacheKey().Target(query.CacheKey{ 186 Path: fmt.Sprintf("p%d", i), 187 Offset: int64(10 * i), 188 Sz: int64(10 * i), 189 }) 190 if i < 10 { 191 assert.Equal(t, "127.0.0.1:8888", target) 192 } else { 193 assert.Equal(t, "", target) 194 } 195 } 196 } 197 198 func TestDelegate_StatsInfo_LocalStateAndMergeRemoteState(t *testing.T) { 199 l, err := zap.NewDevelopment() 200 assert.NoError(t, err) 201 d1 := newDelegate(l, "127.0.0.1:8888") 202 d2 := newDelegate(l, "127.0.0.1:8889") 203 204 for i := 0; i < 10; i++ { 205 si := statsinfo.StatsInfoKey{ 206 DatabaseID: uint64(i), 207 TableID: uint64(10 * i), 208 } 209 d1.getStatsInfoKey().AddItem(gossip.CommonItem{ 210 Operation: gossip.Operation_Set, 211 Key: &gossip.CommonItem_StatsInfoKey{ 212 StatsInfoKey: &si, 213 }, 214 }) 215 assert.Equal(t, i+1, len(d1.statsInfoKey.queueMu.itemQueue)) 216 } 217 218 data := d1.GetBroadcasts(4, 32*1024) 219 assert.NotNil(t, data) 220 221 for _, single := range data { 222 d2.NotifyMsg(single) 223 } 224 assert.Equal(t, 10, len(d2.statsInfoKey.mu.keyTarget)) 225 226 buf := d2.LocalState(false) 227 d2.MergeRemoteState(buf, true) 228 for i := 0; i < 15; i++ { 229 target := d2.getStatsInfoKey().Target(statsinfo.StatsInfoKey{ 230 DatabaseID: uint64(i), 231 TableID: uint64(10 * i), 232 }) 233 if i < 10 { 234 assert.Equal(t, "127.0.0.1:8888", target) 235 } else { 236 assert.Equal(t, "", target) 237 } 238 } 239 }