Files
alfresco-community-repo/src/test/java/org/alfresco/heartbeat/HBDataCollectorServiceImplTest.java
Erik Knizat 310b9d88f3 REPO-3007: This is a squash commit containing scheduling and clustering commits
* Add job cron expression and unique id to HBBaseDataCollector
* Remove HeartBeat class and move its responsibilities to HBDataCollectorServiceImpl
* Refactor HBDataCollectorServiceImpl
* Update collector bean definitions with cron expression
* Add job locking functionality
* Refactor tests to work with new code changes

Squashed commit of the following:

commit 522a8c07b65aa0170387d7cea8b1a89230e019ed
Author: Erik Knizat <erik.knizat@alfresco.com>
Date:   Fri Nov 17 15:22:48 2017 +0000

    Cleaned up code:
    * HBDataCollectorServiceImpl - improved logic so that collectors which fail to schedule job do not get registered
    * HBDataCollectorServiceImpl - clean up code
    * HBDataCollectorServiceImplTest - Revrite old test to work with new changed
    * heartbeat-context.xml - add register method to the base collector template
    * Minor comment updates

commit d75b920a9172b3e53d901d1abbf33bada9419c4d
Author: Martin Müller <martin.muller@alfresco.com>
Date:   Mon Nov 6 20:17:30 2017 +0000

    REPO-3007 bug in usage system collector

commit 2f25ec04868569eab4de34bd804ee9fb3490f9c2
Author: Martin Müller <martin.muller@alfresco.com>
Date:   Mon Nov 6 20:15:41 2017 +0000

    Revert "REPO-3007 try to fix context load errors"

    This reverts commit 30d0f70609e1c774b7989c18cd76ad87ccf239ed.

commit 30d0f70609e1c774b7989c18cd76ad87ccf239ed
Author: Martin Müller <martin.muller@alfresco.com>
Date:   Mon Nov 6 19:57:14 2017 +0000

    REPO-3007 try to fix context load errors

commit 615e72bfdb6d08f1d82cfe581d0a222932eec483
Merge: c128561fc e357becce
Author: Martin Müller <martin.muller@alfresco.com>
Date:   Mon Nov 6 19:30:41 2017 +0000

    Merge branch 'develop' into feature/REPO-2627_Clust-Test

commit c128561fc977979dda5c2d397da9b05993e0db68
Author: Martin Müller <martin.muller@alfresco.com>
Date:   Mon Nov 6 19:13:47 2017 +0000

    REPO-3007 reduced time for TTL and TTL_OFFSET

commit 02184034908775ea835d7022478a9c7b6a8dc66a
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Mon Nov 6 18:47:06 2017 +0000

    REPO-3007 additional life time of the job for keeping the lock longer. Reduced additional work.

commit 63332cdf08d8819518bbe6a3b80e988e2c8bc554
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Mon Nov 6 18:42:22 2017 +0000

    REPO-3007 additional life time of the job for keeping the lock longer. Reduced additional work.

commit 7333ae7f448b5fe3601a99d25c814c8a58ca0e92
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Mon Nov 6 18:12:47 2017 +0000

    REPO-3007 additional life time of the job for keeping the lock longer. Reduced additional work.

commit e43c3cff787d6985cbf4d957dfcf7cf27e4aa495
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Mon Nov 6 18:05:51 2017 +0000

    REPO-3007 additional life time of the job for keeping the lock longer. Reduced additional work.

commit 5f8d19fc05b5ef4855c89aab86e2e786fcc8b0d9
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Wed Nov 1 15:37:27 2017 +0000

    REPO-3007 spell check.

commit e7c8aa3f96939e29cd9ac6b962ad5a5d848269e3
Merge: 2bbcba39f b0f057f0d
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Tue Oct 31 14:47:23 2017 +0000

    Merge branch 'develop' into feature/REPO-2627_Clust-Test

commit 2bbcba39faaa4ba97d9ec81a691256c2292e5f8d
Author: alfresco-build <build@alfresco.com>
Date:   Tue Oct 31 14:03:04 2017 +0000

    [maven-release-plugin] prepare for next development iteration

commit 1e74e1dc6652daa5964d2c8cf1b06ef32147e15f
Author: alfresco-build <build@alfresco.com>
Date:   Tue Oct 31 14:02:57 2017 +0000

    [maven-release-plugin] prepare release alfresco-repository-6.9-REPO-2627_Clust-Test-1

commit fdf8b40dc6ad00433c3601782864c5cdaec63706
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Tue Oct 31 13:45:02 2017 +0000

    REPO-3007 Some syntax clean ups

commit ca1b8a2b6019b1a21c84eb949bbe57ae9bbdef9c
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Tue Oct 31 11:59:48 2017 +0000

    REPO-3007 Bring back the concrete collector tests. And little refactoring of them.

commit 73fbafc4732f06e0c48d0fa0e6225ba26265d75c
Author: Martin Müller <martin.muller@alfresco.com>
Date:   Mon Oct 30 16:49:21 2017 +0000

    REPO-3007 Implemented each data collection in his own data collector. Removed some unnecessary tests.

commit e41d3afd7b414aae921599aa193d6fbd167ab35a
Author: Martin Müller <martin.muller@alfresco.com>
Date:   Mon Oct 30 16:08:00 2017 +0000

    REPO-3007 Implemented each data collection in his own data collector. Removed some unnecessary tests.

commit f13ec13a5b4dc6fb490fcc93c06a13d0b0348df5
Author: Martin Müller <martin.muller@alfresco.com>
Date:   Mon Oct 30 11:54:33 2017 +0000

    REPO-3007 Implemented each data collection in his own data collector. Removed some unnecessary tests.

commit 37115ba39a73b9ea4fc7e5d562c83bb792b11c9b
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Fri Oct 27 18:00:44 2017 +0100

    REPO-3007 Remove old HeartBeat.java references

commit cd6af905914119bc4f8526500303d29b4759a6e2
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Fri Oct 27 17:50:39 2017 +0100

    REPO-3007 Reduce mocking objects

commit 4c5b82a50f1d42b35162dfcf31a21a303fddbc6b
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Fri Oct 27 17:38:09 2017 +0100

    REPO-3007 Improve tests for hb default enabled

commit 7abd1467de33b72f304c26bdf18f3769ef9632b1
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Fri Oct 27 14:22:53 2017 +0100

    REPO-3007 Delete old HeartBeat files

commit 301ab0525f1a146223f21aaba0122a70384c6342
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Fri Oct 27 14:21:58 2017 +0100

    Delete old HeartBeat files

commit c3a84596d7f809aa7ebb7789ae98f953f44b4d36
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Fri Oct 27 14:13:50 2017 +0100

    REPO-3007 Some CollectorServiceImplTests

commit e7071811e9e0276861c374248215fdde94285623
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Fri Oct 27 14:11:39 2017 +0100

    Some CollectorServiceImplTests

commit ac5aa8e9a295f250920f8ebbe2a5ad97ba4270eb
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Fri Oct 27 13:26:25 2017 +0100

    Continued refactoring. Get some tests running

commit d5ef15fe0080cebb586360f10a21eef94ba63d51
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Fri Oct 27 12:45:05 2017 +0100

    Continued refactoring. Get some tests running

commit f7ae53236daaa5e48dbd829f5f1cd984c2c06e8c
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Fri Oct 27 12:24:36 2017 +0100

    Continued refactoring. Get some tests running

commit 7c200d0f7633efe7cda4a68e7ef279e85b7e5d99
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Thu Oct 26 17:32:37 2017 +0100

    REPO-3007 implemented dynamic lock with JobLockCallBack

commit 5def15950b87b9df4136fedff447d4f54aa87736
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Thu Oct 26 17:28:09 2017 +0100

    REPO-3007 implemented dynamic lock with JobLockCallBack

commit bdbbe49a081661373a2114b7cf2cb415d4383afd
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Thu Oct 26 17:25:28 2017 +0100

    REPO-3007 implemented dynamic lock with JobLockCallBack

commit a5d1f45f28a0ae228a8c9bf2f8a4171413a692e7
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Thu Oct 26 16:07:46 2017 +0100

    Update heartbeat-context.xml

    We don't need the licenseService here

commit c2efe1bd325f89f0a4c0ebf95b5a0e99baa249f4
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Thu Oct 26 16:07:03 2017 +0100

    Update HBDataCollectorServiceImpl.java

    We don't need the licenseService here

commit 8f1bd298e1f03f0a45be31b28f14289ad1684c9f
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Thu Oct 26 14:41:53 2017 +0100

    REPO-3007 the collectors are unique

commit 2f8ad8fff384c1f525759be59ab8c1ba5b05b5f2
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Thu Oct 26 14:32:02 2017 +0100

    REPO-3007 starting refactoring

commit 5a3e7f2d25d9c20017f12b8fec26198a4f7d705b
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Thu Oct 26 14:26:32 2017 +0100

    REPO-3007 starting refactoring

commit ed111b77f6f311be967929ba09025d6bb4fb9ab8
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Thu Oct 26 14:16:49 2017 +0100

    REPO-3007 starting refactoring

commit 8e2497dc1f05f71451736b8366b3be1f420bf8a6
Author: alfresco-build <build@alfresco.com>
Date:   Thu Oct 26 11:26:19 2017 +0100

    [maven-release-plugin] prepare for next development iteration

commit e15f02390fc09ab7baaad1e186ed35e27e1f7445
Author: alfresco-build <build@alfresco.com>
Date:   Thu Oct 26 11:26:12 2017 +0100

    [maven-release-plugin] prepare release alfresco-repository-6.9-REPO-2627_Clust-Test

commit 35eed74bd869ee8a6a9f4a30c86b9f7cd4bd331b
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Wed Oct 25 13:41:47 2017 +0100

    REPO-2627 Implemented job locking for hb collectors and added unit cluster test

commit b67fc751cd11a9a73fee6873663216bbd05422e1
Author: Martin Muller <martin.muller@alfresco.com>
Date:   Wed Oct 25 13:22:05 2017 +0100

    REPO-2627 Implemented job locking for hb collectors and added unit cluster test
2017-11-17 16:12:24 +00:00

335 lines
13 KiB
Java

/*
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.heartbeat;
import org.alfresco.heartbeat.datasender.HBData;
import org.alfresco.heartbeat.datasender.HBDataSenderService;
import org.alfresco.repo.scheduler.AlfrescoSchedulerFactory;
import org.alfresco.service.license.LicenseDescriptor;
import org.junit.Before;
import org.junit.Test;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author eknizat
*/
public class HBDataCollectorServiceImplTest
{
private final String VALID_CRON = "0 0/2 * * * ?";
private final String INVALID_CRON = "0 no no * * ?";
private final HBBaseDataCollector validCollector1 = new SimpleHBDataCollector("validCollector1", VALID_CRON);
private final HBBaseDataCollector validCollector2 = new SimpleHBDataCollector("validCollector2", VALID_CRON);
private Scheduler scheduler;
@Before
public void before() throws Exception
{
// Create fresh scheduler
SchedulerFactoryBean sfb = new SchedulerFactoryBean();
sfb.setSchedulerFactoryClass(AlfrescoSchedulerFactory.class);
sfb.setAutoStartup(false);
sfb.afterPropertiesSet();
scheduler = sfb.getScheduler();
}
@Test
public void testInitialEnabledEqualsDefaultState() throws Exception
{
HBDataCollectorServiceImpl dataCollectorService = new HBDataCollectorServiceImpl(true);
assertTrue(dataCollectorService.isEnabledByDefault());
dataCollectorService = new HBDataCollectorServiceImpl(false);
assertFalse(dataCollectorService.isEnabledByDefault());
}
@Test
public void testJobSchedulingWhenEnabled() throws Exception
{
// Enable heartbeat by setting the default enabled state ( as if set in prop file)
final HBDataCollectorServiceImpl collectorService = new HBDataCollectorServiceImpl(true);
collectorService.setScheduler(scheduler);
// Register few collectors
registerValidCollectors(collectorService);
// Check that the jobs are scheduled for all collectors when heartbeat is enabled
assertTrue(collectorService.isEnabled());
assertCollectorJobsScheduled("Job was not scheduled but HB is enabled", scheduler);
}
@Test
public void testJobSchedulingWhenDisabled() throws Exception
{
// Disable heartbeat by setting the default enabled state ( as if set in prop file)
final HBDataCollectorServiceImpl collectorService = new HBDataCollectorServiceImpl(false);
collectorService.setScheduler(scheduler);
// Register collectors
registerValidCollectors(collectorService);
// Check that the jobs are not scheduled for any collectors when heartbeat is disabled
assertFalse(collectorService.isEnabled());
assertCollectorJobsNotScheduled("Job was scheduled but HB is disabled", scheduler);
}
/**
* Heartbeat enabled by default but disabled in licence on onLicenseChange
*/
@Test
public void testOnLicenseChangeOverridesDefaultEnabled() throws Exception
{
// Enable heartbeat by setting the default enabled state ( as if set in prop file)
final HBDataCollectorServiceImpl collectorService = new HBDataCollectorServiceImpl(true);
final HBDataSenderService sender = mock( HBDataSenderService.class);
collectorService.setHbDataSenderService(sender);
collectorService.setScheduler(scheduler);
// Register few collectors
registerValidCollectors(collectorService);
// Heartbeat disabled in licence
enabledHbInLicense( collectorService,false);
// Check heart beat is disabled and all collector jobs are unscheduled
assertFalse(collectorService.isEnabled());
assertCollectorJobsNotScheduled("Job was scheduled but HB is disabled", scheduler);
// Also check sender is updated
verify(sender).enable(false);
}
/**
* Heartbeat disabled by default but enabled in licence on onLicenseChange
*/
@Test
public void testOnLicenseChangeOverridesDefaultDisabled() throws Exception
{
// Disable heartbeat by setting the default enabled state ( as if set in prop file)
final HBDataCollectorServiceImpl collectorService = new HBDataCollectorServiceImpl(false);
final HBDataSenderService sender = mock( HBDataSenderService.class);
collectorService.setHbDataSenderService(sender);
collectorService.setScheduler(scheduler);
// Register few collectors
registerValidCollectors(collectorService);
// Heartbeat enabled in licence
enabledHbInLicense( collectorService,true);
// Check heart beat is enabled and all collector jobs are scheduled
assertTrue(collectorService.isEnabled());
assertCollectorJobsScheduled("Job was not scheduled but HB is enabled", scheduler);
// Also check sender is updated
verify(sender).enable(true);
}
/**
* Heartbeat revert back to default enabled state on license fail
*/
@Test
public void testOnLicenceFailRevertsToEnabled() throws Exception
{
// Enable heartbeat by setting the default enabled state ( as if set in prop file)
final HBDataCollectorServiceImpl collectorService = new HBDataCollectorServiceImpl(true);
collectorService.setScheduler(scheduler);
// Register few collectors
registerValidCollectors(collectorService);
// Heartbeat disabled in licence
enabledHbInLicense( collectorService,false);
// Revert back to default state
collectorService.onLicenseFail();
// Check heartbeat state reverts to default enabled state and jobs are scheduled
assertTrue(collectorService.isEnabled());
assertCollectorJobsScheduled("Job should be unscheduled.", scheduler);
}
/**
* Heartbeat revert back to default disabled state on license fail
*/
@Test
public void testOnLicenceFailRevertsToDisabled() throws Exception
{
// Disable heartbeat by setting the default enabled state ( as if set in prop file)
final HBDataCollectorServiceImpl collectorService = new HBDataCollectorServiceImpl(false);
collectorService.setScheduler(scheduler);
// Register few collectors
registerValidCollectors(collectorService);
// Heartbeat enabled in licence
enabledHbInLicense( collectorService,true);
// Revert back to default state
collectorService.onLicenseFail();
// Check heartbeat is disabled and job unscheduled
assertFalse(collectorService.isEnabled());
assertCollectorJobsNotScheduled("Job should be unscheduled.",scheduler);
}
/**
* Test scheduling job for collector with invalid cron expression
*/
@Test
public void testInvalidCronExpression() throws Exception
{
final HBDataCollectorServiceImpl collectorService = new HBDataCollectorServiceImpl(true);
collectorService.setScheduler(scheduler);
// Register collector with valid cron expression
SimpleHBDataCollector c1 = new SimpleHBDataCollector("c1", VALID_CRON);
collectorService.registerCollector(c1);
// Register collector with invalid cron expression
SimpleHBDataCollector c2 = new SimpleHBDataCollector("c2", INVALID_CRON);
collectorService.registerCollector(c2);
assertTrue(isJobScheduledForCollector(c1.getCollectorId(),scheduler));
assertFalse(isJobScheduledForCollector(c2.getCollectorId(),scheduler));
}
/**
*
* Jobs are scheduled with cron expressions provided by collectors
*
*/
@Test
public void testJobsScheduledWithDifferentCronExpressions() throws Exception
{
final HBDataCollectorServiceImpl collectorService = new HBDataCollectorServiceImpl(true);
collectorService.setScheduler(scheduler);
final String cron1 = "0 0/1 * * * ?";
final String cron2 = "0 0/2 * * * ?";
final String cron3 = "0 0/3 * * * ?";
final HBBaseDataCollector c1 = new SimpleHBDataCollector("c1", cron1);
final HBBaseDataCollector c2 = new SimpleHBDataCollector("c2", cron2);
final HBBaseDataCollector c3 = new SimpleHBDataCollector("c3", cron3);
final String triggerName1 = "heartbeat-" + c1.getCollectorId() + "-Trigger";
final String triggerName2 = "heartbeat-" + c2.getCollectorId() + "-Trigger";
final String triggerName3 = "heartbeat-" + c3.getCollectorId() + "-Trigger";
// Register 3 collectors with 3 different cron expressions
collectorService.registerCollector(c1);
collectorService.registerCollector(c2);
collectorService.registerCollector(c3);
String testCron1 = ((CronTrigger) scheduler.getTrigger(triggerName1, Scheduler.DEFAULT_GROUP)).getCronExpression();
String testCron2 = ((CronTrigger) scheduler.getTrigger(triggerName2, Scheduler.DEFAULT_GROUP)).getCronExpression();
String testCron3 = ((CronTrigger) scheduler.getTrigger(triggerName3, Scheduler.DEFAULT_GROUP)).getCronExpression();
assertEquals("Cron expression doesn't match", cron1, testCron1);
assertEquals("Cron expression doesn't match", cron2, testCron2);
assertEquals("Cron expression doesn't match", cron3, testCron3);
}
@Test
public void testRegisterSameCollectorTwice() throws Exception
{
final HBDataCollectorServiceImpl collectorService = new HBDataCollectorServiceImpl(true);
collectorService.setScheduler(scheduler);
HBBaseDataCollector c1 = new SimpleHBDataCollector("c1", VALID_CRON);
collectorService.registerCollector(c1);
collectorService.registerCollector(c1);
assertEquals("Expected only one collector to be scheduled.",1,scheduler.getJobNames(Scheduler.DEFAULT_GROUP).length );
}
// Helper methods
private void registerValidCollectors(HBDataCollectorServiceImpl collectorService)
{
collectorService.registerCollector(validCollector1);
collectorService.registerCollector(validCollector2);
}
private void assertCollectorJobsScheduled(String message, Scheduler scheduler) throws Exception
{
assertTrue(message, isJobScheduledForCollector(validCollector1.getCollectorId(), scheduler));
assertTrue(message, isJobScheduledForCollector(validCollector2.getCollectorId(), scheduler));
}
private void assertCollectorJobsNotScheduled(String message, Scheduler scheduler) throws Exception
{
assertFalse(message, isJobScheduledForCollector(validCollector1.getCollectorId(), scheduler));
assertFalse(message, isJobScheduledForCollector(validCollector2.getCollectorId(), scheduler));
}
private void enabledHbInLicense(HBDataCollectorServiceImpl collectorService, boolean activate)
{
LicenseDescriptor mockLicenseDescriptor = mock(LicenseDescriptor.class);
when(mockLicenseDescriptor.isHeartBeatDisabled()).thenReturn(!activate);
collectorService.onLicenseChange(mockLicenseDescriptor);
}
private boolean isJobScheduledForCollector(String collectorId, Scheduler scheduler) throws Exception
{
String jobName = "heartbeat-" + collectorId;
String triggerName = jobName + "-Trigger";
String[] jobs = scheduler.getJobNames(Scheduler.DEFAULT_GROUP);
String[] triggers = scheduler.getTriggerNames(Scheduler.DEFAULT_GROUP);
return Arrays.asList(jobs).contains(jobName) && Arrays.asList(triggers).contains(triggerName);
}
private class SimpleHBDataCollector extends HBBaseDataCollector
{
public SimpleHBDataCollector(String collectorId, String cron)
{
super(collectorId);
this.setCronExpression(cron);
}
public List<HBData> collectData()
{
List<HBData> result = new LinkedList<>();
return result;
}
}
}