該当するシーン
企業はESを操作する複数のコマンド(挿入してから更新、削除してから挿入など)を実行する必要があり、複数のコマンドの実行間隔は非常に短いです。Logstashはイベントをバッチで送信するため、Elasticsearchは非同期スレッドプールによって処理され、データのトランザクション性は通常の使用では保証されません。
Elasticsearchの楽観的ロックメカニズム
Elasticsearchには、保存されているデータごとにバージョン管理があり、レコードを追加、削除、または変更するたびに、レコードのバージョン番号が1つ増えます。特定の操作中に最初にバージョン番号を確認し、現在のバージョン番号が予期されたバージョン番号でない場合、操作はキャンセルされます。
レコードを削除した後、レコードのバージョン情報はデフォルトで1分間保存されます。
Logstashの構成例
間隔が非常に近い2つの操作リクエストにdataVersionフィールドを追加します。このフィールドはタイムスタンプにすることができ、2つの操作のフィールド値は同じである必要があります
input {
kafka {
bootstrap_servers => "192.168.x.x:9092,192.168.x.x:9092,192.168.x.x:9092"
topics => "synclogs"
group_id => "logstash-sync"
consumer_threads => "1"
max_partition_fetch_bytes=> "5242880"
codec => "json"
}
}
output {
if [dataVersion] {
if [action] and [action] == "delete" {
elasticsearch {
hosts => ["192.168.x.x:9200","192.168.x.x:9200"]
index => "sync_test"
document_type => "test"
action => "delete"
codec => "json"
document_id => "%{id}"
version => "%{dataVersion}"
version_type => "external"
retry_on_conflict => 4
}
} else {
elasticsearch {
hosts => ["192.168.x.x:9200","192.168.x.x:9200"]
index => "sync_test"
document_type => "test"
action => "index"
codec => "json"
document_id => "%{id}"
version => "%{dataVersion}"
retry_on_conflict => 4
}
}
}else {
elasticsearch {
hosts => ["192.168.x.x:9200","192.168.x.x:9200"]
index => "sync_test"
document_type => "test"
action => "index"
codec => "json"
document_id => "%{id}"
}
}
}