Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/rmi/log/ReliableLog/Recovery.java
38858 views
/*1* Copyright (c) 1998, 2002, 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;90if (size == 4 && deathpoint == 6) {91Runtime.getRuntime().traceMethodCalls(true);92}93log = new ReliableLog(dir, handler);94if (size == 4 && deathpoint == 6) {95Runtime.getRuntime().traceMethodCalls(false);96}9798// Generate a number of updates (size - 1) until failing99int i;100StringBuffer writ = new StringBuffer();101char[] u = new char[1];102for (i = 1; i < size; i++) {103u[0] = (char)(64 + (i % 26));104String update = new String(u);105handler.basicUpdate(update);106log.update(update, true);107writ.append(update);108}109110// Fail111String f = ("FALL" + deathpoint);112boolean failed_as_desired = false;113try {114ReliableLog.fallOverPoint = deathpoint;115handler.basicUpdate(f);116log.update(f, true);117log.snapshot(handler);118} catch (InternalError e) {119if (!e.getMessage().equals(f))120throw e; // oops, not ours121failed_as_desired = true;122} finally {123ReliableLog.fallOverPoint = 0;124try {125log.close();126} catch (IOException ign) {127}128}129130// deathpoint == 0 means that there is no deathpoint and we are testing131// undisastered behaviour.132if (!failed_as_desired && deathpoint != 0) {133System.err.println ("sun.rmi.log.ReliableLog is not instrumented for" +134" this test; test skipped");135return;136}137138// Now try to recover.139Recovery laz = new Recovery();140ReliableLog carbon = new ReliableLog(dir, laz);141Recovery thingy;142thingy = (Recovery)carbon.recover();143try {144carbon.close();145} catch (IOException ign) {146}147148// Recovered thingy must be equal to writ or to writ + f, but nothing149// else (or in between).150String recovered = thingy.contents;151String sacr = writ.toString();152if (recovered.length() == sacr.length()153&& recovered.compareTo(sacr) == 0)154{155lg.println("Passed test " + size + "/" + deathpoint156+ ": rolled back to v1");157} else if (recovered.length() == (sacr.length() + f.length())158&& recovered.compareTo(sacr + f) == 0)159{160lg.println("Passed test " + size + "/" + deathpoint161+ ": committed to v2");162} else {163final String q = "\"";164lg.println("Wrote " + sacr.length() + ": " + q + sacr + q);165lg.println("Simulated failure during write "166+ f.length() + ": " + q + f + q);167lg.println("Recovered " + recovered.length() + ": " + q + recovered + q);168throw new InternalError("Failed test " + size + "/" + deathpoint169+ " (size/deathpoint):");170}171}172}173} catch (Exception e) {174e.printStackTrace(lg);175//return (Status.failed176throw (new RuntimeException177("Exception in sanity test for sun.rmi.log.ReliableLog"));178}179//return (Status.passed ("OKAY"));180}181182private String contents;183transient private StringBuffer trc = null;184185public Recovery()186{187super();188if (this.contents == null)189this.contents = "?";190}191192// implements LogHandler.initialSnapshot()193public Object initialSnapshot()194throws Exception195{196this.contents = "";197return (this);198}199200// implements LogHandler.applyUpdate()201public Object applyUpdate (Object update, Object state)202throws Exception203{204// basicUpdate appends the string205((Recovery)state).basicUpdate ((String)update);206return (state);207}208209// an "update" is a short string to append to this.contents (must ignore state)210public void basicUpdate (String extra)211{212this.contents = this.contents + extra;213}214}215216217