snapshot compression

Change-Id: I0de15a66a129d2966a196b2de5491456aacc20e2
This commit is contained in:
Yuta HIGUCHI 2014-11-06 22:17:37 -08:00
parent 2ee20002b0
commit a7680a3448

View File

@ -2,10 +2,14 @@ package org.onlab.onos.store.service.impl;
import static org.slf4j.LoggerFactory.getLogger; import static org.slf4j.LoggerFactory.getLogger;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
import net.kuujo.copycat.Command; import net.kuujo.copycat.Command;
import net.kuujo.copycat.Query; import net.kuujo.copycat.Query;
@ -23,6 +27,7 @@ import org.slf4j.Logger;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.io.ByteStreams;
/** /**
* StateMachine whose transitions are coordinated/replicated * StateMachine whose transitions are coordinated/replicated
@ -50,6 +55,8 @@ public class DatabaseStateMachine implements StateMachine {
private State state = new State(); private State state = new State();
private boolean compressSnapshot = false;
@Command @Command
public boolean createTable(String tableName) { public boolean createTable(String tableName) {
return state.getTables().putIfAbsent(tableName, Maps.newHashMap()) == null; return state.getTables().putIfAbsent(tableName, Maps.newHashMap()) == null;
@ -214,7 +221,16 @@ public class DatabaseStateMachine implements StateMachine {
@Override @Override
public byte[] takeSnapshot() { public byte[] takeSnapshot() {
try { try {
return SERIALIZER.encode(state); if (compressSnapshot) {
byte[] input = SERIALIZER.encode(state);
ByteArrayOutputStream comp = new ByteArrayOutputStream(input.length);
DeflaterOutputStream compressor = new DeflaterOutputStream(comp);
compressor.write(input, 0, input.length);
compressor.close();
return comp.toByteArray();
} else {
return SERIALIZER.encode(state);
}
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to take snapshot", e); log.error("Failed to take snapshot", e);
throw new SnapshotException(e); throw new SnapshotException(e);
@ -224,7 +240,14 @@ public class DatabaseStateMachine implements StateMachine {
@Override @Override
public void installSnapshot(byte[] data) { public void installSnapshot(byte[] data) {
try { try {
this.state = SERIALIZER.decode(data); if (compressSnapshot) {
ByteArrayInputStream in = new ByteArrayInputStream(data);
InflaterInputStream decompressor = new InflaterInputStream(in);
ByteStreams.toByteArray(decompressor);
this.state = SERIALIZER.decode(ByteStreams.toByteArray(decompressor));
} else {
this.state = SERIALIZER.decode(data);
}
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to install from snapshot", e); log.error("Failed to install from snapshot", e);
throw new SnapshotException(e); throw new SnapshotException(e);