Files
Athou_commafeed/src/main/java/com/commafeed/backend/feeds/FeedRefreshUpdater.java

222 lines
6.3 KiB
Java
Raw Normal View History

package com.commafeed.backend.feeds;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
2013-05-22 00:07:13 +02:00
import java.util.List;
2013-05-22 21:57:53 +02:00
import java.util.concurrent.TimeUnit;
2013-05-27 10:44:43 +02:00
import java.util.concurrent.locks.Lock;
2013-05-22 21:57:53 +02:00
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.CollectionUtils;
2013-05-27 22:52:14 +02:00
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
2013-05-20 14:06:09 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.commafeed.backend.MetricsBean;
2013-06-28 17:02:29 +02:00
import com.commafeed.backend.cache.CacheService;
import com.commafeed.backend.dao.FeedDAO;
import com.commafeed.backend.dao.FeedEntryDAO;
2013-05-22 00:07:13 +02:00
import com.commafeed.backend.dao.FeedSubscriptionDAO;
import com.commafeed.backend.feeds.FeedRefreshExecutor.Task;
2013-05-22 21:57:53 +02:00
import com.commafeed.backend.model.ApplicationSettings;
import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedEntry;
2013-07-25 16:41:48 +02:00
import com.commafeed.backend.model.FeedEntryContent;
2013-05-22 00:07:13 +02:00
import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.model.User;
2013-05-20 14:06:09 +02:00
import com.commafeed.backend.pubsubhubbub.SubscriptionHandler;
2013-05-21 07:09:48 +02:00
import com.commafeed.backend.services.ApplicationSettingsService;
import com.commafeed.backend.services.FeedUpdateService;
import com.google.api.client.util.Lists;
2013-05-27 10:44:43 +02:00
import com.google.common.util.concurrent.Striped;
2013-05-24 09:21:20 +02:00
@ApplicationScoped
public class FeedRefreshUpdater {
2013-07-25 09:17:33 +02:00
protected static Logger log = LoggerFactory.getLogger(FeedRefreshUpdater.class);
2013-05-20 14:06:09 +02:00
@Inject
FeedUpdateService feedUpdateService;
2013-05-20 14:06:09 +02:00
@Inject
SubscriptionHandler handler;
2013-05-22 00:07:13 +02:00
@Inject
FeedRefreshTaskGiver taskGiver;
@Inject
FeedDAO feedDAO;
2013-05-21 07:09:48 +02:00
@Inject
ApplicationSettingsService applicationSettingsService;
2013-05-27 10:44:43 +02:00
@Inject
MetricsBean metricsBean;
2013-05-21 07:09:48 +02:00
2013-05-22 00:07:13 +02:00
@Inject
FeedSubscriptionDAO feedSubscriptionDAO;
@Inject
FeedEntryDAO feedEntryDAO;
2013-06-28 17:02:29 +02:00
@Inject
CacheService cache;
private FeedRefreshExecutor pool;
2013-05-27 10:44:43 +02:00
private Striped<Lock> locks;
2013-05-22 21:57:53 +02:00
@PostConstruct
public void init() {
ApplicationSettings settings = applicationSettingsService.get();
2013-05-23 06:38:58 +02:00
int threads = Math.max(settings.getDatabaseUpdateThreads(), 1);
2013-07-28 16:36:21 +02:00
pool = new FeedRefreshExecutor("feed-refresh-updater", threads, Math.min(50 * threads, 1000));
2013-06-07 15:54:47 +02:00
locks = Striped.lazyWeakLock(threads * 100000);
2013-05-22 21:57:53 +02:00
}
@PreDestroy
2013-05-24 12:28:48 +02:00
public void shutdown() {
pool.shutdown();
2013-05-24 12:28:48 +02:00
}
public void updateFeed(FeedRefreshContext context) {
pool.execute(new EntryTask(context));
2013-05-22 21:57:53 +02:00
}
private class EntryTask implements Task {
2013-05-22 21:57:53 +02:00
private FeedRefreshContext context;
2013-05-22 21:57:53 +02:00
public EntryTask(FeedRefreshContext context) {
this.context = context;
}
2013-05-22 00:07:13 +02:00
2013-05-22 21:57:53 +02:00
@Override
public void run() {
boolean ok = true;
Feed feed = context.getFeed();
List<FeedEntry> entries = context.getEntries();
2013-05-30 13:51:18 +02:00
if (entries.isEmpty() == false) {
2013-06-30 07:44:59 +02:00
List<String> lastEntries = cache.getLastEntries(feed);
List<String> currentEntries = Lists.newArrayList();
2013-06-30 07:44:59 +02:00
List<FeedSubscription> subscriptions = null;
2013-05-22 21:57:53 +02:00
for (FeedEntry entry : entries) {
2013-07-02 18:07:08 +02:00
String cacheKey = cache.buildUniqueEntryKey(feed, entry);
if (!lastEntries.contains(cacheKey)) {
log.debug("cache miss for {}", entry.getUrl());
2013-06-30 07:44:59 +02:00
if (subscriptions == null) {
2013-07-25 09:17:33 +02:00
subscriptions = feedSubscriptionDAO.findByFeed(feed);
2013-06-30 07:44:59 +02:00
}
ok &= addEntry(feed, entry, subscriptions);
metricsBean.entryCacheMiss();
} else {
log.debug("cache hit for {}", entry.getUrl());
metricsBean.entryCacheHit();
}
currentEntries.add(cacheKey);
2013-05-22 21:57:53 +02:00
}
cache.setLastEntries(feed, currentEntries);
if (CollectionUtils.isNotEmpty(subscriptions)) {
List<User> users = Lists.newArrayList();
for (FeedSubscription sub : subscriptions) {
users.add(sub.getUser());
}
cache.invalidateUnreadCount(subscriptions.toArray(new FeedSubscription[0]));
cache.invalidateUserRootCategory(users.toArray(new User[0]));
}
2013-05-22 21:57:53 +02:00
}
if (applicationSettingsService.get().isPubsubhubbub()) {
handlePubSub(feed);
}
if (!ok) {
// requeue asap
feed.setDisabledUntil(new Date(0));
}
metricsBean.feedUpdated();
2013-05-22 21:57:53 +02:00
taskGiver.giveBack(feed);
2013-05-21 07:09:48 +02:00
}
@Override
public boolean isUrgent() {
return context.isUrgent();
}
2013-05-20 14:06:09 +02:00
}
2013-07-25 09:17:33 +02:00
private boolean addEntry(final Feed feed, final FeedEntry entry, final List<FeedSubscription> subscriptions) {
2013-06-07 15:54:47 +02:00
boolean success = false;
// lock on feed, make sure we are not updating the same feed twice at
// the same time
String key1 = StringUtils.trimToEmpty("" + feed.getId());
2013-07-25 16:41:48 +02:00
// lock on content, make sure we are not updating the same entry
// twice at the same time
2013-07-25 16:41:48 +02:00
FeedEntryContent content = entry.getContent();
2013-07-26 08:15:23 +02:00
String key2 = DigestUtils.sha1Hex(StringUtils.trimToEmpty(content.getContent() + content.getTitle()));
2013-07-25 09:17:33 +02:00
Iterator<Lock> iterator = locks.bulkGet(Arrays.asList(key1, key2)).iterator();
Lock lock1 = iterator.next();
Lock lock2 = iterator.next();
boolean locked1 = false;
boolean locked2 = false;
2013-05-27 10:44:43 +02:00
try {
locked1 = lock1.tryLock(1, TimeUnit.MINUTES);
locked2 = lock2.tryLock(1, TimeUnit.MINUTES);
if (locked1 && locked2) {
boolean inserted = feedUpdateService.addEntry(feed, entry);
if (inserted) {
metricsBean.entryInserted();
}
2013-06-07 15:54:47 +02:00
success = true;
2013-05-27 16:41:29 +02:00
} else {
log.error("lock timeout for " + feed.getUrl() + " - " + key1);
2013-05-27 16:41:29 +02:00
}
} catch (InterruptedException e) {
2013-07-25 09:17:33 +02:00
log.error("interrupted while waiting for lock for " + feed.getUrl() + " : " + e.getMessage(), e);
2013-05-27 10:44:43 +02:00
} finally {
if (locked1) {
lock1.unlock();
}
if (locked2) {
lock2.unlock();
}
2013-05-27 10:44:43 +02:00
}
2013-06-07 15:54:47 +02:00
return success;
}
2013-05-21 12:41:57 +02:00
private void handlePubSub(final Feed feed) {
2013-06-05 21:50:26 +02:00
if (feed.getPushHub() != null && feed.getPushTopic() != null) {
Date lastPing = feed.getPushLastPing();
Date now = new Date();
if (lastPing == null || lastPing.before(DateUtils.addDays(now, -3))) {
new Thread() {
@Override
public void run() {
handler.subscribe(feed);
}
}.start();
}
2013-05-20 14:06:09 +02:00
}
}
2013-05-27 11:20:19 +02:00
public int getQueueSize() {
return pool.getQueueSize();
2013-05-27 11:20:19 +02:00
}
2013-07-05 09:52:23 +02:00
public int getActiveCount() {
return pool.getActiveCount();
}
}