Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -450,40 +450,4 @@ public static <P extends HasMetadata> P addFinalizerWithSSA(
e);
}
}

public static int compareResourceVersions(String v1, String v2) {
int v1Length = validateResourceVersion(v1);
int v2Length = validateResourceVersion(v2);
int comparison = v1Length - v2Length;
if (comparison != 0) {
return comparison;
}
for (int i = 0; i < v2Length; i++) {
int comp = v1.charAt(i) - v2.charAt(i);
if (comp != 0) {
return comp;
}
}
return 0;
}

private static int validateResourceVersion(String v1) {
int v1Length = v1.length();
if (v1Length == 0) {
throw new NonComparableResourceVersionException("Resource version is empty");
}
for (int i = 0; i < v1Length; i++) {
char char1 = v1.charAt(i);
if (char1 == '0') {
if (i == 0) {
throw new NonComparableResourceVersionException(
"Resource version cannot begin with 0: " + v1);
}
} else if (char1 < '0' || char1 > '9') {
throw new NonComparableResourceVersionException(
"Non numeric characters in resource version: " + v1);
}
}
return v1Length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.function.UnaryOperator;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -40,7 +39,6 @@
import io.javaoperatorsdk.operator.sample.simple.TestCustomResource;

import static io.javaoperatorsdk.operator.api.reconciler.PrimaryUpdateAndCacheUtils.DEFAULT_MAX_RETRY;
import static io.javaoperatorsdk.operator.api.reconciler.PrimaryUpdateAndCacheUtils.compareResourceVersions;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -182,53 +180,4 @@ void cachePollTimeouts() {
10L));
assertThat(ex.getMessage()).contains("Timeout");
}

@Test
public void compareResourceVersionsTest() {
assertThat(compareResourceVersions("11", "22")).isNegative();
assertThat(compareResourceVersions("22", "11")).isPositive();
assertThat(compareResourceVersions("1", "1")).isZero();
assertThat(compareResourceVersions("11", "11")).isZero();
assertThat(compareResourceVersions("123", "2")).isPositive();
assertThat(compareResourceVersions("3", "211")).isNegative();

assertThrows(
NonComparableResourceVersionException.class, () -> compareResourceVersions("aa", "22"));
assertThrows(
NonComparableResourceVersionException.class, () -> compareResourceVersions("11", "ba"));
assertThrows(
NonComparableResourceVersionException.class, () -> compareResourceVersions("", "22"));
assertThrows(
NonComparableResourceVersionException.class, () -> compareResourceVersions("11", ""));
assertThrows(
NonComparableResourceVersionException.class, () -> compareResourceVersions("01", "123"));
assertThrows(
NonComparableResourceVersionException.class, () -> compareResourceVersions("123", "01"));
assertThrows(
NonComparableResourceVersionException.class, () -> compareResourceVersions("3213", "123a"));
assertThrows(
NonComparableResourceVersionException.class, () -> compareResourceVersions("321", "123a"));
}

// naive performance test that compares the work case scenario for the parsing and non-parsing
// variants
@Test
@Disabled("test sometimes fails, we plan to iterate over it and related features for 5.3")
public void compareResourcePerformanceTest() {
var execNum = 30000000;
var startTime = System.currentTimeMillis();
for (int i = 0; i < execNum; i++) {
var res = compareResourceVersions("123456788", "123456789");
}
var dur1 = System.currentTimeMillis() - startTime;
log.info("Duration without parsing: {}", dur1);
startTime = System.currentTimeMillis();
for (int i = 0; i < execNum; i++) {
var res = Long.parseLong("123456788") > Long.parseLong("123456789");
}
var dur2 = System.currentTimeMillis() - startTime;
log.info("Duration with parsing: {}", dur2);

assertThat(dur1).isLessThan(dur2);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
#
# Copyright Java Operator SDK Authors
#
# Licensed 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.
#

apiVersion: apps/v1
kind: StatefulSet
metadata:
Expand Down