From 7a1365e583b5f88eb54de6e1876068f25bdb2e97 Mon Sep 17 00:00:00 2001 From: Brian Calvert Date: Fri, 9 Aug 2024 09:41:45 -0700 Subject: [PATCH] Change most `raise e from e` calls to be just `raise` and remove unneeded ones --- pymilvus/bulk_writer/local_bulk_writer.py | 2 +- pymilvus/client/grpc_handler.py | 12 ++--- pymilvus/decorators.py | 20 +++---- pymilvus/milvus_client/milvus_client.py | 65 ++++++++--------------- 4 files changed, 36 insertions(+), 63 deletions(-) diff --git a/pymilvus/bulk_writer/local_bulk_writer.py b/pymilvus/bulk_writer/local_bulk_writer.py index a4441aade..61af608df 100644 --- a/pymilvus/bulk_writer/local_bulk_writer.py +++ b/pymilvus/bulk_writer/local_bulk_writer.py @@ -139,7 +139,7 @@ def _flush(self, call_back: Optional[Callable] = None): call_back(file_list) except Exception as e: logger.error(f"Failed to fulsh, error: {e}") - raise e from e + raise finally: del self._working_thread[threading.current_thread().name] logger.info(f"Flush thread finished, name: {threading.current_thread().name}") diff --git a/pymilvus/client/grpc_handler.py b/pymilvus/client/grpc_handler.py index fd7998305..3713be68a 100644 --- a/pymilvus/client/grpc_handler.py +++ b/pymilvus/client/grpc_handler.py @@ -151,8 +151,6 @@ def _wait_for_channel_ready(self, timeout: Union[float] = 10): code=Status.CONNECT_FAILED, message=f"Fail connecting to server on {self._address}, illegal connection params or server unavailable", ) from e - except Exception as e: - raise e from e def close(self): self.deregister_state_change_callbacks() @@ -579,7 +577,7 @@ def batch_insert( except Exception as err: if kwargs.get("_async", False): return MutationFuture(None, None, err) - raise err from err + raise else: return m @@ -616,7 +614,7 @@ def delete( except Exception as err: if kwargs.get("_async", False): return MutationFuture(None, None, err) - raise err from err + raise else: return m @@ -676,7 +674,7 @@ def upsert( except Exception as err: if kwargs.get("_async", False): return MutationFuture(None, None, err) - raise err from err + raise else: return m @@ -738,7 +736,7 @@ def _execute_search( except Exception as e: if kwargs.get("_async", False): return SearchFuture(None, None, e) - raise e from e + raise def _execute_hybrid_search( self, request: milvus_types.HybridSearchRequest, timeout: Optional[float] = None, **kwargs @@ -757,7 +755,7 @@ def _execute_hybrid_search( except Exception as e: if kwargs.get("_async", False): return SearchFuture(None, None, e) - raise e from e + raise @retry_on_rpc_failure() def search( diff --git a/pymilvus/decorators.py b/pymilvus/decorators.py index cc1e3be8c..1494c6e32 100644 --- a/pymilvus/decorators.py +++ b/pymilvus/decorators.py @@ -87,7 +87,7 @@ def timeout(start_time: Optional[float] = None) -> bool: except grpc.RpcError as e: # Do not retry on these codes if e.code() in IGNORE_RETRY_CODES: - raise e from e + raise if timeout(start_time): raise MilvusException(e.code, f"{to_msg}, message={e.details()}") from e @@ -113,9 +113,7 @@ def timeout(start_time: Optional[float] = None) -> bool: time.sleep(back_off) back_off = min(back_off * back_off_multiplier, max_back_off) else: - raise e from e - except Exception as e: - raise e from e + raise finally: counter += 1 @@ -138,21 +136,21 @@ def handler(*args, **kwargs): except MilvusException as e: record_dict["RPC error"] = str(datetime.datetime.now()) LOGGER.error(f"RPC error: [{inner_name}], {e}, ") - raise e from e + raise except grpc.FutureTimeoutError as e: record_dict["gRPC timeout"] = str(datetime.datetime.now()) LOGGER.error( f"grpc Timeout: [{inner_name}], <{e.__class__.__name__}: " f"{e.code()}, {e.details()}>, " ) - raise e from e + raise except grpc.RpcError as e: record_dict["gRPC error"] = str(datetime.datetime.now()) LOGGER.error( f"grpc RpcError: [{inner_name}], <{e.__class__.__name__}: " f"{e.code()}, {e.details()}>, " ) - raise e from e + raise except Exception as e: record_dict["Exception"] = str(datetime.datetime.now()) LOGGER.error(f"Unexpected error: [{inner_name}], {e}, ") @@ -190,9 +188,7 @@ def handler(*args, **kwargs): if e.code() == grpc.StatusCode.UNIMPLEMENTED: LOGGER.debug(f"{func.__name__} unimplemented, ignore it") return default_return_value - raise e from e - except Exception as e: - raise e from e + raise return handler @@ -211,8 +207,6 @@ def handler(*args, **kwargs): "please downgrade your sdk or upgrade your server" ) raise MilvusException(message=msg) from e - raise e from e - except Exception as e: - raise e from e + raise return handler diff --git a/pymilvus/milvus_client/milvus_client.py b/pymilvus/milvus_client/milvus_client.py index d0fbb798f..23fbffec7 100644 --- a/pymilvus/milvus_client/milvus_client.py +++ b/pymilvus/milvus_client/milvus_client.py @@ -136,7 +136,7 @@ def _fast_create_collection( logger.debug("Successfully created collection: %s", collection_name) except Exception as ex: logger.error("Failed to create collection: %s", collection_name) - raise ex from ex + raise index_params = IndexParams() index_params.add_index(vector_field_name, "", "", metric_type=metric_type) @@ -173,7 +173,7 @@ def _create_index( logger.debug("Successfully created an index on collection: %s", collection_name) except Exception as ex: logger.error("Failed to create an index on collection: %s", collection_name) - raise ex from ex + raise def insert( self, @@ -216,12 +216,9 @@ def insert( conn = self._get_connection() # Insert into the collection. - try: - res = conn.insert_rows( - collection_name, data, partition_name=partition_name, timeout=timeout - ) - except Exception as ex: - raise ex from ex + res = conn.insert_rows( + collection_name, data, partition_name=partition_name, timeout=timeout + ) return OmitZeroDict( { "insert_count": res.insert_count, @@ -268,13 +265,9 @@ def upsert( conn = self._get_connection() # Upsert into the collection. - try: - res = conn.upsert_rows( - collection_name, data, partition_name=partition_name, timeout=timeout, **kwargs - ) - except Exception as ex: - raise ex from ex - + res = conn.upsert_rows( + collection_name, data, partition_name=partition_name, timeout=timeout, **kwargs + ) return OmitZeroDict( { "upsert_count": res.upsert_count, @@ -333,7 +326,7 @@ def search( ) except Exception as ex: logger.error("Failed to search collection: %s", collection_name) - raise ex from ex + raise ret = [] for hits in res: @@ -384,7 +377,7 @@ def query( schema_dict = conn.describe_collection(collection_name, timeout=timeout, **kwargs) except Exception as ex: logger.error("Failed to describe collection: %s", collection_name) - raise ex from ex + raise if ids: filter = self._pack_pks_expr(schema_dict, ids) @@ -406,7 +399,7 @@ def query( ) except Exception as ex: logger.error("Failed to query collection: %s", collection_name) - raise ex from ex + raise return res @@ -446,7 +439,7 @@ def get( schema_dict = conn.describe_collection(collection_name, timeout=timeout, **kwargs) except Exception as ex: logger.error("Failed to describe collection: %s", collection_name) - raise ex from ex + raise if not output_fields: output_fields = ["*"] @@ -466,7 +459,7 @@ def get( ) except Exception as ex: logger.error("Failed to get collection: %s", collection_name) - raise ex from ex + raise return res @@ -528,7 +521,7 @@ def delete( schema_dict = conn.describe_collection(collection_name, timeout=timeout, **kwargs) except Exception as ex: logger.error("Failed to describe collection: %s", collection_name) - raise ex from ex + raise expr = self._pack_pks_expr(schema_dict, pks) @@ -555,7 +548,7 @@ def delete( ret_pks.extend(res.primary_keys) except Exception as ex: logger.error("Failed to delete primary keys in collection: %s", collection_name) - raise ex from ex + raise if ret_pks: return ret_pks @@ -625,7 +618,7 @@ def _create_collection_with_schema( logger.debug("Successfully created collection: %s", collection_name) except Exception as ex: logger.error("Failed to create collection: %s", collection_name) - raise ex from ex + raise if index_params: self.create_index(collection_name, index_params, timeout=timeout) @@ -653,7 +646,7 @@ def _create_connection( connections.connect(using, user, password, db_name, token, uri=uri, **kwargs) except Exception as ex: logger.error("Failed to create new connection using: %s", using) - raise ex from ex + raise else: logger.debug("Created new connection using: %s", using) return using @@ -700,7 +693,7 @@ def load_collection(self, collection_name: str, timeout: Optional[float] = None, conn.load_collection(collection_name, timeout=timeout, **kwargs) except MilvusException as ex: logger.error("Failed to load collection: %s", collection_name) - raise ex from ex + raise def release_collection(self, collection_name: str, timeout: Optional[float] = None, **kwargs): conn = self._get_connection() @@ -708,7 +701,7 @@ def release_collection(self, collection_name: str, timeout: Optional[float] = No conn.release_collection(collection_name, timeout=timeout, **kwargs) except MilvusException as ex: logger.error("Failed to load collection: %s", collection_name) - raise ex from ex + raise def get_load_state( self, @@ -721,10 +714,7 @@ def get_load_state( partition_names = None if partition_name: partition_names = [partition_name] - try: - state = conn.get_load_state(collection_name, partition_names, timeout=timeout, **kwargs) - except Exception as ex: - raise ex from ex + state = conn.get_load_state(collection_name, partition_names, timeout=timeout, **kwargs) ret = {"state": state} if state == LoadState.Loading: @@ -865,10 +855,7 @@ def list_users(self, timeout: Optional[float] = None, **kwargs): def describe_user(self, user_name: str, timeout: Optional[float] = None, **kwargs): conn = self._get_connection() - try: - res = conn.select_one_user(user_name, True, timeout=timeout, **kwargs) - except Exception as ex: - raise ex from ex + res = conn.select_one_user(user_name, True, timeout=timeout, **kwargs) if res.groups: item = res.groups[0] return {"user_name": user_name, "roles": item.roles} @@ -897,10 +884,7 @@ def describe_role( ) -> List[Dict]: conn = self._get_connection() db_name = kwargs.pop("db_name", "") - try: - res = conn.select_grant_for_one_role(role_name, db_name, timeout=timeout, **kwargs) - except Exception as ex: - raise ex from ex + res = conn.select_grant_for_one_role(role_name, db_name, timeout=timeout, **kwargs) ret = {} ret["role"] = role_name ret["privileges"] = [dict(i) for i in res.groups] @@ -908,10 +892,7 @@ def describe_role( def list_roles(self, timeout: Optional[float] = None, **kwargs): conn = self._get_connection() - try: - res = conn.select_all_role(False, timeout=timeout, **kwargs) - except Exception as ex: - raise ex from ex + res = conn.select_all_role(False, timeout=timeout, **kwargs) groups = res.groups return [g.role_name for g in groups]