kaqing 1.98.15__py3-none-any.whl → 2.0.145__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 (180) hide show
  1. adam/app_session.py +1 -1
  2. adam/apps.py +2 -2
  3. adam/batch.py +30 -31
  4. adam/checks/check_utils.py +4 -4
  5. adam/checks/compactionstats.py +1 -1
  6. adam/checks/cpu.py +2 -2
  7. adam/checks/disk.py +1 -1
  8. adam/checks/gossip.py +1 -1
  9. adam/checks/memory.py +3 -3
  10. adam/checks/status.py +1 -1
  11. adam/commands/alter_tables.py +81 -0
  12. adam/commands/app.py +3 -3
  13. adam/commands/app_ping.py +2 -2
  14. adam/commands/audit/audit.py +86 -0
  15. adam/commands/audit/audit_repair_tables.py +77 -0
  16. adam/commands/audit/audit_run.py +58 -0
  17. adam/commands/audit/show_last10.py +51 -0
  18. adam/commands/audit/show_slow10.py +50 -0
  19. adam/commands/audit/show_top10.py +48 -0
  20. adam/commands/audit/utils_show_top10.py +59 -0
  21. adam/commands/bash/bash.py +133 -0
  22. adam/commands/bash/bash_completer.py +93 -0
  23. adam/commands/cat.py +56 -0
  24. adam/commands/cd.py +12 -82
  25. adam/commands/check.py +6 -0
  26. adam/commands/cli_commands.py +3 -3
  27. adam/commands/code.py +60 -0
  28. adam/commands/command.py +48 -12
  29. adam/commands/commands_utils.py +4 -5
  30. adam/commands/cql/cql_completions.py +28 -0
  31. adam/commands/cql/cql_utils.py +209 -0
  32. adam/commands/{cqlsh.py → cql/cqlsh.py} +15 -10
  33. adam/commands/deploy/__init__.py +0 -0
  34. adam/commands/{frontend → deploy}/code_start.py +1 -1
  35. adam/commands/{frontend → deploy}/code_stop.py +1 -1
  36. adam/commands/{frontend → deploy}/code_utils.py +2 -2
  37. adam/commands/deploy/deploy.py +48 -0
  38. adam/commands/deploy/deploy_frontend.py +52 -0
  39. adam/commands/deploy/deploy_pg_agent.py +38 -0
  40. adam/commands/deploy/deploy_pod.py +110 -0
  41. adam/commands/deploy/deploy_utils.py +29 -0
  42. adam/commands/deploy/undeploy.py +48 -0
  43. adam/commands/deploy/undeploy_frontend.py +41 -0
  44. adam/commands/deploy/undeploy_pg_agent.py +42 -0
  45. adam/commands/deploy/undeploy_pod.py +51 -0
  46. adam/commands/devices/__init__.py +0 -0
  47. adam/commands/devices/device.py +27 -0
  48. adam/commands/devices/device_app.py +146 -0
  49. adam/commands/devices/device_auit_log.py +43 -0
  50. adam/commands/devices/device_cass.py +145 -0
  51. adam/commands/devices/device_export.py +86 -0
  52. adam/commands/devices/device_postgres.py +109 -0
  53. adam/commands/devices/devices.py +25 -0
  54. adam/commands/export/__init__.py +0 -0
  55. adam/commands/export/clean_up_export_session.py +53 -0
  56. adam/commands/{frontend/teardown_frontend.py → export/clean_up_export_sessions.py} +9 -11
  57. adam/commands/export/drop_export_database.py +58 -0
  58. adam/commands/export/drop_export_databases.py +46 -0
  59. adam/commands/export/export.py +83 -0
  60. adam/commands/export/export_databases.py +170 -0
  61. adam/commands/export/export_select.py +85 -0
  62. adam/commands/export/export_select_x.py +54 -0
  63. adam/commands/export/export_use.py +55 -0
  64. adam/commands/export/exporter.py +364 -0
  65. adam/commands/export/import_session.py +68 -0
  66. adam/commands/export/importer.py +67 -0
  67. adam/commands/export/importer_athena.py +80 -0
  68. adam/commands/export/importer_sqlite.py +47 -0
  69. adam/commands/export/show_column_counts.py +63 -0
  70. adam/commands/export/show_export_databases.py +39 -0
  71. adam/commands/export/show_export_session.py +51 -0
  72. adam/commands/export/show_export_sessions.py +47 -0
  73. adam/commands/export/utils_export.py +291 -0
  74. adam/commands/help.py +12 -7
  75. adam/commands/issues.py +6 -0
  76. adam/commands/kubectl.py +41 -0
  77. adam/commands/login.py +9 -5
  78. adam/commands/logs.py +2 -1
  79. adam/commands/ls.py +4 -107
  80. adam/commands/medusa/medusa.py +2 -26
  81. adam/commands/medusa/medusa_backup.py +2 -2
  82. adam/commands/medusa/medusa_restore.py +3 -4
  83. adam/commands/medusa/medusa_show_backupjobs.py +4 -3
  84. adam/commands/medusa/medusa_show_restorejobs.py +3 -3
  85. adam/commands/nodetool.py +9 -4
  86. adam/commands/param_set.py +1 -1
  87. adam/commands/postgres/postgres.py +42 -43
  88. adam/commands/postgres/postgres_context.py +248 -0
  89. adam/commands/postgres/postgres_preview.py +0 -1
  90. adam/commands/postgres/postgres_utils.py +31 -0
  91. adam/commands/postgres/psql_completions.py +10 -0
  92. adam/commands/preview_table.py +18 -40
  93. adam/commands/pwd.py +2 -28
  94. adam/commands/reaper/reaper.py +4 -24
  95. adam/commands/reaper/reaper_restart.py +1 -1
  96. adam/commands/reaper/reaper_session.py +2 -2
  97. adam/commands/repair/repair.py +3 -27
  98. adam/commands/repair/repair_log.py +1 -1
  99. adam/commands/repair/repair_run.py +2 -2
  100. adam/commands/repair/repair_scan.py +2 -7
  101. adam/commands/repair/repair_stop.py +1 -1
  102. adam/commands/report.py +6 -0
  103. adam/commands/restart.py +2 -2
  104. adam/commands/rollout.py +1 -1
  105. adam/commands/shell.py +33 -0
  106. adam/commands/show/show.py +11 -26
  107. adam/commands/show/show_app_actions.py +3 -0
  108. adam/commands/show/show_app_id.py +1 -1
  109. adam/commands/show/show_app_queues.py +3 -2
  110. adam/commands/show/show_cassandra_status.py +3 -3
  111. adam/commands/show/show_cassandra_version.py +3 -3
  112. adam/commands/show/show_commands.py +4 -1
  113. adam/commands/show/show_host.py +33 -0
  114. adam/commands/show/show_login.py +3 -0
  115. adam/commands/show/show_processes.py +1 -1
  116. adam/commands/show/show_repairs.py +2 -2
  117. adam/commands/show/show_storage.py +1 -1
  118. adam/commands/watch.py +1 -1
  119. adam/config.py +16 -3
  120. adam/embedded_params.py +1 -1
  121. adam/pod_exec_result.py +10 -2
  122. adam/repl.py +132 -117
  123. adam/repl_commands.py +62 -18
  124. adam/repl_state.py +276 -55
  125. adam/sql/__init__.py +0 -0
  126. adam/sql/sql_completer.py +120 -0
  127. adam/sql/sql_state_machine.py +617 -0
  128. adam/sql/term_completer.py +76 -0
  129. adam/sso/authenticator.py +1 -1
  130. adam/sso/authn_ad.py +36 -56
  131. adam/sso/authn_okta.py +6 -32
  132. adam/sso/cred_cache.py +1 -1
  133. adam/sso/idp.py +74 -9
  134. adam/sso/idp_login.py +2 -2
  135. adam/sso/idp_session.py +10 -7
  136. adam/utils.py +85 -4
  137. adam/utils_athena.py +145 -0
  138. adam/utils_audits.py +102 -0
  139. adam/utils_k8s/__init__.py +0 -0
  140. adam/utils_k8s/app_clusters.py +33 -0
  141. adam/utils_k8s/app_pods.py +31 -0
  142. adam/{k8s_utils → utils_k8s}/cassandra_clusters.py +6 -21
  143. adam/{k8s_utils → utils_k8s}/cassandra_nodes.py +12 -5
  144. adam/utils_k8s/config_maps.py +34 -0
  145. adam/utils_k8s/deployment.py +56 -0
  146. adam/{k8s_utils → utils_k8s}/jobs.py +1 -1
  147. adam/{k8s_utils → utils_k8s}/kube_context.py +1 -1
  148. adam/utils_k8s/pods.py +342 -0
  149. adam/{k8s_utils → utils_k8s}/secrets.py +4 -0
  150. adam/utils_k8s/service_accounts.py +169 -0
  151. adam/{k8s_utils → utils_k8s}/statefulsets.py +5 -4
  152. adam/{k8s_utils → utils_k8s}/volumes.py +9 -0
  153. adam/utils_net.py +24 -0
  154. adam/utils_repl/__init__.py +0 -0
  155. adam/utils_repl/automata_completer.py +48 -0
  156. adam/utils_repl/repl_completer.py +46 -0
  157. adam/utils_repl/state_machine.py +173 -0
  158. adam/utils_sqlite.py +101 -0
  159. adam/version.py +1 -1
  160. {kaqing-1.98.15.dist-info → kaqing-2.0.145.dist-info}/METADATA +1 -1
  161. kaqing-2.0.145.dist-info/RECORD +227 -0
  162. adam/commands/bash.py +0 -87
  163. adam/commands/cql_utils.py +0 -53
  164. adam/commands/devices.py +0 -89
  165. adam/commands/frontend/setup.py +0 -60
  166. adam/commands/frontend/setup_frontend.py +0 -58
  167. adam/commands/frontend/teardown.py +0 -61
  168. adam/commands/postgres/postgres_session.py +0 -225
  169. adam/commands/user_entry.py +0 -77
  170. adam/k8s_utils/pods.py +0 -211
  171. kaqing-1.98.15.dist-info/RECORD +0 -160
  172. /adam/commands/{frontend → audit}/__init__.py +0 -0
  173. /adam/{k8s_utils → commands/bash}/__init__.py +0 -0
  174. /adam/{medusa_show_restorejobs.py → commands/cql/__init__.py} +0 -0
  175. /adam/{k8s_utils → utils_k8s}/custom_resources.py +0 -0
  176. /adam/{k8s_utils → utils_k8s}/ingresses.py +0 -0
  177. /adam/{k8s_utils → utils_k8s}/services.py +0 -0
  178. {kaqing-1.98.15.dist-info → kaqing-2.0.145.dist-info}/WHEEL +0 -0
  179. {kaqing-1.98.15.dist-info → kaqing-2.0.145.dist-info}/entry_points.txt +0 -0
  180. {kaqing-1.98.15.dist-info → kaqing-2.0.145.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,85 @@
1
+ from adam.commands.command import Command
2
+ from adam.commands.export.export_databases import ExportDatabases
3
+ from adam.repl_state import ReplState, RequiredState
4
+ from adam.sql.sql_completer import SqlCompleter, SqlVariant
5
+ from adam.utils import log2
6
+ from adam.utils_athena import Athena
7
+ from adam.utils_sqlite import SQLite
8
+
9
+ class ExportSelect(Command):
10
+ COMMAND = '.select'
11
+
12
+ # the singleton pattern
13
+ def __new__(cls, *args, **kwargs):
14
+ if not hasattr(cls, 'instance'): cls.instance = super(ExportSelect, cls).__new__(cls)
15
+
16
+ return cls.instance
17
+
18
+ def __init__(self, successor: Command=None):
19
+ super().__init__(successor)
20
+
21
+ def command(self):
22
+ return ExportSelect.COMMAND
23
+
24
+ def required(self):
25
+ return RequiredState.EXPORT_DB
26
+
27
+ def run(self, cmd: str, state: ReplState):
28
+ if not(args := self.args(cmd)):
29
+ return super().run(cmd, state)
30
+
31
+ state, args = self.apply_state(args, state)
32
+ if not self.validate_state(state):
33
+ return state
34
+
35
+ if not state.export_session:
36
+ if state.in_repl:
37
+ if state.device == ReplState.C:
38
+ log2("Select an export database first with 'use' command.")
39
+ else:
40
+ log2('cd to an export database first.')
41
+ else:
42
+ log2('* export database is missing.')
43
+
44
+ Command.display_help()
45
+
46
+ return 'command-missing'
47
+
48
+ if not args:
49
+ if state.in_repl:
50
+ log2('Use a SQL statement.')
51
+ else:
52
+ log2('* SQL statement is missing.')
53
+
54
+ Command.display_help()
55
+
56
+ return 'command-missing'
57
+
58
+ query = ' '.join(args)
59
+
60
+ ExportDatabases.run_query(f'select {query}', database=state.export_session)
61
+
62
+ return state
63
+
64
+ def completion(self, state: ReplState):
65
+ if not state.export_session:
66
+ return {}
67
+
68
+ db = state.export_session
69
+
70
+ # warm up the caches first time when x: drive is accessed
71
+ ExportDatabases.table_names(db)
72
+ Athena.column_names(database=db, function='export')
73
+ Athena.column_names(partition_cols_only=True, database=db, function='export')
74
+
75
+ return {ExportSelect.COMMAND: SqlCompleter(
76
+ lambda: ExportDatabases.table_names(db),
77
+ dml='select',
78
+ expandables={
79
+ 'columns':lambda table: Athena.column_names(database=db, function='export'),
80
+ },
81
+ variant=SqlVariant.ATHENA
82
+ )}
83
+
84
+ def help(self, _: ReplState):
85
+ return f'.<sql-select-statements>\t run queries on export database'
@@ -0,0 +1,54 @@
1
+ from adam.commands.command import Command
2
+ from adam.commands.export.export_databases import ExportDatabases
3
+ from adam.repl_state import ReplState, RequiredState
4
+ from adam.sql.sql_completer import SqlCompleter, SqlVariant
5
+ from adam.utils_athena import Athena
6
+
7
+ # No action body, only for a help entry and auto-completion
8
+ class ExportSelectX(Command):
9
+ COMMAND = 'select_on_x'
10
+
11
+ # the singleton pattern
12
+ def __new__(cls, *args, **kwargs):
13
+ if not hasattr(cls, 'instance'): cls.instance = super(ExportSelectX, 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 ExportSelectX.COMMAND
22
+
23
+ def required(self):
24
+ return RequiredState.EXPORT_DB
25
+
26
+ def completion(self, state: ReplState):
27
+ completions = {}
28
+
29
+ if state.device == ReplState.X:
30
+ completions = {'drop': SqlCompleter(
31
+ lambda: ExportDatabases.table_names(state.export_session),
32
+ dml='drop',
33
+ expandables={
34
+ 'export-dbs': lambda: ExportDatabases.database_names(),
35
+ 'columns':lambda _: Athena.column_names(database=state.export_session, function='export'),
36
+ },
37
+ variant=SqlVariant.ATHENA
38
+ )}
39
+
40
+ if state.export_session:
41
+ completions |= {'select': SqlCompleter(
42
+ lambda: ExportDatabases.table_names(state.export_session),
43
+ dml='select',
44
+ expandables={
45
+ 'export-dbs': lambda: ExportDatabases.database_names(),
46
+ 'columns':lambda _: Athena.column_names(database=state.export_session, function='export'),
47
+ },
48
+ variant=SqlVariant.ATHENA
49
+ )}
50
+
51
+ return completions
52
+
53
+ def help(self, _: ReplState):
54
+ return f'<sql-select-statements>\t run queries on export database'
@@ -0,0 +1,55 @@
1
+ from adam.commands.command import Command
2
+ from adam.commands.export.export_databases import ExportDatabases
3
+ from adam.repl_state import ReplState
4
+ from adam.utils import log2
5
+ from adam.utils_athena import Athena
6
+ from adam.utils_sqlite import SQLite
7
+
8
+ class ExportUse(Command):
9
+ COMMAND = 'use'
10
+
11
+ # the singleton pattern
12
+ def __new__(cls, *args, **kwargs):
13
+ if not hasattr(cls, 'instance'): cls.instance = super(ExportUse, 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 ExportUse.COMMAND
22
+
23
+ def required(self):
24
+ return [ReplState.C, ReplState.X]
25
+
26
+ def run(self, cmd: str, state: ReplState):
27
+ if not(args := self.args(cmd)):
28
+ return super().run(cmd, state)
29
+
30
+ state, args = self.apply_state(args, state)
31
+ if not self.validate_state(state):
32
+ return state
33
+
34
+ if not args:
35
+ state.export_session = None
36
+
37
+ log2('Export database is unset.')
38
+
39
+ return state
40
+
41
+ state.export_session = args[0]
42
+ if state.export_session.startswith('e'):
43
+ Athena.clear_cache()
44
+ else:
45
+ SQLite.clear_cache()
46
+
47
+ ExportDatabases.display_export_db(state.export_session)
48
+
49
+ return state
50
+
51
+ def completion(self, state: ReplState):
52
+ return super().completion(state, {n: None for n in ExportDatabases.database_names()})
53
+
54
+ def help(self, _: ReplState):
55
+ return f'{ExportUse.COMMAND} <export-database-name>\t use export database'
@@ -0,0 +1,364 @@
1
+ from concurrent.futures import ThreadPoolExecutor, as_completed
2
+ from datetime import datetime
3
+ import functools
4
+ import re
5
+ import time
6
+ import traceback
7
+
8
+ from adam.commands.cql.cql_utils import cassandra_table_names, run_cql, table_spec
9
+ from adam.commands.export.export_databases import ExportDatabases
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, find_files
14
+ from adam.config import Config
15
+ from adam.pod_exec_result import PodExecResult
16
+ from adam.repl_state import ReplState
17
+ from adam.utils import elapsed_time, log2, ing
18
+ from adam.utils_k8s.cassandra_nodes import CassandraNodes
19
+ from adam.utils_k8s.pods import log_prefix
20
+ from adam.utils_k8s.statefulsets import StatefulSets
21
+
22
+ class Exporter:
23
+ def export_tables(args: list[str], state: ReplState, export_only: bool = False, max_workers = 0) -> tuple[list[str], ExportSpec]:
24
+ if export_only:
25
+ log2('export-only for testing')
26
+
27
+ spec: ExportSpec = None
28
+ try:
29
+ spec = Exporter.export_spec(' '.join(args), state)
30
+
31
+ statuses, spec = Exporter._export_tables(spec, state, max_workers=max_workers, export_state='init')
32
+ if not statuses:
33
+ return statuses, spec
34
+
35
+ return Exporter._export_tables(spec, state, export_only, max_workers, 'pending_export')
36
+ except Exception as e:
37
+ log2(e)
38
+
39
+ return [], None
40
+
41
+ def export_spec(spec_str: str, state: ReplState):
42
+ spec: ExportSpec = ExportSpec.parse_specs(spec_str)
43
+
44
+ session = state.export_session
45
+ if session:
46
+ if spec.importer:
47
+ importer_from_session = Importer.importer_from_session(session)
48
+ if spec.importer != importer_from_session:
49
+ if spec.importer == 'csv':
50
+ prefix = Importer.prefix_from_importer(spec.importer)
51
+ session = f'{prefix}{session[1:]}'
52
+ else:
53
+ raise Exception(f"You're currently using {importer_from_session} export database. You cannot export tables with {spec.importer} type database.")
54
+ else:
55
+ spec.importer = Importer.importer_from_session(session)
56
+ else:
57
+ if not spec.importer:
58
+ spec.importer = Config().get('export.default-importer', 'sqlite')
59
+
60
+ prefix = Importer.prefix_from_importer(spec.importer)
61
+ session = f'{prefix}{datetime.now().strftime("%Y%m%d%H%M%S")[3:]}'
62
+ if spec.importer != 'csv':
63
+ state.export_session = session
64
+
65
+ spec.session = session
66
+
67
+ return spec
68
+
69
+ def import_session(args: list[str], state: ReplState, max_workers = 0) -> tuple[list[str], ExportSpec]:
70
+ import_spec: ImportSpec = None
71
+ try:
72
+ import_spec = Exporter.import_spec(' '.join(args), state)
73
+ tables, status_in_whole = ExportTableStatus.from_session(state.sts, state.pod, state.namespace, import_spec.session)
74
+ if status_in_whole == 'done':
75
+ log2(f'The session has been completely done - no more csv files are found.')
76
+ return [], ExportSpec(None, None, importer=import_spec.importer, tables=[])
77
+
78
+ spec = ExportSpec(None, None, importer=import_spec.importer, tables=[ExportTableSpec.from_status(table) for table in tables], session=import_spec.session)
79
+
80
+ return Exporter._export_tables(spec, state, max_workers=max_workers)
81
+ except Exception as e:
82
+ if Config().is_debug():
83
+ traceback.print_exception(e)
84
+ else:
85
+ log2(e)
86
+
87
+ return [], None
88
+
89
+ def import_spec(spec_str: str, state: ReplState):
90
+ spec: ImportSpec = ImportSpec.parse_specs(spec_str)
91
+
92
+ session = state.export_session
93
+ if session:
94
+ if spec.importer:
95
+ importer = Importer.importer_from_session(state.export_session)
96
+ if spec.importer != importer:
97
+ raise Exception(f"You're currently using {importer} export database. You cannot import to {spec.importer} type database.")
98
+ else:
99
+ spec.importer = Importer.importer_from_session(state.export_session)
100
+ if not spec.importer:
101
+ spec.importer = Config().get('export.default-importer', 'sqlite')
102
+ else:
103
+ if spec.importer:
104
+ if not AthenaImporter.ping():
105
+ raise Exception('Credentials for Athena are not present.')
106
+ else:
107
+ spec.importer = Importer.importer_from_session(spec.session)
108
+
109
+ if spec.importer == 'csv':
110
+ spec.importer = Config().get('export.default-importer', 'sqlite')
111
+
112
+ prefix = Importer.prefix_from_importer(spec.importer)
113
+ session = f'{prefix}{spec.session[1:]}'
114
+ state.export_session = session
115
+
116
+ return spec
117
+
118
+ def _export_tables(spec: ExportSpec, state: ReplState, export_only = False, max_workers = 0, export_state = None) -> tuple[list[str], ExportSpec]:
119
+ if not spec.keyspace:
120
+ spec.keyspace = f'{state.namespace}_db'
121
+
122
+ if not spec.tables:
123
+ spec.tables = [ExportTableSpec.parse(t) for t in cassandra_table_names(state, keyspace=spec.keyspace)]
124
+
125
+ if not max_workers:
126
+ max_workers = Config().action_workers(f'export.{spec.importer}', 8)
127
+
128
+ if export_state == 'init':
129
+ CassandraNodes.exec(state.pod, state.namespace, f'rm -rf {csv_dir()}/{spec.session}_*', show_out=Config().is_debug(), shell='bash')
130
+
131
+ if max_workers > 1 and len(spec.tables) > 1:
132
+ log2(f'Executing on {len(spec.tables)} Cassandra tables in parallel...')
133
+ start_time = time.time()
134
+ try:
135
+ with ThreadPoolExecutor(max_workers=max_workers) as executor:
136
+ futures = [executor.submit(Exporter.export_table, table, state, spec.session, spec.importer, export_only, True, consistency=spec.consistency, export_state=export_state) for table in spec.tables]
137
+ if len(futures) == 0:
138
+ return [], spec
139
+
140
+ return [future.result() for future in as_completed(futures)], spec
141
+ finally:
142
+ log2(f"{len(spec.tables)} parallel table export elapsed time: {elapsed_time(start_time)} with {max_workers} workers")
143
+ else:
144
+ return [Exporter.export_table(table, state, spec.session, spec.importer, export_only, multi_tables=len(spec.tables) > 1, consistency=spec.consistency, export_state=export_state) for table in spec.tables], spec
145
+
146
+ def export_table(spec: ExportTableSpec, state: ReplState, session: str, importer: str, export_only = False, multi_tables = True, consistency: str = None, export_state=None):
147
+ s: str = None
148
+
149
+ table, target_table, columns = Exporter.resove_table_n_columns(spec, state, include_ks_in_target=False, importer=importer)
150
+
151
+ log_file = f'{log_prefix()}-{session}_{spec.keyspace}.{target_table}.log'
152
+ create_db = not state.export_session
153
+
154
+ if export_state == 'init':
155
+ Exporter.create_table_log(spec, state, session, table, target_table)
156
+ return 'table_log_created'
157
+ else:
158
+ if export_state == 'pending_export':
159
+ Exporter.export_to_csv(spec, state, session, table, target_table, columns, multi_tables=multi_tables, consistency=consistency)
160
+
161
+ log_files: list[str] = find_files(state.pod, state.namespace, f'{log_file}*')
162
+ if not log_files:
163
+ return s
164
+
165
+ log_file = log_files[0]
166
+
167
+ status: ExportTableStatus = ExportTableStatus.from_log_file(state.pod, state.namespace, session, log_file)
168
+ while status.status != 'done':
169
+ if status.status == 'export_in_pregress':
170
+ if Config().is_debug():
171
+ log2('Exporting to CSV is still in progess, sleeping for 1 sec...')
172
+ time.sleep(1)
173
+ elif status.status == 'exported':
174
+ log_file = Exporter.rename_to_pending_import(spec, state, session, target_table)
175
+ if importer == 'csv' or export_only:
176
+ return 'pending_import'
177
+ elif status.status == 'pending_import':
178
+ log_file, session = Exporter.import_from_csv(spec, state, session, importer, table, target_table, columns, multi_tables=multi_tables, create_db=create_db)
179
+
180
+ status = ExportTableStatus.from_log_file(state.pod, state.namespace, session, log_file)
181
+
182
+ return status.status
183
+
184
+ def create_table_log(spec: ExportTableSpec, state: ReplState, session: str, table: str, target_table: str):
185
+ log_file = f'{log_prefix()}-{session}_{spec.keyspace}.{target_table}.log'
186
+
187
+ CassandraNodes.exec(state.pod, state.namespace, f'rm -f {log_file}* && touch {log_file}', show_out=Config().is_debug(), shell='bash')
188
+
189
+ return table
190
+
191
+ def export_to_csv(spec: ExportTableSpec, state: ReplState, session: str, table: str, target_table: str, columns: str, multi_tables = True, consistency: str = None):
192
+ db = f'{session}_{target_table}'
193
+
194
+ CassandraNodes.exec(state.pod, state.namespace, f'mkdir -p {csv_dir()}/{db}', show_out=Config().is_debug(), shell='bash')
195
+ csv_file = f'{csv_dir()}/{db}/{table}.csv'
196
+ log_file = f'{log_prefix()}-{session}_{spec.keyspace}.{target_table}.log'
197
+
198
+ suppress_ing_log = Config().is_debug() or multi_tables
199
+ queries = []
200
+ if consistency:
201
+ queries.append(f'CONSISTENCY {consistency}')
202
+ queries.append(f"COPY {spec.keyspace}.{table}({columns}) TO '{csv_file}' WITH HEADER = TRUE")
203
+ r: PodExecResult = ing(
204
+ f'[{session}] Dumping table {spec.keyspace}.{table}{f" with consistency {consistency}" if consistency else ""}',
205
+ lambda: run_cql(state, ';'.join(queries), show_out=Config().is_debug(), background=True, log_file=log_file),
206
+ suppress_log=suppress_ing_log)
207
+
208
+ return log_file
209
+
210
+ def rename_to_pending_import(spec: ExportTableSpec, state: ReplState, session: str, target_table: str):
211
+ log_file = f'{log_prefix()}-{session}_{spec.keyspace}.{target_table}.log'
212
+ to = f'{log_file}.pending_import'
213
+
214
+ CassandraNodes.exec(state.pod, state.namespace, f'mv {log_file} {to}', show_out=Config().is_debug(), shell='bash')
215
+
216
+ return to
217
+
218
+ 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):
219
+ im = AthenaImporter() if importer == 'athena' else SqliteImporter()
220
+ 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)
221
+
222
+ def clear_export_session_cache():
223
+ Exporter.find_export_sessions.cache_clear()
224
+ Exporter.export_session_names.cache_clear()
225
+
226
+ @functools.lru_cache()
227
+ def export_session_names(sts: str, pod: str, namespace: str, importer: str = None, export_state = None):
228
+ if not sts or not namespace:
229
+ return []
230
+
231
+ if not pod:
232
+ pod = StatefulSets.pod_names(sts, namespace)[0]
233
+
234
+ if not pod:
235
+ return []
236
+
237
+ return [session for session, state in Exporter.find_export_sessions(pod, namespace, importer).items() if not export_state or state == export_state]
238
+
239
+ @functools.lru_cache()
240
+ def find_export_sessions(pod: str, namespace: str, importer: str = None, limit = 100):
241
+ sessions: dict[str, str] = {}
242
+
243
+ prefix = Importer.prefix_from_importer(importer)
244
+
245
+ log_files: list[str] = find_files(pod, namespace, f'{log_prefix()}-{prefix}*_*.log*')
246
+
247
+ if not log_files:
248
+ return {}
249
+
250
+ for log_file in log_files[:limit]:
251
+ m = re.match(f'{log_prefix()}-(.*?)_.*\.log?(.*)', log_file)
252
+ if m:
253
+ s = m.group(1)
254
+ state = m.group(2) # '', '.pending_import', '.done'
255
+ if state:
256
+ state = state.strip('.')
257
+ else:
258
+ state = 'in_export'
259
+
260
+ if s not in sessions:
261
+ sessions[s] = state
262
+ elif sessions[s] == 'done' and state != 'done':
263
+ sessions[s] = state
264
+
265
+ return sessions
266
+
267
+ def clean_up_all_sessions(sts: str, pod: str, namespace: str):
268
+ if not sts or not namespace:
269
+ return False
270
+
271
+ if not pod:
272
+ pod = StatefulSets.pod_names(sts, namespace)[0]
273
+
274
+ CassandraNodes.exec(pod, namespace, f'rm -rf {csv_dir()}/*', show_out=Config().is_debug(), shell='bash')
275
+ CassandraNodes.exec(pod, namespace, f'rm -rf {log_prefix()}-*.log*', show_out=Config().is_debug(), shell='bash')
276
+
277
+ return True
278
+
279
+ def clean_up_sessions(sts: str, pod: str, namespace: str, sessions: list[str], max_workers = 0):
280
+ if not sessions:
281
+ return []
282
+
283
+ if not max_workers:
284
+ max_workers = Config().action_workers('export', 8)
285
+
286
+ if max_workers > 1 and len(sessions) > 1:
287
+ log2(f'Executing on {len(sessions)} export session clean ups in parallel...')
288
+ start_time = time.time()
289
+ try:
290
+ with ThreadPoolExecutor(max_workers=max_workers) as executor:
291
+ futures = [executor.submit(Exporter.clean_up_session, sts, pod, namespace, session, True) for session in sessions]
292
+ if len(futures) == 0:
293
+ return []
294
+
295
+ return [future.result() for future in as_completed(futures)]
296
+ finally:
297
+ log2(f"{len(sessions)} parallel session clean ups elapsed time: {elapsed_time(start_time)} with {max_workers} workers")
298
+ else:
299
+ return [Exporter.clean_up_session(sts, pod, namespace, session) for session in sessions]
300
+
301
+ def clean_up_session(sts: str, pod: str, namespace: str, session: str, multi_tables = True):
302
+ if not sts or not namespace:
303
+ return 0, 0
304
+
305
+ if not pod:
306
+ pod = StatefulSets.pod_names(sts, namespace)[0]
307
+
308
+ if not pod:
309
+ return 0, 0
310
+
311
+ csv_cnt = 0
312
+ log_cnt = 0
313
+
314
+ log_files: list[str] = find_files(pod, namespace, f'{log_prefix()}-{session}_*.log*')
315
+
316
+ for log_file in log_files:
317
+ m = re.match(f'{log_prefix()}-{session}_(.*?)\.(.*?)\.log.*', log_file)
318
+ if m:
319
+ table = m.group(2)
320
+
321
+ CassandraNodes.exec(pod, namespace, f'rm -rf {csv_dir()}/{session}_{table}', show_out=not multi_tables, shell='bash')
322
+ csv_cnt += 1
323
+
324
+ CassandraNodes.exec(pod, namespace, f'rm -rf {log_file}', show_out=not multi_tables, shell='bash')
325
+ log_cnt += 1
326
+
327
+ return csv_cnt, log_cnt
328
+
329
+ def resove_table_n_columns(spec: ExportTableSpec, state: ReplState, include_ks_in_target = False, importer = 'sqlite'):
330
+ table = spec.table
331
+ columns = spec.columns
332
+ if not columns:
333
+ columns = Config().get(f'export.{importer}.columns', f'<keys>')
334
+
335
+ keyspaced_table = f'{spec.keyspace}.{spec.table}'
336
+ if columns == '<keys>':
337
+ columns = ','.join(table_spec(state, keyspaced_table, on_any=True).keys())
338
+ elif columns == '<row-key>':
339
+ columns = table_spec(state, keyspaced_table, on_any=True).row_key()
340
+ elif columns == '*':
341
+ columns = ','.join([c.name for c in table_spec(state, keyspaced_table, on_any=True).columns])
342
+
343
+ if not columns:
344
+ log2(f'ERROR: Empty columns on {table}.')
345
+ return table, None, None
346
+
347
+ target_table = spec.target_table if spec.target_table else table
348
+ if not include_ks_in_target and '.' in target_table:
349
+ target_table = target_table.split('.')[-1]
350
+
351
+ return table, target_table, columns
352
+
353
+ def drop_databases(sts: str, pod: str, namespace: str, db: str = None):
354
+ importer = None
355
+ if db:
356
+ importer = Importer.importer_from_session(db)
357
+
358
+ sessions_done = Exporter.export_session_names(sts, pod, namespace, importer=importer, export_state='done')
359
+ sessions = ExportDatabases.sessions_from_dbs(ExportDatabases.drop_export_dbs(db))
360
+ if sessions_done and sessions:
361
+ intersects = list(set(sessions_done) & set(sessions))
362
+ with ing(f'Cleaning up {len(intersects)} completed sessions'):
363
+ Exporter.clean_up_sessions(sts, pod, namespace, list(intersects))
364
+ Exporter.clear_export_session_cache()
@@ -0,0 +1,68 @@
1
+ from adam.commands.command import Command
2
+ from adam.commands.export.export_databases import ExportDatabases
3
+ from adam.commands.export.exporter import Exporter
4
+ from adam.repl_state import ReplState, RequiredState
5
+ from adam.utils import log, log2
6
+ from adam.utils_k8s.statefulsets import StatefulSets
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
+ state, args = self.apply_state(args, state)
31
+ if not self.validate_state(state):
32
+ return state
33
+
34
+ if not args:
35
+ if state.in_repl:
36
+ log2('Specify export session name.')
37
+ else:
38
+ log2('* Export session name is missing.')
39
+
40
+ Command.display_help()
41
+
42
+ return 'command-missing'
43
+
44
+ if not state.pod:
45
+ state.push()
46
+ state.pod = StatefulSets.pod_names(state.sts, state.namespace)[0]
47
+
48
+ try:
49
+ tables, _ = Exporter.import_session(args, state)
50
+ if tables:
51
+ Exporter.clear_export_session_cache()
52
+
53
+ log()
54
+ ExportDatabases.display_export_db(state.export_session)
55
+ finally:
56
+ state.pop()
57
+
58
+ return state
59
+
60
+ def completion(self, state: ReplState):
61
+ # warm up cache
62
+ Exporter.export_session_names(state.sts, state.pod, state.namespace)
63
+ Exporter.export_session_names(state.sts, state.pod, state.namespace, export_state='pending_import')
64
+
65
+ return {}
66
+
67
+ def help(self, _: ReplState):
68
+ return f'{ImportSession.COMMAND} <export-session-name>\t import files in session to Athena or SQLite'