Skip to content

Commit

Permalink
[JBPM-9464] Kie server notification causes bad status in BC
Browse files Browse the repository at this point in the history
  • Loading branch information
elguardian committed Nov 30, 2020
1 parent b3efee1 commit df5bb35
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class KieServerConstants {
public static final String KIE_SERVER_IMAGESERVICE_MAX_NODES = "org.kie.server.service.image.max_nodes";
public static final String KIE_SERVER_REST_MODE_READONLY = "org.kie.server.rest.mode.readonly";
public static final String KIE_SERVER_NOTIFY_UPDATES_TO_CONTROLLERS = "org.kie.server.update.notifications.rest.enabled";
public static final String KIE_SERVER_NOTIFY_UPDATES_TO_CONTROLLERS_DELAY = "org.kie.server.update.notifications.delay";

// configuration parameters
public static final String CFG_PERSISTANCE_DS = "org.kie.server.persistence.ds";
public static final String CFG_PERSISTANCE_TM = "org.kie.server.persistence.tm";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Container( final String containerSpecId,
this.serverInstanceId = serverInstanceKey.getServerInstanceId();
this.messages.addAll( messages );
this.resolvedReleasedId = resolvedReleasedId;

setUrl(url);
}

public String getServerInstanceId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,7 @@ public KieServerSetup update(KieServerStateInfo kieServerStateInfo) {

// we update the server instance with the containers
List<Container> containerList = new ArrayList<Container>();
List<KieContainerStatus> invalidStatus = Collections.singletonList(KieContainerStatus.STOPPED);
for(ContainerSpec containerSpec : serverTemplate.getContainersSpec()) {
if(invalidStatus.contains(containerSpec.getStatus())) {
continue;
}
Container container = new Container(containerSpec.getId(),
containerSpec.getContainerName(),
serverInstanceKey,
Expand All @@ -189,7 +185,6 @@ public KieServerSetup update(KieServerStateInfo kieServerStateInfo) {

// we update and notify
notificationService.notify(new ServerInstanceUpdated(serverInstance));
notificationService.notify(new ServerTemplateUpdated(serverTemplate));

for(ContainerSpec currentSpec : serverTemplate.getContainersSpec()) {
List<Container> specContainerList = new ArrayList<Container>();
Expand All @@ -206,7 +201,6 @@ public KieServerSetup update(KieServerStateInfo kieServerStateInfo) {
notificationService.notify(serverTemplate, currentSpec, specContainerList);
}


return toKieServerSetup(serverTemplate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.kie.server.controller.impl;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.ServiceLoader;

Expand Down Expand Up @@ -63,6 +64,10 @@ public static KieServerInstanceManager getInstance() {
return INSTANCE;
}

public boolean isAsync() {
return false;
}

public List<Container> startScanner(ServerTemplate serverTemplate,
final ContainerSpec containerSpec,
final long interval) {
Expand Down Expand Up @@ -375,10 +380,16 @@ public List<Container> getContainers(final ServerTemplate serverTemplate,

RemoteKieServerOperation<Void> getContainersRemoteOperation(final ServerTemplate serverTemplate,
final ContainerSpec containerSpec) {

return new RemoteKieServerOperation<Void>() {
@Override
public Void doOperation(KieServicesClient client,
Container container) {

EnumSet<KieContainerStatus> set = EnumSet.of(KieContainerStatus.STARTED, KieContainerStatus.CREATING);
if(!set.contains(container.getStatus())) {
return null;
}

final ServiceResponse<KieContainerResource> response = client.getContainerInfo(containerSpec.getId());
final KieContainerResource containerResource = response.getResult();
Expand Down Expand Up @@ -560,6 +571,8 @@ protected void collectContainerInfo(ContainerSpec containerSpec,
container.setResolvedReleasedId(containerResource.getResolvedReleaseId() == null ? containerResource.getReleaseId() : containerResource.getResolvedReleaseId());
container.setMessages(containerResource.getMessages());
container.setStatus(containerResource.getStatus());
container.setContainerName(containerResource.getContainerAlias());
container.setMessages(containerResource.getMessages());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ public synchronized void saveContainerSpec(String serverTemplateId,

templateStorage.update(serverTemplate);


notificationService.notify(new ServerTemplateUpdated(serverTemplate));

if (containerSpec.getStatus().equals(KieContainerStatus.STARTED)) {
List<Container> containers = kieServerInstanceManager.startContainer(serverTemplate, containerSpec);
notificationService.notify(serverTemplate, containerSpec, containers);
if(!kieServerInstanceManager.isAsync()) {
notificationService.notify(serverTemplate, containerSpec, containers);
}
}
}

Expand Down Expand Up @@ -120,7 +123,9 @@ public synchronized void updateContainerSpec(String serverTemplateId, String con
// in case container was started before it was update or update comes with status started update container in running servers
if (currentVersion.getStatus().equals(KieContainerStatus.STARTED) || containerSpec.getStatus().equals(KieContainerStatus.STARTED)) {
List<Container> containers = kieServerInstanceManager.upgradeAndStartContainer(serverTemplate, containerSpec, resetBeforeUpdate);
notificationService.notify(serverTemplate, containerSpec, containers);
if(!kieServerInstanceManager.isAsync()) {
notificationService.notify(serverTemplate, containerSpec, containers);
}
}
}

Expand All @@ -140,7 +145,9 @@ public synchronized void saveServerTemplate(ServerTemplate serverTemplate) {
for (ContainerSpec containerSpec : containerSpecs) {
if (containerSpec.getStatus().equals(KieContainerStatus.STARTED)) {
List<Container> containers = kieServerInstanceManager.startContainer(serverTemplate, containerSpec);
notificationService.notify(serverTemplate, containerSpec, containers);
if(!kieServerInstanceManager.isAsync()) {
notificationService.notify(serverTemplate, containerSpec, containers);
}
}
}
}
Expand Down Expand Up @@ -404,7 +411,9 @@ public synchronized void startContainer(ContainerSpecKey containerSpecKey) {

List<Container> containers = kieServerInstanceManager.startContainer(serverTemplate, containerSpec);

notificationService.notify(serverTemplate, containerSpec, containers);
if(!kieServerInstanceManager.isAsync()) {
notificationService.notify(serverTemplate, containerSpec, containers);
}
}

@Override
Expand All @@ -425,7 +434,9 @@ public synchronized void stopContainer(ContainerSpecKey containerSpecKey) {

List<Container> containers = kieServerInstanceManager.stopContainer(serverTemplate, containerSpec);

notificationService.notify(serverTemplate, containerSpec, containers);
if(!kieServerInstanceManager.isAsync()) {
notificationService.notify(serverTemplate, containerSpec, containers);
}
}

@Override
Expand All @@ -450,7 +461,9 @@ public synchronized void activateContainer(ContainerSpecKey containerSpecKey) {

List<Container> containers = kieServerInstanceManager.activateContainer(serverTemplate, containerSpec);

notificationService.notify(serverTemplate, containerSpec, containers);
if(!kieServerInstanceManager.isAsync()) {
notificationService.notify(serverTemplate, containerSpec, containers);
}
}

@Override
Expand All @@ -475,7 +488,9 @@ public synchronized void deactivateContainer(ContainerSpecKey containerSpecKey)

List<Container> containers = kieServerInstanceManager.deactivateContainer(serverTemplate, containerSpec);

notificationService.notify(serverTemplate, containerSpec, containers);
if(!kieServerInstanceManager.isAsync()) {
notificationService.notify(serverTemplate, containerSpec, containers);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public ServiceResponse<KieContainerResource> createContainer(String containerId,
currentState.getContainers().add(container);
});
eventSupport.fireAfterContainerStarted(this, ci);
getDefaultController().update(getInternalServerState());
notifyStatusToControllers();
return new ServiceResponse<KieContainerResource>(ServiceResponse.ResponseType.SUCCESS, "Container " + containerId + " successfully deployed with module " + releaseId + ".", ci.getResource());
} else {
ci.getResource().setStatus(KieContainerStatus.FAILED);
Expand Down Expand Up @@ -432,7 +432,7 @@ public ServiceResponse<KieContainerResource> activateContainer(String containerI
});

eventSupport.fireAfterContainerActivated(this, kci);
getDefaultController().update(getInternalServerState());
notifyStatusToControllers();
messages.add(new Message(Severity.INFO, "Container " + containerId + " activated successfully."));
return new ServiceResponse<KieContainerResource>(ServiceResponse.ResponseType.SUCCESS, "Container " + containerId + " activated successfully.", kci.getResource());
}
Expand Down Expand Up @@ -481,7 +481,7 @@ public ServiceResponse<KieContainerResource> deactivateContainer(String containe
});

eventSupport.fireAfterContainerDeactivated(this, kci);
getDefaultController().update(getInternalServerState());
notifyStatusToControllers();
messages.add(new Message(Severity.INFO, "Container " + containerId + " deactivated successfully."));
return new ServiceResponse<KieContainerResource>(ServiceResponse.ResponseType.SUCCESS, "Container " + containerId + " deactivated successfully.", kci.getResource());
}
Expand Down Expand Up @@ -614,7 +614,7 @@ public ServiceResponse<Void> disposeContainer(String containerId) {
messages.add(new Message(Severity.INFO, "Container " + containerId + " successfully stopped."));

eventSupport.fireAfterContainerStopped(this, kci);
getDefaultController().update(getInternalServerState());
notifyStatusToControllers();
return new ServiceResponse<Void>(ServiceResponse.ResponseType.SUCCESS, "Container " + containerId + " successfully disposed.");
} else {
messages.add(new Message(Severity.INFO, "Container " + containerId + " was not instantiated."));
Expand Down Expand Up @@ -678,7 +678,7 @@ public ServiceResponse<KieScannerResource> updateScanner(String id, KieScannerRe
}
});
});
getDefaultController().update(getInternalServerState());
notifyStatusToControllers();
return scannerResponse;
}
} else {
Expand Down Expand Up @@ -955,7 +955,7 @@ public ServiceResponse<ReleaseId> updateContainerReleaseId(String containerId, R

logger.info("Container {} successfully updated to release id {}", containerId, releaseId);
ks.getRepository().removeKieModule(originalReleaseId);
getDefaultController().update(getInternalServerState());
notifyStatusToControllers();
messages.add(new Message(Severity.INFO, "Release id successfully updated for container " + containerId));
return new ServiceResponse<ReleaseId>(ServiceResponse.ResponseType.SUCCESS, "Release id successfully updated.", kci.getResource().getReleaseId());
} else {
Expand Down Expand Up @@ -1108,6 +1108,18 @@ protected KieServerController getController() {
return controller;
}

protected void notifyStatusToControllers() {
new Thread(() -> {
try {
Thread.sleep(Long.getLong(KieServerConstants.KIE_SERVER_NOTIFY_UPDATES_TO_CONTROLLERS_DELAY, 1000L));
} catch (InterruptedException e) {
// do nothing
} finally {
getDefaultController().update(getInternalServerState());
}
}).start();
}

protected KieServerController getDefaultController() {
return new DefaultRestControllerImpl(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,21 @@ public KieServerSetup connect(KieServerInfo serverInfo) {

KieServerConfig config = currentState.getConfiguration();
if (controllers != null && !controllers.isEmpty()) {
KieServerSetup kieServerSetup = null;
for (String controllerUrl : controllers) {

if (controllerUrl != null && !controllerUrl.isEmpty()) {
KieServerSetup kieServerSetup = connectToSingleController(serverInfo, config, controllerUrl);
if (kieServerSetup != null && kieServerSetup.hasNoErrors()) {
return kieServerSetup;
KieServerSetup kieServerSetupController = connectToSingleController(serverInfo, config, controllerUrl);
if (kieServerSetup == null && kieServerSetupController != null && kieServerSetupController.hasNoErrors()) {
kieServerSetup = kieServerSetupController;
}
}
}

if(kieServerSetup != null) {
return kieServerSetup;
}

throw new KieControllerNotConnectedException("Unable to connect to any controller");
} else {
throw new KieControllerNotDefinedException("Unable to connect to any controller");
Expand All @@ -170,13 +175,8 @@ public void disconnect(KieServerInfo serverInfo) {
KieServerConfig config = currentState.getConfiguration();

for (String controllerUrl : controllers ) {

if (controllerUrl != null && !controllerUrl.isEmpty()) {

boolean disconnected = disconnectFromSingleController(serverInfo, config, controllerUrl);
if (disconnected) {
break;
}
disconnectFromSingleController(serverInfo, config, controllerUrl);
}
}
}
Expand Down Expand Up @@ -212,9 +212,7 @@ public KieServerSetup update(KieServerStateInfo serverTemplateUpdate) {
}
}

} else {
logger.warn("No controllers found to update to new status {}", serverTemplateUpdate);
}
}
// we return one of them coming fron the controller
return kieServerSetup;
}
Expand Down

0 comments on commit df5bb35

Please sign in to comment.