tzulitai commented on a change in pull request #6909: [FLINK-8995][tests] Add 
keyed state that uses custom stateful seriali…
URL: https://github.com/apache/flink/pull/6909#discussion_r227863415
 
 

 ##########
 File path: 
flink-end-to-end-tests/flink-datastream-allround-test/src/main/java/org/apache/flink/streaming/tests/artificialstate/StatefulComplexPayloadSerializer.java
 ##########
 @@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.streaming.tests.artificialstate;
+
+import org.apache.flink.api.common.typeutils.CompatibilityResult;
+import org.apache.flink.api.common.typeutils.ParameterlessTypeSerializerConfig;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerConfigSnapshot;
+import org.apache.flink.api.java.typeutils.runtime.DataInputViewStream;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.util.InstantiationUtil;
+
+import java.io.IOException;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * A custom stateful serializer to test that serializers are not used 
concurrently.
+ */
+public class StatefulComplexPayloadSerializer extends 
TypeSerializer<ComplexPayload> {
+
+       private static final long serialVersionUID = 8766687317209282373L;
+
+       /** This holds the thread that currently has exclusive ownership over 
the serializer. */
+       private final AtomicReference<Thread> currentOwnerThread;
+
+       public StatefulComplexPayloadSerializer() {
+               this.currentOwnerThread = new AtomicReference<>(null);
+       }
+
+       @Override
+       public boolean isImmutableType() {
+               return false;
+       }
+
+       @Override
+       public TypeSerializer<ComplexPayload> duplicate() {
+               return new StatefulComplexPayloadSerializer();
+       }
+
+       @Override
+       public ComplexPayload createInstance() {
+               throw new UnsupportedOperationException();
+       }
+
+       @Override
+       public ComplexPayload copy(ComplexPayload from) {
+               try {
+                       if (currentOwnerThread.compareAndSet(null, 
Thread.currentThread())) {
+                               return InstantiationUtil.deserializeObject(
+                                       
InstantiationUtil.serializeObject(from), 
Thread.currentThread().getContextClassLoader());
 
 Review comment:
   Can avoid multiple invocations on `Thread.currentThread()`:
   
   ```
   Thread currentThread = Thread.currentThread();
   if (currentOwnerThread.compareAndSet(null, currentThread)) {
       return deserializeObject(serializeObject(from), 
currentThread.getContextClassLoader());
   }
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to