rocksmq_retention_test.go 18.4 KB
Newer Older
Y
yukun 已提交
1 2 3 4 5 6 7 8 9 10 11
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under the License.

J
jaime 已提交
12
package server
Y
yukun 已提交
13 14 15 16

import (
	"os"
	"strconv"
Y
yukun 已提交
17
	"sync/atomic"
Y
yukun 已提交
18 19 20
	"testing"
	"time"

Y
yukun 已提交
21
	"github.com/milvus-io/milvus/internal/log"
22
	"github.com/milvus-io/milvus/internal/util/paramtable"
Y
yukun 已提交
23
	"github.com/stretchr/testify/assert"
Y
yukun 已提交
24
	"go.uber.org/zap"
Y
yukun 已提交
25 26
)

27
var retentionPath = "/tmp/rmq_retention/"
28 29 30 31 32 33

func TestMain(m *testing.M) {
	code := m.Run()
	os.Exit(code)
}

X
Xiaofan 已提交
34 35 36 37 38 39
// Test write data and wait for retention
func TestRmqRetention_Basic(t *testing.T) {
	err := os.MkdirAll(retentionPath, os.ModePerm)
	if err != nil {
		log.Error("MkdirAll error for path", zap.Any("path", retentionPath))
		return
40
	}
X
Xiaofan 已提交
41
	defer os.RemoveAll(retentionPath)
X
Xiaofan 已提交
42
	rocksdbPath := retentionPath
Y
yukun 已提交
43
	defer os.RemoveAll(rocksdbPath)
44
	metaPath := retentionPath + metaPathSuffix
Y
yukun 已提交
45 46
	defer os.RemoveAll(metaPath)

47 48 49
	var params paramtable.BaseTable
	params.Init()
	rmq, err := NewRocksMQ(params, rocksdbPath, nil)
Y
yukun 已提交
50
	assert.Nil(t, err)
51 52 53 54 55
	defer rmq.Close()
	atomic.StoreInt64(&RocksmqRetentionSizeInMB, 0)
	atomic.StoreInt64(&RocksmqRetentionTimeInSecs, 0)
	atomic.StoreInt64(&RocksmqPageSize, 10)
	atomic.StoreInt64(&TickerTimeInSeconds, 2)
Y
yukun 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68

	topicName := "topic_a"
	err = rmq.CreateTopic(topicName)
	assert.Nil(t, err)
	defer rmq.DestroyTopic(topicName)

	msgNum := 100
	pMsgs := make([]ProducerMessage, msgNum)
	for i := 0; i < msgNum; i++ {
		msg := "message_" + strconv.Itoa(i)
		pMsg := ProducerMessage{Payload: []byte(msg)}
		pMsgs[i] = pMsg
	}
69
	ids, err := rmq.Produce(topicName, pMsgs)
Y
yukun 已提交
70
	assert.Nil(t, err)
71
	assert.Equal(t, len(pMsgs), len(ids))
Y
yukun 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

	groupName := "test_group"
	_ = rmq.DestroyConsumerGroup(topicName, groupName)
	err = rmq.CreateConsumerGroup(topicName, groupName)

	consumer := &Consumer{
		Topic:     topicName,
		GroupName: groupName,
	}
	rmq.RegisterConsumer(consumer)

	assert.Nil(t, err)
	cMsgs := make([]ConsumerMessage, 0)
	for i := 0; i < msgNum; i++ {
		cMsg, err := rmq.Consume(topicName, groupName, 1)
		assert.Nil(t, err)
		cMsgs = append(cMsgs, cMsg[0])
	}
	assert.Equal(t, len(cMsgs), msgNum)

92
	checkTimeInterval := 2
Y
yukun 已提交
93
	time.Sleep(time.Duration(checkTimeInterval+1) * time.Second)
Y
yukun 已提交
94 95 96 97 98 99
	// Seek to a previous consumed message, the message should be clean up
	err = rmq.Seek(topicName, groupName, cMsgs[msgNum/2].MsgID)
	assert.Nil(t, err)
	newRes, err := rmq.Consume(topicName, groupName, 1)
	assert.Nil(t, err)
	assert.Equal(t, len(newRes), 0)
X
Xiaofan 已提交
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

	// test acked size acked ts and other meta are updated as expect
	msgSizeKey := MessageSizeTitle + topicName
	msgSizeVal, err := rmq.kv.Load(msgSizeKey)
	assert.NoError(t, err)
	assert.Equal(t, msgSizeVal, "0")

	pageMsgSizeKey := constructKey(PageMsgSizeTitle, topicName)
	keys, values, err := rmq.kv.LoadWithPrefix(pageMsgSizeKey)
	assert.NoError(t, err)
	assert.Equal(t, len(keys), 0)
	assert.Equal(t, len(values), 0)

	pageTsSizeKey := constructKey(PageTsTitle, topicName)
	keys, values, err = rmq.kv.LoadWithPrefix(pageTsSizeKey)
	assert.NoError(t, err)
	assert.Equal(t, len(keys), 0)
	assert.Equal(t, len(values), 0)

	aclTsSizeKey := constructKey(AckedTsTitle, topicName)
	keys, values, err = rmq.kv.LoadWithPrefix(aclTsSizeKey)
	assert.NoError(t, err)
	assert.Equal(t, len(keys), 0)
	assert.Equal(t, len(values), 0)
}

// Not acked message should not be purged
func TestRmqRetention_NotConsumed(t *testing.T) {
	err := os.MkdirAll(retentionPath, os.ModePerm)
	if err != nil {
		log.Error("MkdirAll error for path", zap.Any("path", retentionPath))
		return
	}
	defer os.RemoveAll(retentionPath)

X
Xiaofan 已提交
135
	rocksdbPath := retentionPath
X
Xiaofan 已提交
136 137 138 139
	defer os.RemoveAll(rocksdbPath)
	metaPath := retentionPath + metaPathSuffix
	defer os.RemoveAll(metaPath)

140 141 142
	var params paramtable.BaseTable
	params.Init()
	rmq, err := NewRocksMQ(params, rocksdbPath, nil)
X
Xiaofan 已提交
143
	assert.Nil(t, err)
144 145 146 147 148 149
	defer rmq.Close()

	atomic.StoreInt64(&RocksmqRetentionSizeInMB, 0)
	atomic.StoreInt64(&RocksmqRetentionTimeInSecs, 0)
	atomic.StoreInt64(&RocksmqPageSize, 10)
	atomic.StoreInt64(&TickerTimeInSeconds, 2)
X
Xiaofan 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

	topicName := "topic_a"
	err = rmq.CreateTopic(topicName)
	assert.Nil(t, err)
	defer rmq.DestroyTopic(topicName)

	msgNum := 100
	pMsgs := make([]ProducerMessage, msgNum)
	for i := 0; i < msgNum; i++ {
		msg := "message_" + strconv.Itoa(i)
		pMsg := ProducerMessage{Payload: []byte(msg)}
		pMsgs[i] = pMsg
	}
	ids, err := rmq.Produce(topicName, pMsgs)
	assert.Nil(t, err)
	assert.Equal(t, len(pMsgs), len(ids))

	groupName := "test_group"
	_ = rmq.DestroyConsumerGroup(topicName, groupName)
	err = rmq.CreateConsumerGroup(topicName, groupName)

	consumer := &Consumer{
		Topic:     topicName,
		GroupName: groupName,
	}
	rmq.RegisterConsumer(consumer)

	assert.Nil(t, err)
	cMsgs := make([]ConsumerMessage, 0)
	for i := 0; i < 5; i++ {
		cMsg, err := rmq.Consume(topicName, groupName, 1)
		assert.Nil(t, err)
		cMsgs = append(cMsgs, cMsg[0])
	}
	assert.Equal(t, len(cMsgs), 5)
	id := cMsgs[0].MsgID

	aclTsSizeKey := constructKey(AckedTsTitle, topicName)
	keys, values, err := rmq.kv.LoadWithPrefix(aclTsSizeKey)
	assert.NoError(t, err)
	assert.Equal(t, len(keys), 2)
	assert.Equal(t, len(values), 2)

	// wait for retention
	checkTimeInterval := 2
195
	time.Sleep(time.Duration(checkTimeInterval+1) * time.Second)
X
Xiaofan 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	// Seek to a previous consumed message, the message should be clean up
	err = rmq.Seek(topicName, groupName, cMsgs[1].MsgID)
	assert.Nil(t, err)
	newRes, err := rmq.Consume(topicName, groupName, 1)
	assert.Nil(t, err)
	assert.Equal(t, len(newRes), 1)
	assert.Equal(t, newRes[0].MsgID, id+4)

	// test acked size acked ts and other meta are updated as expect
	msgSizeKey := MessageSizeTitle + topicName
	msgSizeVal, err := rmq.kv.Load(msgSizeKey)
	assert.NoError(t, err)
	assert.Equal(t, msgSizeVal, "0")

	// should only clean 2 pages
	pageMsgSizeKey := constructKey(PageMsgSizeTitle, topicName)
	keys, values, err = rmq.kv.LoadWithPrefix(pageMsgSizeKey)
	assert.NoError(t, err)
	assert.Equal(t, len(keys), 48)
	assert.Equal(t, len(values), 48)

	pageTsSizeKey := constructKey(PageTsTitle, topicName)
	keys, values, err = rmq.kv.LoadWithPrefix(pageTsSizeKey)
	assert.NoError(t, err)

	assert.Equal(t, len(keys), 48)
	assert.Equal(t, len(values), 48)

	aclTsSizeKey = constructKey(AckedTsTitle, topicName)
	keys, values, err = rmq.kv.LoadWithPrefix(aclTsSizeKey)
	assert.NoError(t, err)
	assert.Equal(t, len(keys), 0)
	assert.Equal(t, len(values), 0)
}

// Test multiple topic
func TestRmqRetention_MultipleTopic(t *testing.T) {
X
Xiaofan 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246
	err := os.MkdirAll(retentionPath, os.ModePerm)
	if err != nil {
		log.Error("MkdirALl error for path", zap.Any("path", retentionPath))
		return
	}
	defer os.RemoveAll(retentionPath)
	kvPath := retentionPath + "kv_multi_topic"
	os.RemoveAll(kvPath)
	idAllocator := InitIDAllocator(kvPath)

	rocksdbPath := retentionPath + "db_multi_topic"
	os.RemoveAll(rocksdbPath)
	metaPath := retentionPath + "meta_multi_topic"
	os.RemoveAll(metaPath)
247 248 249
	var params paramtable.BaseTable
	params.Init()
	rmq, err := NewRocksMQ(params, rocksdbPath, idAllocator)
X
Xiaofan 已提交
250 251 252
	assert.Nil(t, err)
	defer rmq.Close()

253 254 255 256 257 258 259
	// no retention by size
	atomic.StoreInt64(&RocksmqRetentionSizeInMB, -1)
	// retention by secs
	atomic.StoreInt64(&RocksmqRetentionTimeInSecs, 1)
	atomic.StoreInt64(&RocksmqPageSize, 10)
	atomic.StoreInt64(&TickerTimeInSeconds, 1)

X
Xiaofan 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
	topicName := "topic_a"
	err = rmq.CreateTopic(topicName)
	assert.Nil(t, err)
	defer rmq.DestroyTopic(topicName)

	msgNum := 100
	pMsgs := make([]ProducerMessage, msgNum)
	for i := 0; i < msgNum; i++ {
		msg := "message_" + strconv.Itoa(i)
		pMsg := ProducerMessage{Payload: []byte(msg)}
		pMsgs[i] = pMsg
	}
	ids1, err := rmq.Produce(topicName, pMsgs)
	assert.Nil(t, err)
	assert.Equal(t, len(pMsgs), len(ids1))

	topicName = "topic_b"
	err = rmq.CreateTopic(topicName)
	assert.Nil(t, err)
	defer rmq.DestroyTopic(topicName)
	pMsgs = make([]ProducerMessage, msgNum)
	for i := 0; i < msgNum; i++ {
		msg := "message_" + strconv.Itoa(i)
		pMsg := ProducerMessage{Payload: []byte(msg)}
		pMsgs[i] = pMsg
	}
	ids2, err := rmq.Produce(topicName, pMsgs)
	assert.Nil(t, err)
	assert.Equal(t, len(pMsgs), len(ids2))

	topicName = "topic_a"
	groupName := "test_group"
	_ = rmq.DestroyConsumerGroup(topicName, groupName)
	err = rmq.CreateConsumerGroup(topicName, groupName)
	assert.NoError(t, err)

	consumer := &Consumer{
		Topic:     topicName,
		GroupName: groupName,
	}
	rmq.RegisterConsumer(consumer)

	cMsgs := make([]ConsumerMessage, 0)
	for i := 0; i < msgNum; i++ {
		cMsg, err := rmq.Consume(topicName, groupName, 1)
		assert.Nil(t, err)
		cMsgs = append(cMsgs, cMsg[0])
	}
	assert.Equal(t, len(cMsgs), msgNum)
	assert.Equal(t, cMsgs[0].MsgID, ids1[0])

	time.Sleep(time.Duration(3) * time.Second)

	err = rmq.Seek(topicName, groupName, ids1[10])
	assert.Nil(t, err)
	newRes, err := rmq.Consume(topicName, groupName, 1)
	assert.Nil(t, err)
	assert.Equal(t, len(newRes), 0)

	// test acked size acked ts and other meta are updated as expect
	msgSizeKey := MessageSizeTitle + topicName
	msgSizeVal, err := rmq.kv.Load(msgSizeKey)
	assert.NoError(t, err)
	assert.Equal(t, msgSizeVal, "0")

	// 100 page left, each entity is a page
	pageMsgSizeKey := constructKey(PageMsgSizeTitle, "topic_a")
	keys, values, err := rmq.kv.LoadWithPrefix(pageMsgSizeKey)
	assert.NoError(t, err)
	assert.Equal(t, len(keys), 0)
	assert.Equal(t, len(values), 0)

	pageTsSizeKey := constructKey(PageTsTitle, "topic_a")
	keys, values, err = rmq.kv.LoadWithPrefix(pageTsSizeKey)
	assert.NoError(t, err)

	assert.Equal(t, len(keys), 0)
	assert.Equal(t, len(values), 0)

	aclTsSizeKey := constructKey(AckedTsTitle, "topic_a")
	keys, values, err = rmq.kv.LoadWithPrefix(aclTsSizeKey)
	assert.NoError(t, err)
	assert.Equal(t, len(keys), 0)
	assert.Equal(t, len(values), 0)

	// for topic B, nothing has been cleadn
	pageMsgSizeKey = constructKey(PageMsgSizeTitle, "topic_b")
	keys, values, err = rmq.kv.LoadWithPrefix(pageMsgSizeKey)
	assert.NoError(t, err)
	assert.Equal(t, len(keys), 50)
	assert.Equal(t, len(values), 50)

	pageTsSizeKey = constructKey(PageTsTitle, "topic_b")
	keys, values, err = rmq.kv.LoadWithPrefix(pageTsSizeKey)
	assert.NoError(t, err)

	assert.Equal(t, len(keys), 50)
	assert.Equal(t, len(values), 50)

	aclTsSizeKey = constructKey(AckedTsTitle, "topic_b")
	keys, values, err = rmq.kv.LoadWithPrefix(aclTsSizeKey)
	assert.NoError(t, err)
	assert.Equal(t, len(keys), 0)
	assert.Equal(t, len(values), 0)

	topicName = "topic_b"
	_ = rmq.DestroyConsumerGroup(topicName, groupName)
	err = rmq.CreateConsumerGroup(topicName, groupName)
	assert.NoError(t, err)

	consumer = &Consumer{
		Topic:     topicName,
		GroupName: groupName,
	}
	rmq.RegisterConsumer(consumer)

	cMsgs = make([]ConsumerMessage, 0)
	for i := 0; i < msgNum; i++ {
		cMsg, err := rmq.Consume(topicName, groupName, 1)
		assert.Nil(t, err)
		cMsgs = append(cMsgs, cMsg[0])
	}
	assert.Equal(t, len(cMsgs), msgNum)
	assert.Equal(t, cMsgs[0].MsgID, ids2[0])

	time.Sleep(time.Duration(3) * time.Second)

	err = rmq.Seek(topicName, groupName, ids2[10])
	assert.Nil(t, err)
	newRes, err = rmq.Consume(topicName, groupName, 1)
	assert.Nil(t, err)
	assert.Equal(t, len(newRes), 0)
X
Xiaofan 已提交
392

Y
yukun 已提交
393
}
Y
yukun 已提交
394

395
func TestRetentionInfo_InitRetentionInfo(t *testing.T) {
X
Xiaofan 已提交
396 397 398 399 400 401
	err := os.MkdirAll(retentionPath, os.ModePerm)
	if err != nil {
		log.Error("MkdirALl error for path", zap.Any("path", retentionPath))
		return
	}
	defer os.RemoveAll(retentionPath)
402 403 404 405 406
	suffix := "init"
	kvPath := retentionPath + kvPathSuffix + suffix
	defer os.RemoveAll(kvPath)
	idAllocator := InitIDAllocator(kvPath)

X
Xiaofan 已提交
407
	rocksdbPath := retentionPath + suffix
408 409 410 411 412
	defer os.RemoveAll(rocksdbPath)
	metaPath := retentionPath + metaPathSuffix + suffix

	defer os.RemoveAll(metaPath)

413 414 415
	var params paramtable.BaseTable
	params.Init()
	rmq, err := NewRocksMQ(params, rocksdbPath, idAllocator)
416 417 418
	assert.Nil(t, err)
	assert.NotNil(t, rmq)

X
Xiaofan 已提交
419 420 421 422 423
	topicName := "topic_a"
	err = rmq.CreateTopic(topicName)
	assert.Nil(t, err)

	rmq.Close()
424
	rmq, err = NewRocksMQ(params, rocksdbPath, idAllocator)
X
Xiaofan 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
	assert.Nil(t, err)
	assert.NotNil(t, rmq)

	assert.Equal(t, rmq.isClosed(), false)
	// write some data, restart and check.
	topicName = "topic_a"
	err = rmq.CreateTopic(topicName)
	assert.Nil(t, err)
	topicName = "topic_b"
	err = rmq.CreateTopic(topicName)
	assert.Nil(t, err)

	msgNum := 100
	pMsgs := make([]ProducerMessage, msgNum)
	for i := 0; i < msgNum; i++ {
		msg := "message_" + strconv.Itoa(i)
		pMsg := ProducerMessage{Payload: []byte(msg)}
		pMsgs[i] = pMsg
	}
	ids, err := rmq.Produce(topicName, pMsgs)
	assert.Nil(t, err)
	assert.Equal(t, len(pMsgs), len(ids))

	rmq.Close()
449 450
}

X
Xiaofan 已提交
451 452 453 454 455 456 457
func TestRmqRetention_PageTimeExpire(t *testing.T) {
	err := os.MkdirAll(retentionPath, os.ModePerm)
	if err != nil {
		log.Error("MkdirALl error for path", zap.Any("path", retentionPath))
		return
	}
	defer os.RemoveAll(retentionPath)
458

X
Xiaofan 已提交
459 460
	kvPath := retentionPath + "kv_com1"
	os.RemoveAll(kvPath)
Y
yukun 已提交
461 462
	idAllocator := InitIDAllocator(kvPath)

X
Xiaofan 已提交
463 464 465 466
	rocksdbPath := retentionPath + "db_com1"
	os.RemoveAll(rocksdbPath)
	metaPath := retentionPath + "meta_kv_com1"
	os.RemoveAll(metaPath)
Y
yukun 已提交
467

468 469 470
	var params paramtable.BaseTable
	params.Init()
	rmq, err := NewRocksMQ(params, rocksdbPath, idAllocator)
Y
yukun 已提交
471
	assert.Nil(t, err)
X
Xiaofan 已提交
472
	defer rmq.Close()
Y
yukun 已提交
473

474 475 476 477 478 479 480
	// no retention by size
	atomic.StoreInt64(&RocksmqRetentionSizeInMB, -1)
	// retention by secs
	atomic.StoreInt64(&RocksmqRetentionTimeInSecs, 5)
	atomic.StoreInt64(&RocksmqPageSize, 10)
	atomic.StoreInt64(&TickerTimeInSeconds, 1)

Y
yukun 已提交
481 482 483 484 485 486 487 488 489 490 491 492
	topicName := "topic_a"
	err = rmq.CreateTopic(topicName)
	assert.Nil(t, err)
	defer rmq.DestroyTopic(topicName)

	msgNum := 100
	pMsgs := make([]ProducerMessage, msgNum)
	for i := 0; i < msgNum; i++ {
		msg := "message_" + strconv.Itoa(i)
		pMsg := ProducerMessage{Payload: []byte(msg)}
		pMsgs[i] = pMsg
	}
493
	ids, err := rmq.Produce(topicName, pMsgs)
Y
yukun 已提交
494
	assert.Nil(t, err)
495
	assert.Equal(t, len(pMsgs), len(ids))
Y
yukun 已提交
496 497 498 499

	groupName := "test_group"
	_ = rmq.DestroyConsumerGroup(topicName, groupName)
	err = rmq.CreateConsumerGroup(topicName, groupName)
X
Xiaofan 已提交
500
	assert.NoError(t, err)
Y
yukun 已提交
501 502 503 504 505 506 507 508 509 510 511 512 513 514

	consumer := &Consumer{
		Topic:     topicName,
		GroupName: groupName,
	}
	rmq.RegisterConsumer(consumer)

	cMsgs := make([]ConsumerMessage, 0)
	for i := 0; i < msgNum; i++ {
		cMsg, err := rmq.Consume(topicName, groupName, 1)
		assert.Nil(t, err)
		cMsgs = append(cMsgs, cMsg[0])
	}
	assert.Equal(t, len(cMsgs), msgNum)
X
Xiaofan 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527
	assert.Equal(t, cMsgs[0].MsgID, ids[0])
	time.Sleep(time.Duration(3) * time.Second)

	// insert another 100 messages which should not be cleand up
	pMsgs2 := make([]ProducerMessage, msgNum)
	for i := 0; i < msgNum; i++ {
		msg := "message_" + strconv.Itoa(i+100)
		pMsg := ProducerMessage{Payload: []byte(msg)}
		pMsgs2[i] = pMsg
	}
	ids2, err := rmq.Produce(topicName, pMsgs2)
	assert.Nil(t, err)
	assert.Equal(t, len(pMsgs2), len(ids2))
Y
yukun 已提交
528

X
Xiaofan 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541
	assert.Nil(t, err)
	cMsgs = make([]ConsumerMessage, 0)
	for i := 0; i < msgNum; i++ {
		cMsg, err := rmq.Consume(topicName, groupName, 1)
		assert.Nil(t, err)
		cMsgs = append(cMsgs, cMsg[0])
	}
	assert.Equal(t, len(cMsgs), msgNum)
	assert.Equal(t, cMsgs[0].MsgID, ids2[0])

	time.Sleep(time.Duration(3) * time.Second)

	err = rmq.Seek(topicName, groupName, ids[10])
Y
yukun 已提交
542 543 544
	assert.Nil(t, err)
	newRes, err := rmq.Consume(topicName, groupName, 1)
	assert.Nil(t, err)
X
Xiaofan 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
	assert.Equal(t, len(newRes), 1)
	// point to first not consumed messages
	assert.Equal(t, newRes[0].MsgID, ids2[0])

	// test acked size acked ts and other meta are updated as expect
	msgSizeKey := MessageSizeTitle + topicName
	msgSizeVal, err := rmq.kv.Load(msgSizeKey)
	assert.NoError(t, err)
	assert.Equal(t, msgSizeVal, "0")

	// 100 page left, each entity is a page
	pageMsgSizeKey := constructKey(PageMsgSizeTitle, topicName)
	keys, values, err := rmq.kv.LoadWithPrefix(pageMsgSizeKey)
	assert.NoError(t, err)
	assert.Equal(t, len(keys), 100)
	assert.Equal(t, len(values), 100)

	pageTsSizeKey := constructKey(PageTsTitle, topicName)
	keys, values, err = rmq.kv.LoadWithPrefix(pageTsSizeKey)
	assert.NoError(t, err)

	assert.Equal(t, len(keys), 100)
	assert.Equal(t, len(values), 100)

	aclTsSizeKey := constructKey(AckedTsTitle, topicName)
	keys, values, err = rmq.kv.LoadWithPrefix(aclTsSizeKey)
	assert.NoError(t, err)
	assert.Equal(t, len(keys), 100)
	assert.Equal(t, len(values), 100)
Y
yukun 已提交
574 575
}

X
Xiaofan 已提交
576 577 578 579 580 581 582 583
func TestRmqRetention_PageSizeExpire(t *testing.T) {
	err := os.MkdirAll(retentionPath, os.ModePerm)
	if err != nil {
		log.Error("MkdirALl error for path", zap.Any("path", retentionPath))
		return
	}
	defer os.RemoveAll(retentionPath)
	kvPath := retentionPath + "kv_com2"
584
	os.RemoveAll(kvPath)
Y
yukun 已提交
585 586
	idAllocator := InitIDAllocator(kvPath)

X
Xiaofan 已提交
587
	rocksdbPath := retentionPath + "db_com2"
588
	os.RemoveAll(rocksdbPath)
X
Xiaofan 已提交
589
	metaPath := retentionPath + "meta_kv_com2"
590
	os.RemoveAll(metaPath)
Y
yukun 已提交
591

592 593 594
	var params paramtable.BaseTable
	params.Init()
	rmq, err := NewRocksMQ(params, rocksdbPath, idAllocator)
Y
yukun 已提交
595
	assert.Nil(t, err)
X
Xiaofan 已提交
596
	defer rmq.Close()
Y
yukun 已提交
597

598 599 600 601 602 603
	// update some configrocksmq_retentions to make cleanup trigger faster
	atomic.StoreInt64(&RocksmqRetentionSizeInMB, 1)
	atomic.StoreInt64(&RocksmqRetentionTimeInSecs, -1)
	atomic.StoreInt64(&RocksmqPageSize, 10)
	atomic.StoreInt64(&TickerTimeInSeconds, 1)

Y
yukun 已提交
604 605 606 607 608
	topicName := "topic_a"
	err = rmq.CreateTopic(topicName)
	assert.Nil(t, err)
	defer rmq.DestroyTopic(topicName)

X
Xiaofan 已提交
609 610
	// need to be larger than 1M
	msgNum := 100000
Y
yukun 已提交
611 612 613 614 615 616
	pMsgs := make([]ProducerMessage, msgNum)
	for i := 0; i < msgNum; i++ {
		msg := "message_" + strconv.Itoa(i)
		pMsg := ProducerMessage{Payload: []byte(msg)}
		pMsgs[i] = pMsg
	}
617
	ids, err := rmq.Produce(topicName, pMsgs)
Y
yukun 已提交
618
	assert.Nil(t, err)
619
	assert.Equal(t, len(pMsgs), len(ids))
Y
yukun 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638

	groupName := "test_group"
	_ = rmq.DestroyConsumerGroup(topicName, groupName)
	err = rmq.CreateConsumerGroup(topicName, groupName)

	consumer := &Consumer{
		Topic:     topicName,
		GroupName: groupName,
	}
	rmq.RegisterConsumer(consumer)

	assert.Nil(t, err)
	cMsgs := make([]ConsumerMessage, 0)
	for i := 0; i < msgNum; i++ {
		cMsg, err := rmq.Consume(topicName, groupName, 1)
		assert.Nil(t, err)
		cMsgs = append(cMsgs, cMsg[0])
	}
	assert.Equal(t, len(cMsgs), msgNum)
X
Xiaofan 已提交
639 640 641
	log.Debug("Already consumed, wait for message cleaned by retention")
	// wait for enough time for page expiration
	time.Sleep(time.Duration(2) * time.Second)
X
Xiaofan 已提交
642
	err = rmq.Seek(topicName, groupName, ids[0])
Y
yukun 已提交
643 644 645
	assert.Nil(t, err)
	newRes, err := rmq.Consume(topicName, groupName, 1)
	assert.Nil(t, err)
X
Xiaofan 已提交
646 647
	assert.Equal(t, len(newRes), 1)
	// make sure clean up happens
X
Xiaofan 已提交
648
	assert.True(t, newRes[0].MsgID > ids[0])
Y
yukun 已提交
649
}