Add PriorityStoragePayload interfaces to delay processing of Offer and TradeStatistic items at startup to avoid that the UI get stuck by performance peaks.

This commit is contained in:
Manfred Karrer 2016-08-06 13:37:49 +02:00
parent b438239ebe
commit 9ec9c7ec6e
5 changed files with 38 additions and 14 deletions

View File

@ -28,8 +28,8 @@ import io.bitsquare.common.handlers.ErrorMessageHandler;
import io.bitsquare.common.handlers.ResultHandler;
import io.bitsquare.common.util.JsonExclude;
import io.bitsquare.p2p.NodeAddress;
import io.bitsquare.p2p.storage.payload.Priority1StoragePayload;
import io.bitsquare.p2p.storage.payload.RequiresOwnerIsOnlinePayload;
import io.bitsquare.p2p.storage.payload.StoragePayload;
import io.bitsquare.payment.PaymentMethod;
import io.bitsquare.trade.exceptions.MarketPriceNotAvailableException;
import io.bitsquare.trade.exceptions.TradePriceOutOfToleranceException;
@ -53,7 +53,8 @@ import java.util.concurrent.TimeUnit;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
public final class Offer implements StoragePayload, RequiresOwnerIsOnlinePayload {
public final class Offer implements Priority1StoragePayload, RequiresOwnerIsOnlinePayload {
///////////////////////////////////////////////////////////////////////////////////////////
// Static
///////////////////////////////////////////////////////////////////////////////////////////

View File

@ -5,8 +5,7 @@ import io.bitsquare.app.Version;
import io.bitsquare.common.crypto.PubKeyRing;
import io.bitsquare.common.util.JsonExclude;
import io.bitsquare.p2p.storage.payload.CapabilityRequiringPayload;
import io.bitsquare.p2p.storage.payload.LazyProcessedPayload;
import io.bitsquare.p2p.storage.payload.StoragePayload;
import io.bitsquare.p2p.storage.payload.Priority2StoragePayload;
import io.bitsquare.trade.offer.Offer;
import org.bitcoinj.core.Coin;
import org.bitcoinj.utils.ExchangeRate;
@ -20,7 +19,7 @@ import java.util.List;
import java.util.concurrent.TimeUnit;
@Immutable
public final class TradeStatistics implements StoragePayload, CapabilityRequiringPayload, LazyProcessedPayload {
public final class TradeStatistics implements Priority2StoragePayload, CapabilityRequiringPayload {
@JsonExclude
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
@JsonExclude

View File

@ -18,7 +18,8 @@ import io.bitsquare.p2p.peers.getdata.messages.GetDataResponse;
import io.bitsquare.p2p.peers.getdata.messages.GetUpdatedDataRequest;
import io.bitsquare.p2p.peers.getdata.messages.PreliminaryGetDataRequest;
import io.bitsquare.p2p.storage.P2PDataStorage;
import io.bitsquare.p2p.storage.payload.LazyProcessedPayload;
import io.bitsquare.p2p.storage.payload.Priority1StoragePayload;
import io.bitsquare.p2p.storage.payload.Priority2StoragePayload;
import io.bitsquare.p2p.storage.payload.StoragePayload;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -173,20 +174,21 @@ public class RequestDataHandler implements MessageListener {
final NodeAddress sender = connection.getPeersNodeAddressOptional().get();
// The non LazyProcessedPayload items we process directly (mainly Offers)
// The non PriorityStoragePayload items we process directly
getDataResponse.dataSet.stream()
.filter(e -> !(e.getStoragePayload() instanceof LazyProcessedPayload))
.filter(e -> !(e.getStoragePayload() instanceof Priority1StoragePayload) &&
!(e.getStoragePayload() instanceof Priority2StoragePayload))
.forEach(protectedStorageEntry -> {
// We dont broadcast here as we are only connected to the seed node and would be pointless
dataStorage.add(protectedStorageEntry, sender, null, false, false);
});
// The LazyProcessedPayload items we process delayed (TradeStatistics)
// The Priority1StoragePayload items we process with a short delay (Offers)
final long[] i = {0};
getDataResponse.dataSet.stream()
.filter(e -> e.getStoragePayload() instanceof LazyProcessedPayload)
.filter(e -> e.getStoragePayload() instanceof Priority1StoragePayload)
.forEach(protectedStorageEntry -> {
i[0] += 100;
i[0] += 50;
// We don't want the UI get stuck when processing 100s of entries.
// The dataStorage.add call is a bit expensive as sig checks is done there
UserThread.runAfter(() -> {
@ -195,6 +197,22 @@ public class RequestDataHandler implements MessageListener {
},
i[0], TimeUnit.MILLISECONDS);
});
// The Priority2StoragePayload items we process with a longer delay (TradeStatistics)
final long[] n = {0};
getDataResponse.dataSet.stream()
.filter(e -> e.getStoragePayload() instanceof Priority2StoragePayload)
.forEach(protectedStorageEntry -> {
n[0] += 100;
// We don't want the UI get stuck when processing 100s of entries.
// The dataStorage.add call is a bit expensive as sig checks is done there
UserThread.runAfter(() -> {
// We dont broadcast here as we are only connected to the seed node and would be pointless
dataStorage.add(protectedStorageEntry, sender, null, false, false);
},
n[0], TimeUnit.MILLISECONDS);
});
cleanup();
listener.onComplete();
} else {

View File

@ -0,0 +1,8 @@
package io.bitsquare.p2p.storage.payload;
/**
* Marker interface for payload which gets delayed processed at startup so we don't hit performance too much.
* Used for Offer.
*/
public interface Priority1StoragePayload extends StoragePayload {
}

View File

@ -1,10 +1,8 @@
package io.bitsquare.p2p.storage.payload;
import io.bitsquare.common.wire.Payload;
/**
* Marker interface for payload which gets delayed processed at startup so we don't hit performance too much.
* Used for TradeStatistics.
*/
public interface LazyProcessedPayload extends Payload {
public interface Priority2StoragePayload extends StoragePayload {
}