SQL Server 环形缓冲区(Ring Buffer) -- 环形缓冲在AlwaysOn的应用
可以从SQL Server环形缓冲区得到一些诊断AlwaysOn的信息,或从sys.dm_os_ring_buffers动态管理视图。环形缓冲在SQL Server启动的时候创建,在SQL Server系统内记录告警用于内部诊断。它们不被支持,但你仍能从中获取有用的信息。下面的查询能从AlwaysON环形缓冲获取所有的事件记录。
SELECT * FROM sys.dm_os_ring_buffers WHERE ring_buffer_type LIKE ‘%HADR%‘
为了让数据更好管理,通过日期和环形缓冲类型来过滤数据。下面的查询获取今天特定的环形缓冲中的记录。
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>‘
在每行的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
AlwaysOn环形缓冲类型
有4种类型的环形缓冲在sys.dm_os_ring_buffers里:
RING_BUFFER_HADRDBMGR_API – 记录已经发生或者正在发生的状态变化。当关注状态变化时,集中注意objectType的值。
RING_BUFFER_HADRDBMGR_STATE – 记录AlwaysOn活动的内部方法和函数调用。可以从中看到例如suspend、resume或role changes,包括所有的入口和出口。
RING_BUFFER_HADRDBMGR_COMMIT
RING_BUFFER_HADR_TRANSPORT_STATE
时间: 2024-10-10 21:32:13