Skip to content

Commit

Permalink
[FIX]v1.5 mysql fix (#371)
Browse files Browse the repository at this point in the history
* add log

* fix

* remove useless log

* remove useless log

* remove useless log

* fix
  • Loading branch information
TelmaZzzz authored Oct 20, 2020
1 parent 3eb9b5c commit 97718ae
Showing 1 changed file with 2 additions and 17 deletions.
19 changes: 2 additions & 17 deletions fedlearner/common/mysql_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ def __init__(self, database, addr, user, password, base_dir):
self._addr = ''
self._password = ''
self._base_dir = base_dir
if self._base_dir[0] != '/':
self._base_dir = '/' + self._base_dir
self._create_engine_inner()
logging.info('success to create table')

def get_data(self, key):
with self.closing(self._engine) as sess:
Expand All @@ -59,10 +60,8 @@ def get_data(self, key):
self._generate_key(key)).one().kv_value
if isinstance(value, str):
return value.encode()
logging.info('success to get data')
return value
except NoResultFound:
logging.warning('data is not exists')
return None
except Exception as e: # pylint: disable=broad-except
logging.error('failed to get data. msg[%s]', e)
Expand All @@ -84,7 +83,6 @@ def set_data(self, key, data):
kv_value=data)
sess.add(context)
sess.commit()
logging.info('success to set data')
return True
except Exception as e: # pylint: disable=broad-except
logging.error('failed to set data. msg[%s]', e)
Expand All @@ -100,7 +98,6 @@ def delete(self, key):
self._generate_key(key)):
sess.delete(context)
sess.commit()
logging.info('success to delete')
return True
except Exception as e: # pylint: disable=broad-except
logging.error('failed to delete. msg[%s]', e)
Expand All @@ -115,7 +112,6 @@ def delete_prefix(self, key):
like(self._generate_key(key) + '%')):
sess.delete(context)
sess.commit()
logging.info('success to delete prefix')
return True
except Exception as e: # pylint: disable=broad-except
logging.error('failed to delete prefix. msg[%s]', e)
Expand All @@ -138,12 +134,9 @@ def cas(self, key, old_data, new_data):
self._generate_key(key)).one()
if context.kv_value != old_data:
flag = False
logging.warning('old data and new data \
are not the same')
return flag
context.kv_value = new_data
sess.commit()
logging.info('success to cas')
return flag
except Exception as e: # pylint: disable=broad-except
logging.error('failed to cas. msg[%s]', e)
Expand All @@ -154,14 +147,10 @@ def get_prefix_kvs(self, prefix, ignor_prefix=False):
kvs = []
path = self._generate_key(prefix)
with self.closing(self._engine) as sess:
logging.info('start get_prefix_kvs. prefix is [%s] [%s]',
prefix, path)
try:
table = self._datasource_meta
for context in sess.query(table).filter(table.kv_key.\
like(path + '%')).order_by(table.kv_key):
logging.info('type of kv_key is[%s]',
type(context.kv_key))
if ignor_prefix and context.kv_key == path:
continue
nkey = self._normalize_output_key(context.kv_key,
Expand All @@ -172,7 +161,6 @@ def get_prefix_kvs(self, prefix, ignor_prefix=False):
if isinstance(value, str):
value = value.encode()
kvs.append((nkey, value))
logging.info('success to get prefix kvs')
return kvs
except Exception as e: # pylint: disable=broad-except
logging.error('failed to get prefix kvs. msg[%s]', e)
Expand All @@ -194,8 +182,6 @@ def _normalize_input_key(key):

@staticmethod
def _normalize_output_key(key, base_dir):
logging.info('normalize ouput key is[%s] type[%s]', key,
type(key))
if isinstance(key, str):
assert key.startswith(base_dir)
else:
Expand All @@ -212,7 +198,6 @@ def _create_engine_inner(self):
if self._unix_socket:
sub = '?unix_socket={}'.format(self._unix_socket)
conn_string = conn_string + sub
logging.info('conn_string is [%s]', conn_string)
self._engine = create_engine(conn_string, echo=False,
pool_recycle=180)
Base = automap_base()
Expand Down

0 comments on commit 97718ae

Please sign in to comment.