May 21, 2012

sp_MShelptracertokenhistory (Transact-SQL MetaData) Definition

Please note: that the following source code is provided and copyrighted by Microsoft and is for educational purpose only.
The meta data is from an SQL 2012 Server.

I have posted alot more, find the whole list here.

Goto Definition or MetaData

Definition:

sys.sp_MShelptracertokenhistory(nvarchar @publisher
, nvarchar @publisher_db
, nvarchar @publication
, int @tracer_id)

MetaData:

   
--
-- Name:
-- sp_MShelptracertokenhistory
--
-- Description:
-- This procedure is used by the UI to retrieve the tracer token history.
-- Calculates and returns a list of the subscribers, the distribution_latency,
-- subscriber_latency and the overall_latency. NULL is used for any latency
-- statistic that can not be calculated due to insufficient data (such as
-- a tracer record not having arrived at the subscriber). Values are in seconds.
--
-- Parameters:
-- @publisher sysname
-- @publisher_db sysname
-- @publication sysname
-- @tracer_id varchar(22) lsn of the tracer record to return history for
--
--
-- Returns:
-- 0 - succeeded
-- 1 - failed
--
-- Result:
-- (see description)
--
-- Security:
-- DBO of distribution db (used by the UI)
-- Requires Certificate signature for catalog access
--
create procedure sys.sp_MShelptracertokenhistory
(
@publisher sysname,
@publisher_db sysname,
@publication sysname,
@tracer_id int
)
as
begin
declare @retcode int,
@publication_id int,
@distributor_latency bigint,
@subscriber_latency bigint,
@overall_latency bigint,
@publisher_commit datetime,
@distributor_commit datetime,
@subscriber sysname,
@subscriber_db sysname,
@subscriber_commit datetime,
@distribution_db sysname

declare @tracer_history table(distributor_latency bigint, subscriber sysname, subscriber_db sysname, subscriber_latency bigint, overall_latency bigint)

select @distribution_db = db_name()

--
-- security check.
-- User must be member of 'replmonitor' role in distribution database.
--
exec @retcode = sys.sp_MSrepl_DistributorReplMonitorAccess @distribution_db = @distribution_db
if @retcode != 0 or @@error != 0
return 1

-- Has to be executed from distribution database
if sys.fn_MSrepl_isdistdb (@distribution_db) != 1
begin
-- sp_MSispeertopeeragent can only be executed in the distribution database.
raiserror (21482, 16, -1, 'sp_MShelptracertokens', 'distribution')
return 1
end

-- check to see if the publication exists
select @publication_id = msp.publication_id
from dbo.MSpublications msp
join sys.servers ss
on msp.publisher_id = ss.server_id
where UPPER(ss.name) = UPPER(@publisher)
and msp.publisher_db = @publisher_db
and msp.publication = @publication

if @publication_id is NULL
begin
-- Publication 'publication' does not exist.
raiserror(21200, 16, -1, @publication)
return 1
end

select @publisher_commit = publisher_commit,
@distributor_commit = distributor_commit
from MStracer_tokens
where publication_id = @publication_id
and tracer_id = @tracer_id

-- the publisher commit should never be null
-- if we have a valid parent tracer LSN
if @publisher_commit is NULL
begin
-- The tracer token id (%d) could not be found for publisher:%s, database:%s, publication:%s.
raiserror(20688, 16, -1, @tracer_id, @publisher, @publisher_db, @publication)
return 1
end

-- calculate the distributor latency
select @distributor_latency = datediff(ss, @publisher_commit, @distributor_commit)

-- gather remaining latency information
declare tracer_history_cursor cursor fast_forward for
select distinct syss.name,
msda.subscriber_db,
msth.subscriber_commit
from MStracer_history msth
join MSdistribution_agents msda
on msth.agent_id = msda.id
join sys.servers syss
on msda.subscriber_id = syss.server_id
where msth.parent_tracer_id = @tracer_id

open tracer_history_cursor

fetch next from tracer_history_cursor into @subscriber, @subscriber_db, @subscriber_commit
while (@@fetch_status <> -1)
begin
-- calculate the subscriber latency
select @subscriber_latency = datediff(ss, @distributor_commit, @subscriber_commit)

-- calculate the overall latency
select @overall_latency = @distributor_latency + @subscriber_latency

-- add history row to the temp table
insert into @tracer_history
(
distributor_latency,
subscriber,
subscriber_db,
subscriber_latency,
overall_latency
)
values
(
@distributor_latency,
@subscriber,
@subscriber_db,
@subscriber_latency,
@overall_latency
)

fetch next from tracer_history_cursor into @subscriber, @subscriber_db, @subscriber_commit
end

close tracer_history_cursor
deallocate tracer_history_cursor

-- return the tracer history information
select distributor_latency,
subscriber,
subscriber_db,
subscriber_latency,
overall_latency
from @tracer_history
order by overall_latency desc,
subscriber_latency desc,
distributor_latency desc

return 0
end

No comments:

Post a Comment

Total Pageviews