Skip to content

Commit b2614b1

Browse files
author
admitrov
committed
Disable time travel
1 parent 7981e56 commit b2614b1

File tree

7 files changed

+128
-57
lines changed

7 files changed

+128
-57
lines changed

embedded-aerospike/README.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ ToxiProxy is a great tool for simulating network conditions, meaning that you ca
1919

2020
* `embedded.aerospike.enabled` `(true|false, default is 'true')`
2121
* `embedded.aerospike.reuseContainer` `(true|false, default is 'false')`
22-
* `embedded.aerospike.dockerImage` `(default is set to 'aerospike/aerospike-server:5.5.0.2')`
22+
* `embedded.aerospike.dockerImage` `(default is set to 'aerospike/aerospike-server:5.7.0.17')`
2323
** You can pick wanted version on https://hub.docker.com/r/library/aerospike/tags/[dockerhub]
2424
* `embedded.aerospike.featureKey` `(base64 of a feature-key-file https://docs.aerospike.com/docs/reference/configuration/index.html#feature-key-file, default is null) is only required for the EE image`
2525
* `embedded.aerospike.waitTimeoutInSeconds` `(default is 60 seconds)`
2626
* `embedded.toxiproxy.proxies.aerospike.enabled` Enables both creation of the container with ToxiProxy TCP proxy and a proxy to the `embedded-aerospike` container.
27+
* `embedded.aerospike.time-travel.enabled` Enables time travel to clean expired documents by time. Does not work on ARM(mac m1) because of LUA scripts are not supported on ARM.
2728
* https://mvnrepository.com/artifact/com.aerospike/aerospike-client[aerospike client library]
2829

2930
==== Produces
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.playtika.test.aerospike;
2+
3+
import com.aerospike.client.AerospikeClient;
4+
import com.aerospike.client.Language;
5+
import com.aerospike.client.Value;
6+
import com.aerospike.client.query.Statement;
7+
import com.aerospike.client.task.ExecuteTask;
8+
import com.aerospike.client.task.RegisterTask;
9+
import org.springframework.util.Assert;
10+
11+
import java.time.Instant;
12+
import java.util.concurrent.TimeUnit;
13+
14+
public class AerospikeExpiredDocumentsCleaner implements ExpiredDocumentsCleaner {
15+
16+
private static final String PACKAGE_NAME = "remove_expired";
17+
private static final String FUNC_NAME = "remove_expired";
18+
private static final String RESOURCE_PATH = "udf/remove_expired.lua";
19+
private static final String SERVER_PATH = "remove_expired.lua";
20+
private static final int SLEEP_INTERVAL = 100;
21+
private static final int TIMEOUT = 10_000;
22+
23+
private final AerospikeClient client;
24+
private final String namespace;
25+
26+
public AerospikeExpiredDocumentsCleaner(AerospikeClient client, String namespace) {
27+
Assert.notNull(client, "Aerospike client can not be null");
28+
Assert.notNull(namespace, "Namespace can not be null");
29+
this.client = client;
30+
this.namespace = namespace;
31+
32+
registerUdf();
33+
}
34+
35+
private void registerUdf() {
36+
ClassLoader classLoader = AerospikeExpiredDocumentsCleaner.class.getClassLoader();
37+
RegisterTask registerTask = client.register(null, classLoader, RESOURCE_PATH, SERVER_PATH, Language.LUA);
38+
registerTask.waitTillComplete(SLEEP_INTERVAL, TIMEOUT);
39+
}
40+
41+
@Override
42+
public void cleanExpiredDocumentsBefore(Instant expireTime) {
43+
cleanExpiredDocumentsBefore(expireTime.toEpochMilli());
44+
}
45+
46+
@Override
47+
public void cleanExpiredDocumentsBefore(long expireTimeMillis) {
48+
long duration = expireTimeMillis - System.currentTimeMillis();
49+
int expiration = (int) TimeUnit.MILLISECONDS.toSeconds(duration);
50+
Value value = Value.get(expiration);
51+
52+
Statement statement = new Statement();
53+
statement.setNamespace(namespace);
54+
55+
ExecuteTask executeTask = client.execute(null, statement, PACKAGE_NAME, FUNC_NAME, value);
56+
executeTask.waitTillComplete(SLEEP_INTERVAL, TIMEOUT);
57+
}
58+
}

embedded-aerospike/src/main/java/com/playtika/test/aerospike/AerospikeProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ public AerospikeProperties() {
2727

2828
@Override
2929
public String getDefaultDockerImage() {
30-
return "aerospike/aerospike-server:5.5.0.2";
30+
return "aerospike/aerospike-server:5.7.0.17";
3131
}
3232
}

embedded-aerospike/src/main/java/com/playtika/test/aerospike/EmbeddedAerospikeTestOperationsAutoConfiguration.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.springframework.context.annotation.Configuration;
1818
import org.testcontainers.containers.GenericContainer;
1919

20+
import java.time.Instant;
2021
import java.util.Collections;
2122

2223
import static com.playtika.test.aerospike.AerospikeProperties.AEROSPIKE_BEAN_NAME;
@@ -45,10 +46,26 @@ public PackageInstaller aerospikePackageInstaller(
4546
}
4647

4748
@Bean
48-
@ConditionalOnMissingBean
49+
@ConditionalOnProperty(value = "embedded.aerospike.time-travel.enabled", havingValue="true", matchIfMissing = true)
4950
public ExpiredDocumentsCleaner expiredDocumentsCleaner(AerospikeClient client,
50-
AerospikeProperties properties) {
51-
return new ExpiredDocumentsCleaner(client, properties.getNamespace());
51+
AerospikeProperties properties) {
52+
return new AerospikeExpiredDocumentsCleaner(client, properties.getNamespace());
53+
}
54+
55+
@Bean
56+
@ConditionalOnProperty(value = "embedded.aerospike.time-travel.enabled", havingValue="false", matchIfMissing = false)
57+
public ExpiredDocumentsCleaner disabledExpiredDocumentsCleaner() {
58+
return new ExpiredDocumentsCleaner() {
59+
@Override
60+
public void cleanExpiredDocumentsBefore(long millis) {
61+
throw new UnsupportedOperationException("Expired documents cleaner is disabled. Change property embedded.aerospike.time-travel.enabled to enable it.");
62+
}
63+
64+
@Override
65+
public void cleanExpiredDocumentsBefore(Instant expireTime) {
66+
throw new UnsupportedOperationException("Expired documents cleaner is disabled. Change property embedded.aerospike.time-travel.enabled to enable it.");
67+
}
68+
};
5269
}
5370

5471
@Bean
Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,8 @@
11
package com.playtika.test.aerospike;
22

3-
import com.aerospike.client.AerospikeClient;
4-
import com.aerospike.client.Language;
5-
import com.aerospike.client.Value;
6-
import com.aerospike.client.query.Statement;
7-
import com.aerospike.client.task.ExecuteTask;
8-
import com.aerospike.client.task.RegisterTask;
9-
import org.springframework.util.Assert;
10-
113
import java.time.Instant;
12-
import java.util.concurrent.TimeUnit;
13-
14-
public class ExpiredDocumentsCleaner {
15-
16-
private static final String PACKAGE_NAME = "remove_expired";
17-
private static final String FUNC_NAME = "remove_expired";
18-
private static final String RESOURCE_PATH = "udf/remove_expired.lua";
19-
private static final String SERVER_PATH = "remove_expired.lua";
20-
private static final int SLEEP_INTERVAL = 100;
21-
private static final int TIMEOUT = 10_000;
22-
23-
private final AerospikeClient client;
24-
private final String namespace;
25-
26-
public ExpiredDocumentsCleaner(AerospikeClient client, String namespace) {
27-
Assert.notNull(client, "Aerospike client can not be null");
28-
Assert.notNull(namespace, "Namespace can not be null");
29-
this.client = client;
30-
this.namespace = namespace;
31-
32-
registerUdf();
33-
}
34-
35-
private void registerUdf() {
36-
ClassLoader classLoader = ExpiredDocumentsCleaner.class.getClassLoader();
37-
RegisterTask registerTask = client.register(null, classLoader, RESOURCE_PATH, SERVER_PATH, Language.LUA);
38-
registerTask.waitTillComplete(SLEEP_INTERVAL, TIMEOUT);
39-
}
40-
41-
public void cleanExpiredDocumentsBefore(Instant expireTime) {
42-
cleanExpiredDocumentsBefore(expireTime.toEpochMilli());
43-
}
44-
45-
public void cleanExpiredDocumentsBefore(long expireTimeMillis) {
46-
long duration = expireTimeMillis - System.currentTimeMillis();
47-
int expiration = (int) TimeUnit.MILLISECONDS.toSeconds(duration);
48-
Value value = Value.get(expiration);
49-
50-
Statement statement = new Statement();
51-
statement.setNamespace(namespace);
524

53-
ExecuteTask executeTask = client.execute(null, statement, PACKAGE_NAME, FUNC_NAME, value);
54-
executeTask.waitTillComplete(SLEEP_INTERVAL, TIMEOUT);
55-
}
5+
public interface ExpiredDocumentsCleaner {
6+
void cleanExpiredDocumentsBefore(long millis);
7+
void cleanExpiredDocumentsBefore(Instant expireTime);
568
}

embedded-aerospike/src/test/java/com/playtika/test/aerospike/DisableAerospikeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void contextLoads() {
2121
.withPropertyValues(
2222
"embedded.aerospike.enabled=false"
2323
)
24-
.run((context) -> assertThat(context)
24+
.run(context -> assertThat(context)
2525
.hasNotFailed()
2626
.doesNotHaveBean(Container.class)
2727
.doesNotHaveBean("aerospikeClientDependencyPostProcessor"));
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.playtika.test.aerospike;
2+
3+
import com.aerospike.client.AerospikeClient;
4+
import org.junit.jupiter.api.Test;
5+
import org.mockito.Mockito;
6+
import org.springframework.boot.autoconfigure.AutoConfigurations;
7+
import org.springframework.boot.context.annotation.UserConfigurations;
8+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
public class DisableTimeTravelTest {
15+
16+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
17+
.withConfiguration(UserConfigurations.of(TestConfiguration.class))
18+
.withConfiguration(AutoConfigurations.of(
19+
EmbeddedAerospikeBootstrapConfiguration.class,
20+
EmbeddedAerospikeTestOperationsAutoConfiguration.class));
21+
22+
23+
@Test
24+
public void contextLoadsWithDisabledTimeTravelProperty() {
25+
contextRunner
26+
.withPropertyValues(
27+
"embedded.aerospike.time-travel.enabled=false"
28+
)
29+
.run(context -> assertThat(context)
30+
.hasNotFailed()
31+
.hasSingleBean(ExpiredDocumentsCleaner.class)
32+
.hasBean("disabledExpiredDocumentsCleaner"));
33+
}
34+
35+
@Configuration
36+
static class TestConfiguration {
37+
38+
@Bean
39+
public AerospikeClient mockAerospikeClient() {
40+
return Mockito.mock(AerospikeClient.class);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)