import javax.annotation.Nonnull;
import javax.annotation.Nullable;
+import com.codahale.metrics.*;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidAlbumFound;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidImageFound;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidParentAlbumFound;
/** The time the configuration was last touched. */
private volatile long lastConfigurationUpdate;
+ private final MetricRegistry metricRegistry;
+
/**
* Creates a new core.
*
* The database
*/
@Inject
- public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
+ public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database, MetricRegistry metricRegistry) {
super("Sone Core");
this.configuration = configuration;
this.freenetInterface = freenetInterface;
this.webOfTrustUpdater = webOfTrustUpdater;
this.eventBus = eventBus;
this.database = database;
+ this.metricRegistry = metricRegistry;
preferences = new Preferences(eventBus);
}
sone.setLatestEdition(fromNullable(tryParse(property)).or(0L));
sone.setClient(new Client("Sone", SonePlugin.getPluginVersion()));
sone.setKnown(true);
- SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId());
+ SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, metricRegistry, ownIdentity.getId());
soneInserter.insertionDelayChanged(new InsertionDelayChangedEvent(preferences.getInsertionDelay()));
eventBus.register(soneInserter);
synchronized (soneInserters) {
import static java.lang.String.format;
import static java.lang.System.currentTimeMillis;
+import static java.util.concurrent.TimeUnit.*;
import static java.util.logging.Logger.getLogger;
import static net.pterodactylus.sone.data.Album.NOT_EMPTY;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
+import com.codahale.metrics.*;
+import com.google.common.base.*;
import net.pterodactylus.sone.core.SoneModificationDetector.LockableFingerprintProvider;
import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent;
import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
import net.pterodactylus.util.template.XmlFilter;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Charsets;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Ordering;
import com.google.common.eventbus.EventBus;
private final SoneModificationDetector soneModificationDetector;
private final long delay;
private final String soneId;
+ private final Histogram soneInsertDurationHistogram;
/**
* Creates a new Sone inserter.
* @param soneId
* The ID of the Sone to insert
*/
- public SoneInserter(final Core core, EventBus eventBus, FreenetInterface freenetInterface, final String soneId) {
- this(core, eventBus, freenetInterface, soneId, new SoneModificationDetector(new LockableFingerprintProvider() {
+ public SoneInserter(final Core core, EventBus eventBus, FreenetInterface freenetInterface, MetricRegistry metricRegistry, final String soneId) {
+ this(core, eventBus, freenetInterface, metricRegistry, soneId, new SoneModificationDetector(new LockableFingerprintProvider() {
@Override
public boolean isLocked() {
Sone sone = core.getSone(soneId);
}
@VisibleForTesting
- SoneInserter(Core core, EventBus eventBus, FreenetInterface freenetInterface, String soneId, SoneModificationDetector soneModificationDetector, long delay) {
+ SoneInserter(Core core, EventBus eventBus, FreenetInterface freenetInterface, MetricRegistry metricRegistry, String soneId, SoneModificationDetector soneModificationDetector, long delay) {
super("Sone Inserter for “" + soneId + "”", false);
this.core = core;
this.eventBus = eventBus;
this.freenetInterface = freenetInterface;
+ this.soneInsertDurationHistogram = metricRegistry.histogram("sone.insert.duration");
this.soneId = soneId;
this.soneModificationDetector = soneModificationDetector;
this.delay = delay;
sone.setStatus(SoneStatus.inserting);
long insertTime = currentTimeMillis();
eventBus.post(new SoneInsertingEvent(sone));
+ Stopwatch stopwatch = Stopwatch.createStarted();
FreenetURI finalUri = freenetInterface.insertDirectory(sone.getInsertUri(), insertInformation.generateManifestEntries(), "index.html");
- eventBus.post(new SoneInsertedEvent(sone, currentTimeMillis() - insertTime, insertInformation.getFingerprint()));
+ stopwatch.stop();
+ soneInsertDurationHistogram.update(stopwatch.elapsed(MICROSECONDS));
+ eventBus.post(new SoneInsertedEvent(sone, stopwatch.elapsed(MILLISECONDS), insertInformation.getFingerprint()));
/* at this point we might already be stopped. */
if (shouldStop()) {
/* if so, bail out, don’t change anything. */
package net.pterodactylus.sone.core
+import com.codahale.metrics.*
import com.google.common.collect.*
import com.google.common.eventbus.*
import net.pterodactylus.sone.core.event.*
val webOfTrustUpdater = mock<WebOfTrustUpdater>()
val eventBus = mock<EventBus>()
val database = mock<Database>()
- val core = Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database)
+ val metricRegistry = MetricRegistry()
+ val core = Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database, metricRegistry)
val ownIdentity = mock<OwnIdentity>()
val identity = mock<Identity>()
whenever(identity.id).thenReturn("sone-id")
val updateChecker = mock<UpdateChecker>()
val webOfTrustUpdater = mock<WebOfTrustUpdater>()
val database = mock<Database>()
- return Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database)
+ val metricRegistry = MetricRegistry()
+ return Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database, metricRegistry)
}
}
package net.pterodactylus.sone.core
+import com.codahale.metrics.*
import com.google.common.base.*
import com.google.common.base.Optional
import com.google.common.eventbus.*
import org.mockito.stubbing.*
import java.lang.System.*
import java.util.*
+import kotlin.test.Test
/**
* Unit test for [SoneInserter] and its subclasses.
*/
class SoneInserterTest {
+ private val metricRegistry = MetricRegistry()
private val core = mock<Core>()
private val eventBus = mock<EventBus>()
private val freenetInterface = mock<FreenetInterface>()
@Test
fun `insertion delay is forwarded to sone inserter`() {
val eventBus = AsyncEventBus(directExecutor())
- eventBus.register(SoneInserter(core, eventBus, freenetInterface, "SoneId"))
+ eventBus.register(SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId"))
eventBus.post(InsertionDelayChangedEvent(15))
assertThat(SoneInserter.getInsertionDelay().get(), equalTo(15))
}
fun `isModified is true if modification detector says so`() {
val soneModificationDetector = mock<SoneModificationDetector>()
whenever(soneModificationDetector.isModified).thenReturn(true)
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
assertThat(soneInserter.isModified, equalTo(true))
}
@Test
fun `isModified is false if modification detector says so`() {
val soneModificationDetector = mock<SoneModificationDetector>()
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
assertThat(soneInserter.isModified, equalTo(false))
}
@Test
fun `last fingerprint is stored correctly`() {
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId")
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId")
soneInserter.lastInsertFingerprint = "last-fingerprint"
assertThat(soneInserter.lastInsertFingerprint, equalTo("last-fingerprint"))
}
@Test
fun `sone inserter stops when it should`() {
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId")
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId")
soneInserter.stop()
soneInserter.serviceRun()
}
val soneModificationDetector = mock<SoneModificationDetector>()
whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
whenever(freenetInterface.insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenReturn(finalUri)
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
doAnswer {
soneInserter.stop()
null
val sone = createSone(insertUri)
val soneModificationDetector = mock<SoneModificationDetector>()
whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
whenever(freenetInterface.insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenAnswer {
soneInserter.stop()
finalUri
val insertUri = mock<FreenetURI>()
createSone(insertUri)
val soneModificationDetector = mock<SoneModificationDetector>()
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
Thread(Runnable {
try {
Thread.sleep(500)
val sone = createSone(insertUri)
val soneModificationDetector = mock<SoneModificationDetector>()
whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
val soneException = SoneException(Exception())
whenever(freenetInterface.insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenAnswer {
soneInserter.stop()
@Test
fun `sone inserter exits if sone is unknown`() {
val soneModificationDetector = mock<SoneModificationDetector>()
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
whenever(core.getSone("SoneId")).thenReturn(null)
soneInserter.serviceRun()
@Test
fun `sone inserter catches exception and continues`() {
val soneModificationDetector = mock<SoneModificationDetector>()
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
val stopInserterAndThrowException = Answer<Optional<Sone>> {
soneInserter.stop()
throw NullPointerException()
nullValue())
}
+ @Test
+ fun `successful insert updates metrics`() {
+ val insertUri = mock<FreenetURI>()
+ val finalUri = mock<FreenetURI>()
+ createSone(insertUri)
+ val soneModificationDetector = mock<SoneModificationDetector>()
+ whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
+ whenever(freenetInterface.insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenReturn(finalUri)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry,"SoneId", soneModificationDetector, 1)
+ doAnswer {
+ soneInserter.stop()
+ null
+ }.`when`(core).touchConfiguration()
+ soneInserter.serviceRun()
+ val histogram = metricRegistry.histogram("sone.insert.duration")
+ assertThat(histogram.count, equalTo(1L))
+ }
+
+ @Test
+ fun `unsuccessful insert does not update metrics`() {
+ val insertUri = mock<FreenetURI>()
+ createSone(insertUri)
+ val soneModificationDetector = mock<SoneModificationDetector>()
+ whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
+ whenever(freenetInterface.insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenAnswer {
+ soneInserter.stop()
+ throw SoneException(Exception())
+ }
+ soneInserter.serviceRun()
+ val histogram = metricRegistry.histogram("sone.insert.duration")
+ assertThat(histogram.count, equalTo(0L))
+ }
+
}