Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logging): add logs to FilterManager class #2908

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
38 changes: 27 additions & 11 deletions rskj-core/src/main/java/org/ethereum/rpc/FilterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,31 @@
import org.ethereum.core.TransactionReceipt;
import org.ethereum.facade.Ethereum;
import org.ethereum.listener.EthereumListenerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.concurrent.GuardedBy;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Created by ajlopez on 17/01/2018.
*/
public class FilterManager {
private static final long filterTimeout = 5 * 60 * 1000L; // 5 minutes in milliseconds
private static final long filterCleanupPeriod = 1 * 60 * 1000L; // 1 minute in milliseconds

private long latestFilterCleanup = System.currentTimeMillis();
private static final Logger logger = LoggerFactory.getLogger(FilterManager.class);

private final Object filterLock = new Object();
private static final long FILTER_TIMEOUT = Duration.ofMinutes(5).toMillis(); // 5 minutes in milliseconds
private static final long FILTER_CLEANUP_PERIOD = Duration.ofMinutes(1).toMillis(); // 1 minute in milliseconds

private AtomicInteger filterCounter = new AtomicInteger(1);
private final Object filterLock = new Object();
private final AtomicInteger filterCounter = new AtomicInteger(1);

@GuardedBy("filterLock")
private Map<Integer, Filter> installedFilters = new HashMap<>();
private final Map<Integer, Filter> installedFilters = new HashMap<>();

private long latestFilterCleanup = System.currentTimeMillis();

public FilterManager(Ethereum eth) {
eth.addListener(new EthereumListenerAdapter() {
Expand All @@ -66,17 +71,27 @@ public int registerFilter(Filter filter) {
int id = filterCounter.getAndIncrement();
installedFilters.put(id, filter);

logger.debug("[{}] installed with id: [{}]", filter.getClass().getSimpleName(), id);

return id;
}
}

public boolean removeFilter(int id) {
synchronized (filterLock) {
return installedFilters.remove(id) != null;
Filter filter = installedFilters.remove(id);
boolean removed = filter != null;
if (removed) {
logger.debug("[{}] with id: [{}] uninstalled", filter.getClass().getSimpleName(), id);
} else {
logger.debug("Cannot uninstalled filter with id: [{}] - not found", id);
}

return removed;
}
}

public Object[] getFilterEvents(int id, boolean newevents) {
public Object[] getFilterEvents(int id, boolean newEvents) {
synchronized (filterLock) {
filtersCleanup();

Expand All @@ -86,7 +101,7 @@ public Object[] getFilterEvents(int id, boolean newevents) {
throw filterNotFound("filter not found");
}

if (newevents) {
if (newEvents) {
return filter.getNewEvents();
}
else {
Expand Down Expand Up @@ -120,7 +135,7 @@ public void newPendingTx(List<Transaction> transactions) {
private void filtersCleanup() {
long now = System.currentTimeMillis();

if (latestFilterCleanup + filterCleanupPeriod > now) {
if (latestFilterCleanup + FILTER_CLEANUP_PERIOD > now) {
return;
}

Expand All @@ -129,8 +144,9 @@ private void filtersCleanup() {
for (Map.Entry<Integer, Filter> entry : installedFilters.entrySet()) {
Filter f = entry.getValue();

if (f.hasExpired(filterTimeout)) {
if (f.hasExpired(FILTER_TIMEOUT)) {
toremove.add(entry.getKey());
logger.debug("[{}] with id: [{}] expired", f.getClass().getSimpleName(), entry.getKey());
}
}

Expand Down
Loading