Search in sources :

Example 1 with ComparableVersion

use of org.kie.api.builder.ReleaseIdComparator.ComparableVersion in project drools by kiegroup.

the class KieModuleRepoTest method testDeployTwoArtifactVersionsSameTime.

/**
 * TESTS ----------------------------------------------------------------------------------------------------------------------
 */
// simultaneous requests to deploy two new deployments (different versions)
// for an empty/new GA artifactMap
@Test(timeout = 5000)
public void testDeployTwoArtifactVersionsSameTime() throws Exception {
    final String groupId = "org";
    final String artifactId = "one";
    final String firstVersion = "1.0";
    final String secondVersion = "1.0-NEW-FEATURE";
    final CyclicBarrier storeOperationBarrier = new CyclicBarrier(2);
    final CyclicBarrier threadsFinishedBarrier = new CyclicBarrier(3);
    final Thread firstThread = new Thread(getStoreArtifactRunnable(kieModuleRepo, groupId, artifactId, firstVersion, storeOperationBarrier, threadsFinishedBarrier));
    final Thread secondThread = new Thread(getStoreArtifactRunnable(kieModuleRepo, groupId, artifactId, secondVersion, storeOperationBarrier, threadsFinishedBarrier));
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    firstThread.setName("normal");
    executor.submit(firstThread);
    secondThread.setName("newFeature");
    executor.submit(secondThread);
    try {
        waitFor(threadsFinishedBarrier);
    } finally {
        executor.shutdownNow();
    }
    final String ga = groupId + ":" + artifactId;
    final Map<ComparableVersion, KieModule> artifactMap = kieModuleRepo.kieModules.get(ga);
    final ComparableVersion normalVersion = new ComparableVersion(firstVersion);
    final KieModule normalKieModule = artifactMap.get(normalVersion);
    final ComparableVersion newFeatureVersion = new ComparableVersion(secondVersion);
    final KieModule newFeatureKieModule = artifactMap.get(newFeatureVersion);
    assertNotNull("Race condition occurred: normal KieModule disappeared from KieModuleRepo!", normalKieModule);
    assertNotNull("Race condition occurred: new feature KieModule disappeared from KieModuleRepo!", newFeatureKieModule);
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ComparableVersion(org.kie.api.builder.ReleaseIdComparator.ComparableVersion) KieModule(org.kie.api.builder.KieModule) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 2 with ComparableVersion

use of org.kie.api.builder.ReleaseIdComparator.ComparableVersion 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 ComparableVersion

use of org.kie.api.builder.ReleaseIdComparator.ComparableVersion in project drools by kiegroup.

the class KieModuleRepoTest method removeStoreArtifactMapTest.

// remove request followed by a store request on a high load system
// * remove does not completely finish before store starts
@Test(timeout = 5000)
public void removeStoreArtifactMapTest() throws Exception {
    // actual test
    final ReleaseIdImpl releaseId = new ReleaseIdImpl("org", "redeploy", "2.0");
    final InternalKieModule originalKieModule = mock(InternalKieModule.class);
    when(originalKieModule.getReleaseId()).thenReturn(releaseId);
    when(originalKieModule.getCreationTimestamp()).thenReturn(0l);
    final InternalKieModule redeployKieModule = mock(InternalKieModule.class);
    when(redeployKieModule.getReleaseId()).thenReturn(releaseId);
    when(redeployKieModule.getCreationTimestamp()).thenReturn(1l);
    // 1. initial deploy ("long ago")
    kieModuleRepo.store(originalKieModule);
    final CyclicBarrier storeRemoveOperationBarrier = new CyclicBarrier(2);
    final CyclicBarrier operationsSerializationBarrier = new CyclicBarrier(2);
    final CyclicBarrier threadsFinishedBarrier = new CyclicBarrier(3);
    final Runnable removeRunnable = () -> {
        waitFor(storeRemoveOperationBarrier);
        kieModuleRepo.remove(releaseId);
        waitFor(operationsSerializationBarrier);
        waitFor(threadsFinishedBarrier);
    };
    final Runnable redeployRunnable = () -> {
        waitFor(storeRemoveOperationBarrier);
        waitFor(operationsSerializationBarrier);
        kieModuleRepo.store(redeployKieModule);
        waitFor(threadsFinishedBarrier);
    };
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    // 2. remove and redploy
    executor.submit(removeRunnable);
    executor.submit(redeployRunnable);
    try {
        waitFor(threadsFinishedBarrier);
    } finally {
        executor.shutdownNow();
    }
    final String ga = releaseId.getGroupId() + ":" + releaseId.getArtifactId();
    final Map<ComparableVersion, KieModule> artifactMap = kieModuleRepo.kieModules.get(ga);
    assertNotNull("Artifact Map for GA '" + ga + "' not in KieModuleRepo!", artifactMap);
    // never gets this far, but this is a good check
    final KieModule redeployedKieModule = artifactMap.get(new ComparableVersion(releaseId.getVersion()));
    assertNotNull("Redeployed module has disappeared from KieModuleRepo!", redeployedKieModule);
    assertEquals("Original module retrieved instead of redeployed module!", 1l, redeployKieModule.getCreationTimestamp());
}
Also used : ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) ExecutorService(java.util.concurrent.ExecutorService) ComparableVersion(org.kie.api.builder.ReleaseIdComparator.ComparableVersion) KieModule(org.kie.api.builder.KieModule) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 KieModule (org.kie.api.builder.KieModule)3 ComparableVersion (org.kie.api.builder.ReleaseIdComparator.ComparableVersion)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 ExecutorService (java.util.concurrent.ExecutorService)2 ReleaseIdImpl (org.drools.compiler.kproject.ReleaseIdImpl)2