You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@arrow.apache.org by "pablodcar (via GitHub)" <gi...@apache.org> on 2023/09/20 08:47:44 UTC
[GitHub] [arrow] pablodcar opened a new issue, #37801: Real Time Analytics: concat_tables memory and performance degradation
pablodcar opened a new issue, #37801:
URL: https://github.com/apache/arrow/issues/37801
### Describe the usage question you have. Please include as many useful details as possible.
Hi, I would like to use Arrow to maintain a couple of tables in memory to do analytics for arbitrary periods of time. My use case is to maintain currency rates and transactions in _real time_. It means the data is not loaded once and then analyzed but data is continuously being appended.
In my local testing, I init an empty table with a schema and then I add new rows as they are available. The issue I see is every `concat_tables` call is taking more time than the previous one and there seems to be a memory leak: the memory in the process is not freed, and if I use `combine_chunks` from time to time, things get worse.
Could you advise if this is a limitation of the tool (i.e. not being designed to continuously append data) or if I should use another approach? Thanks in advance
This is my Python code to reproduce the issue:
```python
import random
from datetime import datetime, timedelta, time
from time import perf_counter
import os
import psutil
import pyarrow as pa
PID = os.getpid()
RatesSchema = pa.schema(
[
pa.field("ts", pa.timestamp("us")),
pa.field("ccy1", pa.string()),
pa.field("ccy2", pa.string()),
pa.field("bid", pa.float64()),
pa.field("ask", pa.float64()),
]
)
# 5 currencies per cycle
COUNTER_CURRENCIES = ["EUR", "CHF", "JPY", "GBP", "SEK"]
start_time = datetime.combine(date=datetime.utcnow().date(), time=time.min)
# 60 points per second x 5 currencies = 300 records => 12M in 12 hours
cycles = int(3 * timedelta(hours=6).total_seconds())
timestamps = (start_time + timedelta(milliseconds=i) for i in range(cycles))
def add_rates():
rates_table = pa.Table.from_pylist([], schema=RatesSchema)
t0 = perf_counter()
for ts in timestamps:
ts_rates = fetch_rates(COUNTER_CURRENCIES, ts)
chunk = pa.Table.from_pylist(ts_rates)
rates_table = pa.concat_tables((rates_table, chunk))
if len(rates_table) % 5000 == 0:
t1 = perf_counter()
print_stats(rates_table, t1 - t0)
t0 = perf_counter()
return rates_table
def fetch_rates(counter_currencies, ts):
"""this method mocks a requests to an external service"""
ts_rates = []
for ccy2 in counter_currencies:
# random bid and ask
ask = round(random.uniform(0.8, 1.2), 4)
bid = round(ask - random.uniform(0.0001, 0.01), 4)
ts_rates.append({"ts": ts, "ccy1": "USD", "ccy2": ccy2, "bid": bid, "ask": ask})
return ts_rates
def print_stats(table: pa.Table, elapsed: float) -> None:
memory_info = psutil.Process(PID).memory_info()
memory_usage_mb = memory_info.rss / (1024**2)
table_size_bytes = table.get_total_buffer_size()
table_size_mb = table_size_bytes / (1024**2)
print(
f"Process Memory usage: {memory_usage_mb:.2f}Mb, "
f"table ({len(table)}): {table_size_mb:.2f}Mb in {elapsed:.2f}s"
)
t0 = perf_counter()
my_table = add_rates()
print("cycle finished")
print_stats(my_table, perf_counter() - t0)
print("done")
```
### Component(s)
Python
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@arrow.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Real Time Analytics: concat_tables memory and performance degradation [arrow]
Posted by "thisisnic (via GitHub)" <gi...@apache.org>.
thisisnic closed issue #37801: Real Time Analytics: concat_tables memory and performance degradation
URL: https://github.com/apache/arrow/issues/37801
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Real Time Analytics: concat_tables memory and performance degradation [arrow]
Posted by "thisisnic (via GitHub)" <gi...@apache.org>.
thisisnic commented on issue #37801:
URL: https://github.com/apache/arrow/issues/37801#issuecomment-3547497206
This issue has been marked as stale because it has had no activity in the past 365 days. Please remove the stale label or comment below, or this issue will be closed in 14 days. If this usage question has evolved into a feature request or docs update, please remove the 'Type: usage' label and add the 'Type: enhancement' label instead.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] jorisvandenbossche commented on issue #37801: Real Time Analytics: concat_tables memory and performance degradation
Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on issue #37801:
URL: https://github.com/apache/arrow/issues/37801#issuecomment-1740891131
> Could you advise if this is a limitation of the tool (i.e. not being designed to continuously append data) or if I should use another approach?
This is not an answer to whether there might be an actual memory issue or leak in the implementation, but the way you are constructing the Table is definitely sub-optimal.
What you are doing is creating tables of 5 rows at a time, and then appending this to the existing table (`rates_table = pa.concat_tables((rates_table, chunk))`, where chunk has 5 rows). The `concat_tables` actually doesn't make any copy, but will preserve the original chunks but put them together in a single Table object. So you end up with a table of > 10000 chunks of each 5 rows, which is very inefficient (each chunk has _some_ overhead).
In a process where you are continuously appending data, the better approach would be a two-step logic: first gather the batches of data in a separate Table, and only when this reaches a certain size threshold (eg 65k), actually combine the chunks of this table (involving a copy) into a single chunk, and then append this combined chunk to the overall `rates_table`.
It would be interesting to see when using such an approach if you still notice memory issues.
> and if I use `combine_chunks` from time to time, things get worse.
Given my explanation above, I have to admit that this is a bit strange ..
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Real Time Analytics: concat_tables memory and performance degradation [arrow]
Posted by "pablodcar (via GitHub)" <gi...@apache.org>.
pablodcar commented on issue #37801:
URL: https://github.com/apache/arrow/issues/37801#issuecomment-1744394914
Thanks for the response.
> In a process where you are continuously appending data, the better approach would be a two-step logic: first gather the batches of data in a separate Table, and only when this reaches a certain size threshold (eg 65k), actually combine the chunks of this table (involving a copy) into a single chunk, and then append this combined chunk to the overall `rates_table`.
>
> It would be interesting to see when using such an approach if you still notice memory issues.
>
> > and if I use `combine_chunks` from time to time, things get worse.
>
> Given my explanation above, I have to admit that this is a bit strange ..
Calling `combine_chunks` every 65k items - instead of a single call at the end - helps in terms of CPU and memory. In fact, the memory remains almost constant, i.e. if I run the cycle twice, the memory is not doubled, only it's increased a little.
Will test with a long-running process, thanks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org