Example usage for org.apache.zookeeper.server.persistence Util getZxidFromName

List of usage examples for org.apache.zookeeper.server.persistence Util getZxidFromName

Introduction

In this page you can find the example usage for org.apache.zookeeper.server.persistence Util getZxidFromName.

Prototype

public static long getZxidFromName(String name, String prefix) 

Source Link

Document

Extracts zxid from the file name.

Usage

From source file:com.nesscomputing.service.discovery.server.zookeeper.ZookeeperCleanupJob.java

License:Apache License

@Override
public void execute(final JobExecutionContext context) throws JobExecutionException {
    try {//w  ww .j a va2 s. c o m
        LOG.info("Running zookeeper cleanup.");

        // found any valid recent snapshots?
        // files to exclude from deletion
        final Set<File> excludes = Sets.newHashSet();
        final List<File> snaps = fileTxnSnapLog.findNRecentSnapshots(KEEP_VERSIONS_COUNT);

        if (!snaps.isEmpty()) {
            File snapFile = null;

            for (Iterator<File> it = snaps.iterator(); it.hasNext();) {
                snapFile = it.next();
                excludes.add(snapFile);
            }

            // add the snapshots for the last file in the list.
            final long zxid = Util.getZxidFromName(snapFile.getName(), "snapshot");
            excludes.addAll(Arrays.asList(fileTxnSnapLog.getSnapshotLogs(zxid)));

            final List<File> files = Lists.newArrayList();

            // add all non-excluded log files
            files.addAll(Arrays
                    .asList(fileTxnSnapLog.getDataDir().listFiles(new PrefixExcludesFilter("log.", excludes))));

            // add all non-excluded snapshot files to the deletion list
            files.addAll(Arrays.asList(
                    fileTxnSnapLog.getSnapDir().listFiles(new PrefixExcludesFilter("snapshot.", excludes))));

            // remove the old files
            if (!files.isEmpty()) {
                for (File file : files) {
                    LOG.info("Removing file %s (%s)", file.getPath(),
                            DateFormat.getDateTimeInstance().format(file.lastModified()));

                    if (!file.delete()) {
                        LOG.warn("Failed to remove %s", file.getPath());
                    }
                }
            } else {
                LOG.info("No files to remove.");
            }
        }
    } catch (IOException ioe) {
        throw new JobExecutionException(ioe);
    }
}