5.4.5 log::write



next up previous
Next: 5.4.6 log::read Up: 5.4 class log Previous: 5.4.4 log::~log

5.4.5 log::write

 

The write method first checks if the log record supplied is beyond the maximum size allowed. If not, it executes global_shm_log_ptr->lock() because it extensively accesses and modifies the shared memory structure. It then computes the total number of pages that the log record would needgif and the number of pages available in the log. The current size of the log is defined as the number of pages between the first LSN ( global_shm_log_ptr->sl_firstlan) and the current last LSN ( global_shm_log_ptr->sl_nextlsn) inclusive. For normal log writes [those writes that do not correspond to transaction aborts (CLRs)] a check is made to verify that writing the log record will not cause the log size to exceed half its maximum size. For CLRs, a check if made against the maximum size of the log. That way, if a transaction decides to abort, there will be enough log space to write out all the CLRs.

If the check for log size fails, the recovery manager is called to compute a more optimistic value of first LSN and the check is repeated. If it fails this time, the log write is failed.

It is now known that the log record will fit into the log. Therefore, it is assigned an LSN. The LSN of the log record is the current value of global_shm_log_ptr->sl_nextlsn.

Normally, a small log record will fit into the log tail. This is the simplest case. The log record is copied into the log tail and the last LSN is updated. If the log record is too big to fit a page, then whatever fits is copied into the log tail, the log tail is flushed, and the remaining portion is written in a similar manner. Thus, the write method will work for log records that span many pages.

Unfortunately, the procedure outlined above is not foolproof. Suppose a log record spans more than a page. Assume that the first write and flush succeeded. Suppose that during the second write, a power failure occurred. Now the log is left inconsistent. During restart, the end of log will not be detected properly (see Section 5.4.6) and the read may return incorrect data. One way of detecting corruption is to end every log record with a magic number. If we do not detect the magic number at the end of the record read, we know for sure that the corresponding write failed midway. However, if we do detect the magic number, we still cannot be sure that there was no corruption. Hence, using this method of detecting corruption is not guaranteed to work.

When the entire log record is written, write executes global_shm_log_ptr->unlock() and returns. The log record that was supplied as a parameter to write will now contain the LSN field filled in.



next up previous
Next: 5.4.6 log::read Up: 5.4 class log Previous: 5.4.4 log::~log



ajitk@cs.wisc.edu, cjin@cs.wisc.edu