124 lines
5.8 KiB
Go
124 lines
5.8 KiB
Go
/*
|
||
* Copyright 2026 Safronov Grigorii
|
||
*
|
||
* Licensed under the CDDL, Version 1.0 (the "License");
|
||
* you may not use this file except in compliance with the License.
|
||
*
|
||
* You may obtain a copy of the License at
|
||
* https://opensource.org/licenses/CDDL-1.0
|
||
*/
|
||
|
||
// Файл: internal/cluster/types.go
|
||
// Назначение: Общие типы данных для кластерных операций с поддержкой временных меток
|
||
|
||
package cluster
|
||
|
||
import "time"
|
||
|
||
// NodeInfo представляет информацию об узле для координатора
|
||
type NodeInfo struct {
|
||
ID string `json:"id"`
|
||
IP string `json:"ip"`
|
||
Port int `json:"port"`
|
||
Status string `json:"status"`
|
||
LastSeen int64 `json:"last_seen"`
|
||
JoinedAt int64 `json:"joined_at"` // Время присоединения узла к кластеру
|
||
UpdatedAt int64 `json:"updated_at"` // Время последнего обновления статуса
|
||
Version uint64 `json:"version"` // Версия информации об узле
|
||
}
|
||
|
||
// ClusterStatus представляет статус кластера
|
||
type ClusterStatus struct {
|
||
Name string `json:"name"`
|
||
TotalNodes int `json:"total_nodes"`
|
||
ActiveNodes int `json:"active_nodes"`
|
||
SyncingNodes int `json:"syncing_nodes"`
|
||
FailedNodes int `json:"failed_nodes"`
|
||
ReplicationFactor int `json:"replication_factor"`
|
||
LeaderID string `json:"leader_id"`
|
||
Health string `json:"health"`
|
||
CreatedAt int64 `json:"created_at"` // Время создания кластера
|
||
UpdatedAt int64 `json:"updated_at"` // Время последнего обновления статуса
|
||
}
|
||
|
||
// ClusterHealth представляет информацию о здоровье кластера
|
||
type ClusterHealth struct {
|
||
Nodes map[string]*NodeHealth `json:"nodes"`
|
||
OverallScore float64 `json:"overall_score"`
|
||
Recommendations string `json:"recommendations"`
|
||
CheckedAt int64 `json:"checked_at"` // Время проверки здоровья
|
||
}
|
||
|
||
// NodeHealth представляет здоровье отдельного узла
|
||
type NodeHealth struct {
|
||
Status string `json:"status"`
|
||
LatencyMs int64 `json:"latency_ms"`
|
||
LastCheck int64 `json:"last_check"`
|
||
LastSuccess int64 `json:"last_success"` // Время последнего успешного heartbeat
|
||
LastFailure int64 `json:"last_failure"` // Время последней ошибки
|
||
FailureCount int `json:"failure_count"` // Счётчик ошибок
|
||
}
|
||
|
||
// NodeRequest представляет запрос от одного узла к другому
|
||
type NodeRequest struct {
|
||
Type string `json:"type"` // replicate, query, sync, heartbeat
|
||
Data []byte `json:"data"` // Данные запроса
|
||
FromNode string `json:"from_node"` // ID узла-отправителя
|
||
RequestID string `json:"request_id"` // Уникальный ID запроса
|
||
Timestamp int64 `json:"timestamp"` // Время отправки запроса
|
||
}
|
||
|
||
// ShardInfo представляет информацию о шарде
|
||
type ShardInfo struct {
|
||
ID string `json:"id"`
|
||
Name string `json:"name"`
|
||
Nodes []string `json:"nodes"` // ID узлов, содержащих шард
|
||
LeaderNode string `json:"leader_node"` // Лидер шарда
|
||
Status string `json:"status"` // active, syncing, offline, rebalancing
|
||
CreatedAt int64 `json:"created_at"` // Время создания шарда
|
||
UpdatedAt int64 `json:"updated_at"` // Время последнего обновления
|
||
LastRebalanced int64 `json:"last_rebalanced"` // Время последней перебалансировки
|
||
DocumentCount int64 `json:"document_count"` // Количество документов в шарде
|
||
SizeBytes int64 `json:"size_bytes"` // Размер шарда в байтах
|
||
}
|
||
|
||
// ReplicationLogEntry представляет запись в журнале репликации
|
||
type ReplicationLogEntry struct {
|
||
ID string `json:"id"`
|
||
Timestamp int64 `json:"timestamp"`
|
||
SourceNode string `json:"source_node"`
|
||
TargetNode string `json:"target_node"`
|
||
Operation string `json:"operation"` // replicate, sync, snapshot
|
||
Database string `json:"database"`
|
||
Collection string `json:"collection"`
|
||
DocumentID string `json:"document_id"`
|
||
Status string `json:"status"` // pending, success, failed
|
||
DurationMs int64 `json:"duration_ms"`
|
||
Error string `json:"error,omitempty"`
|
||
Details map[string]interface{} `json:"details,omitempty"`
|
||
}
|
||
|
||
// NodeJoinedAt возвращает человекочитаемое время присоединения узла
|
||
func (n *NodeInfo) NodeJoinedAt() string {
|
||
if n.JoinedAt == 0 {
|
||
return "not joined"
|
||
}
|
||
return time.UnixMilli(n.JoinedAt).Format("2006-01-02 15:04:05.000")
|
||
}
|
||
|
||
// LastSeenAt возвращает человекочитаемое время последнего контакта
|
||
func (n *NodeInfo) LastSeenAt() string {
|
||
if n.LastSeen == 0 {
|
||
return "never"
|
||
}
|
||
return time.UnixMilli(n.LastSeen).Format("2006-01-02 15:04:05.000")
|
||
}
|
||
|
||
// GetUptime возвращает время жизни узла в кластере
|
||
func (n *NodeInfo) GetUptime() time.Duration {
|
||
if n.JoinedAt == 0 {
|
||
return 0
|
||
}
|
||
return time.Duration(time.Now().UnixMilli() - n.JoinedAt) * time.Millisecond
|
||
}
|