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 149 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 195 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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 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 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 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 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
|
//go:build functional
package sarama
import (
"context"
"errors"
"fmt"
"log"
"reflect"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestFuncConsumerGroupPartitioning(t *testing.T) {
checkKafkaVersion(t, "0.10.2")
setupFunctionalTest(t)
defer teardownFunctionalTest(t)
groupID := testFuncConsumerGroupID(t)
// start M1
m1 := runTestFuncConsumerGroupMember(t, groupID, "M1", 0, nil)
defer m1.Stop()
m1.WaitForState(2)
m1.WaitForClaims(map[string]int{"test.4": 4})
m1.WaitForHandlers(4)
// start M2
m2 := runTestFuncConsumerGroupMember(t, groupID, "M2", 0, nil, "test.1", "test.4")
defer m2.Stop()
m2.WaitForState(2)
// assert that claims are shared among both members
m1.WaitForClaims(map[string]int{"test.4": 2})
m1.WaitForHandlers(2)
m2.WaitForClaims(map[string]int{"test.1": 1, "test.4": 2})
m2.WaitForHandlers(3)
// shutdown M1, wait for M2 to take over
m1.AssertCleanShutdown()
m2.WaitForClaims(map[string]int{"test.1": 1, "test.4": 4})
m2.WaitForHandlers(5)
// shutdown M2
m2.AssertCleanShutdown()
}
func TestFuncConsumerGroupPartitioningStateful(t *testing.T) {
checkKafkaVersion(t, "0.10.2")
setupFunctionalTest(t)
defer teardownFunctionalTest(t)
groupID := testFuncConsumerGroupID(t)
m1s := newTestStatefulStrategy(t)
config := defaultConfig("M1")
config.Consumer.Group.Rebalance.GroupStrategies = []BalanceStrategy{m1s}
config.Consumer.Group.Member.UserData = []byte(config.ClientID)
// start M1
m1 := runTestFuncConsumerGroupMemberWithConfig(t, config, groupID, 0, nil)
defer m1.Stop()
m1.WaitForState(2)
m1.WaitForClaims(map[string]int{"test.4": 4})
m1.WaitForHandlers(4)
m1s.AssertInitialValues(1)
m2s := newTestStatefulStrategy(t)
config = defaultConfig("M2")
config.Consumer.Group.Rebalance.GroupStrategies = []BalanceStrategy{m2s}
config.Consumer.Group.Member.UserData = []byte(config.ClientID)
// start M2
m2 := runTestFuncConsumerGroupMemberWithConfig(t, config, groupID, 0, nil, "test.1", "test.4")
defer m2.Stop()
m2.WaitForState(2)
m1s.AssertInitialValues(2)
m2s.AssertNoInitialValues()
// assert that claims are shared among both members
m1.WaitForClaims(map[string]int{"test.4": 2})
m1.WaitForHandlers(2)
m2.WaitForClaims(map[string]int{"test.1": 1, "test.4": 2})
m2.WaitForHandlers(3)
// shutdown M1, wait for M2 to take over
m1.AssertCleanShutdown()
m2.WaitForClaims(map[string]int{"test.1": 1, "test.4": 4})
m2.WaitForHandlers(5)
m2s.AssertNoInitialValues()
}
func TestFuncConsumerGroupExcessConsumers(t *testing.T) {
checkKafkaVersion(t, "0.10.2")
setupFunctionalTest(t)
defer teardownFunctionalTest(t)
groupID := testFuncConsumerGroupID(t)
// start members
m1 := runTestFuncConsumerGroupMember(t, groupID, "M1", 0, nil)
defer m1.Stop()
m2 := runTestFuncConsumerGroupMember(t, groupID, "M2", 0, nil)
defer m2.Stop()
m3 := runTestFuncConsumerGroupMember(t, groupID, "M3", 0, nil)
defer m3.Stop()
m4 := runTestFuncConsumerGroupMember(t, groupID, "M4", 0, nil)
defer m4.Stop()
m1.WaitForClaims(map[string]int{"test.4": 1})
m2.WaitForClaims(map[string]int{"test.4": 1})
m3.WaitForClaims(map[string]int{"test.4": 1})
m4.WaitForClaims(map[string]int{"test.4": 1})
// start M5
m5 := runTestFuncConsumerGroupMember(t, groupID, "M5", 0, nil)
defer m5.Stop()
m5.WaitForState(1)
m5.AssertNoErrs()
// assert that claims are shared among both members
m4.AssertCleanShutdown()
m5.WaitForState(2)
m5.WaitForClaims(map[string]int{"test.4": 1})
// shutdown everything
m1.AssertCleanShutdown()
m2.AssertCleanShutdown()
m3.AssertCleanShutdown()
m5.AssertCleanShutdown()
}
func TestFuncConsumerGroupRebalanceAfterAddingPartitions(t *testing.T) {
checkKafkaVersion(t, "0.10.2")
setupFunctionalTest(t)
defer teardownFunctionalTest(t)
config := NewFunctionalTestConfig()
admin, err := NewClusterAdmin(FunctionalTestEnv.KafkaBrokerAddrs, config)
if err != nil {
t.Fatal(err)
}
defer func() {
_ = admin.Close()
}()
groupID := testFuncConsumerGroupID(t)
// start M1
m1 := runTestFuncConsumerGroupMember(t, groupID, "M1", 0, nil, "test.1")
defer m1.Stop()
m1.WaitForClaims(map[string]int{"test.1": 1})
m1.WaitForHandlers(1)
// start M2
m2 := runTestFuncConsumerGroupMember(t, groupID, "M2", 0, nil, "test.1_to_2")
defer m2.Stop()
m2.WaitForClaims(map[string]int{"test.1_to_2": 1})
m1.WaitForHandlers(1)
// add a new partition to topic "test.1_to_2"
err = admin.CreatePartitions("test.1_to_2", 2, nil, false)
if err != nil {
t.Fatal(err)
}
// assert that claims are shared among both members
m2.WaitForClaims(map[string]int{"test.1_to_2": 2})
m2.WaitForHandlers(2)
m1.WaitForClaims(map[string]int{"test.1": 1})
m1.WaitForHandlers(1)
m1.AssertCleanShutdown()
m2.AssertCleanShutdown()
}
func TestFuncConsumerGroupFuzzy(t *testing.T) {
checkKafkaVersion(t, "0.10.2")
setupFunctionalTest(t)
defer teardownFunctionalTest(t)
if err := testFuncConsumerGroupFuzzySeed("test.4"); err != nil {
t.Fatal(err)
}
groupID := testFuncConsumerGroupID(t)
sink := &testFuncConsumerGroupSink{msgs: make(chan testFuncConsumerGroupMessage, 20000)}
waitForMessages := func(t *testing.T, n int) {
t.Helper()
for i := 0; i < 600; i++ {
if sink.Len() >= n {
break
}
time.Sleep(100 * time.Millisecond)
}
if sz := sink.Len(); sz < n {
log.Fatalf("expected to consume %d messages, but consumed %d", n, sz)
}
}
defer runTestFuncConsumerGroupMember(t, groupID, "M1", 1500, sink).Stop()
defer runTestFuncConsumerGroupMember(t, groupID, "M2", 3000, sink).Stop()
defer runTestFuncConsumerGroupMember(t, groupID, "M3", 1500, sink).Stop()
defer runTestFuncConsumerGroupMember(t, groupID, "M4", 200, sink).Stop()
defer runTestFuncConsumerGroupMember(t, groupID, "M5", 100, sink).Stop()
waitForMessages(t, 3000)
defer runTestFuncConsumerGroupMember(t, groupID, "M6", 300, sink).Stop()
defer runTestFuncConsumerGroupMember(t, groupID, "M7", 400, sink).Stop()
defer runTestFuncConsumerGroupMember(t, groupID, "M8", 500, sink).Stop()
defer runTestFuncConsumerGroupMember(t, groupID, "M9", 2000, sink).Stop()
waitForMessages(t, 8000)
defer runTestFuncConsumerGroupMember(t, groupID, "M10", 1000, sink).Stop()
waitForMessages(t, 10000)
defer runTestFuncConsumerGroupMember(t, groupID, "M11", 1000, sink).Stop()
defer runTestFuncConsumerGroupMember(t, groupID, "M12", 2500, sink).Stop()
waitForMessages(t, 12000)
defer runTestFuncConsumerGroupMember(t, groupID, "M13", 1000, sink).Stop()
waitForMessages(t, 15000)
if umap := sink.Close(); len(umap) != 15000 {
dupes := make(map[string][]string)
for k, v := range umap {
if len(v) > 1 {
dupes[k] = v
}
}
t.Fatalf("expected %d unique messages to be consumed but got %d, including %d duplicates:\n%v", 15000, len(umap), len(dupes), dupes)
}
}
func TestFuncConsumerGroupOffsetDeletion(t *testing.T) {
checkKafkaVersion(t, "2.4.0")
setupFunctionalTest(t)
defer teardownFunctionalTest(t)
config := NewFunctionalTestConfig()
client, err := NewClient(FunctionalTestEnv.KafkaBrokerAddrs, config)
defer safeClose(t, client)
if err != nil {
t.Fatal(err)
}
// create a consumer group with offsets on
// - topic test.1 partition 0
// - topic test.4 partition 0
groupID := testFuncConsumerGroupID(t)
consumerGroup, err := NewConsumerGroupFromClient(groupID, client)
if err != nil {
t.Fatal(err)
}
defer safeClose(t, consumerGroup)
offsetMgr, _ := NewOffsetManagerFromClient(groupID, client)
defer safeClose(t, offsetMgr)
markOffset(t, offsetMgr, "test.1", 0, 1)
markOffset(t, offsetMgr, "test.4", 0, 2)
offsetMgr.Commit()
admin, err := NewClusterAdminFromClient(client)
if err != nil {
t.Fatal(err)
}
offsetFetch, err := admin.ListConsumerGroupOffsets(groupID, nil)
if err != nil {
t.Fatal(err)
}
if len(offsetFetch.Blocks) != 2 {
t.Fatal("Expected offsets on two topics. Found offsets on ", len(offsetFetch.Blocks), "topics.")
}
// Delete offset for partition topic test.4 partition 0
err = admin.DeleteConsumerGroupOffset(groupID, "test.4", 0)
if err != nil {
t.Fatal(err)
}
offsetFetch, err = admin.ListConsumerGroupOffsets(groupID, nil)
if err != nil {
t.Fatal(err)
}
if len(offsetFetch.Blocks) != 1 {
t.Fatal("Expected offsets on one topic. Found offsets on ", len(offsetFetch.Blocks), "topics.")
}
if offsetFetch.Blocks["test.4"] != nil {
t.Fatal("Offset still exists for topic 'topic.4'. It should have been deleted.")
}
}
// --------------------------------------------------------------------
func testFuncConsumerGroupID(t *testing.T) string {
return fmt.Sprintf("sarama.%s%d", t.Name(), time.Now().UnixNano())
}
func markOffset(t *testing.T, offsetMgr OffsetManager, topic string, partition int32, offset int64) {
partitionOffsetManager, err := offsetMgr.ManagePartition(topic, partition)
defer safeClose(t, partitionOffsetManager)
if err != nil {
t.Fatal(err)
}
partitionOffsetManager.MarkOffset(offset, "")
}
func testFuncConsumerGroupFuzzySeed(topic string) error {
client, err := NewClient(FunctionalTestEnv.KafkaBrokerAddrs, NewFunctionalTestConfig())
if err != nil {
return err
}
defer func() { _ = client.Close() }()
total := int64(0)
for pn := int32(0); pn < 4; pn++ {
newest, err := client.GetOffset(topic, pn, OffsetNewest)
if err != nil {
return err
}
oldest, err := client.GetOffset(topic, pn, OffsetOldest)
if err != nil {
return err
}
total = total + newest - oldest
}
if total >= 21000 {
return nil
}
producer, err := NewAsyncProducerFromClient(client)
if err != nil {
return err
}
for i := total; i < 21000; i++ {
producer.Input() <- &ProducerMessage{Topic: topic, Value: ByteEncoder([]byte("testdata"))}
}
return producer.Close()
}
type testFuncConsumerGroupMessage struct {
ClientID string
*ConsumerMessage
}
type testFuncConsumerGroupSink struct {
msgs chan testFuncConsumerGroupMessage
count int32
}
func (s *testFuncConsumerGroupSink) Len() int {
if s == nil {
return -1
}
return int(atomic.LoadInt32(&s.count))
}
func (s *testFuncConsumerGroupSink) Push(clientID string, m *ConsumerMessage) {
if s != nil {
s.msgs <- testFuncConsumerGroupMessage{ClientID: clientID, ConsumerMessage: m}
atomic.AddInt32(&s.count, 1)
}
}
func (s *testFuncConsumerGroupSink) Close() map[string][]string {
close(s.msgs)
res := make(map[string][]string)
for msg := range s.msgs {
key := fmt.Sprintf("%s-%d:%d", msg.Topic, msg.Partition, msg.Offset)
res[key] = append(res[key], msg.ClientID)
}
return res
}
type testFuncConsumerGroupMember struct {
ConsumerGroup
clientID string
claims map[string]int
generationId int32
state int32
handlers int32
errs []error
maxMessages int32
isCapped bool
sink *testFuncConsumerGroupSink
t *testing.T
mu sync.RWMutex
}
func defaultConfig(clientID string) *Config {
config := NewFunctionalTestConfig()
config.ClientID = clientID
config.Consumer.Return.Errors = true
config.Consumer.Offsets.Initial = OffsetOldest
config.Consumer.Group.Rebalance.Timeout = 10 * time.Second
config.Metadata.Full = false
config.Metadata.RefreshFrequency = 5 * time.Second
return config
}
func runTestFuncConsumerGroupMember(t *testing.T, groupID, clientID string, maxMessages int32, sink *testFuncConsumerGroupSink, topics ...string) *testFuncConsumerGroupMember {
t.Helper()
config := defaultConfig(clientID)
return runTestFuncConsumerGroupMemberWithConfig(t, config, groupID, maxMessages, sink, topics...)
}
func runTestFuncConsumerGroupMemberWithConfig(t *testing.T, config *Config, groupID string, maxMessages int32, sink *testFuncConsumerGroupSink, topics ...string) *testFuncConsumerGroupMember {
t.Helper()
group, err := NewConsumerGroup(FunctionalTestEnv.KafkaBrokerAddrs, groupID, config)
if err != nil {
t.Fatal(err)
return nil
}
if len(topics) == 0 {
topics = []string{"test.4"}
}
member := &testFuncConsumerGroupMember{
ConsumerGroup: group,
clientID: config.ClientID,
claims: make(map[string]int),
maxMessages: maxMessages,
isCapped: maxMessages != 0,
sink: sink,
t: t,
}
go member.loop(topics)
return member
}
func (m *testFuncConsumerGroupMember) AssertCleanShutdown() {
m.t.Helper()
if err := m.Close(); err != nil {
m.t.Fatalf("unexpected error on Close(): %v", err)
}
m.WaitForState(4)
m.WaitForHandlers(0)
m.AssertNoErrs()
}
func (m *testFuncConsumerGroupMember) AssertNoErrs() {
m.t.Helper()
var errs []error
m.mu.RLock()
errs = append(errs, m.errs...)
m.mu.RUnlock()
if len(errs) != 0 {
m.t.Fatalf("unexpected consumer errors: %v", errs)
}
}
func (m *testFuncConsumerGroupMember) WaitForState(expected int32) {
m.t.Helper()
m.waitFor("state", expected, func() (interface{}, error) {
return atomic.LoadInt32(&m.state), nil
})
}
func (m *testFuncConsumerGroupMember) WaitForHandlers(expected int) {
m.t.Helper()
m.waitFor("handlers", expected, func() (interface{}, error) {
return int(atomic.LoadInt32(&m.handlers)), nil
})
}
func (m *testFuncConsumerGroupMember) WaitForClaims(expected map[string]int) {
m.t.Helper()
m.waitFor("claims", expected, func() (interface{}, error) {
m.mu.RLock()
claims := m.claims
m.mu.RUnlock()
return claims, nil
})
}
func (m *testFuncConsumerGroupMember) Stop() { _ = m.Close() }
func (m *testFuncConsumerGroupMember) Setup(s ConsumerGroupSession) error {
// store claims
claims := make(map[string]int)
for topic, partitions := range s.Claims() {
claims[topic] = len(partitions)
}
m.mu.Lock()
m.claims = claims
m.mu.Unlock()
// store generationID
atomic.StoreInt32(&m.generationId, s.GenerationID())
// enter post-setup state
atomic.StoreInt32(&m.state, 2)
return nil
}
func (m *testFuncConsumerGroupMember) Cleanup(s ConsumerGroupSession) error {
// enter post-cleanup state
atomic.StoreInt32(&m.state, 3)
return nil
}
func (m *testFuncConsumerGroupMember) ConsumeClaim(s ConsumerGroupSession, c ConsumerGroupClaim) error {
atomic.AddInt32(&m.handlers, 1)
defer atomic.AddInt32(&m.handlers, -1)
for msg := range c.Messages() {
if n := atomic.AddInt32(&m.maxMessages, -1); m.isCapped && n < 0 {
break
}
s.MarkMessage(msg, "")
m.sink.Push(m.clientID, msg)
}
return nil
}
func (m *testFuncConsumerGroupMember) waitFor(kind string, expected interface{}, factory func() (interface{}, error)) {
m.t.Helper()
deadline := time.NewTimer(60 * time.Second)
defer deadline.Stop()
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
var actual interface{}
for {
var err error
if actual, err = factory(); err != nil {
m.t.Errorf("failed retrieve value, expected %s %#v but received error %v", kind, expected, err)
}
if reflect.DeepEqual(expected, actual) {
return
}
select {
case <-deadline.C:
m.t.Fatalf("ttl exceeded, expected %s %#v but got %#v", kind, expected, actual)
return
case <-ticker.C:
}
}
}
func (m *testFuncConsumerGroupMember) loop(topics []string) {
defer atomic.StoreInt32(&m.state, 4)
go func() {
for err := range m.Errors() {
_ = m.Close()
m.mu.Lock()
m.errs = append(m.errs, err)
m.mu.Unlock()
}
}()
ctx := context.Background()
for {
// set state to pre-consume
atomic.StoreInt32(&m.state, 1)
if err := m.Consume(ctx, topics, m); errors.Is(err, ErrClosedConsumerGroup) {
return
} else if err != nil {
m.mu.Lock()
m.errs = append(m.errs, err)
m.mu.Unlock()
return
}
// return if capped
if n := atomic.LoadInt32(&m.maxMessages); m.isCapped && n < 0 {
return
}
}
}
func newTestStatefulStrategy(t *testing.T) *testStatefulStrategy {
return &testStatefulStrategy{
BalanceStrategy: NewBalanceStrategyRange(),
t: t,
}
}
type testStatefulStrategy struct {
BalanceStrategy
t *testing.T
initial int32
state sync.Map
}
func (h *testStatefulStrategy) Name() string {
return "TestStatefulStrategy"
}
func (h *testStatefulStrategy) Plan(members map[string]ConsumerGroupMemberMetadata, topics map[string][]int32) (BalanceStrategyPlan, error) {
h.state = sync.Map{}
for memberID, metadata := range members {
if !strings.HasSuffix(string(metadata.UserData), "-stateful") {
metadata.UserData = []byte(string(metadata.UserData) + "-stateful")
atomic.AddInt32(&h.initial, 1)
}
h.state.Store(memberID, metadata.UserData)
}
return h.BalanceStrategy.Plan(members, topics)
}
func (h *testStatefulStrategy) AssignmentData(memberID string, topics map[string][]int32, generationID int32) ([]byte, error) {
if obj, ok := h.state.Load(memberID); ok {
return obj.([]byte), nil
}
return nil, nil
}
func (h *testStatefulStrategy) AssertInitialValues(count int32) {
h.t.Helper()
actual := atomic.LoadInt32(&h.initial)
if actual != count {
h.t.Fatalf("unexpected count of initial values: %d, expected: %d", actual, count)
}
}
func (h *testStatefulStrategy) AssertNoInitialValues() {
h.t.Helper()
h.AssertInitialValues(0)
}
|