🎨 Declare return value for ticker explicitely
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneInserter.java
1 /*
2  * Sone - SoneInserter.java - Copyright Â© 2010–2019 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.core;
19
20 import static java.lang.String.format;
21 import static java.lang.System.currentTimeMillis;
22 import static java.util.concurrent.TimeUnit.*;
23 import static java.util.logging.Logger.getLogger;
24 import static net.pterodactylus.sone.data.Album.NOT_EMPTY;
25
26 import java.io.Closeable;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.io.StringWriter;
30 import java.nio.charset.Charset;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.Map;
34 import java.util.Set;
35 import java.util.concurrent.*;
36 import java.util.concurrent.atomic.AtomicInteger;
37 import java.util.logging.Level;
38 import java.util.logging.Logger;
39
40 import com.codahale.metrics.*;
41 import com.google.common.base.*;
42 import net.pterodactylus.sone.core.SoneModificationDetector.LockableFingerprintProvider;
43 import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent;
44 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
45 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
46 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
47 import net.pterodactylus.sone.data.Album;
48 import net.pterodactylus.sone.data.Post;
49 import net.pterodactylus.sone.data.Reply;
50 import net.pterodactylus.sone.data.Sone;
51 import net.pterodactylus.sone.data.Sone.SoneStatus;
52 import net.pterodactylus.sone.main.SonePlugin;
53 import net.pterodactylus.util.io.Closer;
54 import net.pterodactylus.util.service.AbstractService;
55 import net.pterodactylus.util.template.HtmlFilter;
56 import net.pterodactylus.util.template.ReflectionAccessor;
57 import net.pterodactylus.util.template.Template;
58 import net.pterodactylus.util.template.TemplateContext;
59 import net.pterodactylus.util.template.TemplateContextFactory;
60 import net.pterodactylus.util.template.TemplateException;
61 import net.pterodactylus.util.template.TemplateParser;
62 import net.pterodactylus.util.template.XmlFilter;
63
64 import com.google.common.annotations.VisibleForTesting;
65 import com.google.common.collect.FluentIterable;
66 import com.google.common.collect.Ordering;
67 import com.google.common.eventbus.EventBus;
68 import com.google.common.eventbus.Subscribe;
69
70 import freenet.keys.FreenetURI;
71 import freenet.support.api.Bucket;
72 import freenet.support.api.ManifestElement;
73 import freenet.support.api.RandomAccessBucket;
74 import freenet.support.io.ArrayBucket;
75
76 /**
77  * A Sone inserter is responsible for inserting a Sone if it has changed.
78  */
79 public class SoneInserter extends AbstractService {
80
81         /** The logger. */
82         private static final Logger logger = getLogger(SoneInserter.class.getName());
83
84         /** The insertion delay (in seconds). */
85         private static final AtomicInteger insertionDelay = new AtomicInteger(60);
86
87         /** The template factory used to create the templates. */
88         private static final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
89
90         static {
91                 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
92                 templateContextFactory.addFilter("xml", new XmlFilter());
93                 templateContextFactory.addFilter("html", new HtmlFilter());
94         }
95
96         /** The UTF-8 charset. */
97         private static final Charset utf8Charset = Charset.forName("UTF-8");
98
99         /** The core. */
100         private final Core core;
101
102         /** The event bus. */
103         private final EventBus eventBus;
104
105         /** The Freenet interface. */
106         private final FreenetInterface freenetInterface;
107
108         private final SoneModificationDetector soneModificationDetector;
109         private final long delay;
110         private final String soneId;
111         private final Histogram soneInsertDurationHistogram;
112         private final Meter soneInsertErrorMeter;
113
114         /**
115          * Creates a new Sone inserter.
116          *
117          * @param core
118          *            The core
119          * @param eventBus
120          *            The event bus
121          * @param freenetInterface
122          *            The freenet interface
123          * @param soneId
124          *            The ID of the Sone to insert
125          */
126         public SoneInserter(final Core core, EventBus eventBus, FreenetInterface freenetInterface, MetricRegistry metricRegistry, final String soneId) {
127                 this(core, eventBus, freenetInterface, metricRegistry, soneId, new SoneModificationDetector(new LockableFingerprintProvider() {
128                         @Override
129                         public boolean isLocked() {
130                                 Sone sone = core.getSone(soneId);
131                                 if (sone == null) {
132                                         return false;
133                                 }
134                                 return core.isLocked(sone);
135                         }
136
137                         @Override
138                         public String getFingerprint() {
139                                 Sone sone = core.getSone(soneId);
140                                 if (sone == null) {
141                                         return null;
142                                 }
143                                 return sone.getFingerprint();
144                         }
145                 }, insertionDelay), 1000);
146         }
147
148         @VisibleForTesting
149         SoneInserter(Core core, EventBus eventBus, FreenetInterface freenetInterface, MetricRegistry metricRegistry, String soneId, SoneModificationDetector soneModificationDetector, long delay) {
150                 super("Sone Inserter for â€ś" + soneId + "”", false);
151                 this.core = core;
152                 this.eventBus = eventBus;
153                 this.freenetInterface = freenetInterface;
154                 this.soneInsertDurationHistogram = metricRegistry.histogram("sone.insert.duration", () -> new Histogram(new ExponentiallyDecayingReservoir(3000, 0)));
155                 this.soneInsertErrorMeter = metricRegistry.meter("sone.insert.errors");
156                 this.soneId = soneId;
157                 this.soneModificationDetector = soneModificationDetector;
158                 this.delay = delay;
159         }
160
161         //
162         // ACCESSORS
163         //
164
165         @VisibleForTesting
166         static AtomicInteger getInsertionDelay() {
167                 return insertionDelay;
168         }
169
170         /**
171          * Changes the insertion delay, i.e. the time the Sone inserter waits after it
172          * has noticed a Sone modification before it starts the insert.
173          *
174          * @param insertionDelay
175          *            The insertion delay (in seconds)
176          */
177         private static void setInsertionDelay(int insertionDelay) {
178                 SoneInserter.insertionDelay.set(insertionDelay);
179         }
180
181         /**
182          * Returns the fingerprint of the last insert.
183          *
184          * @return The fingerprint of the last insert
185          */
186         public String getLastInsertFingerprint() {
187                 return soneModificationDetector.getLastInsertFingerprint();
188         }
189
190         /**
191          * Sets the fingerprint of the last insert.
192          *
193          * @param lastInsertFingerprint
194          *            The fingerprint of the last insert
195          */
196         public void setLastInsertFingerprint(String lastInsertFingerprint) {
197                 soneModificationDetector.setFingerprint(lastInsertFingerprint);
198         }
199
200         /**
201          * Returns whether the Sone inserter has detected a modification of the
202          * Sone.
203          *
204          * @return {@code true} if the Sone has been modified, {@code false}
205          *         otherwise
206          */
207         public boolean isModified() {
208                 return soneModificationDetector.isModified();
209         }
210
211         //
212         // SERVICE METHODS
213         //
214
215         /**
216          * {@inheritDoc}
217          */
218         @Override
219         protected void serviceRun() {
220                 while (!shouldStop()) {
221                         try {
222                                 /* check every second. */
223                                 sleep(delay);
224
225                                 if (soneModificationDetector.isEligibleForInsert()) {
226                                         Sone sone = core.getSone(soneId);
227                                         if (sone == null) {
228                                                 logger.log(Level.WARNING, format("Sone %s has disappeared, exiting inserter.", soneId));
229                                                 return;
230                                         }
231                                         InsertInformation insertInformation = new InsertInformation(sone);
232                                         logger.log(Level.INFO, String.format("Inserting Sone â€ś%s”…", sone.getName()));
233
234                                         boolean success = false;
235                                         try {
236                                                 sone.setStatus(SoneStatus.inserting);
237                                                 long insertTime = currentTimeMillis();
238                                                 eventBus.post(new SoneInsertingEvent(sone));
239                                                 Stopwatch stopwatch = Stopwatch.createStarted();
240                                                 FreenetURI finalUri = freenetInterface.insertDirectory(sone.getInsertUri(), insertInformation.generateManifestEntries(), "index.html");
241                                                 stopwatch.stop();
242                                                 soneInsertDurationHistogram.update(stopwatch.elapsed(MICROSECONDS));
243                                                 eventBus.post(new SoneInsertedEvent(sone, stopwatch.elapsed(MILLISECONDS), insertInformation.getFingerprint()));
244                                                 /* at this point we might already be stopped. */
245                                                 if (shouldStop()) {
246                                                         /* if so, bail out, don’t change anything. */
247                                                         break;
248                                                 }
249                                                 sone.setTime(insertTime);
250                                                 sone.setLatestEdition(finalUri.getEdition());
251                                                 core.touchConfiguration();
252                                                 success = true;
253                                                 logger.log(Level.INFO, String.format("Inserted Sone â€ś%s” at %s.", sone.getName(), finalUri));
254                                         } catch (SoneException se1) {
255                                                 soneInsertErrorMeter.mark();
256                                                 eventBus.post(new SoneInsertAbortedEvent(sone, se1));
257                                                 logger.log(Level.WARNING, String.format("Could not insert Sone â€ś%s”!", sone.getName()), se1);
258                                         } finally {
259                                                 insertInformation.close();
260                                                 sone.setStatus(SoneStatus.idle);
261                                         }
262
263                                         /*
264                                          * reset modification counter if Sone has not been modified
265                                          * while it was inserted.
266                                          */
267                                         if (success) {
268                                                 synchronized (sone) {
269                                                         if (insertInformation.getFingerprint().equals(sone.getFingerprint())) {
270                                                                 logger.log(Level.FINE, String.format("Sone â€ś%s” was not modified further, resetting counter…", sone));
271                                                                 soneModificationDetector.setFingerprint(insertInformation.getFingerprint());
272                                                                 core.touchConfiguration();
273                                                         }
274                                                 }
275                                         }
276                                 }
277                         } catch (Throwable t1) {
278                                 logger.log(Level.SEVERE, "SoneInserter threw an Exception!", t1);
279                         }
280                 }
281         }
282
283         @Subscribe
284         public void insertionDelayChanged(InsertionDelayChangedEvent insertionDelayChangedEvent) {
285                 setInsertionDelay(insertionDelayChangedEvent.getInsertionDelay());
286         }
287
288         /**
289          * Container for information that are required to insert a Sone. This
290          * container merely exists to copy all relevant data without holding a lock
291          * on the {@link Sone} object for too long.
292          */
293         @VisibleForTesting
294         class InsertInformation implements Closeable {
295
296                 /** All properties of the Sone, copied for thread safety. */
297                 private final Map<String, Object> soneProperties = new HashMap<>();
298                 private final String fingerprint;
299                 private final ManifestCreator manifestCreator;
300
301                 /**
302                  * Creates a new insert information container.
303                  *
304                  * @param sone
305                  *            The sone to insert
306                  */
307                 public InsertInformation(Sone sone) {
308                         this.fingerprint = sone.getFingerprint();
309                         Map<String, Object> soneProperties = new HashMap<>();
310                         soneProperties.put("id", sone.getId());
311                         soneProperties.put("name", sone.getName());
312                         soneProperties.put("time", currentTimeMillis());
313                         soneProperties.put("requestUri", sone.getRequestUri());
314                         soneProperties.put("profile", sone.getProfile());
315                         soneProperties.put("posts", Ordering.from(Post.NEWEST_FIRST).sortedCopy(sone.getPosts()));
316                         soneProperties.put("replies", Ordering.from(Reply.TIME_COMPARATOR).reverse().sortedCopy(sone.getReplies()));
317                         soneProperties.put("likedPostIds", new HashSet<>(sone.getLikedPostIds()));
318                         soneProperties.put("likedReplyIds", new HashSet<>(sone.getLikedReplyIds()));
319                         soneProperties.put("albums", FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).filter(NOT_EMPTY).toList());
320                         manifestCreator = new ManifestCreator(core, soneProperties);
321                 }
322
323                 //
324                 // ACCESSORS
325                 //
326
327                 @VisibleForTesting
328                 String getFingerprint() {
329                         return fingerprint;
330                 }
331
332                 //
333                 // ACTIONS
334                 //
335
336                 /**
337                  * Generates all manifest entries required to insert this Sone.
338                  *
339                  * @return The manifest entries for the Sone insert
340                  */
341                 public HashMap<String, Object> generateManifestEntries() {
342                         HashMap<String, Object> manifestEntries = new HashMap<>();
343
344                         /* first, create an index.html. */
345                         manifestEntries.put("index.html", manifestCreator.createManifestElement(
346                                         "index.html", "text/html; charset=utf-8",
347                                         "/templates/insert/index.html"));
348
349                         /* now, store the sone. */
350                         manifestEntries.put("sone.xml", manifestCreator.createManifestElement(
351                                         "sone.xml", "text/xml; charset=utf-8",
352                                         "/templates/insert/sone.xml"));
353
354                         return manifestEntries;
355                 }
356
357                 @Override
358                 public void close() {
359                         manifestCreator.close();
360                 }
361
362         }
363
364         /**
365          * Creates manifest elements for an insert by rendering a template.
366          */
367         @VisibleForTesting
368         static class ManifestCreator implements Closeable {
369
370                 private final Core core;
371                 private final Map<String, Object> soneProperties;
372                 private final Set<Bucket> buckets = new HashSet<>();
373
374                 ManifestCreator(Core core, Map<String, Object> soneProperties) {
375                         this.core = core;
376                         this.soneProperties = soneProperties;
377                 }
378
379                 public ManifestElement createManifestElement(String name, String contentType, String templateName) {
380                         InputStreamReader templateInputStreamReader = null;
381                         InputStream templateInputStream = null;
382                         Template template;
383                         try {
384                                 templateInputStream = getClass().getResourceAsStream(templateName);
385                                 templateInputStreamReader = new InputStreamReader(templateInputStream, utf8Charset);
386                                 template = TemplateParser.parse(templateInputStreamReader);
387                         } catch (TemplateException te1) {
388                                 logger.log(Level.SEVERE, String.format("Could not parse template â€ś%s”!", templateName), te1);
389                                 return null;
390                         } finally {
391                                 Closer.close(templateInputStreamReader);
392                                 Closer.close(templateInputStream);
393                         }
394
395                         TemplateContext templateContext = templateContextFactory.createTemplateContext();
396                         templateContext.set("core", core);
397                         templateContext.set("currentSone", soneProperties);
398                         templateContext.set("currentEdition", core.getUpdateChecker().getLatestEdition());
399                         templateContext.set("version", SonePlugin.getPluginVersion());
400                         StringWriter writer = new StringWriter();
401                         try {
402                                 template.render(templateContext, writer);
403                                 RandomAccessBucket bucket = new ArrayBucket(writer.toString().getBytes(Charsets.UTF_8));
404                                 buckets.add(bucket);
405                                 return new ManifestElement(name, bucket, contentType, bucket.size());
406                         } catch (TemplateException te1) {
407                                 logger.log(Level.SEVERE, String.format("Could not render template â€ś%s”!", templateName), te1);
408                                 return null;
409                         } finally {
410                                 Closer.close(writer);
411                         }
412                 }
413
414                 public void close() {
415                         for (Bucket bucket : buckets) {
416                                 bucket.free();
417                         }
418                 }
419
420         }
421
422 }