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

📄 t_recoverbadlog.java

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

			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));


			///////////////////////////////////////////
			//// log switch without checkpoint here ///
			///////////////////////////////////////////
			factory.checkpoint();

			//////////////////////////////////////////////////////////
			// writing approx 1/2 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(numcol*20));
			}
			
			logFactory.flushAll();

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

			if(checksumTest)
				simulateLogFileCorruption();

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

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

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

	/*
	 * test recovery of test 1
	 */
	void RTest1() throws T_Fail, StandardException
	{
		long cid = find(key(1, 1));
		if (cid < 0)
		{
			REPORT("bad log test1 not run");
			return;
		}
		int numcol = (int)find(key(1,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("RTest1 passed");

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

	/*
	 * test2 manufactures a log with the following recoverable 'defects':
	 *		- a log file that ends with a large 1/2 written log record
	 */
	protected void STest2() 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 with 20 bytes row
			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");

			rh = t_util.t_insert(page, bigrow);

			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);

			//////////////////////////////////////////////////////////
			// writing approx 1/2 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(numcol*20));
			}

			logFactory.flushAll();
			page.update(rh, bigrow.getRow(), (FormatableBitSet) null);
			
			if(checksumTest)
				simulateLogFileCorruption();

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

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

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

	/*
	 * test recovery of test 2
	 */
	void RTest2() throws T_Fail, StandardException
	{
		long cid = find(key(2, 1));
		if (cid < 0)
		{
			REPORT("bad log test2 not run");
			return;
		}
		int numcol = (int)find(key(2,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("RTest2 passed");

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



	/*
	 * test3 manufactures a log with the following recoverable 'defects':
	 *    - a log with multiple files but no checkpoint log record
	 *	  - a last log file with a paritally written log record at the end
	 */
	protected void STest3() throws T_Fail, StandardException
	{
		int numtrans = 7;
		int numpages = 7;
		int i,j;

		// this is basically T_Recovery S203 with a couple of log switches
		try
		{
			T_TWC[] t = new T_TWC[numtrans];
			for (i = 0; i < numtrans; i++)
				t[i] =  t_util.t_startTransactionWithContext();

			long[] cid = new long[numtrans];
			ContainerHandle[] c = new ContainerHandle[numtrans];

			for (i = 0; i < numtrans; i++)
			{
				cid[i] = t_util.t_addContainer(t[i], 0);
				t_util.t_commit(t[i]);
				c[i] = t_util.t_openContainer(t[i], 0, cid[i], true);
			}

			Page page[][] = new Page[numtrans][numpages];
			long pagenum[][] = new long[numtrans][numpages];

			for (i = 0; i < numtrans; i++)
			{
				for (j = 0; j < numpages; j++)
				{
					t[i].switchTransactionContext();
					page[i][j] = t_util.t_addPage(c[i]);
					pagenum[i][j] = page[i][j].getPageNumber();
					t[i].resetContext();
				}
			}

			// set up numtrans (at least 5) transactions, each with one
			// container and numpages pages.  Do the following test:
			//
			// 1) insert 1 row onto each page
			// set savepoint SP1 on first transaction (t0)
			//
			// 2) update every rows
			// set savepoint SP1 on all other transactions
			//
			// 3) update every rows
			// set savepoint SP2 on all transactions
			// 
			// 4) update every rows
			//
			// 5) rollback t0 to SP1
			//
			// check that only page[0][x] have been rolled back
			// past SP2
			//
			// 6) update every row
			// 7) rollback SP2 on all transaction except the first
			// 
			// 8) update every rows
			// 9) rollback t0 to SP1
			//
			// 10) leave transactions in the following state
			// t0 - incomplete
			// t1 - abort
			// t2 - commit
			// t3 - incomplete
			// t4 - commit
			// any other transactions - incomplete


			//////////////////////// step 1 ////////////////////////
			RecordHandle[][] rh = new RecordHandle[numtrans][numpages];
			T_RawStoreRow row1 = new T_RawStoreRow(REC_001);
			for (i = 0; i < numtrans; i++)
				for (j = 0; j < numpages; j++)
				{
					t[i].switchTransactionContext();
					rh[i][j] = t_util.t_insert(page[i][j], row1); 
					t[i].resetContext();
				}

			t[0].setSavePoint(SP1, null);	// sp1

			//////////////////////// step 2 ////////////////////////
			T_RawStoreRow row2 = new T_RawStoreRow(REC_002);
			for (i = 0; i < numtrans; i++)
				for (j = 0; j < numpages; j++)
				{
					t[i].switchTransactionContext();
					page[i][j].update(rh[i][j], row2.getRow(), (FormatableBitSet) null);
					t[i].resetContext();
				}

			for (i = 1; i < numtrans; i++) // sp1
			{
				t[i].setSavePoint(SP1, null);
			}

			///////////////////////////////////////////
			//// log switch without checkpoint here ///
			///////////////////////////////////////////
			factory.checkpoint();


			//////////////////////// step 3 ////////////////////////
			T_RawStoreRow row3 = new T_RawStoreRow(REC_003);
			for (i = 0; i < numtrans; i++)
				for (j = 0; j < numpages; j++)
					page[i][j].update(rh[i][j], row3.getRow(), (FormatableBitSet) null);

			for (i = 0; i < numtrans; i++)
				t[i].setSavePoint(SP2, null);	// sp2

			//////////////////////// step 4 ////////////////////////
			T_RawStoreRow row4 = new T_RawStoreRow(REC_004);
			for (i = 0; i < numtrans; i++)
			{
				t[i].switchTransactionContext();

				for (j = 0; j < numpages; j++)
					page[i][j].update(rh[i][j], row4.getRow(), (FormatableBitSet) null);
				t[i].resetContext();
			}


			//////////////////////// step 5 ////////////////////////
			// unlatch relavante pages
			t[0].switchTransactionContext();

			for (j = 0; j < numpages; j++)
				page[0][j].unlatch();

			t[0].rollbackToSavePoint(SP1, null); // step 5

			// relatch relavante pages
			for (j = 0; j < numpages; j++)
				page[0][j] = t_util.t_getPage(c[0], pagenum[0][j]);

			t[0].resetContext();

			///////////////////////////////////////////
			//// log switch without checkpoint here ///
			///////////////////////////////////////////
			factory.checkpoint();


			//////////////////////// check ////////////////////////
			for (i = 1; i < numtrans; i++)
			{
				t[i].switchTransactionContext();
				for (j = 0; j < numpages; j++)
					t_util.t_checkFetch(page[i][j], rh[i][j], REC_004);
				t[i].resetContext();
			}

			t[0].switchTransactionContext();
			for (j = 0; j < numpages; j++)
				t_util.t_checkFetch(page[0][j], rh[0][j], REC_001);
			t[0].resetContext();

			//////////////////////// step 6 ////////////////////////
			T_RawStoreRow row5 = new T_RawStoreRow(REC_005);
			for (i = 0; i < numtrans; i++)
			{
				t[i].switchTransactionContext();
				for (j = 0; j < numpages; j++)
					page[i][j].update(rh[i][j], row5.getRow(), (FormatableBitSet) null);
				t[i].resetContext();
			}

			//////////////////////// step 7 ////////////////////////
			for (i = 1; i < numtrans; i++)
			{
				t[i].switchTransactionContext();

				for (j = 0; j < numpages; j++)
					page[i][j].unlatch();

				t[i].rollbackToSavePoint(SP2, null);

				for (j = 0; j < numpages; j++)

⌨️ 快捷键说明

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