Permalink
Browse files

fixing race condition when initializing a subset of slaves

  • Loading branch information...
1 parent affc9d0 commit 525fa0801e55137e64664b4b2b47f32f6aec8890 @ssandler ssandler committed Mar 6, 2013
Showing with 13 additions and 3 deletions.
  1. +2 −1 scripts/AddTable-ToCT.ps1
  2. +11 −2 scripts/Initialize-DB.ps1
@@ -29,6 +29,7 @@ Param(
[Parameter(Mandatory=$false)][string]$plinkpath, #path to plink.exe
[Parameter(Mandatory=$false)][string]$nzloadscript, #path to netezza load script on the netezza box
[Parameter(Mandatory=$false)][string]$bcppath, #path to bcp files out to for netezza
+ [Parameter(Mandatory=$false)][long]$syncstartversion, #once the table finishes initializing Tesla will start from this version
[switch]$reinitialize, #just reinitialize the table, don't drop/recreate it
[switch]$notlast, #is this the last slave being (re)initialized? we only update tblCTInitialize if this the last one
[switch]$notfirstshard #for sharding, we only truncate/recreate the slave table for the first shard
@@ -361,7 +362,7 @@ Invoke-SqlCmd2 -serverinstance $master -database $masterdb -query $query
#insert table name and current change tracking version into tblCTInitialize
#this will only do anything for the first slave being initialized across multiple slaves
$query = "insert into CT_" + $masterdb + "..tblctinitialize
-select '$table', GETDATE(), 1, null, change_tracking_current_version()
+select '$table', GETDATE(), 1, null, $syncstartversion
where not exists (select 1 from CT_" + $masterdb + "..tblctinitialize where tablename = '$table ')"
Invoke-SqlCmd2 -serverinstance $master -database $masterdb -query $query
View
@@ -554,6 +554,13 @@ CREATE TABLE [dbo].[tblCTSlaveVersion](
VALUES ($version, '1/1/1990', $version, 0);"
$result = invoke-sqlcmd2 -serverinstance $relay -database $relaydb -query $query
}
+} else {
+ #get $version as the start version of the most recent batch, to make sure
+ #no transactions are skipped after initializing the tables
+ Write-Host "getting latest syncStartVersion from relay"
+ $query = "SELECT TOP 1 syncStartVersion FROM tblCTVersion ORDER BY CTID DESC"
+ $result = invoke-sqlcmd2 -serverinstance $relay -database $relaydb -query $query
+ $version = $result.syncStartVersion
}
if ($newdatabase -or $newslave) {
@@ -648,7 +655,8 @@ foreach ($tableconf in $tables.SelectNodes("table")) {
"reinitialize" = $reinitialize;
"notlast" = $notlastslave;
"notfirstshard" = $notfirstshard;
- "directory" = $pwd #$pwd is a magic variable for the current working directory
+ "directory" = $pwd; #$pwd is a magic variable for the current working directory
+ "syncstartversion" = $version;
}
$tablestoinitialize += $arguments
}
@@ -666,7 +674,8 @@ $tablestoinitialize | Foreach-Object {
-table $_.table -schema $_.schema -user $_.user -password $_.password -slavecolumnlist $_.slavecolumnlist `
-mastercolumnlist $_.mastercolumnlist -slavecolumnmodifiers $_.slavecolumnmodifiers -mastercolumnmodifiers $_.mastercolumnmodifiers `
-netezzastringlength $_.netezzastringlength -mappingsfile $_.mappingsfile -sshuser $_.sshuser -pkpath $_.pkpath -plinkpath $_.plinkpath `
- -nzloadscript $_.nzloadscript -bcppath $_.bcppath -reinitialize:$_.reinitialize -notlast:$_.notlast -notfirstshard:$_.notfirstshard
+ -nzloadscript $_.nzloadscript -bcppath $_.bcppath -reinitialize:$_.reinitialize -notlast:$_.notlast `
+ -notfirstshard:$_.notfirstshard -syncstartversion $_.syncstartversion
$duration = [math]::Round((Get-Date).Subtract($starttime).TotalMinutes, 2)
Write-Host ("Initialization complete for table " + $_.table + " in " + $duration + " minutes")

0 comments on commit 525fa08

Please sign in to comment.