tillrohrmann commented on a change in pull request #9501: [FLINK-12697] [State 
Backends] Support on-disk state storage for spill-able heap backend
URL: https://github.com/apache/flink/pull/9501#discussion_r321353063
 
 

 ##########
 File path: 
flink-state-backends/flink-statebackend-heap-spillable/src/test/java/org/apache/flink/runtime/state/heap/CopyOnWriteSkipListStateMapTest.java
 ##########
 @@ -0,0 +1,1448 @@
+/*
+ *
+ *  * 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.runtime.state.heap;
+
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.api.common.typeutils.base.LongSerializer;
+import org.apache.flink.api.common.typeutils.base.StringSerializer;
+import org.apache.flink.core.memory.ByteArrayInputStreamWithPos;
+import org.apache.flink.core.memory.ByteArrayOutputStreamWithPos;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+import org.apache.flink.runtime.state.StateEntry;
+import org.apache.flink.runtime.state.StateSnapshotTransformer;
+import org.apache.flink.runtime.state.StateTransformationFunction;
+import org.apache.flink.runtime.state.heap.space.Allocator;
+import org.apache.flink.runtime.state.internal.InternalKvState;
+import org.apache.flink.util.IOUtils;
+import org.apache.flink.util.TestLogger;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ThreadLocalRandom;
+
+import static org.apache.flink.runtime.state.heap.SkipListUtils.NIL_NODE;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for {@link CopyOnWriteSkipListStateMap}.
+ */
+public class CopyOnWriteSkipListStateMapTest extends TestLogger {
+
+       private TestAllocator spaceAllocator;
+
+       @Before
+       public void setUp() {
+               int maxAllocateSize = 256;
+               spaceAllocator = new TestAllocator(maxAllocateSize);
+       }
+
+       @After
+       public void tearDown() {
+               IOUtils.closeQuietly(spaceAllocator);
+       }
+
+       /**
+        * Test initialization of state map.
+        */
+       @Test
+       public void testInitStateMap() {
+               TypeSerializer<Integer> keySerializer = IntSerializer.INSTANCE;
+               TypeSerializer<Long> namespaceSerializer = 
LongSerializer.INSTANCE;
+               TypeSerializer<String> stateSerializer = 
StringSerializer.INSTANCE;
+               CopyOnWriteSkipListStateMap<Integer, Long, String> stateMap = 
new CopyOnWriteSkipListStateMap<>(
+                       keySerializer, namespaceSerializer, stateSerializer, 
spaceAllocator);
+
+               assertTrue(stateMap.isEmpty());
+               assertEquals(0, stateMap.size());
+               assertEquals(0, stateMap.totalSize());
+               assertEquals(0, stateMap.getRequestCount());
+               assertTrue(stateMap.getLogicallyRemovedNodes().isEmpty());
+               assertEquals(0, stateMap.getHighestRequiredSnapshotVersion());
+               assertEquals(0, stateMap.getHighestFinishedSnapshotVersion());
+               assertTrue(stateMap.getSnapshotVersions().isEmpty());
+               assertTrue(stateMap.getPruningValueNodes().isEmpty());
+               assertEquals(0, stateMap.getResourceGuard().getLeaseCount());
+               assertFalse(stateMap.getResourceGuard().isClosed());
+               assertFalse(stateMap.isClosed());
+
+               assertNull(stateMap.get(0, 0L));
+               assertFalse(stateMap.containsKey(1, 2L));
+               assertNull(stateMap.removeAndGetOld(3, 4L));
+               assertFalse(stateMap.getKeys(-92L).iterator().hasNext());
+               assertEquals(0, stateMap.sizeOfNamespace(8L));
+               assertFalse(stateMap.iterator().hasNext());
+               assertFalse(stateMap.getStateIncrementalVisitor(100).hasNext());
+
+               stateMap.close();
+               assertEquals(0, stateMap.size());
+               assertEquals(0, stateMap.totalSize());
+               assertTrue(stateMap.isClosed());
+       }
+
+       /**
+        * Test basic operations.
+        */
+       @Test
+       public void testBasicOperations() throws Exception {
+               TypeSerializer<Integer> keySerializer = IntSerializer.INSTANCE;
+               TypeSerializer<Long> namespaceSerializer = 
LongSerializer.INSTANCE;
+               TypeSerializer<String> stateSerializer = 
StringSerializer.INSTANCE;
+               CopyOnWriteSkipListStateMap<Integer, Long, String> stateMap = 
new CopyOnWriteSkipListStateMap<>(
+                       keySerializer, namespaceSerializer, stateSerializer, 
spaceAllocator);
+
+               ThreadLocalRandom random = ThreadLocalRandom.current();
+               // map to store expected states, namespace -> key -> state
+               Map<Long, Map<Integer, String>> referenceStates = new 
HashMap<>();
+               int totalSize = 0;
+
+               // put some states
+               for (long namespace = 0; namespace < 10; namespace++) {
+                       for (int key = 0; key < 100; key++) {
+                               totalSize++;
+                               String state = String.valueOf(key * namespace);
+                               if (random.nextBoolean()) {
+                                       stateMap.put(key, namespace, state);
+                               } else {
+                                       assertNull(stateMap.putAndGetOld(key, 
namespace, state));
+                               }
+                               referenceStates.computeIfAbsent(namespace, 
(none) -> new HashMap<>()).put(key, state);
+                               assertEquals(totalSize, stateMap.size());
+                               assertEquals(totalSize, stateMap.totalSize());
+                       }
+               }
+
+               // validates space allocation. Each pair need 2 spaces
+               assertEquals(totalSize * 2, 
spaceAllocator.getTotalSpaceNumber());
+               verifyState(referenceStates, stateMap);
+
+               // remove some states
+               Map<Long, Set<Integer>> removedStates = new HashMap<>();
+               for (Map.Entry<Long, Map<Integer, String>> namespaceEntry : 
referenceStates.entrySet()) {
+                       long namespace = namespaceEntry.getKey();
+                       for (Map.Entry<Integer, String> keyEntry : 
namespaceEntry.getValue().entrySet()) {
+                               if (random.nextBoolean()) {
+                                       int key = keyEntry.getKey();
+                                       String state = keyEntry.getValue();
+                                       
removedStates.computeIfAbsent(namespace, (none) -> new HashSet<>()).add(key);
+                                       totalSize--;
+                                       if (random.nextBoolean()) {
+                                               stateMap.remove(key, namespace);
+                                       } else {
+                                               assertEquals(state, 
stateMap.removeAndGetOld(key, namespace));
+                                       }
+                                       assertEquals(totalSize, 
stateMap.size());
+                                       assertEquals(totalSize, 
stateMap.totalSize());
+                               }
+                       }
+               }
+
+               for (Map.Entry<Long, Set<Integer>> entry : 
removedStates.entrySet()) {
+                       long namespace = entry.getKey();
+                       Map<Integer, String> keyMap = 
referenceStates.get(namespace);
+                       if (keyMap != null) {
+                               entry.getValue().forEach(keyMap::remove);
+                               if (keyMap.isEmpty()) {
+                                       referenceStates.remove(namespace);
+                               }
+                       }
+                       for (int key : entry.getValue()) {
+                               assertNull(stateMap.get(key, namespace));
+                               assertFalse(stateMap.containsKey(key, 
namespace));
+                       }
+               }
+
+               assertEquals(totalSize * 2, 
spaceAllocator.getTotalSpaceNumber());
+               verifyState(referenceStates, stateMap);
+
+               // update some states
+               for (Map.Entry<Long, Map<Integer, String>> namespaceEntry : 
referenceStates.entrySet()) {
+                       long namespace = namespaceEntry.getKey();
+                       for (Map.Entry<Integer, String> keyEntry : 
namespaceEntry.getValue().entrySet()) {
+                               if (random.nextBoolean()) {
+                                       int key = keyEntry.getKey();
+                                       String state = keyEntry.getValue();
+                                       String newState = state + "-update";
+                                       keyEntry.setValue(newState);
+                                       if (random.nextBoolean()) {
+                                               stateMap.put(key, namespace, 
newState);
+                                       } else {
+                                               assertEquals(state, 
stateMap.putAndGetOld(key, namespace, newState));
+                                       }
+                                       assertEquals(totalSize, 
stateMap.size());
+                                       assertEquals(totalSize, 
stateMap.totalSize());
+                               }
+                       }
+               }
+
+               // put some new states
+               for (long namespace = 10; namespace < 15; namespace++) {
+                       for (int key = 0; key < 100; key++) {
+                               totalSize++;
+                               String state = String.valueOf(key * namespace);
+                               if (random.nextBoolean()) {
+                                       stateMap.put(key, namespace, state);
+                               } else {
+                                       assertNull(stateMap.putAndGetOld(key, 
namespace, state));
+                               }
+                               referenceStates.computeIfAbsent(namespace, 
(none) -> new HashMap<>()).put(key, state);
+                               assertEquals(totalSize, stateMap.size());
+                               assertEquals(totalSize, stateMap.totalSize());
+                       }
+               }
+
+               // remove some absent states
+               for (Map.Entry<Long, Set<Integer>> entry : 
removedStates.entrySet()) {
+                       long namespace = entry.getKey();
+                       for (int key : entry.getValue()) {
+                               if (random.nextBoolean()) {
+                                       stateMap.remove(key, namespace);
+                               } else {
+                                       
assertNull(stateMap.removeAndGetOld(key, namespace));
+                               }
+                               assertEquals(totalSize, stateMap.size());
+                               assertEquals(totalSize, stateMap.totalSize());
+                       }
+               }
+
+               assertEquals(totalSize * 2, 
spaceAllocator.getTotalSpaceNumber());
+               verifyState(referenceStates, stateMap);
+
+               StateTransformationFunction<String, Integer> function =
+                       (String prevState, Integer value) -> prevState == null 
? String.valueOf(value) : prevState + value;
+
+               // transform some old states
+               for (Map.Entry<Long, Map<Integer, String>> namespaceEntry : 
referenceStates.entrySet()) {
+                       long namespace = namespaceEntry.getKey();
+                       for (Map.Entry<Integer, String> keyEntry : 
namespaceEntry.getValue().entrySet()) {
+                               if (random.nextBoolean()) {
+                                       int key = keyEntry.getKey();
+                                       String state = keyEntry.getValue();
+                                       int delta = random.nextInt();
+                                       String newState = function.apply(state, 
delta);
+                                       keyEntry.setValue(newState);
+                                       stateMap.transform(key, namespace, 
delta, function);
+                                       assertEquals(totalSize, 
stateMap.size());
+                                       assertEquals(totalSize, 
stateMap.totalSize());
+                               }
+                       }
+               }
+
+               // transform some new states
+               for (long namespace = 15; namespace < 20; namespace++) {
+                       for (int key = 0; key < 100; key++) {
+                               totalSize++;
+                               int value = (int) (key * namespace);
+                               stateMap.transform(key, namespace, value, 
function);
+                               referenceStates.computeIfAbsent(namespace, 
(none) -> new HashMap<>()).put(key, String.valueOf(value));
+                               assertEquals(totalSize, stateMap.size());
+                               assertEquals(totalSize, stateMap.totalSize());
+                       }
+               }
+
+               assertEquals(totalSize * 2, 
spaceAllocator.getTotalSpaceNumber());
+               verifyState(referenceStates, stateMap);
+
+               // put some previously removed states
+               for (Map.Entry<Long, Set<Integer>> entry : 
removedStates.entrySet()) {
+                       long namespace = entry.getKey();
+                       for (int key : entry.getValue()) {
+                               totalSize++;
+                               String state = String.valueOf(key * namespace);
+                               if (random.nextBoolean()) {
+                                       stateMap.put(key, namespace, state);
+                               } else {
+                                       assertNull(stateMap.putAndGetOld(key, 
namespace, state));
+                               }
+                               referenceStates.computeIfAbsent(namespace, 
(none) -> new HashMap<>()).put(key, String.valueOf(state));
+                               assertEquals(totalSize, stateMap.size());
+                               assertEquals(totalSize, stateMap.totalSize());
+                       }
+               }
+
+               assertEquals(totalSize * 2, 
spaceAllocator.getTotalSpaceNumber());
+               verifyState(referenceStates, stateMap);
+
+               // remove some namespaces
+               Set<Long> removedNamespaces = new HashSet<>();
+               for (Map.Entry<Long, Map<Integer, String>> namespaceEntry : 
referenceStates.entrySet()) {
+                       if (random.nextBoolean()) {
+                               long namespace = namespaceEntry.getKey();
+                               removedNamespaces.add(namespace);
+                               for (Map.Entry<Integer, String> keyEntry : 
namespaceEntry.getValue().entrySet()) {
+                                       int key = keyEntry.getKey();
+                                       if (random.nextBoolean()) {
+                                               stateMap.remove(key, namespace);
+                                       } else {
+                                               
assertEquals(keyEntry.getValue(), stateMap.removeAndGetOld(key, namespace));
+                                       }
+                                       totalSize--;
+                                       assertEquals(totalSize, 
stateMap.size());
+                                       assertEquals(totalSize, 
stateMap.totalSize());
+                               }
+                       }
+               }
+
+               for (long namespace : removedNamespaces) {
+                       referenceStates.remove(namespace);
+                       assertEquals(0, stateMap.sizeOfNamespace(namespace));
+                       
assertFalse(stateMap.getKeys(namespace).iterator().hasNext());
+               }
+
+               assertEquals(totalSize * 2, 
spaceAllocator.getTotalSpaceNumber());
+               verifyState(referenceStates, stateMap);
+
+               stateMap.close();
+               assertEquals(0, stateMap.size());
+               assertEquals(0, stateMap.totalSize());
+               // all spaces should be free
+               assertEquals(0, spaceAllocator.getTotalSpaceNumber());
+               assertTrue(stateMap.isClosed());
+       }
+
+       /**
+        *  Tests copy-on-write contracts.
+        */
+       @SuppressWarnings("unchecked")
+       @Test
+       public void testCopyOnWriteContracts() throws IOException {
+               TypeSerializer<Integer> keySerializer = IntSerializer.INSTANCE;
+               TypeSerializer<Long> namespaceSerializer = 
LongSerializer.INSTANCE;
+               TypeSerializer<String> stateSerializer = 
StringSerializer.INSTANCE;
+               // do not remove states physically when get, put, remove and 
snapshot
+               CopyOnWriteSkipListStateMap<Integer, Long, String> stateMap = 
new CopyOnWriteSkipListStateMap<>(
+                       keySerializer,
+                       namespaceSerializer,
+                       stateSerializer,
+                       spaceAllocator,
+                       0,
+                       1.0f);
+
+               StateSnapshotTransformer<String> transformer = new 
StateSnapshotTransformer<String>() {
+                       @Nullable
+                       @Override
+                       public String filterOrTransform(@Nullable String value) 
{
+                               if (value == null) {
+                                       return null;
+                               }
+                               int op = value.hashCode() % 3;
+                               switch (op) {
+                                       case 0:
+                                               return null;
+                                       case 1:
+                                               return value + "-transform";
+                                       default:
+                                               return value;
+                               }
+                       }
+               };
+
+               // map to store expected states, namespace -> key -> state
+               Map<Long, Map<Integer, String>> referenceStates = new 
HashMap<>();
+               int totalStateSize = 0;
+               int totalSizeIncludingLogicalRemovedKey = 0;
+               int totalLogicallyRemovedKey = 0;
+               int totalSpaceNumber = 0;
+
+               // take snapshot 1 which is an empty snapshot
+               Map<Long, Map<Integer, String>> expectedSnapshot1 = 
snapshotReferenceStates(referenceStates);
+               CopyOnWriteSkipListStateMapSnapshot<Integer, Long, String> 
snapshot1 =
+                       (CopyOnWriteSkipListStateMapSnapshot<Integer, Long, 
String>) stateMap.stateSnapshot();
+               assertEquals(1, stateMap.getHighestRequiredSnapshotVersion());
+               assertEquals(1, stateMap.getSnapshotVersions().size());
+               assertEquals(true, stateMap.getSnapshotVersions().contains(1));
 
 Review comment:
   could be simplified to `assertTrue`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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