Notion-powered devlog

Jade Devlog

Jade의 기술 블로그

DAY-4 - Primary Shard 수 변경과 재색인

오늘의 주제

  1. 학습 목표
    1. Primary shard 수가 index 생성 시 중요한 이유를 이해하고 split 또는 reindex와 alias 전환으로 shard 구성을 변경한다.

학습 전 기반 지식

  1. Static index setting
    1. index.number_of_shards는 document routing 공간과 Lucene index 개수를 결정하므로 일반적인 dynamic setting처럼 열린 기존 index에서 바로 바꿀 수 없다. 반면 replica 수와 refresh interval 같은 일부 설정은 동적으로 변경할 수 있다.
  1. Shard sizing
    1. 정답인 shard 개수는 고정 공식이 아니라 예상 데이터 크기, 증가율, query 동시성, node 자원, recovery 목표에 따라 정한다. shard는 병렬 처리 단위인 동시에 고정 overhead를 가진다.
  1. Split
    1. 기존 index를 read-only로 만든 뒤 primary shard 수가 배수인 새 target index를 만든다. routing shard 조건과 file system 특성의 영향을 받으며 source mapping을 상속한다. split 후에도 alias 전환과 검증이 필요하다.
  1. Reindex
    1. 원하는 mapping과 shard 수로 새 index를 만들고 source document를 다시 색인한다. shard 수를 자유롭게 바꾸거나 mapping/analyzer를 변경할 수 있지만 읽기·쓰기 부하와 추가 저장 공간이 필요하다.
  1. Alias cutover
    1. 애플리케이션이 물리 index 이름 대신 alias를 사용하게 하고, 새 index 검증 후 한 번의 alias update 요청으로 읽기 대상을 전환한다. 쓰기가 계속된다면 변경분 동기화 또는 write pause 전략이 필요하다.

변경 전략 선택

  1. Split: primary 수를 배수로 늘리고 mapping을 유지하며 source를 일시적으로 read-only로 만들 수 있을 때.
  1. Reindex: shard 수를 줄이거나 자유롭게 변경하고 mapping/analyzer도 바꾸려 할 때.
  1. Rollover: 시계열 index에서 기존 index를 바꾸기보다 다음 index부터 새 shard 정책을 적용할 때.

단계별 실습

  1. source index를 만들고 sample document를 넣는다.
    1. OS=http://localhost:9200 curl -s -X PUT "$OS/catalog-v1" -H 'Content-Type: application/json' -d ' {"settings":{"number_of_shards":1,"number_of_replicas":0}}' curl -s -X PUT "$OS/catalog-v1/_doc/1" \ -H 'Content-Type: application/json' -d '{"name":"keyboard"}' curl -s -X PUT "$OS/catalog-v1/_doc/2?refresh=wait_for" \ -H 'Content-Type: application/json' -d '{"name":"mouse"}'
  1. source를 read-only로 만들고 2개 primary를 가진 target으로 split한다.
    1. curl -s -X PUT "$OS/catalog-v1/_settings" -H 'Content-Type: application/json' -d ' {"settings":{"index.blocks.write":true}}' curl -s -X POST "$OS/catalog-v1/_split/catalog-v2" \ -H 'Content-Type: application/json' -d ' {"settings":{"index.number_of_shards":2,"index.number_of_replicas":0,"index.blocks.write":null}}' curl -s "$OS/_cluster/health/catalog-v2?wait_for_status=yellow&pretty" curl -s "$OS/catalog-v1/_count" curl -s "$OS/catalog-v2/_count"
  1. alias를 target으로 연결하고 alias로 검색한다.
    1. curl -s -X POST "$OS/_aliases" -H 'Content-Type: application/json' -d ' {"actions":[{"add":{"index":"catalog-v2","alias":"catalog-read"}}]}' curl -s "$OS/catalog-read/_search?pretty"

전환 검증 항목

  1. source와 target의 document count와 핵심 집계 결과가 같은가?
  1. mapping, analyzer, index setting이 의도한 값인가?
  1. target shard가 모두 active이고 recovery가 끝났는가?
  1. search latency와 indexing throughput이 목표 범위인가?
  1. rollback 시 alias를 source로 되돌릴 수 있는가?

문제 상황과 해결 관점

  1. 무중단이라고 가정: split의 read-only 조건과 reindex 중 변경분 처리를 계획해야 한다.
  1. document count만 검증: mapping 오류나 분석기 변경은 count로 잡히지 않는다. 대표 query와 집계를 비교한다.
  1. Alias 없이 물리 이름 사용: client 설정을 모두 바꿔야 한다. read/write alias를 배포 계약으로 사용한다.

정리와 cleanup

curl -s -X DELETE "$OS/catalog-v1,catalog-v2"

완료 기준

  1. primary shard 수가 일반 dynamic setting이 아닌 이유를 설명할 수 있다.
  1. split과 reindex의 조건과 비용을 비교할 수 있다.
  1. 새 index 검증과 alias cutover 절차를 수행했다.

공식 자료

  1. https://docs.opensearch.org/latest/api-reference/index-apis/split/
  1. https://docs.opensearch.org/latest/api-reference/index-apis/reindex/
  1. https://docs.opensearch.org/latest/api-reference/index-apis/alias/