xintongsong commented on a change in pull request #13864:
URL: https://github.com/apache/flink/pull/13864#discussion_r518479705



##########
File path: 
flink-kubernetes/src/test/java/org/apache/flink/kubernetes/highavailability/KubernetesStateHandleStoreTest.java
##########
@@ -0,0 +1,382 @@
+/*
+ * 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.kubernetes.highavailability;
+
+import org.apache.flink.api.common.JobID;
+import 
org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector;
+import org.apache.flink.runtime.statehandle.StateHandleStore;
+import 
org.apache.flink.runtime.statehandle.TestingLongStateHandleHelper.LongRetrievableStateHandle;
+import 
org.apache.flink.runtime.statehandle.TestingLongStateHandleHelper.LongStateStorage;
+import org.apache.flink.util.function.FunctionUtils;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.function.Predicate;
+
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+/**
+ * Tests for {@link KubernetesStateHandleStore} operations.
+ */
+public class KubernetesStateHandleStoreTest extends 
KubernetesHighAvailabilityTestBase {
+
+       private static final String PREFIX = "test-prefix-";
+       private final String key = PREFIX + JobID.generate();
+       private final Predicate<String> filter = k -> k.startsWith(PREFIX);
+       private final Long state = 12345L;
+
+       private LongStateStorage longStateStorage;
+
+       @Before
+       public void setup() {
+               super.setup();
+               longStateStorage = new LongStateStorage();
+       }
+
+       @Test
+       public void testAdd() throws Exception {
+               new Context() {{
+                       runTest(
+                               () -> {
+                                       leaderCallbackGrantLeadership();
+
+                                       final KubernetesStateHandleStore<Long> 
store = new KubernetesStateHandleStore<>(
+                                               flinkKubeClient, 
LEADER_CONFIGMAP_NAME, longStateStorage, filter, LOCK_IDENTITY);
+                                       store.add(key, state);
+                                       assertThat(store.getAll().size(), 
is(1));
+                                       
assertThat(store.get(key).retrieveState(), is(state));
+                               });
+               }};
+       }
+
+       @Test
+       public void testAddAlreadyExistingKey() throws Exception {
+               new Context() {{
+                       runTest(
+                               () -> {
+                                       leaderCallbackGrantLeadership();
+
+                                       final KubernetesStateHandleStore<Long> 
store = new KubernetesStateHandleStore<>(
+                                               flinkKubeClient, 
LEADER_CONFIGMAP_NAME, longStateStorage, filter, LOCK_IDENTITY);
+                                       store.add(key, state);
+
+                                       try {
+                                               store.add(key, state);
+                                               fail("Exception should be 
thrown.");
+                                       } catch 
(StateHandleStore.AlreadyExistException ex) {
+                                               final String msg = 
String.format(
+                                                       "%s already exists in 
ConfigMap %s", key, LEADER_CONFIGMAP_NAME);
+                                               assertThat(ex.getMessage(), 
containsString(msg));
+                                       }
+                                       
assertThat(longStateStorage.getStateHandles().size(), is(2));
+                                       
assertThat(longStateStorage.getStateHandles().get(1).getNumberOfDiscardCalls(), 
is(1));
+                               });
+               }};
+       }
+
+       @Test
+       public void testAddFailedWhenConfigMapNotExistAndDiscardState() throws 
Exception {
+               new Context() {{
+                       runTest(
+                               () -> {
+                                       final KubernetesStateHandleStore<Long> 
store = new KubernetesStateHandleStore<>(
+                                               flinkKubeClient, 
LEADER_CONFIGMAP_NAME, longStateStorage, filter, LOCK_IDENTITY);
+
+                                       try {
+                                               store.add(key, state);
+                                               fail("Exception should be 
thrown.");
+                                       } catch (Exception ex) {
+                                               final String msg = 
String.format("ConfigMap %s does not exist.", LEADER_CONFIGMAP_NAME);
+                                               assertThat(ex.getMessage(), 
containsString(msg));
+                                       }
+                                       
assertThat(longStateStorage.getStateHandles().size(), is(1));
+                                       
assertThat(longStateStorage.getStateHandles().get(0).getNumberOfDiscardCalls(), 
is(1));
+                               });
+               }};
+       }
+
+       @Test
+       public void testReplace() throws Exception {
+               new Context() {{
+                       runTest(
+                               () -> {
+                                       leaderCallbackGrantLeadership();
+
+                                       final KubernetesStateHandleStore<Long> 
store = new KubernetesStateHandleStore<>(
+                                               flinkKubeClient, 
LEADER_CONFIGMAP_NAME, longStateStorage, filter, LOCK_IDENTITY);
+
+                                       store.add(key, state);
+
+                                       final Long newState = 23456L;
+                                       final String resourceVersion = 
store.exists(key);
+                                       store.replace(key, resourceVersion, 
newState);
+
+                                       assertThat(store.getAll().size(), 
is(1));
+                                       
assertThat(store.get(key).retrieveState(), is(newState));
+                               });
+               }};
+       }
+
+       @Test
+       public void testReplaceWithKeyNotExist() throws Exception {
+               new Context() {{
+                       runTest(
+                               () -> {
+                                       leaderCallbackGrantLeadership();
+
+                                       final KubernetesStateHandleStore<Long> 
store = new KubernetesStateHandleStore<>(
+                                               flinkKubeClient, 
LEADER_CONFIGMAP_NAME, longStateStorage, filter, LOCK_IDENTITY);
+                                       final Long newState = 23456L;
+
+                                       try {
+                                               assertThat(store.exists(key), 
is(StateHandleStore.NON_EXIST_RESOURCE_VERSION));
+                                               store.replace(key, 
StateHandleStore.NON_EXIST_RESOURCE_VERSION, newState);
+                                               fail("Exception should be 
thrown.");
+                                       } catch 
(StateHandleStore.NotExistException e) {
+                                               final String msg = 
String.format(
+                                                       "Could not find %s in 
ConfigMap %s", key, LEADER_CONFIGMAP_NAME);
+                                               assertThat(e.getMessage(), 
containsString(msg));
+                                       }
+                                       assertThat(store.getAll().size(), 
is(0));
+                               });
+               }};
+       }
+
+       @Test
+       public void testReplaceFailedAndDiscardState() throws Exception {

Review comment:
       Ok, thanks for the explaination.




----------------------------------------------------------------
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


Reply via email to