snapshot compression
Change-Id: I0de15a66a129d2966a196b2de5491456aacc20e2
Showing
1 changed file
with
25 additions
and
2 deletions
| ... | @@ -2,10 +2,14 @@ package org.onlab.onos.store.service.impl; | ... | @@ -2,10 +2,14 @@ package org.onlab.onos.store.service.impl; |
| 2 | 2 | ||
| 3 | import static org.slf4j.LoggerFactory.getLogger; | 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | 4 | ||
| 5 | +import java.io.ByteArrayInputStream; | ||
| 6 | +import java.io.ByteArrayOutputStream; | ||
| 5 | import java.util.ArrayList; | 7 | import java.util.ArrayList; |
| 6 | import java.util.Arrays; | 8 | import java.util.Arrays; |
| 7 | import java.util.List; | 9 | import java.util.List; |
| 8 | import java.util.Map; | 10 | import java.util.Map; |
| 11 | +import java.util.zip.DeflaterOutputStream; | ||
| 12 | +import java.util.zip.InflaterInputStream; | ||
| 9 | 13 | ||
| 10 | import net.kuujo.copycat.Command; | 14 | import net.kuujo.copycat.Command; |
| 11 | import net.kuujo.copycat.Query; | 15 | import net.kuujo.copycat.Query; |
| ... | @@ -23,6 +27,7 @@ import org.slf4j.Logger; | ... | @@ -23,6 +27,7 @@ import org.slf4j.Logger; |
| 23 | 27 | ||
| 24 | import com.google.common.collect.ImmutableList; | 28 | import com.google.common.collect.ImmutableList; |
| 25 | import com.google.common.collect.Maps; | 29 | import com.google.common.collect.Maps; |
| 30 | +import com.google.common.io.ByteStreams; | ||
| 26 | 31 | ||
| 27 | /** | 32 | /** |
| 28 | * StateMachine whose transitions are coordinated/replicated | 33 | * StateMachine whose transitions are coordinated/replicated |
| ... | @@ -50,6 +55,8 @@ public class DatabaseStateMachine implements StateMachine { | ... | @@ -50,6 +55,8 @@ public class DatabaseStateMachine implements StateMachine { |
| 50 | 55 | ||
| 51 | private State state = new State(); | 56 | private State state = new State(); |
| 52 | 57 | ||
| 58 | + private boolean compressSnapshot = false; | ||
| 59 | + | ||
| 53 | @Command | 60 | @Command |
| 54 | public boolean createTable(String tableName) { | 61 | public boolean createTable(String tableName) { |
| 55 | return state.getTables().putIfAbsent(tableName, Maps.newHashMap()) == null; | 62 | return state.getTables().putIfAbsent(tableName, Maps.newHashMap()) == null; |
| ... | @@ -214,7 +221,16 @@ public class DatabaseStateMachine implements StateMachine { | ... | @@ -214,7 +221,16 @@ public class DatabaseStateMachine implements StateMachine { |
| 214 | @Override | 221 | @Override |
| 215 | public byte[] takeSnapshot() { | 222 | public byte[] takeSnapshot() { |
| 216 | try { | 223 | try { |
| 217 | - return SERIALIZER.encode(state); | 224 | + if (compressSnapshot) { |
| 225 | + byte[] input = SERIALIZER.encode(state); | ||
| 226 | + ByteArrayOutputStream comp = new ByteArrayOutputStream(input.length); | ||
| 227 | + DeflaterOutputStream compressor = new DeflaterOutputStream(comp); | ||
| 228 | + compressor.write(input, 0, input.length); | ||
| 229 | + compressor.close(); | ||
| 230 | + return comp.toByteArray(); | ||
| 231 | + } else { | ||
| 232 | + return SERIALIZER.encode(state); | ||
| 233 | + } | ||
| 218 | } catch (Exception e) { | 234 | } catch (Exception e) { |
| 219 | log.error("Failed to take snapshot", e); | 235 | log.error("Failed to take snapshot", e); |
| 220 | throw new SnapshotException(e); | 236 | throw new SnapshotException(e); |
| ... | @@ -224,7 +240,14 @@ public class DatabaseStateMachine implements StateMachine { | ... | @@ -224,7 +240,14 @@ public class DatabaseStateMachine implements StateMachine { |
| 224 | @Override | 240 | @Override |
| 225 | public void installSnapshot(byte[] data) { | 241 | public void installSnapshot(byte[] data) { |
| 226 | try { | 242 | try { |
| 227 | - this.state = SERIALIZER.decode(data); | 243 | + if (compressSnapshot) { |
| 244 | + ByteArrayInputStream in = new ByteArrayInputStream(data); | ||
| 245 | + InflaterInputStream decompressor = new InflaterInputStream(in); | ||
| 246 | + ByteStreams.toByteArray(decompressor); | ||
| 247 | + this.state = SERIALIZER.decode(ByteStreams.toByteArray(decompressor)); | ||
| 248 | + } else { | ||
| 249 | + this.state = SERIALIZER.decode(data); | ||
| 250 | + } | ||
| 228 | } catch (Exception e) { | 251 | } catch (Exception e) { |
| 229 | log.error("Failed to install from snapshot", e); | 252 | log.error("Failed to install from snapshot", e); |
| 230 | throw new SnapshotException(e); | 253 | throw new SnapshotException(e); | ... | ... |
-
Please register or login to post a comment