001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.store.checkPoint
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.store;
023:
024: import java.io.File;
025: import java.io.ByteArrayInputStream;
026:
027: import java.math.BigDecimal;
028:
029: import java.sql.Connection;
030: import java.sql.DriverManager;
031: import java.sql.SQLException;
032: import java.sql.PreparedStatement;
033: import java.sql.ResultSet;
034: import java.sql.Statement;
035: import org.apache.derby.tools.ij;
036: import org.apache.derby.tools.JDBCDisplayUtil;
037:
038: /**
039: * Test to make sure checkpoint or occuring as expected.
040: * Check is done by looking at the timestamp for "log.ctrl" file,
041: * If modified time is more than what it was in the last lookup
042: * means , we know that checkpoint occured.
043: * Other thing that is counted is in this program is number of log switches.
044: * @author suresht
045: */
046:
047: public class checkPoint {
048:
049: public static void main(String args[]) {
050: System.out.println("Test checkpoint starting");
051:
052: try {
053: // use the ij utility to read the property file and
054: // make the initial connection.
055: ij.getPropertyArg(args);
056: Connection conn = ij.startJBMS();
057:
058: //open all the internal derby files involved in this test.
059: setupAllTestFiles();
060:
061: Statement stmt = conn.createStatement();
062: stmt
063: .executeUpdate("CREATE PROCEDURE WAIT_FOR_POST_COMMIT() DYNAMIC RESULT SETS 0 LANGUAGE JAVA EXTERNAL NAME 'org.apache.derbyTesting.functionTests.util.T_Access.waitForPostCommitToFinish' PARAMETER STYLE JAVA");
064:
065: stmt
066: .executeUpdate("create table t1("
067: + "c1 int not null primary key , c2 varchar(200) not null unique , c3 char(200) not null unique)");
068: conn.setAutoCommit(true);
069: String ins_string = "insert into t1 values(?,?,?)";
070: PreparedStatement insStmt = conn
071: .prepareStatement(ins_string);
072: //wait to make sure that checkpoint thread finished it's work
073: stmt
074: .executeUpdate("CALL SYSCS_UTIL.SYSCS_CHECKPOINT_DATABASE()");
075: stmt.executeUpdate("call WAIT_FOR_POST_COMMIT()");
076: checkpointOccured();
077: boolean modifiedIntervals = false;
078: for (int uniqueid = 0; uniqueid < 3500; uniqueid++) {
079: insStmt.setLong(1, uniqueid);
080: insStmt.setString(2, "IBM GREAT COMPANY " + uniqueid);
081: insStmt.setString(3, "IBM GREAT COMPANY " + uniqueid);
082: insStmt.executeUpdate();
083:
084: //check every 300 rows inserted how many log files
085: //are there and whether a checkpoint occured
086: if ((uniqueid % 400) == 0) {
087: System.out
088: .println("Checking logs and Checkpoint at Insert:"
089: + uniqueid);
090: //wait to make sure that checkpoint thread finished it's work
091: stmt.executeUpdate("call WAIT_FOR_POST_COMMIT()");
092: checkpointOccured();
093: }
094:
095: //change the checkpointInterval and LogInterval to equal values
096: if (uniqueid > 2500 && !modifiedIntervals) {
097: ResultSet rs;
098: System.out
099: .println("Modifying the checkpoint/log intervals");
100: //modify the values.
101: String value = "150001";
102: stmt
103: .executeUpdate("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY"
104: + "('derby.storage.logSwitchInterval', "
105: + "'" + value + "'" + ")");
106: stmt
107: .executeUpdate("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY"
108: + "('derby.storage.checkpointInterval', "
109: + "'" + value + "'" + ")");
110: rs = stmt
111: .executeQuery("values SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY"
112: + "('derby.storage.checkpointInterval')");
113: while (rs.next()) {
114: System.out.println("checkPointInterval:"
115: + rs.getString(1));
116: }
117:
118: rs = stmt
119: .executeQuery("values SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY"
120: + "('derby.storage.logSwitchInterval')");
121: while (rs.next()) {
122: System.out.println("logSwitchInterval:"
123: + rs.getString(1));
124: }
125:
126: modifiedIntervals = true;
127: }
128: }
129:
130: //print the number of the last log file
131: //to make sure we are creating too many log files.
132: numberOfLogFiles();
133: conn.commit();
134: stmt.close();
135: insStmt.close();
136: conn.close();
137: } catch (SQLException e) {
138: dumpSQLExceptions(e);
139: } catch (Throwable e) {
140: System.out.println("FAIL -- unexpected exception:"
141: + e.toString());
142: }
143:
144: //shutdown the database ..
145: try {
146: //shutdown
147: Connection conn = DriverManager
148: .getConnection("jdbc:derby:wombat;shutdown=true");
149: } catch (SQLException se) {
150: if (se.getSQLState() != null
151: && se.getSQLState().equals("08006"))
152: System.out.println("database shutdown properly\n");
153: else
154: dumpSQLExceptions(se);
155: } catch (Throwable e) {
156: System.out.println("FAIL -- unexpected exception:"
157: + e.toString());
158: }
159:
160: System.out.println("Test checkpoint finished");
161: }
162:
163: static private void dumpSQLExceptions(SQLException se) {
164: System.out.println("FAIL -- unexpected exception: "
165: + se.toString());
166: while (se != null) {
167: System.out.print("SQLSTATE(" + se.getSQLState() + "):");
168: se = se.getNextException();
169: }
170: }
171:
172: //utility routines to trach number of log files
173: //and checkpoints.
174: private static String derbyHome;
175: private static File dbDir;
176: private static File logDir;
177: private static File logControlFile;
178: private static long lastCheckPointTime = 0;
179:
180: private static void setupAllTestFiles() {
181: derbyHome = System.getProperty("derby.system.home");
182: dbDir = new File(derbyHome, "wombat");
183: logDir = new File(dbDir, "log");
184: logControlFile = new File(logDir, "log.ctrl");
185: lastCheckPointTime = logControlFile.lastModified();
186: }
187:
188: private static boolean checkpointOccured() {
189: long currentModifiedTime = logControlFile.lastModified();
190: if (currentModifiedTime > lastCheckPointTime) {
191: lastCheckPointTime = currentModifiedTime;
192: System.out.println("CHECKPOINT WAS DONE");
193: return true;
194: }
195:
196: return false;
197: }
198:
199: private static int numberOfLogFiles() {
200: //find out how many log files are in logDir
201: //-2 (control files log.ctrl, logmirror.ctrl)
202: File[] logFiles = logDir.listFiles();
203: int noFiles = (logFiles == null) ? 0 : logFiles.length;
204: String lastLogFile = "";
205: for (int i = 0; i < noFiles; i++) {
206: String current = logFiles[i].getName();
207: if (current.compareTo("log.ctrl") == 0
208: || current.compareTo("logmirror.ctrl") == 0)
209: continue;
210: if (current.compareTo(lastLogFile) > 0)
211: lastLogFile = current;
212: }
213:
214: if (lastLogFile.compareTo("log21.dat") > 0) {
215: System.out.println("There seems to be too many log files");
216: System.out.println(lastLogFile);
217: }
218: logFiles = null;
219: return noFiles - 2;
220:
221: }
222:
223: }
|