Skip to content
Closed
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
50 changes: 50 additions & 0 deletions test/go/data_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,53 @@ func TestCopyThrottleResumesSingleTableWithOneRowBatches(t *testing.T) {
require.Equal(t, []int{1, 1, 1, 1, 1}, run.copiedBatchSizes())
run.requireSourceRowsCopied(t)
}

// RunStandaloneDataCopy runs during cutover with the throttler disabled and
// without Ferry.Run's supporting services. A paused throttler must not block it.
func TestCopyThrottleStandaloneCopyBypassesPause(t *testing.T) {
tables := []string{"t1", "t2"}
ferry := testhelpers.NewTestFerry()

source, err := ferry.Source.SqlDB(nil)
require.NoError(t, err)
defer source.Close()
target, err := ferry.Target.SqlDB(nil)
require.NoError(t, err)
defer target.Close()

dropTestSchema := func() {
for _, db := range []*sql.DB{source, target} {
_, err := db.Exec("DROP DATABASE IF EXISTS gftest")
require.NoError(t, err)
}
}
dropTestSchema()
defer dropTestSchema()
for _, table := range tables {
testhelpers.SeedInitialData(source, "gftest", table, 500)
testhelpers.SeedInitialData(target, "gftest", table, 0)
}

throttler := &ghostferry.PauserThrottler{}
throttler.SetPaused(true)
throttler.SetDisabled(true)
ferry.Throttler = throttler
require.NoError(t, ferry.Initialize())

done := make(chan error, 1)
go func() {
done <- ferry.RunStandaloneDataCopy(ferry.Tables.AsSlice())
}()

select {
case err := <-done:
require.NoError(t, err)
case <-time.After(30 * time.Second):
t.Fatal("standalone data copy did not finish while the throttler was paused and disabled")
}

for _, table := range tables {
rows := testhelpers.AssertQueriesHaveEqualResult(t, ferry.Ferry, fmt.Sprintf("SELECT * FROM gftest.%s ORDER BY id", table))
require.Len(t, rows, 500)
}
}
62 changes: 62 additions & 0 deletions test/integration/interrupt_resume_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require "test_helper"
require "json"
require "net/http"

class InterruptResumeTest < GhostferryTestCase
THROTTLE_RECOVERY_GHOSTFERRY = "throttle_recovery_ghostferry"

def setup
seed_simple_database_with_single_table
end
Expand Down Expand Up @@ -31,6 +34,65 @@ def test_interrupt_resume_without_writes_to_source_to_check_target_state_when_in
assert_equal last_successful_id, dumped_state["LastSuccessfulPaginationKeys"]["#{DEFAULT_DB}.#{DEFAULT_TABLE}"]["value"]
end

def test_interrupt_resume_during_throttle_recovery
ghostferry = new_ghostferry(THROTTLE_RECOVERY_GHOSTFERRY)

post_control = lambda do |path|
attempts = 0
begin
response = Net::HTTP.new("127.0.0.1", 18000).post(path, "", "Content-Type" => "application/json")
rescue Errno::ECONNREFUSED
attempts += 1
raise if attempts > 50
sleep 0.1
retry
end
raise "POST #{path} returned #{response.code}" unless response.code == "200"
end

before_row_copies = 0
after_row_copies = 0
unpause_thread = nil

# Pause while the first, full-sized batch is copied, and again while the
# first recovery batch is copied.
ghostferry.on_status(Ghostferry::Status::BEFORE_ROW_COPY) do
before_row_copies += 1
post_control.call("/api/pause") if before_row_copies <= 2
end

ghostferry.on_status(Ghostferry::Status::AFTER_ROW_COPY) do
after_row_copies += 1
if after_row_copies == 1
# The batch finishes while paused, so the next batch is the first
# recovery batch with 200 / 8 = 25 rows.
unpause_thread = Thread.new do
sleep 1
post_control.call("/api/unpause")
end
else
# Still paused, so no third batch starts before Ghostferry exits.
ghostferry.term_and_wait_for_exit
end
end

dumped_state = ghostferry.run_expecting_interrupt
refute_nil unpause_thread
unpause_thread.join
assert_basic_fields_exist_in_dumped_state(dumped_state)
assert_equal 2, before_row_copies
refute dumped_state.keys.any? { |key| key =~ /throttl|recovery/i }

result = target_db.query("SELECT COUNT(*) AS cnt, MAX(id) AS max_id FROM #{DEFAULT_FULL_TABLE_NAME}").first
assert_equal 200 + 25, result["cnt"]
assert_equal result["max_id"], dumped_state["LastSuccessfulPaginationKeys"]["#{DEFAULT_DB}.#{DEFAULT_TABLE}"]["value"]

ghostferry = new_ghostferry(MINIMAL_GHOSTFERRY)
ghostferry.run(dumped_state)

assert_test_table_is_identical
end

def test_interrupt_and_resume_without_last_known_schema_cache
ghostferry = new_ghostferry(MINIMAL_GHOSTFERRY)

Expand Down
26 changes: 26 additions & 0 deletions test/lib/go/throttle_recovery_ghostferry/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
tf "github.com/Shopify/ghostferry/test/lib/go/integrationferry"
)

// A Ghostferry with one copy worker, a 30s throttle recovery interval and the
// control server on 127.0.0.1:18000, so tests can pause and unpause it.
func main() {
c := tf.RunCallbacks{
BeforeInitialize: func(f *tf.IntegrationFerry) error {
f.Config.DataIterationConcurrency = 1
f.Config.UpdatableConfig.DataIterationBatchSize = 200
f.Config.DataIterationThrottleRecovery.RecoveryInterval = "30s"
f.Config.ControlServerConfig.Enabled = true
f.Config.ControlServerConfig.ServerBindAddr = "127.0.0.1:18000"
return nil
},
}
f := tf.Setup(&c)

err := tf.Run(f)
if err != nil {
panic(err)
}
}
Loading