使用环形缓冲区获取有关 AlwaysOn 可用性组的运行状况信息

适用于: SQL Server

可从 SQL Server 环形缓冲区或 sys.dm_os_ring_buffers 动态管理视图 (DMV) 获取某些 Always On 可用性组 (AG) 诊断信息。 环形缓冲区是在 SQL Server 启动过程中创建的,记录了 SQL Server 系统内的内部诊断警报。 它们不受支持,但你仍可在排除问题时从其中提取有价值的信息。 当 SQL Server 挂起或崩溃时,这些环形缓冲区可提供另一诊断源。

以下 Transact-SQL (T-SQL) 查询可检索 AG 环形缓冲区中的所有事件记录。

SELECT * FROM sys.dm_os_ring_buffers WHERE ring_buffer_type LIKE '%HADR%'

为使数据更易于管理,请按日期和环形缓冲区类型筛选数据。 下面的查询可从指定的环形缓冲区中检索今天发生的记录。

DECLARE @start_of_today DATETIME,
    @start_of_tomorrow DATETIME;

SET @start_of_today = CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME);
SET @start_of_tomorrow = DATEADD(DAY, 1, @start_of_today);

DECLARE @runtime DATETIME;

SET @runtime = GETDATE();

SELECT CONVERT(VARCHAR(30), @runtime, 121) AS data_collection_runtime,
    DATEADD(ms, - 1 * (inf.ms_ticks - ring.timestamp), GETDATE()) AS ring_buffer_record_time,
    ring.timestamp AS record_timestamp,
    inf.ms_ticks AS cur_timestamp,
    ring.*
FROM sys.dm_os_ring_buffers ring
CROSS JOIN sys.dm_os_sys_info inf
WHERE ring_buffer_type = '<RING_BUFFER_TYPE>'
    AND DATEADD(ms, - 1 * (inf.ms_ticks - ring.timestamp), GETDATE()) >= @start_of_today
    AND DATEADD(ms, - 1 * (inf.ms_ticks - ring.timestamp), GETDATE()) < @start_of_tomorrow;
GO

每个记录中的 record 列都包含 XML 格式的诊断数据。 环形缓冲区类型之间的 XML 数据不同。 有关每个环形缓冲区类型的详细信息,请参阅可用性组环形缓冲区类型。 为使 XML 数据更具可读性,需要自定义 T-SQL 查询,以提取所需 XML 元素。 例如,下面的查询可检索 RING_BUFFER_HADRDBMGR_API 环形缓冲区中的所有事件,并将 XML 数据的格式设置为单独的表列。

WITH hadr (ts, type, record)
AS (
    SELECT timestamp AS ts,
        ring_buffer_type AS type,
        CAST(record AS XML) AS record
    FROM sys.dm_os_ring_buffers
    WHERE ring_buffer_type = 'RING_BUFFER_HADRDBMGR_API'
    )
SELECT ts,
    type,
    record.value('(./Record/@id)[1]', 'bigint') AS [Record ID],
    record.value('(./Record/@time)[1]', 'bigint') AS [Time],
    record.value('(./Record/HadrDbMgrAPI/dbId)[1]', 'bigint') AS [DBID],
    record.value('(/Record/HadrDbMgrAPI/API)[1]', 'varchar(50)') AS [API],
    record.value('(/Record/HadrDbMgrAPI/Action)[1]', 'varchar(50)') AS [Action],
    record.value('(/Record/HadrDbMgrAPI/role)[1]', 'int') AS [Role],
    record.value('(/Record/Stack)[1]', 'varchar(100)') AS [Call Stack]
FROM hadr
ORDER BY record.value('(./Record/@time)[1]', 'bigint') DESC;
GO

可用性组环形缓冲区类型

sys.dm_os_ring_buffers 中有 4 个 AG 环形缓冲区。 以下部分介绍了这些环形缓冲区类型以及每种环形缓冲区类型 record 列的内容示例。

RING_BUFFER_HADRDBMGR_API

记录已发生或正在进行的状态转换。 查看状态转换时,请密切关注 objectType 值。

<Record id="11" type="RING_BUFFER_HADRDBMGR_STATE" time="860243">
  <HadrDbMgrState>
    <objectType>HadrUsers</objectType>
    <currentState>HDbMState_Starting</currentState>
    <proposedState>HDbMState_Started</proposedState>
    <targetState>HDbMState_Started</targetState>
    <legalTransition>Y</legalTransition>
    <role>1</role>
  </HadrDbMgrState>
</Record>

RING_BUFFER_HADRDBMGR_STATE

记录内部方法或 AG 活动执行的函数调用。 可显示挂起、恢复或角色更改等信息,包括入口点和退出点。

<Record id="45" type="RING_BUFFER_HADRDBMGR_STATE" time="1723487912">
  <HadrDbMgrState>
    <dbId>5</dbId>
    <objectType>HadrDbMgr</objectType>
    <currentState>HDbMState_Starting</currentState>
    <proposedState>HDbMState_Started</proposedState>
    <targetState>HDbMState_Started</targetState>
    <legalTransition>Y</legalTransition>
    <role>2</role>
  </HadrDbMgrState>
</Record>

RING_BUFFER_HADRDBMGR_COMMIT

<Record id="0" type="RING_BUFFER_HADRDBMGR_COMMIT" time="1723475368">
  <HadrDbMgrCommitPolicy>
    <dbId>5</dbId>
    <replicaId>883a18f5-97d5-450f-8f8f-9983a4fa5299</replicaId>
    <dbHardenPolicy>KillAll</dbHardenPolicy>
    <dbSyncConfig>0x0</dbSyncConfig>
    <syncPartnerCount>0</syncPartnerCount>
    <minSyncPartnerConfig>0</minSyncPartnerConfig>
    <partnerHardenPolicy>KillAll</partnerHardenPolicy>
    <partnerSyncConfig>0x0</partnerSyncConfig>
    <logBlock>0x0000000000000000</logBlock>
    <leaseExpired>Y</leaseExpired>
    <partnerChange>N</partnerChange>
    <role>2</role>
  </HadrDbMgrCommitPolicy>
</Record>

RING_BUFFER_HADR_TRANSPORT_STATE

<Record id="3" type="RING_BUFFER_HADR_TRANSPORT_STATE" time="1723485399">
  <HadrTransportState>
    <agId>08264B79-D10B-412F-B38D-CA07B08E9BD8</agId>
    <localArId>883A18F5-97D5-450F-8F8F-9983A4FA5299</localArId>
    <targetArId>628D6349-72DD-4D18-A6E1-1272645660BA</targetArId>
    <currentState>HadrSession_Configuring</currentState>
    <targetState>HadrSession_Connected</targetState>
    <legalTransition>Y</legalTransition>
  </HadrTransportState>
</Record>

分析环形缓冲区中的 XML 数据

可通过在查询中使用 value() 方法(xml 数据类型)来分析正在检查的环形缓冲区中的 Record 字段。 要使用此方法,需首先将环形缓冲区中的记录列强制转换为 XML。 例如,下面的查询演示了如何使用此方法将 RING_BUFFER_HADRDBMGR_API 分析为可读格式。

WITH hadr (ts, type, record)
AS (
    SELECT timestamp AS ts,
        ring_buffer_type AS type,
        CAST(record AS XML) AS record
    FROM sys.dm_os_ring_buffers
    WHERE ring_buffer_type = 'RING_BUFFER_HADRDBMGR_API'
    )
SELECT ts,
    type,
    record.value('(./Record/@id)[1]', 'bigint') AS [Record id],
    record.value('(./Record/@time)[1]', 'bigint') AS [Time],
    record.value('(./Record/HadrDbMgrAPI/dbId)[1]', 'bigint') AS [dbid],
    record.value('(/Record/HadrDbMgrAPI/API)[1]', 'varchar(50)') AS [API],
    record.value('(/Record/HadrDbMgrAPI/Action)[1]', 'varchar(50)') AS [Action],
    record.value('(/Record/HadrDbMgrAPI/role)[1]', 'int') AS [Role],
    record.value('(/Record/Stack)[1]', 'varchar(100)') AS [Call Stack]
FROM hadr
ORDER BY record.value('(./Record/@time)[1]', 'bigint') DESC;
GO

后续步骤