| 1 |
from os import SEEK_SET |
| 2 |
import psycopg2 |
| 3 |
import sys |
| 4 |
if (sys.version_info < (3, 0)): |
| 5 |
from StringIO import StringIO |
| 6 |
else: |
| 7 |
from io import StringIO |
| 8 |
|
| 9 |
|
| 10 |
def get_global_snapfuncs_sql(ver): |
| 11 |
"""Get the list of enabled global functions for snapshotting""" |
| 12 |
# XXX should we ignore entries without query_src? |
| 13 |
|
| 14 |
if (int(ver[0][0]) >= 5): |
| 15 |
return """SELECT name, external, query_source, query_cleanup, |
| 16 |
function_name |
| 17 |
FROM {powa}.powa_functions pf |
| 18 |
-- FIXME |
| 19 |
-- JOIN pg_extension ext ON ext.extname = pf.module |
| 20 |
-- JOIN pg_namespace nsp ON nsp.oid = ext.extnamespace |
| 21 |
WHERE operation = 'snapshot' AND enabled |
| 22 |
AND srvid = %s |
| 23 |
ORDER BY priority""" |
| 24 |
else: |
| 25 |
return """SELECT module AS name, false AS external, query_source, |
| 26 |
query_cleanup, function_name |
| 27 |
FROM {powa}.powa_functions pf |
| 28 |
-- FIXME |
| 29 |
-- JOIN pg_extension ext ON ext.extname = pf.module |
| 30 |
-- JOIN pg_namespace nsp ON nsp.oid = ext.extnamespace |
| 31 |
WHERE operation = 'snapshot' AND enabled |
| 32 |
AND srvid = %s |
| 33 |
ORDER BY priority""" |
| 34 |
|
| 35 |
|
| 36 |
def get_src_query(schema, src_fct, srvid): |
| 37 |
""" |
| 38 |
Get the SQL query for global dataousource we'll use to get results from |
| 39 |
a snapshot function |
| 40 |
""" |
| 41 |
return ("SELECT %(srvid)d, * FROM %(schema)s.%(fname)s(0)" % |
| 42 |
{'fname': src_fct, 'schema': schema, 'srvid': srvid}) |
| 43 |
|
| 44 |
|
| 45 |
def get_db_mod_snapfuncs_sql(srvid, server_version_num): |
| 46 |
""" |
| 47 |
Get the SQL query for a db_module we'll use to get results from a snapshot |
| 48 |
function |
| 49 |
""" |
| 50 |
return ("""SELECT db_module, query_source, tmp_table, dbnames |
| 51 |
FROM {powa}.powa_db_functions(%(srvid)d, %(server_version_num)d) |
| 52 |
WHERE operation = 'snapshot' |
| 53 |
ORDER BY priority |
| 54 |
""" % {'srvid': srvid, 'server_version_num': server_version_num}) |
| 55 |
|
| 56 |
|
| 57 |
def get_db_cat_snapfuncs_sql(srvid, server_version_num, force=False): |
| 58 |
""" |
| 59 |
Get the SQL query for a db catalog we'll use to get results from a snapshot |
| 60 |
function |
| 61 |
""" |
| 62 |
if force: |
| 63 |
interval = 'NULL' |
| 64 |
else: |
| 65 |
interval = "'1 year'" |
| 66 |
|
| 67 |
return ("""SELECT catname, query_source, tmp_table, excluded_dbnames |
| 68 |
FROM {powa}.powa_catalog_functions( |
| 69 |
%(srvid)d, %(server_version_num)d, %(interval)s |
| 70 |
)""" % {'srvid': srvid, |
| 71 |
'server_version_num': server_version_num, |
| 72 |
'interval': interval}) |
| 73 |
|
| 74 |
|
| 75 |
def get_global_tmp_name(schema, src_fct): |
| 76 |
"""Get the temp table name we'll use to spool changes""" |
| 77 |
return "%s.%s_tmp" % (schema, src_fct) |
| 78 |
|
| 79 |
|
| 80 |
def get_nsp(conn, external, module): |
| 81 |
""" |
| 82 |
Returns the given module schema. |
| 83 |
If "external" is true, then it's an external extension so returns its |
| 84 |
associated schema. |
| 85 |
If "external" is false then it's an internal module so return the powa |
| 86 |
extension schema instead. |
| 87 |
|
| 88 |
Returns None if the module is not found. Caller is responsible for |
| 89 |
reporting an appropriate error in that case. |
| 90 |
""" |
| 91 |
if external: |
| 92 |
return conn._nsps.get(module, None) |
| 93 |
else: |
| 94 |
return conn._nsps.get('powa', None) |
| 95 |
|
| 96 |
def copy_remote_data_to_repo(cls, data_name, |
| 97 |
data_src, data_src_sql, data_ins, target_tbl_name, |
| 98 |
cleanup_sql=None): |
| 99 |
""" |
| 100 |
Retrieve the wanted datasource from the given connection and insert it on |
| 101 |
the repository server in the given table. |
| 102 |
|
| 103 |
data_src: the cursor to use to get the data on the remote server |
| 104 |
data_src_sql: the SQL query to execute to get the datasource data |
| 105 |
data_name: a string describing the datasource |
| 106 |
data_ins: the cursor to use to write the data on the repository server |
| 107 |
target_tbl_name: a string containing the (fully qualified and properly |
| 108 |
quoted) target table name to COPY data to on the |
| 109 |
repository server |
| 110 |
cleanup_sql: an optional SQL query to execute after executing data_src_sql. |
| 111 |
Note that this query is executed even if there's an error |
| 112 |
during data_src_sql execution |
| 113 |
""" |
| 114 |
errors = [] |
| 115 |
src_ok = True |
| 116 |
|
| 117 |
# use savepoint, there could be an error while retrieving data on the |
| 118 |
# remote server. |
| 119 |
data_src.execute("SAVEPOINT src") |
| 120 |
|
| 121 |
# XXX should we use os.pipe() or a temp file instead, to avoid too |
| 122 |
# much memory consumption? |
| 123 |
buf = StringIO() |
| 124 |
try: |
| 125 |
data_src.copy_expert("COPY (%s) TO stdout" % data_src_sql, buf) |
| 126 |
data_src.execute("RELEASE src") |
| 127 |
except psycopg2.Error as e: |
| 128 |
src_ok = False |
| 129 |
err = "Error retrieving datasource data %s:\n%s" % (data_name, e) |
| 130 |
errors.append(err) |
| 131 |
data_src.execute("ROLLBACK TO src") |
| 132 |
|
| 133 |
# execute the cleanup query if provided |
| 134 |
if (cleanup_sql is not None): |
| 135 |
data_src.execute("SAVEPOINT src") |
| 136 |
try: |
| 137 |
cls.logger.debug("Calling %s..." % cleanup_sql) |
| 138 |
data_src.execute(cleanup_sql) |
| 139 |
data_src.execute("RELEASE src") |
| 140 |
except psycopg2.Error as e: |
| 141 |
err = "Error while calling %s:\n%s" % (cleanup_sql, e) |
| 142 |
errors.append(err) |
| 143 |
data_src.execute("ROLLBACK TO src") |
| 144 |
|
| 145 |
# If user want to stop the collector or if any error happened during |
| 146 |
# datasource retrieval, there's nothing more to do so simply inform caller |
| 147 |
# of the errors. |
| 148 |
if (cls.is_stopping() or not src_ok): |
| 149 |
return errors |
| 150 |
|
| 151 |
# insert the data to the transient unlogged table |
| 152 |
data_ins.execute("SAVEPOINT data") |
| 153 |
buf.seek(0, SEEK_SET) |
| 154 |
try: |
| 155 |
# For data import the schema is now on the repository server |
| 156 |
data_ins.copy_expert("COPY %s FROM stdin" % target_tbl_name, buf) |
| 157 |
data_ins.execute("RELEASE data") |
| 158 |
except psycopg2.Error as e: |
| 159 |
err = "Error while inserting data:\n%s" % e |
| 160 |
cls.logger.warning(err) |
| 161 |
errors.append(err) |
| 162 |
cls.logger.warning("Giving up for datasource %s", data_name) |
| 163 |
data_ins.execute("ROLLBACK TO data") |
| 164 |
|
| 165 |
buf.close() |
| 166 |
|
| 167 |
return errors |