Remove use of thread-local output 67/82467/3
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 3 Jun 2019 19:12:44 +0000 (21:12 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 10 Jun 2019 11:16:01 +0000 (13:16 +0200)
All callers can be safely migrated to using a non-shared writer,
as they all are expected to be apex implementations.

JIRA: CONTROLLER-1888
Change-Id: I87cfa1d3ec415b79c40c29a91ccbd8da2869b27a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 6a32d897d2dcb4fa54977b3b2defe76dc0a5d5e2)

opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeDataOutput.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/SerializationUtils.java
opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/SerializationUtilsTest.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/AbstractRead.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/ReadDataReply.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/RegisterDataTreeChangeListener.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/modification/DeleteModification.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/modification/WriteModification.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/MetadataShardDataTreeSnapshot.java
opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/messages/ExecuteRpc.java
opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/messages/RpcResponse.java

index 7c42da18234a41388b5867c9a514cabd6bd23299..dd267a0bfaca15b34c77c1fb8d85b559bedc186a 100644 (file)
@@ -10,6 +10,8 @@ package org.opendaylight.controller.cluster.datastore.node.utils.stream;
 import com.google.common.annotations.Beta;
 import java.io.DataOutput;
 import java.io.IOException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -21,7 +23,7 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath;
  */
 @Beta
 public interface NormalizedNodeDataOutput extends AutoCloseable, DataOutput {
-    void writeNormalizedNode(NormalizedNode<?, ?> normalizedNode) throws IOException;
+    void writeNormalizedNode(@NonNull NormalizedNode<?, ?> normalizedNode) throws IOException;
 
     void writePathArgument(PathArgument pathArgument) throws IOException;
 
@@ -31,4 +33,13 @@ public interface NormalizedNodeDataOutput extends AutoCloseable, DataOutput {
 
     @Override
     void close() throws IOException;
+
+    default void writeOptionalNormalizedNode(final @Nullable NormalizedNode<?, ?> normalizedNode) throws IOException {
+        if (normalizedNode != null) {
+            writeBoolean(true);
+            writeNormalizedNode(normalizedNode);
+        } else {
+            writeBoolean(false);
+        }
+    }
 }
index 3a04347a54faeef7fe74cfedea65ef55a0d8ad42..46bcf0892936017579ec304586f4bb1e8a6d64e1 100644 (file)
@@ -15,6 +15,8 @@ import java.io.DataInputStream;
 import java.io.DataOutput;
 import java.io.DataOutputStream;
 import java.io.IOException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
@@ -24,6 +26,7 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
  * @author Thomas Pantelis
  */
 public final class SerializationUtils {
+    @Deprecated
     public static final ThreadLocal<NormalizedNodeDataOutput> REUSABLE_WRITER_TL = new ThreadLocal<>();
     public static final ThreadLocal<NormalizedNodeDataInput> REUSABLE_READER_TL = new ThreadLocal<>();
 
@@ -34,7 +37,8 @@ public final class SerializationUtils {
         void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
     }
 
-    private static NormalizedNodeDataOutput streamWriter(DataOutput out) {
+    @Deprecated
+    private static NormalizedNodeDataOutput streamWriter(final DataOutput out) {
         NormalizedNodeDataOutput streamWriter = REUSABLE_WRITER_TL.get();
         if (streamWriter == null) {
             streamWriter = NormalizedNodeInputOutput.newDataOutput(out);
@@ -43,7 +47,7 @@ public final class SerializationUtils {
         return streamWriter;
     }
 
-    private static NormalizedNodeDataInput streamReader(DataInput in) throws IOException {
+    private static NormalizedNodeDataInput streamReader(final DataInput in) throws IOException {
         NormalizedNodeDataInput streamReader = REUSABLE_READER_TL.get();
         if (streamReader == null) {
             streamReader = NormalizedNodeInputOutput.newDataInput(in);
@@ -52,8 +56,9 @@ public final class SerializationUtils {
         return streamReader;
     }
 
-    public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node,
-            DataOutput out) {
+    @Deprecated
+    public static void serializePathAndNode(final YangInstanceIdentifier path, final NormalizedNode<?, ?> node,
+            final DataOutput out) {
         Preconditions.checkNotNull(path);
         Preconditions.checkNotNull(node);
         try {
@@ -61,12 +66,11 @@ public final class SerializationUtils {
             streamWriter.writeNormalizedNode(node);
             streamWriter.writeYangInstanceIdentifier(path);
         } catch (IOException e) {
-            throw new IllegalArgumentException(String.format("Error serializing path %s and Node %s",
-                    path, node), e);
+            throw new IllegalArgumentException(String.format("Error serializing path %s and Node %s", path, node), e);
         }
     }
 
-    public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
+    public static <T> void deserializePathAndNode(final DataInput in, final T instance, final Applier<T> applier) {
         try {
             NormalizedNodeDataInput streamReader = streamReader(in);
             NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
@@ -77,7 +81,7 @@ public final class SerializationUtils {
         }
     }
 
-    private static NormalizedNode<?, ?> tryDeserializeNormalizedNode(DataInput in) throws IOException {
+    private static NormalizedNode<?, ?> tryDeserializeNormalizedNode(final DataInput in) throws IOException {
         boolean present = in.readBoolean();
         if (present) {
             NormalizedNodeDataInput streamReader = streamReader(in);
@@ -87,7 +91,7 @@ public final class SerializationUtils {
         return null;
     }
 
-    public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
+    public static NormalizedNode<?, ?> deserializeNormalizedNode(final DataInput in) {
         try {
             return tryDeserializeNormalizedNode(in);
         } catch (IOException e) {
@@ -95,7 +99,7 @@ public final class SerializationUtils {
         }
     }
 
-    public static NormalizedNode<?, ?> deserializeNormalizedNode(byte [] bytes) {
+    public static NormalizedNode<?, ?> deserializeNormalizedNode(final byte [] bytes) {
         try {
             return tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
         } catch (IOException e) {
@@ -103,7 +107,8 @@ public final class SerializationUtils {
         }
     }
 
-    public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
+    @Deprecated
+    public static void serializeNormalizedNode(final NormalizedNode<?, ?> node, final DataOutput out) {
         try {
             out.writeBoolean(node != null);
             if (node != null) {
@@ -111,18 +116,31 @@ public final class SerializationUtils {
                 streamWriter.writeNormalizedNode(node);
             }
         } catch (IOException e) {
-            throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s",
-                    node), e);
+            throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s", node), e);
         }
     }
 
-    public static byte [] serializeNormalizedNode(NormalizedNode<?, ?> node) {
+    public static byte [] serializeNormalizedNode(final NormalizedNode<?, ?> node) {
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         serializeNormalizedNode(node, new DataOutputStream(bos));
         return bos.toByteArray();
     }
 
-    public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
+    public static void writeNormalizedNode(final DataOutput out, final @Nullable NormalizedNode<?, ?> node)
+            throws IOException {
+        if (node != null) {
+            out.writeBoolean(true);
+
+            try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
+                stream.writeNormalizedNode(node);
+            }
+        } else {
+            out.writeBoolean(false);
+        }
+    }
+
+    @Deprecated
+    public static void serializePath(final YangInstanceIdentifier path, final DataOutput out) {
         Preconditions.checkNotNull(path);
         try {
             NormalizedNodeDataOutput streamWriter = streamWriter(out);
@@ -132,7 +150,30 @@ public final class SerializationUtils {
         }
     }
 
-    public static YangInstanceIdentifier deserializePath(DataInput in) {
+    public static void writePath(final DataOutput out, final @NonNull YangInstanceIdentifier path)
+            throws IOException {
+        try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
+            stream.writeYangInstanceIdentifier(path);
+        }
+    }
+
+    public static void writeNodeAndPath(final DataOutput out, final YangInstanceIdentifier path,
+            final NormalizedNode<?, ?> node) throws IOException {
+        try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
+            stream.writeNormalizedNode(node);
+            stream.writeYangInstanceIdentifier(path);
+        }
+    }
+
+    public static void writePathAndNode(final DataOutput out, final YangInstanceIdentifier path,
+            final NormalizedNode<?, ?> node) throws IOException {
+        try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
+            stream.writeYangInstanceIdentifier(path);
+            stream.writeNormalizedNode(node);
+        }
+    }
+
+    public static YangInstanceIdentifier deserializePath(final DataInput in) {
         try {
             NormalizedNodeDataInput streamReader = streamReader(in);
             return streamReader.readYangInstanceIdentifier();
index 50d06ada231c23ca611d1ad0234288ed9bedfce3..f0cfdc41f23fa82cc23b6e4be2d51958944f179b 100644 (file)
@@ -13,6 +13,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.DataInputStream;
 import java.io.DataOutput;
 import java.io.DataOutputStream;
+import java.io.IOException;
 import java.nio.charset.Charset;
 import java.util.Arrays;
 import java.util.Set;
@@ -47,11 +48,10 @@ public class SerializationUtilsTest {
     private static final QName CONTAINER_Q_NAME = QName.create("ns-1", "2017-03-17", "container1");
 
     @Test
-    public void testSerializeDeserializeNodes() {
+    public void testSerializeDeserializeNodes() throws IOException {
         final NormalizedNode<?, ?> normalizedNode = createNormalizedNode();
         final byte[] bytes = SerializationUtils.serializeNormalizedNode(normalizedNode);
         Assert.assertEquals(normalizedNode, SerializationUtils.deserializeNormalizedNode(bytes));
-
     }
 
     @Test
@@ -72,7 +72,7 @@ public class SerializationUtilsTest {
     }
 
     @Test
-    public void testSerializeDeserializePath() {
+    public void testSerializeDeserializePath() throws IOException {
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
         final DataOutput out = new DataOutputStream(bos);
         final YangInstanceIdentifier path = YangInstanceIdentifier.builder()
@@ -81,19 +81,19 @@ public class SerializationUtilsTest {
                 .node(listId("list1", "keyName1", "keyValue1"))
                 .node(leafSetId("leafSer1", "leafSetValue1"))
                 .build();
-        SerializationUtils.serializePath(path, out);
+        SerializationUtils.writePath(out, path);
         final YangInstanceIdentifier deserialized =
                 SerializationUtils.deserializePath(new DataInputStream(new ByteArrayInputStream(bos.toByteArray())));
         Assert.assertEquals(path, deserialized);
     }
 
     @Test
-    public void testSerializeDeserializePathAndNode() {
+    public void testSerializeDeserializePathAndNode() throws IOException {
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
         final DataOutput out = new DataOutputStream(bos);
         final NormalizedNode<?, ?> node = createNormalizedNode();
         final YangInstanceIdentifier path = YangInstanceIdentifier.create(id("container1"));
-        SerializationUtils.serializePathAndNode(path, node, out);
+        SerializationUtils.writeNodeAndPath(out, path, node);
         final DataInputStream in = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
         final AtomicBoolean applierCalled = new AtomicBoolean(false);
         SerializationUtils.deserializePathAndNode(in, applierCalled, (instance, deserializedPath, deserializedNode) -> {
index 94e1a3779c5fba68a6e304ef8ff899d2bedab1e8..5b92545c82ad132f45256962fb01eab93dbf6ad2 100644 (file)
@@ -41,18 +41,18 @@ public abstract class AbstractRead<T> extends VersionedExternalizableMessage {
     }
 
     @Override
-    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+    public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
         super.readExternal(in);
         path = SerializationUtils.deserializePath(in);
     }
 
     @Override
-    public void writeExternal(ObjectOutput out) throws IOException {
+    public void writeExternal(final ObjectOutput out) throws IOException {
         super.writeExternal(out);
-        SerializationUtils.serializePath(path, out);
+        SerializationUtils.writePath(out, path);
     }
 
-    public AbstractRead<T> asVersion(short version) {
+    public AbstractRead<T> asVersion(final short version) {
         return version == getVersion() ? this : newInstance(version);
     }
 
index eda5c262802669bfcc9eca9aed4f398187728edd..93bf2ece0cc6e002b189e6cf4f7deee6155cb854 100644 (file)
@@ -22,7 +22,7 @@ public class ReadDataReply extends VersionedExternalizableMessage {
     public ReadDataReply() {
     }
 
-    public ReadDataReply(NormalizedNode<?, ?> normalizedNode, short version) {
+    public ReadDataReply(final NormalizedNode<?, ?> normalizedNode, final short version) {
         super(version);
         this.normalizedNode = normalizedNode;
     }
@@ -32,22 +32,22 @@ public class ReadDataReply extends VersionedExternalizableMessage {
     }
 
     @Override
-    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+    public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
         super.readExternal(in);
         normalizedNode = SerializationUtils.deserializeNormalizedNode(in);
     }
 
     @Override
-    public void writeExternal(ObjectOutput out) throws IOException {
+    public void writeExternal(final ObjectOutput out) throws IOException {
         super.writeExternal(out);
-        SerializationUtils.serializeNormalizedNode(normalizedNode, out);
+        SerializationUtils.writeNormalizedNode(out, normalizedNode);
     }
 
-    public static ReadDataReply fromSerializable(Object serializable) {
+    public static ReadDataReply fromSerializable(final Object serializable) {
         return (ReadDataReply) serializable;
     }
 
-    public static boolean isSerializedType(Object message) {
+    public static boolean isSerializedType(final Object message) {
         return message instanceof ReadDataReply;
     }
 }
index f790d1d4b2ada70a7938ed5316850d6ac22ae95c..83542a0de285fbafe05f5b3e6540b232d6f8873f 100644 (file)
@@ -7,9 +7,10 @@
  */
 package org.opendaylight.controller.cluster.datastore.messages;
 
+import static java.util.Objects.requireNonNull;
+
 import akka.actor.ActorPath;
 import akka.actor.ActorRef;
-import com.google.common.base.Preconditions;
 import java.io.Externalizable;
 import java.io.IOException;
 import java.io.ObjectInput;
@@ -23,6 +24,7 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
  */
 public final class RegisterDataTreeChangeListener implements Externalizable {
     private static final long serialVersionUID = 1L;
+
     private ActorRef dataTreeChangeListenerPath;
     private YangInstanceIdentifier path;
     private boolean registerOnAllInstances;
@@ -33,8 +35,8 @@ public final class RegisterDataTreeChangeListener implements Externalizable {
 
     public RegisterDataTreeChangeListener(final YangInstanceIdentifier path, final ActorRef dataTreeChangeListenerPath,
             final boolean registerOnAllInstances) {
-        this.path = Preconditions.checkNotNull(path);
-        this.dataTreeChangeListenerPath = Preconditions.checkNotNull(dataTreeChangeListenerPath);
+        this.path = requireNonNull(path);
+        this.dataTreeChangeListenerPath = requireNonNull(dataTreeChangeListenerPath);
         this.registerOnAllInstances = registerOnAllInstances;
     }
 
@@ -53,7 +55,7 @@ public final class RegisterDataTreeChangeListener implements Externalizable {
     @Override
     public void writeExternal(final ObjectOutput out) throws IOException {
         out.writeObject(dataTreeChangeListenerPath);
-        SerializationUtils.serializePath(path, out);
+        SerializationUtils.writePath(out, path);
         out.writeBoolean(registerOnAllInstances);
     }
 
index 0a76cf9e5b6644f8cf75dde97ce01ec1d1196731..c1cd61dea0a66fbe94835cfbe7312ad30bbed4f9 100644 (file)
@@ -62,8 +62,8 @@ public class DeleteModification extends AbstractModification {
     }
 
     @Override
-    public void writeExternal(final ObjectOutput out) {
-        SerializationUtils.serializePath(getPath(), out);
+    public void writeExternal(final ObjectOutput out) throws IOException {
+        SerializationUtils.writePath(out, getPath());
     }
 
     @Override
index 1abf227ffb5f2900741533f0d2bb32916ccf55ae..80de2e8b07d4c60dd36be6d7e4eb202b1af72b8a 100644 (file)
@@ -72,8 +72,8 @@ public class WriteModification extends AbstractModification {
     }
 
     @Override
-    public void writeExternal(final ObjectOutput out) {
-        SerializationUtils.serializePathAndNode(getPath(), data, out);
+    public void writeExternal(final ObjectOutput out) throws IOException {
+        SerializationUtils.writeNodeAndPath(out, getPath(), data);
     }
 
     @Deprecated
index 05dc4cc3982268003be9c0d17f4a2bce2708ac1e..fa8877fd9fef9b7d3d6f10d475c59793cd92fffd 100644 (file)
@@ -59,7 +59,7 @@ public final class MetadataShardDataTreeSnapshot extends AbstractVersionedShardD
                 out.writeObject(m);
             }
 
-            SerializationUtils.serializeNormalizedNode(rootNode, out);
+            SerializationUtils.writeNormalizedNode(out, rootNode);
         }
 
         @Override
index dca81dc0b7bb29a069d514dca28f0650923011fb..e32ef3dc0df6c5bae9ffd808d7c3fe2494437e7d 100644 (file)
@@ -72,18 +72,19 @@ public final class ExecuteRpc implements Serializable {
         public Proxy() {
         }
 
-        Proxy(ExecuteRpc executeRpc) {
+        Proxy(final ExecuteRpc executeRpc) {
             this.executeRpc = executeRpc;
         }
 
         @Override
-        public void writeExternal(ObjectOutput out) throws IOException {
+        public void writeExternal(final ObjectOutput out) throws IOException {
+            // FIXME: QName is a WritableObject
             out.writeObject(executeRpc.getRpc());
-            SerializationUtils.serializeNormalizedNode(executeRpc.getInputNormalizedNode(), out);
+            SerializationUtils.writeNormalizedNode(out, executeRpc.getInputNormalizedNode());
         }
 
         @Override
-        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+        public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
             QName qname = (QName) in.readObject();
             executeRpc = new ExecuteRpc(SerializationUtils.deserializeNormalizedNode(in), qname);
         }
index 02d0f1f185116ce30ce0d46754ff26aefe4dd86f..35e561178102187aef2c5453160b4c61d9d0be65 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.controller.remote.rpc.messages;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.io.Externalizable;
+import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
 import java.io.Serializable;
@@ -47,17 +48,17 @@ public class RpcResponse implements Serializable {
         public Proxy() {
         }
 
-        Proxy(RpcResponse rpcResponse) {
+        Proxy(final RpcResponse rpcResponse) {
             this.rpcResponse = rpcResponse;
         }
 
         @Override
-        public void writeExternal(ObjectOutput out) {
-            SerializationUtils.serializeNormalizedNode(rpcResponse.getResultNormalizedNode(), out);
+        public void writeExternal(final ObjectOutput out) throws IOException {
+            SerializationUtils.writeNormalizedNode(out, rpcResponse.getResultNormalizedNode());
         }
 
         @Override
-        public void readExternal(ObjectInput in) {
+        public void readExternal(final ObjectInput in) {
             rpcResponse = new RpcResponse(SerializationUtils.deserializeNormalizedNode(in));
         }