-
Notifications
You must be signed in to change notification settings - Fork 316
/
envValidator.go
292 lines (257 loc) · 8.97 KB
/
envValidator.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package validators
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"strconv"
"time"
"github.com/rudderlabs/rudder-server/config"
migrator "github.com/rudderlabs/rudder-server/services/sql-migrator"
"github.com/rudderlabs/rudder-server/utils/logger"
"github.com/rudderlabs/rudder-server/utils/misc"
)
const (
// This is integer representation of Postgres version.
// For ex, integer representation of version 9.6.3 is 90603
// Minimum postgres version needed for rudder server is 10
minPostgresVersion = 100000
)
var pkgLogger logger.Logger
func init() {
pkgLogger = logger.NewLogger().Child("validators").Child("envValidator")
}
func createWorkspaceTable(dbHandle *sql.DB) error {
// Create table to store workspace token
sqlStatement := `CREATE TABLE IF NOT EXISTS workspace (
token TEXT PRIMARY KEY,
created_at TIMESTAMP NOT NULL,
parameters JSONB);`
_, err := dbHandle.Exec(sqlStatement)
if err != nil {
return fmt.Errorf("error creating workspace table: %w", err)
}
return nil
}
func insertTokenIfNotExists(dbHandle *sql.DB) error {
// Read entries, if there are no entries insert hashed current workspace token
var totalCount int
sqlStatement := `SELECT COUNT(*) FROM workspace`
row := dbHandle.QueryRow(sqlStatement)
err := row.Scan(&totalCount)
if err != nil {
return fmt.Errorf("error reading workspace table: %w", err)
}
if totalCount > 0 {
return nil
}
// There are no entries in the table, hash current workspace token and insert
if _, err := dbHandle.Exec(`INSERT INTO workspace (token, created_at)
VALUES ($1, $2)`,
misc.GetMD5Hash(config.GetWorkspaceToken()),
time.Now()); err != nil {
return fmt.Errorf("error inserting workspace token: %w", err)
}
return nil
}
func setWHSchemaVersionIfNotExists(dbHandle *sql.DB) error {
hashedToken := misc.GetMD5Hash(config.GetWorkspaceToken())
whSchemaVersion := config.GetString("Warehouse.schemaVersion", "v1")
var parameters sql.NullString
sqlStatement := fmt.Sprintf(`SELECT parameters FROM workspace WHERE token = '%s'`, hashedToken)
row := dbHandle.QueryRow(sqlStatement)
err := row.Scan(¶meters)
if err == sql.ErrNoRows {
return nil
}
if err != nil {
return fmt.Errorf("error reading workspace table: %w", err)
}
if !parameters.Valid {
// insert current version
sqlStatement = fmt.Sprintf(`UPDATE workspace SET parameters = '{"wh_schema_version":%q}' WHERE token = '%s'`, whSchemaVersion, hashedToken)
_, err := dbHandle.Exec(sqlStatement)
if err != nil {
return fmt.Errorf("error updating workspace table: %w", err)
}
} else {
var parametersMap map[string]interface{}
err = json.Unmarshal([]byte(parameters.String), ¶metersMap)
if err != nil {
return fmt.Errorf("error unmarshalling parameters: %w", err)
}
if version, ok := parametersMap["wh_schema_version"]; ok {
whSchemaVersion = version.(string)
config.Set("Warehouse.schemaVersion", whSchemaVersion)
return nil
}
parametersMap["wh_schema_version"] = whSchemaVersion
marshalledParameters, err := json.Marshal(parametersMap)
if err != nil {
return fmt.Errorf("error marshalling parameters: %w", err)
}
sqlStatement = fmt.Sprintf(`UPDATE workspace SET parameters = '%s' WHERE token = '%s'`, marshalledParameters, hashedToken)
_, err = dbHandle.Exec(sqlStatement)
if err != nil {
return fmt.Errorf("error updating workspace table: %w", err)
}
}
return nil
}
func getWorkspaceFromDB(dbHandle *sql.DB) (string, error) {
sqlStatement := `SELECT token FROM workspace order by created_at desc limit 1`
var token string
row := dbHandle.QueryRow(sqlStatement)
if err := row.Scan(&token); err != nil {
return token, fmt.Errorf("error reading workspace table: %w", err)
}
return token, nil
}
func createDBConnection() (*sql.DB, error) {
psqlInfo := misc.GetConnectionString()
var err error
dbHandle, err := sql.Open("postgres", psqlInfo)
if err != nil {
return nil, fmt.Errorf("error opening db connection: %w", err)
}
err = dbHandle.Ping()
if err != nil {
return nil, fmt.Errorf("error pinging db: %w", err)
}
return dbHandle, nil
}
func closeDBConnection(handle *sql.DB) error {
err := handle.Close()
if err != nil {
return fmt.Errorf("error closing db connection: %w", err)
}
return nil
}
func killDanglingDBConnections(db *sql.DB) error {
rows, err := db.Query(`SELECT PID, QUERY_START, COALESCE(WAIT_EVENT_TYPE,''), COALESCE(WAIT_EVENT, ''), COALESCE(STATE, ''), QUERY, PG_TERMINATE_BACKEND(PID)
FROM PG_STAT_ACTIVITY
WHERE PID <> PG_BACKEND_PID()
AND APPLICATION_NAME = CURRENT_SETTING('APPLICATION_NAME')
AND APPLICATION_NAME <> ''`)
if err != nil {
return fmt.Errorf("error occurred when querying pg_stat_activity table for terminating dangling connections: %w", err)
}
defer func() { _ = rows.Close() }()
type danglingConnRow struct {
pid int
queryStart *string
waitEventType string
waitEvent string
state string
query string
terminated bool
}
dangling := make([]*danglingConnRow, 0)
for rows.Next() {
var row danglingConnRow
err := rows.Scan(&row.pid, &row.queryStart, &row.waitEventType, &row.waitEvent, &row.state, &row.query, &row.terminated)
if err != nil {
return fmt.Errorf("error occurred when scanning pg_stat_activity table for terminating dangling connections: %w", err)
}
dangling = append(dangling, &row)
}
if len(dangling) > 0 {
pkgLogger.Warnf("Terminated %d dangling connection(s)", len(dangling))
for i, rowPtr := range dangling {
pkgLogger.Warnf("dangling connection #%d: %+v", i+1, *rowPtr)
}
}
return nil
}
// IsPostgresCompatible checks the if the version of postgres is greater than minPostgresVersion
func IsPostgresCompatible(ctx context.Context, db *sql.DB) (bool, error) {
var versionNum int
err := db.QueryRowContext(ctx, "SHOW server_version_num;").Scan(&versionNum)
if err != nil {
return false, err
}
return versionNum >= minPostgresVersion, nil
}
// ValidateEnv validates the current environment available for the server
func ValidateEnv() error {
dbHandle, err := createDBConnection()
if err != nil {
return err
}
defer func() { _ = closeDBConnection(dbHandle) }()
isDBCompatible, err := IsPostgresCompatible(context.TODO(), dbHandle)
if err != nil {
return err
}
if !isDBCompatible {
return errors.New("rudder server needs postgres version >= 10")
}
// SQL statements in rudder-server are not executed with a timeout context, instead we are letting them take as much time as they need :)
// Due to the above, when a server shutdown is initiated in a cloud environment while long-running statements are being executed,
// the server process will not manage to shutdown gracefully, since it will be blocked by the SQL statements.
// The container orchestrator will eventually kill the server process, leaving one or more dangling connections in the database.
// This will ensure that before a new rudder-server instance starts working, all previous dangling connections belonging to this server are being killed.
return killDanglingDBConnections(dbHandle)
}
// InitializeEnv initializes the environment for the server
func InitializeNodeMigrations() error {
dbHandle, err := createDBConnection()
if err != nil {
return err
}
defer func() { _ = closeDBConnection(dbHandle) }()
m := &migrator.Migrator{
Handle: dbHandle,
MigrationsTable: "node_migrations",
ShouldForceSetLowerVersion: config.GetBool("SQLMigrator.forceSetLowerVersion", true),
}
if err := m.Migrate("node"); err != nil {
return fmt.Errorf("could not run node schema migrations: %w", err)
}
return nil
}
func CheckAndValidateWorkspaceToken() error {
dbHandle, err := createDBConnection()
if err != nil {
return err
}
defer func() { _ = closeDBConnection(dbHandle) }()
if err := createWorkspaceTable(dbHandle); err != nil {
return err
}
if err := insertTokenIfNotExists(dbHandle); err != nil {
return err
}
if err := setWHSchemaVersionIfNotExists(dbHandle); err != nil {
return err
}
workspaceTokenHashInDB, err := getWorkspaceFromDB(dbHandle)
if err != nil {
return err
}
if workspaceTokenHashInDB == misc.GetMD5Hash(config.GetWorkspaceToken()) {
return nil
}
// db connection should be closed. Else alter db fails.
// A new connection will be created again below, which will be closed on returning of this function (due to defer statement).
_ = closeDBConnection(dbHandle)
pkgLogger.Warn("Previous workspace token is not same as the current workspace token. Parking current jobsdb aside and creating a new one")
dbName := config.GetString("DB.name", "ubuntu")
misc.ReplaceDB(dbName, dbName+"_"+strconv.FormatInt(time.Now().Unix(), 10)+"_"+workspaceTokenHashInDB)
dbHandle, err = createDBConnection()
if err != nil {
return err
}
// create workspace table and insert hashed token
if err := createWorkspaceTable(dbHandle); err != nil {
return err
}
if err := insertTokenIfNotExists(dbHandle); err != nil {
return err
}
if err := setWHSchemaVersionIfNotExists(dbHandle); err != nil {
return err
}
return nil
}