Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.bitrepository.audittrails;

import org.bitrepository.audittrails.store.AuditTrailStore;
import org.bitrepository.common.ArgumentValidator;
import org.bitrepository.common.settings.Settings;
import org.bitrepository.common.utils.XmlUtils;

import java.time.Duration;

/**
* Parent class for classes starting TimerTasks related to audit-trail operations.
*/
public abstract class AuditTrailTaskStarter {
/** Default duration to pass after system-startup before starting audit trail tasks. */
protected final Duration DEFAULT_GRACE_PERIOD = Duration.ZERO;
protected final Settings settings;
protected final AuditTrailStore store;

public AuditTrailTaskStarter(Settings settings, AuditTrailStore store) {
ArgumentValidator.checkNotNull(settings, "settings");
ArgumentValidator.checkNotNull(store, "AuditTrailStore store");
this.settings = settings;
this.store = store;
}

/**
* Get the grace period/delay duration for audit trail tasks from settings/default duration.
*
* Changing the grace period allows time for the system to finish startup before it has to start
* delivering/processing audit trails. Zero duration = start tasks immediately on startup.
* @return The time to wait before starting audit trail tasks (collection/preservation).
*/
protected Duration getGracePeriod() {
if (settings.getReferenceSettings().getAuditTrailServiceSettings().isSetGracePeriod()) {
javax.xml.datatype.Duration gracePeriod =
settings.getReferenceSettings().getAuditTrailServiceSettings().getGracePeriod();
return XmlUtils.xmlDurationToDuration(gracePeriod);
} else {
return DEFAULT_GRACE_PERIOD;
}
}

public abstract void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ public synchronized void runCollection() {

@Override
public void run() {
if (schedule.getNextRun().getTime() < System.currentTimeMillis()) {
runCollection();
}
runCollection();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.bitrepository.audittrails.collector;

import org.bitrepository.access.getaudittrails.AuditTrailClient;
import org.bitrepository.audittrails.AuditTrailTaskStarter;
import org.bitrepository.audittrails.store.AuditTrailStore;
import org.bitrepository.audittrails.webservice.CollectorInfo;
import org.bitrepository.common.ArgumentValidator;
Expand All @@ -46,36 +47,28 @@
/**
* Manages the retrieval of AuditTrails from contributors.
*/
public class AuditTrailCollector {
public class AuditTrailCollector extends AuditTrailTaskStarter {
private final Logger log = LoggerFactory.getLogger(getClass());
private final Map<String, AuditTrailCollectionTimerTask> collectorTasks = new HashMap<>();
private final Timer timer;
private final Settings settings;
/**
* Initial grace period in milliseconds after startup to allow the system to finish startup.
*/
private static final Duration DEFAULT_GRACE_PERIOD = Duration.ZERO;

/**
* @param settings The settings for this collector.
* @param client The client for handling the conversation for collecting the audit trails.
* @param store The storage of the audit trails data.
* @param alarmDispatcher the alarm dispatcher. Can be null.
* @param alarmDispatcher The alarm dispatcher. Can be null.
*/
public AuditTrailCollector(Settings settings, AuditTrailClient client, AuditTrailStore store,
AlarmDispatcher alarmDispatcher) {
ArgumentValidator.checkNotNull(settings, "settings");
super(settings, store);
ArgumentValidator.checkNotNull(client, "AuditTrailClient client");
ArgumentValidator.checkNotNull(store, "AuditTrailStore store");
ArgumentValidator.checkNotNull(alarmDispatcher, "AlarmDispatcher alarmDispatcher");


this.settings = settings;
this.timer = new Timer(true);
javax.xml.datatype.Duration collectAuditInterval =
settings.getReferenceSettings().getAuditTrailServiceSettings().getCollectAuditInterval();
Duration collectionInterval = XmlUtils.xmlDurationToDuration(collectAuditInterval);
long collectionIntervalMillis = collectionInterval.toMillis();
Duration collectionGracePeriod = getGracePeriod();

for (Collection c : settings.getRepositorySettings().getCollections().getCollection()) {
IncrementalCollector collector = new IncrementalCollector(c.getID(),
Expand All @@ -84,10 +77,10 @@ public AuditTrailCollector(Settings settings, AuditTrailClient client, AuditTrai
SettingsUtils.getMaxClientPageSize(),
alarmDispatcher);
AuditTrailCollectionTimerTask collectorTask = new AuditTrailCollectionTimerTask(
collector, collectionIntervalMillis, Math.toIntExact(getGracePeriod().toMillis()));
log.info("Will start collection of audit trails every {} after a grace period of {}",
TimeUtils.durationToHuman(collectionInterval), TimeUtils.durationToHuman(getGracePeriod()));
timer.scheduleAtFixedRate(collectorTask, getGracePeriod().toMillis(), collectionIntervalMillis / 10);
collector, collectionInterval.toMillis(), Math.toIntExact(collectionGracePeriod.toMillis()));
log.info("Starting collection of audit trails every {} after grace period of {}.",
TimeUtils.durationToHuman(collectionInterval), TimeUtils.durationToHuman(collectionGracePeriod));
timer.scheduleAtFixedRate(collectorTask, collectionGracePeriod.toMillis(), collectionInterval.toMillis());
collectorTasks.put(c.getID(), collectorTask);
}
}
Expand All @@ -107,7 +100,7 @@ public CollectorInfo getCollectorInfo(String collectionID) {
info.setLastDuration("Collection has not finished yet");
}
} else {
info.setLastStart("Audit trail collection has not started");
info.setLastStart("Audit trail collection has not started yet");
info.setLastDuration("Not available");
}
info.setNextStart(TimeUtils.shortDate(nextRun));
Expand All @@ -124,20 +117,6 @@ public void collectNewestAudits(String collectionID) {
collectorTasks.get(collectionID).runCollection();
}

/**
* @return The time to wait before starting collection of audit trails. This enables the system to have time to
* finish startup before they have to start delivering/process audit trails.
*/
private Duration getGracePeriod() {
if (settings.getReferenceSettings().getAuditTrailServiceSettings().isSetGracePeriod()) {
javax.xml.datatype.Duration gracePeriod =
settings.getReferenceSettings().getAuditTrailServiceSettings().getGracePeriod();
return XmlUtils.xmlDurationToDuration(gracePeriod);
} else {
return DEFAULT_GRACE_PERIOD;
}
}

/**
* Closes the AuditTrailCollector.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
package org.bitrepository.audittrails.preserver;

import org.apache.commons.codec.DecoderException;
import org.bitrepository.common.TimerTaskSchedule;
import org.bitrepository.audittrails.AuditTrailTaskStarter;
import org.bitrepository.audittrails.store.AuditTrailStore;
import org.bitrepository.audittrails.webservice.PreservationInfo;
import org.bitrepository.bitrepositoryelements.ChecksumDataForFileTYPE;
import org.bitrepository.bitrepositoryelements.ChecksumSpecTYPE;
import org.bitrepository.client.eventhandler.EventHandler;
import org.bitrepository.common.ArgumentValidator;
import org.bitrepository.common.TimerTaskSchedule;
import org.bitrepository.common.exceptions.OperationFailedException;
import org.bitrepository.common.settings.Settings;
import org.bitrepository.common.utils.Base16Utils;
Expand All @@ -52,26 +53,22 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;

/**
* Handles the preservation of audit trails to a collection defined for the local repository.
* This means, that each set of audit trails will be preserved within its own collection.
*/
public class LocalAuditTrailPreserver implements AuditTrailPreserver {
public class LocalAuditTrailPreserver extends AuditTrailTaskStarter implements AuditTrailPreserver {
private final Logger log = LoggerFactory.getLogger(getClass());
private final AuditTrailStore store;
private final BlockingPutFileClient client;
private final Map<String, AuditPacker> auditPackers = new HashMap<>();
private final AuditTrailPreservation preservationSettings;
private final Settings settings;
private final FileExchange exchange;
private Timer timer;
private AuditPreservationTimerTask preservationTask = null;
Expand All @@ -83,13 +80,10 @@ public class LocalAuditTrailPreserver implements AuditTrailPreserver {
* @param client The PutFileClient for putting the audit trail packages to the collection.
*/
public LocalAuditTrailPreserver(Settings settings, AuditTrailStore store, PutFileClient client, FileExchange exchange) {
ArgumentValidator.checkNotNull(settings, "Settings preservationSettings");
ArgumentValidator.checkNotNull(store, "AuditTrailStore store");
super(settings, store);
ArgumentValidator.checkNotNull(client, "PutFileClient client");

this.settings = settings;
this.preservationSettings = settings.getReferenceSettings().getAuditTrailServiceSettings().getAuditTrailPreservation();
this.store = store;
this.client = new BlockingPutFileClient(client);
this.exchange = exchange;
for (String collectionID : SettingsUtils.getAllCollectionsIDs()) {
Expand Down Expand Up @@ -123,19 +117,22 @@ public void start() {
log.debug("Cancelling old timer.");
timer.cancel();
}

javax.xml.datatype.Duration preservationIntervalXmlDur = preservationSettings.getAuditTrailPreservationInterval();
Duration preservationInterval = XmlUtils.xmlDurationToDuration(preservationIntervalXmlDur);
long timerCheckIntervalMillis = preservationInterval.dividedBy(10).toMillis();
log.info("Instantiating the preservation of audit trails every {}",
TimeUtils.durationToHuman(preservationInterval));
Duration preservationGracePeriod = getGracePeriod();
log.info("Starting preservation of audit trails every {} after grace period of {}.",
TimeUtils.durationToHuman(preservationInterval), TimeUtils.durationToHuman(preservationGracePeriod));
timer = new Timer(true);
preservationTask = new AuditPreservationTimerTask(preservationInterval.toMillis());
timer.scheduleAtFixedRate(preservationTask, timerCheckIntervalMillis, timerCheckIntervalMillis);
preservationTask = new AuditPreservationTimerTask(preservationInterval.toMillis(),
Math.toIntExact(preservationGracePeriod.toMillis()));
timer.scheduleAtFixedRate(preservationTask, preservationGracePeriod.toMillis(), preservationInterval.toMillis());
}

@Override
public void close() {
if (timer != null) {
preservationTask.cancel();
timer.cancel();
}
}
Expand Down Expand Up @@ -270,8 +267,8 @@ private class AuditPreservationTimerTask extends TimerTask {
* @param interval The interval between running this timer task.
*/
// TODO: Replace old time representation (https://sbforge.org/jira/browse/BITMAG-1180)
private AuditPreservationTimerTask(long interval) {
this.schedule = new TimerTaskSchedule(interval, 0);
private AuditPreservationTimerTask(long interval, int gracePeriod) {
this.schedule = new TimerTaskSchedule(interval, gracePeriod);
}

public Date getNextScheduledRun() {
Expand All @@ -288,13 +285,11 @@ public Date getLastPreservationFinish() {

@Override
public void run() {
if (getNextScheduledRun().getTime() < System.currentTimeMillis()) {
log.info("Starting preservation of audit trails.");
schedule.start();
preserveRepositoryAuditTrails();
schedule.finish();
log.info("Finished preservation. Scheduled new preservation task to start {}", getNextScheduledRun());
}
log.info("Starting preservation of audit trails.");
schedule.start();
preserveRepositoryAuditTrails();
schedule.finish();
log.info("Finished preservation. Scheduled new preservation task to start {}", getNextScheduledRun());
}
}
}