*/
package org.opendaylight.controller.cluster.persistence;
+import static com.google.common.base.Preconditions.checkArgument;
+
import akka.actor.ExtendedActorSystem;
import akka.dispatch.Futures;
import akka.persistence.SelectedSnapshot;
import com.google.common.io.ByteStreams;
import com.typesafe.config.Config;
import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
-import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.concurrent.ExecutionContext;
private Object deserialize(final File file) throws IOException {
return JavaSerializer.currentSystem().withValue((ExtendedActorSystem) context().system(),
(Callable<Object>) () -> {
- try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
+ try (ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)))) {
return in.readObject();
} catch (ClassNotFoundException e) {
throw new IOException("Error loading snapshot file " + file, e);
LOG.debug("Saving to temp file: {}", temp);
- try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(temp))) {
+ try (ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(temp)))) {
out.writeObject(snapshot);
} catch (IOException e) {
LOG.error("Error saving snapshot file {}. Deleting file..", temp, e);
LOG.debug("Deleting files: {}", files);
- files.forEach(File::delete);
+ files.forEach(file -> {
+ try {
+ Files.delete(file.toPath());
+ } catch (IOException | SecurityException e) {
+ LOG.error("Unable to delete snapshot file: {}, persistenceId: {} ", file, persistenceId);
+ }
+ });
return null;
}
LOG.debug("Deleting files: {}", files);
- files.forEach(File::delete);
+ files.forEach(file -> {
+ try {
+ Files.delete(file.toPath());
+ } catch (IOException | SecurityException e) {
+ LOG.error("Unable to delete snapshot file: {}", file);
+ }
+ });
return null;
}
.filter(criteria::matches).collect(Collectors.toList());
}
- private static Stream<SnapshotMetadata> toStream(@Nullable final SnapshotMetadata md) {
+ private static Stream<SnapshotMetadata> toStream(final @Nullable SnapshotMetadata md) {
return md != null ? Stream.of(md) : Stream.empty();
}
- @Nullable
- private static SnapshotMetadata extractMetadata(final File file) {
+ private static @Nullable SnapshotMetadata extractMetadata(final File file) {
String name = file.getName();
int sequenceNumberEndIndex = name.lastIndexOf('-');
int persistenceIdEndIndex = name.lastIndexOf('-', sequenceNumberEndIndex - 1);
@VisibleForTesting
static int compare(final SnapshotMetadata m1, final SnapshotMetadata m2) {
- return (int) (!m1.persistenceId().equals(m2.persistenceId())
- ? m1.persistenceId().compareTo(m2.persistenceId()) :
- m1.sequenceNr() != m2.sequenceNr() ? m1.sequenceNr() - m2.sequenceNr() :
- m1.timestamp() != m2.timestamp() ? m1.timestamp() - m2.timestamp() : 0);
+ checkArgument(m1.persistenceId().equals(m2.persistenceId()),
+ "Persistence id does not match. id1: %s, id2: %s", m1.persistenceId(), m2.persistenceId());
+ final int cmp = Long.compare(m1.timestamp(), m2.timestamp());
+ return cmp != 0 ? cmp : Long.compare(m1.sequenceNr(), m2.sequenceNr());
}
}