理解TimesTen中兩個CheckPoint檔案與Trans Log的關係及其原理

tangyunoracle發表於2015-03-13

我們先看下面幾個我們實際工作中經常會遇到的問題:

1Times Ten為什麼有兩個CheckPoint檔案?

2、兩個CheckPoint檔案之間是什麼關係?

3、兩個CheckPoint檔案與Trans Log之間是什麼關係?

4TimesTen是怎麼維護兩個CheckPoint檔案和Trans Log的?

5、如果CheckPoint檔案刪除或者損壞會不會當機?

6、只有CheckPoint檔案和Trans Log是否可以對記憶體資料庫進行恢復?

 

一、TimesTen的記憶體結構

        為了很好的理解TimesTen中兩個CheckPoint檔案與Trans Log的關係及其原理,我們先簡單介紹一下TimesTen的記憶體結構。


1Data Store,資料庫所有資料的儲存區域;載入所有資料的記憶體區域,TimesTen的所有的運算都是在Data Store中完成的。
2
、日誌快取(LogBuffer),用於暫時儲存記錄Data Store變更的日誌緩衝區,由後臺程式將緩衝區的資料寫入Trans Log、複製程式實時複製到目標端。

3、臨時資料區域,臨時儲存執行計劃等資料的共享區域,排序等操作臨時使用。

4、檢查點(CheckPoint)檔案,儲存了兩個互為備份的DsName.ds0DsName.ds1,可以理解為Oracle的資料檔案,是記憶體資料庫儲存於磁碟的資料映象。

5、事務日誌(Transation Log)檔案DsName.logNNN,儲存Data Store的資料變更到物理裝置。

從記憶體結構圖可以看到,TimesTen包含兩個檢查點(CheckPoint)檔案,常常都會有客戶或者同事會問到為什麼會存在兩個檢查點(CheckPoint)檔案,這兩個檔案之間是什麼關係?其實這兩個檔案的關係很簡單,就是互為映象。

 本文只針對上圖兩個CheckPoint檔案之間的關係及兩個CheckPoint檔案與Trans Log之間的關係進行詳述。

 

二、通過CheckPoint檔案和Trans Log恢復

       在實際運維中,雖然我們幾乎不會出現採用CheckPoint檔案和Trans Log進行恢復,這裡為了很好的理解TimesTen中兩個CheckPoint檔案與Trans Log的關係及其原理,所以簡單做一個通過CheckPoint檔案和Trans Log恢復TimesTen記憶體資料庫的實驗。

下面是一個正常執行的DSN

$ ttadmin -ramload tyinfo

RAM Residence Policy            : manual

Manually Loaded In RAM          : True

Replication Agent Policy        : manual

Replication Manually Started    : False

Cache Agent Policy              : manual

Cache Agent Manually Started    : False

$ ttstatus

TimesTen status report as of Thu Mar  5 20:46:19 2015

 

Daemon pid 3171 port 53396 instance tt1122

TimesTen server pid 3182 started on port 53397

------------------------------------------------------------------------

Data store /ttchk/tt1122/DataStore/TYINFO/tyinfodata

There are 11 connections to the data store

Shared Memory KEY 0x0601f765 ID 32769

Type            PID     Context             Connection Name              ConnID

Subdaemon       3177    0x0000000008641d00  Manager                         127

Subdaemon       3177    0x00000000086b8df0  Rollback                        126

Subdaemon       3177    0x0000000008766fd0  Flusher                         125

Subdaemon       3177    0x00000000087bbe80  Monitor                         124

Subdaemon       3177    0x0000000008831520  AsyncMV                         119

Subdaemon       3177    0x0000000008856950  Deadlock Detector               123

Subdaemon       3177    0x000000000886b5a0  HistGC                          120

Subdaemon       3177    0x00000000088801f0  Log Marker                      118

Subdaemon       3177    0x00002aaac00008c0  Aging                           122

Subdaemon       3177    0x00002aaac40008c0  IndexGC                         121

Subdaemon       3177    0x00002aaac4015510  Checkpoint                      117

RAM residence policy: Manual

Data store is manually loaded into RAM

Replication policy  : Manual

Cache Agent policy  : Manual

------------------------------------------------------------------------

Accessible by group timesten

End of report

此時採用模擬例項crash的方式模擬通過CheckPoint檔案和Trans Log恢復TimesTen記憶體資料庫。

1、模式例項crash

$ kill -9 3171

[timesten@tony5 ~]$ ttstatus

ttStatus: Could not connect to the TimesTen daemon.

If the TimesTen daemon is not running, please start it

by running "ttDaemonAdmin -start".

2、備份CheckPoint檔案和Trans Log檔案

$ ls -l

drwxrwxr-x 2 timesten timesten 4096 Jan 20 09:29 TYINFO

[timesten@tony5 DataStore]$ cp -r TYINFO TYINFO2

[timesten@tony5 DataStore]$ ls -l

total 8

drwxrwxr-x 2 timesten timesten 4096 Jan 20 09:29 TYINFO

drwxrwxr-x 2 timesten timesten 4096 Mar  5 20:59 TYINFO2

[timesten@tony5 DataStore]$ cd TYINFO2

[timesten@tony5 TYINFO2]$ ls

tyinfodata.ds0  tyinfodata.ds1  tyinfodata.inval

 

$ ls -l

total 397932

-rw-rw---- 1 timesten timesten   4411392 Mar  5 20:46 tyinfodata.log0

-rw-rw---- 1 timesten timesten 134217728 Dec 18 15:20 tyinfodata.res0

-rw-rw---- 1 timesten timesten 134217728 Dec 18 15:20 tyinfodata.res1

-rw-rw---- 1 timesten timesten 134217728 Dec 18 15:20 tyinfodata.res2

[timesten@tony5 TYINFO]$ cp tyinfodata.log0 tyinfodata.log0_b

[timesten@tony5 TYINFO]$ ls -l

total 402252

-rw-rw---- 1 timesten timesten   4411392 Mar  5 20:46 tyinfodata.log0

-rw-rw---- 1 timesten timesten   4411392 Mar  5 21:02 tyinfodata.log0_b

-rw-rw---- 1 timesten timesten 134217728 Dec 18 15:20 tyinfodata.res0

-rw-rw---- 1 timesten timesten 134217728 Dec 18 15:20 tyinfodata.res1

-rw-rw---- 1 timesten timesten 134217728 Dec 18 15:20 tyinfodata.res2


3、啟動Daemon程式並刪除DSN

$ ttDaemonAdmin -start -force

/TimesTen/tt1122/info/timestend.pid file exists, attempt start due to -force option.

TimesTen Daemon startup OK.

[timesten@tony5 TYINFO]$ ttdestroy tyinfo

 

4、新建新的同名DSN,並在停止Daemon程式狀態替換原來的CheckPoint檔案和Trans Log檔案

$ ttisql tyinfo

 

Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.

Type ? or "help" for help, type "exit" to quit ttIsql.

connect "DSN=tyinfo";

Connection successful: DSN=TYINFO;UID=timesten;DataStore=/ttchk/tt1122/DataStore/TYINFO/tyinfodata;DatabaseCharacterSet=ZHS16GBK;ConnectionCharacterSet=ZHS16GBK;LogFileSize=128;DRIVER=/TimesTen/tt1122/lib/libtten.so;LogDir=/ttlog/tt1122/TYINFO;PermSize=128;TempSize=64;Connections=80;CkptFrequency=600;RecoveryThreads=4;TypeMode=0;PLSQL=0;CacheGridEnable=0;LogBufMB=64;ReceiverThreads=1;

(Default setting AutoCommit=1)

Command> exit

Disconnecting...

Done.

[timesten@tony5 ~]$ ttdaemonadmin -stop

TimesTen Daemon stopped.

$ cd /ttchk/tt1122/DataStore/

[timesten@tony5 DataStore]$ ls

TYINFO  TYINFO2

[timesten@tony5 DataStore]$ rm -rf TYINFO

[timesten@tony5 DataStore]$ mv TYINFO2 TYINFO

[timesten@tony5 DataStore]$ ls

TYINFO

$ cd /ttlog/tt1122/TYINFO/

[timesten@tony5 TYINFO]$ ls

tyinfodata.log0  tyinfodata.log0_b  tyinfodata.res0  tyinfodata.res1  tyinfodata.res2

[timesten@tony5 TYINFO]$ rm tyinfodata.log0

[timesten@tony5 TYINFO]$ mv tyinfodata.log0_b tyinfodata.log0

5、正常啟動DSN即可,完成通過CheckPoint檔案和Trans Log恢復TimesTen記憶體資料庫

$ ttdaemonadmin -start

TimesTen Daemon startup OK.

[timesten@tony5 TYINFO]$ ttadmin -rampolicy manual tyinfo

RAM Residence Policy            : manual

Manually Loaded In RAM          : False

Replication Agent Policy        : manual

Replication Manually Started    : False

Cache Agent Policy              : manual

Cache Agent Manually Started    : False

[timesten@tony5 TYINFO]$ ttadmin -ramload tyinfo

RAM Residence Policy            : manual

Manually Loaded In RAM          : True

Replication Agent Policy        : manual

Replication Manually Started    : False

Cache Agent Policy              : manual

Cache Agent Manually Started    : False

[timesten@tony5 TYINFO]$ ttstatus

TimesTen status report as of Thu Mar  5 23:35:06 2015

 

Daemon pid 16174 port 53396 instance tt1122

TimesTen server pid 16185 started on port 53397

------------------------------------------------------------------------

Data store /ttchk/tt1122/DataStore/TYINFO/tyinfodata

There are 11 connections to the data store

Shared Memory KEY 0x0401f765 ID 163841

Type            PID     Context             Connection Name              ConnID

Subdaemon       16181   0x0000000002476d00  Manager                         127

Subdaemon       16181   0x00000000024cdba0  Rollback                        126

Subdaemon       16181   0x000000000259bfc0  Flusher                         125

Subdaemon       16181   0x0000000002611660  Monitor                         124

Subdaemon       16181   0x0000000002666510  Deadlock Detector               123

Subdaemon       16181   0x00000000026bb3c0  Checkpoint                      122

Subdaemon       16181   0x0000000002710270  Aging                           121

Subdaemon       16181   0x0000000002765120  Log Marker                      120

Subdaemon       16181   0x00000000027b9fd0  AsyncMV                         119

Subdaemon       16181   0x000000000280ee80  HistGC                          118

Subdaemon       16181   0x0000000002863d30  IndexGC                         117

RAM residence policy: Manual

Data store is manually loaded into RAM

Replication policy  : Manual

Cache Agent policy  : Manual

------------------------------------------------------------------------

Accessible by group timesten

End of report

[timesten@tony5 TYINFO]$ 

測試時可以模擬在原庫中新建物件,插入資料等,恢復後對比恢復前後資料一致性。

三、兩個CheckPoint檔案與Trans Log的關係及原理

      TimesTen為什麼會有兩個CheckPoint檔案,原因其實很簡單,互為映象,至於為什麼互為映象但是又不能存放到不同的路徑/儲存,這屬於設計方面的問題,這裡不做討論。

      為了直觀的說明兩個CheckPoint檔案與Trans Log的關係及原理,我把CKPT的過程抽象成下面圖表,分別對建立DSN開始,到進行4CKPT進行描述。

建立DSN:首次建立DSN時,TimesTen會生成DSName.log0和DSName.ds0和DSName.ds1三個檔案,其中DSName.logN是一個順序遞增的檔名。為了下面描述更加直觀,我們假設每一次CheckPoint時產生2個事務日誌(Trans Log)檔案。

第1次CKPT:由於每次產生兩個Trans Log檔案,所以第一次CheckPoint時,將DSName.log0/1/2三個事務日誌的變化寫入檢查點檔案DSName.ds0,檢查點檔案DSName.ds1不變。由於DSName.log0/1/2三個事務日誌尚未寫入檢查點檔案DSName.ds1,所以此時不會刪除任何事務日誌檔案。

第2次CKPT:第二次CheckPoint時,會將DSName.log0/1/2/3/4五個事務日誌的變化寫入檢查點檔案DSName.ds1,檢查點檔案DSName.ds0不變。由於DSName.log0/1/2三個事務日誌已經寫入檢查點檔案DSName.ds0和DSName.ds1,所以此時SubDaemon程式會觸發刪除DSName.log0/1/2事務日誌檔案。

第3次CKPT:第三次CheckPoint時,會將DSName.log3/4/5/6四個事務日誌的變化寫入檢查點檔案DSName.ds0,檢查點檔案DSName.ds1不變。由於DSName.log3/4兩個事務日誌已經寫入檢查點檔案DSName.ds1和DSName.ds0,所以此時SubDaemon程式會觸發刪除DSName.log3/4事務日誌檔案。

第4次CKPT:第三次CheckPoint時,會將DSName.log5/6/7/8四個事務日誌的變化寫入檢查點檔案DSName.ds1,檢查點檔案DSName.ds0不變。由於DSName.log5/6兩個事務日誌已經寫入檢查點檔案DSName.ds0和DSName.ds1,所以此時SubDaemon程式會觸發刪除DSName.log5/6事務日誌檔案。後面的CheckPoint對檢查點檔案的寫入和Trans Log事務日誌的清理操作以此類推。

   通過以上圖示對兩個CheckPoint檔案與Trans Log的關係及原理進行描述,為了再次驗證兩個CheckPoint檔案的映象作用,我們通過手動模擬CheckPoint檔案被誤刪除及損壞的場景。

四、模擬CheckPoint檔案丟失或損壞

1、模擬1個檢查點檔案丟失

$ ttstatus

TimesTen status report as of Fri Mar  6 07:03:14 2015

Daemon pid 3138 port 53396 instance tt1122

TimesTen server pid 3156 started on port 53397

------------------------------------------------------------------------

Data store /ttchk/tt1122/DataStore/TYINFO/tyinfodata

There are 11 connections to the data store

Shared Memory KEY 0x0601f765 ID 32769

Type            PID     Context             Connection Name              ConnID

Subdaemon       3144    0x000000001bbfed00  Manager                         127

Subdaemon       3144    0x000000001bc55ba0  Rollback                        126

Subdaemon       3144    0x000000001bd23fd0  Flusher                         125

Subdaemon       3144    0x000000001bd99670  Monitor                         124

Subdaemon       3144    0x000000001bdee520  Deadlock Detector               123

Subdaemon       3144    0x000000001be433d0  HistGC                          119

Subdaemon       3144    0x00002aaac00008c0  Checkpoint                      122

Subdaemon       3144    0x00002aaac0015510  Aging                           121

Subdaemon       3144    0x00002aaac40008c0  Log Marker                      120

Subdaemon       3144    0x00002aaac4015510  AsyncMV                         118

Subdaemon       3144    0x00002aaac80008c0  IndexGC                         117

RAM residence policy: Manual

Data store is manually loaded into RAM

Replication policy  : Manual

Cache Agent policy  : Manual

------------------------------------------------------------------------

Accessible by group timesten

End of report

[timesten@tony5 TYINFO]$ ls

tyinfodata.ds0  tyinfodata.ds1  tyinfodata.inval

[timesten@tony5 TYINFO]$ rm tyinfodata.ds0

[timesten@tony5 TYINFO]$ ls

tyinfodata.ds1  tyinfodata.inval

[timesten@tony5 TYINFO]$ ttisql tyinfo

Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.

Type ? or "help" for help, type "exit" to quit ttIsql.

connect "DSN=tyinfo";

Connection successful: DSN=TYINFO;UID=timesten;DataStore=/ttchk/tt1122/DataStore/TYINFO/tyinfodata;DatabaseCharacterSet=ZHS16GBK;ConnectionCharacterSet=ZHS16GBK;LogFileSize=128;DRIVER=/TimesTen/tt1122/lib/libtten.so;LogDir=/ttlog/tt1122/TYINFO;PermSize=128;TempSize=64;Connections=80;CkptFrequency=600;RecoveryThreads=4;TypeMode=0;PLSQL=0;CacheGridEnable=0;LogBufMB=64;ReceiverThreads=1;

(Default setting AutoCommit=1)

Command> call ttckpt;

Command> exit

Disconnecting...

Done.

[timesten@tony5 TYINFO]$ ls

tyinfodata.ds0  tyinfodata.ds1  tyinfodata.inval

[timesten@tony5 TYINFO]$ 

通過上面模擬可以看到,當出現一個檢查點檔案丟失,TimesTen不會crash,並且在下一次CheckPoint時會自動生成(恢復)新的檢查點檔案。

2、模擬2個檢查點檔案丟失

$ ls

tyinfodata.ds0  tyinfodata.ds1  tyinfodata.inval

[timesten@tony5 TYINFO]$ rm tyinfodata.ds*

[timesten@tony5 TYINFO]$ ls

tyinfodata.inval

[timesten@tony5 TYINFO]$ ttstatus

TimesTen status report as of Fri Mar  6 07:07:04 2015

Daemon pid 3138 port 53396 instance tt1122

TimesTen server pid 3156 started on port 53397

------------------------------------------------------------------------

Data store /ttchk/tt1122/DataStore/TYINFO/tyinfodata

There are 11 connections to the data store

Shared Memory KEY 0x0601f765 ID 32769

Type            PID     Context             Connection Name              ConnID

Subdaemon       3144    0x000000001bbfed00  Manager                         127

Subdaemon       3144    0x000000001bc55ba0  Rollback                        126

Subdaemon       3144    0x000000001bd23fd0  Flusher                         125

Subdaemon       3144    0x000000001bd99670  Monitor                         124

Subdaemon       3144    0x000000001bdee520  Deadlock Detector               123

Subdaemon       3144    0x000000001be433d0  HistGC                          119

Subdaemon       3144    0x00002aaac00008c0  Checkpoint                      122

Subdaemon       3144    0x00002aaac0015510  Aging                           121

Subdaemon       3144    0x00002aaac40008c0  Log Marker                      120

Subdaemon       3144    0x00002aaac4015510  AsyncMV                         118

Subdaemon       3144    0x00002aaac80008c0  IndexGC                         117

RAM residence policy: Manual

Data store is manually loaded into RAM

Replication policy  : Manual

Cache Agent policy  : Manual

------------------------------------------------------------------------

Accessible by group timesten

End of report

[timesten@tony5 TYINFO]$ ttisql tyinfo

Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.

Type ? or "help" for help, type "exit" to quit ttIsql.

connect "DSN=tyinfo";

Connection successful: DSN=TYINFO;UID=timesten;DataStore=/ttchk/tt1122/DataStore/TYINFO/tyinfodata;DatabaseCharacterSet=ZHS16GBK;ConnectionCharacterSet=ZHS16GBK;LogFileSize=128;DRIVER=/TimesTen/tt1122/lib/libtten.so;LogDir=/ttlog/tt1122/TYINFO;PermSize=128;TempSize=64;Connections=80;CkptFrequency=600;RecoveryThreads=4;TypeMode=0;PLSQL=0;CacheGridEnable=0;LogBufMB=64;ReceiverThreads=1;

(Default setting AutoCommit=1)

Command> call ttckpt;

Command> exit

Disconnecting...

Done.

[timesten@tony5 TYINFO]$ ls

tyinfodata.ds1  tyinfodata.inval

[timesten@tony5 TYINFO]$ ttisql tyinfo

Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.

Type ? or "help" for help, type "exit" to quit ttIsql.

connect "DSN=tyinfo";

Connection successful: DSN=TYINFO;UID=timesten;DataStore=/ttchk/tt1122/DataStore/TYINFO/tyinfodata;DatabaseCharacterSet=ZHS16GBK;ConnectionCharacterSet=ZHS16GBK;LogFileSize=128;DRIVER=/TimesTen/tt1122/lib/libtten.so;LogDir=/ttlog/tt1122/TYINFO;PermSize=128;TempSize=64;Connections=80;CkptFrequency=600;RecoveryThreads=4;TypeMode=0;PLSQL=0;CacheGridEnable=0;LogBufMB=64;ReceiverThreads=1;

(Default setting AutoCommit=1)

Command> call ttckpt;

Command> exit

Disconnecting...

Done.

[timesten@tony5 TYINFO]$ ls

tyinfodata.ds0  tyinfodata.ds1  tyinfodata.inval

通過上面的測試可以看到,當出現兩個檢查點檔案丟失,TimesTen不會crash,並且在兩次CheckPoint時會自動生成(恢復)新的檢查點檔案。

3、模擬檢查點檔案損壞

由於要模擬檢查點檔案損壞較為困難,所以這裡不模擬;當我們發現檢查點檔案損壞,可以通過轉換成上面檢查點檔案丟失的場景,直接將損壞的檢查點檔案刪除,並手動做兩次CheckPoint使檢查點檔案自動生成;當然這樣會耗費大量的IO資源。

 

Created by Tony.Tang[TangYun]2015.03

-----------------End-----------------

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24930246/viewspace-1457890/,如需轉載,請註明出處,否則將追究法律責任。

相關文章