Search in sources :

Example 1 with AtomicFileWritingIdiom

use of org.apache.zookeeper.common.AtomicFileWritingIdiom 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));
        }
    });
}
Also used : WriterStatement(org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement) AtomicFileWritingIdiom(org.apache.zookeeper.common.AtomicFileWritingIdiom) IOException(java.io.IOException) File(java.io.File) Writer(java.io.Writer) StringWriter(java.io.StringWriter)

Example 2 with AtomicFileWritingIdiom

use of org.apache.zookeeper.common.AtomicFileWritingIdiom 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"));
        }
    });
}
Also used : Entry(java.util.Map.Entry) WriterStatement(org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement) VerifyingFileFactory(org.apache.zookeeper.server.util.VerifyingFileFactory) AtomicFileWritingIdiom(org.apache.zookeeper.common.AtomicFileWritingIdiom) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream) Writer(java.io.Writer)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 Writer (java.io.Writer)2 AtomicFileWritingIdiom (org.apache.zookeeper.common.AtomicFileWritingIdiom)2 WriterStatement (org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement)2 FileInputStream (java.io.FileInputStream)1 StringWriter (java.io.StringWriter)1 Entry (java.util.Map.Entry)1 Properties (java.util.Properties)1 VerifyingFileFactory (org.apache.zookeeper.server.util.VerifyingFileFactory)1