⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 t_recoverbadlog.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

			T_RawStoreRow bigrow = new T_RawStoreRow(numcol);
			String string1 = "01234567890123456789"; // 20 char string
			for (int i = 0; i < numcol; i++)
				bigrow.setColumn(i, string1);

			// if overhead is > 80%, then reduce the row size until it fits
			RecordHandle rh = null;
			while(numcol > 0)
			{
				try {
					rh = t_util.t_insert(page, bigrow);
					break;
				} catch (StandardException se) {
					bigrow.setColumn(--numcol, (String) null);
				}
			}
			if (numcol == 0)
				throw T_Fail.testFailMsg("cannot fit any column into the page");

			

			t_util.t_commit(t);

			// make a big log record - update row
			String string2 = "abcdefghijklmnopqrst"; // 20 char string
			for (int i = 0; i < numcol; i++)
				bigrow.setColumn(i, string2);

			c = t_util.t_openContainer(t, 0, cid, true);
			page = t_util.t_getPage(c, ContainerHandle.FIRST_PAGE_NUMBER);

			Page p2 = t_util.t_addPage(c);		// do something so we get the beginXact log
								// record out of the way
			t_util.t_insert(p2, new T_RawStoreRow(REC_001));


			//////////////////////////////////////////////////////////
			// writing approx 3 bytes of log record to the end of the log - 
			// NO MORE LOG RECORD SHOULD BE WRITTEN,
			// Length  3 bytes (4) of log record length
			//////////////////////////////////////////////////////////
			if(!checksumTest)
			{
				SanityManager.DEBUG_SET(LogToFile.TEST_LOG_INCOMPLETE_LOG_WRITE);
				System.getProperties().put(LogToFile.TEST_LOG_PARTIAL_LOG_WRITE_NUM_BYTES, Integer.toString(3));
			}
			logFactory.flushAll();

			page.update(rh, bigrow.getRow(), (FormatableBitSet) null);

			if(checksumTest)
				simulateLogFileCorruption();

			////////////////////////////////////////////////////////

			REPORT("badlog test5: cid = " + cid + " numcol " + numcol);

			register(key(5,1), cid);
			register(key(5,2), numcol);
		}
		finally
		{
			SanityManager.DEBUG_CLEAR(LogToFile.TEST_LOG_INCOMPLETE_LOG_WRITE);
		}
	}

	/*
	 * test recovery of test 5
	 */
	void RTest5() throws T_Fail, StandardException
	{
		long cid = find(key(5, 1));
		if (cid < 0)
		{
			REPORT("bad log test5 not run");
			return;
		}
		int numcol = (int)find(key(5,2));

		Transaction t = t_util.t_startTransaction();
		try
		{
			ContainerHandle c = t_util.t_openContainer(t, 0, cid, true);
			Page page = t_util.t_getPage(c, ContainerHandle.FIRST_PAGE_NUMBER);

			int optimisticNumcol = (int)((RawStoreFactory.PAGE_SIZE_MINIMUM*8)/(10*20));
			T_RawStoreRow bigrow = new T_RawStoreRow(optimisticNumcol);
			for (int i = 0; i < optimisticNumcol; i++)
				bigrow.setColumn(i, (String) null);

			page.fetchFromSlot(
                (RecordHandle) null, 0, bigrow.getRow(), 
                (FetchDescriptor) null,
                false);

			Storable column;
			String string1 = "01234567890123456789"; // the original 20 char string

			for (int i = 0; i < numcol; i++)
			{
				column = bigrow.getStorableColumn(i);
				if (!(column.toString().equals(string1)))
					throw T_Fail.testFailMsg("Column " + i + " value incorrect, got :" + column.toString());
			}
			for (int i = numcol; i < optimisticNumcol; i++)
			{
				column = bigrow.getStorableColumn(i);
				if (!column.isNull())
					throw T_Fail.testFailMsg("Column " + i + 
											 " expect Null, got : " + column.toString());
			}

			REPORT("RTest5 passed");

		}
		finally
		{
			t_util.t_commit(t);
			t.close();
		}
	}
		
	/*
	 * test6 manufactures a log with the following recoverable 'defects':
	 * - a log file that only has the log record with partial data portion
	 * written (approximately (1997/2 (data)+ 16(log records ov)))	 */
	protected void STest6() throws T_Fail, StandardException
	{
		Transaction t = t_util.t_startTransaction();

		try
		{
			long cid = t_util.t_addContainer(t, 0);
			ContainerHandle c = t_util.t_openContainer(t, 0, cid, true);

			Page page = t_util.t_getPage(c, ContainerHandle.FIRST_PAGE_NUMBER);

			// make a really big record - fill 80% of the page
			int numcol = (int)((RawStoreFactory.PAGE_SIZE_MINIMUM*8)/(10*20));

			T_RawStoreRow bigrow = new T_RawStoreRow(numcol);
			String string1 = "01234567890123456789"; // 20 char string
			for (int i = 0; i < numcol; i++)
				bigrow.setColumn(i, string1);

			// if overhead is > 80%, then reduce the row size until it fits
			RecordHandle rh = null;
			while(numcol > 0)
			{
				try {
					rh = t_util.t_insert(page, bigrow);
					break;
				} catch (StandardException se) {
					bigrow.setColumn(--numcol, (String) null);
				}
			}
			if (numcol == 0)
				throw T_Fail.testFailMsg("cannot fit any column into the page");

			

			t_util.t_commit(t);

			// make a big log record - update row
			String string2 = "abcdefghijklmnopqrst"; // 20 char string
			for (int i = 0; i < numcol; i++)
				bigrow.setColumn(i, string2);

			c = t_util.t_openContainer(t, 0, cid, true);
			page = t_util.t_getPage(c, ContainerHandle.FIRST_PAGE_NUMBER);

			Page p2 = t_util.t_addPage(c);		// do something so we get the beginXact log
								// record out of the way
			t_util.t_insert(p2, new T_RawStoreRow(REC_001));


			//////////////////////////////////////////////////////////
			// writing (1997/2 (data)+ 16(log records ov)) bytes of log record to the end of the log - 
			// NO MORE LOG RECORD SHOULD BE WRITTEN,
			//////////////////////////////////////////////////////////
			if(!checksumTest)
			{
				SanityManager.DEBUG_SET(LogToFile.TEST_LOG_INCOMPLETE_LOG_WRITE);
				System.getProperties().put(LogToFile.TEST_LOG_PARTIAL_LOG_WRITE_NUM_BYTES, Integer.toString((1997/2) + 16));
			}
			logFactory.flushAll();
			page.update(rh, bigrow.getRow(), (FormatableBitSet) null);

			if(checksumTest)
				simulateLogFileCorruption();

			////////////////////////////////////////////////////////

			REPORT("badlog test6: cid = " + cid + " numcol " + numcol);

			register(key(6,1), cid);
			register(key(6,2), numcol);
		}
		finally
		{
			SanityManager.DEBUG_CLEAR(LogToFile.TEST_LOG_INCOMPLETE_LOG_WRITE);
		}
	}

	/*
	 * test recovery of test 6
	 */
	void RTest6() throws T_Fail, StandardException
	{
		long cid = find(key(6, 1));
		if (cid < 0)
		{
			REPORT("bad log test6 not run");
			return;
		}
		int numcol = (int)find(key(6,2));

		Transaction t = t_util.t_startTransaction();
		try
		{
			ContainerHandle c = t_util.t_openContainer(t, 0, cid, true);
			Page page = t_util.t_getPage(c, ContainerHandle.FIRST_PAGE_NUMBER);

			int optimisticNumcol = (int)((RawStoreFactory.PAGE_SIZE_MINIMUM*8)/(10*20));
			T_RawStoreRow bigrow = new T_RawStoreRow(optimisticNumcol);
			for (int i = 0; i < optimisticNumcol; i++)
				bigrow.setColumn(i, (String) null);

			page.fetchFromSlot(
                (RecordHandle) null, 0, bigrow.getRow(), 
                (FetchDescriptor) null,
                false);

			Storable column;
			String string1 = "01234567890123456789"; // the original 20 char string

			for (int i = 0; i < numcol; i++)
			{
				column = bigrow.getStorableColumn(i);
				if (!(column.toString().equals(string1)))
					throw T_Fail.testFailMsg("Column " + i + " value incorrect, got :" + column.toString());
			}
			for (int i = numcol; i < optimisticNumcol; i++)
			{
				column = bigrow.getStorableColumn(i);
				if (!column.isNull())
					throw T_Fail.testFailMsg("Column " + i + 
											 " expect Null, got : " + column.toString());
			}

			REPORT("RTest6 passed");

		}
		finally
		{
			t_util.t_commit(t);
			t.close();
		}
	}
	/*
	 * test7 manufactures a log with the following recoverable 'defects':
	 * - a log file that has the last log record with partial end length
	 * written( 3 of 4 bytes). instead of (1997(data) + 16 (log records overhead)) write (1997 + 15) 
	 */
	protected void STest7() throws T_Fail, StandardException
	{
		Transaction t = t_util.t_startTransaction();

		try
		{
			long cid = t_util.t_addContainer(t, 0);
			ContainerHandle c = t_util.t_openContainer(t, 0, cid, true);

			Page page = t_util.t_getPage(c, ContainerHandle.FIRST_PAGE_NUMBER);

			// make a really big record - fill 80% of the page
			int numcol = (int)((RawStoreFactory.PAGE_SIZE_MINIMUM*8)/(10*20));

			T_RawStoreRow bigrow = new T_RawStoreRow(numcol);
			String string1 = "01234567890123456789"; // 20 char string
			for (int i = 0; i < numcol; i++)
				bigrow.setColumn(i, string1);

			// if overhead is > 80%, then reduce the row size until it fits
			RecordHandle rh = null;
			while(numcol > 0)
			{
				try {
					rh = t_util.t_insert(page, bigrow);
					break;
				} catch (StandardException se) {
					bigrow.setColumn(--numcol, (String) null);
				}
			}
			if (numcol == 0)
				throw T_Fail.testFailMsg("cannot fit any column into the page");

			

			t_util.t_commit(t);

			// make a big log record - update row
			String string2 = "abcdefghijklmnopqrst"; // 20 char string
			for (int i = 0; i < numcol; i++)
				bigrow.setColumn(i, string2);

			c = t_util.t_openContainer(t, 0, cid, true);
			page = t_util.t_getPage(c, ContainerHandle.FIRST_PAGE_NUMBER);

			Page p2 = t_util.t_addPage(c);		// do something so we get the beginXact log
								// record out of the way
			t_util.t_insert(p2, new T_RawStoreRow(REC_001));


			//////////////////////////////////////////////////////////
			// writing only 3 bytes of end length of the log record to the end of the log - 
			//i.e: instead of (1997(data) + 16 (log records overhead)) write (1997 + 15) 
			// NO MORE LOG RECORD SHOULD BE WRITTEN,
			//////////////////////////////////////////////////////////
			if(!checksumTest)
			{
				SanityManager.DEBUG_SET(LogToFile.TEST_LOG_INCOMPLETE_LOG_WRITE);
				System.getProperties().put(LogToFile.TEST_LOG_PARTIAL_LOG_WRITE_NUM_BYTES, Integer.toString(1997+15));
			}
			logFactory.flushAll();
			page.update(rh, bigrow.getRow(), (FormatableBitSet) null);

			if(checksumTest)
				simulateLogFileCorruption();


			////////////////////////////////////////////////////////

			REPORT("badlog test7: cid = " + cid + " numcol " + numcol);

			register(key(7,1), cid);
			register(key(7,2), numcol);
		}
		finally
		{
			SanityManager.DEBUG_CLEAR(LogToFile.TEST_LOG_INCOMPLETE_LOG_WRITE);
		}
	}

	/*
	 * test recovery of test 7
	 */
	void RTest7() throws T_Fail, StandardException
	{
		long cid = find(key(6, 1));
		if (cid < 0)
		{
			REPORT("bad log test7 not run");
			return;
		}
		int numcol = (int)find(key(6,2));

		Transaction t = t_util.t_startTransaction();
		try
		{
			ContainerHandle c = t_util.t_openContainer(t, 0, cid, true);
			Page page = t_util.t_getPage(c, ContainerHandle.FIRST_PAGE_NUMBER);

			int optimisticNumcol = (int)((RawStoreFactory.PAGE_SIZE_MINIMUM*8)/(10*20));
			T_RawStoreRow bigrow = new T_RawStoreRow(optimisticNumcol);
			for (int i = 0; i < optimisticNumcol; i++)
				bigrow.setColumn(i, (String) null);

			page.fetchFromSlot(
                (RecordHandle) null, 0, bigrow.getRow(), 
                (FetchDescriptor) null,
                false);

			Storable column;
			String string1 = "01234567890123456789"; // the original 20 char string

			for (int i = 0; i < numcol; i++)
			{
				column = bigrow.getStorableColumn(i);
				if (!(column.toString().equals(string1)))
					throw T_Fail.testFailMsg("Column " + i + " value incorrect, got :" + column.toString());
			}
			for (int i = numcol; i < optimisticNumcol; i++)
			{
				column = bigrow.getStorableColumn(i);
				if (!column.isNull())
					throw T_Fail.testFailMsg("Column " + i + 
											 " expect Null, got : " + column.toString());
			}

			REPORT("RTest7 passed");

		}
		finally
		{
			t_util.t_commit(t);
			t.close();
		}
	}



	/*
	 * simulate log corruption to test the checksuming of log records. 
	 */
	private void simulateLogFileCorruption() throws T_Fail, StandardException
	{
		long filenum;
		long filepos;
		long amountOfLogWritten;
		LogCounter logInstant = (LogCounter)logFactory.getFirstUnflushedInstant();
		filenum = logInstant.getLogFileNumber();
		filepos = logInstant.getLogFilePosition();
		logFactory.flushAll();
		logInstant = (LogCounter)logFactory.getFirstUnflushedInstant();
		filenum = logInstant.getLogFileNumber();
		amountOfLogWritten = logInstant.getLogFilePosition() - filepos;

		// write some random  garbage into the log file , 
		// purpose of doing this is to test that recovery works correctly when 
		// log records in the end of a log file did not get wrtten completely
		// and in the correct order. 

		try{
			StorageRandomAccessFile log = logFactory.getLogFileToSimulateCorruption(filenum) ;
		
			int noWrites = (int) amountOfLogWritten / 512;
			//mess up few bytes in every block of a 512 bytes.
			filepos += 512;
			java.util.Random r = new java.util.Random();
			for(int i = 0 ; i < noWrites ; i++)
			{
				REPORT("corruptig log file : filenum " + filenum + " fileposition " + filepos);
				log.seek(filepos);
				log.writeInt(r.nextInt());
				filepos +=512;

			}
			log.sync(false);
			log.close();
		}catch(IOException ie)
		{
			throw T_Fail.exceptionFail(ie);
		}
		
	}

}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -