| 1 |
package tntengine |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"log" |
| 6 |
"strconv" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/centrifugal/centrifuge" |
| 11 |
"github.com/centrifugal/protocol" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
) |
| 14 |
|
| 15 |
func newTestTarantoolEngine(tb testing.TB) (*Broker, *PresenceManager) { |
| 16 |
n, _ := centrifuge.New(centrifuge.DefaultConfig) |
| 17 |
var shards []*Shard |
| 18 |
for _, port := range []string{"3301"} { |
| 19 |
shard, err := NewShard(ShardConfig{Addresses: []string{"127.0.0.1:" + port}}) |
| 20 |
if err != nil { |
| 21 |
log.Fatal(err) |
| 22 |
} |
| 23 |
shards = append(shards, shard) |
| 24 |
} |
| 25 |
|
| 26 |
broker, err := NewBroker(n, BrokerConfig{ |
| 27 |
UsePolling: false, |
| 28 |
Shards: shards, |
| 29 |
}) |
| 30 |
if err != nil { |
| 31 |
tb.Fatal(err) |
| 32 |
} |
| 33 |
|
| 34 |
presenceManager, err := NewPresenceManager(n, PresenceManagerConfig{ |
| 35 |
Shards: shards, |
| 36 |
}) |
| 37 |
if err != nil { |
| 38 |
tb.Fatal(err) |
| 39 |
} |
| 40 |
|
| 41 |
n.SetBroker(broker) |
| 42 |
n.SetPresenceManager(presenceManager) |
| 43 |
err = n.Run() |
| 44 |
if err != nil { |
| 45 |
tb.Fatal(err) |
| 46 |
} |
| 47 |
return broker, presenceManager |
| 48 |
} |
| 49 |
|
| 50 |
type recoverTest struct { |
| 51 |
Name string |
| 52 |
HistorySize int |
| 53 |
HistoryLifetime int |
| 54 |
NumPublications int |
| 55 |
SinceOffset uint64 |
| 56 |
NumRecovered int |
| 57 |
Sleep int |
| 58 |
Limit int |
| 59 |
Recovered bool |
| 60 |
} |
| 61 |
|
| 62 |
var recoverTests = []recoverTest{ |
| 63 |
{"empty_stream", 10, 60, 0, 0, 0, 0, 0, true}, |
| 64 |
{"from_position", 10, 60, 10, 8, 2, 0, 0, true}, |
| 65 |
{"from_position_limited", 10, 60, 10, 5, 2, 0, 2, false}, |
| 66 |
{"from_position_with_server_limit", 10, 60, 10, 5, 1, 0, 1, false}, |
| 67 |
{"from_position_that_already_gone", 10, 60, 20, 8, 10, 0, 0, false}, |
| 68 |
{"from_position_that_not_exist_yet", 10, 60, 20, 108, 0, 0, 0, false}, |
| 69 |
{"same_position_no_pubs_expected", 10, 60, 7, 7, 0, 0, 0, true}, |
| 70 |
{"empty_position_recover_expected", 10, 60, 4, 0, 4, 0, 0, true}, |
| 71 |
{"from_position_in_expired_stream", 10, 1, 10, 8, 0, 3, 0, false}, |
| 72 |
{"from_same_position_in_expired_stream", 10, 1, 1, 1, 0, 3, 0, true}, |
| 73 |
} |
| 74 |
|
| 75 |
func TestTarantoolClientSubscribeRecover(t *testing.T) { |
| 76 |
for _, tt := range recoverTests { |
| 77 |
t.Run(tt.Name, func(t *testing.T) { |
| 78 |
testTarantoolClientSubscribeRecover(t, tt) |
| 79 |
}) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func nodeWithTarantoolBroker(tb testing.TB) *centrifuge.Node { |
| 84 |
c := centrifuge.DefaultConfig |
| 85 |
return nodeWithTarantoolBrokerWithConfig(tb, c) |
| 86 |
} |
| 87 |
|
| 88 |
func nodeWithTarantoolBrokerWithConfig(tb testing.TB, c centrifuge.Config) *centrifuge.Node { |
| 89 |
n, err := centrifuge.New(c) |
| 90 |
if err != nil { |
| 91 |
tb.Fatal(err) |
| 92 |
} |
| 93 |
e, _ := newTestTarantoolEngine(tb) |
| 94 |
n.SetBroker(e) |
| 95 |
err = n.Run() |
| 96 |
if err != nil { |
| 97 |
tb.Fatal(err) |
| 98 |
} |
| 99 |
return n |
| 100 |
} |
| 101 |
|
| 102 |
func pubToProto(pub *centrifuge.Publication) *protocol.Publication { |
| 103 |
if pub == nil { |
| 104 |
return nil |
| 105 |
} |
| 106 |
return &protocol.Publication{ |
| 107 |
Offset: pub.Offset, |
| 108 |
Data: pub.Data, |
| 109 |
Info: infoToProto(pub.Info), |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
func isRecovered(historyResult centrifuge.HistoryResult, cmdOffset uint64, cmdEpoch string) ([]*protocol.Publication, bool) { |
| 114 |
latestOffset := historyResult.Offset |
| 115 |
latestEpoch := historyResult.Epoch |
| 116 |
|
| 117 |
recoveredPubs := make([]*protocol.Publication, 0, len(historyResult.Publications)) |
| 118 |
for _, pub := range historyResult.Publications { |
| 119 |
protoPub := pubToProto(pub) |
| 120 |
recoveredPubs = append(recoveredPubs, protoPub) |
| 121 |
} |
| 122 |
|
| 123 |
nextOffset := cmdOffset + 1 |
| 124 |
var recovered bool |
| 125 |
if len(recoveredPubs) == 0 { |
| 126 |
recovered = latestOffset == cmdOffset && latestEpoch == cmdEpoch |
| 127 |
} else { |
| 128 |
recovered = recoveredPubs[0].Offset == nextOffset && |
| 129 |
recoveredPubs[len(recoveredPubs)-1].Offset == latestOffset && |
| 130 |
latestEpoch == cmdEpoch |
| 131 |
} |
| 132 |
|
| 133 |
return recoveredPubs, recovered |
| 134 |
} |
| 135 |
|
| 136 |
// recoverHistory recovers publications since StreamPosition last seen by client. |
| 137 |
func recoverHistory(node *centrifuge.Node, ch string, since centrifuge.StreamPosition, maxPublicationLimit int) (centrifuge.HistoryResult, error) { |
| 138 |
limit := centrifuge.NoLimit |
| 139 |
if maxPublicationLimit > 0 { |
| 140 |
limit = maxPublicationLimit |
| 141 |
} |
| 142 |
return node.History(ch, centrifuge.WithLimit(limit), centrifuge.Since(&since)) |
| 143 |
} |
| 144 |
|
| 145 |
func testTarantoolClientSubscribeRecover(t *testing.T, tt recoverTest) { |
| 146 |
node := nodeWithTarantoolBroker(t) |
| 147 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 148 |
|
| 149 |
channel := "test_recovery_tarantool_" + tt.Name |
| 150 |
|
| 151 |
for i := 1; i <= tt.NumPublications; i++ { |
| 152 |
_, err := node.Publish(channel, []byte(`{"n": `+strconv.Itoa(i)+`}`), centrifuge.WithHistory(tt.HistorySize, time.Duration(tt.HistoryLifetime)*time.Second)) |
| 153 |
require.NoError(t, err) |
| 154 |
} |
| 155 |
|
| 156 |
time.Sleep(time.Duration(tt.Sleep) * time.Second) |
| 157 |
|
| 158 |
res, err := node.History(channel) |
| 159 |
require.NoError(t, err) |
| 160 |
streamTop := res.StreamPosition |
| 161 |
|
| 162 |
historyResult, err := recoverHistory(node, channel, centrifuge.StreamPosition{Offset: tt.SinceOffset, Epoch: streamTop.Epoch}, tt.Limit) |
| 163 |
require.NoError(t, err) |
| 164 |
recoveredPubs, recovered := isRecovered(historyResult, tt.SinceOffset, streamTop.Epoch) |
| 165 |
require.Equal(t, tt.NumRecovered, len(recoveredPubs)) |
| 166 |
require.Equal(t, tt.Recovered, recovered) |
| 167 |
} |
| 168 |
|
| 169 |
func BenchmarkTarantoolPublish_OneChannel(b *testing.B) { |
| 170 |
broker, _ := newTestTarantoolEngine(b) |
| 171 |
rawData := []byte(`{"bench": true}`) |
| 172 |
b.ResetTimer() |
| 173 |
for i := 0; i < b.N; i++ { |
| 174 |
_, err := broker.Publish("channel", rawData, centrifuge.PublishOptions{}) |
| 175 |
if err != nil { |
| 176 |
b.Fatal(err) |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func BenchmarkTarantoolPublish_OneChannel_Parallel(b *testing.B) { |
| 182 |
broker, _ := newTestTarantoolEngine(b) |
| 183 |
rawData := []byte(`{"bench": true}`) |
| 184 |
b.SetParallelism(128) |
| 185 |
b.ResetTimer() |
| 186 |
b.RunParallel(func(pb *testing.PB) { |
| 187 |
for pb.Next() { |
| 188 |
_, err := broker.Publish("channel", rawData, centrifuge.PublishOptions{}) |
| 189 |
if err != nil { |
| 190 |
b.Fatal(err) |
| 191 |
} |
| 192 |
} |
| 193 |
}) |
| 194 |
} |
| 195 |
|
| 196 |
func BenchmarkTarantoolSubscribe(b *testing.B) { |
| 197 |
broker, _ := newTestTarantoolEngine(b) |
| 198 |
j := 0 |
| 199 |
b.ResetTimer() |
| 200 |
for i := 0; i < b.N; i++ { |
| 201 |
j++ |
| 202 |
err := broker.Subscribe("subscribe" + strconv.Itoa(j)) |
| 203 |
if err != nil { |
| 204 |
b.Fatal(err) |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
func BenchmarkTarantoolSubscribe_Parallel(b *testing.B) { |
| 210 |
broker, _ := newTestTarantoolEngine(b) |
| 211 |
i := 0 |
| 212 |
b.SetParallelism(128) |
| 213 |
b.ResetTimer() |
| 214 |
b.RunParallel(func(pb *testing.PB) { |
| 215 |
for pb.Next() { |
| 216 |
i++ |
| 217 |
err := broker.Subscribe("subscribe" + strconv.Itoa(i)) |
| 218 |
if err != nil { |
| 219 |
b.Fatal(err) |
| 220 |
} |
| 221 |
} |
| 222 |
}) |
| 223 |
} |
| 224 |
|
| 225 |
func BenchmarkTarantoolRecover_OneChannel_Parallel(b *testing.B) { |
| 226 |
broker, _ := newTestTarantoolEngine(b) |
| 227 |
rawData := []byte("{}") |
| 228 |
numMessages := 1000 |
| 229 |
numMissing := 5 |
| 230 |
for i := 1; i <= numMessages; i++ { |
| 231 |
_, err := broker.Publish("channel", rawData, centrifuge.PublishOptions{HistorySize: numMessages, HistoryTTL: 300 * time.Second}) |
| 232 |
require.NoError(b, err) |
| 233 |
} |
| 234 |
_, sp, err := broker.History("channel", centrifuge.HistoryFilter{}) |
| 235 |
require.NoError(b, err) |
| 236 |
b.ResetTimer() |
| 237 |
b.SetParallelism(128) |
| 238 |
b.RunParallel(func(pb *testing.PB) { |
| 239 |
for pb.Next() { |
| 240 |
pubs, _, err := broker.History("channel", centrifuge.HistoryFilter{ |
| 241 |
Limit: -1, |
| 242 |
Since: ¢rifuge.StreamPosition{Offset: sp.Offset - uint64(numMissing), Epoch: ""}, |
| 243 |
}) |
| 244 |
if err != nil { |
| 245 |
b.Fatal(err) |
| 246 |
} |
| 247 |
if len(pubs) != numMissing { |
| 248 |
b.Fatalf("len pubs: %d, expected: %d", len(pubs), numMissing) |
| 249 |
} |
| 250 |
} |
| 251 |
}) |
| 252 |
} |
| 253 |
|
| 254 |
func BenchmarkTarantoolPresence_OneChannel(b *testing.B) { |
| 255 |
_, pm := newTestTarantoolEngine(b) |
| 256 |
_ = pm.AddPresence("channel", "uid", ¢rifuge.ClientInfo{}) |
| 257 |
b.ResetTimer() |
| 258 |
for i := 0; i < b.N; i++ { |
| 259 |
p, err := pm.Presence("channel") |
| 260 |
if err != nil { |
| 261 |
b.Fatal(err) |
| 262 |
} |
| 263 |
if len(p) != 1 { |
| 264 |
b.Fatal("wrong presence len") |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
func BenchmarkTarantoolPresence_OneChannel_Parallel(b *testing.B) { |
| 270 |
_, pm := newTestTarantoolEngine(b) |
| 271 |
b.SetParallelism(128) |
| 272 |
_ = pm.AddPresence("channel", "uid", ¢rifuge.ClientInfo{}) |
| 273 |
b.ResetTimer() |
| 274 |
b.RunParallel(func(pb *testing.PB) { |
| 275 |
for pb.Next() { |
| 276 |
p, err := pm.Presence("channel") |
| 277 |
if err != nil { |
| 278 |
b.Fatal(err) |
| 279 |
} |
| 280 |
if len(p) != 1 { |
| 281 |
b.Fatal("wrong presence len") |
| 282 |
} |
| 283 |
} |
| 284 |
}) |
| 285 |
} |
| 286 |
|
| 287 |
func BenchmarkTarantoolAddPresence_OneChannel(b *testing.B) { |
| 288 |
_, pm := newTestTarantoolEngine(b) |
| 289 |
b.ResetTimer() |
| 290 |
for i := 0; i < b.N; i++ { |
| 291 |
err := pm.AddPresence("channel", "uid", ¢rifuge.ClientInfo{}) |
| 292 |
if err != nil { |
| 293 |
b.Fatal(err) |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
func BenchmarkTarantoolAddPresence_OneChannel_Parallel(b *testing.B) { |
| 299 |
_, pm := newTestTarantoolEngine(b) |
| 300 |
b.SetParallelism(128) |
| 301 |
b.ResetTimer() |
| 302 |
b.RunParallel(func(pb *testing.PB) { |
| 303 |
for pb.Next() { |
| 304 |
err := pm.AddPresence("channel", "uid", ¢rifuge.ClientInfo{}) |
| 305 |
if err != nil { |
| 306 |
b.Fatal(err) |
| 307 |
} |
| 308 |
} |
| 309 |
}) |
| 310 |
} |
| 311 |
|
| 312 |
func BenchmarkTarantoolPresenceStats_OneChannel_Parallel(b *testing.B) { |
| 313 |
_, pm := newTestTarantoolEngine(b) |
| 314 |
_ = pm.AddPresence("channel", "uid", ¢rifuge.ClientInfo{}) |
| 315 |
b.SetParallelism(128) |
| 316 |
b.ResetTimer() |
| 317 |
b.RunParallel(func(pb *testing.PB) { |
| 318 |
for pb.Next() { |
| 319 |
p, err := pm.PresenceStats("channel") |
| 320 |
if err != nil { |
| 321 |
b.Fatal(err) |
| 322 |
} |
| 323 |
if p.NumClients != 1 { |
| 324 |
b.Fatal("wrong presence stats") |
| 325 |
} |
| 326 |
} |
| 327 |
}) |
| 328 |
} |