Analyzing the States of Checkpoint File Pairs
SQL Server persists data from durable memory-optimized tables in checkpoint file pairs. This appendix demonstrates how to analyze their states using the sys.db_dm_xtp_checkpoint_files view and shows the state transitions through the CFP lifetime.
Sys.db_dm_xtp_checkpoint_files View
The sys.db_dm_xtp_checkpoint_files view provides information about database checkpoint files, including their state, size, and physical location. We will use this view extensively in this appendix. Let’s look at the most important columns.
Let’s use this view and analyze the state transitions of the checkpoint file pairs.
The Lifetime of Checkpoint File Pairs
As the first step in this test, let’s enable the undocumented trace flag TF9851 using the DBCC TRACEON(9851,-1) command. This trace flag disables the automatic merge process, which will allow you to have more control over your test environment.
Important Do not set TF9851 in production.
Let’s create the database with an In-Memory OLTP file group and perform the full backup starting the backup chain, as shown in Listing C-1. I am doing it in the test environment and not following best practices (such as placing In-Memory OLTP and on-disk data on different drives, creating secondary file groups for on-disk data, and a few others). Obviously, you should remember to follow best practices when you design your real databases.
The database is currently empty and, therefore, it does not have any checkpoint file pairs created. You can confirm this by querying the sys.dm_db_xtp_checkpoint_files view, as shown in Listing C-2.
Figure C-1 shows that result set is empty and that the sys.dm_db_xtp_checkpoint_files view does not return any data.
Figure C-1. State of checkpoint file pairs after database creation
As the next step, let’s create a durable memory-optimized table, as shown in Listing C-3.
If you check the state of the checkpoint file pairs now and run the code from Listing C-2 again, you will see the output shown in Figure C-2. The total number of files and their size may be different in your environment and will depend on the hardware. My test virtual machine has four vCPU and 8GB of RAM, so I have eight checkpoint file pairs in the PRECREATED state with 16MB data and 1MB delta files. I also have one CFP in the UNDER CONSTRUCTION state.
Figure C-2. State of checkpoint file pairs after creating the durable memory-optimized table
Let’s enlarge the output for the files from the UNDER CONSTRUCTION CFP, as shown in Figure C-3. As you can see, the checkpoint_pair_file_id values reference the checkpoint_file_id of the second file in the pair.
Figure C-3. UNDER CONSTRUCTION checkpoint file pair
Relative_file_path provides the path to the file relative to the FILESTREAM container in the In-Memory OLTP file group. Figure C-4 shows the checkpoint files in the folder on the disk.
Figure C-4. Checkpoint files on disk
Now, populate the dbo.HKData table with 1,000 rows and check the status of the checkpoint files, as shown in Listing C-4. The query filters out the checkpoint file pairs in the PRECREATED state from the output.
As you can see in Figure C-5, SQL Server populates the data file in the UNDER CONSTRUCTION CFP by inserting 1,000 rows there. The NULL value in the lower_bound_tsn column indicates that this CFP covers transactions from the time of database creation. Similarly, the NULL value in the upper_bound_tsn column indicates that this CFP covers current transactions.
Figure C-5. UNDER CONSTRUCTION CFP state after insert
Let’s run a manual CHECKPOINT and check the status of checkpoint file pairs, as shown in Listing C-5.
As you can see in Figure C-6, the CHECKPOINT operation transitions the UNDER CONSTRUCTION CFP to the ACTIVE state. The upper_bound_tsn columns are now populated, indicating the maximum timestamp for transactions covered by the checkpoint file pair.
Figure C-6. The CFP state after CHECKPOINT
Let’s insert another 1,000 rows to the dbo.HKData table and check the status of the CFPs. Listing C-6 shows the code to perform this.
Figure C-7 shows the states of the checkpoint file pairs after the second insert. As you can see, SQL Server creates another CFP in the UNDER CONSTRUCTION state with lower_bound_tsn = 4.
Figure C-7. States of CFPsafter the second INSERT
Another CHECKPOINT would transition the UNDER CONSTRUCTION CFP to the ACTIVE state, as shown in Figure C-8. You can force it by running the code from Listing C-5 again. At this point, you have two ACTIVE checkpoint file pairs covering different ranges of transaction timestamps.
Figure C-8. States of CFPs after second CHECKPOINT
As the next step, let’s delete 66.7% of the rows from the table, as shown in Listing C-7. In this listing, you are also running the query that combines the information about the data and delta files, and demonstrates that both checkpoint file pairs are mostly empty.
As you can see in Figure C-9, both files are just 33.3% full so they are perfect candidates for the merge.
Figure C-9. States after deletion
You can trigger the merge by calling the sys.sp_xtp_merge_checkpoint_files system stored procedure. This procedure requires you to provide the lower and upper bounds for the merge and it does not accept NULL as the parameter value. You can use any tsn, which is covered by the CFP file participating in the merge.
As already discussed, in most cases you can rely on the automatic merge and do not need to call this procedure manually. One of the cases when manual merge is beneficial is the situation when the size of the data in the durable memory-optimized tables is close to 256GB and you want granular control over the merge process, avoiding situations when you do not have enough space in the checkpoint file pairs to store the data. Listing C-8 shows the code that calls the stored procedure.
You can check the status of merge requests by examining the sys.dm_db_xtp_merge_requests view, as shown in Listing C-9. Figure C-10 illustrates the output of the query.
Figure C-10. Merge request status
Note You can read more about the sys.sp_xtp_merge_checkpoint_files stored procedure at https://msdn.microsoft.com/en-us/library/dn198330.aspx. More information about sys.dm_db_xtp_merge_requests is available at https://msdn.microsoft.com/en-us/library/dn465868.aspx.
Figure C-11 illustrates the state of checkpoint file pairs after the merge is initiated. As you can see, SQL Server creates the new checkpoint file pair in the MERGE TARGET state and merges data from ACTIVE CFPs there. You can also see the correlation between the checkpoint_file_id of CFPs with destination_file_id and source_file_id columns in sys.dm_db_xtp_merge_requests view.
Figure C-11. The state of checkpoint file pairs after the merge is initiated
The next CHECKPOINT will transition the checkpoint file pairs that participated in merge from ACTIVE to MERGED SOURCE and from MERGE TARGET CFP to ACTIVE states. Figure C-12 demonstrates this. Now the merge is considered to be complete and the request state value from the sys.dm_db_xtp_merge_requests view is changed to INSTALLED.
Figure C-12. The state of the checkpoint file pairs after the merge is completed
After the next CHECKPOINT, the MERGED SOURCE CFPs will be transitioned to the REQUIRED FOR BACKUP/HA state, as shown in Figure C-13.
Figure C-13. The MERGED SOURCE CFPs are transitioned to the REQUIRED FOR BACKUP/HA state after the next checkpoint
After the transaction log backup is taken, log records are transmitted to secondary nodes, and the checkpoint event occurs, these CFPs are eventually picked up by the garbage collection thread and moved to IN TRANSITION TO TOMBSTONE and TOMBSTONE states and eventually deallocated. You can also force manual garbage collection by calling the sys.sp_xtp_checkpoint_force_garbage_collection stored procedure. Listing C-10 illustrates this.
Note In reality, it could take more than one log backup and checkpoint event to transition CFPs to the IN TRANSITION TO TOMBSTONE state. You can execute the code from Listing C-10 multiple times if it happens in your system.
You can read more about the sys.sp_xtp_checkpoint_force_garbage_collection stored procedure at https://msdn.microsoft.com/en-us/library/dn451428.aspx.
Figure C-14 illustrates CFPs in the TOMBSTONE state. Eventually, they will be cleared from the result sets and deallocated.
Figure C-14. CFPs in the TOMBSTONE state
Summary
Every checkpoint file pair transitions through various states during its lifetime. You can analyze these states using the sys.dm_db_xtp_checkpoint_files data management view. This view returns information about individual checkpoint files, including their type, size, state, number of inserted and deleted rows, and quite a few other properties.
The merge process merges information from the ACTIVE checkpoint file pairs that have a large percent of deleted rows, creating a new CFP. In most cases, you can rely on the automatic merge process; however, you can trigger a manual merge using the sys.sp_xtp_merge_checkpoint_files stored procedure. You can monitor the status of merge requests using the sys.dm_db_xtp_merge_requests view.
Merged checkpoint file pairs should be included in the log backup before they are deallocated. As with the merge, you can rely on the automatic garbage collection process in most cases. However, you can trigger the manual garbage collection process using the sys.sp_xtp_checkpoint_force_garbage_collection stored procedure.
18.117.229.44