Skip to content

Commit 04abce6

Browse files
committed
Admin/XMover: Refactor -- util
1 parent a723993 commit 04abce6

File tree

10 files changed

+144
-135
lines changed

10 files changed

+144
-135
lines changed

cratedb_toolkit/admin/xmover/analyze/report.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from cratedb_toolkit.admin.xmover.analyze.shard import ShardAnalyzer
99
from cratedb_toolkit.admin.xmover.model import ShardMoveRecommendation, ShardMoveRequest, SizeCriteria
10-
from cratedb_toolkit.admin.xmover.util import format_percentage, format_size
10+
from cratedb_toolkit.admin.xmover.util.format import format_percentage, format_size
1111

1212
console = Console()
1313

cratedb_toolkit/admin/xmover/analyze/shard.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
from collections import defaultdict
88
from typing import Any, Dict, List, Optional, Set, Tuple, Union
99

10-
from cratedb_toolkit.admin.xmover.database import CrateDBClient
1110
from cratedb_toolkit.admin.xmover.model import (
1211
DistributionStats,
1312
NodeInfo,
1413
RecommendationConstraints,
1514
ShardInfo,
1615
ShardMoveRecommendation,
1716
)
17+
from cratedb_toolkit.admin.xmover.util.database import CrateDBClient
1818

1919
logger = logging.getLogger(__name__)
2020

cratedb_toolkit/admin/xmover/analyze/zone.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from rich.table import Table
77

88
from cratedb_toolkit.admin.xmover.analyze.shard import ShardAnalyzer
9-
from cratedb_toolkit.admin.xmover.database import CrateDBClient
109
from cratedb_toolkit.admin.xmover.model import ShardInfo
10+
from cratedb_toolkit.admin.xmover.util.database import CrateDBClient
1111

1212
console = Console()
1313

cratedb_toolkit/admin/xmover/cli.py

Lines changed: 4 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
"""
66

77
import sys
8-
from typing import List, Optional, cast
8+
from typing import Optional
99

1010
import click
1111
from rich.console import Console
12-
from rich.panel import Panel
1312

1413
from cratedb_toolkit.admin.xmover.analyze.report import ShardReporter
1514
from cratedb_toolkit.admin.xmover.analyze.shard import ShardAnalyzer
@@ -21,8 +20,8 @@
2120
)
2221
from cratedb_toolkit.admin.xmover.tune.recommend import Recommender
2322
from cratedb_toolkit.admin.xmover.tune.recover import RecoveryMonitor, RecoveryOptions
24-
25-
from .database import CrateDBClient
23+
from cratedb_toolkit.admin.xmover.util.database import CrateDBClient
24+
from cratedb_toolkit.admin.xmover.util.error import explain_cratedb_error
2625

2726
console = Console()
2827

@@ -236,130 +235,7 @@ def explain_error(ctx, error_message: Optional[str]):
236235
237236
Example: xmover explain-error "NO(a copy of this shard is already allocated to this node)"
238237
"""
239-
console.print(Panel.fit("[bold blue]CrateDB Error Message Decoder[/bold blue]"))
240-
console.print("[dim]Helps decode and troubleshoot CrateDB shard allocation errors[/dim]")
241-
console.print()
242-
243-
if not error_message:
244-
console.print("Please paste the CrateDB error message (press Enter twice when done):")
245-
lines: List[str] = []
246-
while True:
247-
try:
248-
line = input()
249-
if line.strip() == "" and lines:
250-
break
251-
lines.append(line)
252-
except (EOFError, KeyboardInterrupt):
253-
break
254-
error_message = "\n".join(lines)
255-
256-
if not error_message.strip():
257-
console.print("[yellow]No error message provided[/yellow]")
258-
return
259-
260-
console.print("[dim]Analyzing error message...[/dim]")
261-
console.print()
262-
263-
# Common CrateDB allocation error patterns and solutions
264-
error_patterns = [
265-
{
266-
"pattern": "a copy of this shard is already allocated to this node",
267-
"title": "Node Already Has Shard Copy",
268-
"explanation": "The target node already contains a copy (primary or replica) of this shard.",
269-
"solutions": [
270-
"Choose a different target node that doesn't have this shard",
271-
"Use 'xmover zone-analysis --show-shards' to see current distribution",
272-
"Verify the shard ID and table name are correct",
273-
],
274-
"prevention": "Always check current shard locations before moving",
275-
},
276-
{
277-
"pattern": "there are too many copies of the shard allocated to nodes with attribute",
278-
"title": "Zone Allocation Limit Exceeded",
279-
"explanation": "CrateDB's zone awareness prevents too many copies in the same zone.",
280-
"solutions": [
281-
"Move the shard to a different availability zone",
282-
"Check zone balance with 'xmover check-balance'",
283-
"Ensure target zone doesn't already have copies of this shard",
284-
],
285-
"prevention": "Use 'xmover recommend' which respects zone constraints",
286-
},
287-
{
288-
"pattern": "not enough disk space",
289-
"title": "Insufficient Disk Space",
290-
"explanation": "The target node doesn't have enough free disk space for the shard.",
291-
"solutions": [
292-
"Free up space on the target node",
293-
"Choose a node with more available capacity",
294-
"Check available space with 'xmover analyze'",
295-
],
296-
"prevention": "Use '--min-free-space' parameter in recommendations",
297-
},
298-
{
299-
"pattern": "shard recovery limit",
300-
"title": "Recovery Limit Exceeded",
301-
"explanation": "Too many shards are currently being moved/recovered simultaneously.",
302-
"solutions": [
303-
"Wait for current recoveries to complete",
304-
"Check recovery status in CrateDB admin UI",
305-
"Reduce concurrent recoveries in cluster settings",
306-
],
307-
"prevention": "Move shards gradually, monitor recovery progress",
308-
},
309-
{
310-
"pattern": "allocation is disabled",
311-
"title": "Allocation Disabled",
312-
"explanation": "Shard allocation is temporarily disabled in the cluster.",
313-
"solutions": [
314-
"Re-enable allocation: PUT /_cluster/settings "
315-
'{"persistent":{"cluster.routing.allocation.enable":"all"}}',
316-
"Check if allocation was disabled for maintenance",
317-
"Verify cluster health before re-enabling",
318-
],
319-
"prevention": "Check allocation status before performing moves",
320-
},
321-
]
322-
323-
# Find matching patterns
324-
matches = []
325-
error_lower = error_message.lower()
326-
327-
for pattern_info in error_patterns:
328-
if cast(str, pattern_info["pattern"]).lower() in error_lower:
329-
matches.append(pattern_info)
330-
331-
if matches:
332-
for i, match in enumerate(matches):
333-
if i > 0:
334-
console.print("\n" + "─" * 60 + "\n")
335-
336-
console.print(f"[bold red]🚨 {match['title']}[/bold red]")
337-
console.print(f"[yellow]📝 Explanation:[/yellow] {match['explanation']}")
338-
console.print()
339-
340-
console.print("[green]💡 Solutions:[/green]")
341-
for j, solution in enumerate(match["solutions"], 1):
342-
console.print(f" {j}. {solution}")
343-
console.print()
344-
345-
console.print(f"[blue]🛡️ Prevention:[/blue] {match['prevention']}")
346-
else:
347-
console.print("[yellow]⚠ No specific pattern match found[/yellow]")
348-
console.print()
349-
console.print("[bold]General Troubleshooting Steps:[/bold]")
350-
console.print("1. Check current shard distribution: [cyan]xmover analyze[/cyan]")
351-
console.print(
352-
"2. Validate the specific move: [cyan]xmover validate-move schema.table shard_id from_node to_node[/cyan]"
353-
)
354-
console.print("3. Check zone conflicts: [cyan]xmover zone-analysis --show-shards[/cyan]")
355-
console.print("4. Verify node capacity: [cyan]xmover analyze[/cyan]")
356-
console.print("5. Review CrateDB documentation on shard allocation")
357-
358-
console.print()
359-
console.print("[dim]💡 Tip: Use 'xmover validate-move' to check moves before execution[/dim]")
360-
console.print(
361-
"[dim]📚 For more help: https://crate.io/docs/crate/reference/en/latest/admin/system-information.html[/dim]"
362-
)
238+
explain_cratedb_error(error_message)
363239

364240

365241
@main.command()

cratedb_toolkit/admin/xmover/tune/recommend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
from rich.table import Table
77

88
from cratedb_toolkit.admin.xmover.analyze.shard import ShardAnalyzer
9-
from cratedb_toolkit.admin.xmover.database import CrateDBClient
109
from cratedb_toolkit.admin.xmover.model import RecommendationConstraints
1110
from cratedb_toolkit.admin.xmover.tune.recover import RecoveryMonitor, RecoveryOptions
12-
from cratedb_toolkit.admin.xmover.util import format_size
11+
from cratedb_toolkit.admin.xmover.util.database import CrateDBClient
12+
from cratedb_toolkit.admin.xmover.util.format import format_size
1313

1414
console = Console()
1515

cratedb_toolkit/admin/xmover/tune/recover.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
from rich.console import Console
77

8-
from cratedb_toolkit.admin.xmover.database import CrateDBClient
98
from cratedb_toolkit.admin.xmover.model import RecoveryInfo
10-
from cratedb_toolkit.admin.xmover.util import format_translog_info
9+
from cratedb_toolkit.admin.xmover.util.database import CrateDBClient
10+
from cratedb_toolkit.admin.xmover.util.format import format_translog_info
1111

1212
console = Console()
1313

cratedb_toolkit/admin/xmover/util/__init__.py

Whitespace-only changes.
File renamed without changes.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
from typing import List, Optional, cast
2+
3+
from rich.console import Console
4+
from rich.panel import Panel
5+
6+
console = Console()
7+
8+
9+
def explain_cratedb_error(error_message: Optional[str]):
10+
console.print(Panel.fit("[bold blue]CrateDB Error Message Decoder[/bold blue]"))
11+
console.print("[dim]Helps decode and troubleshoot CrateDB shard allocation errors[/dim]")
12+
console.print()
13+
14+
if not error_message:
15+
console.print("Please paste the CrateDB error message (press Enter twice when done):")
16+
lines: List[str] = []
17+
while True:
18+
try:
19+
line = input()
20+
if line.strip() == "" and lines:
21+
break
22+
lines.append(line)
23+
except (EOFError, KeyboardInterrupt):
24+
break
25+
error_message = "\n".join(lines)
26+
27+
if not error_message.strip():
28+
console.print("[yellow]No error message provided[/yellow]")
29+
return
30+
31+
console.print("[dim]Analyzing error message...[/dim]")
32+
console.print()
33+
34+
# Common CrateDB allocation error patterns and solutions
35+
error_patterns = [
36+
{
37+
"pattern": "a copy of this shard is already allocated to this node",
38+
"title": "Node Already Has Shard Copy",
39+
"explanation": "The target node already contains a copy (primary or replica) of this shard.",
40+
"solutions": [
41+
"Choose a different target node that doesn't have this shard",
42+
"Use 'xmover zone-analysis --show-shards' to see current distribution",
43+
"Verify the shard ID and table name are correct",
44+
],
45+
"prevention": "Always check current shard locations before moving",
46+
},
47+
{
48+
"pattern": "there are too many copies of the shard allocated to nodes with attribute",
49+
"title": "Zone Allocation Limit Exceeded",
50+
"explanation": "CrateDB's zone awareness prevents too many copies in the same zone.",
51+
"solutions": [
52+
"Move the shard to a different availability zone",
53+
"Check zone balance with 'xmover check-balance'",
54+
"Ensure target zone doesn't already have copies of this shard",
55+
],
56+
"prevention": "Use 'xmover recommend' which respects zone constraints",
57+
},
58+
{
59+
"pattern": "not enough disk space",
60+
"title": "Insufficient Disk Space",
61+
"explanation": "The target node doesn't have enough free disk space for the shard.",
62+
"solutions": [
63+
"Free up space on the target node",
64+
"Choose a node with more available capacity",
65+
"Check available space with 'xmover analyze'",
66+
],
67+
"prevention": "Use '--min-free-space' parameter in recommendations",
68+
},
69+
{
70+
"pattern": "shard recovery limit",
71+
"title": "Recovery Limit Exceeded",
72+
"explanation": "Too many shards are currently being moved/recovered simultaneously.",
73+
"solutions": [
74+
"Wait for current recoveries to complete",
75+
"Check recovery status in CrateDB admin UI",
76+
"Reduce concurrent recoveries in cluster settings",
77+
],
78+
"prevention": "Move shards gradually, monitor recovery progress",
79+
},
80+
{
81+
"pattern": "allocation is disabled",
82+
"title": "Allocation Disabled",
83+
"explanation": "Shard allocation is temporarily disabled in the cluster.",
84+
"solutions": [
85+
"Re-enable allocation: PUT /_cluster/settings "
86+
'{"persistent":{"cluster.routing.allocation.enable":"all"}}',
87+
"Check if allocation was disabled for maintenance",
88+
"Verify cluster health before re-enabling",
89+
],
90+
"prevention": "Check allocation status before performing moves",
91+
},
92+
]
93+
94+
# Find matching patterns
95+
matches = []
96+
error_lower = error_message.lower()
97+
98+
for pattern_info in error_patterns:
99+
if cast(str, pattern_info["pattern"]).lower() in error_lower:
100+
matches.append(pattern_info)
101+
102+
if matches:
103+
for i, match in enumerate(matches):
104+
if i > 0:
105+
console.print("\n" + "─" * 60 + "\n")
106+
107+
console.print(f"[bold red]🚨 {match['title']}[/bold red]")
108+
console.print(f"[yellow]📝 Explanation:[/yellow] {match['explanation']}")
109+
console.print()
110+
111+
console.print("[green]💡 Solutions:[/green]")
112+
for j, solution in enumerate(match["solutions"], 1):
113+
console.print(f" {j}. {solution}")
114+
console.print()
115+
116+
console.print(f"[blue]🛡️ Prevention:[/blue] {match['prevention']}")
117+
else:
118+
console.print("[yellow]⚠ No specific pattern match found[/yellow]")
119+
console.print()
120+
console.print("[bold]General Troubleshooting Steps:[/bold]")
121+
console.print("1. Check current shard distribution: [cyan]xmover analyze[/cyan]")
122+
console.print(
123+
"2. Validate the specific move: [cyan]xmover validate-move schema.table shard_id from_node to_node[/cyan]"
124+
)
125+
console.print("3. Check zone conflicts: [cyan]xmover zone-analysis --show-shards[/cyan]")
126+
console.print("4. Verify node capacity: [cyan]xmover analyze[/cyan]")
127+
console.print("5. Review CrateDB documentation on shard allocation")
128+
129+
console.print()
130+
console.print("[dim]💡 Tip: Use 'xmover validate-move' to check moves before execution[/dim]")
131+
console.print(
132+
"[dim]📚 For more help: https://crate.io/docs/crate/reference/en/latest/admin/system-information.html[/dim]"
133+
)
File renamed without changes.

0 commit comments

Comments
 (0)