/
columns.go
227 lines (214 loc) · 5.42 KB
/
columns.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"fmt"
"time"
lib "github.com/cncf/devstatscode"
yaml "gopkg.in/yaml.v2"
)
// columns contains list of columns that must be present on a certain series
type columns struct {
Columns []column `yaml:"columns"`
}
// column contain configuration of columns needed on a specific series
type column struct {
TableRegexp string `yaml:"table_regexp"`
Tag string `yaml:"tag"`
Column string `yaml:"column"`
}
// Ensure that specific TSDB series have all needed columns
func ensureColumns() {
// Environment context parse
var ctx lib.Ctx
ctx.Init()
// If skip TSDB or only ES output - nothing to do
if ctx.SkipTSDB || ctx.UseESOnly {
return
}
// Connect to Postgres DB
con := lib.PgConn(&ctx)
defer func() { lib.FatalOnError(con.Close()) }()
// Local or cron mode?
dataPrefix := ctx.DataDir
if ctx.Local {
dataPrefix = "./"
}
// Read columns config
data, err := lib.ReadFile(&ctx, dataPrefix+ctx.ColumnsYaml)
if err != nil {
lib.FatalOnError(err)
return
}
var allColumns columns
lib.FatalOnError(yaml.Unmarshal(data, &allColumns))
// Per project directory for SQL files
dir := lib.Metrics
if ctx.Project != "" {
dir += ctx.Project + "/"
}
thrN := lib.GetThreadsNum(&ctx)
ch := make(chan [2][]string)
nThreads := 0
allTables := []string{}
allCols := []string{}
// Use integer index to pass to go rountine
for i := range allColumns.Columns {
go func(ch chan [2][]string, idx int) {
tables := []string{}
cols := []string{}
// Refer to current column config using index passed to anonymous function
col := &allColumns.Columns[idx]
if ctx.Debug > 0 {
lib.Printf("Ensure column config: %+v\n", col)
}
crows := lib.QuerySQLWithErr(
con,
&ctx,
fmt.Sprintf(
"select \"%s\" from \"%s\"",
col.Column,
col.Tag,
),
)
defer func() { lib.FatalOnError(crows.Close()) }()
colName := ""
colNames := []string{}
for crows.Next() {
lib.FatalOnError(crows.Scan(&colName))
colNames = append(colNames, colName)
}
lib.FatalOnError(crows.Err())
if len(colNames) == 0 {
lib.Printf("Warning: no tag values for (%s, %s)\n", col.Column, col.Tag)
if ch != nil {
ch <- [2][]string{tables, cols}
}
return
}
if ctx.Debug > 0 {
lib.Printf("Ensure columns: %+v --> %+v\n", col, colNames)
}
rows := lib.QuerySQLWithErr(
con,
&ctx,
fmt.Sprintf(
"select tablename from pg_catalog.pg_tables where "+
"schemaname = 'public' and substring(tablename from %s) is not null",
lib.NValue(1),
),
col.TableRegexp,
)
defer func() { lib.FatalOnError(rows.Close()) }()
table := ""
numTables := 0
for rows.Next() {
lib.FatalOnError(rows.Scan(&table))
for _, colName := range colNames {
_, err := lib.ExecSQL(
con,
&ctx,
"alter table \""+table+"\" add column \""+colName+"\" double precision",
)
if err == nil {
lib.Printf("Added column \"%s\" to \"%s\" table\n", colName, table)
tables = append(tables, table)
cols = append(cols, colName)
//} else {
// lib.Printf("%+v\n", err)
}
}
numTables++
}
lib.FatalOnError(rows.Err())
if numTables == 0 {
lib.Printf("Warning: '%+v': no table hits\n", col)
}
// Synchronize go routine
if ch != nil {
ch <- [2][]string{tables, cols}
}
}(ch, i)
// go routine called with 'ch' channel to sync and column config index
nThreads++
if nThreads == thrN {
data := <-ch
tables := data[0]
cols := data[1]
for i, table := range tables {
col := cols[i]
allTables = append(allTables, table)
allCols = append(allCols, col)
}
nThreads--
}
}
// Usually all work happens on '<-ch'
for nThreads > 0 {
data := <-ch
tables := data[0]
cols := data[1]
for i, table := range tables {
col := cols[i]
allTables = append(allTables, table)
allCols = append(allCols, col)
}
nThreads--
}
//lib.Printf("Tables: %+v\n", allTables)
//lib.Printf("Columns: %+v\n", allCols)
cfg := make(map[string]map[string]struct{})
for i, table := range allTables {
col := allCols[i]
_, ok := cfg[table]
if !ok {
cfg[table] = make(map[string]struct{})
}
cfg[table][col] = struct{}{}
}
if ctx.Debug > 0 {
lib.Printf("Cfg: %+v\n", cfg)
}
// process separate tables in parallel
sch := make(chan [2]string)
nThreads = 0
for table, columns := range cfg {
go func(sch chan [2]string, tab string, cols map[string]struct{}) {
s := "update \"" + tab + "\" set "
for col := range cols {
s += "\"" + col + "\" = 0.0, "
}
s = s[:len(s)-2]
dtStart := time.Now()
lib.ExecSQLWithErr(con, &ctx, s)
dtEnd := time.Now()
nCols := len(cols)
lib.Printf("Mass updated \"%s\", columns: %d, took: %v\n", tab, nCols, dtEnd.Sub(dtStart))
s = "alter table \"" + tab + "\" "
for col := range cols {
s += "alter column \"" + col + "\" set not null, alter column \"" + col + "\" set default 0.0, "
}
s = s[:len(s)-2]
dtStart = time.Now()
lib.ExecSQLWithErr(con, &ctx, s)
dtEnd = time.Now()
lib.Printf("Altered \"%s\" defaults and restrictions, columns: %d, took: %v\n", tab, nCols, dtEnd.Sub(dtStart))
if sch != nil {
sch <- [2]string{tab, "ok"}
}
}(sch, table, columns)
nThreads++
if nThreads == thrN {
<-sch
nThreads--
}
}
for nThreads > 0 {
<-sch
nThreads--
}
}
func main() {
dtStart := time.Now()
ensureColumns()
dtEnd := time.Now()
lib.Printf("Time: %v\n", dtEnd.Sub(dtStart))
}