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 Jan 4, 2021
1 parent 819bbf5 commit aac0ef5
Show file tree
Hide file tree
Showing 13 changed files with 431 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public AsyncKieServerInstanceManager(final @EmbeddedController NotificationServi
this.executor = executorService;
}

@Override
public boolean isAsync() {
return true;
}
@Override
public List<Container> startScanner(final ServerTemplate serverTemplate,
final ContainerSpec containerSpec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.enterprise.context.Dependent;
Expand All @@ -34,6 +36,8 @@
import org.kie.server.api.model.KieContainerStatus;
import org.kie.server.api.model.Message;
import org.kie.server.api.model.Severity;
import org.kie.server.controller.api.model.events.ServerInstanceDeleted;
import org.kie.server.controller.api.model.events.ServerInstanceDisconnected;
import org.kie.server.controller.api.model.events.ServerInstanceUpdated;
import org.kie.server.controller.api.model.runtime.Container;
import org.kie.server.controller.api.model.spec.Capability;
Expand Down Expand Up @@ -77,6 +81,9 @@ public class ContainerPresenter {
private final Event<ServerTemplateSelected> serverTemplateSelectedEvent;
private final Event<NotificationEvent> notification;
private ContainerSpec containerSpec;
private Boolean isEmpty = null;
private Set<String> serverInstances = new HashSet<>();


@Inject
public ContainerPresenter(final Logger logger,
Expand Down Expand Up @@ -104,7 +111,7 @@ public ContainerPresenter(final Logger logger,
@PostConstruct
public void init() {
view.init(this);
view.setStatus(containerRemoteStatusPresenter.getView());
view.setStatus(containerStatusEmptyPresenter.getView());
view.setRulesConfig(containerRulesConfigPresenter.getView());
view.setProcessConfig(containerProcessConfigPresenter.getView());
}
Expand All @@ -119,43 +126,49 @@ protected void setContainerSpec(ContainerSpec containerSpec){

public void onRefresh(@Observes final RefreshRemoteServers refresh) {
if (refresh != null && refresh.getContainerSpecKey() != null) {
load(refresh.getContainerSpecKey());
runtimeManagementService.call((RemoteCallback<ContainerSpecData>) content -> {
logger.info("onRefresh {}", content);
checkNotNull("content", content);
updateContainerStatusView(content.getContainerSpec(), content.getContainers());
}).getContainersByContainerSpec(refresh.getContainerSpecKey().getServerTemplateKey().getId(),
refresh.getContainerSpecKey().getId());
} else {
logger.warn("Illegal event argument.");
logger.warn("Illegal event argument RefreshRemoteServers {}", refresh);
}
}

public void load(@Observes final ContainerSpecSelected containerSpecSelected) {
public void onSelectContainerSpec(@Observes final ContainerSpecSelected containerSpecSelected) {
if (containerSpecSelected != null &&
containerSpecSelected.getContainerSpecKey() != null) {
logger.info("onSelectContainerSpec {}", containerSpecSelected);
load(containerSpecSelected.getContainerSpecKey());
} else {
logger.warn("Illegal event argument.");
}
}

public void onInstanceUpdated(@Observes ServerInstanceUpdated instanceUpdated) {
if (instanceUpdated != null && containerSpec != null && containerSpec.getServerTemplateKey().getId().equals(instanceUpdated.getServerInstance().getServerTemplateId())) {
load(containerSpec);
logger.warn("Illegal event argument ContainerSpecSelected {}", containerSpecSelected);
}
}

public void loadContainers(@Observes final ContainerSpecData content) {
public void onChangeContainerSpecData(@Observes final ContainerSpecData content) {
if (content != null &&
content.getContainerSpec() != null &&
content.getContainers() != null &&
containerSpec != null &&
containerSpec.getId() != null &&
containerSpec.getId().equals(content.getContainerSpec().getId())) {
resetReleaseIdForFailedContainers(content.getContainers(), content.getContainerSpec());
setup(content.getContainerSpec(),
content.getContainers());
} else {
logger.warn("Illegal event argument.");
logger.info("onChangeContainers {}", content);
resetReleaseIdForFailedContainers(content.getContainerSpec(), content.getContainers());
setup(content.getContainerSpec(), content.getContainers());
}
}

private void resetReleaseIdForFailedContainers(Collection<Container> containers, ContainerSpec containerSpec) {
public void onUpdateContainerSpec(@Observes final ContainerUpdateEvent updateEvent) {
final ContainerRuntimeOperation runtimeOperation = updateEvent.getContainerRuntimeOperation();

if (updateEvent.getContainerSpec().equals(containerSpec) && runtimeOperation != STOP_CONTAINER) {
refresh();
}
}

private void resetReleaseIdForFailedContainers(ContainerSpec containerSpec, Collection<Container> containers) {
containers.forEach(container -> {
if (KieContainerStatus.FAILED == container.getStatus() || container.getResolvedReleasedId() == null) {
container.setResolvedReleasedId(containerSpec.getReleasedId());
Expand All @@ -168,74 +181,119 @@ private void resetReleaseIdForFailedContainers(Collection<Container> containers,
}
}


public void refreshOnContainerUpdateEvent(@Observes final ContainerUpdateEvent updateEvent) {
final ContainerRuntimeOperation runtimeOperation = updateEvent.getContainerRuntimeOperation();

if (updateEvent.getContainerSpec().equals(containerSpec) && runtimeOperation != STOP_CONTAINER) {
refresh();
}
}

public void refresh() {
load(containerSpec);
}

public void load(final ContainerSpecKey containerSpecKey) {
checkNotNull("containerSpecKey", containerSpecKey);
runtimeManagementService.call((RemoteCallback<ContainerSpecData>) content -> {
logger.debug("load {}", content);
checkNotNull("content", content);
setContainerSpec(content.getContainerSpec());
loadContainers(content);
if(content.getContainerSpec() != null && content.getContainers() != null) {
setup(content.getContainerSpec(), content.getContainers());
}
}).getContainersByContainerSpec(containerSpecKey.getServerTemplateKey().getId(),
containerSpecKey.getId());

}

private void setup(final ContainerSpec containerSpec,
final Collection<Container> containers) {
this.containerSpec = checkNotNull("containerSpec",
containerSpec);
updateView(containers);
this.containerSpec = checkNotNull("containerSpec", containerSpec);
updateView(containerSpec, containers);
}

private void updateView(final Collection<Container> containers) {
containerStatusEmptyPresenter.setup(containerSpec);
containerRemoteStatusPresenter.setup(containerSpec,
containers);
view.clear();
if (isEmpty(containers)) {
view.setStatus(containerStatusEmptyPresenter.getView());
} else {
view.setStatus(containerRemoteStatusPresenter.getView());
}
private void updateView(final ContainerSpec containerSpecKey, final Collection<Container> containers) {
containerStatusEmptyPresenter.setup(containerSpecKey);
containerRemoteStatusPresenter.setup(containerSpecKey, containers);


view.setContainerName(containerSpec.getContainerName());
view.setGroupIp(containerSpec.getReleasedId().getGroupId());
view.setArtifactId(containerSpec.getReleasedId().getArtifactId());
containerRulesConfigPresenter.setVersion(containerSpec.getReleasedId().getVersion());
updateContainerView(containerSpecKey);
updateContainerStatusView(containerSpecKey, containers);

}

private void updateContainerView(ContainerSpec updatedContainerSpec) {
view.setContainerName(updatedContainerSpec.getContainerName());
view.setGroupIp(updatedContainerSpec.getReleasedId().getGroupId());
view.setArtifactId(updatedContainerSpec.getReleasedId().getArtifactId());
containerRulesConfigPresenter.setVersion(updatedContainerSpec.getReleasedId().getVersion());
containerProcessConfigPresenter.disable();

updateStatus(containerSpec.getStatus() != null ? containerSpec.getStatus() : KieContainerStatus.STOPPED);
updateStatus(updatedContainerSpec.getStatus() != null ? updatedContainerSpec.getStatus() : KieContainerStatus.STOPPED);

for (Map.Entry<Capability, ContainerConfig> entry : containerSpec.getConfigs().entrySet()) {
for (Map.Entry<Capability, ContainerConfig> entry : updatedContainerSpec.getConfigs().entrySet()) {
switch (entry.getKey()) {
case RULE:
setupRuleConfig((RuleConfig) entry.getValue());
break;
case PROCESS:
setupProcessConfig((ProcessConfig) entry.getValue());
break;
case PLANNING:
// do nothing
break;
}
}
}

public void onServerInstanceUpdated( @Observes final ServerInstanceUpdated serverInstanceUpdated ) {
if(this.containerSpec == null) {
return;
}
if ( serverInstanceUpdated != null && serverInstanceUpdated.getServerInstance() != null ) {
serverInstanceUpdated.getServerInstance().getContainers().stream()
.filter(c -> !KieContainerStatus.STOPPED.equals(c.getStatus()))
.filter(c -> this.containerSpec.getId().equals(c.getContainerSpecId()))
.filter(c -> this.containerSpec.getServerTemplateKey().getId().equals(c.getServerTemplateId()))
.forEach(c -> serverInstances.add(c.getServerInstanceId()));
updateContainerStatusView();
} else {
logger.warn( "Illegal event argument ServerInstanceUpdated {}", serverInstanceUpdated);
}
}

private boolean isEmpty(final Collection<Container> containers) {
for (final Container container : containers) {
if (!container.getStatus().equals(KieContainerStatus.STOPPED)) {
return false;
public void onServerInstanceDisconnect( @Observes final ServerInstanceDisconnected serverInstanceDisconnected ) {
if ( serverInstanceDisconnected != null &&
serverInstanceDisconnected.getServerInstanceId() != null ) {
if(serverInstances.remove(serverInstanceDisconnected.getServerInstanceId())) {
updateContainerStatusView();
}
} else {
logger.warn( "Illegal event argument ServerInstanceDisconnected {}", serverInstanceDisconnected);
}
}

public void onServerInstanceDelete( @Observes final ServerInstanceDeleted serverInstanceDeleted ) {
if ( serverInstanceDeleted != null &&
serverInstanceDeleted.getServerInstanceId() != null ) {

if(serverInstances.remove(serverInstanceDeleted.getServerInstanceId())) {
updateContainerStatusView();
}
} else {
logger.warn( "Illegal event argument ServerInstanceDeleted {}", serverInstanceDeleted);
}
}

private void updateContainerStatusView(ContainerSpecKey containerSpecUpdated, Collection<Container> containers) {
logger.debug( "updateContainerStatusView {} with number of containers {}", containerSpecUpdated, containers.size());
serverInstances.clear();
containers.stream().filter(c -> !KieContainerStatus.STOPPED.equals(c.getStatus())).forEach(c -> serverInstances.add(c.getServerInstanceId()));
updateContainerStatusView();
}

private void updateContainerStatusView() {
logger.debug( "updateContainerStatusView with number of containers {}", serverInstances);
// switch from empty -> not empty. Avoid the blink
if ((isEmpty== null && serverInstances.isEmpty()) || (serverInstances.isEmpty() && !isEmpty)) {
view.setStatus(containerStatusEmptyPresenter.getView());
isEmpty = true;
} else if ((isEmpty == null && !serverInstances.isEmpty()) || (!serverInstances.isEmpty() && isEmpty)) {
view.setStatus(containerRemoteStatusPresenter.getView());
isEmpty = false;
}
return true;
}

protected void updateStatus(final KieContainerStatus status) {
Expand Down
Loading

0 comments on commit aac0ef5

Please sign in to comment.