use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class HeartbeatTest method testVotingLoop.
/**
* SLING-5027 : test to reproduce the voting loop
* (and verify that it's fixed)
*/
@Test
public void testVotingLoop() throws Throwable {
logger.info("testVotingLoop: creating slowMachine1...");
FullJR2VirtualInstanceBuilder slowBuilder1 = newBuilder();
slowBuilder1.setDebugName("slow1").newRepository("/var/discovery/impl/", true).setConnectorPingTimeout(600).setConnectorPingInterval(999).setMinEventDelay(0);
FullJR2VirtualInstance slowMachine1 = slowBuilder1.fullBuild();
instances.add(slowMachine1);
SimpleTopologyEventListener slowListener1 = new SimpleTopologyEventListener("slow1");
slowMachine1.bindTopologyEventListener(slowListener1);
logger.info("testVotingLoop: creating slowMachine2...");
FullJR2VirtualInstanceBuilder slowBuilder2 = newBuilder();
slowBuilder2.setDebugName("slow2").useRepositoryOf(slowMachine1).setConnectorPingTimeout(600).setConnectorPingInterval(999).setMinEventDelay(0);
FullJR2VirtualInstance slowMachine2 = slowBuilder2.fullBuild();
instances.add(slowMachine2);
SimpleTopologyEventListener slowListener2 = new SimpleTopologyEventListener("slow2");
slowMachine2.bindTopologyEventListener(slowListener2);
logger.info("testVotingLoop: creating fastMachine...");
FullJR2VirtualInstanceBuilder fastBuilder = newBuilder();
fastBuilder.setDebugName("fast").useRepositoryOf(slowMachine1).setConnectorPingTimeout(600).setConnectorPingInterval(999).setMinEventDelay(0);
FullJR2VirtualInstance fastMachine = fastBuilder.fullBuild();
instances.add(fastMachine);
SimpleTopologyEventListener fastListener = new SimpleTopologyEventListener("fast");
fastMachine.bindTopologyEventListener(fastListener);
HeartbeatHandler hhSlow1 = slowMachine1.getHeartbeatHandler();
HeartbeatHandler hhSlow2 = slowMachine2.getHeartbeatHandler();
HeartbeatHandler hhFast = fastMachine.getHeartbeatHandler();
Thread.sleep(1000);
logger.info("testVotingLoop: after some initial 1sec sleep no event should yet have been sent");
assertFalse(fastMachine.getDiscoveryService().getTopology().isCurrent());
assertFalse(slowMachine1.getDiscoveryService().getTopology().isCurrent());
assertFalse(slowMachine2.getDiscoveryService().getTopology().isCurrent());
assertNull(fastListener.getLastEvent());
assertNull(slowListener1.getLastEvent());
assertNull(slowListener2.getLastEvent());
// prevent the slow machine from voting
logger.info("testVotingLoop: stopping voting of slowMachine1...");
slowMachine1.stopVoting();
// now let all issue a heartbeat
logger.info("testVotingLoop: letting slow1, slow2 and fast all issue 1 heartbeat");
hhSlow1.issueHeartbeat();
hhSlow2.issueHeartbeat();
hhFast.issueHeartbeat();
// now let the fast one start a new voting, to which
// only the fast one will vote, the slow one doesn't.
// that will cause a voting loop
logger.info("testVotingLoop: let the fast one do a checkView, thus initiate a voting");
hhFast.doCheckView();
Calendar previousVotedAt = null;
for (int i = 0; i < 5; i++) {
logger.info("testVotingLoop: sleeping 1sec...");
Thread.sleep(1000);
logger.info("testVotingLoop: check to see that there is no voting loop...");
// now check the ongoing votings
ResourceResolverFactory factory = fastMachine.getResourceResolverFactory();
ResourceResolver resourceResolver = factory.getServiceResourceResolver(null);
try {
List<VotingView> ongoingVotings = VotingHelper.listOpenNonWinningVotings(resourceResolver, fastMachine.getFullConfig());
assertNotNull(ongoingVotings);
assertEquals(1, ongoingVotings.size());
VotingView ongoingVote = ongoingVotings.get(0);
assertFalse(ongoingVote.isWinning());
assertFalse(ongoingVote.hasVotedYes(slowMachine1.getSlingId()));
assertTrue(ongoingVote.hasVotedYes(slowMachine2.getSlingId()));
final Resource memberResource = ongoingVote.getResource().getChild("members").getChild(slowMachine2.getSlingId());
final ModifiableValueMap memberMap = memberResource.adaptTo(ModifiableValueMap.class);
Property vote = (Property) memberMap.get("vote");
assertEquals(Boolean.TRUE, vote.getBoolean());
Property votedAt = (Property) memberMap.get("votedAt");
if (previousVotedAt == null) {
previousVotedAt = votedAt.getDate();
} else {
assertEquals(previousVotedAt, votedAt.getDate());
}
} catch (Exception e) {
resourceResolver.close();
fail("Exception: " + e);
}
}
}
use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class JobHandler method reschedule.
/**
* Reschedule the job
* Update the retry count and remove the started time.
* @return <code>true</code> if rescheduling was successful, <code>false</code> otherwise.
*/
public boolean reschedule() {
final ResourceResolver resolver = this.configuration.createResourceResolver();
try {
final Resource jobResource = resolver.getResource(job.getResourcePath());
if (jobResource != null) {
final ModifiableValueMap mvm = jobResource.adaptTo(ModifiableValueMap.class);
mvm.put(Job.PROPERTY_JOB_RETRY_COUNT, job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT, Integer.class));
if (job.getProperty(Job.PROPERTY_RESULT_MESSAGE) != null) {
mvm.put(Job.PROPERTY_RESULT_MESSAGE, job.getProperty(Job.PROPERTY_RESULT_MESSAGE));
}
mvm.remove(Job.PROPERTY_JOB_STARTED_TIME);
mvm.put(JobImpl.PROPERTY_JOB_QUEUED, Calendar.getInstance());
try {
resolver.commit();
return true;
} catch (final PersistenceException pe) {
this.configuration.getMainLogger().debug("Unable to update reschedule properties for job " + job.getId(), pe);
}
}
} finally {
resolver.close();
}
return false;
}
use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class JobHandler method persistJobProperties.
/**
* Update the property of a job in the resource tree
* @param propNames the property names to update
* @return {@code true} if the update was successful.
*/
public boolean persistJobProperties(final String... propNames) {
if (propNames != null) {
final ResourceResolver resolver = this.configuration.createResourceResolver();
try {
final Resource jobResource = resolver.getResource(job.getResourcePath());
if (jobResource != null) {
final ModifiableValueMap mvm = jobResource.adaptTo(ModifiableValueMap.class);
for (final String propName : propNames) {
final Object val = job.getProperty(propName);
if (val != null) {
if (val.getClass().isEnum()) {
mvm.put(propName, val.toString());
} else {
mvm.put(propName, val);
}
} else {
mvm.remove(propName);
}
}
resolver.commit();
return true;
} else {
this.configuration.getMainLogger().debug("No job resource found at {}", job.getResourcePath());
}
} catch (final PersistenceException ignore) {
this.configuration.getMainLogger().debug("Unable to persist properties", ignore);
} finally {
resolver.close();
}
return false;
}
return true;
}
use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class JobSchedulerImpl method setSuspended.
/**
* Change the suspended flag for a scheduled job
* @param info The schedule info
* @param flag The corresponding flag
*/
public void setSuspended(final ScheduledJobInfoImpl info, final boolean flag) {
final ResourceResolver resolver = configuration.createResourceResolver();
try {
final StringBuilder sb = new StringBuilder(this.configuration.getScheduledJobsPath(true));
sb.append(ResourceHelper.filterName(info.getName()));
final String path = sb.toString();
final Resource eventResource = resolver.getResource(path);
if (eventResource != null) {
final ModifiableValueMap mvm = eventResource.adaptTo(ModifiableValueMap.class);
if (flag) {
mvm.put(ResourceHelper.PROPERTY_SCHEDULE_SUSPENDED, Boolean.TRUE);
} else {
mvm.remove(ResourceHelper.PROPERTY_SCHEDULE_SUSPENDED);
}
resolver.commit();
}
if (flag) {
this.stopScheduledJob(info);
} else {
this.startScheduledJob(info);
}
} catch (final PersistenceException pe) {
// we ignore the exception if removing fails
ignoreException(pe);
} finally {
resolver.close();
}
}
use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class FindUnfinishedJobsTask method initTopic.
/**
* Initialize a topic and update all jobs from that topic.
* Reset started time and increase retry count of unfinished jobs
* @param topicResource The topic resource
*/
private void initTopic(final Resource topicResource) {
logger.debug("Initializing topic {}...", topicResource.getName());
JobTopicTraverser.traverse(logger, topicResource, new JobTopicTraverser.JobCallback() {
@Override
public boolean handle(final JobImpl job) {
if (job.getProcessingStarted() != null) {
logger.debug("Found unfinished job {}", job.getId());
job.retry();
try {
final Resource jobResource = topicResource.getResourceResolver().getResource(job.getResourcePath());
// sanity check
if (jobResource != null) {
final ModifiableValueMap mvm = jobResource.adaptTo(ModifiableValueMap.class);
mvm.remove(Job.PROPERTY_JOB_STARTED_TIME);
mvm.put(Job.PROPERTY_JOB_RETRY_COUNT, job.getRetryCount());
if (job.getProperty(JobImpl.PROPERTY_JOB_QUEUED, Calendar.class) == null) {
mvm.put(JobImpl.PROPERTY_JOB_QUEUED, Calendar.getInstance());
}
jobResource.getResourceResolver().commit();
}
} catch (final PersistenceException ignore) {
logger.error("Unable to update unfinished job " + job, ignore);
}
} else if (job.getProperty(JobImpl.PROPERTY_JOB_QUEUED, Calendar.class) == null) {
logger.debug("Found job without queued date {}", job.getId());
try {
final Resource jobResource = topicResource.getResourceResolver().getResource(job.getResourcePath());
// sanity check
if (jobResource != null) {
final ModifiableValueMap mvm = jobResource.adaptTo(ModifiableValueMap.class);
mvm.put(JobImpl.PROPERTY_JOB_QUEUED, Calendar.getInstance());
jobResource.getResourceResolver().commit();
}
} catch (final PersistenceException ignore) {
logger.error("Unable to update queued date for job " + job.getId(), ignore);
}
}
return true;
}
});
logger.debug("Topic {} initialized", topicResource.getName());
}
Aggregations