Partition Reassignment

Partition Reassignment은 파티션의 Replica를 다른 Broker로 이동하는 작업입니다.

Reassignment 개요

사용 사례

┌─────────────────────────────────────────────────────────────────┐
│                  Partition Reassignment 사용 사례                 │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  1. Broker 추가                                                  │
│     새 Broker에 파티션 분배                                       │
│                                                                 │
│  2. Broker 제거                                                  │
│     제거할 Broker의 파티션 이동                                   │
│                                                                 │
│  3. 부하 분산                                                    │
│     특정 Broker에 집중된 파티션 분산                              │
│                                                                 │
│  4. Replication Factor 변경                                      │
│     Replica 추가/제거                                            │
│                                                                 │
│  5. Rack Awareness 적용                                          │
│     Replica를 다른 Rack으로 분산                                  │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Reassignment 프로세스

기본 흐름

1. 재할당 계획 생성
     ↓
2. 계획 실행 (--execute)
     ↓
3. 데이터 복제 시작
     ↓
4. 복제 완료 대기
     ↓
5. 이전 Replica 제거
     ↓
6. 완료 확인 (--verify)

내부 동작

┌─────────────────────────────────────────────────────────────────┐
│                  Reassignment 내부 동작                          │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  Before: Partition 0, Replicas: [1, 2]                         │
│  Target: Partition 0, Replicas: [2, 3]                         │
│                                                                 │
│  단계 1: 새 Replica 추가                                         │
│  Replicas: [1, 2, 3]  ← Broker 3 추가                           │
│                                                                 │
│  단계 2: 데이터 복제                                              │
│  Broker 3이 Broker 1/2로부터 데이터 복제                         │
│                                                                 │
│  단계 3: ISR 진입                                                │
│  ISR: [1, 2, 3]  ← Broker 3 동기화 완료                         │
│                                                                 │
│  단계 4: 이전 Replica 제거                                        │
│  Replicas: [2, 3]  ← Broker 1 제거                              │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Reassignment 실행

1. 현재 상태 확인

# 토픽 상태 확인
kafka-topics.sh --describe \
    --topic my-topic \
    --bootstrap-server localhost:9092
 
# 출력:
# Topic: my-topic  Partition: 0  Leader: 1  Replicas: 1,2  Isr: 1,2
# Topic: my-topic  Partition: 1  Leader: 2  Replicas: 2,1  Isr: 2,1

2. 재할당 계획 생성

# 방법 1: 수동으로 JSON 작성
cat > reassignment.json << EOF
{
  "version": 1,
  "partitions": [
    {"topic": "my-topic", "partition": 0, "replicas": [2, 3]},
    {"topic": "my-topic", "partition": 1, "replicas": [3, 2]}
  ]
}
EOF

자동 계획 생성

# topics-to-move.json 작성
cat > topics-to-move.json << EOF
{
  "topics": [
    {"topic": "my-topic"}
  ],
  "version": 1
}
EOF
 
# 자동 계획 생성
kafka-reassign-partitions.sh --generate \
    --bootstrap-server localhost:9092 \
    --topics-to-move-json-file topics-to-move.json \
    --broker-list "2,3,4"
 
# 출력을 reassignment.json으로 저장

3. 계획 실행

kafka-reassign-partitions.sh --execute \
    --bootstrap-server localhost:9092 \
    --reassignment-json-file reassignment.json

4. 진행 상황 확인

kafka-reassign-partitions.sh --verify \
    --bootstrap-server localhost:9092 \
    --reassignment-json-file reassignment.json
 
# 출력:
# Status of partition reassignment:
# Reassignment of partition my-topic-0 is complete.
# Reassignment of partition my-topic-1 is still in progress.

5. 취소 (필요시)

kafka-reassign-partitions.sh --cancel \
    --bootstrap-server localhost:9092 \
    --reassignment-json-file reassignment.json

고급 옵션

스로틀링

# 복제 속도 제한 (대역폭 보호)
kafka-reassign-partitions.sh --execute \
    --bootstrap-server localhost:9092 \
    --reassignment-json-file reassignment.json \
    --throttle 50000000  # 50MB/s
 
# 스로틀 변경
kafka-reassign-partitions.sh --execute \
    --bootstrap-server localhost:9092 \
    --reassignment-json-file reassignment.json \
    --throttle 100000000  # 100MB/s로 증가

스로틀 제거

# 재할당 완료 후 스로틀 제거
kafka-reassign-partitions.sh --verify \
    --bootstrap-server localhost:9092 \
    --reassignment-json-file reassignment.json
 
# 또는 수동 제거
kafka-configs.sh --alter \
    --entity-type brokers \
    --entity-name 1 \
    --bootstrap-server localhost:9092 \
    --delete-config leader.replication.throttled.rate,follower.replication.throttled.rate

일반적인 시나리오

시나리오 1: Broker 추가

# 기존: Broker 1, 2 (2노드)
# 추가: Broker 3
 
# 1. 현재 파티션 배치 확인
# 2. 새 Broker를 포함한 재할당 계획 생성
# 3. 스로틀링과 함께 실행
 
kafka-reassign-partitions.sh --execute \
    --bootstrap-server localhost:9092 \
    --reassignment-json-file add-broker.json \
    --throttle 100000000

시나리오 2: Broker 제거

# 제거할 Broker의 모든 파티션을 다른 Broker로 이동
 
# 1. 제거할 Broker의 파티션 목록 확인
kafka-topics.sh --describe \
    --bootstrap-server localhost:9092 \
    | grep "Leader: 3"
 
# 2. 해당 파티션을 다른 Broker로 재할당
# 3. 재할당 완료 확인
# 4. Broker 제거

시나리오 3: Replication Factor 증가

# RF 2 → 3
cat > increase-rf.json << EOF
{
  "version": 1,
  "partitions": [
    {"topic": "my-topic", "partition": 0, "replicas": [1, 2, 3]},
    {"topic": "my-topic", "partition": 1, "replicas": [2, 3, 1]}
  ]
}
EOF
 
kafka-reassign-partitions.sh --execute \
    --bootstrap-server localhost:9092 \
    --reassignment-json-file increase-rf.json

모니터링

진행 상황 메트릭

JMX 메트릭:
kafka.server:type=ReplicaManager,name=ReassigningPartitions
→ 현재 재할당 중인 파티션 수

kafka.server:type=ReplicaFetcherManager,name=MaxLag
→ 복제 지연

대역폭 사용량

# 네트워크 사용량 모니터링
sar -n DEV 1
 
# Kafka 복제 처리량
# JMX: kafka.server:type=BrokerTopicMetrics,name=ReplicationBytesInPerSec

Best Practices

1. 스로틀링 사용

# 프로덕션에서는 항상 스로틀링
--throttle 50000000  # 50MB/s 시작

2. 점진적 실행

# 한 번에 모든 파티션이 아닌 단계별 실행
# 1단계: 일부 파티션
# 2단계: 다음 파티션
# ...

3. 피크 시간 회피

# 트래픽이 적은 시간에 실행
# 복제로 인한 추가 부하 고려

4. 백업 확인

# 재할당 전 ISR 상태 확인
kafka-topics.sh --describe \
    --bootstrap-server localhost:9092 \
    --under-replicated-partitions
 
# Under-replicated가 없어야 안전

5. 롤백 계획

# 원래 상태로 복원할 수 있는 JSON 준비
# 문제 발생 시 --cancel 후 원복

관련 문서