티스토리 뷰
elasticsearch cloud에서 데모 연습 (14일 free trial)
1. 무료 cluster deploy
배포한 cluster edit 버튼 누르고 data tier 추가해준다.
(rollover 테스트 해야하므로 이미 설정된 hot 외에도 warm, cold 티어 추가. 데모 테스트하는 시점에 frozen 티어는 추가하려면 과금됨.)
(참고) 데이터 티어란?
A data tier is a collection of nodes with the same data role that typically share the same hardware profile
이렇게 수정했다..
hot 인스턴스 : 2개
warm 인스턴스 : 2개
cold 인스턴스 : 1개
빠른 테스트를 위해 클러스터의 poll_interval 간격을 줄여주자 (기본 10분)
PUT _cluster/settings
{
"transient": {
"indices.lifecycle.poll_interval": "5s"
}
}
이제 my-cluster의 키바나를 open 한다.
2. lifecycle policy 생성하기
stack management > index lifecycle policies > create policy
이름 : demo-ilm-policy
여기서 중요한 것은 어떤 조건에 도달하면 각 phase (hot, warm, cold)로 넘어갈지 설정해주는 것이다.
특히 rollover 테스트를 위해서, hot phase에서 Use recommended defaults 를 해제하고 금방 확인할 수 있는 custom rollover 조건을 설정해주도록 하자.
000001 인덱스가 roll over 되어 새로운 인덱스 000002 가 생성된다면, 다음부터 insert되는 document는 이 2번 인덱스에 저장되고, 기존 1번 인덱스는 read 용으로만 사용됨.
(참고) shard 와 segment
각 shard에 document는 실질적으로 segment라는 물리적인 파일에 저장됨.
document 업데이트시, 기존 파일 부분을 불용 처리하고 새로 write함.. 그리고 주기적으로 background에서 segment merge 작업을 통해 불용 flag 처리된 데이터는 실제로 디스크에서 삭제하고, 하나의 큰 segment로 합침.
force merge -> shard의 segment 개수 줄이고, 삭제된 document 디스크에서 삭제
shrink -> primary 샤드 개수 줄이기
searchable snapshot -> 스냅샷으로 바꿔서 저장하기..?
이거는 이미 만들어진 딜리버드 관련 policy 아무거나 재사용해도 되지 않을까 싶음.
근데 다 hot phase rollover 설정밖에 안되어 있넹..
GET _ilm/policy/demo-ilm-policy
==> 새로운 인덱스 만들땐 여기부터 시작!
(방법 1) 그냥 인덱스-000001
3. index template 정의하기
index management > index templates > create template
이경우는 dev, prod 따로 만들어줘야할 듯?
이름 : dev-dealibird-job-template
패턴 : dev-dealibird-job*
index setting
{
"index": {
"lifecycle": {
"name": "demo-ilm-policy",
"rollover_alias": "dev-dealibird-job"
}
}
}
mapping (skip 가능)
{
"properties": {
"rails_env": {
"type": "keyword"
},
"job_class": {
"type": "keyword"
},
"job_id": {
"type": "keyword"
},
"args": {
"type": "text"
},
"queue": {
"type": "keyword"
},
"retry_count": {
"type": "integer"
},
"perform_at": {
"type": "date"
}
}
}
확인
GET /_index_template/dev-dealibird-job-template
4. 인덱스 생성하기
alias 세팅도 해줘야 한다..
PUT dev-dealibird-job-000001
{
"aliases": {
"dev-dealibird-job": {
"is_write_index": true
}
},
"mappings": {
"properties": {
"rails_env": {
"type": "keyword"
},
"job_class": {
"type": "keyword"
},
"job_id": {
"type": "keyword"
},
"args": {
"type": "text"
},
"queue": {
"type": "keyword"
},
"retry_count": {
"type": "integer"
},
"perform_at": {
"type": "date"
}
}
}
}
5. document 넣고 rollover 잘 되는지 확인하기
POST _bulk
{"index":{"_index":"dev-dealibird-job"}}
{"rails_env":"qa", "job_class": "CreateRetailBillJob", "job_id": "087bd69b-9833-4700-a155-84e25df938dd", "args": "[1,2]", "queue": "create_bill", "retry_count": 1, "perform_at": "2022-06-29T16:05:03+09:00"}
확인
GET dev-dealibird-job/_search
# 인덱스 확인
GET _cat/indices
# 샤드 확인
GET _cat/shards/dev-dealibird-job?v
document 10개를 인덱싱 한뒤 poll_interval (5s) 정도 기다리고 샤드 확인해보면 인덱스 000002가 자동으로 생겼다.
이제 새로운 document를 더 넣어보면 000002로 들어가는 것을 확인할 수 있다.
p는 primary 샤드, r은 replica 인듯..
(방법 2) 데이터 스트림
이게 더 정석적인 방법 같은데, 이 경우 bulk 인서트의 operation 타입이 create 밖에 안되가지고, kinesis에 붙어있는 람다 업데이트 해줘야하는데 번거로워서 일단 방법1 택함.
데이터 스트림이란?
A data stream lets you store append-only time series data across multiple indices while giving you a single named resource for requests.
3. index template 정의하기
이름 : dealibird-error-template
패턴 : dev-dealibird-error* / prod-dealibird-error*
create data stream 옵션 켜기!
index setting
{
"index.lifecycle.name": "demo-ilm-policy"
}
mapping
매핑에는 타임 관련 필드 설정해줄 필요 없고 인덱싱할 때 @timestamp 값을 넘겨줘야 한다...
{
"properties": {
"rails_env": {
"type": "keyword"
},
"message": {
"type": "text"
},
"backtrace": {
"type": "text"
}
}
}
4. 데이터 스트림 생성하기
PUT _data_stream/dev-dealibird-error
PUT _data_stream/prod-dealibird-error
정보 확인
GET /_data_stream/dev-dealibird-error
데이터 스트림 삭제
DELETE /_data_stream/dev-dealibird-error
5. document 넣고 rollover 잘되는지 확인하기
operation이 create이어야 함!, 그리고 @timestamp 값 꼭 필요함 => 이거 두가지가 data-stream 사용할거면 수정해야할 사항!
POST _bulk
{"create":{"_index":"dev-dealibird-error"}}
{"rails_env":"qa", "message": "에러 테스트", "backtrace": "[1,2]", "@timestamp": "2022-06-29T16:05:03+09:00" }
index postfix에 날짜가 들어간다.
새로운 document는 이제 index 000002로 들어간다.
아 눈알빠지겠다
== (실험) ==
중간에 index management policy 기준 바꿨을때 기존 인덱스 패턴 document는 사라지지 않으면서 잘 적용될까?
: 잘 된다
'시리즈 > Database' 카테고리의 다른 글
[SQL] 문제 풀다 알게 된 문법 정리 (0) | 2021.03.19 |
---|---|
몽구스 튜토리얼 (0) | 2021.03.03 |