Path: blob/master/test/jdk/sun/rmi/log/ReliableLog/Recovery.java
41153 views
/*1* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @summary sanity test for log sudden-death and recovery25* @run ignore Requires special hooks in ReliableLog not yet putback.26*/2728/* The ReliableLog uses three filenames and renaming to effect atomic29* versionFile updates. First, a newVersionFile (an intention list)30* is written. Next, the current versionFile is renamed to an31* oldVersionFile (an undo list). Finally, the newVersionFile is32* renamed to the current versionFile, and the undo list is discarded.33* If the current version file does not exist on restart, then34* stability can always be restored by reading the oldVersionFile.35*36* This test uses little conditional bombs. When a switch is flipped37* in ReliableLog, the code will abort with an InternalError at a38* particular point. We then pretend to have come up from scratch and39* recover from the bombed situation.40*/4142import java.io.File;43import java.io.IOException;44import java.io.PrintStream;45import java.io.Serializable;46import sun.rmi.log.LogHandler;47import sun.rmi.log.ReliableLog;4849//import javasoft.sqe.harness.Status;50//import javasoft.sqe.harness.Test;5152public class Recovery53extends LogHandler54implements Serializable //, Test55{56static private final String dir = "./recovery_tmp";5758static public void main (String[] argv)59{60Recovery test = new Recovery();61//Status status = test.run (argv, System.err, System.out);62//status.exit();63test.run (argv, System.err, System.out);64}6566//public Status run (String argv[], PrintStream log, PrintStream out)67public void run (String argv[], PrintStream lg, PrintStream out)68{69try {70int size;71int deathpoint;72for (size = 1; size < 256; size *= 2) {73for (deathpoint = 0; deathpoint <= 8; deathpoint++) {74// Trash the log directory75try {76(new File(dir,"Version_Number")).delete();77} catch (Exception e) {78}79try {80(new File(dir,"New_Version_Number")).delete();81} catch (Exception e) {82}83try {84(new File(dir,"Old_Version_Number")).delete();85} catch (Exception e) {86}8788Recovery handler = new Recovery();89ReliableLog log;90log = new ReliableLog(dir, handler);9192// Generate a number of updates (size - 1) until failing93int i;94StringBuffer writ = new StringBuffer();95char[] u = new char[1];96for (i = 1; i < size; i++) {97u[0] = (char)(64 + (i % 26));98String update = new String(u);99handler.basicUpdate(update);100log.update(update, true);101writ.append(update);102}103104// Fail105String f = ("FALL" + deathpoint);106boolean failed_as_desired = false;107try {108ReliableLog.fallOverPoint = deathpoint;109handler.basicUpdate(f);110log.update(f, true);111log.snapshot(handler);112} catch (InternalError e) {113if (!e.getMessage().equals(f))114throw e; // oops, not ours115failed_as_desired = true;116} finally {117ReliableLog.fallOverPoint = 0;118try {119log.close();120} catch (IOException ign) {121}122}123124// deathpoint == 0 means that there is no deathpoint and we are testing125// undisastered behaviour.126if (!failed_as_desired && deathpoint != 0) {127System.err.println ("sun.rmi.log.ReliableLog is not instrumented for" +128" this test; test skipped");129return;130}131132// Now try to recover.133Recovery laz = new Recovery();134ReliableLog carbon = new ReliableLog(dir, laz);135Recovery thingy;136thingy = (Recovery)carbon.recover();137try {138carbon.close();139} catch (IOException ign) {140}141142// Recovered thingy must be equal to writ or to writ + f, but nothing143// else (or in between).144String recovered = thingy.contents;145String sacr = writ.toString();146if (recovered.length() == sacr.length()147&& recovered.compareTo(sacr) == 0)148{149lg.println("Passed test " + size + "/" + deathpoint150+ ": rolled back to v1");151} else if (recovered.length() == (sacr.length() + f.length())152&& recovered.compareTo(sacr + f) == 0)153{154lg.println("Passed test " + size + "/" + deathpoint155+ ": committed to v2");156} else {157final String q = "\"";158lg.println("Wrote " + sacr.length() + ": " + q + sacr + q);159lg.println("Simulated failure during write "160+ f.length() + ": " + q + f + q);161lg.println("Recovered " + recovered.length() + ": " + q + recovered + q);162throw new InternalError("Failed test " + size + "/" + deathpoint163+ " (size/deathpoint):");164}165}166}167} catch (Exception e) {168e.printStackTrace(lg);169//return (Status.failed170throw (new RuntimeException171("Exception in sanity test for sun.rmi.log.ReliableLog"));172}173//return (Status.passed ("OKAY"));174}175176private String contents;177transient private StringBuffer trc = null;178179public Recovery()180{181super();182if (this.contents == null)183this.contents = "?";184}185186// implements LogHandler.initialSnapshot()187public Object initialSnapshot()188throws Exception189{190this.contents = "";191return (this);192}193194// implements LogHandler.applyUpdate()195public Object applyUpdate (Object update, Object state)196throws Exception197{198// basicUpdate appends the string199((Recovery)state).basicUpdate ((String)update);200return (state);201}202203// an "update" is a short string to append to this.contents (must ignore state)204public void basicUpdate (String extra)205{206this.contents = this.contents + extra;207}208}209210211