Skip to content
This repository has been archived by the owner on Jun 8, 2020. It is now read-only.

[Binance] Orderbook initial snapshot #227

Merged

Conversation

badgerwithagun
Copy link
Collaborator

@badgerwithagun badgerwithagun commented Oct 8, 2018

Resolves #86. Explanation given on there.

…change dependency to 4.3.9, needed for acceess to convert order book
…itions and simplified the cleanup logic. Also added some commentary here since the way this is working, and why it's working that way, isn't massively obvious. There is definitely a thread leak here (BinanceManualExample never completes) which could explain some memory leaks I'm seeing, but will tackle that separately.
… up partial snapshots, we'll just keep trying to create the initial snapshot, ignoring any updates, until the snapshot is created successfully. Means we can guarantee all the updates contain correct, full order books.
badgerwithagun added a commit to badgerwithagun/xchange-stream that referenced this pull request Oct 8, 2018
@badgerwithagun badgerwithagun changed the title Binance orderbook initial snapshot Binance: Orderbook initial snapshot Oct 8, 2018
@badgerwithagun badgerwithagun changed the title Binance: Orderbook initial snapshot Binance: Orderbook initial snapshot [BUG FIX] Oct 8, 2018
@badgerwithagun badgerwithagun changed the title Binance: Orderbook initial snapshot [BUG FIX] [Binance] Orderbook initial snapshot [BUG FIX] Oct 8, 2018
@badgerwithagun badgerwithagun changed the title [Binance] Orderbook initial snapshot [BUG FIX] [Binance] Orderbook initial snapshot Oct 8, 2018
@Flemingjp Flemingjp added this to the 4.3.11 milestone Oct 8, 2018
@davidjirovec
Copy link
Contributor

I am afraid there is some memory leak. When I modify manual example like following

public static void main(String[] args) throws InterruptedException {
    final ExchangeSpecification exchangeSpecification = new ExchangeSpecification(BinanceStreamingExchange.class);
    exchangeSpecification.setShouldLoadRemoteMetaData(true);
    StreamingExchange exchange = StreamingExchangeFactory.INSTANCE.createExchange(exchangeSpecification);
    ProductSubscription subscription = exchange.getExchangeSymbols().stream().limit(50).reduce(ProductSubscription.create(), ProductSubscription.ProductSubscriptionBuilder::addOrderbook, (productSubscriptionBuilder, productSubscriptionBuilder2) -> {
        throw new UnsupportedOperationException();
    }).build();
    exchange.connect(subscription).blockingAwait();
    Thread.sleep(MAX_VALUE);
}

After 45 minutes memory usage look like this, pattern keeps growing like this even when "Perform GC" is clicked and eventually it would run out of memory.
heap

@badgerwithagun
Copy link
Collaborator Author

@davidjirovec : drat. Thanks for that report. Really useful. Did you confirm that this wasn't occurring beforehand?

I'll look into this in the next couple of days.

@badgerwithagun
Copy link
Collaborator Author

Just checked my production app and it has been quietly cycling every few hours due to OOM errors, but only slightly more often than it always has. There's definitely already a memory leak somewhere. If this has made it worse, that provides some clues perhaps...

@davidjirovec
Copy link
Contributor

I didn't use xchange-stream binance until this PR. You used some previous version even when it gave you OOM? :)

@badgerwithagun
Copy link
Collaborator Author

Background thread issuing a System.exit() every few hours and the container restarts itself.

Filth I know, but I have had more important things to worry about. Memory leaks are somewhere below Urgent.

I will look at this properly now tho.

@davidjirovec
Copy link
Contributor

Ok, I understand :)

@badgerwithagun
Copy link
Collaborator Author

badgerwithagun commented Oct 11, 2018

[snipped] - see below

1 similar comment
@badgerwithagun
Copy link
Collaborator Author

badgerwithagun commented Oct 11, 2018

[snipped] - see below

@badgerwithagun
Copy link
Collaborator Author

badgerwithagun commented Oct 14, 2018

Confirmed. This memory leak goes away when #191 is merged.

@davidjirovec , if you'd like to confirm yourself, compare the results of running your test against this branch (binance-orderbook-initial-snapshot) and develop-fork, both on my fork. The only real material differences between the branches are that the latter has some slightly newer dependencies (but I've tested and they aren't having any effect) and that the latter contains #191, which cleans up connection/reconnection issues.

binance-orderbook-initial-snapshot:

image

develop-fork:

image

In short, I think this PR is OK. It's just (yet) another reason why #191 needs to be merged.

@davidjirovec
Copy link
Contributor

Thanks, I will test it. develop-fork should be handling orderbooks correctly? Because it to me it seems the code is missing. But I've found your branch develop-fork-develop (wow :) ) and to me it seems it contains both fixes for connection issues and orderbook handling? But maybe I am not right, it is complicated :)

@badgerwithagun
Copy link
Collaborator Author

badgerwithagun commented Oct 14, 2018

Thanks, I will test it. develop-fork should be handling orderbooks correctly? Because it to me it seems the code is missing. But I've found your branch develop-fork-develop (wow :) ) and to me it seems it contains both fixes for connection issues and orderbook handling? But maybe I am not right, it is complicated :)

You should find that develop-fork produces a flat memory profile, but binance-orderbook-initial-snapshot shows the leak. This shows (to me at least) that the leak has nothing to do with this PR but is fixed by #191.

@badgerwithagun
Copy link
Collaborator Author

Sorry @davidjirovec, you were quite right, develop-fork was missing this PR. So many forks. I've pushed the update now, but don't run your tests yet. I'm just double-checking. I'm certain I tested this on develop-fork-develop too, but now I'm doubting myself.

@badgerwithagun
Copy link
Collaborator Author

I was indeed confusing myself. I've now replicated this on develop-fork too.

OK, I will go back to trying to work out what the cause is.

@badgerwithagun
Copy link
Collaborator Author

badgerwithagun commented Oct 15, 2018

@davidjirovec - found it, fixed

image

Highlighted is a manual GC.

@davidjirovec
Copy link
Contributor

Tested myself, looks good!

@badgerwithagun
Copy link
Collaborator Author

@davidjirovec, are you able to post an accepting review (see the Review Required section below)? Then we can get it merged.

@davidjirovec
Copy link
Contributor

Says "At least 1 approving review is required by reviewers with write access." :-/

@Flemingjp
Copy link
Collaborator

@davidjirovec It just needs someone with write access to also accept - I can sort this out for you

Copy link
Collaborator

@Flemingjp Flemingjp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based off approving review @davidjirovec

@Flemingjp Flemingjp merged commit 09a7cf6 into bitrich-info:develop Oct 16, 2018
@badgerwithagun badgerwithagun deleted the binance-orderbook-initial-snapshot branch October 28, 2018 16:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants