kaqing 1.77.0__py3-none-any.whl → 2.0.171__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.
Files changed (308) hide show
  1. adam/__init__.py +1 -0
  2. adam/app_session.py +182 -0
  3. {walker → adam}/apps.py +8 -24
  4. {walker → adam}/batch.py +54 -97
  5. {walker → adam}/checks/check.py +3 -3
  6. {walker → adam}/checks/check_result.py +1 -1
  7. adam/checks/check_utils.py +65 -0
  8. {walker → adam}/checks/compactionstats.py +6 -6
  9. {walker → adam}/checks/cpu.py +14 -8
  10. adam/checks/cpu_metrics.py +52 -0
  11. {walker → adam}/checks/disk.py +6 -6
  12. {walker → adam}/checks/gossip.py +5 -5
  13. {walker → adam}/checks/memory.py +7 -7
  14. {walker → adam}/checks/status.py +5 -5
  15. {walker → adam}/cli.py +3 -3
  16. {walker → adam}/columns/column.py +1 -1
  17. adam/columns/columns.py +45 -0
  18. {walker → adam}/columns/compactions.py +5 -5
  19. {walker → adam}/columns/cpu.py +6 -4
  20. adam/columns/cpu_metrics.py +22 -0
  21. {walker → adam}/columns/dir_data.py +3 -3
  22. {walker → adam}/columns/dir_snapshots.py +3 -3
  23. {walker → adam}/columns/gossip.py +5 -5
  24. {walker → adam}/columns/host_id.py +3 -3
  25. {walker → adam}/columns/memory.py +3 -3
  26. {walker → adam}/columns/node_address.py +3 -3
  27. {walker → adam}/columns/node_load.py +3 -3
  28. {walker → adam}/columns/node_owns.py +3 -3
  29. {walker → adam}/columns/node_status.py +3 -3
  30. {walker → adam}/columns/node_tokens.py +3 -3
  31. {walker → adam}/columns/node_utils.py +2 -2
  32. {walker → adam}/columns/pod_name.py +2 -2
  33. {walker → adam}/columns/volume_cassandra.py +4 -4
  34. {walker → adam}/columns/volume_root.py +3 -3
  35. adam/commands/__init__.py +15 -0
  36. adam/commands/alter_tables.py +81 -0
  37. adam/commands/app_cmd.py +38 -0
  38. {walker → adam}/commands/app_ping.py +10 -16
  39. adam/commands/audit/audit.py +84 -0
  40. adam/commands/audit/audit_repair_tables.py +74 -0
  41. adam/commands/audit/audit_run.py +50 -0
  42. adam/commands/audit/show_last10.py +48 -0
  43. adam/commands/audit/show_slow10.py +47 -0
  44. adam/commands/audit/show_top10.py +45 -0
  45. adam/commands/audit/utils_show_top10.py +59 -0
  46. adam/commands/bash/__init__.py +5 -0
  47. adam/commands/bash/bash.py +36 -0
  48. adam/commands/bash/bash_completer.py +93 -0
  49. adam/commands/bash/utils_bash.py +16 -0
  50. adam/commands/cat.py +50 -0
  51. adam/commands/cd.py +43 -0
  52. adam/commands/check.py +73 -0
  53. {walker → adam}/commands/cli_commands.py +7 -8
  54. adam/commands/code.py +57 -0
  55. adam/commands/command.py +190 -0
  56. {walker → adam}/commands/command_helpers.py +1 -1
  57. {walker → adam}/commands/commands_utils.py +15 -25
  58. adam/commands/cp.py +89 -0
  59. adam/commands/cql/cql_completions.py +33 -0
  60. {walker/commands → adam/commands/cql}/cqlsh.py +20 -35
  61. adam/commands/cql/utils_cql.py +343 -0
  62. {walker/commands/frontend → adam/commands/deploy}/code_start.py +11 -14
  63. adam/commands/deploy/code_stop.py +40 -0
  64. {walker/commands/frontend → adam/commands/deploy}/code_utils.py +7 -9
  65. adam/commands/deploy/deploy.py +25 -0
  66. adam/commands/deploy/deploy_frontend.py +49 -0
  67. adam/commands/deploy/deploy_pg_agent.py +35 -0
  68. adam/commands/deploy/deploy_pod.py +108 -0
  69. adam/commands/deploy/deploy_utils.py +29 -0
  70. adam/commands/deploy/undeploy.py +25 -0
  71. adam/commands/deploy/undeploy_frontend.py +38 -0
  72. adam/commands/deploy/undeploy_pg_agent.py +39 -0
  73. adam/commands/deploy/undeploy_pod.py +48 -0
  74. adam/commands/devices/device.py +118 -0
  75. adam/commands/devices/device_app.py +173 -0
  76. adam/commands/devices/device_auit_log.py +49 -0
  77. adam/commands/devices/device_cass.py +185 -0
  78. adam/commands/devices/device_export.py +86 -0
  79. adam/commands/devices/device_postgres.py +144 -0
  80. adam/commands/devices/devices.py +25 -0
  81. {walker → adam}/commands/exit.py +3 -6
  82. adam/commands/export/clean_up_all_export_sessions.py +37 -0
  83. adam/commands/export/clean_up_export_sessions.py +51 -0
  84. adam/commands/export/drop_export_database.py +55 -0
  85. adam/commands/export/drop_export_databases.py +43 -0
  86. adam/commands/export/export.py +53 -0
  87. adam/commands/export/export_databases.py +170 -0
  88. adam/commands/export/export_handlers.py +71 -0
  89. adam/commands/export/export_select.py +81 -0
  90. adam/commands/export/export_select_x.py +54 -0
  91. adam/commands/export/export_use.py +52 -0
  92. adam/commands/export/exporter.py +352 -0
  93. adam/commands/export/import_session.py +40 -0
  94. adam/commands/export/importer.py +67 -0
  95. adam/commands/export/importer_athena.py +80 -0
  96. adam/commands/export/importer_sqlite.py +47 -0
  97. adam/commands/export/show_column_counts.py +54 -0
  98. adam/commands/export/show_export_databases.py +36 -0
  99. adam/commands/export/show_export_session.py +48 -0
  100. adam/commands/export/show_export_sessions.py +44 -0
  101. adam/commands/export/utils_export.py +314 -0
  102. {walker → adam}/commands/help.py +17 -12
  103. adam/commands/intermediate_command.py +49 -0
  104. adam/commands/issues.py +43 -0
  105. adam/commands/kubectl.py +38 -0
  106. adam/commands/login.py +70 -0
  107. {walker → adam}/commands/logs.py +8 -10
  108. adam/commands/ls.py +41 -0
  109. adam/commands/medusa/medusa.py +27 -0
  110. adam/commands/medusa/medusa_backup.py +57 -0
  111. adam/commands/medusa/medusa_restore.py +83 -0
  112. adam/commands/medusa/medusa_show_backupjobs.py +51 -0
  113. adam/commands/medusa/medusa_show_restorejobs.py +47 -0
  114. {walker → adam}/commands/nodetool.py +17 -21
  115. {walker → adam}/commands/param_get.py +15 -16
  116. adam/commands/param_set.py +43 -0
  117. adam/commands/postgres/postgres.py +104 -0
  118. adam/commands/postgres/postgres_context.py +274 -0
  119. {walker → adam}/commands/postgres/postgres_ls.py +7 -11
  120. {walker → adam}/commands/postgres/postgres_preview.py +8 -13
  121. adam/commands/postgres/psql_completions.py +10 -0
  122. adam/commands/postgres/utils_postgres.py +66 -0
  123. adam/commands/preview_table.py +37 -0
  124. adam/commands/pwd.py +47 -0
  125. adam/commands/reaper/reaper.py +35 -0
  126. adam/commands/reaper/reaper_forward.py +93 -0
  127. adam/commands/reaper/reaper_forward_session.py +6 -0
  128. {walker → adam}/commands/reaper/reaper_forward_stop.py +13 -19
  129. {walker → adam}/commands/reaper/reaper_restart.py +10 -17
  130. adam/commands/reaper/reaper_run_abort.py +46 -0
  131. adam/commands/reaper/reaper_runs.py +82 -0
  132. adam/commands/reaper/reaper_runs_abort.py +63 -0
  133. adam/commands/reaper/reaper_schedule_activate.py +45 -0
  134. adam/commands/reaper/reaper_schedule_start.py +45 -0
  135. adam/commands/reaper/reaper_schedule_stop.py +45 -0
  136. {walker → adam}/commands/reaper/reaper_schedules.py +6 -16
  137. {walker → adam}/commands/reaper/reaper_status.py +11 -19
  138. adam/commands/reaper/utils_reaper.py +196 -0
  139. adam/commands/repair/repair.py +26 -0
  140. {walker → adam}/commands/repair/repair_log.py +7 -10
  141. adam/commands/repair/repair_run.py +70 -0
  142. adam/commands/repair/repair_scan.py +71 -0
  143. {walker → adam}/commands/repair/repair_stop.py +8 -11
  144. adam/commands/report.py +61 -0
  145. adam/commands/restart.py +60 -0
  146. {walker → adam}/commands/rollout.py +25 -30
  147. adam/commands/shell.py +34 -0
  148. adam/commands/show/show.py +39 -0
  149. walker/commands/show/show_version.py → adam/commands/show/show_adam.py +14 -10
  150. adam/commands/show/show_app_actions.py +57 -0
  151. {walker → adam}/commands/show/show_app_id.py +12 -15
  152. {walker → adam}/commands/show/show_app_queues.py +9 -12
  153. adam/commands/show/show_cassandra_repairs.py +38 -0
  154. adam/commands/show/show_cassandra_status.py +124 -0
  155. {walker → adam}/commands/show/show_cassandra_version.py +6 -16
  156. adam/commands/show/show_commands.py +59 -0
  157. walker/commands/show/show_storage.py → adam/commands/show/show_host.py +11 -13
  158. adam/commands/show/show_login.py +62 -0
  159. {walker → adam}/commands/show/show_params.py +4 -4
  160. adam/commands/show/show_processes.py +51 -0
  161. adam/commands/show/show_storage.py +42 -0
  162. adam/commands/watch.py +82 -0
  163. {walker → adam}/config.py +10 -22
  164. {walker → adam}/embedded_apps.py +1 -1
  165. adam/embedded_params.py +2 -0
  166. adam/log.py +47 -0
  167. {walker → adam}/pod_exec_result.py +10 -2
  168. adam/repl.py +182 -0
  169. adam/repl_commands.py +124 -0
  170. adam/repl_state.py +458 -0
  171. adam/sql/__init__.py +0 -0
  172. adam/sql/sql_completer.py +120 -0
  173. adam/sql/sql_state_machine.py +618 -0
  174. adam/sql/term_completer.py +76 -0
  175. adam/sso/__init__.py +0 -0
  176. {walker → adam}/sso/authenticator.py +5 -1
  177. adam/sso/authn_ad.py +170 -0
  178. {walker → adam}/sso/authn_okta.py +39 -22
  179. adam/sso/cred_cache.py +60 -0
  180. adam/sso/id_token.py +23 -0
  181. adam/sso/idp.py +143 -0
  182. adam/sso/idp_login.py +50 -0
  183. adam/sso/idp_session.py +55 -0
  184. adam/sso/sso_config.py +63 -0
  185. adam/utils.py +679 -0
  186. adam/utils_app.py +98 -0
  187. adam/utils_athena.py +145 -0
  188. adam/utils_audits.py +106 -0
  189. adam/utils_issues.py +32 -0
  190. adam/utils_k8s/__init__.py +0 -0
  191. adam/utils_k8s/app_clusters.py +28 -0
  192. adam/utils_k8s/app_pods.py +33 -0
  193. adam/utils_k8s/cassandra_clusters.py +36 -0
  194. adam/utils_k8s/cassandra_nodes.py +33 -0
  195. adam/utils_k8s/config_maps.py +34 -0
  196. {walker/k8s_utils → adam/utils_k8s}/custom_resources.py +7 -2
  197. adam/utils_k8s/deployment.py +56 -0
  198. {walker/k8s_utils → adam/utils_k8s}/ingresses.py +3 -4
  199. {walker/k8s_utils → adam/utils_k8s}/jobs.py +3 -3
  200. adam/utils_k8s/k8s.py +87 -0
  201. {walker/k8s_utils → adam/utils_k8s}/kube_context.py +4 -4
  202. adam/utils_k8s/pods.py +290 -0
  203. {walker/k8s_utils → adam/utils_k8s}/secrets.py +8 -4
  204. adam/utils_k8s/service_accounts.py +170 -0
  205. {walker/k8s_utils → adam/utils_k8s}/services.py +3 -4
  206. {walker/k8s_utils → adam/utils_k8s}/statefulsets.py +6 -16
  207. {walker/k8s_utils → adam/utils_k8s}/volumes.py +10 -1
  208. adam/utils_net.py +24 -0
  209. adam/utils_repl/__init__.py +0 -0
  210. adam/utils_repl/automata_completer.py +48 -0
  211. adam/utils_repl/repl_completer.py +46 -0
  212. adam/utils_repl/state_machine.py +173 -0
  213. adam/utils_sqlite.py +109 -0
  214. adam/version.py +5 -0
  215. {kaqing-1.77.0.dist-info → kaqing-2.0.171.dist-info}/METADATA +1 -1
  216. kaqing-2.0.171.dist-info/RECORD +236 -0
  217. kaqing-2.0.171.dist-info/entry_points.txt +3 -0
  218. kaqing-2.0.171.dist-info/top_level.txt +1 -0
  219. kaqing-1.77.0.dist-info/RECORD +0 -159
  220. kaqing-1.77.0.dist-info/entry_points.txt +0 -3
  221. kaqing-1.77.0.dist-info/top_level.txt +0 -1
  222. walker/__init__.py +0 -3
  223. walker/app_session.py +0 -168
  224. walker/checks/check_utils.py +0 -97
  225. walker/columns/columns.py +0 -43
  226. walker/commands/add_user.py +0 -68
  227. walker/commands/app.py +0 -67
  228. walker/commands/bash.py +0 -87
  229. walker/commands/cd.py +0 -115
  230. walker/commands/check.py +0 -68
  231. walker/commands/command.py +0 -104
  232. walker/commands/cp.py +0 -95
  233. walker/commands/cql_utils.py +0 -53
  234. walker/commands/devices.py +0 -89
  235. walker/commands/frontend/code_stop.py +0 -57
  236. walker/commands/frontend/setup.py +0 -60
  237. walker/commands/frontend/setup_frontend.py +0 -58
  238. walker/commands/frontend/teardown.py +0 -61
  239. walker/commands/frontend/teardown_frontend.py +0 -42
  240. walker/commands/issues.py +0 -69
  241. walker/commands/login.py +0 -72
  242. walker/commands/ls.py +0 -145
  243. walker/commands/medusa/medusa.py +0 -69
  244. walker/commands/medusa/medusa_backup.py +0 -61
  245. walker/commands/medusa/medusa_restore.py +0 -86
  246. walker/commands/medusa/medusa_show_backupjobs.py +0 -52
  247. walker/commands/medusa/medusa_show_restorejobs.py +0 -52
  248. walker/commands/param_set.py +0 -44
  249. walker/commands/postgres/postgres.py +0 -113
  250. walker/commands/postgres/postgres_session.py +0 -225
  251. walker/commands/preview_table.py +0 -98
  252. walker/commands/processes.py +0 -53
  253. walker/commands/pwd.py +0 -64
  254. walker/commands/reaper/reaper.py +0 -78
  255. walker/commands/reaper/reaper_forward.py +0 -100
  256. walker/commands/reaper/reaper_run_abort.py +0 -65
  257. walker/commands/reaper/reaper_runs.py +0 -97
  258. walker/commands/reaper/reaper_runs_abort.py +0 -83
  259. walker/commands/reaper/reaper_schedule_activate.py +0 -64
  260. walker/commands/reaper/reaper_schedule_start.py +0 -64
  261. walker/commands/reaper/reaper_schedule_stop.py +0 -64
  262. walker/commands/reaper/reaper_session.py +0 -159
  263. walker/commands/repair/repair.py +0 -68
  264. walker/commands/repair/repair_run.py +0 -72
  265. walker/commands/repair/repair_scan.py +0 -79
  266. walker/commands/report.py +0 -57
  267. walker/commands/restart.py +0 -61
  268. walker/commands/show/show.py +0 -72
  269. walker/commands/show/show_app_actions.py +0 -53
  270. walker/commands/show/show_cassandra_status.py +0 -35
  271. walker/commands/show/show_commands.py +0 -58
  272. walker/commands/show/show_processes.py +0 -35
  273. walker/commands/show/show_repairs.py +0 -47
  274. walker/commands/status.py +0 -128
  275. walker/commands/storage.py +0 -52
  276. walker/commands/user_entry.py +0 -69
  277. walker/commands/watch.py +0 -85
  278. walker/embedded_params.py +0 -2
  279. walker/k8s_utils/cassandra_clusters.py +0 -48
  280. walker/k8s_utils/cassandra_nodes.py +0 -26
  281. walker/k8s_utils/pods.py +0 -211
  282. walker/repl.py +0 -165
  283. walker/repl_commands.py +0 -58
  284. walker/repl_state.py +0 -211
  285. walker/sso/authn_ad.py +0 -94
  286. walker/sso/idp.py +0 -150
  287. walker/sso/idp_login.py +0 -29
  288. walker/sso/sso_config.py +0 -45
  289. walker/utils.py +0 -194
  290. walker/version.py +0 -5
  291. {walker → adam}/checks/__init__.py +0 -0
  292. {walker → adam}/checks/check_context.py +0 -0
  293. {walker → adam}/checks/issue.py +0 -0
  294. {walker → adam}/cli_group.py +0 -0
  295. {walker → adam}/columns/__init__.py +0 -0
  296. {walker/commands → adam/commands/audit}/__init__.py +0 -0
  297. {walker/commands/frontend → adam/commands/cql}/__init__.py +0 -0
  298. {walker/commands/medusa → adam/commands/deploy}/__init__.py +0 -0
  299. {walker/commands/postgres → adam/commands/devices}/__init__.py +0 -0
  300. {walker/commands/reaper → adam/commands/export}/__init__.py +0 -0
  301. {walker/commands/repair → adam/commands/medusa}/__init__.py +0 -0
  302. {walker → adam}/commands/nodetool_commands.py +0 -0
  303. {walker/commands/show → adam/commands/postgres}/__init__.py +0 -0
  304. {walker/k8s_utils → adam/commands/reaper}/__init__.py +0 -0
  305. {walker/sso → adam/commands/repair}/__init__.py +0 -0
  306. /walker/medusa_show_restorejobs.py → /adam/commands/show/__init__.py +0 -0
  307. {walker → adam}/repl_session.py +0 -0
  308. {kaqing-1.77.0.dist-info → kaqing-2.0.171.dist-info}/WHEEL +0 -0
@@ -0,0 +1,352 @@
1
+ from datetime import datetime
2
+ import functools
3
+ import re
4
+ import time
5
+ import traceback
6
+
7
+ from adam.commands.cql.utils_cql import cassandra_table_names, run_cql, table_spec
8
+ from adam.commands.export.export_databases import ExportDatabases
9
+ from adam.commands.export.importer import Importer
10
+ from adam.commands.export.importer_athena import AthenaImporter
11
+ from adam.commands.export.importer_sqlite import SqliteImporter
12
+ from adam.commands.export.utils_export import ExportSpec, ExportTableStatus, ExportTableSpec, ImportSpec, csv_dir, find_files
13
+ from adam.config import Config
14
+ from adam.pod_exec_result import PodExecResult
15
+ from adam.repl_state import ReplState
16
+ from adam.utils import parallelize, log2, ing
17
+ from adam.utils_k8s.cassandra_nodes import CassandraNodes
18
+ from adam.utils_k8s.pods import log_prefix
19
+ from adam.utils_k8s.statefulsets import StatefulSets
20
+
21
+ class Exporter:
22
+ def export_tables(args: list[str], state: ReplState, export_only: bool = False, max_workers = 0) -> tuple[list[str], ExportSpec]:
23
+ if export_only:
24
+ log2('export-only for testing')
25
+
26
+ spec: ExportSpec = None
27
+ try:
28
+ spec = Exporter.export_spec(' '.join(args), state)
29
+
30
+ statuses, spec = Exporter._export_tables(spec, state, max_workers=max_workers, export_state='init')
31
+ if not statuses:
32
+ return statuses, spec
33
+
34
+ return Exporter._export_tables(spec, state, export_only, max_workers, 'pending_export')
35
+ except Exception as e:
36
+ log2(e)
37
+
38
+ return [], None
39
+
40
+ def export_spec(spec_str: str, state: ReplState):
41
+ spec: ExportSpec = ExportSpec.parse_specs(spec_str)
42
+
43
+ session = state.export_session
44
+ if session:
45
+ if spec.importer:
46
+ importer_from_session = Importer.importer_from_session(session)
47
+ if spec.importer != importer_from_session:
48
+ if spec.importer == 'csv':
49
+ prefix = Importer.prefix_from_importer(spec.importer)
50
+ session = f'{prefix}{session[1:]}'
51
+ else:
52
+ raise Exception(f"You're currently using {importer_from_session} export database. You cannot export tables with {spec.importer} type database.")
53
+ else:
54
+ spec.importer = Importer.importer_from_session(session)
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
+ if spec.importer != 'csv':
62
+ state.export_session = session
63
+
64
+ spec.session = session
65
+
66
+ return spec
67
+
68
+ def import_session(args: list[str], state: ReplState, max_workers = 0) -> tuple[list[str], ExportSpec]:
69
+ import_spec: ImportSpec = None
70
+ try:
71
+ import_spec = Exporter.import_spec(' '.join(args), state)
72
+ tables, status_in_whole = ExportTableStatus.from_session(state.sts, state.pod, state.namespace, import_spec.session)
73
+ if status_in_whole == 'done':
74
+ log2(f'The session has been completely done - no more csv files are found.')
75
+ return [], ExportSpec(None, None, importer=import_spec.importer, tables=[])
76
+
77
+ spec = ExportSpec(None, None, importer=import_spec.importer, tables=[ExportTableSpec.from_status(table) for table in tables], session=import_spec.session)
78
+
79
+ return Exporter._export_tables(spec, state, max_workers=max_workers, export_state = 'import')
80
+ except Exception as e:
81
+ if Config().is_debug():
82
+ traceback.print_exception(e)
83
+ else:
84
+ log2(e)
85
+
86
+ return [], None
87
+
88
+ def import_spec(spec_str: str, state: ReplState):
89
+ spec: ImportSpec = ImportSpec.parse_specs(spec_str)
90
+
91
+ session = state.export_session
92
+ if session:
93
+ if spec.importer:
94
+ importer = Importer.importer_from_session(state.export_session)
95
+ if spec.importer != importer:
96
+ raise Exception(f"You're currently using {importer} export database. You cannot import to {spec.importer} type database.")
97
+ else:
98
+ spec.importer = Importer.importer_from_session(state.export_session)
99
+ if not spec.importer:
100
+ spec.importer = Config().get('export.default-importer', 'sqlite')
101
+ else:
102
+ if spec.importer:
103
+ if not AthenaImporter.ping():
104
+ raise Exception('Credentials for Athena are not present.')
105
+ else:
106
+ spec.importer = Importer.importer_from_session(spec.session)
107
+
108
+ if spec.importer == 'csv':
109
+ spec.importer = Config().get('export.default-importer', 'sqlite')
110
+
111
+ prefix = Importer.prefix_from_importer(spec.importer)
112
+ session = f'{prefix}{spec.session[1:]}'
113
+ state.export_session = session
114
+
115
+ return spec
116
+
117
+ def _export_tables(spec: ExportSpec, state: ReplState, export_only = False, max_workers = 0, export_state = None) -> tuple[list[str], ExportSpec]:
118
+ if not spec.keyspace:
119
+ spec.keyspace = f'{state.namespace}_db'
120
+
121
+ if not spec.tables:
122
+ spec.tables = [ExportTableSpec.parse(t) for t in cassandra_table_names(state, keyspace=spec.keyspace)]
123
+
124
+ if not max_workers:
125
+ max_workers = Config().action_workers(f'export.{spec.importer}', 8)
126
+
127
+ if export_state == 'init':
128
+ CassandraNodes.exec(state.pod, state.namespace, f'rm -rf {csv_dir()}/{spec.session}_*', show_out=Config().is_debug(), shell='bash')
129
+
130
+ action = f'[{spec.session}] Exporting|Exported'
131
+ if export_state == 'init':
132
+ action = f'[{spec.session}] Preparing|Prepared'
133
+ elif export_state == 'import':
134
+ action = f'[{spec.session}] Importing|Imported'
135
+
136
+ with parallelize(spec.tables, max_workers, msg=action + ' {size} Cassandra tables') as exec:
137
+ return exec.map(lambda table: Exporter.export_table(table, state, spec.session, spec.importer, export_only, len(spec.tables) > 1, consistency=spec.consistency, export_state=export_state)), spec
138
+
139
+ def export_table(spec: ExportTableSpec, state: ReplState, session: str, importer: str, export_only = False, multi_tables = True, consistency: str = None, export_state=None):
140
+ s: str = None
141
+
142
+ table, target_table, columns = Exporter.resove_table_n_columns(spec, state, include_ks_in_target=False, importer=importer)
143
+
144
+ log_file = f'{log_prefix()}-{session}_{spec.keyspace}.{target_table}.log'
145
+ create_db = not state.export_session
146
+
147
+ if export_state == 'init':
148
+ Exporter.create_table_log(spec, state, session, table, target_table)
149
+ return 'table_log_created'
150
+ else:
151
+ if export_state == 'pending_export':
152
+ Exporter.export_to_csv(spec, state, session, table, target_table, columns, multi_tables=multi_tables, consistency=consistency)
153
+
154
+ log_files: list[str] = find_files(state.pod, state.namespace, f'{log_file}*')
155
+ if not log_files:
156
+ return s
157
+
158
+ log_file = log_files[0]
159
+
160
+ status: ExportTableStatus = ExportTableStatus.from_log_file(state.pod, state.namespace, session, log_file)
161
+ while status.status != 'done':
162
+ if status.status == 'export_in_pregress':
163
+ if Config().is_debug():
164
+ log2('Exporting to CSV is still in progess, sleeping for 1 sec...')
165
+ time.sleep(1)
166
+ elif status.status == 'exported':
167
+ log_file = Exporter.rename_to_pending_import(spec, state, session, target_table)
168
+ if importer == 'csv' or export_only:
169
+ return 'pending_import'
170
+ elif status.status == 'pending_import':
171
+ log_file, session = Exporter.import_from_csv(spec, state, session, importer, table, target_table, columns, multi_tables=multi_tables, create_db=create_db)
172
+
173
+ status = ExportTableStatus.from_log_file(state.pod, state.namespace, session, log_file)
174
+
175
+ return status.status
176
+
177
+ def create_table_log(spec: ExportTableSpec, state: ReplState, session: str, table: str, target_table: str):
178
+ log_file = f'{log_prefix()}-{session}_{spec.keyspace}.{target_table}.log'
179
+
180
+ CassandraNodes.exec(state.pod, state.namespace, f'rm -f {log_file}* && touch {log_file}', show_out=Config().is_debug(), shell='bash')
181
+
182
+ return table
183
+
184
+ def export_to_csv(spec: ExportTableSpec, state: ReplState, session: str, table: str, target_table: str, columns: str, multi_tables = True, consistency: str = None):
185
+ db = f'{session}_{target_table}'
186
+
187
+ CassandraNodes.exec(state.pod, state.namespace, f'mkdir -p {csv_dir()}/{db}', show_out=Config().is_debug(), shell='bash')
188
+ csv_file = f'{csv_dir()}/{db}/{table}.csv'
189
+ log_file = f'{log_prefix()}-{session}_{spec.keyspace}.{target_table}.log'
190
+
191
+ suppress_ing_log = Config().is_debug() or multi_tables
192
+ queries = []
193
+ if consistency:
194
+ queries.append(f'CONSISTENCY {consistency}')
195
+ queries.append(f"COPY {spec.keyspace}.{table}({columns}) TO '{csv_file}' WITH HEADER = TRUE")
196
+ r: PodExecResult = ing(
197
+ f'[{session}] Dumping table {spec.keyspace}.{table}{f" with consistency {consistency}" if consistency else ""}',
198
+ lambda: run_cql(state, ';'.join(queries), show_out=Config().is_debug(), background=True, log_file=log_file),
199
+ suppress_log=suppress_ing_log)
200
+
201
+ return log_file
202
+
203
+ def rename_to_pending_import(spec: ExportTableSpec, state: ReplState, session: str, target_table: str):
204
+ log_file = f'{log_prefix()}-{session}_{spec.keyspace}.{target_table}.log'
205
+ to = f'{log_file}.pending_import'
206
+
207
+ CassandraNodes.exec(state.pod, state.namespace, f'mv {log_file} {to}', show_out=Config().is_debug(), shell='bash')
208
+
209
+ return to
210
+
211
+ def import_from_csv(spec: ExportTableSpec, state: ReplState, session: str, importer: str, table: str, target_table: str, columns: str, multi_tables = True, create_db = False):
212
+ im = AthenaImporter() if importer == 'athena' else SqliteImporter()
213
+ return im.import_from_csv(state.pod, state.namespace, state.export_session, session if session else state.export_session, spec.keyspace, table, target_table, columns, multi_tables, create_db)
214
+
215
+ def clear_export_session_cache():
216
+ Exporter.find_export_sessions.cache_clear()
217
+ Exporter.export_session_names.cache_clear()
218
+
219
+ @functools.lru_cache()
220
+ def export_session_names(sts: str, pod: str, namespace: str, importer: str = None, export_state = None):
221
+ if not sts or not namespace:
222
+ return []
223
+
224
+ if not pod:
225
+ pod = StatefulSets.pod_names(sts, namespace)[0]
226
+
227
+ if not pod:
228
+ return []
229
+
230
+ return [session for session, state in Exporter.find_export_sessions(pod, namespace, importer).items() if not export_state or state == export_state]
231
+
232
+ @functools.lru_cache()
233
+ def find_export_sessions(pod: str, namespace: str, importer: str = None, limit = 100):
234
+ sessions: dict[str, str] = {}
235
+
236
+ prefix = Importer.prefix_from_importer(importer)
237
+
238
+ log_files: list[str] = find_files(pod, namespace, f'{log_prefix()}-{prefix}*_*.log*')
239
+
240
+ if not log_files:
241
+ return {}
242
+
243
+ for log_file in log_files[:limit]:
244
+ m = re.match(f'{log_prefix()}-(.*?)_.*\.log?(.*)', log_file)
245
+ if m:
246
+ s = m.group(1)
247
+ state = m.group(2) # '', '.pending_import', '.done'
248
+ if state:
249
+ state = state.strip('.')
250
+ else:
251
+ state = 'in_export'
252
+
253
+ if s not in sessions:
254
+ sessions[s] = state
255
+ elif sessions[s] == 'done' and state != 'done':
256
+ sessions[s] = state
257
+
258
+ return sessions
259
+
260
+ def clean_up_all_sessions(sts: str, pod: str, namespace: str):
261
+ if not sts or not namespace:
262
+ return False
263
+
264
+ if not pod:
265
+ pod = StatefulSets.pod_names(sts, namespace)[0]
266
+
267
+ CassandraNodes.exec(pod, namespace, f'rm -rf {csv_dir()}/*', show_out=Config().is_debug(), shell='bash')
268
+ CassandraNodes.exec(pod, namespace, f'rm -rf {log_prefix()}-*.log*', show_out=Config().is_debug(), shell='bash')
269
+
270
+ return True
271
+
272
+ def clean_up_sessions(sts: str, pod: str, namespace: str, sessions: list[str], max_workers = 0):
273
+ if not sessions:
274
+ return []
275
+
276
+ if not max_workers:
277
+ max_workers = Config().action_workers('export', 8)
278
+
279
+ with parallelize(sessions, max_workers, msg='Cleaning|Cleaned up {size} export sessions') as exec:
280
+ cnt_tuples = exec.map(lambda session: Exporter.clean_up_session(sts, pod, namespace, session, True))
281
+ csv_cnt = 0
282
+ log_cnt = 0
283
+ for (csv, log) in cnt_tuples:
284
+ csv_cnt += csv
285
+ log_cnt += log
286
+
287
+ return csv_cnt, log_cnt
288
+
289
+ def clean_up_session(sts: str, pod: str, namespace: str, session: str, multi_tables = True):
290
+ if not sts or not namespace:
291
+ return 0, 0
292
+
293
+ if not pod:
294
+ pod = StatefulSets.pod_names(sts, namespace)[0]
295
+
296
+ if not pod:
297
+ return 0, 0
298
+
299
+ csv_cnt = 0
300
+ log_cnt = 0
301
+
302
+ log_files: list[str] = find_files(pod, namespace, f'{log_prefix()}-{session}_*.log*')
303
+
304
+ for log_file in log_files:
305
+ m = re.match(f'{log_prefix()}-{session}_(.*?)\.(.*?)\.log.*', log_file)
306
+ if m:
307
+ table = m.group(2)
308
+
309
+ CassandraNodes.exec(pod, namespace, f'rm -rf {csv_dir()}/{session}_{table}', show_out=not multi_tables, shell='bash')
310
+ csv_cnt += 1
311
+
312
+ CassandraNodes.exec(pod, namespace, f'rm -rf {log_file}', show_out=not multi_tables, shell='bash')
313
+ log_cnt += 1
314
+
315
+ return csv_cnt, log_cnt
316
+
317
+ def resove_table_n_columns(spec: ExportTableSpec, state: ReplState, include_ks_in_target = False, importer = 'sqlite'):
318
+ table = spec.table
319
+ columns = spec.columns
320
+ if not columns:
321
+ columns = Config().get(f'export.{importer}.columns', f'<keys>')
322
+
323
+ keyspaced_table = f'{spec.keyspace}.{spec.table}'
324
+ if columns == '<keys>':
325
+ columns = ','.join(table_spec(state, keyspaced_table, on_any=True).keys())
326
+ elif columns == '<row-key>':
327
+ columns = table_spec(state, keyspaced_table, on_any=True).row_key()
328
+ elif columns == '*':
329
+ columns = ','.join([c.name for c in table_spec(state, keyspaced_table, on_any=True).columns])
330
+
331
+ if not columns:
332
+ log2(f'ERROR: Empty columns on {table}.')
333
+ return table, None, None
334
+
335
+ target_table = spec.target_table if spec.target_table else table
336
+ if not include_ks_in_target and '.' in target_table:
337
+ target_table = target_table.split('.')[-1]
338
+
339
+ return table, target_table, columns
340
+
341
+ def drop_databases(sts: str, pod: str, namespace: str, db: str = None):
342
+ importer = None
343
+ if db:
344
+ importer = Importer.importer_from_session(db)
345
+
346
+ sessions_done = Exporter.export_session_names(sts, pod, namespace, importer=importer, export_state='done')
347
+ sessions = ExportDatabases.sessions_from_dbs(ExportDatabases.drop_export_dbs(db))
348
+ if sessions_done and sessions:
349
+ intersects = list(set(sessions_done) & set(sessions))
350
+ with ing(f'Cleaning up {len(intersects)} completed sessions'):
351
+ Exporter.clean_up_sessions(sts, pod, namespace, list(intersects))
352
+ Exporter.clear_export_session_cache()
@@ -0,0 +1,40 @@
1
+ from adam.commands.command import Command
2
+ from adam.commands.export.export_handlers import export
3
+ from adam.commands.export.exporter import Exporter
4
+ from adam.repl_state import ReplState, RequiredState
5
+
6
+ class ImportSession(Command):
7
+ COMMAND = 'import session'
8
+
9
+ # the singleton pattern
10
+ def __new__(cls, *args, **kwargs):
11
+ if not hasattr(cls, 'instance'): cls.instance = super(ImportSession, cls).__new__(cls)
12
+
13
+ return cls.instance
14
+
15
+ def __init__(self, successor: Command=None):
16
+ super().__init__(successor)
17
+
18
+ def command(self):
19
+ return ImportSession.COMMAND
20
+
21
+ def required(self):
22
+ return RequiredState.CLUSTER_OR_POD
23
+
24
+ def run(self, cmd: str, state: ReplState):
25
+ if not(args := self.args(cmd)):
26
+ return super().run(cmd, state)
27
+
28
+ with self.validate(args, state) as (args, state):
29
+ with export(state) as exporter:
30
+ return exporter.import_sesion(args)
31
+
32
+ def completion(self, state: ReplState):
33
+ # warm up cache
34
+ Exporter.export_session_names(state.sts, state.pod, state.namespace)
35
+ Exporter.export_session_names(state.sts, state.pod, state.namespace, export_state='pending_import')
36
+
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,67 @@
1
+ from abc import abstractmethod
2
+
3
+ from adam.commands.export.utils_export import csv_dir
4
+ from adam.config import Config
5
+ from adam.utils import ing
6
+ from adam.utils_k8s.cassandra_nodes import CassandraNodes
7
+ from adam.utils_k8s.pods import log_prefix
8
+
9
+ class Importer:
10
+ @abstractmethod
11
+ def prefix(self):
12
+ pass
13
+
14
+ @abstractmethod
15
+ def import_from_csv(self, pod: str, namespace: str, to_session: str, from_session: str, keyspace: str, table: str, target_table: str, columns: str, multi_tables = True, create_db = False):
16
+ pass
17
+
18
+ def move_to_done(self, pod: str, namespace: str, to_session: str, from_session: str, keyspace: str, target_table: str):
19
+ log_file = f'{log_prefix()}-{from_session}_{keyspace}.{target_table}.log.pending_import'
20
+
21
+ to = f'{log_prefix()}-{to_session}_{keyspace}.{target_table}.log.done'
22
+
23
+ CassandraNodes.exec(pod, namespace, f'mv {log_file} {to}', show_out=Config().is_debug(), shell='bash')
24
+
25
+ return to, to_session
26
+
27
+ def prefix_adjusted_session(self, session: str):
28
+ if not session.startswith(self.prefix()):
29
+ return f'{self.prefix()}{session[1:]}'
30
+
31
+ return session
32
+
33
+ def remove_csv(self, pod: str, namespace: str, session: str, table: str, target_table: str, multi_tables = True):
34
+ with ing(f'[{session}] Cleaning up temporary files', suppress_log=multi_tables):
35
+ CassandraNodes.exec(pod, namespace, f'rm -rf {self.csv_file(session, table, target_table)}', show_out=Config().is_debug(), shell='bash')
36
+
37
+ def db(self, session: str, keyspace: str):
38
+ return f'{session}_{keyspace}'
39
+
40
+ def csv_file(self, session: str, table: str, target_table: str):
41
+ return f'{csv_dir()}/{session}_{target_table}/{table}.csv'
42
+
43
+ def prefix_from_importer(importer: str = ''):
44
+ if not importer:
45
+ return ''
46
+
47
+ prefix = 's'
48
+
49
+ if importer == 'athena':
50
+ prefix = 'e'
51
+ elif importer == 'csv':
52
+ prefix = 'c'
53
+
54
+ return prefix
55
+
56
+ def importer_from_session(session: str):
57
+ if not session:
58
+ return None
59
+
60
+ importer = 'csv'
61
+
62
+ if session.startswith('s'):
63
+ importer = 'sqlite'
64
+ elif session.startswith('e'):
65
+ importer = 'athena'
66
+
67
+ return importer
@@ -0,0 +1,80 @@
1
+ import boto3
2
+
3
+ from adam.commands.export.importer import Importer
4
+ from adam.commands.export.utils_export import GeneratorStream
5
+ from adam.config import Config
6
+ from adam.utils import log2, ing
7
+ from adam.utils_athena import Athena
8
+ from adam.utils_k8s.pods import Pods
9
+
10
+ class AthenaImporter(Importer):
11
+ def ping():
12
+ session = boto3.session.Session()
13
+ credentials = session.get_credentials()
14
+
15
+ return credentials is not None
16
+
17
+ def prefix(self):
18
+ return 'e'
19
+
20
+ def import_from_csv(self, pod: str, namespace: str, to_session: str, from_session: str, keyspace: str, table: str, target_table: str, columns: str, multi_tables = True, create_db = False):
21
+ csv_file = self.csv_file(from_session, table, target_table)
22
+ db = self.db(to_session, keyspace)
23
+
24
+ succeeded = False
25
+ try:
26
+ bucket = Config().get('export.bucket', 'c3.ops--qing')
27
+
28
+ with ing(f'[{to_session}] Uploading to S3', suppress_log=multi_tables):
29
+ bytes = Pods.read_file(pod, 'cassandra', namespace, csv_file)
30
+
31
+ s3 = boto3.client('s3')
32
+ s3.upload_fileobj(GeneratorStream(bytes), bucket, f'export/{db}/{keyspace}/{target_table}/{table}.csv')
33
+
34
+ msg: str = None
35
+ if create_db:
36
+ msg = f"[{to_session}] Creating database {db}"
37
+ else:
38
+ msg = f"[{to_session}] Creating table {target_table}"
39
+ with ing(msg, suppress_log=multi_tables):
40
+ query = f'CREATE DATABASE IF NOT EXISTS {db};'
41
+ if Config().is_debug():
42
+ log2(query)
43
+ Athena.query(query, 'default')
44
+
45
+ query = f'DROP TABLE IF EXISTS {target_table};'
46
+ if Config().is_debug():
47
+ log2(query)
48
+ Athena.query(query, db)
49
+
50
+ athena_columns = ', '.join([f'{c} string' for c in columns.split(',')])
51
+ query = f'CREATE EXTERNAL TABLE IF NOT EXISTS {target_table}(\n' + \
52
+ f' {athena_columns})\n' + \
53
+ "ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'\n" + \
54
+ 'WITH SERDEPROPERTIES (\n' + \
55
+ ' "separatorChar" = ",",\n' + \
56
+ ' "quoteChar" = "\\"")\n' + \
57
+ f"LOCATION 's3://{bucket}/export/{db}/{keyspace}/{target_table}'\n" + \
58
+ 'TBLPROPERTIES ("skip.header.line.count"="1");'
59
+ if Config().is_debug():
60
+ log2(query)
61
+ try:
62
+ Athena.query(query, db)
63
+ except Exception as e:
64
+ log2(f'*** Failed query:\n{query}')
65
+ raise e
66
+
67
+ to, _ = self.move_to_done(pod, namespace, to_session, from_session, keyspace, target_table)
68
+
69
+ succeeded = True
70
+
71
+ return to, to_session
72
+ finally:
73
+ if succeeded:
74
+ self.remove_csv(pod, namespace, from_session, table, target_table, multi_tables)
75
+ Athena.clear_cache()
76
+
77
+ if not multi_tables:
78
+ query = f'select * from {target_table} limit 10'
79
+ log2(query)
80
+ Athena.run_query(query, db)
@@ -0,0 +1,47 @@
1
+ import os
2
+ import sqlite3
3
+ import pandas
4
+
5
+ from adam.commands.export.importer import Importer
6
+ from adam.commands.export.utils_export import GeneratorStream
7
+ from adam.utils import log2, ing
8
+ from adam.utils_k8s.pods import Pods
9
+ from adam.utils_sqlite import SQLite
10
+
11
+ class SqliteImporter(Importer):
12
+ def prefix(self):
13
+ return 's'
14
+
15
+ def import_from_csv(self, pod: str, namespace: str, to_session: str, from_session: str, keyspace: str, table: str, target_table: str, columns: str, multi_tables = True, create_db = False):
16
+ csv_file = self.csv_file(from_session, table, target_table)
17
+ db = self.db(to_session, keyspace)
18
+
19
+ succeeded = False
20
+ conn = None
21
+ try:
22
+ os.makedirs(SQLite.local_db_dir(), exist_ok=True)
23
+ conn = sqlite3.connect(f'{SQLite.local_db_dir()}/{db}.db')
24
+
25
+ with ing(f'[{to_session}] Uploading to Sqlite', suppress_log=multi_tables):
26
+ bytes = Pods.read_file(pod, 'cassandra', namespace, csv_file)
27
+ df = pandas.read_csv(GeneratorStream(bytes))
28
+
29
+ df.to_sql(target_table, conn, index=False, if_exists='replace')
30
+
31
+ to, _ = self.move_to_done(pod, namespace, to_session, from_session, keyspace, target_table)
32
+
33
+ succeeded = True
34
+
35
+ return to, to_session
36
+ finally:
37
+ if succeeded:
38
+ self.remove_csv(pod, namespace, from_session, table, target_table, multi_tables)
39
+ SQLite.clear_cache()
40
+
41
+ if not multi_tables:
42
+ query = f'select * from {target_table} limit 10'
43
+ log2(query)
44
+ SQLite.run_query(query, conn_passed=conn)
45
+
46
+ if conn:
47
+ conn.close()
@@ -0,0 +1,54 @@
1
+ from adam.commands.command import Command
2
+ from adam.commands.export.export_databases import ExportDatabases
3
+ from adam.config import Config
4
+ from adam.repl_state import ReplState, RequiredState
5
+ from adam.utils import log2
6
+
7
+ class ShowColumnCounts(Command):
8
+ COMMAND = 'show column counts on'
9
+
10
+ # the singleton pattern
11
+ def __new__(cls, *args, **kwargs):
12
+ if not hasattr(cls, 'instance'): cls.instance = super(ShowColumnCounts, cls).__new__(cls)
13
+
14
+ return cls.instance
15
+
16
+ def __init__(self, successor: Command=None):
17
+ super().__init__(successor)
18
+
19
+ def command(self):
20
+ return ShowColumnCounts.COMMAND
21
+
22
+ def required(self):
23
+ return RequiredState.EXPORT_DB
24
+
25
+ def run(self, cmd: str, state: ReplState):
26
+ if not(args := self.args(cmd)):
27
+ return super().run(cmd, state)
28
+
29
+ with self.validate(args, state) as (args, state):
30
+ if not args:
31
+ if state.in_repl:
32
+ log2('Use a SQL statement.')
33
+ else:
34
+ log2('* SQL statement is missing.')
35
+
36
+ Command.display_help()
37
+
38
+ return 'command-missing'
39
+
40
+ table = args[0]
41
+ query = Config().get(f'export.column_counts_query', 'select id, count(id) as columns from {table} group by id')
42
+ query = query.replace('{table}', table)
43
+ ExportDatabases.run_query(query, state.export_session)
44
+
45
+ return state
46
+
47
+ def completion(self, state: ReplState):
48
+ if not state.export_session:
49
+ return {}
50
+
51
+ return super().completion(state, lambda: {t: None for t in ExportDatabases.table_names(state.export_session)})
52
+
53
+ def help(self, _: ReplState):
54
+ return f'{ShowColumnCounts.COMMAND} <export-table-name>\t show column count per id'
@@ -0,0 +1,36 @@
1
+ from adam.commands.command import Command
2
+ from adam.commands.devices.device_export import DeviceExport
3
+ from adam.repl_state import ReplState
4
+
5
+ class ShowExportDatabases(Command):
6
+ COMMAND = 'show export databases'
7
+
8
+ # the singleton pattern
9
+ def __new__(cls, *args, **kwargs):
10
+ if not hasattr(cls, 'instance'): cls.instance = super(ShowExportDatabases, cls).__new__(cls)
11
+
12
+ return cls.instance
13
+
14
+ def __init__(self, successor: Command=None):
15
+ super().__init__(successor)
16
+
17
+ def command(self):
18
+ return ShowExportDatabases.COMMAND
19
+
20
+ def required(self):
21
+ return [ReplState.C, ReplState.X]
22
+
23
+ def run(self, cmd: str, state: ReplState):
24
+ if not(args := self.args(cmd)):
25
+ return super().run(cmd, state)
26
+
27
+ with self.validate(args, state) as (args, state):
28
+ DeviceExport().show_export_databases()
29
+
30
+ return state
31
+
32
+ def completion(self, state: ReplState):
33
+ return DeviceExport().ls_completion(ShowExportDatabases.COMMAND, state, default = super().completion(state))
34
+
35
+ def help(self, _: ReplState):
36
+ return f'{ShowExportDatabases.COMMAND}\t list export databases'