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

📄 t_recoverbadlog.java

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

   Derby - Class org.apache.derbyTesting.unitTests.store.T_RecoverBadLog

   Copyright 1997, 2005 The Apache Software Foundation or its licensors, as applicable.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

package org.apache.derbyTesting.unitTests.store;

import org.apache.derbyTesting.unitTests.harness.T_Generic;
import org.apache.derbyTesting.unitTests.harness.T_Fail;
import org.apache.derbyTesting.unitTests.harness.UnitTest;

import org.apache.derby.impl.store.raw.log.*;

import org.apache.derby.iapi.services.context.ContextService;
import org.apache.derby.iapi.services.context.ContextManager;
import org.apache.derby.iapi.services.daemon.DaemonService;
import org.apache.derby.iapi.services.monitor.Monitor;
import org.apache.derby.iapi.services.locks.LockFactory;
import org.apache.derby.iapi.services.io.Storable;
import org.apache.derby.iapi.services.sanity.SanityManager;
import org.apache.derby.iapi.reference.Property;
import org.apache.derby.iapi.reference.EngineType;
import org.apache.derby.iapi.services.property.PropertyUtil;
import org.apache.derby.iapi.services.io.FormatableBitSet;
import org.apache.derby.io.StorageRandomAccessFile;

import org.apache.derby.iapi.error.StandardException;

import org.apache.derby.iapi.store.raw.*;

import org.apache.derby.iapi.store.access.conglomerate.LogicalUndo;

import org.apache.derby.iapi.store.access.Qualifier;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.File;
import java.util.Properties;



/**
	A implementation unit test for recovering log that has been damanged but salvagable.

    To run, create a derby.properties file in a new directory with the
	contents

	derby.module.test.recoverBadLog=org.apache.derbyTesting.unitTests.store.T_RecoverBadLog

    Execute in order

	To Test Bad Log due to partial write that are identified by checking the
	length in the beginning and end of the log record. 

	java -DTestBadLogSetup=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	java -DTestBadLog1=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	java -DTestBadLog2=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	java -DTestBadLog3=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	java -DTestBadLog4=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	java -DTestBadLog5=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	java -DTestBadLog6=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	java -DTestBadLog7=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	java -DTestBadLog1=true org.apache.derbyTesting.unitTests.harness.UnitTestMain

	To Test Bad Log due to an incomplete out of order write that is identified
	by the checksum logic (simulated by	explicitly corrupting a middle of a 
	log record at  the  end of log file after it is written).
	
	java -DTestBadLogSetup=true -DTestBadChecksumLog=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	java -DTestBadLog1=true -DTestBadChecksumLog=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	java -DTestBadLog2=true -DTestBadChecksumLog=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	java -DTestBadLog3=true -DTestBadChecksumLog=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	java -DTestBadLog4=true -DTestBadChecksumLog=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	java -DTestBadLog5=true -DTestBadChecksumLog=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	java -DTestBadLog6=true -DTestBadChecksumLog=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	java -DTestBadLog7=true -DTestBadChecksumLog=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	java -DTestBadLog1=true -DTestBadChecksumLog=true org.apache.derbyTesting.unitTests.harness.UnitTestMain
	
	
*/

public class T_RecoverBadLog extends T_Generic {

	private  String testService = "BadLogTest";

	static final String REC_001 = "McLaren";
	static final String REC_002 = "Ferrari";
	static final String REC_003 = "Benetton";
	static final String REC_004 = "Prost";
	static final String REC_005 = "Tyrell";
	static final String REC_006 = "Derby, Natscape, Goatscape, the popular names";
	static final String REC_UNDO = "Lotus";

	static final String SP1 = "savepoint1";
	static final String SP2 = "savepoint2";

	private RandomAccessFile infofile = null;

	private boolean setup;
	private boolean test1;
	private boolean test2;
	private boolean test3;
	private boolean test4;
	private boolean test5;
	private boolean test6;
	private boolean test7;
	private boolean checksumTest; 
	
	private  String infoPath = "extinout/T_RecoverBadLog.info";

	private static final String TEST_BADLOG_SETUP = "TestBadLogSetup";
	private static final String TEST_BADLOG1 = "TestBadLog1";
	private static final String TEST_BADLOG2 = "TestBadLog2";
	private static final String TEST_BADLOG3 = "TestBadLog3";
	private static final String TEST_BADLOG4 = "TestBadLog4";
	private static final String TEST_BADLOG5 = "TestBadLog5";
	private static final String TEST_BADLOG6 = "TestBadLog6";
	private static final String TEST_BADLOG7 = "TestBadLog7";

	private static final String TEST_BAD_CHECKSUM_LOG = "TestBadChecksumLog";

	private static final String TEST_BADLOG_INFO = "TestBadLogInfo";
	private static final String TEST_BADCHECKSUMLOG_INFO = "TestBadChecksumLogInfo";

	RawStoreFactory	factory;
	LockFactory  lf;
	LogToFile   logFactory;
	ContextService contextService;
	T_Util t_util;

	public T_RecoverBadLog() {
		super();
	}

	/*
	** Methods required by T_Generic
	*/

	public String getModuleToTestProtocolName() {
		return RawStoreFactory.MODULE;
	}

	/**
	*/
	private void getConfig()
	{
		String param;

		param = PropertyUtil.getSystemProperty(TEST_BADLOG_SETUP);
		setup = Boolean.valueOf(param).booleanValue();

		param = PropertyUtil.getSystemProperty(TEST_BADLOG1);
		test1 = Boolean.valueOf(param).booleanValue();

		param = PropertyUtil.getSystemProperty(TEST_BADLOG2);
		test2 = Boolean.valueOf(param).booleanValue();

		param = PropertyUtil.getSystemProperty(TEST_BADLOG3);
		test3 = Boolean.valueOf(param).booleanValue();
		
		
		param = PropertyUtil.getSystemProperty(TEST_BADLOG4);
		test4 = Boolean.valueOf(param).booleanValue();
		
		param = PropertyUtil.getSystemProperty(TEST_BADLOG5);
		test5 = Boolean.valueOf(param).booleanValue();

		param = PropertyUtil.getSystemProperty(TEST_BADLOG6);
		test6 = Boolean.valueOf(param).booleanValue();

		param = PropertyUtil.getSystemProperty(TEST_BADLOG7);
		test7 = Boolean.valueOf(param).booleanValue();
		
		param = PropertyUtil.getSystemProperty(TEST_BAD_CHECKSUM_LOG);
		checksumTest = Boolean.valueOf(param).booleanValue();
		
		if(checksumTest)
		{
			infoPath = "extinout/T_RecoverBadChecksumLog.info";
			testService = "BadChecksumLogTest";
		}
	}


	/**
	    See T_Recovery for the general testing frame work

		@exception T_Fail Unexpected behaviour from the API
	 */
	public void runTests() throws T_Fail {

		getConfig();
		int tests = 0;
		if (setup) tests++;
		if (test1) tests++;
		if (test2) tests++;
		if (test3) tests++;
		if (test4) tests++;
		if (test5) tests++;
		if (test6) tests++;
		if (test7) tests++;
		
		if (tests != 1)
			throw T_Fail.testFailMsg("One & only one of the bad log recovery test should be run");

		if (!SanityManager.DEBUG)
		{
			REPORT("recoverBadLog cannot be run on an insane server");
			return;
		}

		try {
			contextService = ContextService.getFactory();

			File ifile = new File(infoPath);

			//
			// no checkpoint log record in any of the log files - unless this value
			// is reset. LogToFile.TEST_LOG_SWITCH_LOG
			// this will cause recovery to switch log without checkpointing
			//
			SanityManager.DEBUG_SET(LogToFile.TEST_LOG_SWITCH_LOG);

			// don't want background checkpoint process to be running
			SanityManager.DEBUG_SET(DaemonService.DaemonOff);

			// see if we are testing encryption
			startParams = T_Util.setEncryptionParam(startParams);

			if (setup)				// the first test cleans up and start from fresh
			{
				// remove the service directory to ensure a clean run
				REPORT("_______________________________________________________");
				REPORT("\n\t\tcleaning up database for recovering from bad logs");
				REPORT("_______________________________________________________");

				// don't automatic boot this service if it gets left around
				if (startParams == null) 
					startParams = new Properties();
				
				startParams.put(Property.NO_AUTO_BOOT, Boolean.TRUE.toString());
				// remove the service directory to ensure a clean run
				startParams.put(Property.DELETE_ON_CREATE, Boolean.TRUE.toString());

				factory = (RawStoreFactory) Monitor.createPersistentService(getModuleToTestProtocolName(),
															  testService,
															  startParams);
				// create a database with nothing

				// delete the info file
				if (ifile.exists())
					ifile.delete();

				return;				// don't run anything now

			}
			else					// not setup, recover it
			{
				REPORT("_______________________________________________________");
				
				String message = "\n\t\tRunning bad log test ";
				if (checksumTest)
					message = "\n\t\tRunning bad checksum log test ";
				if (test1)
					REPORT(message + " 1");
				if (test2)
					REPORT(message + " 2");
				if (test3)
					REPORT(message + " 3");
				if (test4)
					REPORT(message + " 4");
				if (test5)
					REPORT(message + " 5");
				if (test6)
					REPORT(message + " 6");
				if (test7)
					REPORT(message + " 7");

				REPORT("_______________________________________________________");

				//if external input output files does not exist ,create one
				File ifdir = new File("extinout");
				if(!ifdir.exists())
					ifdir.mkdirs();

				try
				{
					// make sure it does exist
					infofile = new RandomAccessFile(ifile, "rw");
				}
				catch (IOException ioe)
				{
					System.out.println("Cannot write to temporary file " +
									   infoPath + 
									   ".  Please make sure it is correct, if not, please set the property " +
									   "TestBadLogInfo=<where temp files should go>");
					
					throw T_Fail.exceptionFail(ioe);
				}

				if (!Monitor.startPersistentService(testService, startParams))
					throw T_Fail.testFailMsg("Monitor didn't know how to restart service: " + testService);

				factory = (RawStoreFactory) Monitor.findService(getModuleToTestProtocolName(), testService);
				logFactory =(LogToFile) Monitor.findServiceModule(factory, factory.getLogFactoryModule());
				
			}
		} catch (StandardException mse) {
			throw T_Fail.exceptionFail(mse);
		}

		if (factory == null) {
			throw T_Fail.testFailMsg(getModuleToTestProtocolName() + " service not started.");
		}
			
		lf = factory.getLockFactory();
		if (lf == null) {
			throw T_Fail.testFailMsg("LockFactory.MODULE not found");
		}

		// get a utility helper
		t_util = new T_Util(factory, lf, contextService);

		try {
			

			// these tests can be run in any order
			RTest1();
			RTest2();
			RTest3();
			RTest4();
			RTest5();
			RTest6();
			RTest7();

			if (test1)
				STest1();

			if (test2)
				STest2();
				
			if (test3)
				STest3();
						
			if (test4)
				STest4();

			if(test5) 
				STest5();

			if(test6) 
				STest6();

			if(test7) 
				STest7();

			if (infofile != null)
				infofile.close();

		} catch (StandardException se) {

			throw T_Fail.exceptionFail(se);
		}
		catch (IOException ioe)
		{
			throw T_Fail.exceptionFail(ioe);
		}
	}

	private long find(long inkey)
	{
		if (infofile == null)
			return -1;

		try 
		{
			infofile.seek(0);
			long key;

			while(true)
			{
				key = infofile.readLong();
				if (key == inkey)
				{
					long value = infofile.readLong();
					// System.out.println("found " + key + " " + value);
					return value;
				}
				infofile.readLong();
			}
		} 
		catch (IOException ioe)
		{
			// System.out.println("key not found " + inkey);
			return -1;
		}

	}

	private long key(int test, int param)
	{
		long i = test;
		return ((i << 32) + param);
	}

	private void register(long key, long value)
		 throws T_Fail
	{
		// System.out.println("registering " + key + " " + value);
		try 
		{
			// go to the end
			infofile.seek(infofile.length());
			infofile.writeLong(key);
			infofile.writeLong(value);
		}
		catch (IOException ioe)
		{
			T_Fail.exceptionFail(ioe);
		}
	}


	/*
	 * test1 manufactures a log with the following recoverable 'defects':
	 *		- a log file that only have a single large 1/2 written log record
	 */
	protected void STest1() throws T_Fail, StandardException
	{
		Transaction t = t_util.t_startTransaction();

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

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

⌨️ 快捷键说明

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