0N/A/*
2362N/A * Copyright (c) 1998, 2002, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @summary sanity test for log sudden-death and recovery
0N/A * @run ignore Requires special hooks in ReliableLog not yet putback.
0N/A */
0N/A
0N/A/* The ReliableLog uses three filenames and renaming to effect atomic
0N/A * versionFile updates. First, a newVersionFile (an intention list)
0N/A * is written. Next, the current versionFile is renamed to an
0N/A * oldVersionFile (an undo list). Finally, the newVersionFile is
0N/A * renamed to the current versionFile, and the undo list is discarded.
0N/A * If the current version file does not exist on restart, then
0N/A * stability can always be restored by reading the oldVersionFile.
0N/A *
0N/A * This test uses little conditional bombs. When a switch is flipped
0N/A * in ReliableLog, the code will abort with an InternalError at a
0N/A * particular point. We then pretend to have come up from scratch and
0N/A * recover from the bombed situation.
0N/A */
0N/A
0N/Aimport java.io.File;
0N/Aimport java.io.IOException;
0N/Aimport java.io.PrintStream;
0N/Aimport java.io.Serializable;
0N/Aimport sun.rmi.log.LogHandler;
0N/Aimport sun.rmi.log.ReliableLog;
0N/A
0N/A//import javasoft.sqe.harness.Status;
0N/A//import javasoft.sqe.harness.Test;
0N/A
0N/Apublic class Recovery
0N/A extends LogHandler
0N/A implements Serializable //, Test
0N/A{
0N/A static private final String dir = "./recovery_tmp";
0N/A
0N/A static public void main (String[] argv)
0N/A {
0N/A Recovery test = new Recovery();
0N/A //Status status = test.run (argv, System.err, System.out);
0N/A //status.exit();
0N/A test.run (argv, System.err, System.out);
0N/A }
0N/A
0N/A //public Status run (String argv[], PrintStream log, PrintStream out)
0N/A public void run (String argv[], PrintStream lg, PrintStream out)
0N/A {
0N/A try {
0N/A int size;
0N/A int deathpoint;
0N/A for (size = 1; size < 256; size *= 2) {
0N/A for (deathpoint = 0; deathpoint <= 8; deathpoint++) {
0N/A // Trash the log directory
0N/A try {
0N/A (new File(dir,"Version_Number")).delete();
0N/A } catch (Exception e) {
0N/A }
0N/A try {
0N/A (new File(dir,"New_Version_Number")).delete();
0N/A } catch (Exception e) {
0N/A }
0N/A try {
0N/A (new File(dir,"Old_Version_Number")).delete();
0N/A } catch (Exception e) {
0N/A }
0N/A
0N/A Recovery handler = new Recovery();
0N/A ReliableLog log;
0N/A if (size == 4 && deathpoint == 6) {
0N/A Runtime.getRuntime().traceMethodCalls(true);
0N/A }
0N/A log = new ReliableLog(dir, handler);
0N/A if (size == 4 && deathpoint == 6) {
0N/A Runtime.getRuntime().traceMethodCalls(false);
0N/A }
0N/A
0N/A // Generate a number of updates (size - 1) until failing
0N/A int i;
0N/A StringBuffer writ = new StringBuffer();
0N/A char[] u = new char[1];
0N/A for (i = 1; i < size; i++) {
0N/A u[0] = (char)(64 + (i % 26));
0N/A String update = new String(u);
0N/A handler.basicUpdate(update);
0N/A log.update(update, true);
0N/A writ.append(update);
0N/A }
0N/A
0N/A // Fail
0N/A String f = ("FALL" + deathpoint);
0N/A boolean failed_as_desired = false;
0N/A try {
0N/A ReliableLog.fallOverPoint = deathpoint;
0N/A handler.basicUpdate(f);
0N/A log.update(f, true);
0N/A log.snapshot(handler);
0N/A } catch (InternalError e) {
0N/A if (!e.getMessage().equals(f))
0N/A throw e; // oops, not ours
0N/A failed_as_desired = true;
0N/A } finally {
0N/A ReliableLog.fallOverPoint = 0;
0N/A try {
0N/A log.close();
0N/A } catch (IOException ign) {
0N/A }
0N/A }
0N/A
0N/A // deathpoint == 0 means that there is no deathpoint and we are testing
0N/A // undisastered behaviour.
0N/A if (!failed_as_desired && deathpoint != 0) {
0N/A System.err.println ("sun.rmi.log.ReliableLog is not instrumented for" +
0N/A " this test; test skipped");
0N/A return;
0N/A }
0N/A
0N/A // Now try to recover.
0N/A Recovery laz = new Recovery();
0N/A ReliableLog carbon = new ReliableLog(dir, laz);
0N/A Recovery thingy;
0N/A thingy = (Recovery)carbon.recover();
0N/A try {
0N/A carbon.close();
0N/A } catch (IOException ign) {
0N/A }
0N/A
0N/A // Recovered thingy must be equal to writ or to writ + f, but nothing
0N/A // else (or in between).
0N/A String recovered = thingy.contents;
0N/A String sacr = writ.toString();
0N/A if (recovered.length() == sacr.length()
0N/A && recovered.compareTo(sacr) == 0)
0N/A {
0N/A lg.println("Passed test " + size + "/" + deathpoint
0N/A + ": rolled back to v1");
0N/A } else if (recovered.length() == (sacr.length() + f.length())
0N/A && recovered.compareTo(sacr + f) == 0)
0N/A {
0N/A lg.println("Passed test " + size + "/" + deathpoint
0N/A + ": committed to v2");
0N/A } else {
0N/A final String q = "\"";
0N/A lg.println("Wrote " + sacr.length() + ": " + q + sacr + q);
0N/A lg.println("Simulated failure during write "
0N/A + f.length() + ": " + q + f + q);
0N/A lg.println("Recovered " + recovered.length() + ": " + q + recovered + q);
0N/A throw new InternalError("Failed test " + size + "/" + deathpoint
0N/A + " (size/deathpoint):");
0N/A }
0N/A }
0N/A }
0N/A } catch (Exception e) {
0N/A e.printStackTrace(lg);
0N/A //return (Status.failed
0N/A throw (new RuntimeException
0N/A ("Exception in sanity test for sun.rmi.log.ReliableLog"));
0N/A }
0N/A //return (Status.passed ("OKAY"));
0N/A }
0N/A
0N/A private String contents;
0N/A transient private StringBuffer trc = null;
0N/A
0N/A public Recovery()
0N/A {
0N/A super();
0N/A if (this.contents == null)
0N/A this.contents = "?";
0N/A }
0N/A
0N/A // implements LogHandler.initialSnapshot()
0N/A public Object initialSnapshot()
0N/A throws Exception
0N/A {
0N/A this.contents = "";
0N/A return (this);
0N/A }
0N/A
0N/A // implements LogHandler.applyUpdate()
0N/A public Object applyUpdate (Object update, Object state)
0N/A throws Exception
0N/A {
0N/A // basicUpdate appends the string
0N/A ((Recovery)state).basicUpdate ((String)update);
0N/A return (state);
0N/A }
0N/A
0N/A // an "update" is a short string to append to this.contents (must ignore state)
0N/A public void basicUpdate (String extra)
0N/A {
0N/A this.contents = this.contents + extra;
0N/A }
0N/A}