-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e79cd06
commit f9313e7
Showing
15 changed files
with
725 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/org/prebid/cache/repository/ignite/IgniteConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.prebid.cache.repository.ignite; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import org.apache.commons.lang3.BooleanUtils; | ||
import org.apache.ignite.Ignition; | ||
import org.apache.ignite.client.ClientCache; | ||
import org.apache.ignite.client.ClientConnectionException; | ||
import org.apache.ignite.client.IgniteClient; | ||
import org.apache.ignite.client.SslMode; | ||
import org.apache.ignite.configuration.ClientConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.Arrays; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
@Configuration | ||
@EnableConfigurationProperties(IgniteConfigurationProperties.class) | ||
@ConditionalOnProperty(prefix = "spring.ignite", name = {"host"}) | ||
public class IgniteConfiguration { | ||
|
||
private static final int DEFAULT_PORT = 10800; | ||
|
||
@Bean | ||
public ClientConfiguration clientConfiguration(IgniteConfigurationProperties properties) { | ||
ClientConfiguration cfg = new ClientConfiguration(); | ||
|
||
final String host = properties.getHost(); | ||
final int port = properties.getPort() == null ? DEFAULT_PORT : properties.getPort(); | ||
|
||
if (isCluster(host)) { | ||
cfg.setAddresses(extractHosts(host)); | ||
} else { | ||
cfg.setAddresses(host + ":" + port); | ||
} | ||
|
||
cfg.setSslMode(BooleanUtils.isTrue(properties.getSecure()) ? SslMode.REQUIRED : SslMode.DISABLED); | ||
return cfg; | ||
} | ||
|
||
public static boolean isCluster(@NotNull String hostList) { | ||
return hostList.split(",").length > 1; | ||
} | ||
|
||
private static String[] extractHosts(@NotNull String hostList) { | ||
return Arrays.stream(hostList.split(",")) | ||
.map(host -> { | ||
String[] params = host.split(":"); | ||
String hostname = requireNonNull(params[0]); | ||
int port = DEFAULT_PORT; | ||
if (params.length == 2) { | ||
port = Integer.parseInt(params[1]); | ||
} | ||
return hostname + ":" + port; | ||
}) | ||
.toArray(String[]::new); | ||
} | ||
|
||
@Bean(destroyMethod = "close") | ||
public IgniteClient igniteClient(ClientConfiguration clientConfiguration) throws ClientConnectionException { | ||
return Ignition.startClient(clientConfiguration); | ||
} | ||
|
||
@Bean | ||
public ClientCache<String, String> igniteCache(IgniteClient igniteClient, | ||
IgniteConfigurationProperties properties) { | ||
|
||
return igniteClient.cache(properties.getCacheName()); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/org/prebid/cache/repository/ignite/IgniteConfigurationProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.prebid.cache.repository.ignite; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@Data | ||
@ConfigurationProperties(prefix = "spring.ignite") | ||
public class IgniteConfigurationProperties { | ||
|
||
private String host; | ||
|
||
private Integer port; | ||
|
||
private String cacheName; | ||
|
||
private Boolean secure; | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/org/prebid/cache/repository/ignite/IgniteRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.prebid.cache.repository.ignite; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.exception.ExceptionUtils; | ||
import org.apache.ignite.client.ClientCache; | ||
import org.apache.ignite.client.ClientException; | ||
import org.prebid.cache.exceptions.PayloadWrapperPropertyException; | ||
import org.prebid.cache.exceptions.RepositoryException; | ||
import org.prebid.cache.helpers.Json; | ||
import org.prebid.cache.model.PayloadWrapper; | ||
import org.prebid.cache.repository.ReactiveRepository; | ||
import reactor.core.publisher.Mono; | ||
|
||
import javax.cache.expiry.CreatedExpiryPolicy; | ||
import javax.cache.expiry.Duration; | ||
import javax.cache.expiry.ExpiryPolicy; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Slf4j | ||
public class IgniteRepositoryImpl implements ReactiveRepository<PayloadWrapper, String> { | ||
|
||
private final ClientCache<String, String> cache; | ||
|
||
public IgniteRepositoryImpl(ClientCache<String, String> cache) { | ||
this.cache = cache; | ||
} | ||
|
||
@Override | ||
public Mono<PayloadWrapper> save(PayloadWrapper wrapper) { | ||
ExpiryPolicy expiryPolicy; | ||
String normalizedId; | ||
|
||
try { | ||
normalizedId = wrapper.getNormalizedId(); | ||
expiryPolicy = new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, wrapper.getExpiry())); | ||
} catch (PayloadWrapperPropertyException e) { | ||
log.error("Exception occurred while extracting normalized id from payload: '{}', cause: '{}'", | ||
ExceptionUtils.getMessage(e), ExceptionUtils.getMessage(e)); | ||
return Mono.empty(); | ||
} | ||
|
||
final ClientCache<String, String> expiredCache = cache.withExpirePolicy(expiryPolicy); | ||
return Mono.fromFuture(expiredCache.putIfAbsentAsync(normalizedId, Json.toJson(wrapper)).toCompletableFuture()) | ||
.map(payload -> wrapper) | ||
.onErrorResume(IgniteRepositoryImpl::handleError); | ||
} | ||
|
||
@Override | ||
public Mono<PayloadWrapper> findById(String id) { | ||
return Mono.fromFuture(cache.getAsync(id).toCompletableFuture()) | ||
.map(json -> Json.createPayloadFromJson(json, PayloadWrapper.class)) | ||
.onErrorResume(IgniteRepositoryImpl::handleError); | ||
} | ||
|
||
private static <T> Mono<T> handleError(Throwable throwable) { | ||
if (throwable instanceof ClientException) { | ||
return Mono.error(new RepositoryException(throwable.toString(), throwable)); | ||
} | ||
|
||
return Mono.error(throwable); | ||
} | ||
} |
Oops, something went wrong.