/** * If a backup file has not yet been created, reserve a new one with the following file name format: * {@code {yyyyMMdd}T{HHmmss}.{unique long}.old}. * @return a {@link Pair} containing the unique filename prefix for this account update and the path to use for * previous state backups. * @throws IOException */ private Pair<String, Path> reserveBackupFile() throws IOException { String timestamp = LocalDateTime.now().format(TIMESTAMP_FORMATTER); for (long n = 0; n < Long.MAX_VALUE; n++) { String prefix = timestamp + SEP + n + SEP; Path filepath = backupDirPath.resolve(prefix + OLD_STATE_SUFFIX); try { return new Pair<>(prefix, Files.createFile(filepath)); } catch (FileAlreadyExistsException e) { // retry with a new suffix. } } throw new IOException("Could not create a unique file with timestamp " + timestamp); }
/** * Save the zookeeper state from before the update to disk. * The following file name format will be used: {@code {yyyyMMdd}T{HHmmss}.{unique long}.old}. * If there are multiple files with the same timestamp, the unique long will prevent the file names from clashing. */ private void maybePersistOldState(Map<String, String> oldState) { if (backupPrefixAndPath != null) { try { writeBackup(backupPrefixAndPath.getSecond(), oldState); } catch (Exception e) { logger.error("Could not write previous state backup file", e); accountServiceMetrics.backupErrorCount.inc(); } } }
/** * Save the zookeeper state from after the update to disk. This will only save the file if the update succeeded * and the old state backup file was successfully reserved. * The following file name format will be used: {@code {yyyyMMdd}T{HHmmss}.{unique long}.new}. * @param succeeded {@code true} iff the update succeeded. */ void maybePersistNewState(boolean succeeded) { if (backupPrefixAndPath != null && succeeded) { try { Path filepath = backupDirPath.resolve(backupPrefixAndPath.getFirst() + NEW_STATE_SUFFIX); writeBackup(filepath, potentialNewState); } catch (Exception e) { logger.error("Could not write new state backup file", e); accountServiceMetrics.backupErrorCount.inc(); } } }