Add Payload.serializedSize()
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / persisted / SimpleReplicatedLogEntrySerializer.java
1 /*
2  * Copyright (c) 2018 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.raft.persisted;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import akka.actor.ExtendedActorSystem;
14 import akka.serialization.JSerializer;
15 import akka.util.ClassLoaderObjectInputStream;
16 import java.io.ByteArrayInputStream;
17 import java.io.ByteArrayOutputStream;
18 import java.io.IOException;
19 import org.apache.commons.lang3.SerializationUtils;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Specialized serializer for {@link SimpleReplicatedLogEntry} that optimizes serialization.
25  *
26  * @author Thomas Pantelis
27  */
28 public class SimpleReplicatedLogEntrySerializer extends JSerializer {
29     private static final Logger LOG = LoggerFactory.getLogger(SimpleReplicatedLogEntrySerializer.class);
30
31     private final ExtendedActorSystem system;
32
33     public SimpleReplicatedLogEntrySerializer(final ExtendedActorSystem system) {
34         this.system = requireNonNull(system);
35     }
36
37     @Override
38     public int identifier() {
39         return 97439500;
40     }
41
42     @Override
43     public boolean includeManifest() {
44         return false;
45     }
46
47     @Override
48     public byte[] toBinary(Object obj) {
49         checkArgument(obj instanceof SimpleReplicatedLogEntry, "Unsupported object type %s", obj.getClass());
50
51         SimpleReplicatedLogEntry replicatedLogEntry = (SimpleReplicatedLogEntry)obj;
52         final int estimatedSerializedSize = replicatedLogEntry.serializedSize();
53
54         final ByteArrayOutputStream bos = new ByteArrayOutputStream(estimatedSerializedSize);
55         SerializationUtils.serialize(replicatedLogEntry, bos);
56         final byte[] bytes = bos.toByteArray();
57
58         LOG.debug("Estimated serialized size {}, data size {} for payload: {}. Actual serialized size: {}",
59             estimatedSerializedSize, replicatedLogEntry.getData().size(), replicatedLogEntry.getData(), bytes.length);
60
61         return bytes;
62     }
63
64     @Override
65     public Object fromBinaryJava(byte[] bytes, Class<?> manifest) {
66         try (ClassLoaderObjectInputStream is = new ClassLoaderObjectInputStream(system.dynamicAccess().classLoader(),
67                 new ByteArrayInputStream(bytes))) {
68             return is.readObject();
69         } catch (IOException | ClassNotFoundException e) {
70             throw new IllegalStateException("Failed to deserialize object", e);
71         }
72     }
73 }