Skip to content

Commit

Permalink
DROOLS-1235 MBean naming rework (apache#866)
Browse files Browse the repository at this point in the history
* Fixing kie-container monitoring

* DROOLS-1235 MBean naming rework

* give KieContainer an optional user defined name
* KieContainer MBean and various refactorings.
* jargon: containerId 
* jargon: configuredReleaseId, resolvedReleaseId
* unit testing
* assuming the containerId can be "recycled" on dispose.
* introducing test for KieServices API with containerId.
* fixing other tests.
* avoid using lock and leverage ConcurrentMap without java8
* fixing objectname conventions.
  • Loading branch information
tarilabs authored and mariofusco committed Aug 22, 2016
1 parent 6aeeff8 commit b274c62
Show file tree
Hide file tree
Showing 13 changed files with 606 additions and 110 deletions.
Expand Up @@ -49,6 +49,15 @@ public interface InternalKieContainer extends KieContainer {
*/
void dispose();

/**
* Internal use: returns the RelaseId configured while creating the Kiecontainer,
* or alternatively if the RelaseId was NOT configured while creating the Kiecontainer,
* returns the the ReleaseId of the KieModule wrapped by this KieContainer.
* Additionally, please notice this will always gets updated to the parameter passed as updateToVersion(ReleaseId).
* @see org.drools.compiler.kie.builder.impl.KieContainerImpl#KieContainerImpl(String, KieProject, org.kie.api.builder.KieRepository, ReleaseId)
* @see org.kie.api.runtime.KieContainer#getReleaseId()
* @see org.kie.api.runtime.KieContainer#updateToVersion(ReleaseId)
*/
ReleaseId getContainerReleaseId();

long getCreationTimestamp();
Expand All @@ -62,4 +71,22 @@ public interface InternalKieContainer extends KieContainer {
*/
KieModule getMainKieModule();

/**
* Returns the unique ID assigned to the container.
* @return the unique ID assigned to the container.
*/
String getContainerId();

/**
* Returns the RelaseId configured while creating the Kiecontainer.
* @return the RelaseId configured while creating the Kiecontainer.
*/
ReleaseId getConfiguredReleaseId();

/**
* Returns the actual resolved ReleaseId.
* @return the actual resolved ReleaseId.
*/
ReleaseId getResolvedReleaseId();

}
Expand Up @@ -17,7 +17,14 @@

import org.drools.compiler.kie.builder.impl.event.KieServicesEventListerner;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;

public interface InternalKieServices extends KieServices {
void registerListener(KieServicesEventListerner listener);

/**
* Clear the containerId reference from the internal registry hold by the KieServices.
* Epsecially helpful to avoid leaking reference on container dispose(), to inadvertently keep a reference in the internal registry which would never be GC.
*/
void clearRefToContainerId(String containerId, KieContainer containerRef);
}
Expand Up @@ -15,19 +15,40 @@

package org.drools.compiler.kie.builder.impl;

import static org.drools.compiler.kie.builder.impl.AbstractKieModule.buildKnowledgePackages;
import static org.drools.compiler.kie.builder.impl.KieBuilderImpl.filterFileInKBase;
import static org.drools.compiler.kie.util.CDIHelper.wireListnersAndWIHs;
import static org.drools.core.util.ClassUtils.convertResourceToClassName;
import static org.drools.core.util.Drools.isJndiAvailable;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import javax.management.ObjectName;

import org.drools.compiler.builder.impl.KnowledgeBuilderImpl;
import org.drools.compiler.compiler.PackageBuilderErrors;
import org.drools.compiler.kie.util.ChangeSetBuilder;
import org.drools.compiler.kie.util.KieJarChangeSet;
import org.drools.compiler.kproject.models.KieBaseModelImpl;
import org.drools.compiler.kproject.models.KieSessionModelImpl;
import org.drools.compiler.management.KieContainerMonitor;
import org.drools.core.RuleBaseConfiguration;
import org.drools.core.common.InternalWorkingMemory;
import org.drools.core.common.ProjectClassLoader;
import org.drools.core.definitions.impl.KnowledgePackageImpl;
import org.drools.core.definitions.rule.impl.RuleImpl;
import org.drools.core.impl.InternalKnowledgeBase;
import org.drools.core.impl.StatefulKnowledgeSessionImpl;
import org.drools.core.management.DroolsManagementAgent;
import org.kie.api.KieBase;
import org.kie.api.KieBaseConfiguration;
import org.kie.api.KieServices;
Expand All @@ -40,6 +61,7 @@
import org.kie.api.builder.model.KieBaseModel;
import org.kie.api.builder.model.KieSessionModel;
import org.kie.api.conf.EventProcessingOption;
import org.kie.api.conf.MBeansOption;
import org.kie.api.event.KieRuntimeEventManager;
import org.kie.api.io.Resource;
import org.kie.api.io.ResourceType;
Expand All @@ -60,22 +82,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import static org.drools.compiler.kie.builder.impl.AbstractKieModule.buildKnowledgePackages;
import static org.drools.compiler.kie.builder.impl.KieBuilderImpl.filterFileInKBase;
import static org.drools.compiler.kie.util.CDIHelper.wireListnersAndWIHs;
import static org.drools.core.util.ClassUtils.convertResourceToClassName;
import static org.drools.core.util.Drools.isJndiAvailable;

public class KieContainerImpl
implements
InternalKieContainer {
Expand All @@ -91,24 +97,82 @@ public class KieContainerImpl

private final KieRepository kr;

private ReleaseId configuredReleaseId;
private ReleaseId containerReleaseId;

private final String containerId;

public KieModule getMainKieModule() {
return kr.getKieModule(getReleaseId());
}


/**
* Please note: the recommended way of getting a KieContainer is relying on {@link org.kie.api.KieServices KieServices} API,
* for example: {@link org.kie.api.KieServices#newKieContainer(ReleaseId) KieServices.newKieContainer(...)}.
* The direct manual call to KieContainerImpl constructor instead would not guarantee the consistency of the supplied containerId.
*/
public KieContainerImpl(KieProject kProject, KieRepository kr) {
this("impl"+UUID.randomUUID(), kProject, kr);
}

/**
* Please note: the recommended way of getting a KieContainer is relying on {@link org.kie.api.KieServices KieServices} API,
* for example: {@link org.kie.api.KieServices#newKieContainer(ReleaseId) KieServices.newKieContainer(...)}.
* The direct manual call to KieContainerImpl constructor instead would not guarantee the consistency of the supplied containerId.
*/
public KieContainerImpl(KieProject kProject, KieRepository kr, ReleaseId containerReleaseId) {
this("impl"+UUID.randomUUID(), kProject, kr, containerReleaseId);
}

/**
* Please note: the recommended way of getting a KieContainer is relying on {@link org.kie.api.KieServices KieServices} API,
* for example: {@link org.kie.api.KieServices#newKieContainer(ReleaseId) KieServices.newKieContainer(...)}.
* The direct manual call to KieContainerImpl constructor instead would not guarantee the consistency of the supplied containerId.
*/
public KieContainerImpl(String containerId, KieProject kProject, KieRepository kr) {
this.kr = kr;
this.kProject = kProject;
this.containerId = containerId;
kProject.init();
}

public KieContainerImpl(KieProject kProject, KieRepository kr, ReleaseId containerReleaseId) {
this(kProject, kr);

/**
* Please note: the recommended way of getting a KieContainer is relying on {@link org.kie.api.KieServices KieServices} API,
* for example: {@link org.kie.api.KieServices#newKieContainer(ReleaseId) KieServices.newKieContainer(...)}.
* The direct manual call to KieContainerImpl constructor instead would not guarantee the consistency of the supplied containerId.
*/
public KieContainerImpl(String containerId, KieProject kProject, KieRepository kr, ReleaseId containerReleaseId) {
this(containerId, kProject, kr);
this.configuredReleaseId = containerReleaseId;
this.containerReleaseId = containerReleaseId;

initMBeans(containerId);
}

public ReleaseId getReleaseId() {
private void initMBeans(String containerId) {
if ( MBeansOption.isEnabled( System.getProperty( MBeansOption.PROPERTY_NAME, MBeansOption.DISABLED.toString() ) ) ) {
KieContainerMonitor monitor = new KieContainerMonitor(this);
ObjectName on = DroolsManagementAgent.createObjectNameByContainerId(containerId);
DroolsManagementAgent.getInstance().registerMBean( this, monitor, on );
}
}

@Override
public String getContainerId() {
return this.containerId;
}

@Override
public ReleaseId getConfiguredReleaseId() {
return configuredReleaseId;
}

@Override
public ReleaseId getResolvedReleaseId() {
return getReleaseId();
}

public ReleaseId getReleaseId() {
return kProject.getGAV();
}

Expand All @@ -120,6 +184,7 @@ public long getCreationTimestamp() {
return kProject.getCreationTimestamp();
}

@Override
public ReleaseId getContainerReleaseId() {
return containerReleaseId != null ? containerReleaseId : getReleaseId();
}
Expand Down Expand Up @@ -242,6 +307,8 @@ private void updateKBase( InternalKnowledgeBase kBase, InternalKieModule current
}
rebuildAll(newReleaseId, results, newKM, modifyingUsedClass, kieBaseModel, pkgbuilder, ckbuilder);
}

kBase.setResolvedReleaseId(newReleaseId);

for ( InternalWorkingMemory wm : kBase.getWorkingMemories() ) {
wm.notifyWaitOnRest();
Expand Down Expand Up @@ -512,7 +579,10 @@ private KieBase createKieBase(KieBaseModelImpl kBaseModel, KieProject kieProject
} else if (conf instanceof RuleBaseConfiguration) {
((RuleBaseConfiguration)conf).setClassLoader(cl);
}
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase( conf );
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase( kBaseModel.getName(), conf );
kBase.setResolvedReleaseId(containerReleaseId);
kBase.setContainerId(containerId);
kBase.initMBeans();

kBase.addKnowledgePackages( pkgs );
return kBase;
Expand Down Expand Up @@ -698,6 +768,7 @@ public void dispose() {
}
kSessions.clear();
statelessKSessions.clear();
((InternalKieServices) KieServices.Factory.get()).clearRefToContainerId(this.containerId, this);
}

public KieProject getKieProject() {
Expand Down

0 comments on commit b274c62

Please sign in to comment.