kaqing 2.0.110__py3-none-any.whl → 2.0.214__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of kaqing might be problematic. Click here for more details.

Files changed (251) hide show
  1. adam/__init__.py +0 -2
  2. adam/app_session.py +9 -12
  3. adam/apps.py +18 -4
  4. adam/batch.py +19 -19
  5. adam/checks/check_utils.py +16 -46
  6. adam/checks/cpu.py +7 -1
  7. adam/checks/cpu_metrics.py +52 -0
  8. adam/checks/disk.py +2 -3
  9. adam/columns/columns.py +3 -1
  10. adam/columns/cpu.py +3 -1
  11. adam/columns/cpu_metrics.py +22 -0
  12. adam/columns/memory.py +3 -4
  13. adam/commands/__init__.py +24 -0
  14. adam/commands/app/app.py +38 -0
  15. adam/commands/{app_ping.py → app/app_ping.py} +7 -13
  16. adam/commands/{login.py → app/login.py} +22 -24
  17. adam/commands/app/show_app_actions.py +49 -0
  18. adam/commands/{show → app}/show_app_id.py +8 -11
  19. adam/commands/{show → app}/show_app_queues.py +7 -14
  20. adam/commands/app/show_login.py +56 -0
  21. adam/commands/app/utils_app.py +106 -0
  22. adam/commands/audit/audit.py +22 -40
  23. adam/commands/audit/audit_repair_tables.py +15 -19
  24. adam/commands/audit/audit_run.py +15 -22
  25. adam/commands/audit/completions_l.py +15 -0
  26. adam/commands/audit/show_last10.py +4 -18
  27. adam/commands/audit/show_slow10.py +4 -17
  28. adam/commands/audit/show_top10.py +4 -16
  29. adam/commands/audit/utils_show_top10.py +15 -3
  30. adam/commands/bash/__init__.py +5 -0
  31. adam/commands/bash/bash.py +36 -0
  32. adam/commands/bash/bash_completer.py +93 -0
  33. adam/commands/bash/utils_bash.py +16 -0
  34. adam/commands/cassandra/__init__.py +0 -0
  35. adam/commands/cassandra/download_cassandra_log.py +45 -0
  36. adam/commands/{restart.py → cassandra/restart_cluster.py} +12 -26
  37. adam/commands/cassandra/restart_node.py +51 -0
  38. adam/commands/cassandra/restart_nodes.py +47 -0
  39. adam/commands/{rollout.py → cassandra/rollout.py} +20 -25
  40. adam/commands/cassandra/show_cassandra_repairs.py +37 -0
  41. adam/commands/cassandra/show_cassandra_status.py +117 -0
  42. adam/commands/{show → cassandra}/show_cassandra_version.py +5 -18
  43. adam/commands/cassandra/show_processes.py +50 -0
  44. adam/commands/cassandra/show_storage.py +44 -0
  45. adam/commands/{watch.py → cassandra/watch.py} +26 -29
  46. adam/commands/cli/__init__.py +0 -0
  47. adam/commands/{cli_commands.py → cli/cli_commands.py} +8 -4
  48. adam/commands/cli/clipboard_copy.py +86 -0
  49. adam/commands/cli/show_cli_commands.py +56 -0
  50. adam/commands/code.py +57 -0
  51. adam/commands/command.py +211 -40
  52. adam/commands/commands_utils.py +20 -27
  53. adam/commands/config/__init__.py +0 -0
  54. adam/commands/{param_get.py → config/param_get.py} +11 -14
  55. adam/commands/{param_set.py → config/param_set.py} +8 -12
  56. adam/commands/{show → config}/show_params.py +2 -5
  57. adam/commands/cql/alter_tables.py +66 -0
  58. adam/commands/cql/completions_c.py +29 -0
  59. adam/commands/cql/cqlsh.py +10 -32
  60. adam/commands/cql/utils_cql.py +306 -0
  61. adam/commands/debug/__init__.py +0 -0
  62. adam/commands/debug/debug.py +22 -0
  63. adam/commands/debug/debug_completes.py +35 -0
  64. adam/commands/debug/debug_timings.py +35 -0
  65. adam/commands/debug/show_offloaded_completes.py +45 -0
  66. adam/commands/deploy/code_start.py +7 -10
  67. adam/commands/deploy/code_stop.py +4 -21
  68. adam/commands/deploy/code_utils.py +3 -3
  69. adam/commands/deploy/deploy.py +4 -27
  70. adam/commands/deploy/deploy_frontend.py +14 -17
  71. adam/commands/deploy/deploy_pg_agent.py +3 -6
  72. adam/commands/deploy/deploy_pod.py +65 -73
  73. adam/commands/deploy/deploy_utils.py +14 -24
  74. adam/commands/deploy/undeploy.py +4 -27
  75. adam/commands/deploy/undeploy_frontend.py +4 -7
  76. adam/commands/deploy/undeploy_pg_agent.py +6 -8
  77. adam/commands/deploy/undeploy_pod.py +11 -12
  78. adam/commands/devices/__init__.py +0 -0
  79. adam/commands/devices/device.py +149 -0
  80. adam/commands/devices/device_app.py +163 -0
  81. adam/commands/devices/device_auit_log.py +49 -0
  82. adam/commands/devices/device_cass.py +179 -0
  83. adam/commands/devices/device_export.py +87 -0
  84. adam/commands/devices/device_postgres.py +160 -0
  85. adam/commands/devices/devices.py +25 -0
  86. adam/commands/diag/__init__.py +0 -0
  87. adam/commands/{check.py → diag/check.py} +16 -25
  88. adam/commands/diag/generate_report.py +52 -0
  89. adam/commands/diag/issues.py +43 -0
  90. adam/commands/exit.py +1 -4
  91. adam/commands/export/__init__.py +0 -0
  92. adam/commands/export/clean_up_all_export_sessions.py +37 -0
  93. adam/commands/export/clean_up_export_sessions.py +39 -0
  94. adam/commands/export/completions_x.py +11 -0
  95. adam/commands/export/download_export_session.py +40 -0
  96. adam/commands/export/drop_export_database.py +39 -0
  97. adam/commands/export/drop_export_databases.py +37 -0
  98. adam/commands/export/export.py +37 -0
  99. adam/commands/export/export_databases.py +251 -0
  100. adam/commands/export/export_select.py +34 -0
  101. adam/commands/export/export_sessions.py +210 -0
  102. adam/commands/export/export_use.py +49 -0
  103. adam/commands/export/export_x_select.py +48 -0
  104. adam/commands/export/exporter.py +419 -0
  105. adam/commands/export/import_files.py +44 -0
  106. adam/commands/export/import_session.py +40 -0
  107. adam/commands/export/importer.py +81 -0
  108. adam/commands/export/importer_athena.py +157 -0
  109. adam/commands/export/importer_sqlite.py +78 -0
  110. adam/commands/export/show_column_counts.py +45 -0
  111. adam/commands/export/show_export_databases.py +39 -0
  112. adam/commands/export/show_export_session.py +39 -0
  113. adam/commands/export/show_export_sessions.py +37 -0
  114. adam/commands/export/utils_export.py +366 -0
  115. adam/commands/fs/__init__.py +0 -0
  116. adam/commands/fs/cat.py +36 -0
  117. adam/commands/fs/cat_local.py +42 -0
  118. adam/commands/fs/cd.py +41 -0
  119. adam/commands/fs/download_file.py +47 -0
  120. adam/commands/fs/find_files.py +51 -0
  121. adam/commands/fs/find_processes.py +76 -0
  122. adam/commands/fs/head.py +36 -0
  123. adam/commands/fs/ls.py +41 -0
  124. adam/commands/fs/ls_local.py +40 -0
  125. adam/commands/fs/pwd.py +45 -0
  126. adam/commands/fs/rm.py +18 -0
  127. adam/commands/fs/rm_downloads.py +39 -0
  128. adam/commands/fs/rm_logs.py +38 -0
  129. adam/commands/{shell.py → fs/shell.py} +12 -4
  130. adam/commands/{show → fs}/show_adam.py +3 -3
  131. adam/commands/{show → fs}/show_host.py +1 -1
  132. adam/commands/help.py +5 -3
  133. adam/commands/intermediate_command.py +52 -0
  134. adam/commands/kubectl.py +38 -0
  135. adam/commands/medusa/medusa.py +4 -22
  136. adam/commands/medusa/medusa_backup.py +20 -27
  137. adam/commands/medusa/medusa_restore.py +35 -48
  138. adam/commands/medusa/medusa_show_backupjobs.py +16 -18
  139. adam/commands/medusa/medusa_show_restorejobs.py +13 -18
  140. adam/commands/medusa/utils_medusa.py +15 -0
  141. adam/commands/nodetool/__init__.py +0 -0
  142. adam/commands/{nodetool.py → nodetool/nodetool.py} +9 -20
  143. adam/commands/postgres/completions_p.py +22 -0
  144. adam/commands/postgres/postgres.py +47 -55
  145. adam/commands/postgres/postgres_databases.py +269 -0
  146. adam/commands/postgres/postgres_ls.py +5 -9
  147. adam/commands/postgres/postgres_preview.py +5 -9
  148. adam/commands/postgres/utils_postgres.py +80 -0
  149. adam/commands/preview_table.py +8 -44
  150. adam/commands/reaper/reaper.py +4 -27
  151. adam/commands/reaper/reaper_forward.py +49 -56
  152. adam/commands/reaper/reaper_forward_session.py +6 -0
  153. adam/commands/reaper/reaper_forward_stop.py +10 -16
  154. adam/commands/reaper/reaper_restart.py +7 -14
  155. adam/commands/reaper/reaper_run_abort.py +8 -33
  156. adam/commands/reaper/reaper_runs.py +43 -58
  157. adam/commands/reaper/reaper_runs_abort.py +29 -49
  158. adam/commands/reaper/reaper_schedule_activate.py +14 -33
  159. adam/commands/reaper/reaper_schedule_start.py +9 -33
  160. adam/commands/reaper/reaper_schedule_stop.py +9 -33
  161. adam/commands/reaper/reaper_schedules.py +4 -14
  162. adam/commands/reaper/reaper_status.py +8 -16
  163. adam/commands/reaper/utils_reaper.py +203 -0
  164. adam/commands/repair/repair.py +4 -22
  165. adam/commands/repair/repair_log.py +5 -11
  166. adam/commands/repair/repair_run.py +27 -34
  167. adam/commands/repair/repair_scan.py +32 -40
  168. adam/commands/repair/repair_stop.py +5 -12
  169. adam/commands/show.py +40 -0
  170. adam/config.py +5 -15
  171. adam/embedded_params.py +1 -1
  172. adam/log.py +4 -4
  173. adam/repl.py +83 -116
  174. adam/repl_commands.py +86 -45
  175. adam/repl_session.py +9 -1
  176. adam/repl_state.py +176 -40
  177. adam/sql/async_executor.py +62 -0
  178. adam/sql/lark_completer.py +286 -0
  179. adam/sql/lark_parser.py +604 -0
  180. adam/sql/qingl.lark +1076 -0
  181. adam/sql/sql_completer.py +52 -27
  182. adam/sql/sql_state_machine.py +131 -19
  183. adam/sso/authn_ad.py +6 -8
  184. adam/sso/authn_okta.py +4 -6
  185. adam/sso/cred_cache.py +4 -9
  186. adam/sso/idp.py +9 -12
  187. adam/utils.py +670 -31
  188. adam/utils_athena.py +145 -0
  189. adam/utils_audits.py +12 -103
  190. adam/utils_issues.py +32 -0
  191. adam/utils_k8s/app_clusters.py +35 -0
  192. adam/utils_k8s/app_pods.py +41 -0
  193. adam/utils_k8s/cassandra_clusters.py +35 -20
  194. adam/utils_k8s/cassandra_nodes.py +15 -6
  195. adam/utils_k8s/custom_resources.py +16 -17
  196. adam/utils_k8s/ingresses.py +2 -2
  197. adam/utils_k8s/jobs.py +7 -11
  198. adam/utils_k8s/k8s.py +96 -0
  199. adam/utils_k8s/kube_context.py +3 -6
  200. adam/{pod_exec_result.py → utils_k8s/pod_exec_result.py} +13 -4
  201. adam/utils_k8s/pods.py +159 -89
  202. adam/utils_k8s/secrets.py +4 -4
  203. adam/utils_k8s/service_accounts.py +5 -4
  204. adam/utils_k8s/services.py +2 -2
  205. adam/utils_k8s/statefulsets.py +6 -14
  206. adam/utils_local.py +80 -0
  207. adam/utils_net.py +4 -4
  208. adam/utils_repl/__init__.py +0 -0
  209. adam/utils_repl/appendable_completer.py +6 -0
  210. adam/utils_repl/automata_completer.py +48 -0
  211. adam/utils_repl/repl_completer.py +93 -0
  212. adam/utils_repl/state_machine.py +173 -0
  213. adam/utils_sqlite.py +132 -0
  214. adam/version.py +1 -1
  215. {kaqing-2.0.110.dist-info → kaqing-2.0.214.dist-info}/METADATA +1 -1
  216. kaqing-2.0.214.dist-info/RECORD +272 -0
  217. kaqing-2.0.214.dist-info/top_level.txt +2 -0
  218. teddy/__init__.py +0 -0
  219. teddy/lark_parser.py +436 -0
  220. teddy/lark_parser2.py +618 -0
  221. adam/commands/alter_tables.py +0 -81
  222. adam/commands/app.py +0 -67
  223. adam/commands/bash.py +0 -150
  224. adam/commands/cd.py +0 -125
  225. adam/commands/cp.py +0 -95
  226. adam/commands/cql/cql_completions.py +0 -15
  227. adam/commands/cql/cql_utils.py +0 -112
  228. adam/commands/devices.py +0 -118
  229. adam/commands/issues.py +0 -75
  230. adam/commands/logs.py +0 -40
  231. adam/commands/ls.py +0 -146
  232. adam/commands/postgres/postgres_context.py +0 -239
  233. adam/commands/postgres/postgres_utils.py +0 -31
  234. adam/commands/postgres/psql_completions.py +0 -10
  235. adam/commands/pwd.py +0 -77
  236. adam/commands/reaper/reaper_session.py +0 -159
  237. adam/commands/report.py +0 -63
  238. adam/commands/show/show.py +0 -54
  239. adam/commands/show/show_app_actions.py +0 -56
  240. adam/commands/show/show_cassandra_status.py +0 -128
  241. adam/commands/show/show_commands.py +0 -61
  242. adam/commands/show/show_login.py +0 -63
  243. adam/commands/show/show_processes.py +0 -53
  244. adam/commands/show/show_repairs.py +0 -47
  245. adam/commands/show/show_storage.py +0 -52
  246. kaqing-2.0.110.dist-info/RECORD +0 -187
  247. kaqing-2.0.110.dist-info/top_level.txt +0 -1
  248. /adam/commands/{show → app}/__init__.py +0 -0
  249. /adam/commands/{nodetool_commands.py → nodetool/nodetool_commands.py} +0 -0
  250. {kaqing-2.0.110.dist-info → kaqing-2.0.214.dist-info}/WHEEL +0 -0
  251. {kaqing-2.0.110.dist-info → kaqing-2.0.214.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,419 @@
1
+ from datetime import datetime
2
+ import os
3
+ import time
4
+ import traceback
5
+
6
+ from adam.commands.command import InvalidArgumentsException
7
+ from adam.commands.cql.utils_cql import cassandra_table_names, run_cql, table_spec
8
+ from adam.commands.export.export_databases import export_db
9
+ from adam.commands.export.export_sessions import ExportSessions
10
+ from adam.commands.export.importer import Importer
11
+ from adam.commands.export.importer_athena import AthenaImporter
12
+ from adam.commands.export.importer_sqlite import SqliteImporter
13
+ from adam.commands.export.utils_export import ExportSpec, ExportTableStatus, ExportTableSpec, ImportSpec, csv_dir, export_log_dir, find_files, os_system_exec, state_with_pod
14
+ from adam.config import Config
15
+ from adam.repl_session import ReplSession
16
+ from adam.repl_state import ReplState
17
+ from adam.utils import debug, kaqing_log_file_name, log, offload, parallelize, log2, ing, log_exc
18
+ from adam.utils_k8s.cassandra_nodes import CassandraNodes
19
+
20
+ class Exporter:
21
+ def export_tables(args: list[str], state: ReplState, export_only: bool = False, max_workers = 0) -> tuple[list[str], ExportSpec]:
22
+ if export_only:
23
+ log2('export-only for testing')
24
+
25
+ spec: ExportSpec = None
26
+ with log_exc(True):
27
+ spec = Exporter.export_spec(' '.join(args), state)
28
+
29
+ statuses, spec = Exporter._export_tables(spec, state, max_workers=max_workers, export_state='init')
30
+ if not statuses:
31
+ return statuses, spec
32
+
33
+ return Exporter._export_tables(spec, state, export_only, max_workers, 'pending_export')
34
+
35
+ return [], None
36
+
37
+ def export_spec(spec_str: str, state: ReplState):
38
+ spec: ExportSpec = ExportSpec.parse_specs(spec_str)
39
+
40
+ session = state.export_session
41
+ if session:
42
+ if spec.importer:
43
+ importer_from_session = Importer.importer_from_session(session)
44
+ if spec.importer != importer_from_session:
45
+ if spec.importer == 'csv':
46
+ prefix = Importer.prefix_from_importer(spec.importer)
47
+ session = f'{prefix}{session[1:]}'
48
+ else:
49
+ raise Exception(f"You're currently using {importer_from_session} export database. You cannot export tables with {spec.importer} type database.")
50
+ else:
51
+ spec.importer = Importer.importer_from_session(session)
52
+
53
+ if spec.importer == 'athena' and not AthenaImporter.ping():
54
+ raise Exception('Credentials for Athena is not present.')
55
+ else:
56
+ if not spec.importer:
57
+ spec.importer = Config().get('export.default-importer', 'sqlite')
58
+
59
+ prefix = Importer.prefix_from_importer(spec.importer)
60
+ session = f'{prefix}{datetime.now().strftime("%Y%m%d%H%M%S")[3:]}'
61
+
62
+ if spec.importer == 'athena' and not AthenaImporter.ping():
63
+ raise Exception('Credentials for Athena is not present.')
64
+
65
+ if spec.importer != 'csv':
66
+ state.export_session = session
67
+
68
+ spec.session = session
69
+
70
+ return spec
71
+
72
+ def import_session(spec_str: str, state: ReplState, max_workers = 0) -> tuple[list[str], ExportSpec]:
73
+ import_spec: ImportSpec = None
74
+ with log_exc(True):
75
+ import_spec = Exporter.import_spec(spec_str, state)
76
+ tables, status_in_whole = ExportTableStatus.from_session(state.sts, state.pod, state.namespace, import_spec.session)
77
+ if status_in_whole == 'done':
78
+ log2(f'The session has been completely done - no more csv files are found.')
79
+ return [], ExportSpec(None, None, importer=import_spec.importer, tables=[])
80
+
81
+ spec = ExportSpec(None, None, importer=import_spec.importer, tables=[ExportTableSpec.from_status(table) for table in tables], session=import_spec.session)
82
+
83
+ return Exporter._export_tables(spec, state, max_workers=max_workers, export_state = 'import')
84
+
85
+ return [], None
86
+
87
+ def import_local_csv_files(spec_str: str, state: ReplState, max_workers = 0) -> tuple[list[str], ExportSpec]:
88
+ spec: ImportSpec = None
89
+ with log_exc(True):
90
+ spec = Exporter.import_spec(spec_str, state, files=True)
91
+ if not spec.table_name:
92
+ log2(f"Use 'as <database-name>.<table-name>'.")
93
+ raise InvalidArgumentsException()
94
+
95
+ d_t = spec.table_name.split('.')
96
+ if len(d_t) != 2:
97
+ log2(f'Need <database-name>.<table-name> format for target table.')
98
+ raise InvalidArgumentsException()
99
+
100
+ database = d_t[0]
101
+ table = d_t[1]
102
+ im = AthenaImporter() if spec.importer == 'athena' else SqliteImporter()
103
+
104
+ with parallelize(spec.files, max_workers, msg='Importing|Imported {size} csv files') as exec:
105
+ return exec.map(lambda f: im.import_from_local_csv(state, database, table, f, len(spec.files) > 1, True)), spec
106
+
107
+ return [], None
108
+
109
+ def import_spec(spec_str: str, state: ReplState, files = False):
110
+ spec: ImportSpec = ImportSpec.parse_specs(spec_str, files=files)
111
+
112
+ session = state.export_session
113
+ if session:
114
+ if spec.importer:
115
+ importer = Importer.importer_from_session(state.export_session)
116
+ if spec.importer != importer:
117
+ raise Exception(f"You're currently using {importer} export database. You cannot import to {spec.importer} type database.")
118
+ else:
119
+ spec.importer = Importer.importer_from_session(state.export_session)
120
+ if not spec.importer:
121
+ spec.importer = Config().get('export.default-importer', 'sqlite')
122
+
123
+ if spec.importer == 'athena' and not AthenaImporter.ping():
124
+ raise Exception('Credentials for Athena is not present.')
125
+ else:
126
+ if not spec.importer:
127
+ spec.importer = Importer.importer_from_session(spec.session)
128
+
129
+ if not spec.importer or spec.importer == 'csv':
130
+ spec.importer = Config().get('export.default-importer', 'sqlite')
131
+
132
+ if spec.importer == 'athena' and not AthenaImporter.ping():
133
+ raise Exception('Credentials for Athena is not present.')
134
+
135
+ prefix = Importer.prefix_from_importer(spec.importer)
136
+ if spec.session:
137
+ state.export_session = f'{prefix}{spec.session[1:]}'
138
+ else:
139
+ state.export_session = f'{prefix}{datetime.now().strftime("%Y%m%d%H%M%S")[3:]}'
140
+
141
+ return spec
142
+
143
+ def _export_tables(spec: ExportSpec, state: ReplState, export_only = False, max_workers = 0, export_state = None) -> tuple[list[str], ExportSpec]:
144
+ if not spec.keyspace:
145
+ spec.keyspace = f'{state.namespace}_db'
146
+
147
+ if not spec.tables:
148
+ spec.tables = [ExportTableSpec.parse(t) for t in cassandra_table_names(state, keyspace=spec.keyspace)]
149
+
150
+ if not max_workers:
151
+ max_workers = Config().action_workers(f'export.{spec.importer}', 8)
152
+
153
+ if export_state == 'init':
154
+ CassandraNodes.exec(state.pod, state.namespace, f'rm -rf {csv_dir()}/{spec.session}_*', show_out=Config().is_debug(), shell='bash')
155
+
156
+ job_log = kaqing_log_file_name()
157
+
158
+ action = f'[{spec.session}] Triggering export of'
159
+ if export_state == 'init':
160
+ action = f'[{spec.session}] Preparing|Prepared'
161
+ elif export_state == 'import':
162
+ action = f'[{spec.session}] Importing|Imported'
163
+
164
+ msg = action + ' {size} Cassandra tables'
165
+
166
+ if export_state != 'init':
167
+ log2(f'[{spec.session}] Logging to {job_log}...')
168
+ ReplSession().append_history(f':cat {job_log}')
169
+
170
+ pod = state.pod
171
+ with parallelize(spec.tables, max_workers, msg=msg, collect=export_state == 'init', name='exporter') as exec:
172
+ return exec.map(lambda table: Exporter.export_table(table,
173
+ state.with_pod(pod),
174
+ spec.session,
175
+ spec.importer,
176
+ export_only,
177
+ len(spec.tables) > 1,
178
+ consistency=spec.consistency,
179
+ export_state=export_state,
180
+ job_log=None if export_state == 'init' else job_log)), spec
181
+
182
+ def export_table(spec: ExportTableSpec,
183
+ state: ReplState,
184
+ session: str,
185
+ importer: str,
186
+ export_only = False,
187
+ multi_tables = True,
188
+ consistency: str = None,
189
+ export_state=None,
190
+ job_log: str = None):
191
+ s: str = None
192
+
193
+ table, target_table, columns = Exporter.resove_table_n_columns(spec, state, include_ks_in_target=False, importer=importer)
194
+
195
+ log_file = f'{export_log_dir()}/{session}_{spec.keyspace}.{target_table}.log'
196
+ create_db = not state.export_session
197
+
198
+ if export_state == 'init':
199
+ Exporter.create_table_log(spec, state, session, table, target_table)
200
+ return 'table_log_created'
201
+ else:
202
+ try:
203
+ if export_state == 'pending_export':
204
+ Exporter.export_to_csv(spec, state, session, table, target_table, columns, multi_tables=multi_tables, consistency=consistency, job_log=job_log)
205
+
206
+ log_files: list[str] = find_files(state.pod, state.namespace, f'{log_file}*')
207
+ if not log_files:
208
+ return s
209
+
210
+ log_file = log_files[0]
211
+
212
+ status: ExportTableStatus = ExportTableStatus.from_log_file(state.pod, state.namespace, session, log_file)
213
+
214
+ with offload(name='exporter') as exec:
215
+ ctx = ExportTableContext(spec, state, session, importer, export_only, multi_tables, table, target_table, columns, create_db, log_file, status, job_log)
216
+ exec.submit(lambda: Exporter.export_loop(ctx))
217
+ # Exporter.export_loop(ExportTableContext(spec, state, session, importer, export_only, multi_tables, table, target_table, columns, create_db, log_file, status))
218
+ except:
219
+ traceback.print_exc()
220
+
221
+ return status.status
222
+
223
+ def export_loop(ctx: 'ExportTableContext'):
224
+ try:
225
+ while ctx.status.status != 'done':
226
+ if ctx.status.status == 'export_in_pregress':
227
+ debug('Exporting to CSV is still in progess, sleeping for 1 sec...')
228
+ time.sleep(1)
229
+ elif ctx.status.status == 'exported':
230
+ ctx.log_file = Exporter.rename_to_pending_import(ctx.spec, ctx.state, ctx.session, ctx.target_table)
231
+ ExportSessions.clear_export_session_cache()
232
+ if ctx.importer == 'csv' or ctx.export_only:
233
+ return 'pending_import'
234
+ elif ctx.status.status == 'pending_import':
235
+ ctx.log_file, ctx.session = Exporter.import_from_csv(ctx.spec,
236
+ ctx.state,
237
+ ctx.session,
238
+ ctx.importer,
239
+ ctx.table,
240
+ ctx.target_table,
241
+ ctx.columns,
242
+ multi_tables=ctx.multi_tables,
243
+ create_db=ctx.create_db,
244
+ job_log=ctx.f)
245
+
246
+ ctx.status = ExportTableStatus.from_log_file(ctx.state.pod, ctx.state.namespace, ctx.session, ctx.log_file)
247
+
248
+ return ctx.status.status
249
+ except:
250
+ traceback.print_exc()
251
+
252
+ def create_table_log(spec: ExportTableSpec, state: ReplState, session: str, table: str, target_table: str):
253
+ log_file = f'{export_log_dir()}/{session}_{spec.keyspace}.{target_table}.log'
254
+ dir = os.path.dirname(log_file)
255
+
256
+ cmd = f'rm -f {log_file}* && mkdir -p {dir} && touch {log_file}'
257
+ os_system_exec(cmd, show_out=Config().is_debug())
258
+
259
+ return table
260
+
261
+ def export_to_csv(spec: ExportTableSpec,
262
+ state: ReplState,
263
+ session: str,
264
+ table: str,
265
+ target_table: str,
266
+ columns: str,
267
+ multi_tables = True,
268
+ consistency: str = None,
269
+ job_log: str = None):
270
+ db = f'{session}_{target_table}'
271
+
272
+ CassandraNodes.exec(state.pod, state.namespace, f'mkdir -p {csv_dir()}/{db}', show_out=Config().is_debug(), shell='bash')
273
+ csv_file = f'{csv_dir()}/{db}/{table}.csv'
274
+ table_log_file = f'{export_log_dir()}/{session}_{spec.keyspace}.{target_table}.log'
275
+
276
+ suppress_ing_log = Config().is_debug() or multi_tables
277
+ queries = []
278
+ if consistency:
279
+ queries.append(f'CONSISTENCY {consistency}')
280
+ queries.append(f"COPY {spec.keyspace}.{table}({columns}) TO '{csv_file}' WITH HEADER = TRUE")
281
+
282
+ with ing(f'[{session}] Triggering dump of table {spec.keyspace}.{table}{f" with consistency {consistency}" if consistency else ""}',
283
+ suppress_log=suppress_ing_log,
284
+ job_log = job_log):
285
+ run_cql(state, ';'.join(queries), show_out=Config().is_debug(), backgrounded=True, log_file=table_log_file, history=False)
286
+
287
+ return table_log_file
288
+
289
+ def rename_to_pending_import(spec: ExportTableSpec, state: ReplState, session: str, target_table: str):
290
+ log_file = f'{export_log_dir()}/{session}_{spec.keyspace}.{target_table}.log'
291
+ to = f'{log_file}.pending_import'
292
+
293
+ cmd =f'mv {log_file} {to}'
294
+ os_system_exec(cmd, show_out=Config().is_debug())
295
+
296
+ return to
297
+
298
+ def import_from_csv(spec: ExportTableSpec,
299
+ state: ReplState,
300
+ session: str,
301
+ importer: str,
302
+ table: str,
303
+ target_table: str,
304
+ columns: str,
305
+ multi_tables = True,
306
+ create_db = False,
307
+ job_log: str = None):
308
+ im = AthenaImporter() if importer == 'athena' else SqliteImporter()
309
+ return im.import_from_csv(state, session if session else state.export_session, spec.keyspace, table, target_table, columns, multi_tables, create_db, job_log=job_log)
310
+
311
+ def resove_table_n_columns(spec: ExportTableSpec, state: ReplState, include_ks_in_target = False, importer = 'sqlite'):
312
+ table = spec.table
313
+ columns = spec.columns
314
+ if not columns:
315
+ columns = Config().get(f'export.{importer}.columns', f'<keys>')
316
+
317
+ keyspaced_table = f'{spec.keyspace}.{spec.table}'
318
+ if columns == '<keys>':
319
+ columns = ','.join(table_spec(state, keyspaced_table, on_any=True).keys())
320
+ elif columns == '<row-key>':
321
+ columns = table_spec(state, keyspaced_table, on_any=True).row_key()
322
+ elif columns == '*':
323
+ columns = ','.join([c.name for c in table_spec(state, keyspaced_table, on_any=True).columns])
324
+
325
+ if not columns:
326
+ log2(f'ERROR: Empty columns on {table}.')
327
+ return table, None, None
328
+
329
+ target_table = spec.target_table if spec.target_table else table
330
+ if not include_ks_in_target and '.' in target_table:
331
+ target_table = target_table.split('.')[-1]
332
+
333
+ return table, target_table, columns
334
+
335
+ class ExportTableContext:
336
+ def __init__(self, spec: ExportTableSpec, state: ReplState, session: str, importer: str, export_only = False, multi_tables = True, table: str = None, target_table: str = None, columns: str = None, create_db = False, log_file: str = None, status: ExportTableStatus = None, f: str = None):
337
+ self.spec = spec
338
+ self.state = state
339
+ self.session = session
340
+ self.importer = importer
341
+ self.export_only = export_only
342
+ self.multi_tables = multi_tables
343
+ self.table = table
344
+ self.target_table = target_table
345
+ self.columns = columns
346
+ self.create_db = create_db
347
+ self.log_file = log_file
348
+ self.status = status
349
+ self.f = f
350
+
351
+ class ExportService:
352
+ def __init__(self, handler: 'ExporterHandler'):
353
+ self.handler = handler
354
+
355
+ def export(self, args: list[str], export_only=False):
356
+ state = self.handler.state
357
+ export_session = state.export_session
358
+ spec: ExportSpec = None
359
+ try:
360
+ with state_with_pod(state) as state:
361
+ # --export-only for testing only
362
+ statuses, spec = Exporter.export_tables(args, state, export_only=export_only)
363
+ if not statuses:
364
+ return state
365
+
366
+ ExportSessions.clear_export_session_cache()
367
+
368
+ # if spec.importer == 'csv' or export_only:
369
+ # ExportSessions.show_session(state.sts, state.pod, state.namespace, spec.session)
370
+ # else:
371
+ # log()
372
+ # with export_db(state) as dbs:
373
+ # dbs.show_database()
374
+ finally:
375
+ pass
376
+ # if exporting to csv, do not bind the new session id to repl state
377
+ # if spec and spec.importer == 'csv':
378
+ # state.export_session = export_session
379
+
380
+ return state
381
+
382
+ def import_session(self, spec_str: str):
383
+ state = self.handler.state
384
+
385
+ tables, _ = Exporter.import_session(spec_str, state)
386
+ if tables:
387
+ ExportSessions.clear_export_session_cache()
388
+
389
+ log()
390
+ with export_db(state) as dbs:
391
+ dbs.show_database()
392
+
393
+ return state
394
+
395
+ def import_files(self, spec_str: str):
396
+ state = self.handler.state
397
+
398
+ tables, _ = Exporter.import_local_csv_files(spec_str, state)
399
+ if tables:
400
+ ExportSessions.clear_export_session_cache()
401
+
402
+ log()
403
+ with export_db(state) as dbs:
404
+ dbs.show_database()
405
+
406
+ return state
407
+
408
+ class ExporterHandler:
409
+ def __init__(self, state: ReplState):
410
+ self.state = state
411
+
412
+ def __enter__(self):
413
+ return ExportService(self)
414
+
415
+ def __exit__(self, exc_type, exc_val, exc_tb):
416
+ return False
417
+
418
+ def export(state: ReplState):
419
+ return ExporterHandler(state)
@@ -0,0 +1,44 @@
1
+ from adam.commands import validate_args
2
+ from adam.commands.command import Command
3
+ from adam.commands.export.export_sessions import ExportSessions
4
+ from adam.commands.export.exporter import export
5
+ from adam.commands.export.utils_export import state_with_pod
6
+ from adam.repl_state import ReplState, RequiredState
7
+
8
+ class ImportCSVFiles(Command):
9
+ COMMAND = 'import files'
10
+
11
+ # the singleton pattern
12
+ def __new__(cls, *args, **kwargs):
13
+ if not hasattr(cls, 'instance'): cls.instance = super(ImportCSVFiles, cls).__new__(cls)
14
+
15
+ return cls.instance
16
+
17
+ def __init__(self, successor: Command=None):
18
+ super().__init__(successor)
19
+
20
+ def command(self):
21
+ return ImportCSVFiles.COMMAND
22
+
23
+ def required(self):
24
+ return RequiredState.CLUSTER_OR_POD
25
+
26
+ def run(self, cmd: str, state: ReplState):
27
+ if not(args := self.args(cmd)):
28
+ return super().run(cmd, state)
29
+
30
+ with self.validate(args, state) as (args, state):
31
+ with validate_args(args, state, name='file') as spec:
32
+ with state_with_pod(state) as state:
33
+ with export(state) as exporter:
34
+ return exporter.import_files(spec)
35
+
36
+ def completion(self, state: ReplState):
37
+ # warm up cache
38
+ # ExportSessions.export_session_names(state.sts, state.pod, state.namespace)
39
+ # ExportSessions.export_session_names(state.sts, state.pod, state.namespace, export_state='pending_import')
40
+
41
+ return {}
42
+
43
+ def help(self, _: ReplState):
44
+ return f'{ImportCSVFiles.COMMAND} <file-names,...>\t import files in session to Athena or SQLite'
@@ -0,0 +1,40 @@
1
+ from adam.commands import validate_args
2
+ from adam.commands.command import Command
3
+ from adam.commands.export.export_sessions import ExportSessions
4
+ from adam.commands.export.exporter import export
5
+ from adam.commands.export.utils_export import state_with_pod
6
+ from adam.repl_state import ReplState, RequiredState
7
+
8
+ class ImportSession(Command):
9
+ COMMAND = 'import session'
10
+
11
+ # the singleton pattern
12
+ def __new__(cls, *args, **kwargs):
13
+ if not hasattr(cls, 'instance'): cls.instance = super(ImportSession, cls).__new__(cls)
14
+
15
+ return cls.instance
16
+
17
+ def __init__(self, successor: Command=None):
18
+ super().__init__(successor)
19
+
20
+ def command(self):
21
+ return ImportSession.COMMAND
22
+
23
+ def required(self):
24
+ return RequiredState.CLUSTER_OR_POD
25
+
26
+ def run(self, cmd: str, state: ReplState):
27
+ if not(args := self.args(cmd)):
28
+ return super().run(cmd, state)
29
+
30
+ with self.validate(args, state) as (args, state):
31
+ with validate_args(args, state, name='export session') as spec:
32
+ with state_with_pod(state) as state:
33
+ with export(state) as exporter:
34
+ return exporter.import_session(spec)
35
+
36
+ def completion(self, state: ReplState):
37
+ return {}
38
+
39
+ def help(self, _: ReplState):
40
+ return f'{ImportSession.COMMAND} <export-session-name>\t import files in session to Athena or SQLite'
@@ -0,0 +1,81 @@
1
+ from abc import abstractmethod
2
+
3
+ from adam.commands.export.utils_export import csv_dir, export_log_dir, os_system_exec
4
+ from adam.config import Config
5
+ from adam.repl_state import ReplState
6
+ from adam.utils import ing
7
+
8
+ class Importer:
9
+ @abstractmethod
10
+ def prefix(self):
11
+ pass
12
+
13
+ @abstractmethod
14
+ def import_from_csv(self, state: ReplState, from_session: str, keyspace: str, table: str, target_table: str, columns: str, multi_tables = True, create_db = False, log_file: str = None):
15
+ pass
16
+
17
+ @abstractmethod
18
+ def import_from_local_csv(self, state: ReplState,
19
+ keyspace: str, table: str, target_table: str, columns: str,
20
+ csv_file: str,
21
+ multi_tables = True, create_db = False):
22
+ pass
23
+
24
+ def move_to_done(self, state: ReplState, from_session: str, keyspace: str, target_table: str):
25
+ pod = state.pod
26
+ namespace = state.namespace
27
+ to_session = state.export_session
28
+ log_file = f'{export_log_dir()}/{from_session}_{keyspace}.{target_table}.log.pending_import'
29
+
30
+ to = f'{export_log_dir()}/{to_session}_{keyspace}.{target_table}.log.done'
31
+
32
+ cmd = f'mv {log_file} {to}'
33
+ os_system_exec(cmd, show_out=Config().is_debug())
34
+
35
+ return to, to_session
36
+
37
+ def prefix_adjusted_session(self, session: str):
38
+ if not session.startswith(self.prefix()):
39
+ return f'{self.prefix()}{session[1:]}'
40
+
41
+ return session
42
+
43
+ def remove_csv(self, state: ReplState, from_session: str, table: str, target_table: str, multi_tables = True, job_log: str = None):
44
+ pod = state.pod
45
+ namespace = state.namespace
46
+
47
+ with ing(f'[{from_session}] Cleaning up temporary files', suppress_log=multi_tables, job_log=job_log):
48
+ cmd = f'rm -rf {self.csv_file(from_session, table, target_table)}'
49
+ os_system_exec(cmd, show_out=Config().is_debug())
50
+
51
+ def db(self, session: str, keyspace: str):
52
+ return f'{session}_{keyspace}'
53
+
54
+ def csv_file(self, session: str, table: str, target_table: str):
55
+ return f'{csv_dir()}/{session}_{target_table}/{table}.csv'
56
+
57
+ def prefix_from_importer(importer: str = ''):
58
+ if not importer:
59
+ return ''
60
+
61
+ prefix = 's'
62
+
63
+ if importer == 'athena':
64
+ prefix = 'e'
65
+ elif importer == 'csv':
66
+ prefix = 'c'
67
+
68
+ return prefix
69
+
70
+ def importer_from_session(session: str):
71
+ if not session:
72
+ return None
73
+
74
+ importer = 'csv'
75
+
76
+ if session.startswith('s'):
77
+ importer = 'sqlite'
78
+ elif session.startswith('e'):
79
+ importer = 'athena'
80
+
81
+ return importer