Skip to content

Commit

Permalink
tweeking
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyGaluzo committed Dec 11, 2024
1 parent daa5c53 commit cfa461c
Show file tree
Hide file tree
Showing 13 changed files with 2,396 additions and 681 deletions.
1,681 changes: 1,463 additions & 218 deletions src/Microsoft.Health.Fhir.SqlServer/Features/Schema/Migrations/84.diff.sql

Large diffs are not rendered by default.

816 changes: 547 additions & 269 deletions src/Microsoft.Health.Fhir.SqlServer/Features/Schema/Migrations/84.sql

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
@ResourceTypeId smallint
,@ResourceId varchar(64)
,@KeepCurrentVersion bit
,@IsResourceChangeCaptureEnabled bit
,@IsResourceChangeCaptureEnabled bit = 1 -- TODO: Remove input parameter after deployment
AS
set nocount on
DECLARE @SP varchar(100) = object_name(@@procid)
,@Mode varchar(200) = 'RT='+convert(varchar,@ResourceTypeId)+' R='+@ResourceId+' V='+convert(varchar,@KeepCurrentVersion)+' CC='+convert(varchar,@IsResourceChangeCaptureEnabled)
,@Mode varchar(200) = ' RT='+convert(varchar,@ResourceTypeId)+' R='+@ResourceId+' V='+convert(varchar,@KeepCurrentVersion)
,@st datetime = getUTCdate()
,@TransactionId bigint
,@DeletedIdMap int
,@Rows int

BEGIN TRY
IF @IsResourceChangeCaptureEnabled = 1 EXECUTE dbo.MergeResourcesBeginTransaction @Count = 1, @TransactionId = @TransactionId OUT
EXECUTE dbo.MergeResourcesBeginTransaction @Count = 1, @TransactionId = @TransactionId OUT
SET @Mode = 'T='+convert(varchar,@TransactionId) + @Mode

RetryResourceIdIntMapLogic:
BEGIN TRY
IF @KeepCurrentVersion = 0
BEGIN TRANSACTION

DECLARE @SurrogateIds TABLE (ResourceSurrogateId BIGINT NOT NULL)
DECLARE @SurrogateIds TABLE (ResourceSurrogateId bigint NOT NULL)

UPDATE dbo.Resource
SET IsDeleted = 1
Expand All @@ -31,9 +35,32 @@ BEGIN TRY

IF @KeepCurrentVersion = 0
BEGIN
DECLARE @RefIdsRaw TABLE (ResourceTypeId smallint NOT NULL, ResourceIdInt bigint NOT NULL)
DECLARE @RefIds TABLE (ResourceTypeId smallint NOT NULL, ResourceIdInt bigint NOT NULL PRIMARY KEY (ResourceTypeId, ResourceIdInt))

-- PAGLOCK allows deallocation of empty page without waiting for ghost cleanup
DELETE FROM B FROM @SurrogateIds A INNER LOOP JOIN dbo.ResourceWriteClaim B WITH (INDEX = 1, FORCESEEK, PAGLOCK) ON B.ResourceSurrogateId = A.ResourceSurrogateId OPTION (MAXDOP 1)
DELETE FROM dbo.ReferenceSearchParam WHERE ResourceTypeId = @ResourceTypeId AND ResourceSurrogateId IN (SELECT ResourceSurrogateId FROM @SurrogateIds)
DELETE FROM B
OUTPUT deleted.ReferenceResourceTypeId, deleted.ReferenceResourceIdInt INTO @RefIdsRaw
FROM @SurrogateIds A INNER LOOP JOIN dbo.ResourceReferenceSearchParams B WITH (INDEX = 1, FORCESEEK, PAGLOCK) ON B.ResourceTypeId = @ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId OPTION (MAXDOP 1)
INSERT INTO @RefIds SELECT DISTINCT ResourceTypeId, ResourceIdInt FROM @RefIdsRaw
SET @Rows = @@rowcount
IF @Rows > 0
BEGIN
DELETE FROM A FROM @RefIds A WHERE EXISTS (SELECT * FROM dbo.CurrentResources B WHERE B.ResourceTypeId = A.ResourceTypeId AND B.ResourceIdInt = A.ResourceIdInt)
SET @Rows -= @@rowcount
IF @Rows > 0
BEGIN
DELETE FROM A FROM @RefIds A WHERE EXISTS (SELECT * FROM dbo.ResourceReferenceSearchParams B WHERE B.ReferenceResourceTypeId = A.ResourceTypeId AND B.ReferenceResourceIdInt = A.ResourceIdInt)
SET @Rows -= @@rowcount
IF @Rows > 0
BEGIN
DELETE FROM B FROM @RefIds A INNER LOOP JOIN dbo.ResourceIdIntMap B ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceIdInt = A.ResourceIdInt
SET @DeletedIdMap = @@rowcount
END
END
END
DELETE FROM B FROM @SurrogateIds A INNER LOOP JOIN dbo.StringReferenceSearchParams B WITH (INDEX = 1, FORCESEEK, PAGLOCK) ON B.ResourceSurrogateId = A.ResourceSurrogateId OPTION (MAXDOP 1)
DELETE FROM B FROM @SurrogateIds A INNER LOOP JOIN dbo.TokenSearchParam B WITH (INDEX = 1, FORCESEEK, PAGLOCK) ON B.ResourceTypeId = @ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId OPTION (MAXDOP 1)
DELETE FROM B FROM @SurrogateIds A INNER LOOP JOIN dbo.TokenText B WITH (INDEX = 1, FORCESEEK, PAGLOCK) ON B.ResourceTypeId = @ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId OPTION (MAXDOP 1)
DELETE FROM B FROM @SurrogateIds A INNER LOOP JOIN dbo.StringSearchParam B WITH (INDEX = 1, FORCESEEK, PAGLOCK) ON B.ResourceTypeId = @ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId OPTION (MAXDOP 1)
Expand All @@ -51,13 +78,23 @@ BEGIN TRY

IF @@trancount > 0 COMMIT TRANSACTION

IF @IsResourceChangeCaptureEnabled = 1 EXECUTE dbo.MergeResourcesCommitTransaction @TransactionId
EXECUTE dbo.MergeResourcesCommitTransaction @TransactionId

EXECUTE dbo.LogEvent @Process=@SP,@Mode=@Mode,@Status='End',@Start=@st
EXECUTE dbo.LogEvent @Process=@SP,@Mode=@Mode,@Status='End',@Start=@st,@Text=@DeletedIdMap
END TRY
BEGIN CATCH
IF @@trancount > 0 ROLLBACK TRANSACTION
EXECUTE dbo.LogEvent @Process=@SP,@Mode=@Mode,@Status='Error',@Start=@st;
THROW
EXECUTE dbo.LogEvent @Process=@SP,@Mode=@Mode,@Status='Error',@Start=@st

IF error_number() = 547 AND error_message() LIKE '%DELETE%'-- reference violation on DELETE
BEGIN
DELETE FROM @SurrogateIds
DELETE FROM @RefIdsRaw
DELETE FROM @RefIds

GOTO RetryResourceIdIntMapLogic
END
ELSE
THROW
END CATCH
GO
Loading

0 comments on commit cfa461c

Please sign in to comment.