You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by "JackieTien97 (via GitHub)" <gi...@apache.org> on 2023/07/17 08:37:59 UTC

[GitHub] [iotdb] JackieTien97 commented on a diff in pull request #10558: [IOTDB-6069] Support SessionPool in client-py

JackieTien97 commented on code in PR #10558:
URL: https://github.com/apache/iotdb/pull/10558#discussion_r1265039435


##########
iotdb-client/client-py/iotdb/SessionPool.py:
##########
@@ -0,0 +1,122 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+import multiprocessing
+import time
+from queue import Queue
+from threading import Lock
+
+from iotdb.Session import Session
+
+DEFAULT_MULTIPIE = 5
+DEFAULT_FETCH_SIZE = 5000
+DEFAULT_MAX_RETRY = 3
+DEFAULT_TIME_ZONE = "Asia/Shanghai"
+
+
+class PoolConfig(object):
+    def __init__(self, host: str, ip: str, user_name: str, password: str, node_urls: list = None,
+                 fetch_size: int = DEFAULT_FETCH_SIZE, time_zone: str = DEFAULT_TIME_ZONE,
+                 max_retry: int = DEFAULT_MAX_RETRY):
+        self.host = host
+        self.ip = ip
+        if node_urls is None:
+            node_urls = []
+        self.node_urls = node_urls
+        self.user_name = user_name
+        self.password = password
+        self.fetch_size = fetch_size
+        self.time_zone = time_zone
+        self.max_retry = max_retry
+
+
+class SessionPool(object):
+
+    def __init__(self, pool_config: PoolConfig, max_pool_size: int, wait_timeout_in_ms: int):
+        self.__pool_config = pool_config
+        self.__max_pool_size = max_pool_size
+        self.__wait_timeout_in_ms = wait_timeout_in_ms / 1000
+        self.__pool_size = 0
+        self.__queue = Queue(max_pool_size)
+        self.__lock = Lock()
+
+    def __construct_session(self) -> Session:
+        if len(self.__pool_config.node_urls) > 0:
+            return Session.init_from_node_urls(self.__pool_config.node_urls, self.__pool_config.user_name,
+                                               self.__pool_config.password, self.__pool_config.fetch_size,
+                                               self.__pool_config.time_zone)
+
+        else:
+            return Session(self.__pool_config.host, self.__pool_config.ip, self.__pool_config.user_name,
+                           self.__pool_config.password, self.__pool_config.fetch_size, self.__pool_config.time_zone)
+
+    def __poll_session(self) -> Session | None:
+        if self.__queue.empty():
+            return None
+        return self.__queue.get(block=False)
+
+    def get_session(self) -> Session:
+        should_create = False
+        start = time.time()
+
+        session = self.__poll_session()
+        while session is None:
+            self.__lock.acquire()
+            if self.__pool_size < self.__max_pool_size:
+                self.__pool_size += 1
+                should_create = True
+                self.__lock.release()
+                break
+            else:
+                if time.time() - start > self.__wait_timeout_in_ms:
+                    self.__lock.release()
+                    raise TimeoutError("Wait to get session timeout in SessionPool, current pool size: {0}"
+                                       .format(self.__max_pool_size))
+                time.sleep(1)

Review Comment:
   sleep for 1 second? Make sure default time unit in python sleep is second.



##########
iotdb-client/client-py/iotdb/SessionPool.py:
##########
@@ -0,0 +1,122 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+import multiprocessing
+import time
+from queue import Queue
+from threading import Lock
+
+from iotdb.Session import Session
+
+DEFAULT_MULTIPIE = 5
+DEFAULT_FETCH_SIZE = 5000
+DEFAULT_MAX_RETRY = 3
+DEFAULT_TIME_ZONE = "Asia/Shanghai"
+
+
+class PoolConfig(object):
+    def __init__(self, host: str, ip: str, user_name: str, password: str, node_urls: list = None,
+                 fetch_size: int = DEFAULT_FETCH_SIZE, time_zone: str = DEFAULT_TIME_ZONE,
+                 max_retry: int = DEFAULT_MAX_RETRY):
+        self.host = host
+        self.ip = ip
+        if node_urls is None:
+            node_urls = []
+        self.node_urls = node_urls
+        self.user_name = user_name
+        self.password = password
+        self.fetch_size = fetch_size
+        self.time_zone = time_zone
+        self.max_retry = max_retry
+
+
+class SessionPool(object):
+
+    def __init__(self, pool_config: PoolConfig, max_pool_size: int, wait_timeout_in_ms: int):
+        self.__pool_config = pool_config
+        self.__max_pool_size = max_pool_size
+        self.__wait_timeout_in_ms = wait_timeout_in_ms / 1000
+        self.__pool_size = 0
+        self.__queue = Queue(max_pool_size)
+        self.__lock = Lock()
+
+    def __construct_session(self) -> Session:
+        if len(self.__pool_config.node_urls) > 0:
+            return Session.init_from_node_urls(self.__pool_config.node_urls, self.__pool_config.user_name,
+                                               self.__pool_config.password, self.__pool_config.fetch_size,
+                                               self.__pool_config.time_zone)
+
+        else:
+            return Session(self.__pool_config.host, self.__pool_config.ip, self.__pool_config.user_name,
+                           self.__pool_config.password, self.__pool_config.fetch_size, self.__pool_config.time_zone)
+
+    def __poll_session(self) -> Session | None:
+        if self.__queue.empty():
+            return None
+        return self.__queue.get(block=False)
+
+    def get_session(self) -> Session:
+        should_create = False
+        start = time.time()
+
+        session = self.__poll_session()
+        while session is None:
+            self.__lock.acquire()
+            if self.__pool_size < self.__max_pool_size:
+                self.__pool_size += 1
+                should_create = True
+                self.__lock.release()
+                break
+            else:
+                if time.time() - start > self.__wait_timeout_in_ms:
+                    self.__lock.release()
+                    raise TimeoutError("Wait to get session timeout in SessionPool, current pool size: {0}"
+                                       .format(self.__max_pool_size))
+                time.sleep(1)
+            session = self.__poll_session()
+            self.__lock.release()
+
+        if should_create:
+            try:
+                session = self.__construct_session()
+            except Exception as e:
+                self.__lock.acquire()
+                self.__pool_size -= 1
+                self.__lock.release()
+                raise e
+
+        return session
+
+    def put_back(self, session: Session):
+        if session.is_open():
+            self.__queue.put(session)
+        else:
+            self.__lock.acquire()
+            self.__pool_size -= 1
+            self.__lock.release()
+
+    def close(self):
+        while not self.__queue.empty():
+            session = self.__queue.get(block=False)
+            session.close()

Review Comment:
   don't need to update `__pool_size`?



##########
iotdb-client/client-py/iotdb/SessionPool.py:
##########
@@ -0,0 +1,122 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+import multiprocessing
+import time
+from queue import Queue
+from threading import Lock
+
+from iotdb.Session import Session
+
+DEFAULT_MULTIPIE = 5
+DEFAULT_FETCH_SIZE = 5000
+DEFAULT_MAX_RETRY = 3
+DEFAULT_TIME_ZONE = "Asia/Shanghai"
+
+
+class PoolConfig(object):
+    def __init__(self, host: str, ip: str, user_name: str, password: str, node_urls: list = None,
+                 fetch_size: int = DEFAULT_FETCH_SIZE, time_zone: str = DEFAULT_TIME_ZONE,
+                 max_retry: int = DEFAULT_MAX_RETRY):
+        self.host = host
+        self.ip = ip
+        if node_urls is None:
+            node_urls = []
+        self.node_urls = node_urls
+        self.user_name = user_name
+        self.password = password
+        self.fetch_size = fetch_size
+        self.time_zone = time_zone
+        self.max_retry = max_retry
+
+
+class SessionPool(object):
+
+    def __init__(self, pool_config: PoolConfig, max_pool_size: int, wait_timeout_in_ms: int):
+        self.__pool_config = pool_config
+        self.__max_pool_size = max_pool_size
+        self.__wait_timeout_in_ms = wait_timeout_in_ms / 1000
+        self.__pool_size = 0
+        self.__queue = Queue(max_pool_size)

Review Comment:
   Is this `Queue` a thread-safe class?



-- 
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: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org