-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathusers.go
160 lines (150 loc) · 5.75 KB
/
users.go
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
package ckconfig
import (
"github.com/housepower/ckman/common"
"github.com/housepower/ckman/model"
"github.com/imdario/mergo"
)
type HostInfo struct {
MemoryTotal int
}
func users(conf *model.CKManClickHouseConfig) map[string]interface{} {
output := make(map[string]interface{})
userConf := make(map[string]interface{})
//default
defaultUser := make(map[string]interface{})
defaultUser["password"] = conf.Password
defaultUser["profile"] = model.ClickHouseUserProfileDefault
defaultUser["quota"] = model.ClickHouseUserQuotaDefault
defaultUser["access_management"] = 1
defaultNetworks := make(map[string]interface{})
defaultNetworks["ip"] = model.ClickHouseUserNetIpDefault
defaultUser["networks"] = defaultNetworks
userConf["default"] = defaultUser
//normal users
if len(conf.UsersConf.Users) > 0 {
for _, normalUser := range conf.UsersConf.Users {
normal := make(map[string]interface{})
normal[common.CkPasswdLabel(normalUser.EncryptType)] = common.CkPassword(normalUser.Password, normalUser.EncryptType)
normal["profile"] = common.GetStringwithDefault(normalUser.Profile, model.ClickHouseUserProfileDefault)
normal["quota"] = common.GetStringwithDefault(normalUser.Quota, model.ClickHouseUserQuotaDefault)
networks := make(map[string]interface{})
if len(normalUser.Networks.IPs) > 0 {
var ips []string
for _, ip := range normalUser.Networks.IPs {
ips = append(ips, common.GetStringwithDefault(ip, model.ClickHouseUserNetIpDefault))
}
networks["ip"] = ips
} else {
networks["ip"] = model.ClickHouseUserNetIpDefault
}
if len(normalUser.Networks.Hosts) > 0 {
networks["host"] = normalUser.Networks.Hosts
}
if len(normalUser.Networks.HostRegexps) > 0 {
networks["host_regexp"] = normalUser.Networks.HostRegexps
}
normal["networks"] = networks
database := make(map[string]interface{})
if len(normalUser.DbRowPolices) > 0 {
for _, rowsdb := range normalUser.DbRowPolices {
rowpolicies := make(map[string]interface{})
for _, policy := range rowsdb.TblRowPolicies {
rowpolicies[policy.Table] = map[string]interface{}{
"filter": policy.Filter,
}
}
database[rowsdb.Database] = rowpolicies
}
normal["databases"] = database
}
userConf[normalUser.Name] = normal
}
}
output["users"] = userConf
return output
}
func profiles(userProfiles []model.Profile, info HostInfo) map[string]interface{} {
output := make(map[string]interface{})
profileMap := make(map[string]interface{})
//default
defaultProfile := make(map[string]interface{})
defaultProfile["max_memory_usage"] = int64((info.MemoryTotal / 2) * 1e3)
defaultProfile["max_memory_usage_for_all_queries"] = int64(((info.MemoryTotal * 3) / 4) * 1e3)
defaultProfile["max_bytes_before_external_group_by"] = int64((info.MemoryTotal / 4) * 1e3)
defaultProfile["max_query_size"] = 1073741824
defaultProfile["distributed_aggregation_memory_efficient"] = 1
defaultProfile["joined_subquery_requires_alias"] = 0
defaultProfile["distributed_ddl_task_timeout"] = 60
defaultProfile["allow_drop_detached"] = 1
defaultProfile["use_uncompressed_cache"] = 0
defaultProfile["max_execution_time"] = 3600 // 1 hour
defaultProfile["max_partitions_per_insert_block"] = 500
profileMap["default"] = defaultProfile
//normal
if len(userProfiles) > 0 {
for _, prof := range userProfiles {
normalProfile := make(map[string]interface{})
normalProfile["readonly"] = prof.ReadOnly
normalProfile["allow_ddl"] = prof.AllowDDL
normalProfile["max_threads"] = prof.MaxThreads
if prof.MaxMemoryUsage > int64((info.MemoryTotal/2)*1e3) {
prof.MaxMemoryUsage = int64((info.MemoryTotal / 2) * 1e3)
}
normalProfile["max_memory_usage"] = prof.MaxMemoryUsage
if prof.MaxMemoryUsageForAllQueries > int64(((info.MemoryTotal*3)/4)*1e3) {
prof.MaxMemoryUsageForAllQueries = int64(((info.MemoryTotal * 3) / 4) * 1e3)
}
normalProfile["max_memory_usage_for_all_queries"] = prof.MaxMemoryUsageForAllQueries
normalProfile["max_execution_time"] = prof.MaxExecutionTime
mergo.Merge(&normalProfile, expert(prof.Expert))
profileMap[prof.Name] = normalProfile
}
}
output["profiles"] = profileMap
return output
}
func quotas(userQuotas []model.Quota) map[string]interface{} {
output := make(map[string]interface{})
if len(userQuotas) > 0 {
quotasMap := make(map[string]interface{})
for _, quota := range userQuotas {
quotaInterval := make(map[string]interface{})
var intervals []map[string]interface{}
for _, interval := range quota.Intervals {
intervalMap := make(map[string]interface{})
intervalMap["duration"] = interval.Duration
intervalMap["queries"] = interval.Queries
intervalMap["query_selects"] = interval.QuerySelects
intervalMap["query_inserts"] = interval.QueryInserts
intervalMap["errors"] = interval.Errors
intervalMap["result_rows"] = interval.ResultRows
intervalMap["read_rows"] = interval.ReadRows
intervalMap["execution_time"] = interval.ExecutionTime
intervals = append(intervals, intervalMap)
}
quotaInterval["interval"] = intervals
quotasMap[quota.Name] = quotaInterval
}
output["quotas"] = quotasMap
}
return output
}
func GenerateUsersXML(filename string, conf *model.CKManClickHouseConfig, info HostInfo) (string, error) {
rootTag := "yandex"
if common.CompareClickHouseVersion(conf.Version, "22.x") >= 0 {
rootTag = "clickhouse"
}
userconf := make(map[string]interface{})
mergo.Merge(&userconf, expert(conf.UsersConf.Expert))
mergo.Merge(&userconf, users(conf))
mergo.Merge(&userconf, profiles(conf.UsersConf.Profiles, info))
mergo.Merge(&userconf, quotas(conf.UsersConf.Quotas))
xml := common.NewXmlFile(filename)
xml.Begin(rootTag)
xml.Merge(userconf)
xml.End(rootTag)
if err := xml.Dump(); err != nil {
return filename, err
}
return filename, nil
}