Skip to content

Commit

Permalink
Configuration refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Smelov committed Jun 5, 2024
1 parent 6426684 commit c134f80
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 38 deletions.
73 changes: 38 additions & 35 deletions src/main/java/com/exactpro/th2/estore/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,64 @@
package com.exactpro.th2.estore;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Configuration {
private static final int DEFAULT_MAX_TASK_RETRIES = 1000000;
private static final int DEFAULT_MAX_TASK_COUNT = 256;
private static final long DEFAULT_RETRY_DELAY_BASEM_S = 5000;
private static final int DEFAULT_PROCESSING_THREADS = Runtime.getRuntime().availableProcessors();

private Integer maxTaskCount;
private Long maxTaskDataSize;
private Integer maxRetryCount;
private Long retryDelayBase;
private Integer processingThreads;
private final int maxTaskCount;
private final long maxTaskDataSize;
private final int maxRetryCount;
private final long retryDelayBase;
private final int processingThreads;

public Long getMaxTaskDataSize() {
return maxTaskDataSize == null ? defaultMaxDataSize() : maxTaskDataSize;
}

public Integer getMaxTaskCount() {
return maxTaskCount == null ? DEFAULT_MAX_TASK_COUNT : maxTaskCount;
public Configuration() {
this(DEFAULT_MAX_TASK_COUNT, defaultMaxDataSize(), DEFAULT_MAX_TASK_RETRIES,
DEFAULT_RETRY_DELAY_BASEM_S, DEFAULT_PROCESSING_THREADS);
}

public Integer getMaxRetryCount() {
return maxRetryCount == null ? DEFAULT_MAX_TASK_RETRIES : maxRetryCount;
@JsonCreator
public Configuration(
@JsonProperty("maxTaskCount") Integer maxTaskCount,
@JsonProperty("maxTaskDataSize") Long maxTaskDataSize,
@JsonProperty("maxRetryCount") Integer maxRetryCount,
@JsonProperty ("retryDelayBase") Long retryDelayBase,
@JsonProperty("processingThreads") Integer processingThreads
) {
this.maxTaskCount = maxTaskCount == null ? DEFAULT_MAX_TASK_COUNT : maxTaskCount;
this.maxTaskDataSize = maxTaskDataSize == null ? defaultMaxDataSize() : maxTaskDataSize;
this.maxRetryCount = maxRetryCount == null ? DEFAULT_MAX_TASK_RETRIES : maxRetryCount;
this.retryDelayBase = retryDelayBase == null ? DEFAULT_RETRY_DELAY_BASEM_S : retryDelayBase;
this.processingThreads = processingThreads == null ? DEFAULT_PROCESSING_THREADS : processingThreads;

if (this.maxTaskCount <= 1) throw new IllegalArgumentException("'maxTaskCount' should be >=1. Actual: " + maxTaskCount);
if (this.maxTaskDataSize <= 1) throw new IllegalArgumentException("'maxTaskDataSize' should be >=1. Actual: " + maxTaskDataSize);
if (this.maxRetryCount <= 0) throw new IllegalArgumentException("'maxRetryCount' should be >=0. Actual: " + maxRetryCount);
if (this.retryDelayBase <= 1) throw new IllegalArgumentException("'retryDelayBase' should be >=1. Actual: " + retryDelayBase);
if (this.processingThreads <= 1) throw new IllegalArgumentException("'processingThreads' should be >=1. Actual: " + processingThreads);
}

public Long getRetryDelayBase() {
return retryDelayBase == null ? DEFAULT_RETRY_DELAY_BASEM_S : retryDelayBase;
public int getMaxTaskCount() {
return maxTaskCount;
}

public int getProcessingThreads() {
return processingThreads == null ? DEFAULT_PROCESSING_THREADS : processingThreads;
public long getMaxTaskDataSize() {
return maxTaskDataSize;
}

public Configuration() {
this(DEFAULT_MAX_TASK_COUNT, DEFAULT_MAX_TASK_RETRIES, DEFAULT_RETRY_DELAY_BASEM_S,
defaultMaxDataSize(), DEFAULT_PROCESSING_THREADS);
public int getMaxRetryCount() {
return maxRetryCount;
}

@JsonCreator
public Configuration(Integer maxTaskCount, Integer maxTaskRetries, Long taskRetryDelayBase,
Long maxTaskDataSize, Integer processingThreads) {
this.maxTaskCount = maxTaskCount;
this.maxRetryCount = maxTaskRetries;
this.retryDelayBase = taskRetryDelayBase;
this.maxTaskDataSize = maxTaskDataSize;
this.processingThreads = processingThreads;
validate();
public long getRetryDelayBase() {
return retryDelayBase;
}

public void validate() {
if (maxTaskCount <= 1) throw new IllegalArgumentException("'maxTaskCount' should be >=1. Actual: " + maxTaskCount);
if (maxTaskDataSize <= 1) throw new IllegalArgumentException("'maxTaskDataSize' should be >=1. Actual: " + maxTaskDataSize);
if (maxRetryCount <= 0) throw new IllegalArgumentException("'maxRetryCount' should be >=0. Actual: " + maxRetryCount);
if (retryDelayBase <= 1) throw new IllegalArgumentException("'retryDelayBase' should be >=1. Actual: " + retryDelayBase);
if (processingThreads <= 1) throw new IllegalArgumentException("'processingThreads' should be >=1. Actual: " + processingThreads);
public int getProcessingThreads() {
return processingThreads;
}

private static long defaultMaxDataSize() {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/exactpro/th2/estore/EventStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public static void main(String[] args) {
Configuration config = factory.getCustomConfiguration(Configuration.class);
if (config == null) {
config = new Configuration();
} else {
config.validate();
}

LOGGER.info("Effective configuration:\n{}", config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void setUp() throws CradleStorageException, IOException, InterruptedException {
cradleEntitiesFactory = spy(new CradleEntitiesFactory(MAX_MESSAGE_BATCH_SIZE, MAX_TEST_EVENT_BATCH_SIZE, STORE_ACTION_REJECTION_THRESHOLD));
doReturn(CompletableFuture.completedFuture(null)).when(storageMock).storeTestEventAsync(any());

Configuration config = new Configuration(MAX_EVENT_QUEUE_TASK_SIZE, MAX_EVENT_PERSIST_RETRIES, 10L, MAX_EVENT_QUEUE_DATA_SIZE, TASK_PROCESSING_THREADS);
Configuration config = new Configuration(MAX_EVENT_QUEUE_TASK_SIZE, MAX_EVENT_QUEUE_DATA_SIZE, MAX_EVENT_PERSIST_RETRIES, 10L, TASK_PROCESSING_THREADS);
persistor = spy(new EventPersistor(errorCollector, config, storageMock));
persistor.start();
}
Expand Down

0 comments on commit c134f80

Please sign in to comment.