This repository was archived by the owner on Jan 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpt.go
More file actions
552 lines (482 loc) · 16.4 KB
/
cpt.go
File metadata and controls
552 lines (482 loc) · 16.4 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// Copyright 2020 Winfried Klum
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bufio"
"bytes"
"compress/zlib"
"context"
"encoding/base64"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/jackc/pgx/v4"
)
var conn *pgx.Conn
var states = []string{"ENQUEUED", "PROCESSING", "WAITING", "FINISHED", "INVALID", "ERROR", "ALL"}
var validClassName = regexp.MustCompile(`^(?:\w+|\w+\.\w+)+$`)
var validLike = regexp.MustCompile(`^[^"'%]+$`)
const pgTimestampFormat = "2006-01-02 15:04:05.999999999"
func main() {
var err error
var args []string
pArgs := readPipe()
if len(os.Args) > 2 {
args = os.Args[2:]
}
args = append(args, pArgs...)
countCommand := flag.NewFlagSet("count", flag.ExitOnError)
countStatePtr := countCommand.String("state", "ERROR", "workflow instance state. Possible states:"+fmt.Sprint(states))
brokenCommand := flag.NewFlagSet("broken", flag.ExitOnError)
brokenPatternPtr := brokenCommand.String("exception-pattern", "", "filter search pattern for broken workflow instance exception message")
brokenStartTimePtr := brokenCommand.String("error-time-start", "", "filter on workflow error time, intervall start time in timestamp format, e.g. 2020-04-25 11:40:40.78")
brokenEndTimePtr := brokenCommand.String("error-time-end", "", "filter on workflow error time, intervall end time in timestamp format, e.g. 2020-04-26 11:40:40.78")
brokenCountTimerPtr := brokenCommand.Bool("print-count", false, "Print number of (filtered) broken workflow instances")
brokenClassPtr := brokenCommand.String("workflow-class", "", "filter on workflow instance class, full package name required, e.g. org.foo.wf.MyWorkflow")
dataCommand := flag.NewFlagSet("data", flag.ExitOnError)
dataSelectorPtr := dataCommand.String("json-selector", "", "json selector, e.g. json->1='test', (only available for data in JSON format)")
dataStatePtr := dataCommand.String("state", "ERROR", "workflow instance state. Possible states:"+fmt.Sprint(states))
showCommand := flag.NewFlagSet("show", flag.ExitOnError)
showDataPtr := showCommand.Bool("workflow-data", false, "show workflow instance data (only available for data in JSON format)")
showAuditPtr := showCommand.Bool("audit-trail", false, "show audit trail messages")
showInstancePtr := showCommand.Bool("instance-details", false, "show workflow instance details")
showDataArrPtr := showCommand.Bool("print-data-array", false, "print workflow data list as json array, only valid in combination with -workflow-data flag")
deleteCommand := flag.NewFlagSet("delete", flag.ExitOnError)
restartCommand := flag.NewFlagSet("restart", flag.ExitOnError)
cleanupCommand := flag.NewFlagSet("cleanup", flag.ExitOnError)
cleanupAgePtr := cleanupCommand.String("age", "", "filter on age. Format as timestamp, days(d) or hours(h), e.g. 2006-01-02 15:04:05.99, 35d, 24h")
cleanupAuditPtr := cleanupCommand.Bool("audit-trail", false, "delete audit trail data older than age")
cleanupInstancePtr := cleanupCommand.Bool("workflow-instance", false, "delete workflow instance data older than age")
if len(os.Args) < 2 {
printHelp()
os.Exit(1)
}
switch os.Args[1] {
case "count":
countCommand.Parse(args)
case "broken":
brokenCommand.Parse(args)
case "delete":
deleteCommand.Parse(args)
case "show":
showCommand.Parse(args)
case "restart":
restartCommand.Parse(args)
case "data":
dataCommand.Parse(args)
case "cleanup":
cleanupCommand.Parse(args)
default:
printHelp()
os.Exit(1)
}
conn, err = pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
exitOnErr("Unable to connect to database: %v\nMake sure that DATABASE_URL environment variable is set", err)
}
if countCommand.Parsed() {
count(*countStatePtr)
}
if brokenCommand.Parsed() {
broken(*brokenPatternPtr, *brokenStartTimePtr, *brokenEndTimePtr, *brokenCountTimerPtr, *brokenClassPtr)
}
if deleteCommand.Parsed() {
args := deleteCommand.Args()
delete(args)
}
if showCommand.Parsed() {
args := showCommand.Args()
show(*showDataPtr, args, *showAuditPtr, *showInstancePtr, *showDataArrPtr)
}
if restartCommand.Parsed() {
args := restartCommand.Args()
restart(args)
}
if dataCommand.Parsed() {
jsonData(dataSelectorPtr, *dataStatePtr)
}
if cleanupCommand.Parsed() {
cleanup(*cleanupAgePtr, *cleanupAuditPtr, *cleanupInstancePtr)
}
}
func printHelp() {
fmt.Println("Copper Postgres Tool\nUsage:\n",
"count\n",
"broken\n",
"show\n",
"delete\n",
"restart\n",
"data (only available for Json format)\n",
"cleanup\n",
"add -help after command to get more information")
}
func count(state string) {
var count int64
var err error
stateIdx := stateIndex(state)
if stateIdx < 0 {
exitOnErr("Invalid state: %v. Allowed states are: %v ", state, fmt.Sprint(states))
}
if states[stateIdx] == "ALL" {
err = conn.QueryRow(context.Background(), "select count(id) as WORKFLOW_COUNT from cop_workflow_instance").Scan(&count)
} else {
err = conn.QueryRow(context.Background(), "select count(id) as WORKFLOW_COUNT from cop_workflow_instance where state=$1;", stateIdx).Scan(&count)
}
if err != nil {
exitOnErr("Error reading count: %v", err)
}
fmt.Fprintf(os.Stdout, "%v\n", count)
}
func broken(pattern string, stime string, etime string, count bool, classname string) {
var sqlBuffer bytes.Buffer
if count {
sqlBuffer.WriteString("select count(workflow_instance_id)")
} else {
sqlBuffer.WriteString("select workflow_instance_id")
}
sqlBuffer.WriteString("from cop_workflow_instance_error as e, cop_workflow_instance as i where e.workflow_instance_id=i.id")
if len(pattern) > 0 {
if validLike.MatchString(pattern) {
sqlBuffer.WriteString(" and e.exception like '%" + pattern + "%'")
} else {
exitOnErr("Invalid exception-pattern: %v", pattern)
}
}
if len(classname) > 0 {
if validClassName.MatchString(classname) {
sqlBuffer.WriteString(" and i.classname='" + classname + "'")
} else {
exitOnErr("Invalid workflow-classname: %v", classname)
}
}
if len(stime) > 0 {
sqlBuffer.WriteString(" and e.error_ts >= '" + stime + "'")
}
if len(etime) > 0 {
sqlBuffer.WriteString(" and e.error_ts <= '" + etime + "'")
}
sqlBuffer.WriteString(";")
if count {
var ids int64
err := conn.QueryRow(context.Background(), sqlBuffer.String()).Scan(&ids)
if err != nil {
fmt.Fprintf(os.Stderr, "Workflow instance error count failed: %v\n", err)
return
}
fmt.Println(ids)
return
}
rows, err := conn.Query(context.Background(), sqlBuffer.String())
if err != nil {
exitOnErr("Workflow instance error search failed: %v\n", err)
}
for rows.Next() {
var id string
err1 := rows.Scan(&id)
if err1 != nil {
fmt.Fprintf(os.Stderr, "Error reading workflow instance id, error: %v", err1)
}
fmt.Println(id)
}
}
func delete(args []string) {
var err error
for _, id := range args {
_, err = conn.Exec(context.Background(), "select 1 from cop_workflow_instance where id=$1 for update", id)
if err != nil {
fmt.Fprintf(os.Stderr, "Error locking workflow instance id=%v, skipping...\n", id)
continue
}
_, err = conn.Exec(context.Background(), "delete from cop_response where correlation_id in (select correlation_id from cop_wait where workflow_instance_id=$1)", id)
if err != nil {
fmt.Fprintf(os.Stderr, "Error deleteing workflow instance from COP_RESPONSE id=%v\n", id)
err = nil
}
_, err = conn.Exec(context.Background(), "delete from cop_wait where workflow_instance_id=$1", id)
if err != nil {
fmt.Fprintf(os.Stderr, "Error deleting workflow instance from COP_WAIT id=%v\n", id)
err = nil
}
_, err = conn.Exec(context.Background(), "delete from cop_workflow_instance_error where workflow_instance_id=$1", id)
if err != nil {
fmt.Fprintf(os.Stderr, "Error deleting workflow instance from COP_WORKFLOW_INSTANCE_ERROR, id=%v\n", id)
err = nil
}
_, err = conn.Exec(context.Background(), "delete from cop_workflow_instance where id=$1", id)
if err != nil {
fmt.Fprintf(os.Stderr, "Error deleting workflow instance from COP_WORKFLOW_INSTANCE, id=%v\n", id)
}
}
}
func show(showData bool, args []string, audit bool, instance bool, asArray bool) {
var data, ppoolID, classname string
var state, priority, csWaitmode, minNumbOfResp, numbOfWaits int
var lastModTs, creationTs time.Time
var timeout *time.Time
if (!showData && !audit) && !instance {
exitOnErr("Use at least one of the following flags:[-workflow-data,-audit-trail,-instance-details]")
}
if (asArray && !showData) || (asArray && (audit || instance)) {
exitOnErr("Flag -print-data-array is only allowed together with -workflow-data flag")
}
if asArray {
instance = false
audit = false
fmt.Println("[")
}
for i, id := range args {
sql := "select data, state, priority, last_mod_ts, ppool_id,cs_waitmode, min_numb_of_resp, numb_of_waits, timeout, creation_ts, classname from cop_workflow_instance where id=$1"
err := conn.QueryRow(context.Background(), sql, id).Scan(&data, &state, &priority, &lastModTs, &ppoolID, &csWaitmode, &minNumbOfResp, &numbOfWaits, &timeout, &creationTs, &classname)
if err == nil {
if instance {
stateTxt, _ := indexState(state)
fmt.Println("Workflow Instance:")
fmt.Printf("id:%v, state:%v, priority:%v, creation time:%v, last modification:%v\n", id, stateTxt, priority, creationTs, lastModTs)
fmt.Printf("pool id:%v, wait mode:%v, number of waits:%v, class name:%v", ppoolID, csWaitmode, numbOfWaits, classname)
if timeout != nil {
fmt.Printf(", timeout:%v", *timeout)
}
fmt.Println("")
}
if showData && instance {
fmt.Println("Instance data:")
}
if showData {
fmt.Printf("%v\n", data)
}
}
if audit {
auditMsgs(id)
}
if i+1 < len(args) {
if asArray {
fmt.Println(",")
} else {
fmt.Println("--------------------------------------------------------------------------------------------------------------")
}
}
}
if asArray {
fmt.Println("]")
}
}
func auditMsgs(id string) {
first := true
sql := "select long_message,occurrence from cop_audit_trail_event where instance_id=$1 order by occurrence"
rows, err := conn.Query(context.Background(), sql, id)
if err != nil {
return
}
for rows.Next() {
if first {
fmt.Println("Audit Trail:")
}
first = false
var msg string
var occurrence time.Time
err1 := rows.Scan(&msg, &occurrence)
if err1 != nil {
fmt.Fprintf(os.Stderr, "Error retrieving audit entry:%v", err1)
continue
}
dmsg, err := decodeMsg(msg)
if err == nil {
fmt.Printf("Occurrence: %v, message: %v\n", occurrence, dmsg)
}
}
}
func restart(args []string) {
for _, id := range args {
now := time.Now()
sql := "insert into cop_queue (ppool_id, priority, last_mod_ts, workflow_instance_id) (select ppool_id, priority, $1, id from cop_workflow_instance where id=$2 and (state=4 or state=5))"
_, err := conn.Exec(context.Background(), sql, now, id)
if err != nil {
fmt.Fprintf(os.Stderr, "Error restarting workflow instance: %v, %v", id, err)
continue
}
sql = "update cop_workflow_instance set state=0, last_mod_ts=$1 where id=$2 and (state=4 or state=4)"
_, err = conn.Exec(context.Background(), sql, now, id)
if err != nil {
fmt.Fprintf(os.Stderr, "Error restarting workflow instance:%v, %v", id, err)
continue
}
_, err = conn.Exec(context.Background(), "delete from cop_workflow_instance_error where workflow_instance_id=$1", id)
if err != nil {
fmt.Fprintf(os.Stderr, "Error removing workflow instance: %v from error table, %v", id, err)
}
}
}
func cleanup(age string, audit bool, instance bool) {
if len(age) < 1 {
exitOnErr("Flag -age is mandatory")
}
if !audit && !instance {
exitOnErr("Use at least one of the following flags: [-audit-trail,-workflow-instance]")
}
atime, err := parseAge(age)
if err != nil {
exitOnErr("Invalid age value %v. Use valid day(d), hours(h) or timestamp format", age)
}
if instance {
sql := "delete from cop_workflow_instance_error where error_ts < $1;"
conn.Exec(context.Background(), sql, atime)
sql = "delete from cop_response where response_ts < $1;"
conn.Exec(context.Background(), sql, atime)
sql = "delete from cop_wait where workflow_instance_id IN (select id from cop_workflow_instance where creation_ts < $1);"
conn.Exec(context.Background(), sql, atime)
sql = "delete from cop_adaptercall where workflowid IN (select id from cop_workflow_instance where creation_ts < $1);"
conn.Exec(context.Background(), sql, atime)
sql = "delete from cop_lock where workflow_instance_id IN (select id from cop_workflow_instance where creation_ts < $1);"
conn.Exec(context.Background(), sql, atime)
sql = "delete from cop_queue where workflow_instance_id IN (select id from cop_workflow_instance where creation_ts < $1);"
conn.Exec(context.Background(), sql, atime)
sql = "delete from cop_workflow_instance where creation_ts < $1;"
conn.Exec(context.Background(), sql, atime)
}
if audit {
sql := "delete from cop_audit_trail_event where occurrence < $1;"
conn.Exec(context.Background(), sql, atime)
}
fmt.Fprintf(os.Stdout, "deleted data older than %v", atime)
}
func readPipe() []string {
info, _ := os.Stdin.Stat()
if (info.Mode() & os.ModeCharDevice) == 0 {
reader := bufio.NewReader(os.Stdin)
var output []rune
for {
input, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
}
output = append(output, input)
}
return strings.Split(strings.Trim(string(output), "\n"), "\n")
}
return nil
}
func stateIndex(state string) int {
aState := strings.ToUpper(state)
idx, _ := find(states, aState)
return idx
}
func indexState(idx int) (string, error) {
if idx > -1 && idx < len(states) {
return states[idx], nil
}
return "", errors.New("Invalid state index")
}
func find(slice []string, val string) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
func decodeMsg(msg string) (string, error) {
var dmsg []byte
if msg[0] != 'C' && msg[0] != 'U' {
return msg, nil
}
r := base64.NewDecoder(base64.StdEncoding, strings.NewReader(msg[1:]))
dmsg, err := ioutil.ReadAll(r)
if err != nil {
return msg, err
}
if msg[0] == 'C' {
r, err := zlib.NewReader(bytes.NewReader(dmsg))
if err != nil {
return "", err
}
dmsg, err = ioutil.ReadAll(r)
if err != nil {
return "", err
}
}
return string(dmsg[7:]), err
}
func jsonData(selector *string, state string) {
var err error
var sql string
if selector == nil {
exitOnErr("Flag -json-selector is mandatory")
}
stateIdx := stateIndex(state)
stateInt := strconv.Itoa(stateIdx)
if stateIdx < 0 {
exitOnErr("Invalid state: %v. Allowed states are: %v", state, fmt.Sprint(states))
}
if states[stateIdx] == "ALL" {
sql = "select id from (select id,state, data::jsonb as json from cop_workflow_instance) as r where " + *selector
} else {
sql = "select id from (select id,state, data::jsonb as json from cop_workflow_instance) as r where state=" + stateInt + " and " + *selector
}
rows, err := conn.Query(context.Background(), sql)
if err != nil {
exitOnErr("Query Error:%v", err)
}
for rows.Next() {
var id string
err1 := rows.Scan(&id)
if err1 == nil {
fmt.Println(id)
}
}
}
func parseAge(age string) (time.Time, error) {
var dur string
var err error
var atime time.Time
if strings.HasSuffix(age, "d") {
days, err := strconv.Atoi(age[0 : len(age)-1])
if err != nil {
err = errors.New("Invalid day format " + age)
} else {
dur = strconv.Itoa(days*24) + "h"
}
} else if strings.HasSuffix(age, "h") {
dur = age
}
if err == nil {
if len(dur) > 0 {
var value time.Duration
value, err = time.ParseDuration(dur)
if value <= 0 {
err = errors.New("Invalid age value " + age)
}
if err == nil {
atime = time.Now().Add(-value)
}
} else {
atime, err = time.Parse(pgTimestampFormat, age)
}
}
return atime, err
}
func exitOnErr(msg string, val ...interface{}) {
if val != nil {
fmt.Fprintf(os.Stderr, msg+"\n", val...)
} else {
fmt.Fprintln(os.Stderr, msg)
}
os.Exit(1)
}