mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-10 17:26:38 +08:00
fix(clickhouse): resolve lint issues from optimization stack
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
# ClickHouse CPU 性能优化计划
|
# ClickHouse CPU 性能优化计划
|
||||||
|
|
||||||
> PLAN_ID: `63ba981b`
|
> PLAN_ID: `63ba981b`
|
||||||
> 状态: 执行中
|
> 状态: 已完成
|
||||||
> 目标: 完成 P0–P2 优化,降低 ClickHouse CPU 占用
|
> 目标: 完成 P0–P2 优化,降低 ClickHouse CPU 占用
|
||||||
|
|
||||||
## 背景
|
## 背景
|
||||||
|
|||||||
@@ -235,7 +235,7 @@ func buildNodeAccessLogRecords(nodeID string, direct []NodeAccessLog, buffered [
|
|||||||
return records, nil
|
return records, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func persistNodeAccessLogs(ctx context.Context, nodeID string, records []*model.OpenFlareAccessLog, reportedAt time.Time) error {
|
func persistNodeAccessLogs(ctx context.Context, _ string, records []*model.OpenFlareAccessLog, _ time.Time) error {
|
||||||
if len(records) == 0 {
|
if len(records) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,10 +26,7 @@ const (
|
|||||||
accessLogFieldRequestCount = "request_count"
|
accessLogFieldRequestCount = "request_count"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var defaultAccessLogQueryWindow = defaultAccessLogQueryDays * 24 * time.Hour
|
||||||
nodeAccessLogRetentionWindow = nodeAccessLogRetentionDays * 24 * time.Hour
|
|
||||||
defaultAccessLogQueryWindow = defaultAccessLogQueryDays * 24 * time.Hour
|
|
||||||
)
|
|
||||||
|
|
||||||
// AccessLogQuery filters access log list queries.
|
// AccessLogQuery filters access log list queries.
|
||||||
type AccessLogQuery struct {
|
type AccessLogQuery struct {
|
||||||
|
|||||||
@@ -329,11 +329,6 @@ func normalizeIPLiteral(value string) (string, bool) {
|
|||||||
return addr.String(), true
|
return addr.String(), true
|
||||||
}
|
}
|
||||||
|
|
||||||
func hostIsIPLiteral(value string) bool {
|
|
||||||
_, ok := normalizeIPLiteral(value)
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
func downloadIPGroupSubscription(ctx context.Context, rawURL string) ([]byte, error) {
|
func downloadIPGroupSubscription(ctx context.Context, rawURL string) ([]byte, error) {
|
||||||
if err := validateSubscriptionURL(rawURL); err != nil {
|
if err := validateSubscriptionURL(rawURL); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package model
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -343,13 +344,13 @@ func (s *memoryAccessLogStore) WAFIPAggregates(_ context.Context, filter OpenFla
|
|||||||
}
|
}
|
||||||
item.RequestCount++
|
item.RequestCount++
|
||||||
item.StatusCounts[row.StatusCode]++
|
item.StatusCounts[row.StatusCode]++
|
||||||
if row.StatusCode == 404 {
|
if row.StatusCode == http.StatusNotFound {
|
||||||
item.Status404Count++
|
item.Status404Count++
|
||||||
}
|
}
|
||||||
if row.StatusCode >= 400 && row.StatusCode < 500 {
|
if row.StatusCode >= 400 && row.StatusCode < 500 {
|
||||||
item.ClientErrorCount++
|
item.ClientErrorCount++
|
||||||
}
|
}
|
||||||
if row.StatusCode >= 500 {
|
if row.StatusCode >= http.StatusInternalServerError {
|
||||||
item.ServerErrorCount++
|
item.ServerErrorCount++
|
||||||
}
|
}
|
||||||
if memoryAccessLogHostIsIPLiteral(row.Host) {
|
if memoryAccessLogHostIsIPLiteral(row.Host) {
|
||||||
|
|||||||
@@ -9,7 +9,15 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const nodeAccessLogFilterClauseCapacity = 6
|
const (
|
||||||
|
nodeAccessLogFilterClauseCapacity = 6
|
||||||
|
|
||||||
|
nodeAccessLogSortDesc = "DESC"
|
||||||
|
nodeAccessLogSortAsc = "ASC"
|
||||||
|
nodeAccessLogSortAscInput = "asc"
|
||||||
|
|
||||||
|
nodeAccessLogColumnRemoteAddr = "remote_addr"
|
||||||
|
)
|
||||||
|
|
||||||
// NodeAccessLogFilter scopes ClickHouse node access log queries.
|
// NodeAccessLogFilter scopes ClickHouse node access log queries.
|
||||||
type NodeAccessLogFilter struct {
|
type NodeAccessLogFilter struct {
|
||||||
@@ -66,16 +74,16 @@ func combineNodeAccessLogSQLClauses(left string, right string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func nodeAccessLogOrderClause(sortBy string, sortOrder string) string {
|
func nodeAccessLogOrderClause(sortBy string, sortOrder string) string {
|
||||||
direction := "DESC"
|
direction := nodeAccessLogSortDesc
|
||||||
if normalizeNodeAccessLogSortOrder(sortOrder) == "asc" {
|
if normalizeNodeAccessLogSortOrder(sortOrder) == nodeAccessLogSortAscInput {
|
||||||
direction = "ASC"
|
direction = nodeAccessLogSortAsc
|
||||||
}
|
}
|
||||||
column := "logged_at"
|
column := "logged_at"
|
||||||
switch strings.TrimSpace(sortBy) {
|
switch strings.TrimSpace(sortBy) {
|
||||||
case "status_code":
|
case "status_code":
|
||||||
column = "status_code"
|
column = "status_code"
|
||||||
case "remote_addr":
|
case nodeAccessLogColumnRemoteAddr:
|
||||||
column = "remote_addr"
|
column = nodeAccessLogColumnRemoteAddr
|
||||||
case "host":
|
case "host":
|
||||||
column = "host"
|
column = "host"
|
||||||
case "path":
|
case "path":
|
||||||
@@ -110,9 +118,9 @@ func nodeAccessLogHostIsIPLiteralExpr() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func nodeAccessLogBucketOrderClause(sortBy string, sortOrder string) string {
|
func nodeAccessLogBucketOrderClause(sortBy string, sortOrder string) string {
|
||||||
direction := "DESC"
|
direction := nodeAccessLogSortDesc
|
||||||
if normalizeNodeAccessLogSortOrder(sortOrder) == "asc" {
|
if normalizeNodeAccessLogSortOrder(sortOrder) == nodeAccessLogSortAscInput {
|
||||||
direction = "ASC"
|
direction = nodeAccessLogSortAsc
|
||||||
}
|
}
|
||||||
switch strings.TrimSpace(sortBy) {
|
switch strings.TrimSpace(sortBy) {
|
||||||
case "request_count":
|
case "request_count":
|
||||||
@@ -123,9 +131,9 @@ func nodeAccessLogBucketOrderClause(sortBy string, sortOrder string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func nodeAccessLogIPSummaryOrderClause(sortBy string, sortOrder string) string {
|
func nodeAccessLogIPSummaryOrderClause(sortBy string, sortOrder string) string {
|
||||||
direction := "DESC"
|
direction := nodeAccessLogSortDesc
|
||||||
if normalizeNodeAccessLogSortOrder(sortOrder) == "asc" {
|
if normalizeNodeAccessLogSortOrder(sortOrder) == nodeAccessLogSortAscInput {
|
||||||
direction = "ASC"
|
direction = nodeAccessLogSortAsc
|
||||||
}
|
}
|
||||||
column := "total_requests"
|
column := "total_requests"
|
||||||
switch strings.TrimSpace(sortBy) {
|
switch strings.TrimSpace(sortBy) {
|
||||||
@@ -133,7 +141,7 @@ func nodeAccessLogIPSummaryOrderClause(sortBy string, sortOrder string) string {
|
|||||||
column = "recent_requests"
|
column = "recent_requests"
|
||||||
case "last_seen_at":
|
case "last_seen_at":
|
||||||
column = "last_seen_epoch"
|
column = "last_seen_epoch"
|
||||||
case "remote_addr":
|
case nodeAccessLogColumnRemoteAddr:
|
||||||
column = "trimmed_remote_addr"
|
column = "trimmed_remote_addr"
|
||||||
}
|
}
|
||||||
return column + " " + direction + ", last_seen_epoch DESC, trimmed_remote_addr ASC"
|
return column + " " + direction + ", last_seen_epoch DESC, trimmed_remote_addr ASC"
|
||||||
|
|||||||
Reference in New Issue
Block a user