Skip to content

Commit c372b3e

Browse files
authored
Fix 0.11 issues2 (#67)
* Tests need psutil * Check thread count only in standalone test run * Ignore
1 parent 87dc278 commit c372b3e

File tree

11 files changed

+220
-15
lines changed

11 files changed

+220
-15
lines changed

.github/workflows/build_wheels.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ jobs:
132132
- name: Run tests
133133
run: |
134134
python3 -m pip install dist/*.whl
135-
python3 -m pip install pandas pyarrow
135+
python3 -m pip install pandas pyarrow psutil
136136
python3 -c "import chdb; res = chdb.query('select 1112222222,555', 'CSV'); print(res)"
137137
make test
138138
continue-on-error: false
@@ -249,7 +249,7 @@ jobs:
249249
- name: Run tests
250250
run: |
251251
python3 -m pip install dist/*.whl
252-
python3 -m pip install pandas pyarrow
252+
python3 -m pip install pandas pyarrow psutil
253253
python3 -c "import chdb; res = chdb.query('select 1112222222,555', 'CSV'); print(res)"
254254
make test
255255
continue-on-error: false
@@ -344,7 +344,7 @@ jobs:
344344
CIBW_BEFORE_BUILD: "pip install -U pip tox pybind11 && bash -x gen_manifest.sh && bash chdb/build.sh"
345345
CIBW_BUILD_VERBOSITY: 3
346346
CIBW_BUILD: "cp38-macosx_x86_64 cp39-macosx_x86_64 cp310-macosx_x86_64"
347-
CIBW_TEST_REQUIRES: "pyarrow pandas"
347+
CIBW_TEST_REQUIRES: "pyarrow pandas psutil"
348348
CIBW_TEST_COMMAND: "cd {project} && make test"
349349
- name: Keep killall ccache and wait for ccache to finish
350350
if: always()

.gitignore

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@
1212
*.logrt
1313

1414
/python_pkg/
15+
/tmps
1516
state_tmp_l3jfk
1617
/chdb-0.*/
1718
*.strings
1819
/arrow1100
1920
test_main
20-
/buildlib
21-
/builddbg
22-
/buildx86
23-
/build
24-
/build_*
25-
/build-*
21+
/build*
2622
/bench
2723
/tests/venv
2824
/obj-x86_64-linux-gnu/

chdb/session/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .state import *

chdb/session/state.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import tempfile
2+
import shutil
3+
4+
from chdb import query_stateful
5+
6+
7+
class Session():
8+
"""
9+
Session will keep the state of query. All DDL and DML state will be kept in a dir.
10+
Dir path could be passed in as an argument. If not, a temporary dir will be created.
11+
12+
If path is not specified, the temporary dir will be deleted when the Session object is deleted.
13+
Otherwise path will be kept.
14+
15+
Note: The default database is "_local" and the default engine is "Memory" which means all data
16+
will be stored in memory. If you want to store data in disk, you should create another database.
17+
"""
18+
19+
def __init__(self, path=None):
20+
if path is None:
21+
self._cleanup = True
22+
self._path = tempfile.mkdtemp()
23+
else:
24+
self._cleanup = False
25+
self._path = path
26+
27+
def __del__(self):
28+
if self._cleanup:
29+
self.cleanup()
30+
31+
def cleanup(self):
32+
shutil.rmtree(self._path)
33+
34+
def query(self, sql, fmt="CSV"):
35+
"""
36+
Execute a query.
37+
"""
38+
return query_stateful(sql, fmt, path=self._path)

src/Common/ThreadPool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ ThreadPoolImpl<Thread>::ThreadPoolImpl(
4242
Metric metric_threads_,
4343
Metric metric_active_threads_,
4444
size_t max_threads_)
45-
: ThreadPoolImpl(metric_threads_, metric_active_threads_, max_threads_, max_threads_, max_threads_)
45+
: ThreadPoolImpl(metric_threads_, metric_active_threads_, max_threads_, 0, max_threads_)
4646
{
4747
}
4848

src/Disks/IO/ThreadPoolReader.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@ static bool hasBugInPreadV2()
8888
#endif
8989

9090
ThreadPoolReader::ThreadPoolReader(size_t pool_size, size_t queue_size_)
91-
: pool(std::make_unique<ThreadPool>(CurrentMetrics::ThreadPoolFSReaderThreads, CurrentMetrics::ThreadPoolFSReaderThreadsActive, pool_size, pool_size, queue_size_))
91+
: pool(std::make_unique<ThreadPool>(
92+
CurrentMetrics::ThreadPoolFSReaderThreads,
93+
CurrentMetrics::ThreadPoolFSReaderThreadsActive,
94+
pool_size,
95+
/* max_free_threads = */ 0,
96+
queue_size_))
9297
{
9398
}
9499

src/Disks/IO/ThreadPoolRemoteFSReader.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ IAsynchronousReader::Result RemoteFSFileDescriptor::readInto(char * data, size_t
6363

6464

6565
ThreadPoolRemoteFSReader::ThreadPoolRemoteFSReader(size_t pool_size, size_t queue_size_)
66-
: pool(std::make_unique<ThreadPool>(CurrentMetrics::ThreadPoolRemoteFSReaderThreads, CurrentMetrics::ThreadPoolRemoteFSReaderThreadsActive, pool_size, pool_size, queue_size_))
66+
: pool(std::make_unique<ThreadPool>(
67+
CurrentMetrics::ThreadPoolRemoteFSReaderThreads,
68+
CurrentMetrics::ThreadPoolRemoteFSReaderThreadsActive,
69+
pool_size,
70+
/* max_free_threads = */ 0,
71+
queue_size_))
6772
{
6873
}
6974

src/Interpreters/Context.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,7 +2281,11 @@ ThreadPool & Context::getLoadMarksThreadpool() const
22812281
auto pool_size = config.getUInt(".load_marks_threadpool_pool_size", 50);
22822282
auto queue_size = config.getUInt(".load_marks_threadpool_queue_size", 1000000);
22832283
shared->load_marks_threadpool = std::make_unique<ThreadPool>(
2284-
CurrentMetrics::MarksLoaderThreads, CurrentMetrics::MarksLoaderThreadsActive, pool_size, pool_size, queue_size);
2284+
CurrentMetrics::MarksLoaderThreads,
2285+
CurrentMetrics::MarksLoaderThreadsActive,
2286+
pool_size,
2287+
/* max_free_threads = */ 0,
2288+
queue_size);
22852289
}
22862290
return *shared->load_marks_threadpool;
22872291
}
@@ -2307,7 +2311,7 @@ ThreadPool & Context::getPrefetchThreadpool() const
23072311
auto pool_size = getPrefetchThreadpoolSize();
23082312
auto queue_size = config.getUInt(".prefetch_threadpool_queue_size", 1000000);
23092313
shared->prefetch_threadpool = std::make_unique<ThreadPool>(
2310-
CurrentMetrics::IOPrefetchThreads, CurrentMetrics::IOPrefetchThreadsActive, pool_size, pool_size, queue_size);
2314+
CurrentMetrics::IOPrefetchThreads, CurrentMetrics::IOPrefetchThreadsActive, pool_size, /* max_free_threads = */ 0, queue_size);
23112315
}
23122316
return *shared->prefetch_threadpool;
23132317
}
@@ -4249,7 +4253,7 @@ ThreadPool & Context::getThreadPoolWriter() const
42494253
auto queue_size = config.getUInt(".threadpool_writer_queue_size", 1000000);
42504254

42514255
shared->threadpool_writer = std::make_unique<ThreadPool>(
4252-
CurrentMetrics::IOWriterThreads, CurrentMetrics::IOWriterThreadsActive, pool_size, pool_size, queue_size);
4256+
CurrentMetrics::IOWriterThreads, CurrentMetrics::IOWriterThreadsActive, pool_size, /* max_free_threads = */ 0, queue_size);
42534257
}
42544258

42554259
return *shared->threadpool_writer;

tests/test_final_join.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!python3
2+
3+
import unittest
4+
import psutil
5+
from chdb import session
6+
7+
8+
current_process = psutil.Process()
9+
check_thread_count = False
10+
11+
12+
class TestStateful(unittest.TestCase):
13+
def test_zfree_thread_count(self):
14+
sess2 = session.Session()
15+
ret = sess2.query("SELECT sleep(2)", "Debug")
16+
# self.assertEqual(str(ret), "")
17+
thread_count = current_process.num_threads()
18+
print("Number of threads using psutil library: ", thread_count)
19+
if check_thread_count:
20+
self.assertEqual(thread_count, 1)
21+
22+
23+
if __name__ == "__main__":
24+
check_thread_count = True
25+
unittest.main()
26+

tests/test_gc.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!python3
2+
3+
import unittest
4+
import gc
5+
import chdb
6+
7+
class TestGC(unittest.TestCase):
8+
def test_gc(self):
9+
print("query started")
10+
gc.set_debug(gc.DEBUG_STATS)
11+
12+
ret = chdb.query("SELECT 123,'adbcd'", 'CSV')
13+
# print("ret:", ret)
14+
# print("ret type:", type(ret))
15+
self.assertEqual(str(ret), '123,"adbcd"\n')
16+
gc.collect()
17+
18+
mv = ret.get_memview()
19+
self.assertIsNotNone(mv)
20+
gc.collect()
21+
22+
self.assertEqual(len(mv), 12)
23+
24+
out = mv.tobytes()
25+
self.assertEqual(out, b'123,"adbcd"\n')
26+
27+
ret2 = chdb.query("SELECT 123,'adbcdefg'", 'CSV').get_memview().tobytes()
28+
self.assertEqual(ret2, b'123,"adbcdefg"\n')
29+
30+
mv2 = chdb.query("SELECT 123,'adbcdefg'", 'CSV').get_memview()
31+
gc.collect()
32+
33+
self.assertEqual(mv2.tobytes(), b'123,"adbcdefg"\n')
34+
35+
mv3 = mv2.view()
36+
gc.collect()
37+
self.assertEqual(mv3.tobytes(), b'123,"adbcdefg"\n')
38+
self.assertEqual(len(mv3), 15)
39+
40+
if __name__ == '__main__':
41+
unittest.main()

0 commit comments

Comments
 (0)