Search in sources :

Example 6 with KieServerExtension

use of org.kie.server.services.api.KieServerExtension in project droolsjbpm-integration by kiegroup.

the class AbstractKieServerImplTest method testHealthCheckFailedExtension.

@Test
public void testHealthCheckFailedExtension() {
    extensions.add(new KieServerExtension() {

        @Override
        public List<Message> healthCheck(boolean report) {
            List<Message> messages = KieServerExtension.super.healthCheck(report);
            messages.add(new Message(Severity.ERROR, "TEST extension is unhealthy"));
            return messages;
        }

        @Override
        public void updateContainer(String id, KieContainerInstance kieContainerInstance, Map<String, Object> parameters) {
        }

        @Override
        public boolean isUpdateContainerAllowed(String id, KieContainerInstance kieContainerInstance, Map<String, Object> parameters) {
            return false;
        }

        @Override
        public boolean isInitialized() {
            return true;
        }

        @Override
        public boolean isActive() {
            return true;
        }

        @Override
        public void init(KieServerImpl kieServer, KieServerRegistry registry) {
        }

        @Override
        public Integer getStartOrder() {
            return 10;
        }

        @Override
        public List<Object> getServices() {
            return null;
        }

        @Override
        public String getImplementedCapability() {
            return "TEST";
        }

        @Override
        public String getExtensionName() {
            return "TEST";
        }

        @Override
        public <T> T getAppComponents(Class<T> serviceType) {
            return null;
        }

        @Override
        public List<Object> getAppComponents(SupportedTransports type) {
            return null;
        }

        @Override
        public void disposeContainer(String id, KieContainerInstance kieContainerInstance, Map<String, Object> parameters) {
        }

        @Override
        public void destroy(KieServerImpl kieServer, KieServerRegistry registry) {
        }

        @Override
        public void createContainer(String id, KieContainerInstance kieContainerInstance, Map<String, Object> parameters) {
        }
    });
    kieServer.init();
    List<Message> healthMessages = kieServer.healthCheck(false);
    assertEquals(healthMessages.size(), 1);
    Message failedContainer = healthMessages.get(0);
    assertEquals(Severity.ERROR, failedContainer.getSeverity());
    assertEquals(1, failedContainer.getMessages().size());
    assertEquals("TEST extension is unhealthy", failedContainer.getMessages().iterator().next());
}
Also used : Message(org.kie.server.api.model.Message) KieServerExtension(org.kie.server.services.api.KieServerExtension) Matchers.anyString(org.mockito.Matchers.anyString) KieContainerInstance(org.kie.server.services.api.KieContainerInstance) SupportedTransports(org.kie.server.services.api.SupportedTransports) KieServerRegistry(org.kie.server.services.api.KieServerRegistry) KieContainerResourceList(org.kie.server.api.model.KieContainerResourceList) List(java.util.List) ArrayList(java.util.ArrayList) ServiceResponsesList(org.kie.server.api.model.ServiceResponsesList) Test(org.junit.Test)

Example 7 with KieServerExtension

use of org.kie.server.services.api.KieServerExtension in project droolsjbpm-integration by kiegroup.

the class AbstractKieServerImplTest method testUpdateContainerWithExtensionNotAllowing.

@Test
public void testUpdateContainerWithExtensionNotAllowing() {
    KieServerExtension extension = mock(KieServerExtension.class);
    when(extension.isUpdateContainerAllowed(any(), any(), any())).thenReturn(false);
    extensions.add(extension);
    String containerId = "container-to-update";
    startContainerToUpdate(containerId, getVersion(mode));
    ServiceResponse<ReleaseId> updateResponse = kieServer.updateContainerReleaseId(containerId, new ReleaseId(this.releaseId), true);
    Assertions.assertThat(updateResponse.getType()).isEqualTo(ServiceResponse.ResponseType.FAILURE);
    verify(extension).isUpdateContainerAllowed(anyString(), any(), any());
    verify(extension, never()).updateContainer(any(), any(), any());
    kieServer.disposeContainer(containerId);
}
Also used : KieServerExtension(org.kie.server.services.api.KieServerExtension) Matchers.anyString(org.mockito.Matchers.anyString) ReleaseId(org.kie.server.api.model.ReleaseId) Test(org.junit.Test)

Example 8 with KieServerExtension

use of org.kie.server.services.api.KieServerExtension in project droolsjbpm-integration by kiegroup.

the class AbstractKieServerImplTest method testUpdateContainer.

@Test
public void testUpdateContainer() {
    KieServerExtension extension = mock(KieServerExtension.class);
    when(extension.isUpdateContainerAllowed(any(), any(), any())).thenReturn(true);
    extensions.add(extension);
    String containerId = "container-to-update";
    startContainerToUpdate(containerId, getVersion(mode));
    ServiceResponse<ReleaseId> updateResponse = kieServer.updateContainerReleaseId(containerId, new ReleaseId(releaseId), true);
    Assertions.assertThat(updateResponse.getType()).isEqualTo(ServiceResponse.ResponseType.SUCCESS);
    verify(extension).isUpdateContainerAllowed(anyString(), any(), any());
    verify(extension).updateContainer(any(), any(), any());
    kieServer.disposeContainer(containerId);
}
Also used : KieServerExtension(org.kie.server.services.api.KieServerExtension) Matchers.anyString(org.mockito.Matchers.anyString) ReleaseId(org.kie.server.api.model.ReleaseId) Test(org.junit.Test)

Example 9 with KieServerExtension

use of org.kie.server.services.api.KieServerExtension in project droolsjbpm-integration by kiegroup.

the class KieServerImplDevelopmentModeTest method testUpdateContainer.

private void testUpdateContainer(String version) {
    KieServerExtension extension = mock(KieServerExtension.class);
    when(extension.isUpdateContainerAllowed(any(), any(), any())).thenReturn(true);
    extensions.add(extension);
    String containerId = "container-to-update";
    startContainerToUpdate(containerId, version);
    ReleaseId updateReleaseId = new ReleaseId(GROUP_ID, containerId, version);
    ServiceResponse<ReleaseId> updateResponse = kieServer.updateContainerReleaseId(containerId, updateReleaseId, true);
    Assertions.assertThat(updateResponse.getType()).isEqualTo(ServiceResponse.ResponseType.SUCCESS);
    verify(extension).isUpdateContainerAllowed(anyString(), any(), any());
    verify(extension).prepareContainerUpdate(anyString(), any(), any());
    verify(extension).updateContainer(any(), any(), any());
    kieServer.disposeContainer(containerId);
}
Also used : KieServerExtension(org.kie.server.services.api.KieServerExtension) Matchers.anyString(org.mockito.Matchers.anyString) ReleaseId(org.kie.server.api.model.ReleaseId)

Example 10 with KieServerExtension

use of org.kie.server.services.api.KieServerExtension in project droolsjbpm-integration by kiegroup.

the class KieServerImpl method disposeContainer.

public ServiceResponse<Void> disposeContainer(String containerId, Boolean abortInstances) {
    List<Message> messages = new CopyOnWriteArrayList<Message>();
    try {
        KieContainerInstanceImpl kci = context.unregisterContainer(containerId);
        if (kci != null) {
            synchronized (kci) {
                eventSupport.fireBeforeContainerStopped(this, kci);
                // just in case
                kci.setStatus(KieContainerStatus.DISPOSING);
                if (kci.getKieContainer() != null) {
                    org.kie.api.builder.ReleaseId releaseId = kci.getKieContainer().getReleaseId();
                    List<KieServerExtension> disposedExtensions = new ArrayList<KieServerExtension>();
                    try {
                        // first attempt to dispose container on all extensions
                        logger.debug("Container {} (for release id {}) shutdown: In Progress", containerId, kci.getResource().getReleaseId());
                        // process server extensions
                        List<KieServerExtension> extensions = context.getServerExtensions();
                        for (KieServerExtension extension : extensions) {
                            extension.disposeContainer(containerId, kci, singletonMap(KieServerConstants.IS_DISPOSE_CONTAINER_PARAM, abortInstances));
                            logger.debug("Container {} (for release id {}) {} shutdown: DONE", containerId, kci.getResource().getReleaseId(), extension);
                            disposedExtensions.add(extension);
                        }
                    } catch (Exception e) {
                        logger.warn("Dispose of container {} failed, putting it back to started state by recreating container on {}", containerId, disposedExtensions);
                        // since the dispose fail rollback must take place to put it back to running state
                        KieModuleMetaData metadata = buildKieModuleMetadata(kci.getKieContainer().getReleaseId());
                        Map<String, Object> parameters = getContainerParameters(metadata, messages);
                        for (KieServerExtension extension : disposedExtensions) {
                            extension.createContainer(containerId, kci, parameters);
                            logger.debug("Container {} (for release id {}) {} restart: DONE", containerId, kci.getResource().getReleaseId(), extension);
                        }
                        kci.setStatus(KieContainerStatus.STARTED);
                        context.registerContainer(containerId, kci);
                        logger.info("Container {} (for release id {}) STARTED after failed dispose", containerId, kci.getResource().getReleaseId());
                        messages.add(new Message(Severity.WARN, "Error disposing container '" + containerId + "' due to " + e.getMessage() + ", container is running"));
                        return new ServiceResponse<Void>(ResponseType.FAILURE, "Container " + containerId + " failed to dispose, exception was raised: " + e.getClass().getName() + ": " + e.getMessage());
                    }
                    if (kci.getScanner() != null && kci.getScanner().getStatus() != Status.SHUTDOWN) {
                        kci.stopScanner();
                    }
                    InternalKieContainer kieContainer = kci.getKieContainer();
                    // helps reduce concurrent access issues
                    kci.setKieContainer(null);
                    kci.disposeMarshallers();
                    // this may fail, but we already removed the container from the registry
                    kieContainer.dispose();
                    ks.getRepository().removeKieModule(releaseId);
                    logger.info("Container {} (for release id {}) successfully stopped", containerId, kci.getResource().getReleaseId());
                    // store the current state of the server
                    storeServerState(currentState -> {
                        List<KieContainerResource> containers = new ArrayList<>();
                        currentState.getContainers().forEach(containerResource -> {
                            if (!containerId.equals(containerResource.getContainerId())) {
                                containers.add(containerResource);
                            }
                        });
                        currentState.setContainers(new HashSet<KieContainerResource>(containers));
                    });
                    messages.add(new Message(Severity.INFO, "Container " + containerId + " successfully stopped."));
                    eventSupport.fireAfterContainerStopped(this, kci);
                    notifyStatusToControllers();
                    return new ServiceResponse<Void>(ServiceResponse.ResponseType.SUCCESS, "Container " + containerId + " successfully disposed.");
                } else {
                    messages.add(new Message(Severity.INFO, "Container " + containerId + " was not instantiated."));
                    return new ServiceResponse<Void>(ServiceResponse.ResponseType.SUCCESS, "Container " + containerId + " was not instantiated.");
                }
            }
        } else {
            messages.add(new Message(Severity.INFO, "Container " + containerId + " was not instantiated."));
            return new ServiceResponse<Void>(ServiceResponse.ResponseType.SUCCESS, "Container " + containerId + " was not instantiated.");
        }
    } catch (Exception e) {
        messages.add(new Message(Severity.ERROR, "Error disposing container '" + containerId + "' due to " + e.getMessage()));
        logger.error("Error disposing Container '" + containerId + "'", e);
        return new ServiceResponse<Void>(ServiceResponse.ResponseType.FAILURE, "Error disposing container " + containerId + ": " + e.getClass().getName() + ": " + e.getMessage());
    } finally {
        this.containerMessages.put(containerId, messages);
    }
}
Also used : Message(org.kie.server.api.model.Message) KieServerExtension(org.kie.server.services.api.KieServerExtension) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) KieModuleMetaData(org.kie.scanner.KieModuleMetaData) ServiceResponse(org.kie.server.api.model.ServiceResponse) InternalKieContainer(org.drools.core.impl.InternalKieContainer) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) KieContainerResource(org.kie.server.api.model.KieContainerResource) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

KieServerExtension (org.kie.server.services.api.KieServerExtension)43 Message (org.kie.server.api.model.Message)10 ArrayList (java.util.ArrayList)9 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 ReleaseId (org.kie.server.api.model.ReleaseId)8 Test (org.junit.Test)6 KieModuleMetaData (org.kie.scanner.KieModuleMetaData)6 KieContainerResource (org.kie.server.api.model.KieContainerResource)6 ServiceResponse (org.kie.server.api.model.ServiceResponse)6 Matchers.anyString (org.mockito.Matchers.anyString)6 JbpmKieServerExtension (org.kie.server.services.jbpm.JbpmKieServerExtension)5 DeploymentService (org.jbpm.services.api.DeploymentService)4 ProcessService (org.jbpm.services.api.ProcessService)4 KieContainerInstance (org.kie.server.services.api.KieContainerInstance)4 KieServerRegistry (org.kie.server.services.api.KieServerRegistry)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ExecutorServiceImpl (org.jbpm.executor.impl.ExecutorServiceImpl)3 RuntimeDataService (org.jbpm.services.api.RuntimeDataService)3