* chore: remove unreachable dead code across the codebase Remove ~50,000 lines of unreachable code identified by static analysis. Major removals: - weed/filer/redis_lua: entire unused Redis Lua filer store implementation - weed/wdclient/net2, resource_pool: unused connection/resource pool packages - weed/plugin/worker/lifecycle: unused lifecycle plugin worker - weed/s3api: unused S3 policy templates, presigned URL IAM, streaming copy, multipart IAM, key rotation, and various SSE helper functions - weed/mq/kafka: unused partition mapping, compression, schema, and protocol functions - weed/mq/offset: unused SQL storage and migration code - weed/worker: unused registry, task, and monitoring functions - weed/query: unused SQL engine, parquet scanner, and type functions - weed/shell: unused EC proportional rebalance functions - weed/storage/erasure_coding/distribution: unused distribution analysis functions - Individual unreachable functions removed from 150+ files across admin, credential, filer, iam, kms, mount, mq, operation, pb, s3api, server, shell, storage, topology, and util packages * fix(s3): reset shared memory store in IAM test to prevent flaky failure TestLoadIAMManagerFromConfig_EmptyConfigWithFallbackKey was flaky because the MemoryStore credential backend is a singleton registered via init(). Earlier tests that create anonymous identities pollute the shared store, causing LookupAnonymous() to unexpectedly return true. Fix by calling Reset() on the memory store before the test runs. * style: run gofmt on changed files * fix: restore KMS functions used by integration tests * fix(plugin): prevent panic on send to closed worker session channel The Plugin.sendToWorker method could panic with "send on closed channel" when a worker disconnected while a message was being sent. The race was between streamSession.close() closing the outgoing channel and sendToWorker writing to it concurrently. Add a done channel to streamSession that is closed before the outgoing channel, and check it in sendToWorker's select to safely detect closed sessions without panicking.
217 lines
6.9 KiB
Go
217 lines
6.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/seaweedfs/seaweedfs/weed/admin/dash"
|
|
"github.com/seaweedfs/seaweedfs/weed/admin/view/app"
|
|
"github.com/seaweedfs/seaweedfs/weed/admin/view/layout"
|
|
)
|
|
|
|
// MessageQueueHandlers contains all the HTTP handlers for message queue management
|
|
type MessageQueueHandlers struct {
|
|
adminServer *dash.AdminServer
|
|
}
|
|
|
|
// NewMessageQueueHandlers creates a new instance of MessageQueueHandlers
|
|
func NewMessageQueueHandlers(adminServer *dash.AdminServer) *MessageQueueHandlers {
|
|
return &MessageQueueHandlers{
|
|
adminServer: adminServer,
|
|
}
|
|
}
|
|
|
|
// ShowBrokers renders the message queue brokers page
|
|
func (h *MessageQueueHandlers) ShowBrokers(w http.ResponseWriter, r *http.Request) {
|
|
// Get cluster brokers data
|
|
brokersData, err := h.adminServer.GetClusterBrokers()
|
|
if err != nil {
|
|
writeJSONError(w, http.StatusInternalServerError, "Failed to get cluster brokers: "+err.Error())
|
|
return
|
|
}
|
|
|
|
// Set username
|
|
username := dash.UsernameFromContext(r.Context())
|
|
if username == "" {
|
|
username = "admin"
|
|
}
|
|
brokersData.Username = username
|
|
|
|
// Render HTML template
|
|
w.Header().Set("Content-Type", "text/html")
|
|
brokersComponent := app.ClusterBrokers(*brokersData)
|
|
viewCtx := layout.NewViewContext(r, username, dash.CSRFTokenFromContext(r.Context()))
|
|
layoutComponent := layout.Layout(viewCtx, brokersComponent)
|
|
err = layoutComponent.Render(r.Context(), w)
|
|
if err != nil {
|
|
writeJSONError(w, http.StatusInternalServerError, "Failed to render template: "+err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
// ShowTopics renders the message queue topics page
|
|
func (h *MessageQueueHandlers) ShowTopics(w http.ResponseWriter, r *http.Request) {
|
|
// Get topics data
|
|
topicsData, err := h.adminServer.GetTopics()
|
|
if err != nil {
|
|
writeJSONError(w, http.StatusInternalServerError, "Failed to get topics: "+err.Error())
|
|
return
|
|
}
|
|
|
|
// Set username
|
|
username := dash.UsernameFromContext(r.Context())
|
|
if username == "" {
|
|
username = "admin"
|
|
}
|
|
topicsData.Username = username
|
|
|
|
// Render HTML template
|
|
w.Header().Set("Content-Type", "text/html")
|
|
topicsComponent := app.Topics(*topicsData)
|
|
viewCtx := layout.NewViewContext(r, username, dash.CSRFTokenFromContext(r.Context()))
|
|
layoutComponent := layout.Layout(viewCtx, topicsComponent)
|
|
err = layoutComponent.Render(r.Context(), w)
|
|
if err != nil {
|
|
writeJSONError(w, http.StatusInternalServerError, "Failed to render template: "+err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
// ShowTopicDetails renders the topic details page
|
|
func (h *MessageQueueHandlers) ShowTopicDetails(w http.ResponseWriter, r *http.Request) {
|
|
// Get topic parameters from URL
|
|
vars := mux.Vars(r)
|
|
namespace := vars["namespace"]
|
|
topicName := vars["topic"]
|
|
|
|
if namespace == "" || topicName == "" {
|
|
writeJSONError(w, http.StatusBadRequest, "Missing namespace or topic name")
|
|
return
|
|
}
|
|
|
|
// Get topic details data
|
|
topicDetailsData, err := h.adminServer.GetTopicDetails(namespace, topicName)
|
|
if err != nil {
|
|
writeJSONError(w, http.StatusInternalServerError, "Failed to get topic details: "+err.Error())
|
|
return
|
|
}
|
|
|
|
// Set username
|
|
username := dash.UsernameFromContext(r.Context())
|
|
if username == "" {
|
|
username = "admin"
|
|
}
|
|
topicDetailsData.Username = username
|
|
|
|
// Render HTML template
|
|
w.Header().Set("Content-Type", "text/html")
|
|
topicDetailsComponent := app.TopicDetails(*topicDetailsData)
|
|
viewCtx := layout.NewViewContext(r, username, dash.CSRFTokenFromContext(r.Context()))
|
|
layoutComponent := layout.Layout(viewCtx, topicDetailsComponent)
|
|
err = layoutComponent.Render(r.Context(), w)
|
|
if err != nil {
|
|
writeJSONError(w, http.StatusInternalServerError, "Failed to render template: "+err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
// GetTopicDetailsAPI returns topic details as JSON for AJAX calls
|
|
func (h *MessageQueueHandlers) GetTopicDetailsAPI(w http.ResponseWriter, r *http.Request) {
|
|
// Get topic parameters from URL
|
|
vars := mux.Vars(r)
|
|
namespace := vars["namespace"]
|
|
topicName := vars["topic"]
|
|
|
|
if namespace == "" || topicName == "" {
|
|
writeJSONError(w, http.StatusBadRequest, "Missing namespace or topic name")
|
|
return
|
|
}
|
|
|
|
// Get topic details data
|
|
topicDetailsData, err := h.adminServer.GetTopicDetails(namespace, topicName)
|
|
if err != nil {
|
|
writeJSONError(w, http.StatusInternalServerError, "Failed to get topic details: "+err.Error())
|
|
return
|
|
}
|
|
|
|
// Return JSON data
|
|
writeJSON(w, http.StatusOK, topicDetailsData)
|
|
}
|
|
|
|
// CreateTopicAPI creates a new topic with retention configuration
|
|
func (h *MessageQueueHandlers) CreateTopicAPI(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Namespace string `json:"namespace" binding:"required"`
|
|
Name string `json:"name" binding:"required"`
|
|
PartitionCount int32 `json:"partition_count" binding:"required"`
|
|
Retention struct {
|
|
Enabled bool `json:"enabled"`
|
|
RetentionSeconds int64 `json:"retention_seconds"`
|
|
} `json:"retention"`
|
|
}
|
|
|
|
if err := decodeJSONBody(newJSONMaxReader(w, r), &req); err != nil {
|
|
writeJSONError(w, http.StatusBadRequest, "Invalid request: "+err.Error())
|
|
return
|
|
}
|
|
|
|
// Validate inputs
|
|
if req.PartitionCount < 1 || req.PartitionCount > 100 {
|
|
writeJSONError(w, http.StatusBadRequest, "Partition count must be between 1 and 100")
|
|
return
|
|
}
|
|
|
|
if req.Retention.Enabled && req.Retention.RetentionSeconds <= 0 {
|
|
writeJSONError(w, http.StatusBadRequest, "Retention seconds must be positive when retention is enabled")
|
|
return
|
|
}
|
|
|
|
// Create the topic via admin server
|
|
err := h.adminServer.CreateTopicWithRetention(req.Namespace, req.Name, req.PartitionCount, req.Retention.Enabled, req.Retention.RetentionSeconds)
|
|
if err != nil {
|
|
writeJSONError(w, http.StatusInternalServerError, "Failed to create topic: "+err.Error())
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]interface{}{
|
|
"message": "Topic created successfully",
|
|
"topic": fmt.Sprintf("%s.%s", req.Namespace, req.Name),
|
|
})
|
|
}
|
|
|
|
type UpdateTopicRetentionRequest struct {
|
|
Namespace string `json:"namespace"`
|
|
Name string `json:"name"`
|
|
Retention struct {
|
|
Enabled bool `json:"enabled"`
|
|
RetentionSeconds int64 `json:"retention_seconds"`
|
|
} `json:"retention"`
|
|
}
|
|
|
|
func (h *MessageQueueHandlers) UpdateTopicRetentionAPI(w http.ResponseWriter, r *http.Request) {
|
|
var request UpdateTopicRetentionRequest
|
|
if err := decodeJSONBody(newJSONMaxReader(w, r), &request); err != nil {
|
|
writeJSONError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
// Validate required fields
|
|
if request.Namespace == "" || request.Name == "" {
|
|
writeJSONError(w, http.StatusBadRequest, "namespace and name are required")
|
|
return
|
|
}
|
|
|
|
// Update the topic retention
|
|
err := h.adminServer.UpdateTopicRetention(request.Namespace, request.Name, request.Retention.Enabled, request.Retention.RetentionSeconds)
|
|
if err != nil {
|
|
writeJSONError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]interface{}{
|
|
"message": "Topic retention updated successfully",
|
|
"topic": request.Namespace + "." + request.Name,
|
|
})
|
|
}
|