Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/elastomer_client/client/ccr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,34 @@ def get_auto_follow(params = {})
response = client.get "/_ccr/auto_follow{/pattern_name}", params.merge(action: "get_auto_follow_pattern", rest_api: "ccr")
response.body
end

# Pauses a follower index.
#
# follower_index - String name of the follower index to pause
# params - Hash of the request body
#
# Examples
# ccr.pause_follow("follower_index")
#
# See https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-post-pause-follow.html
def pause_follow(follower_index, params = {})
response = client.post "/#{follower_index}/_ccr/pause_follow", params.merge(action: "pause_follow", rest_api: "ccr")
response.body
end

# Unfollows a leader index given a follower index.
# The follower index must be paused and closed before unfollowing.
#
# follower_index - String name of the follower index to unfollow
# params - Hash of the request body
#
# Examples
# ccr.unfollow("follower_index")
# See https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-post-unfollow.html
def unfollow(follower_index, params = {})
response = client.post "/#{follower_index}/_ccr/unfollow", params.merge(action: "unfollow", rest_api: "ccr")
response.body
end
end
end
end
76 changes: 68 additions & 8 deletions test/client/ccr_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_relative "../test_helper"

describe ElastomerClient::Client::Ccr do
before do
before :each do
skip "Cannot test Ccr API without a replica cluster" unless $replica_client.available?

@leader_index = $client.index("leader_index")
Expand All @@ -27,17 +27,14 @@
@leader_index.create(default_index_settings)
wait_for_index(@leader_index.name, "green")

@leader_index.docs.index(document_wrapper("book", { _id: 1, title: "Book 1" }))
@leader_index.refresh

begin
ccr.delete_auto_follow("follower_pattern")
rescue StandardError
puts "No auto-follow pattern to delete"
end
end

after do
after :each do
@leader_index.delete if @leader_index.exists?
@follower_index.delete if @follower_index.exists?
@auto_followed_index.delete if @auto_followed_index.exists?
Expand All @@ -50,16 +47,79 @@
end
end

it "successfully follows a leader index" do
def follow_index(follower_index_name, leader_index_name)
ccr = $replica_client.ccr
response = ccr.follow(follower_index_name, { leader_index: leader_index_name, remote_cluster: "leader" })
wait_for_index(follower_index_name, "green")
response
end

def pause_follow(follower_index_name)
ccr = $replica_client.ccr
response = ccr.pause_follow(follower_index_name)
wait_for_index(follower_index_name, "green")
response
end

def unfollow_index(follower_index_name)
ccr = $replica_client.ccr
response = ccr.unfollow(follower_index_name)
wait_for_index(follower_index_name, "green")
response
end

def create_document(index, type, document)
response = index.docs.index(document_wrapper(type, document))
index.refresh
response
end

it "successfully follows a leader index" do
create_document(@leader_index, "book", { _id: 1, title: "Book 1" })

follow_index(@follower_index, @leader_index)

ccr.follow(@follower_index.name, { leader_index: @leader_index.name, remote_cluster: "leader" })
wait_for_index(@follower_index.name, "green")
doc = @follower_index.docs.get(id: 1, type: "book")

assert_equal "Book 1", doc["_source"]["title"]
end

it "successfully pauses a follower index" do
follow_index(@follower_index, @leader_index)

response = pause_follow(@follower_index)

assert response["acknowledged"]

create_document(@leader_index, "book", { _id: 2, title: "Book 2" })

doc = @follower_index.docs.get(id: 2, type: "book")

refute doc["found"]
end

it "successfully unfollow a leader index" do
follow_index(@follower_index, @leader_index)

pause_follow(@follower_index)

@follower_index.close

response = unfollow_index(@follower_index)

assert response["acknowledged"]

@follower_index.open

wait_for_index(@follower_index.name, "green")

create_document(@leader_index, "book", { _id: 2, title: "Book 2" })

doc = @follower_index.docs.get(id: 2, type: "book")

refute doc["found"]
end

it "successfully implements an auto-follow policy" do
ccr = $replica_client.ccr

Expand Down
Loading