-
Notifications
You must be signed in to change notification settings - Fork 1
Added microservice entry-point #278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Nikita-Smirnov-Exactpro
wants to merge
13
commits into
master
Choose a base branch
from
microservice-main
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
665af82
Added microservice entry-point
Nikita-Smirnov-Exactpro c2b86df
Added configuration provider
Nikita-Smirnov-Exactpro b2261f9
Refactored
Nikita-Smirnov-Exactpro 07a3537
Refactored CommonFactory constructors
Nikita-Smirnov-Exactpro d3cb3b5
Corrected after review
Nikita-Smirnov-Exactpro 4d83df0
tmp
Nikita-Smirnov-Exactpro 70e6467
Merge remote-tracking branch 'origin/master' into microservice-main
Nikita-Smirnov-Exactpro 19cd19c
Added IDictionaryProvider
Nikita-Smirnov-Exactpro 9078577
Refactored load dictionary tests
Nikita-Smirnov-Exactpro 5185125
Merge remote-tracking branch 'origin/load-dictionary-tests' into micr…
Nikita-Smirnov-Exactpro 0fde596
Refactored CommonFactoryTest
Nikita-Smirnov-Exactpro 1515f9c
Merge remote-tracking branch 'origin/master' into microservice-main
Nikita-Smirnov-Exactpro 5582958
Corrected after review
Nikita-Smirnov-Exactpro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/com/exactpro/th2/common/microservice/ApplicationContext.kt
This file contains hidden or 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,28 @@ | ||
| /* | ||
| * Copyright 2023 Exactpro (Exactpro Systems Limited) | ||
| * | ||
| * 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.exactpro.th2.common.microservice | ||
|
|
||
| import com.exactpro.th2.common.schema.factory.AbstractCommonFactory | ||
|
|
||
| /** | ||
| * @param registerResource: register all resources which must be closed in reverse registration order. | ||
| * @param onPanic: call this method when application can not correct work anymore. | ||
| */ | ||
| class ApplicationContext( | ||
| val commonFactory: AbstractCommonFactory, | ||
| val registerResource: (AutoCloseable) -> Unit, | ||
| val onPanic: (Throwable?) -> Unit, | ||
Nikita-Smirnov-Exactpro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/com/exactpro/th2/common/microservice/IApplication.kt
This file contains hidden or 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,31 @@ | ||
| /* | ||
| * Copyright 2023 Exactpro (Exactpro Systems Limited) | ||
| * | ||
| * 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.exactpro.th2.common.microservice | ||
|
|
||
| /** | ||
| * Instance of one time application which works from the [IApplication.start] to [IApplication.close] method. | ||
| */ | ||
| interface IApplication: AutoCloseable { | ||
| /** | ||
| * Starts one time application. | ||
| * This method can be called only once. | ||
| * @exception IllegalStateException can be thrown when: | ||
| * * the method is called the two or more time | ||
| * * close method has been called before | ||
| * * other reasons | ||
| */ | ||
| fun start() | ||
| } |
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/com/exactpro/th2/common/microservice/IApplicationFactory.kt
This file contains hidden or 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,32 @@ | ||
| /* | ||
| * Copyright 2023 Exactpro (Exactpro Systems Limited) | ||
| * | ||
| * 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.exactpro.th2.common.microservice | ||
|
|
||
| /** | ||
| * Factory can share closable resources between application. They should be closed on [IApplicationFactory.close] method call | ||
| */ | ||
| interface IApplicationFactory : AutoCloseable { | ||
| /** | ||
| * Creates onetime application. | ||
| * If you need restart of reconfigure application, close old instance and create new one. | ||
| */ | ||
| fun createApplication(context: ApplicationContext): IApplication | ||
|
|
||
| /** | ||
| * Close resources shared between created application. | ||
| */ | ||
| override fun close() {} | ||
| } |
151 changes: 151 additions & 0 deletions
151
src/main/kotlin/com/exactpro/th2/common/microservice/Main.kt
This file contains hidden or 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,151 @@ | ||
| /* | ||
| * Copyright 2023 Exactpro (Exactpro Systems Limited) | ||
| * | ||
| * 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.exactpro.th2.common.microservice | ||
|
|
||
| import com.exactpro.th2.common.metrics.MetricMonitor | ||
| import com.exactpro.th2.common.metrics.registerLiveness | ||
| import com.exactpro.th2.common.metrics.registerReadiness | ||
| import com.exactpro.th2.common.schema.factory.CommonFactory | ||
| import mu.KotlinLogging | ||
| import java.util.Deque | ||
| import java.util.ServiceLoader | ||
| import java.util.concurrent.ConcurrentLinkedDeque | ||
| import java.util.concurrent.locks.Condition | ||
| import java.util.concurrent.locks.ReentrantLock | ||
| import kotlin.concurrent.thread | ||
| import kotlin.concurrent.withLock | ||
| import kotlin.system.exitProcess | ||
|
|
||
| private val K_LOGGER = KotlinLogging.logger {} | ||
|
|
||
| class Main(args: Array<String>) { | ||
|
|
||
| private val liveness: MetricMonitor = registerLiveness(MONITOR_NAME) | ||
| private val readiness: MetricMonitor = registerReadiness(MONITOR_NAME) | ||
|
|
||
| private val resources: Deque<AutoCloseable> = ConcurrentLinkedDeque() | ||
| private val lock = ReentrantLock() | ||
| private val condition: Condition = lock.newCondition() | ||
|
|
||
| private val commonFactory: CommonFactory | ||
| private val application: IApplication | ||
|
|
||
| init { | ||
| configureShutdownHook(resources, lock, condition, liveness, readiness) | ||
|
|
||
| K_LOGGER.info { "Starting a component" } | ||
| liveness.enable() | ||
|
|
||
| val applicationFactory = loadApplicationFactory() | ||
| .also(resources::add) | ||
| commonFactory = CommonFactory.createFromArguments(*args) | ||
| .also(resources::add) | ||
| application = applicationFactory.createApplication(ApplicationContext(commonFactory, resources::add, ::panic)) | ||
| .also(resources::add) | ||
|
|
||
| K_LOGGER.info { "Prepared the '${commonFactory.boxConfiguration.boxName}' component" } | ||
| } | ||
|
|
||
| fun run() { | ||
| K_LOGGER.info { "Starting the '${commonFactory.boxConfiguration.boxName}' component" } | ||
|
|
||
| application.start() | ||
|
|
||
| K_LOGGER.info { "Started the '${commonFactory.boxConfiguration.boxName}' component" } | ||
| readiness.enable() | ||
|
|
||
| awaitShutdown(lock, condition) | ||
| } | ||
|
|
||
| companion object { | ||
| private const val APPLICATION_FACTORY_SYSTEM_PROPERTY = "th2.microservice.application-factory" | ||
| private const val MONITOR_NAME = "microservice_main" | ||
| private fun loadApplicationFactory(): IApplicationFactory { | ||
| val instances = ServiceLoader.load(IApplicationFactory::class.java).toList() | ||
| return when (instances.size) { | ||
| 0 -> error("No instances of ${IApplicationFactory::class.simpleName}") | ||
| 1 -> instances.single().also { single -> | ||
| System.getProperty(APPLICATION_FACTORY_SYSTEM_PROPERTY)?.let { value -> | ||
| check(value == single::class.qualifiedName) { | ||
| "Found instance of ${IApplicationFactory::class.simpleName} mismatches the class specified by $APPLICATION_FACTORY_SYSTEM_PROPERTY system property," + | ||
| "configured: $value, found: ${single::class.qualifiedName}" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| else -> { | ||
| System.getProperty(APPLICATION_FACTORY_SYSTEM_PROPERTY)?.let { value -> | ||
| instances.find { value == it::class.qualifiedName } | ||
| ?: error( | ||
| "Found instances of ${IApplicationFactory::class.simpleName} mismatches the class specified by $APPLICATION_FACTORY_SYSTEM_PROPERTY system property," + | ||
| "configured: $value, found: ${instances.map { Object::class.qualifiedName }}" | ||
| ) | ||
| } ?: error( | ||
| "More than 1 instance of ${IApplicationFactory::class.simpleName} has been found " + | ||
| "and $APPLICATION_FACTORY_SYSTEM_PROPERTY system property isn't specified," + | ||
| "instances: $instances" | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun main(args: Array<String>) { | ||
Nikita-Smirnov-Exactpro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try { | ||
| Main(args).run() | ||
| } catch (ex: Exception) { | ||
| K_LOGGER.error(ex) { "Cannot start the box" } | ||
| exitProcess(1) | ||
| } | ||
| } | ||
| fun panic(ex: Throwable?) { | ||
| K_LOGGER.error(ex) { "Component panic exception" } | ||
| exitProcess(2) | ||
| } | ||
| fun configureShutdownHook( | ||
| resources: Deque<AutoCloseable>, | ||
| lock: ReentrantLock, | ||
| condition: Condition, | ||
| liveness: MetricMonitor, | ||
| readiness: MetricMonitor, | ||
| ) { | ||
| Runtime.getRuntime().addShutdownHook(thread( | ||
| start = false, | ||
| name = "Shutdown-hook" | ||
| ) { | ||
| K_LOGGER.info { "Shutdown start" } | ||
| readiness.disable() | ||
| lock.withLock { condition.signalAll() } | ||
| resources.descendingIterator().forEachRemaining { resource -> | ||
| runCatching { | ||
| resource.close() | ||
| }.onFailure { e -> | ||
| K_LOGGER.error(e) { "Cannot close resource ${resource::class}" } | ||
| } | ||
| } | ||
| liveness.disable() | ||
| K_LOGGER.info { "Shutdown end" } | ||
| }) | ||
| } | ||
| @Throws(InterruptedException::class) | ||
| fun awaitShutdown(lock: ReentrantLock, condition: Condition) { | ||
| lock.withLock { | ||
| K_LOGGER.info { "Wait shutdown" } | ||
| condition.await() | ||
| K_LOGGER.info { "App shutdown" } | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/com/exactpro/th2/common/schema/configuration/IConfigurationProvider.kt
This file contains hidden or 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,20 @@ | ||
| /* | ||
| * Copyright 2023 Exactpro (Exactpro Systems Limited) | ||
| * 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.exactpro.th2.common.schema.configuration | ||
|
|
||
| interface IConfigurationProvider { | ||
| fun <T : Any> load(configClass: Class<T>, alias: String, default: () -> T): T | ||
Nikita-Smirnov-Exactpro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.