remove many to many relationship between entries and feeds

This commit is contained in:
Athou
2013-07-24 15:39:20 +02:00
parent fdacac74cc
commit c2b53b117c
18 changed files with 350 additions and 251 deletions

View File

@@ -1,7 +1,9 @@
package com.commafeed.backend.feeds;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
@@ -11,6 +13,7 @@ import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
@@ -72,7 +75,8 @@ public class FeedRefreshUpdater {
public void init() {
ApplicationSettings settings = applicationSettingsService.get();
int threads = Math.max(settings.getDatabaseUpdateThreads(), 1);
pool = new FeedRefreshExecutor("feed-refresh-updater", threads, 500 * threads);
pool = new FeedRefreshExecutor("feed-refresh-updater", threads,
500 * threads);
locks = Striped.lazyWeakLock(threads * 100000);
}
@@ -112,7 +116,7 @@ public class FeedRefreshUpdater {
subscriptions = feedSubscriptionDAO
.findByFeed(feed);
}
ok &= updateEntry(feed, entry, subscriptions);
ok &= addEntry(feed, entry, subscriptions);
metricsBean.entryCacheMiss();
} else {
log.debug("cache hit for {}", entry.getUrl());
@@ -139,27 +143,42 @@ public class FeedRefreshUpdater {
}
}
private boolean updateEntry(final Feed feed, final FeedEntry entry,
private boolean addEntry(final Feed feed, final FeedEntry entry,
final List<FeedSubscription> subscriptions) {
boolean success = false;
String key = StringUtils.trimToEmpty(entry.getGuid() + entry.getUrl());
Lock lock = locks.get(key);
boolean locked = false;
// lock on feed, make sure we are not updating the same feed twice at
// the same time
String key1 = StringUtils.trimToEmpty("" + feed.getId());
// lock on content hash, make sure we are not updating the same entry
// twice at the same time
String key2 = DigestUtils.sha1Hex(entry.getContent().getContent());
Iterator<Lock> iterator = locks.bulkGet(Arrays.asList(key1, key2))
.iterator();
Lock lock1 = iterator.next();
Lock lock2 = iterator.next();
boolean locked1 = false;
boolean locked2 = false;
try {
locked = lock.tryLock(1, TimeUnit.MINUTES);
if (locked) {
locked1 = lock1.tryLock(1, TimeUnit.MINUTES);
locked2 = lock2.tryLock(1, TimeUnit.MINUTES);
if (locked1 && locked2) {
feedUpdateService.updateEntry(feed, entry, subscriptions);
success = true;
} else {
log.error("lock timeout for " + feed.getUrl() + " - " + key);
log.error("lock timeout for " + feed.getUrl() + " - " + key1);
}
} catch (InterruptedException e) {
log.error("interrupted while waiting for lock for " + feed.getUrl()
+ " : " + e.getMessage(), e);
} finally {
if (locked) {
lock.unlock();
if (locked1) {
lock1.unlock();
}
if (locked2) {
lock2.unlock();
}
}
return success;