-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring configuration management
Signed-off-by: Atanas Atanasov <[email protected]>
- Loading branch information
Showing
11 changed files
with
205 additions
and
36 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
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
55 changes: 55 additions & 0 deletions
55
server/src/main/java/com/hedera/block/server/config/ServerMappedConfigSourceInitializer.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,55 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hedera.block.server.config; | ||
|
||
import com.swirlds.config.extensions.sources.ConfigMapping; | ||
import com.swirlds.config.extensions.sources.MappedConfigSource; | ||
import com.swirlds.config.extensions.sources.SystemEnvironmentConfigSource; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import java.util.List; | ||
|
||
/** | ||
* A class that extends {@link MappedConfigSource} ir order to have project-relevant initialization. | ||
*/ | ||
public final class ServerMappedConfigSourceInitializer { | ||
private static final List<ConfigMapping> MAPPINGS = | ||
List.of( | ||
new ConfigMapping( | ||
"consumer.timeoutThresholdMillis", "CONSUMER_TIMEOUT_THRESHOLD_MILLIS"), | ||
new ConfigMapping( | ||
"persistence.storage.rootPath", "PERSISTENCE_STORAGE_ROOT_PATH"), | ||
new ConfigMapping("service.delayMillis", "SERVICE_DELAY_MILLIS"), | ||
new ConfigMapping("mediator.ringBufferSize", "MEDIATOR_RING_BUFFER_SIZE"), | ||
new ConfigMapping("notifier.ringBufferSize", "NOTIFIER_RING_BUFFER_SIZE")); | ||
|
||
private ServerMappedConfigSourceInitializer() {} | ||
|
||
/** | ||
* This method constructs, initializes and returns a new instance of {@link MappedConfigSource} | ||
* which internally uses {@link SystemEnvironmentConfigSource} and maps relevant config keys to | ||
* other keys so that they could be used within the application | ||
* | ||
* @return newly constructed fully initialized {@link MappedConfigSource} | ||
*/ | ||
@NonNull | ||
public static MappedConfigSource getMappedConfigSource() { | ||
final MappedConfigSource config = | ||
new MappedConfigSource(SystemEnvironmentConfigSource.getInstance()); | ||
MAPPINGS.forEach(config::addMapping); | ||
return config; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
prometheus.endpointPortNumber=9999 | ||
|
||
# Ring buffer sizes for the mediator and notifier | ||
mediator.ringBufferSize=67108864 | ||
notifier.ringBufferSize=2048 | ||
#mediator.ringBufferSize= | ||
#notifier.ringBufferSize= | ||
|
||
# Timeout for consumers to wait for block item before timing out. Default is 1500 milliseconds. | ||
#consumer.timeoutThresholdMillis=1500 |
83 changes: 83 additions & 0 deletions
83
...src/test/java/com/hedera/block/server/config/ServerMappedConfigSourceInitializerTest.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,83 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hedera.block.server.config; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.swirlds.config.extensions.sources.ConfigMapping; | ||
import com.swirlds.config.extensions.sources.MappedConfigSource; | ||
import java.lang.reflect.Field; | ||
import java.util.Queue; | ||
import java.util.function.Predicate; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ServerMappedConfigSourceInitializerTest { | ||
private static final ConfigMapping[] SUPPORTED_MAPPINGS = { | ||
new ConfigMapping("consumer.timeoutThresholdMillis", "CONSUMER_TIMEOUT_THRESHOLD_MILLIS"), | ||
new ConfigMapping("persistence.storage.rootPath", "PERSISTENCE_STORAGE_ROOT_PATH"), | ||
new ConfigMapping("service.delayMillis", "SERVICE_DELAY_MILLIS"), | ||
new ConfigMapping("mediator.ringBufferSize", "MEDIATOR_RING_BUFFER_SIZE"), | ||
new ConfigMapping("notifier.ringBufferSize", "NOTIFIER_RING_BUFFER_SIZE") | ||
}; | ||
private static MappedConfigSource toTest; | ||
|
||
@BeforeAll | ||
static void setUp() { | ||
toTest = ServerMappedConfigSourceInitializer.getMappedConfigSource(); | ||
} | ||
|
||
/** | ||
* This test aims to fail if we have added or removed any {@link ConfigMapping} that will be | ||
* initialized by the {@link ServerMappedConfigSourceInitializer#getMappedConfigSource()} | ||
* without reflecting it here in the test. The purpose is to bring attention to any changes to | ||
* the developer so we can make sure we are aware of them in order to be sure we require the | ||
* change. This test is more to bring attention than to test actual logic. So if this fails, we | ||
* either change the {@link #SUPPORTED_MAPPINGS} here or the {@link | ||
* ServerMappedConfigSourceInitializer#MAPPINGS} to make this pass. | ||
*/ | ||
@Test | ||
void test_VerifyAllSupportedMappingsAreAddedToInstance() throws ReflectiveOperationException { | ||
final Queue<ConfigMapping> actual = extractConfigMappings(); | ||
|
||
assertEquals(SUPPORTED_MAPPINGS.length, actual.size()); | ||
|
||
for (final ConfigMapping current : SUPPORTED_MAPPINGS) { | ||
final Predicate<ConfigMapping> predicate = | ||
cm -> | ||
current.mappedName().equals(cm.mappedName()) | ||
&& current.originalName().equals(cm.originalName()); | ||
assertTrue( | ||
actual.stream().anyMatch(predicate), | ||
() -> | ||
"when testing for: [%s] it is not contained in mappings of the actual initialized object %s" | ||
.formatted(current, actual)); | ||
} | ||
} | ||
|
||
private static Queue<ConfigMapping> extractConfigMappings() | ||
throws ReflectiveOperationException { | ||
final Field configMappings = MappedConfigSource.class.getDeclaredField("configMappings"); | ||
try { | ||
configMappings.setAccessible(true); | ||
return (Queue<ConfigMapping>) configMappings.get(toTest); | ||
} finally { | ||
configMappings.setAccessible(false); | ||
} | ||
} | ||
} |
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