use of org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement in project zookeeper by apache.
the class AtomicFileWritingIdiomTest method testWriterFailureIOException.
@Test
public void testWriterFailureIOException() throws IOException {
File target = new File(tmpdir, "target.txt");
final File tmp = new File(tmpdir, "target.txt.tmp");
createFile(target, "before");
assertEquals("before", getContent(target));
boolean exception = false;
try {
new AtomicFileWritingIdiom(target, new WriterStatement() {
@Override
public void write(Writer os) throws IOException {
os.write("after");
os.flush();
assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists());
throw new IOException();
}
});
} catch (IOException ex) {
exception = true;
}
assertFalse("tmp file should have been deleted", tmp.exists());
assertTrue("should have raised an exception", exception);
// content preserved
assertEquals("before", getContent(target));
target.delete();
}
use of org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement in project zookeeper by apache.
the class QuorumPeer method writeLongToFile.
/**
* Write a long value to disk atomically. Either succeeds or an exception
* is thrown.
* @param name file name to write the long to
* @param value the long value to write to the named file
* @throws IOException if the file cannot be written atomically
*/
private void writeLongToFile(String name, final long value) throws IOException {
File file = new File(logFactory.getSnapDir(), name);
new AtomicFileWritingIdiom(file, new WriterStatement() {
@Override
public void write(Writer bw) throws IOException {
bw.write(Long.toString(value));
}
});
}
use of org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement in project zookeeper by apache.
the class QuorumPeerConfig method editStaticConfig.
/**
* Edit static config file.
* If there are quorum information in static file, e.g. "server.X", "group",
* it will remove them.
* If it needs to erase client port information left by the old config,
* "eraseClientPortAddress" should be set true.
* It should also updates dynamic file pointer on reconfig.
*/
public static void editStaticConfig(final String configFileStr, final String dynamicFileStr, final boolean eraseClientPortAddress) throws IOException {
// Some tests may not have a static config file.
if (configFileStr == null)
return;
File configFile = (new VerifyingFileFactory.Builder(LOG).warnForRelativePath().failForNonExistingPath().build()).create(configFileStr);
final File dynamicFile = (new VerifyingFileFactory.Builder(LOG).warnForRelativePath().failForNonExistingPath().build()).create(dynamicFileStr);
final Properties cfg = new Properties();
FileInputStream in = new FileInputStream(configFile);
try {
cfg.load(in);
} finally {
in.close();
}
new AtomicFileWritingIdiom(new File(configFileStr), new WriterStatement() {
@Override
public void write(Writer out) throws IOException {
for (Entry<Object, Object> entry : cfg.entrySet()) {
String key = entry.getKey().toString().trim();
if (key.startsWith("server.") || key.startsWith("group") || key.startsWith("weight") || key.startsWith("dynamicConfigFile") || key.startsWith("peerType") || (eraseClientPortAddress && (key.startsWith("clientPort") || key.startsWith("clientPortAddress")))) {
// not writing them back to static file
continue;
}
String value = entry.getValue().toString().trim();
out.write(key.concat("=").concat(value).concat("\n"));
}
// updates the dynamic file pointer
String dynamicConfigFilePath = PathUtils.normalizeFileSystemPath(dynamicFile.getCanonicalPath());
out.write("dynamicConfigFile=".concat(dynamicConfigFilePath).concat("\n"));
}
});
}
Aggregations