Skip to content
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

Document checkpoint simulator, with java/groovy/kotlin examples #117

Merged
merged 4 commits into from
Jun 1, 2023

Conversation

wetted
Copy link
Contributor

@wetted wetted commented May 23, 2023

closes #115

@wetted wetted requested a review from timyates May 24, 2023 16:40
@wetted
Copy link
Contributor Author

wetted commented May 24, 2023

@timyates I also thought trying something like the following, but it's apparently not exactly what you meant.

@MicronautTest
class CheckpointSimulatorTest extends Specification {

    void 'emulate checkpoint'() {
        given:
        EmbeddedServer server = ApplicationContext.run(EmbeddedServer.class, ['spec.name': 'CheckpointSimulatorTest'], Environment.TEST)
        CheckpointSimulator checkpointSimulator = server.getApplicationContext().getBean(CheckpointSimulator) // <1>
        TestResource testResource = server.getApplicationContext().getBean(TestResource)

        expect:
        testResource
        testResource.running

        when:
        checkpointSimulator.runBeforeCheckpoint() // <2>
        then:
        !testResource.running

        when:
        checkpointSimulator.runAfterRestore() // <3>
        then:
        testResource.running
    }

    @Singleton
    @Requires(property = "spec.name", value = "CheckpointSimulatorTest")
    static class TestResource implements OrderedResource {
        boolean running = true
        @Override
        void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
            running = false
        }
        @Override
        void afterRestore(Context<? extends Resource> context) throws Exception {
            running = true
        }
    }
}

@timyates
Copy link
Contributor

Hi @wetted 👋

I meant something like this to show people how to implement their own Resource handlers for their own custom beans

import io.micronaut.context.BeanContext
import io.micronaut.context.annotation.Property
import io.micronaut.context.annotation.Requires
import io.micronaut.crac.test.CheckpointSimulator
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import jakarta.inject.Singleton
import org.crac.Context
import org.crac.Resource
import spock.lang.Specification

@MicronautTest
@Property(name = "spec.name", value = SPEC_NAME)
class OrderedResourceCheckpointSimulatorTest extends Specification {

    public static final String SPEC_NAME = "OrderedResourceCheckpointSimulatorTest"

    @Inject
    BeanContext ctx

    void "test custom OrderedResource implementation using the CheckpointSimulator"() {
        given:
        def myBean = ctx.getBean(ResourceBean)
        def checkpointSimulator = ctx.getBean(CheckpointSimulator)

        expect:
        myBean.running

        when:
        checkpointSimulator.runBeforeCheckpoint()

        then:
        !myBean.running

        when:
        checkpointSimulator.runAfterRestore()

        then:
        myBean.running
    }

    /**
     * A bean to demonstrate how to write and test custom OrderedResource implementations.
     * Here it is doing nothing for sake of the example, but in reality it would have a socket or something that needs to be closed.
     */
    @Singleton
    @Requires(property = "spec.name", value = SPEC_NAME)
    static class ResourceBean {

        private boolean running = true

        boolean isRunning() {
            return running
        }

        void stop() {
            this.running = false
        }

        void start() {
            this.running = true
        }
    }

    /**
     * An OrderedResource which will stop the {@link ResourceBean} before a checkpoint and start it again after a restore.
     */
    @Singleton
    @Requires(property = "spec.name", value = SPEC_NAME)
    static class ResourceBeanResource implements OrderedResource {

        private final ResourceBean resourceBean

        ResourceBeanResource(ResourceBean resourceBean) {
            this.resourceBean = resourceBean
        }

        @Override
        void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
            resourceBean.stop()
        }

        @Override
        void afterRestore(Context<? extends Resource> context) throws Exception {
            resourceBean.start()
        }
    }
}

@wetted wetted marked this pull request as ready for review May 25, 2023 19:27
@wetted wetted requested a review from sdelamo May 25, 2023 19:27
Copy link
Contributor

@timyates timyates left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs had static in the bean and resource definition, so I moved them from being inner classes so we could get rid of it

@sonarcloud
Copy link

sonarcloud bot commented May 26, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@sdelamo sdelamo merged commit ec0edf9 into 1.2.x Jun 1, 2023
@sdelamo sdelamo deleted the document_checkpoint_simulator branch June 1, 2023 08:21
sdelamo added a commit that referenced this pull request Jun 1, 2023
* use Gradle Kotlin DSL

* Update slsa-framework/slsa-github-generator action to v1.3.0 (#42)

* ci: distribution temurin cla provenance (#44)

* Update stefanzweifel/git-auto-commit-action action to v4.15.4 (#43)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* build: Micronaut Framework 3.7.4

* build: spock 2.2-groovy-3.0

* doc: eager singletons and inject HttpClient (#66)

See micronaut-projects/micronaut-test#716

* ci: projectVersion 1.2.0-SNAPSHOT

* ci: githubCoreBranch 3.9.x

* build: Micronaut Framework 3.8.5

* build: Micronaut Test 3.8.2

* build: Spock 2.3-groovy-3.0

* wip DataSourceResolver

* Convert Delegating datasource and filter non-hikari contents

* Test

* Test to ensure DBCP still works and isn't supported

* Try to downgrade Jooq

* remove @Inject BeanContext ctx

* Simplify composite (#73)

* Simplify composite

* Extract common code to method

---------

Co-authored-by: Tim Yates <[email protected]>

* build: Micronaut Framework 3.8.6

* build: Micronaut Test 3.9.1

* ci: GitHub Actions sync

* [skip ci] Release v1.2.0

* Back to 1.2.1-SNAPSHOT

* Redis support for CRaC (#78)

* wip: Redis support for CRaC

- Currently doesn't work for `@Cacheable` tags
- Needs manual testing with a proper app

* Add base buildSrc plugin

* Add config and tests

* Docs

* Update src/main/docs/guide/resource/redis.adoc

Co-authored-by: Sergio del Amo <[email protected]>

---------

Co-authored-by: Sergio del Amo <[email protected]>

* [skip ci] Release v1.2.1

* Back to 1.2.2-SNAPSHOT

* bug: StatefulRedisPubSubConnection beans are not supported (#91)

* Also destroy StatefulRedisPubSubConnectionResource beans

As raised here #63 (comment)

StatefulRedisPubSubConnection beans were not destroyed during checkpointing, and so we were getting connection closed errors.

* Fix. There are no PubSub beans, they are all connection beans

* Reduce duplication

* Checkstyle...

* Naming

* Visibility

* [skip ci] Release v1.2.2

* Back to 1.2.3-SNAPSHOT

* Handle multiple named Redis servers (#92)

* Handle multiple named Redis servers

* Remove debug

* Fix imports

* Remove deprecated experimental classes and add accepted-api-changes

* [skip ci] Release v1.2.3

* Back to 1.2.4-SNAPSHOT

* Document checkpoint simulator, with java/groovy/kotlin examples (#117)

* Document checkpoint simulator, with java/groovy/kotlin examples

closes #115

* documentation for CheckpointSimulator

* Java/Groovy/Kotlin docs examples

* Extract documented classes to get rid of static

---------

Co-authored-by: Tim Yates <[email protected]>

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: micronaut-build <[email protected]>
Co-authored-by: Tim Yates <[email protected]>
Co-authored-by: micronaut-build <[email protected]>
Co-authored-by: Dean Wette <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants