-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopics.py
More file actions
205 lines (175 loc) · 7.7 KB
/
topics.py
File metadata and controls
205 lines (175 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""Topic management endpoints"""
from fastapi import APIRouter, HTTPException, status, Depends
from confluent_kafka.admin import NewTopic, ConfigResource, ResourceType, KafkaException
from auth import get_current_user, User
from models import TopicCreate, TopicConfigUpdate
from services.kafka_admin import get_admin_client
import logging
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/topics", tags=["Topics"])
@router.post("", status_code=status.HTTP_201_CREATED)
async def create_topic(topic: TopicCreate, current_user: User = Depends(get_current_user)):
"""Create a new topic in the Kafka cluster"""
try:
admin_client = get_admin_client()
# Check if topic already exists
metadata = admin_client.list_topics(timeout=10)
if topic.name in metadata.topics:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=f"Topic '{topic.name}' already exists"
)
# Create new topic
new_topic = NewTopic(
topic=topic.name,
num_partitions=topic.num_partitions,
replication_factor=topic.replication_factor,
config=topic.config or {}
)
# Execute topic creation
futures = admin_client.create_topics([new_topic])
# Wait for the operation to complete
for topic_name, future in futures.items():
try:
future.result(timeout=10)
logger.info(f"Topic '{topic_name}' created successfully")
except KafkaException as e:
logger.error(f"Failed to create topic '{topic_name}': {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to create topic: {str(e)}"
)
return {
"message": f"Topic '{topic.name}' created successfully",
"topic": {
"name": topic.name,
"num_partitions": topic.num_partitions,
"replication_factor": topic.replication_factor,
"config": topic.config or {}
}
}
except HTTPException:
raise
except Exception as e:
logger.error(f"Failed to create topic: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to create topic: {str(e)}"
)
@router.get("")
async def list_topics(current_user: User = Depends(get_current_user)):
"""List all topics in the Kafka cluster"""
try:
admin_client = get_admin_client()
metadata = admin_client.list_topics(timeout=10)
topics = []
for topic_name, topic_metadata in metadata.topics.items():
if not topic_name.startswith('_'):
partitions = topic_metadata.partitions
replication_factor = len(partitions[0].replicas) if partitions else 0
topics.append({
"name": topic_name,
"partition_count": len(partitions),
"replication_factor": replication_factor
})
return {
"count": len(topics),
"topics": topics
}
except Exception as e:
logger.error(f"Failed to list topics: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to list topics: {str(e)}"
)
@router.get("/{topic_name}")
async def describe_topic(topic_name: str, current_user: User = Depends(get_current_user)):
"""Get detailed information about a specific topic"""
try:
admin_client = get_admin_client()
metadata = admin_client.list_topics(topic=topic_name, timeout=10)
if topic_name not in metadata.topics:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Topic '{topic_name}' not found"
)
topic_metadata = metadata.topics[topic_name]
partitions = []
for partition_id, partition_metadata in topic_metadata.partitions.items():
partitions.append({
"partition_id": partition_id,
"leader": partition_metadata.leader,
"replicas": partition_metadata.replicas,
"isr": partition_metadata.isrs
})
config_resource = ConfigResource(ResourceType.TOPIC, topic_name)
configs_result = admin_client.describe_configs([config_resource])
topic_configs = {}
for res, future in configs_result.items():
try:
config_entries = future.result(timeout=10)
for config_name, config_entry in config_entries.items():
topic_configs[config_name] = {
"value": config_entry.value,
"source": str(config_entry.source),
"is_default": config_entry.is_default,
"is_sensitive": config_entry.is_sensitive
}
except Exception as e:
logger.warning(f"Failed to get configs for topic '{topic_name}': {str(e)}")
replication_factor = len(partitions[0]["replicas"]) if partitions else 0
return {
"name": topic_name,
"partition_count": len(partitions),
"replication_factor": replication_factor,
"partitions": partitions,
"config": topic_configs
}
except HTTPException:
raise
except Exception as e:
logger.error(f"Failed to describe topic '{topic_name}': {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to describe topic: {str(e)}"
)
@router.put("/{topic_name}")
async def alter_topic_config(topic_name: str, config_update: TopicConfigUpdate, current_user: User = Depends(get_current_user)):
"""Alter topic-level configuration properties"""
try:
admin_client = get_admin_client()
metadata = admin_client.list_topics(topic=topic_name, timeout=10)
if topic_name not in metadata.topics:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Topic '{topic_name}' not found"
)
config_resource = ConfigResource(
ResourceType.TOPIC,
topic_name,
set_config=config_update.config
)
futures = admin_client.alter_configs([config_resource])
for res, future in futures.items():
try:
future.result(timeout=10)
logger.info(f"Topic '{topic_name}' configuration updated successfully")
except KafkaException as e:
logger.error(f"Failed to update topic '{topic_name}' configuration: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to update topic configuration: {str(e)}"
)
return {
"message": f"Topic '{topic_name}' configuration updated successfully",
"topic": topic_name,
"updated_config": config_update.config
}
except HTTPException:
raise
except Exception as e:
logger.error(f"Failed to alter topic configuration: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to alter topic configuration: {str(e)}"
)