Skip to content

Commit 539d931

Browse files
committed
Chore: Address minor nitpicks and more from code review by CodeRabbit
1 parent 2cbe824 commit 539d931

15 files changed

Lines changed: 66 additions & 27 deletions

File tree

cratedb_toolkit/cluster/cli.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,15 @@ def ping(ctx: click.Context, cluster_id: str, cluster_name: str):
104104
Ping cluster: API and database.
105105
"""
106106
with handle_command_errors("ping cluster"):
107-
cluster_info = ClusterInformation.from_id_or_name(cluster_id=cluster_id, cluster_name=cluster_name)
108107
cluster = ManagedCluster(cluster_id=cluster_id, cluster_name=cluster_name).start()
109108
has_db_result = bool(cluster.query("SELECT 42 AS availability_check -- ctk"))
110109
response = {
111-
"meta": cluster_info.meta,
112-
"cloud": cluster_info.ready,
110+
"meta": cluster.info.meta,
111+
"cloud": cluster.info.ready,
113112
"database": has_db_result,
114113
}
115114
jd(response)
116-
sys.exit(0 if (cluster_info.ready and has_db_result) else 1)
115+
sys.exit(0 if (cluster.info.ready and has_db_result) else 1)
117116

118117

119118
@make_command(cli, name="start", help=help_start)

cratedb_toolkit/cluster/core.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ def __init__(
123123
self.cluster_name = cluster_name
124124
self.settings = settings or ManagedClusterSettings()
125125
self.address = address
126-
self.info: ClusterInformation = info or ClusterInformation()
126+
self._info: t.Optional[ClusterInformation] = info
127127
self.stop_on_exit = stop_on_exit
128128
self.exists: bool = False
129129

130130
# Default settings and sanity checks.
131131
self.cluster_id = self.cluster_id or self.settings.cluster_id or None
132132
self.cluster_name = self.cluster_name or self.settings.cluster_name or None
133-
if self.cluster_id is None and self.cluster_name is None:
133+
if not self.cluster_id and not self.cluster_name:
134134
raise DatabaseAddressMissingError(
135135
"Failed to address cluster: Either cluster identifier or name needs to be specified"
136136
)
@@ -203,12 +203,22 @@ def delete(self) -> "ManagedCluster":
203203
raise NotImplementedError("Deleting cluster not implemented yet")
204204
return self
205205

206-
def probe(self) -> "ManagedCluster":
206+
@property
207+
def info(self) -> ClusterInformation:
208+
if self._info is None:
209+
raise ValueError("Cluster information not yet available")
210+
return self._info
211+
212+
def probe(self, refresh: bool = False) -> "ManagedCluster":
207213
"""
208214
Probe a CrateDB Cloud cluster, API-wise.
209215
"""
216+
if refresh:
217+
self._info = None
218+
if self._info is not None:
219+
return self
210220
try:
211-
self.info = ClusterInformation.from_id_or_name(cluster_id=self.cluster_id, cluster_name=self.cluster_name)
221+
self._info = ClusterInformation.from_id_or_name(cluster_id=self.cluster_id, cluster_name=self.cluster_name)
212222
self.cluster_id = self.info.cloud["id"]
213223
self.cluster_name = self.info.cloud["name"]
214224
self.address = DatabaseAddress.from_http_uri(self.info.cloud["url"])

cratedb_toolkit/cluster/croud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def set_suspended(self, body: t.Dict[str, t.Any]):
327327
if data is None:
328328
if not errors:
329329
errors = "Unknown error"
330-
raise CroudException(f"Resuming cluster failed: {errors}")
330+
raise CroudException(f"Suspending or resuming cluster failed: {errors}")
331331
_wait_for_completed_operation(
332332
client=self.client,
333333
cluster_id=self.cluster_id,

cratedb_toolkit/cluster/model.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ def meta(self):
101101

102102
@property
103103
def health(self):
104+
try:
105+
self.acquire_dynamic_content()
106+
pass
107+
except Exception as e:
108+
logger.warning(f"Failed to refresh cluster information: {e}")
104109
info = self.asdict()
105110
return {
106111
"meta": self.meta,
@@ -128,13 +133,9 @@ def asdict(self) -> t.Dict[str, t.Any]:
128133
"""
129134
Convert to dictionary after acquiring dynamic content.
130135
"""
131-
try:
132-
self.refresh()
133-
except Exception as e:
134-
logger.warning(f"Failed to refresh cluster information: {e}")
135136
return deepcopy(dataclasses.asdict(self))
136137

137-
def refresh(self):
138+
def acquire_dynamic_content(self):
138139
"""
139140
Refresh dynamic content. Here: The `database` attribute, after inquiring the cluster.
140141
"""

cratedb_toolkit/io/croud.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def fix_job_info_table_name(self):
7777
full-qualified representation.
7878
7979
FIXME: Remove after upstream has fixed the flaw.
80+
https://github.com/crate/croud/issues/566
8081
"""
8182
job_info = self.info
8283
if "destination" in job_info and "table" in job_info["destination"]:

cratedb_toolkit/model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ def from_string(cls, url: str) -> "DatabaseAddress":
6464
"""
6565
Factory method to create an instance from an SQLAlchemy database URL in string format.
6666
"""
67-
if url.startswith("crate"):
67+
# Parse the URL to determine the scheme.
68+
parsed_url = URL(url)
69+
if parsed_url.scheme == "crate":
6870
return cls.from_sqlalchemy_uri(url)
6971
else:
7072
return cls.from_http_uri(url)

cratedb_toolkit/shell/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def cli(
7575
cluster_id=cluster_id,
7676
cluster_name=cluster_name,
7777
cluster_url=cluster_url,
78-
).probe()
78+
)
7979

8080
if cluster.address is None:
8181
raise click.UsageError("Inquiring cluster address failed.")

doc/backlog/main.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
# Main Backlog
22

33
## Iteration +0
4-
- CLI: Consider simplifying by using `--url` instead of `--sqlalchemy-url` or `--http-url`
4+
- CLI: Consider simplifying the API by using canonical `--cluster-url` instead of `--sqlalchemy-url` vs. `--http-url`
55
- CLI: Explore consolidating all four addressing options into a single `--cluster` option
6-
for more intuitive cluster addressing. This could be implemented as an option alongside existing methods.
6+
for more intuitive cluster addressing. This could be implemented as a separate option alongside
7+
existing methods.
8+
- `cratedb_toolkit/cfr/cli.py`: Migrate to `ctx.meta["address"]`, like `cratedb_toolkit/info/cli.py`
9+
- Cluster API: Currently, software tests do not prune tables on CrateDB Cloud.
10+
This will probably cause a disk spillover sooner or later on this instance.
11+
- When running the test suite, a huge log obstructs the output information.
12+
```
13+
2025-05-03 22:48:22,191 [crate.client.http ] DEBUG : JSON response for stmt(SELECT * FROM "sys"."allocations")
14+
2025-05-03 22:48:22,768 [crate.client.http ] DEBUG : JSON response for stmt(SELECT * FROM "sys"."jobs_log")
15+
```
16+
- Refactor `model.py`, `util/database.py`, and possibly more, to `ctk.model` module namespace
17+
- Search:
18+
- https://vectorvfs.readthedocs.io/en/latest/usage.html
19+
- https://news.ycombinator.com/item?id=43896011
720

821
## Iteration +1
922
- Table Loader: Refactor URL dispatcher, use fsspec
@@ -49,7 +62,7 @@
4962
- UX: Accept alias `--format {jsonl,ndjson}` for `--format json_row`
5063
- Catch recursion errors:
5164
```
52-
CRATEDB_SQLALCHEMY_URL=crate://crate@localhost:4200/
65+
CRATEDB_CLUSTER_URL=crate://crate@localhost:4200/
5366
```
5467
- CLI: Verify exit codes.
5568
- UX: Rename `ctk cluster info` to `ctk status cluster --id=foo-bar-baz`

doc/cluster/backlog.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@ a patch for some item, it will be very much welcome.
1212
[issues]: https://github.com/crate/cratedb-toolkit/issues
1313

1414
## Iteration +1
15-
- `ctk cluster info` does not include information about CrateDB yet.
16-
=> Connect to ex-WTF utilities.
17-
- Less verbosity by default for `ctk cluster` operations.
15+
- CLI: Less verbosity by default for `ctk cluster` operations
1816
Possibly display cluster operation and job execution outcomes, and `last_async_operation` details
19-
- Python API: Make `cluster.query` use the excellent `records` package
20-
- `ctk cluster list`: New
17+
- CLI: Implement `ctk cluster list`, `ctk cluster delete`
18+
- Managed: Use `keyring` for caching the JWT token, and compensate token expiry
2119

2220
## Iteration +2
21+
- Python API: Make `cluster.query` use the excellent `records` package
2322
- Xon.sh example
2423
- Windows support. Powershell?
2524
- Validate multi-cluster, multi-org operations work well and make sense
2625
- Implement JWT token expiry handling
2726
- Add Apache Spark / Databricks to the client bundle?
27+
- Use universal `DatabaseCluster` across the board. For that to happen, it will
28+
need to gain an `DatabaseCluster.from_env()` entry point.
2829

2930
## Iteration +3
3031
- Complete UI: `ctk cluster health`
@@ -82,8 +83,16 @@ a patch for some item, it will be very much welcome.
8283
'running_operation': '',
8384
'status': 'GREEN'},
8485
```
85-
- Shell: Currently it is not possible to create multiple instances of a `ManagedCluster`,
86+
- Shell: Currently it is not possible to create multiple instances of a `ManagedCluster`
8687
because parameters like `username` and `password` are obtained from the environment,
8788
using different credentials for connecting to individual clusters.
8889
- Document `ManagedCluster`'s `stop_on_exit` option
8990
- `toolkit/cluster/cli.py`: Also respect cluster ID and name across the board of ex-WTF utilities.
91+
- `ctk cluster info` does not include information about CrateDB yet.
92+
=> Connect to ex-WTF utilities.
93+
- Make CLI options override ENV vars. Otherwise, using `--sqlalchemy-url=crate://`
94+
will never work if an `.env` file is present.
95+
- SDK: Rename `ctk.cluster` to `ctk.sdk`? => No.
96+
- CLI: Shrink address URLs to single parameter `--cluster-url`
97+
- Changelog: Notify about breaking change with input address parameter names
98+
- Docs: Update guidelines about input address parameter preferences

doc/cluster/cli.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(cluster-api-cli)=
2+
23
# CrateDB Cluster CLI
34

45
The `ctk cluster {start,info,stop}` subcommands provide higher level CLI

0 commit comments

Comments
 (0)