Skip to content

Commit

Permalink
Add support for setting uri from environment
Browse files Browse the repository at this point in the history
See also: milvus-io#1369

Signed-off-by: yangxuan <[email protected]>
  • Loading branch information
XuanYang-cn committed Apr 21, 2023
1 parent 05f3384 commit 8c935c4
Show file tree
Hide file tree
Showing 17 changed files with 195 additions and 155 deletions.
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Please copy this file and rename as .env, pymilvus will read .env file if provided

MILVUS_URI=
# MILVUS_URI=https://username:[email protected]:19530

# Milvus connections configs
MILVUS_CONN_ALIAS=default
MILVUS_CONN_TIMEOUT=10

12 changes: 10 additions & 2 deletions pymilvus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@
)
from .client import __version__

from .settings import DEBUG_LOG_LEVEL, INFO_LOG_LEVEL, WARN_LOG_LEVEL, ERROR_LOG_LEVEL
from .settings import (
DEBUG_LOG_LEVEL,
INFO_LOG_LEVEL,
WARN_LOG_LEVEL,
ERROR_LOG_LEVEL,
)
# Compatiable
from .settings import Config as DefaultConfig

from .client.constants import DEFAULT_RESOURCE_GROUP

from .orm.collection import Collection
from .orm.connections import connections, Connections
Expand All @@ -58,7 +67,6 @@
)

from .orm import utility
from .orm.default_config import DefaultConfig, ENV_CONNECTION_CONF, DEFAULT_RESOURCE_GROUP

from .orm.search import SearchResult, Hits, Hit
from .orm.schema import FieldSchema, CollectionSchema
Expand Down
4 changes: 2 additions & 2 deletions pymilvus/client/abstract.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import abc

import numpy as np
from .configs import DefaultConfigs
from ..settings import Config
from .types import DataType
from .constants import DEFAULT_CONSISTENCY_LEVEL
from ..grpc_gen import schema_pb2
Expand Down Expand Up @@ -88,7 +88,7 @@ def __pack(self, raw):
else:
self.params[type_param.key] = type_param.value
# maybe we'd better not to check these fields in ORM.
if type_param.key in ["dim", DefaultConfigs.MaxVarCharLengthKey]:
if type_param.key in ["dim", Config.MaxVarCharLengthKey]:
self.params[type_param.key] = int(type_param.value)

index_dict = {}
Expand Down
7 changes: 0 additions & 7 deletions pymilvus/client/configs.py

This file was deleted.

1 change: 1 addition & 0 deletions pymilvus/client/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
EVENTUALLY_TS = 1
BOUNDED_TS = 2
DEFAULT_CONSISTENCY_LEVEL = ConsistencyLevel.Bounded
DEFAULT_RESOURCE_GROUP = "__default_resource_group"
10 changes: 5 additions & 5 deletions pymilvus/client/entity_helper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ..grpc_gen import schema_pb2 as schema_types
from .types import DataType
from ..exceptions import ParamError
from .configs import DefaultConfigs
from ..settings import Config


def entity_type_to_dtype(entity_type):
Expand All @@ -14,8 +14,8 @@ def entity_type_to_dtype(entity_type):


def get_max_len_of_var_char(field_info) -> int:
k = DefaultConfigs.MaxVarCharLengthKey
v = DefaultConfigs.MaxVarCharLength
k = Config.MaxVarCharLengthKey
v = Config.MaxVarCharLength
return field_info.get("params", {}).get(k, v)


Expand All @@ -30,9 +30,9 @@ def check_str_arr(str_arr, max_len):

def entity_to_str_arr(entity, field_info, check=True):
arr = []
if DefaultConfigs.EncodeProtocol.lower() != 'utf-8'.lower():
if Config.EncodeProtocol.lower() != 'utf-8'.lower():
for s in entity.get("values"):
arr.append(s.encode(DefaultConfigs.EncodeProtocol))
arr.append(s.encode(Config.EncodeProtocol))
else:
arr = entity.get("values")
max_len = int(get_max_len_of_var_char(field_info))
Expand Down
11 changes: 5 additions & 6 deletions pymilvus/client/grpc_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
get_server_type,
)

from ..settings import DefaultConfig as config
from .configs import DefaultConfigs
from ..settings import Config
from . import ts_utils
from . import interceptor

Expand All @@ -68,7 +67,7 @@


class GrpcHandler:
def __init__(self, uri=config.GRPC_URI, host="", port="", channel=None, **kwargs):
def __init__(self, uri=Config.GRPC_URI, host="", port="", channel=None, **kwargs):
self._stub = None
self._channel = channel

Expand Down Expand Up @@ -560,7 +559,7 @@ def alter_alias(self, collection_name, alias, timeout=None, **kwargs):
@retry_on_rpc_failure()
def create_index(self, collection_name, field_name, params, timeout=None, **kwargs):
# for historical reason, index_name contained in kwargs.
index_name = kwargs.pop("index_name", DefaultConfigs.IndexName)
index_name = kwargs.pop("index_name", Config.IndexName)
copy_kwargs = copy.deepcopy(kwargs)

collection_desc = self.describe_collection(collection_name, timeout=timeout, **copy_kwargs)
Expand Down Expand Up @@ -732,7 +731,7 @@ def can_loop(t) -> bool:
progress = self.get_loading_progress(collection_name, timeout=timeout)
if progress >= 100:
return
time.sleep(DefaultConfigs.WaitTimeDurationWhenLoad)
time.sleep(Config.WaitTimeDurationWhenLoad)
raise MilvusException(message=f"wait for loading collection timeout, collection: {collection_name}")

@retry_on_rpc_failure()
Expand Down Expand Up @@ -788,7 +787,7 @@ def can_loop(t) -> bool:
progress = self.get_loading_progress(collection_name, partition_names, timeout=timeout)
if progress >= 100:
return
time.sleep(DefaultConfigs.WaitTimeDurationWhenLoad)
time.sleep(Config.WaitTimeDurationWhenLoad)
raise MilvusException(message=f"wait for loading partition timeout, collection: {collection_name}, partitions: {partition_names}")

@retry_on_rpc_failure()
Expand Down
6 changes: 3 additions & 3 deletions pymilvus/client/stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
from .grpc_handler import GrpcHandler
from ..exceptions import MilvusException, ParamError
from .types import CompactionState, CompactionPlans, Replica, BulkInsertState, ResourceGroupInfo
from ..settings import DefaultConfig as config
from ..settings import Config
from ..decorators import deprecated

from .check import is_legal_host, is_legal_port


class Milvus:
@deprecated
def __init__(self, host=None, port=config.GRPC_PORT, uri=config.GRPC_URI, channel=None, **kwargs):
def __init__(self, host=None, port=Config.GRPC_PORT, uri=Config.GRPC_URI, channel=None, **kwargs):
self.address = self.__get_address(host, port, uri)
self._handler = GrpcHandler(address=self.address, channel=channel, **kwargs)

if kwargs.get("pre_ping", False) is True:
self._handler._wait_for_channel_ready()

def __get_address(self, host=None, port=config.GRPC_PORT, uri=config.GRPC_URI):
def __get_address(self, host=None, port=Config.GRPC_PORT, uri=Config.GRPC_URI):
if host is None and uri is None:
raise ParamError(message='Host and uri cannot both be None')

Expand Down
13 changes: 6 additions & 7 deletions pymilvus/orm/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@
)
from .future import SearchFuture, MutationFuture
from .utility import _get_connection
from .default_config import DefaultConfig
from ..settings import Config
from ..client.types import CompactionState, CompactionPlans, Replica, get_consistency_level, cmp_consistency_level
from ..client.constants import DEFAULT_CONSISTENCY_LEVEL
from ..client.configs import DefaultConfigs



Expand Down Expand Up @@ -167,7 +166,7 @@ def construct_from_dataframe(cls, name, dataframe, **kwargs):
else:
raise SchemaNotReadyException(message=ExceptionsMessage.AutoIDWithData)

using = kwargs.get("using", DefaultConfig.DEFAULT_USING)
using = kwargs.get("using", Config.MILVUS_CONN_ALIAS)
conn = _get_connection(using)
if conn.has_collection(name, **kwargs):
resp = conn.describe_collection(name, **kwargs)
Expand All @@ -186,7 +185,7 @@ def construct_from_dataframe(cls, name, dataframe, **kwargs):
field.is_primary = True
field.auto_id = False
if field.dtype == DataType.VARCHAR:
field.params[DefaultConfigs.MaxVarCharLengthKey] = int(DefaultConfigs.MaxVarCharLength)
field.params[Config.MaxVarCharLengthKey] = int(Config.MaxVarCharLength)
schema = CollectionSchema(fields=fields_schema)

check_schema(schema)
Expand Down Expand Up @@ -951,7 +950,7 @@ def index(self, **kwargs) -> Index:
<pymilvus.index.Index object at 0x7f44355a1460>
"""
copy_kwargs = copy.deepcopy(kwargs)
index_name = copy_kwargs.pop("index_name", DefaultConfigs.IndexName)
index_name = copy_kwargs.pop("index_name", Config.IndexName)
conn = self._get_connection()
tmp_index = conn.describe_index(self._name, index_name, **copy_kwargs)
if tmp_index is not None:
Expand Down Expand Up @@ -1027,7 +1026,7 @@ def has_index(self, timeout=None, **kwargs) -> bool:
"""
conn = self._get_connection()
copy_kwargs = copy.deepcopy(kwargs)
index_name = copy_kwargs.pop("index_name", DefaultConfigs.IndexName)
index_name = copy_kwargs.pop("index_name", Config.IndexName)
if conn.describe_index(self._name, index_name, timeout=timeout, **copy_kwargs) is None:
return False
return True
Expand Down Expand Up @@ -1062,7 +1061,7 @@ def drop_index(self, timeout=None, **kwargs):
False
"""
copy_kwargs = copy.deepcopy(kwargs)
index_name = copy_kwargs.pop("index_name", DefaultConfigs.IndexName)
index_name = copy_kwargs.pop("index_name", Config.IndexName)
conn = self._get_connection()
tmp_index = conn.describe_index(self._name, index_name, timeout=timeout, **copy_kwargs)
if tmp_index is not None:
Expand Down
Loading

0 comments on commit 8c935c4

Please sign in to comment.