Skip to content

Commit e4f9d83

Browse files
committed
Resource version comparison utility
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent 4a02911 commit e4f9d83

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/PrimaryUpdateAndCacheUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,4 +450,24 @@ public static <P extends HasMetadata> P addFinalizerWithSSA(
450450
e);
451451
}
452452
}
453+
454+
public static int compareResourceVersions(String v1, String v2) {
455+
var v1Length = v1.length();
456+
var v2Length = v2.length();
457+
if (v1Length > v2Length) {
458+
return 1;
459+
}
460+
if (v2Length > v1Length) {
461+
return -1;
462+
}
463+
for (int i = 0; i < v1Length; i++) {
464+
if (v1.charAt(i) > v2.charAt(i)) {
465+
return 1;
466+
}
467+
if (v1.charAt(i) < v2.charAt(i)) {
468+
return -1;
469+
}
470+
}
471+
return 0;
472+
}
453473
}

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/reconciler/PrimaryUpdateAndCacheUtilsTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import org.junit.jupiter.api.BeforeEach;
2222
import org.junit.jupiter.api.Test;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
2325

2426
import io.fabric8.kubernetes.api.model.HasMetadata;
2527
import io.fabric8.kubernetes.client.KubernetesClient;
@@ -37,6 +39,7 @@
3739
import io.javaoperatorsdk.operator.sample.simple.TestCustomResource;
3840

3941
import static io.javaoperatorsdk.operator.api.reconciler.PrimaryUpdateAndCacheUtils.DEFAULT_MAX_RETRY;
42+
import static io.javaoperatorsdk.operator.api.reconciler.PrimaryUpdateAndCacheUtils.compareResourceVersions;
4043
import static org.assertj.core.api.Assertions.assertThat;
4144
import static org.junit.jupiter.api.Assertions.assertThrows;
4245
import static org.mockito.ArgumentMatchers.any;
@@ -47,6 +50,8 @@
4750

4851
class PrimaryUpdateAndCacheUtilsTest {
4952

53+
private static final Logger log = LoggerFactory.getLogger(PrimaryUpdateAndCacheUtilsTest.class);
54+
5055
Context<TestCustomResource> context = mock(Context.class);
5156
KubernetesClient client = mock(KubernetesClient.class);
5257
Resource resource = mock(Resource.class);
@@ -176,4 +181,34 @@ void cachePollTimeouts() {
176181
10L));
177182
assertThat(ex.getMessage()).contains("Timeout");
178183
}
184+
185+
@Test
186+
public void compareResourceVersionsTest() {
187+
assertThat(compareResourceVersions("11", "22")).isNegative();
188+
assertThat(compareResourceVersions("22", "11")).isPositive();
189+
assertThat(compareResourceVersions("11", "11")).isZero();
190+
191+
assertThat(compareResourceVersions("123", "2")).isPositive();
192+
assertThat(compareResourceVersions("3", "211")).isNegative();
193+
}
194+
195+
// naive performance that compares the works case scenario for non parsing variant
196+
@Test
197+
public void compareResourcePerformanceTest() {
198+
var execNum = 10000000;
199+
var startTime = System.currentTimeMillis();
200+
for (int i = 0; i < execNum; i++) {
201+
var res = compareResourceVersions("123456788", "123456789");
202+
}
203+
var dur1 = System.currentTimeMillis() - startTime;
204+
log.info("Duration without parsing: {}", dur1);
205+
startTime = System.currentTimeMillis();
206+
for (int i = 0; i < execNum; i++) {
207+
var res = Long.parseLong("123456788") > Long.parseLong("123456789");
208+
}
209+
var dur2 = System.currentTimeMillis() - startTime;
210+
log.info("Duration with parsing: {}", dur2);
211+
212+
assertThat(dur1).isLessThan(dur2);
213+
}
179214
}

0 commit comments

Comments
 (0)