[20170309]關於線上日誌與歸檔1.txt

lfree發表於2017-03-10

[20170309]關於線上日誌與歸檔1.txt

--//當日志寫滿了,或者執行手工了切換,再或者rman備份時有時也會觸發日誌切換:
alter system switch logfile ;
alter system archive log current ;

--//本文簡單探究日誌歸檔是如何儲存的.先探查os塊.

1.環境:
--//啟動到mount狀態.

SYS@book> @ &r/ver
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

SYS@book> @ &r/logfile
GROUP# STATUS     TYPE       MEMBER                          IS_ GROUP# THREAD# SEQUENCE#       BYTES BLOCKSIZE MEMBERS ARC STATUS     FIRST_CHANGE# FIRST_TIME          NEXT_CHANGE# NEXT_TIME
------ ---------- ---------- ------------------------------- --- ------ ------- --------- ----------- --------- ------- --- ---------- ------------- ------------------- ------------ -------------------
     1            ONLINE     /mnt/ramdisk/book/redo01.log    NO       1       1       695    52428800       512       1 YES INACTIVE     13276910949 2017-02-28 14:40:12  13276931102 2017-03-09 10:01:48
     2            ONLINE     /mnt/ramdisk/book/redo02.log    NO       2       1       696    52428800       512       1 YES INACTIVE     13276931102 2017-03-09 10:01:48  13276931986 2017-03-09 10:02:36
     3            ONLINE     /mnt/ramdisk/book/redo03.log    NO       3       1       697    52428800       512       1 NO  CURRENT      13276931986 2017-03-09 10:02:36 2.814750E+14
     4            STANDBY    /mnt/ramdisk/book/redostb01.log NO
     5            STANDBY    /mnt/ramdisk/book/redostb02.log NO
     6            STANDBY    /mnt/ramdisk/book/redostb03.log NO
     7            STANDBY    /mnt/ramdisk/book/redostb04.log NO
7 rows selected.

--//僅僅拿seq#=696 來研究.

$ ls -l /mnt/ramdisk/book/redo02.log /u01/app/oracle/archivelog/book/1_696_896605872.dbf
-rw-r----- 1 oracle oinstall 52429312 2017-03-09 10:02:36 /mnt/ramdisk/book/redo02.log
-rw-r----- 1 oracle oinstall  1626112 2017-03-09 10:02:38 /u01/app/oracle/archivelog/book/1_696_896605872.dbf

--//可以發現2者大小不一樣.你可以觀察基本不會寫滿50M,大約4XM就會發生日誌切換.
--//我以前oracle在歸檔時會刪除一些類似空洞的空間,從而減少歸檔大小.拿這兩個檔案做一些簡單探究:

2.首先探究日誌檔案的OS塊:
--//oracle不管資料檔案還是日誌檔案,第0塊都是OS塊,大小與塊大小有關,日誌一般塊大小512位元組.
--//所以從os角度看,建立50M的日誌檔案,實際大小是50M+512位元組.
--//首先檢查是否可以倍512整除.
52429312/512=102401 = 0x19001 (不包括OS塊 是 0x19000)
1626112/512= 3176 = 0xc68    (不包括OS塊  是 0xc67)

--然後對比前面的位元組是否一樣.
$ dd if=/mnt/ramdisk/book/redo02.log bs=512 count=3176 2>/dev/null | md5sum
812745bc5d7da07bcfa11e578b949226  -

$ md5sum /u01/app/oracle/archivelog/book/1_696_896605872.dbf
3e197986a3dfd1c392e052a14400d4c1  /u01/app/oracle/archivelog/book/1_696_896605872.dbf
--//可以發現不一樣.也就是oracle日誌轉儲歸檔時,不是簡單的複製操作.

$ dd if=/mnt/ramdisk/book/redo02.log bs=512 count=1  | xxd -c 16 >| r0.txt
$ dd if=/u01/app/oracle/archivelog/book/1_696_896605872.dbf bs=512 count=1 | xxd -c 16 > d0.txt

$ diff  r0.txt d0.txt
2c2
< 0000010: 67c8 0000 0002 0000 0090 0100 7d7c 7b7a  g?.........}|{z
---
> 0000010: 0154 0000 0002 0000 670c 0000 7d7c 7b7a  .T......g...}|{z

--// 7d7c7b7a 有看到熟悉的東西,資料檔案的OS塊也能看到一樣的資訊.}
--//你可以看出偏移0x18~0x1B處 線上日誌是0090 0100 =>如果4個位元組顛倒 00019000 這裡就是日誌塊數量(不包括OS塊).
                              備用日誌是670c 0000 =>如果4個位元組顛倒 00000c67 這裡就是日誌塊數量(不包括OS塊).
--//與前面的計算數量一致.

--//但是前面的67c8 ,0154 表示什麼呢? 很自然的猜想檢查和(因為資料庫(8k資料塊大小),這個位置就是檢查和.例子:
BBED> p dba 4,135 chkval_kcbh
ub2 chkval_kcbh                             @16       0xe273

--//可以猜測這個位置也是檢查和.做一個證明看看.

3.取出計算:
$ cut -c10-50 r0.txt >| aa1.txt
...
0000
0000
xor result: 0

$ cut -c10-50 d0.txt >| aa1.txt
0000
0000
xor result: 0

--//異或的結果都是0,說明oracle在歸檔時OS塊在偏移0x18~0x1B處寫入塊歸檔的數量(不包括OS塊),然後在0x10-0x11處計算檢查和.

--//轉載一個連結,我認為分析比較仔細的:

在ue開啟的16進位制中每行是16bytes,一共32行,這為第一個塊file header block.這個block包含的資訊有限,逐一分析:

$ bvi80 -s 512 -b 0 /mnt/ramdisk/book/redo02.log
00000000  00 22 00 00 00 00 C0 FF 00 00 00 00 00 00 00 00 ................
00000010  67 C8 00 00 00 02 00 00 00 90 01 00 7D 7C 7B 7A g...........}|{z
00000020  A0 81 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000030  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000040  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000050  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000060  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000070  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000080  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000090  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000000A0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000000B0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000000C0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000000D0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000000E0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000000F0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000100  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000110  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000120  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000130  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000140  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000150  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000160  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000170  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000180  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000190  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000001A0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000001B0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000001C0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000001D0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000001E0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000001F0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000200
--//注:這裡是我測試機器上的redo檔案}

從第一個位元組開始到第二個位元組為0x22,這2個位元組通常為oracle 檔案型別的標識,像資料檔案的開頭這裡就為0xA2,這裡的0x22就代表
redo log file.接著下來第一行就沒有有意義的東西了其中的0xFFC0應該和scn中的base部分有關.再看看第二行的16個位元組,可以發現在
第21和22位元組0x0200換算成10進位制格式則為512,這裡即代表block size.從第25到28位元組0x00019000換算成10進位制後為102400,而
0x00019000代表redo block的數量,這裡即為代表這個file的blocks或者理解為這個file的size.而在後續跟著的0x7a7b7c7d為檔案識別符號
,為oracle快速識別檔案的一種標識.到這之後第一個塊的資訊就沒啦!

--//實際上如果後面00的地方修改任意字元,使用
SYS@book> alter system dump logfile '/u01/app/oracle/archivelog/book/1_696_896605872.dbf' validate;
System altered.

--//oracle並不報錯.

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

相關文章