Skip to content

Commit

Permalink
chore: remove vertx JSON dep from ResourceService (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
astsiapanay authored Oct 25, 2024
1 parent 8c78cb9 commit 4f1397c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 76 deletions.
3 changes: 2 additions & 1 deletion server/src/main/java/com/epam/aidial/core/server/AiDial.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ void start() throws Exception {

LockService lockService = new LockService(redis, storage.getPrefix());
TimerService timerService = new VertxTimerService(vertx);
resourceService = new ResourceService(timerService, redis, storage, lockService, settings("resources"), storage.getPrefix());
ResourceService.Settings resourceServiceSettings = Json.decodeValue(settings("resources").toBuffer(), ResourceService.Settings.class);
resourceService = new ResourceService(timerService, redis, storage, lockService, resourceServiceSettings, storage.getPrefix());
InvitationService invitationService = new InvitationService(resourceService, encryptionService, settings("invitations"));
ShareService shareService = new ShareService(resourceService, invitationService, encryptionService);
RuleService ruleService = new RuleService(resourceService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import com.epam.aidial.core.server.util.EtagBuilder;
import com.epam.aidial.core.server.util.EtagHeader;
import com.epam.aidial.core.server.util.RedisUtil;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.common.collect.Sets;
import io.vertx.core.json.JsonObject;
import lombok.Builder;
import lombok.Getter;
import lombok.SneakyThrows;
Expand Down Expand Up @@ -102,51 +102,21 @@ public ResourceService(TimerService timerService,
RedissonClient redis,
BlobStorage blobStore,
LockService lockService,
JsonObject settings,
String prefix) {
this(timerService, redis, blobStore, lockService,
settings.getInteger("maxSize"),
settings.getLong("syncPeriod"),
settings.getLong("syncDelay"),
settings.getInteger("syncBatch"),
settings.getLong("cacheExpiration"),
settings.getInteger("compressionMinSize"),
prefix
);
}

/**
* @param maxSize - max allowed size in bytes for a resource.
* @param syncPeriod - period in milliseconds, how frequently check for resources to sync.
* @param syncDelay - delay in milliseconds for a resource to be written back in object storage after last modification.
* @param syncBatch - how many resources to sync in one go.
* @param cacheExpiration - expiration in milliseconds for synced resources in Redis.
* @param compressionMinSize - compress resources with gzip if their size in bytes more or equal to this value.
*/
public ResourceService(TimerService timerService,
RedissonClient redis,
BlobStorage blobStore,
LockService lockService,
int maxSize,
long syncPeriod,
long syncDelay,
int syncBatch,
long cacheExpiration,
int compressionMinSize,
Settings settings,
String prefix) {
this.redis = redis;
this.blobStore = blobStore;
this.lockService = lockService;
this.topic = new ResourceTopic(redis, "resource:" + BlobStorageUtil.toStoragePath(prefix, "topic"));
this.maxSize = maxSize;
this.syncDelay = syncDelay;
this.syncBatch = syncBatch;
this.cacheExpiration = Duration.ofMillis(cacheExpiration);
this.compressionMinSize = compressionMinSize;
this.maxSize = settings.maxSize;
this.syncDelay = settings.syncDelay;
this.syncBatch = settings.syncBatch;
this.cacheExpiration = Duration.ofMillis(settings.cacheExpiration);
this.compressionMinSize = settings.compressionMinSize;
this.prefix = prefix;
this.resourceQueue = "resource:" + BlobStorageUtil.toStoragePath(prefix, "queue");

this.syncTimer = timerService.scheduleWithFixedDelay(syncPeriod, syncPeriod, this::sync);
this.syncTimer = timerService.scheduleWithFixedDelay(settings.syncPeriod, settings.syncPeriod, this::sync);
}

@SneakyThrows
Expand Down Expand Up @@ -893,4 +863,22 @@ public record MultipartData(
long contentLength,
String etag) {
}

/**
* @param maxSize - max allowed size in bytes for a resource.
* @param syncPeriod - period in milliseconds, how frequently check for resources to sync.
* @param syncDelay - delay in milliseconds for a resource to be written back in object storage after last modification.
* @param syncBatch - how many resources to sync in one go.
* @param cacheExpiration - expiration in milliseconds for synced resources in Redis.
* @param compressionMinSize - compress resources with gzip if their size in bytes more or equal to this value.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public record Settings(
int maxSize,
long syncPeriod,
long syncDelay,
int syncBatch,
long cacheExpiration,
int compressionMinSize) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.epam.aidial.core.server.storage.BlobStorage;
import com.epam.aidial.core.server.token.TokenUsage;
import com.epam.aidial.core.server.util.HttpStatus;
import com.epam.aidial.core.server.util.ProxyUtil;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServerRequest;
Expand Down Expand Up @@ -99,24 +100,15 @@ public static void afterAll() throws IOException {
}

@BeforeEach
public void beforeEach() {
public void beforeEach() throws Exception {
RKeys keys = redissonClient.getKeys();
for (String key : keys.getKeys()) {
keys.delete(key);
}
LockService lockService = new LockService(redissonClient, null);
String resourceConfig = """
{
"maxSize" : 1048576,
"syncPeriod": 60000,
"syncDelay": 120000,
"syncBatch": 4096,
"cacheExpiration": 300000,
"compressionMinSize": 256
}
""";
ResourceService.Settings settings = new ResourceService.Settings(1048576, 60000, 120000, 4096, 300000, 256);
ResourceService resourceService = new ResourceService(mock(TimerService.class), redissonClient, blobStorage,
lockService, new JsonObject(resourceConfig), null);
lockService, settings, null);
rateLimiter = new RateLimiter(vertx, resourceService);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,15 @@ public static void afterAll() throws IOException {
}

@BeforeEach
public void beforeEach() {
public void beforeEach() throws Exception {
RKeys keys = redissonClient.getKeys();
for (String key : keys.getKeys()) {
keys.delete(key);
}
LockService lockService = new LockService(redissonClient, null);
String resourceConfig = """
{
"maxSize" : 1048576,
"syncPeriod": 60000,
"syncDelay": 120000,
"syncBatch": 4096,
"cacheExpiration": 300000,
"compressionMinSize": 256
}
""";
ResourceService resourceService = new ResourceService(mock(TimerService.class), redissonClient, blobStorage, lockService, new JsonObject(resourceConfig), null);
ResourceService.Settings settings = new ResourceService.Settings(1048576, 60000, 120000, 4096, 300000, 256);
ResourceService resourceService = new ResourceService(mock(TimerService.class), redissonClient, blobStorage,
lockService, settings, null);
store = new ApiKeyStore(resourceService, vertx);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.epam.aidial.core.server.storage.BlobStorage;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -87,24 +86,15 @@ public static void afterAll() throws IOException {
}

@BeforeEach
public void beforeEach() {
public void beforeEach() throws Exception {
RKeys keys = redissonClient.getKeys();
for (String key : keys.getKeys()) {
keys.delete(key);
}
LockService lockService = new LockService(redissonClient, null);
String resourceConfig = """
{
"maxSize" : 1048576,
"syncPeriod": 60000,
"syncDelay": 120000,
"syncBatch": 4096,
"cacheExpiration": 300000,
"compressionMinSize": 256
}
""";
ResourceService.Settings settings = new ResourceService.Settings(1048576, 60000, 120000, 4096, 300000, 256);
ResourceService resourceService = new ResourceService(mock(TimerService.class), redissonClient, blobStorage,
lockService, new JsonObject(resourceConfig), null);
lockService, settings, null);
tracker = new TokenStatsTracker(vertx, resourceService);
}

Expand Down

0 comments on commit 4f1397c

Please sign in to comment.