Search in sources :

Example 1 with KieServerStateRepository

use of org.kie.server.services.impl.storage.KieServerStateRepository in project droolsjbpm-integration by kiegroup.

the class KieControllerCrashIntegrationTest method testCrashAfterRegistered.

@Test
public void testCrashAfterRegistered() throws Exception {
    final String SERVER_TEMPLATE_ID = "template-id";
    final String SERVER_NAME = "server-name";
    KieServerEnvironment.setServerId(SERVER_TEMPLATE_ID);
    ServerTemplate serverTemplate = new ServerTemplate(SERVER_TEMPLATE_ID, SERVER_NAME);
    controllerClient.saveServerTemplate(serverTemplate);
    ServerTemplateList instanceList = controllerClient.listServerTemplates();
    assertEquals(1, instanceList.getServerTemplates().length);
    // Register kie server in controller.
    KieServerInfo kieServerInfo = new KieServerInfo(SERVER_TEMPLATE_ID, "1.0.0");
    kieServerInfo.setLocation("http://127.0.0.1:20000");
    kieServerInfo.setMode(KieServerMode.PRODUCTION);
    kieServerInfo.setName(SERVER_NAME);
    KieServerRegistry registry = new KieServerRegistryImpl();
    KieServerStateRepository dummyKieServerStateRepository = new KieServerStateRepository() {

        @Override
        public void store(String serverId, KieServerState kieServerState) {
        }

        @Override
        public KieServerState load(String serverId) {
            KieServerState kieServerState = new KieServerState();
            kieServerState.setControllers(Collections.singleton(TestConfig.getControllerHttpUrl()));
            kieServerState.setConfiguration(new KieServerConfig());
            if (TestConfig.isLocalServer()) {
                kieServerState.getConfiguration().addConfigItem(new KieServerConfigItem(KieServerConstants.CFG_KIE_CONTROLLER_USER, "", null));
                kieServerState.getConfiguration().addConfigItem(new KieServerConfigItem(KieServerConstants.CFG_KIE_CONTROLLER_PASSWORD, "", null));
            } else {
                kieServerState.getConfiguration().addConfigItem(new KieServerConfigItem(KieServerConstants.CFG_KIE_CONTROLLER_USER, TestConfig.getUsername(), null));
                kieServerState.getConfiguration().addConfigItem(new KieServerConfigItem(KieServerConstants.CFG_KIE_CONTROLLER_PASSWORD, TestConfig.getPassword(), null));
            }
            return kieServerState;
        }
    };
    registry.registerStateRepository(dummyKieServerStateRepository);
    KieServerController controller = new DefaultRestControllerImpl(registry);
    controller.connect(kieServerInfo);
    // Check that kie server is registered.
    serverUp.await(5, TimeUnit.SECONDS);
    ServerInstanceKeyList list = controllerClient.getServerInstances(instanceList.getServerTemplates()[0].getId());
    if (list.getServerInstanceKeys().length == 0) {
        // Race condition when health check deleted server instance sooner than we were able to check. Resubmitting server instance again.
        serverUp = new CountDownLatch(1);
        serverDown = new CountDownLatch(1);
        controller.connect(kieServerInfo);
        serverUp.await(5, TimeUnit.SECONDS);
        list = controllerClient.getServerInstances(instanceList.getServerTemplates()[0].getId());
    }
    assertNotNull(list.getServerInstanceKeys());
    assertEquals(1, list.getServerInstanceKeys().length);
    // Check that kie server is deregistered automatically.
    serverDown.await(5, TimeUnit.SECONDS);
    list = controllerClient.getServerInstances(instanceList.getServerTemplates()[0].getId());
    assertNotNull(list.getServerInstanceKeys());
    assertEquals(0, list.getServerInstanceKeys().length);
}
Also used : KieServerController(org.kie.server.controller.api.KieServerController) KieServerStateRepository(org.kie.server.services.impl.storage.KieServerStateRepository) ServerTemplateList(org.kie.server.controller.api.model.spec.ServerTemplateList) KieServerInfo(org.kie.server.api.model.KieServerInfo) KieServerConfig(org.kie.server.api.model.KieServerConfig) KieServerConfigItem(org.kie.server.api.model.KieServerConfigItem) CountDownLatch(java.util.concurrent.CountDownLatch) KieServerState(org.kie.server.services.impl.storage.KieServerState) ServerTemplate(org.kie.server.controller.api.model.spec.ServerTemplate) KieServerRegistry(org.kie.server.services.api.KieServerRegistry) ServerInstanceKeyList(org.kie.server.controller.api.model.runtime.ServerInstanceKeyList) DefaultRestControllerImpl(org.kie.server.services.impl.controller.DefaultRestControllerImpl) KieServerRegistryImpl(org.kie.server.services.impl.KieServerRegistryImpl) Test(org.junit.Test)

Example 2 with KieServerStateRepository

use of org.kie.server.services.impl.storage.KieServerStateRepository in project droolsjbpm-integration by kiegroup.

the class KieControllerValidationIntegrationTest method init.

@Before
public void init() {
    dummyKieServerStateRepository = new KieServerStateRepository() {

        @Override
        public void store(String serverId, KieServerState kieServerState) {
        }

        @Override
        public KieServerState load(String serverId) {
            KieServerState kieServerState = new KieServerState();
            kieServerState.setControllers(Collections.singleton(TestConfig.getControllerHttpUrl()));
            kieServerState.setConfiguration(new KieServerConfig());
            if (TestConfig.isLocalServer()) {
                kieServerState.getConfiguration().addConfigItem(new KieServerConfigItem(KieServerConstants.CFG_KIE_CONTROLLER_USER, "", null));
                kieServerState.getConfiguration().addConfigItem(new KieServerConfigItem(KieServerConstants.CFG_KIE_CONTROLLER_PASSWORD, "", null));
            } else {
                kieServerState.getConfiguration().addConfigItem(new KieServerConfigItem(KieServerConstants.CFG_KIE_CONTROLLER_USER, TestConfig.getUsername(), null));
                kieServerState.getConfiguration().addConfigItem(new KieServerConfigItem(KieServerConstants.CFG_KIE_CONTROLLER_PASSWORD, TestConfig.getPassword(), null));
            }
            return kieServerState;
        }
    };
    registry = new KieServerRegistryImpl();
    registry.registerStateRepository(dummyKieServerStateRepository);
}
Also used : KieServerState(org.kie.server.services.impl.storage.KieServerState) KieServerStateRepository(org.kie.server.services.impl.storage.KieServerStateRepository) KieServerConfig(org.kie.server.api.model.KieServerConfig) KieServerConfigItem(org.kie.server.api.model.KieServerConfigItem) KieServerRegistryImpl(org.kie.server.services.impl.KieServerRegistryImpl) Before(org.junit.Before)

Example 3 with KieServerStateRepository

use of org.kie.server.services.impl.storage.KieServerStateRepository in project droolsjbpm-integration by kiegroup.

the class AbstractKieServerImplTest method testPersistScannerState.

@Test
public // https://issues.jboss.org/browse/RHBPMS-4087
void testPersistScannerState() {
    String containerId = "persist-scanner-state";
    createEmptyKjar(containerId);
    // create the container and update the scanner
    KieContainerResource kieContainerResource = new KieContainerResource(containerId, new ReleaseId(releaseId));
    kieServer.createContainer(containerId, kieContainerResource);
    KieScannerResource kieScannerResource = new KieScannerResource(KieScannerStatus.STARTED, 20000L);
    kieServer.updateScanner(containerId, kieScannerResource);
    KieServerStateRepository stateRepository = new KieServerStateFileRepository(REPOSITORY_DIR);
    KieServerState state = stateRepository.load(KIE_SERVER_ID);
    Set<KieContainerResource> containers = state.getContainers();
    Assertions.assertThat(containers).hasSize(1);
    KieContainerResource container = containers.iterator().next();
    Assertions.assertThat(container.getScanner()).isEqualTo(kieScannerResource);
    KieScannerResource updatedKieScannerResource = new KieScannerResource(KieScannerStatus.DISPOSED);
    kieServer.updateScanner(containerId, updatedKieScannerResource);
    // create new state repository instance to avoid caching via 'knownStates'
    // this simulates the server restart (since the status is loaded from filesystem after restart)
    stateRepository = new KieServerStateFileRepository(REPOSITORY_DIR);
    state = stateRepository.load(KIE_SERVER_ID);
    containers = state.getContainers();
    Assertions.assertThat(containers).hasSize(1);
    container = containers.iterator().next();
    Assertions.assertThat(container.getScanner()).isEqualTo(updatedKieScannerResource);
    kieServer.disposeContainer(containerId);
}
Also used : KieServerState(org.kie.server.services.impl.storage.KieServerState) KieServerStateFileRepository(org.kie.server.services.impl.storage.file.KieServerStateFileRepository) KieScannerResource(org.kie.server.api.model.KieScannerResource) KieServerStateRepository(org.kie.server.services.impl.storage.KieServerStateRepository) Matchers.anyString(org.mockito.Matchers.anyString) ReleaseId(org.kie.server.api.model.ReleaseId) KieContainerResource(org.kie.server.api.model.KieContainerResource) Test(org.junit.Test)

Example 4 with KieServerStateRepository

use of org.kie.server.services.impl.storage.KieServerStateRepository in project droolsjbpm-integration by kiegroup.

the class KieServerStateTest method testLoadKieServerState.

@Test
public void testLoadKieServerState() {
    KieServerStateRepository repository = new KieServerStateFileRepository(REPOSITORY_DIR);
    String serverId = UUID.randomUUID().toString();
    KieServerState state = repository.load(serverId);
    Assert.assertNotNull(state);
    KieServerConfig config = state.getConfiguration();
    Assert.assertNotNull(config);
    Assert.assertNull(config.getConfigItemValue(KieServerConstants.CFG_PERSISTANCE_DIALECT));
    Assert.assertNull(config.getConfigItemValue(KieServerConstants.CFG_PERSISTANCE_DS));
    Assert.assertNull(config.getConfigItemValue(KieServerConstants.CFG_PERSISTANCE_TM));
    System.setProperty(KieServerConstants.CFG_PERSISTANCE_DIALECT, "org.hibernate.dialect.PostgreSQLDialect");
    System.setProperty(KieServerConstants.CFG_PERSISTANCE_DS, "jdbc/jbpm");
    System.setProperty(KieServerConstants.CFG_PERSISTANCE_TM, "JBossTS");
    repository.store(serverId, state);
    repository = new KieServerStateFileRepository(REPOSITORY_DIR);
    state = repository.load(serverId);
    Assert.assertNotNull(state);
    config = state.getConfiguration();
    Assert.assertNotNull(config);
    Assert.assertEquals("org.hibernate.dialect.PostgreSQLDialect", config.getConfigItemValue(KieServerConstants.CFG_PERSISTANCE_DIALECT));
    Assert.assertEquals("jdbc/jbpm", config.getConfigItemValue(KieServerConstants.CFG_PERSISTANCE_DS));
    Assert.assertEquals("JBossTS", config.getConfigItemValue(KieServerConstants.CFG_PERSISTANCE_TM));
}
Also used : KieServerState(org.kie.server.services.impl.storage.KieServerState) KieServerStateFileRepository(org.kie.server.services.impl.storage.file.KieServerStateFileRepository) KieServerStateRepository(org.kie.server.services.impl.storage.KieServerStateRepository) KieServerConfig(org.kie.server.api.model.KieServerConfig) Test(org.junit.Test)

Example 5 with KieServerStateRepository

use of org.kie.server.services.impl.storage.KieServerStateRepository in project droolsjbpm-integration by kiegroup.

the class KieServerStateFileInit method init.

public static File init() {
    String serverId = KieServerStateRepositoryUtils.getServerId();
    String serverRepoType = StartupStrategyProvider.get().getStrategy().getRepositoryType();
    KieServerEnvironment.setServerId(serverId);
    KieServerEnvironment.setServerName(serverId);
    KieServerStateRepository repository = null;
    File serverStateFile = KieServerStateRepositoryUtils.getStateFile();
    for (KieServerStateRepository repo : serverStateRepos) {
        if (repo.getClass().getSimpleName().equals(serverRepoType)) {
            repository = repo;
            break;
        }
    }
    if (repository == null) {
        repository = new KieServerStateFileRepository(KieServerStateRepositoryUtils.getFileRepoDir());
        logger.warn("ServiceLoader failed to initiate kie server repository: {}, fall back to KieServerStateFileRepository.", serverRepoType);
    } else {
        logger.info("Initialized with '{}' kie server repository", serverRepoType);
    }
    if (KIE_SERVER_STATE_REPO_TYPE_DEFAULT.equals(serverRepoType) && serverStateFile.exists()) {
        throw new IllegalStateException(String.format("%s already exists. %s should only be used for pre-bootstrapping creation of server state file.", serverStateFile, KieServerStateFileInit.class.getSimpleName()));
    }
    KieServerState serverState = new KieServerState();
    KieServerConfig config = new KieServerConfig();
    Properties properties = new Properties();
    properties.putAll(System.getProperties());
    properties.put(KIE_SERVER_STATE_REPO, KieServerStateRepositoryUtils.getFileRepoPath());
    properties.put(KIE_SERVER_ID, serverId);
    properties.put(KIE_SERVER_STATE_IMMUTABLE, "true");
    KieServerStateRepositoryUtils.populateWithProperties(config, properties);
    /**
     * This ephemeral system property, which is not persisted at KieServerState,
     * provide a signal to KieServerStateRepo to create a NEW server state within
     * KieServerStateFileInit process only.
     */
    System.setProperty(KIE_SERVER_STATE_IMMUTABLE_INIT, "true");
    serverState.setConfiguration(config);
    Set<KieContainerResource> containers = new LinkedHashSet<>();
    String serverContainerDeployment = KieServerStateRepositoryUtils.getValue(KIE_SERVER_CONTAINER_DEPLOYMENT, "KIE_SERVER_CONTAINER_DEPLOYMENT", null);
    Set<KieServerContainerDeployment> deployments = KieServerContainerDeployment.fromString(serverContainerDeployment);
    for (KieServerContainerDeployment deployment : deployments) {
        KieContainerResource container = new KieContainerResource(deployment.getContainerId(), new ReleaseId(deployment.getReleaseId()), KieContainerStatus.STARTED);
        container.setContainerAlias(deployment.getContainerAlias());
        containers.add(container);
    }
    serverState.setContainers(containers);
    repository.store(serverId, serverState);
    return serverStateFile;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) KieServerStateRepository(org.kie.server.services.impl.storage.KieServerStateRepository) KieServerConfig(org.kie.server.api.model.KieServerConfig) KieServerContainerDeployment(org.kie.server.services.impl.KieServerContainerDeployment) ReleaseId(org.kie.server.api.model.ReleaseId) Properties(java.util.Properties) KieServerState(org.kie.server.services.impl.storage.KieServerState) File(java.io.File) KieContainerResource(org.kie.server.api.model.KieContainerResource)

Aggregations

KieServerStateRepository (org.kie.server.services.impl.storage.KieServerStateRepository)9 KieServerState (org.kie.server.services.impl.storage.KieServerState)8 Test (org.junit.Test)6 KieServerConfig (org.kie.server.api.model.KieServerConfig)6 KieContainerResource (org.kie.server.api.model.KieContainerResource)3 KieServerStateFileRepository (org.kie.server.services.impl.storage.file.KieServerStateFileRepository)3 File (java.io.File)2 KieServerConfigItem (org.kie.server.api.model.KieServerConfigItem)2 ReleaseId (org.kie.server.api.model.ReleaseId)2 KieServerRegistryImpl (org.kie.server.services.impl.KieServerRegistryImpl)2 LinkedHashSet (java.util.LinkedHashSet)1 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Before (org.junit.Before)1 KieScannerResource (org.kie.server.api.model.KieScannerResource)1 KieServerInfo (org.kie.server.api.model.KieServerInfo)1 Message (org.kie.server.api.model.Message)1 KieServerController (org.kie.server.controller.api.KieServerController)1 ServerInstanceKeyList (org.kie.server.controller.api.model.runtime.ServerInstanceKeyList)1 ServerTemplate (org.kie.server.controller.api.model.spec.ServerTemplate)1