Search in sources :

Example 1 with ReleaseIdImpl

use of org.drools.compiler.kproject.ReleaseIdImpl in project drools by kiegroup.

the class KieModuleRepoTest method newerVersionDeployOverwritesTest.

// 2. simultaneous deploy requests
// - multitenant UI
// - duplicate REST requests
@Test(timeout = 5000)
public void newerVersionDeployOverwritesTest() throws Exception {
    // setup
    final ReleaseIdImpl releaseId = new ReleaseIdImpl("org", "deployTwiceAfterUpdateDependency", "1.0-SNAPSHOT");
    final InternalKieModule originalOldKieModule = new InternalKieModuleStub();
    final ReleaseId dependentReleaseid = new ReleaseIdImpl("org", "deployTwiceAfterUpdate", "1.0-SNAPSHOT");
    final KieContainerImpl kieContainer = createMockKieContainer(dependentReleaseid, kieModuleRepo);
    // 1. deploy
    kieModuleRepo.store(originalOldKieModule);
    // 2. another project is dependent on this project
    kieContainer.updateDependencyToVersion(releaseId, releaseId);
    final InternalKieModule newKieModule = mock(InternalKieModule.class);
    when(newKieModule.getReleaseId()).thenReturn(releaseId);
    when(newKieModule.getCreationTimestamp()).thenReturn(10l);
    final CyclicBarrier storeOperationBarrier = new CyclicBarrier(2);
    final CyclicBarrier storeSerializationBarrier = new CyclicBarrier(2);
    final CyclicBarrier threadsFinishedBarrier = new CyclicBarrier(3);
    final Runnable deployRunnable = () -> {
        waitFor(storeOperationBarrier);
        // Second thread waits with store until the first one finishes with it.
        if (Thread.currentThread().getName().equals("two")) {
            waitFor(storeSerializationBarrier);
        }
        kieModuleRepo.store(newKieModule);
        if (Thread.currentThread().getName().equals("one")) {
            waitFor(storeSerializationBarrier);
        }
        waitFor(threadsFinishedBarrier);
    };
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    // 2. remove and redploy
    final Thread deployThread = new Thread(deployRunnable);
    deployThread.setName("one");
    executor.submit(deployThread);
    final Thread secondDeployThread = new Thread(deployRunnable);
    secondDeployThread.setName("two");
    executor.submit(secondDeployThread);
    try {
        waitFor(threadsFinishedBarrier);
    } finally {
        executor.shutdownNow();
    }
    // never gets this far, but this is a good check
    final KieModule oldKieModule = kieModuleRepo.oldKieModules.get(releaseId);
    final long oldKieModuleTimeStamp = ((InternalKieModule) oldKieModule).getCreationTimestamp();
    final long originalKieModuleTimestamp = originalOldKieModule.getCreationTimestamp();
    assertEquals("The old kie module in the repo is not the originally deployed module!", originalKieModuleTimestamp, oldKieModuleTimeStamp);
}
Also used : ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) ExecutorService(java.util.concurrent.ExecutorService) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 2 with ReleaseIdImpl

use of org.drools.compiler.kproject.ReleaseIdImpl in project drools by kiegroup.

the class KieModuleRepoTest method testOldKieModulesLRUCache.

@Test
public void testOldKieModulesLRUCache() throws Exception {
    // setup
    setFinalField(maxSizeGaCacheField, null, 2);
    setFinalField(maxSizeGaVersionsCacheField, null, 4);
    final ReleaseIdImpl[] releaseIds = new ReleaseIdImpl[9];
    for (int i = 0; i < releaseIds.length; ++i) {
        final String artifactId = Character.toString((char) ('A' + i / 2));
        releaseIds[i] = new ReleaseIdImpl("org", artifactId, "1." + i);
    }
    // store
    for (int i = 0; i < releaseIds.length; ++i) {
        final InternalKieModule kieModule = mock(InternalKieModule.class);
        when(kieModule.getReleaseId()).thenReturn(releaseIds[i]);
        when(kieModule.getCreationTimestamp()).thenReturn(10l);
        kieModuleRepo.store(kieModule);
        // in order to trigger storage to oldKieModules
        kieModuleRepo.store(kieModule);
    }
    int maxSameGAModules = 0;
    int maxGAs = 0;
    for (final Map<ComparableVersion, KieModule> artifactMap : kieModuleRepo.kieModules.values()) {
        maxGAs++;
        if (artifactMap.size() > maxSameGAModules) {
            maxSameGAModules = artifactMap.size();
        }
    }
    assertTrue("The maximum of artifacts per GA should not grow past " + KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE + ": " + KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE + " < " + maxSameGAModules, KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE >= maxSameGAModules);
    assertTrue("The number of GAs not grow past " + KieModuleRepo.MAX_SIZE_GA_CACHE + ": " + KieModuleRepo.MAX_SIZE_GA_CACHE + " > " + maxGAs, KieModuleRepo.MAX_SIZE_GA_CACHE >= maxGAs);
    final int oldKieModulesSize = kieModuleRepo.oldKieModules.size();
    final int maxOldKieModules = KieModuleRepo.MAX_SIZE_GA_CACHE * KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE;
    assertTrue("KieModuleRepo old KieModules map is not limited in it's growth: " + oldKieModulesSize + " > " + maxOldKieModules, oldKieModulesSize <= maxOldKieModules);
}
Also used : ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) ComparableVersion(org.kie.api.builder.ReleaseIdComparator.ComparableVersion) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 3 with ReleaseIdImpl

use of org.drools.compiler.kproject.ReleaseIdImpl in project drools by kiegroup.

the class KieModuleRepoTest method storingNewProjectsCausesOldProjectEvictionFromKieModuleRepoTest.

@Test
public void storingNewProjectsCausesOldProjectEvictionFromKieModuleRepoTest() throws Exception {
    // setup
    setFinalField(maxSizeGaCacheField, null, 3);
    // to test oldKieModules caching
    setFinalField(maxSizeGaVersionsCacheField, null, 2);
    final ReleaseIdImpl[] releaseIds = new ReleaseIdImpl[7];
    for (int i = 0; i < releaseIds.length; ++i) {
        final String artifactId = Character.toString((char) ('A' + i));
        releaseIds[i] = new ReleaseIdImpl("org", artifactId, "1.0");
    }
    // store
    for (int i = 0; i < releaseIds.length; ++i) {
        final InternalKieModule kieModule = mock(InternalKieModule.class);
        when(kieModule.getReleaseId()).thenReturn(releaseIds[i]);
        when(kieModule.getCreationTimestamp()).thenReturn(10l);
        kieModuleRepo.store(kieModule);
        // store module 2 times to trigger storage to oldKieModules
        kieModuleRepo.store(kieModule);
    }
    final int numKieModules = countKieModules(kieModuleRepo.kieModules);
    assertEquals("KieModuleRepo cache should not grow past " + KieModuleRepo.MAX_SIZE_GA_CACHE + ": ", KieModuleRepo.MAX_SIZE_GA_CACHE, numKieModules);
    final int oldKieModulesSize = kieModuleRepo.oldKieModules.size();
    final int max = KieModuleRepo.MAX_SIZE_GA_CACHE * KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE;
    assertTrue("KieModuleRepot old KieModules map is not limited in it's growth: " + oldKieModulesSize + " > " + max, oldKieModulesSize <= max);
}
Also used : ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) Test(org.junit.Test)

Example 4 with ReleaseIdImpl

use of org.drools.compiler.kproject.ReleaseIdImpl in project drools by kiegroup.

the class KieModuleRepoTest method storingNewProjectVersionsCausesOldVersionEvictionFromKieModuleRepoTest.

@Test
public void storingNewProjectVersionsCausesOldVersionEvictionFromKieModuleRepoTest() throws Exception {
    // setup
    // to test oldKieModules caching
    setFinalField(maxSizeGaCacheField, null, 2);
    setFinalField(maxSizeGaVersionsCacheField, null, 3);
    final ReleaseIdImpl[] releaseIds = new ReleaseIdImpl[7];
    for (int i = 0; i < releaseIds.length; ++i) {
        releaseIds[i] = new ReleaseIdImpl("org", "test", "1." + i);
    }
    // store
    for (int i = 0; i < releaseIds.length; ++i) {
        final InternalKieModule kieModule = mock(InternalKieModule.class);
        when(kieModule.getReleaseId()).thenReturn(releaseIds[i]);
        when(kieModule.getCreationTimestamp()).thenReturn(10l);
        kieModuleRepo.store(kieModule);
        // in order to trigger storage to oldKieModules
        kieModuleRepo.store(kieModule);
    }
    int numKieModules = countKieModules(kieModuleRepo.kieModules);
    assertEquals("KieModuleRepo cache should not grow past " + KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE + ": ", KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE, numKieModules);
    int oldKieModulesSize = kieModuleRepo.oldKieModules.size();
    final int maxOldKieModules = KieModuleRepo.MAX_SIZE_GA_CACHE * KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE;
    assertTrue("KieModuleRepo old KieModules map is not limited in it's growth: " + oldKieModulesSize + " > " + maxOldKieModules, oldKieModulesSize <= maxOldKieModules);
    // store
    for (int o = 0; o < 2; ++o) {
        // loop 2 times in order to trigger storage to oldKieModules
        for (int i = 0; i < releaseIds.length; ++i) {
            final InternalKieModule kieModule = mock(InternalKieModule.class);
            when(kieModule.getReleaseId()).thenReturn(releaseIds[i]);
            when(kieModule.getCreationTimestamp()).thenReturn(10l);
            kieModuleRepo.store(kieModule);
        }
    }
    numKieModules = countKieModules(kieModuleRepo.kieModules);
    assertEquals("KieModuleRepo cache should not grow past " + KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE + ": ", KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE, numKieModules);
    oldKieModulesSize = kieModuleRepo.oldKieModules.size();
    assertTrue("KieModuleRepo old KieModules map is not limited in it's growth: " + oldKieModulesSize + " > " + maxOldKieModules, oldKieModulesSize <= maxOldKieModules);
}
Also used : ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) Test(org.junit.Test)

Example 5 with ReleaseIdImpl

use of org.drools.compiler.kproject.ReleaseIdImpl in project drools by kiegroup.

the class KieModuleDeploymentHelperImpl method internalCreateKieJar.

/**
 * Create a KJar for deployment;
 *
 * @param releaseId Release (deployment) id.
 * @param resourceFilePaths List of resource file paths
 * @param kbaseName The name of the {@link KieBase}
 * @param ksessionName The name of the {@link KieSession}.
 * @param dependencies List of dependencies to add
 *
 * @return The {@link InternalKieModule} which represents the KJar.
 */
private synchronized KieModule internalCreateKieJar(ReleaseId releaseId, String kbaseName, String ksessionName, List<String> resourceFilePaths, List<Class<?>> classes, List<String> dependencies) {
    ReleaseId[] releaseIds = {};
    if (dependencies != null && dependencies.size() > 0) {
        List<ReleaseId> depReleaseIds = new ArrayList<ReleaseId>();
        for (String dep : dependencies) {
            String[] gav = dep.split(":");
            if (gav.length != 3) {
                throw new IllegalArgumentException("Dependendency id '" + dep + "' does not conform to the format <groupId>:<artifactId>:<version> (Classifiers are not accepted).");
            }
            depReleaseIds.add(new ReleaseIdImpl(gav[0], gav[1], gav[2]));
        }
        releaseIds = depReleaseIds.toArray(new ReleaseId[depReleaseIds.size()]);
    }
    config.pomText = getPomText(releaseId, releaseIds);
    KieFileSystem kfs = createKieFileSystemWithKProject(kbaseName, ksessionName);
    kfs.writePomXML(this.config.pomText);
    List<KJarResource> resourceFiles = loadResources(resourceFilePaths);
    for (KJarResource resource : resourceFiles) {
        kfs.write("src/main/resources/" + kbaseName + "/" + resource.name, resource.content);
    }
    if (classes != null) {
        for (Class<?> userClass : classes) {
            addClass(userClass, kfs);
        }
    }
    KieBuilder kieBuilder = config.getKieServicesInstance().newKieBuilder(kfs);
    int buildMsgs = 0;
    for (Message buildMsg : kieBuilder.buildAll().getResults().getMessages()) {
        System.out.println(buildMsg.getPath() + " : " + buildMsg.getText());
        ++buildMsgs;
    }
    if (buildMsgs > 0) {
        throw new RuntimeException("Unable to build KieModule, see the " + buildMsgs + " messages above.");
    }
    return (InternalKieModule) kieBuilder.getKieModule();
}
Also used : ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) ArrayList(java.util.ArrayList) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Aggregations

ReleaseIdImpl (org.drools.compiler.kproject.ReleaseIdImpl)13 ReleaseId (org.kie.api.builder.ReleaseId)9 Test (org.junit.Test)8 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)3 KieServices (org.kie.api.KieServices)3 KieModule (org.kie.api.builder.KieModule)3 KieContainer (org.kie.api.runtime.KieContainer)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 ExecutorService (java.util.concurrent.ExecutorService)2 KieBuilder (org.kie.api.builder.KieBuilder)2 KieFileSystem (org.kie.api.builder.KieFileSystem)2 ComparableVersion (org.kie.api.builder.ReleaseIdComparator.ComparableVersion)2 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)2 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)2 KieSession (org.kie.api.runtime.KieSession)2 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 DependencyDescriptor (org.appformer.maven.integration.DependencyDescriptor)1