-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
704 lines (602 loc) · 21.7 KB
/
db.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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
package statemachine
import (
"database/sql"
"fmt"
"regexp"
"strings"
"time"
)
func normalizeTableName(stateMachineName string) string {
// Remove any non-alphanumeric characters and convert to lowercase
return strings.ToLower(
// Replace non-alphanumeric characters with underscores
regexp.MustCompile(`[^a-zA-Z0-9]+`).ReplaceAllString(stateMachineName, "_"),
)
}
func createGlobalLockTableIfNotExistsSQL() string {
return `
CREATE TABLE IF NOT EXISTS GLOBAL_LOCK (
ID INT NOT NULL AUTO_INCREMENT,
StateMachineType VARCHAR(255),
StateMachineID VARCHAR(255),
LookupKey VARCHAR(255),
LockTimestamp TIMESTAMP,
UnlockTimestamp TIMESTAMP NULL,
PRIMARY KEY (ID),
INDEX (StateMachineType),
INDEX (LookupKey),
INDEX (UnlockTimestamp)
);`
}
func CreateGlobalLockTableIfNotExists(db *sql.DB) error {
// Execute the SQL statement to create the table
_, err := db.Exec(createGlobalLockTableIfNotExistsSQL())
if err != nil {
return err
}
return nil
}
// checkLockExists checks if a lock exists based on provided query and arguments.
func checkLockExists(tx *sql.Tx, query string, args ...interface{}) (bool, error) {
var lockID int
err := tx.QueryRow(query, args...).Scan(&lockID)
if err == nil {
// A lock exists based on the provided query and arguments.
return true, nil
} else if err == sql.ErrNoRows {
// No lock exists based on the provided query and arguments.
return false, nil
}
// Handle other potential errors.
return false, err
}
func removeGlobalLockOwnedByThisInstanceSQL() string {
return "UPDATE GLOBAL_LOCK SET UnlockTimestamp = NOW() WHERE StateMachineType = ? AND StateMachineID = ? AND LookupKey = ? AND (UnlockTimestamp IS NULL OR UnlockTimestamp > NOW());"
}
func removeGlobalLockOwnedByThisInstance(tx *sql.Tx, sm *StateMachine) error {
_, err := tx.Exec(removeGlobalLockOwnedByThisInstanceSQL(), sm.Name, sm.UniqueID, sm.LookupKey)
if err != nil {
return err
}
return nil
}
func removeGlobalLockOwnedByThisMachineTypeSQL() string {
return "UPDATE GLOBAL_LOCK SET UnlockTimestamp = NOW() WHERE StateMachineType = ? AND LookupKey = ? AND (UnlockTimestamp IS NULL OR UnlockTimestamp > NOW());"
}
func removeGlobalLockOwnedByThisMachineType(tx *sql.Tx, sm *StateMachine) error {
_, err := tx.Exec(removeGlobalLockOwnedByThisMachineTypeSQL(), sm.Name, sm.LookupKey)
if err != nil {
return err
}
return nil
}
func removeAllGlobalLocksSQL() string {
return "UPDATE GLOBAL_LOCK SET UnlockTimestamp = NOW() WHERE AND LookupKey = ? AND (UnlockTimestamp IS NULL OR UnlockTimestamp > NOW());"
}
func removeAllGlobalLocks(tx *sql.Tx, sm *StateMachine) error {
_, err := tx.Exec(removeAllGlobalLocksSQL(), sm.Name, sm.LookupKey)
if err != nil {
return err
}
return nil
}
func isGlobalLockOwnedByThisInstanceSQL() string {
return "SELECT StateMachineID FROM GLOBAL_LOCK WHERE StateMachineType = ? AND StateMachineID = ? AND LookupKey = ? AND (UnlockTimestamp IS NULL OR UnlockTimestamp > NOW()) FOR UPDATE;"
}
func isGlobalLockOwnedByThisInstance(tx *sql.Tx, sm *StateMachine) (bool, error) {
return checkLockExists(tx, isGlobalLockOwnedByThisInstanceSQL(), sm.Name, sm.UniqueID, sm.LookupKey)
}
func checkGlobalLockExistsSQL() string {
return "SELECT StateMachineID FROM GLOBAL_LOCK WHERE LookupKey = ? AND (UnlockTimestamp IS NULL OR UnlockTimestamp > NOW()) FOR UPDATE;"
}
// checkGlobalLockExists checks if a global lock exists for the given state machine and custom lookup key.
// by definition, a global lock is for the lookup key (eg, user ID) regardless of the state machine type
func checkGlobalLockExists(tx *sql.Tx, sm *StateMachine) (bool, error) {
return checkLockExists(tx, checkGlobalLockExistsSQL(), sm.LookupKey)
}
func obtainGlobalLockSQL() string {
return "INSERT INTO GLOBAL_LOCK (StateMachineType, StateMachineID, LookupKey, LockTimestamp) VALUES (?, ?, ?, NOW());"
}
// obtainGlobalLock attempts to obtain a global lock for a specific type of state machine instance with a custom lookup key.
func obtainGlobalLock(tx *sql.Tx, sm *StateMachine) error {
// Insert a new global lock record into the GLOBAL_LOCK table with the type, instance, and custom lookup key.
_, err := tx.Exec(obtainGlobalLockSQL(), sm.Name, sm.UniqueID, sm.LookupKey)
if err != nil {
return err
}
// Lock obtained successfully.
return nil
}
func createStateMachineTableIfNotExistsSQL(stateMachineName string) string {
return fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s (
ID VARCHAR(255) PRIMARY KEY,
CurrentState VARCHAR(255),
LookupKey VARCHAR(255),
ResumeFromStep INT,
SaveAfterStep BOOLEAN,
KafkaEventTopic VARCHAR(255),
SerializedState JSON,
CreatedTimestamp TIMESTAMP,
UpdatedTimestamp TIMESTAMP,
UsesGlobalLock BOOLEAN,
UsesLocalLock BOOLEAN,
UnlockedTimestamp TIMESTAMP NULL,
LastRetryTimestamp TIMESTAMP NULL,
INDEX (LookupKey)
);`, normalizeTableName(stateMachineName))
}
func CreateStateMachineTableIfNotExists(db *sql.DB, stateMachineName string) error {
// Execute the SQL statement to create the table
_, err := db.Exec(createStateMachineTableIfNotExistsSQL(stateMachineName))
return err
}
func checkLockStatusSQL(tableName, condition string) string {
return fmt.Sprintf("SELECT ID FROM %s WHERE %s AND CurrentState = '%s' AND (UnlockedTimestamp IS NULL OR UnlockedTimestamp > NOW()) FOR UPDATE;", normalizeTableName(tableName), condition, StateLocked)
}
// checkLockStatus checks if a lock exists based on provided conditions.
func checkLockStatus(tx *sql.Tx, query string, args ...interface{}) (bool, error) {
var lockID string
err := tx.QueryRow(query, args...).Scan(&lockID)
if err == nil {
// A lock exists based on the provided conditions.
return true, nil
} else if err == sql.ErrNoRows {
// No lock exists based on the provided conditions.
return false, nil
}
// Handle other potential errors.
return false, err
}
func checkLocalLockExistsSQL(tableName string) string {
return checkLockStatusSQL(tableName, "LookupKey = ?")
}
func checkLocalLockExists(tx *sql.Tx, sm *StateMachine) (bool, error) {
return checkLockStatus(tx, checkLocalLockExistsSQL(sm.Name), sm.LookupKey)
}
func isLocalLockOwnedByThisInstanceSQL(tableName string) string {
return checkLockStatusSQL(tableName, "ID = ? AND LookupKey = ?")
}
func isLocalLockOwnedByThisInstance(tx *sql.Tx, sm *StateMachine) (bool, error) {
return checkLockStatus(tx, isLocalLockOwnedByThisInstanceSQL(sm.Name), sm.UniqueID, sm.LookupKey)
}
func obtainLocalLockSQL(tableName string) string {
return fmt.Sprintf(`
INSERT INTO %s (ID, CurrentState, LookupKey, ResumeFromStep, SaveAfterStep, KafkaEventTopic, SerializedState, CreatedTimestamp, UpdatedTimestamp, UnlockedTimestamp, LastRetryTimestamp, UsesGlobalLock, UsesLocalLock)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
CurrentState = VALUES(CurrentState),
UsesLocalLock = VALUES(UsesLocalLock),
SerializedState = VALUES(SerializedState),
UpdatedTimestamp = VALUES(UpdatedTimestamp),
UnlockedTimestamp = VALUES(UnlockedTimestamp),
LastRetryTimestamp = VALUES(LastRetryTimestamp);`, normalizeTableName(tableName))
}
// obtainLocalLock attempts to obtain a local lock for a specific type of state machine instance with a custom lookup key
// it will update an existing statemachine if it exists to use the local lock
func obtainLocalLock(tx *sql.Tx, sm *StateMachine) error {
// Serialize the StateMachine to JSON
serializedState, err := sm.serializeToJSON()
if err != nil {
return err
}
var usesGlobalLock, usesLocalLock bool
if sm.LockType == GlobalLock {
usesGlobalLock = true
} else if sm.LockType == LocalLock {
usesLocalLock = true
} else {
return fmt.Errorf("state machine is not configured to use local lock")
}
// Convert zero time.Time to nil for SQL insertion
var unlockedTimestamp interface{}
var lastRetry interface{}
if sm.LastRetry == nil {
lastRetry = nil
} else {
lastRetry = sm.LastRetry.UTC()
}
localSQL := obtainLocalLockSQL(sm.Name)
// Insert or update the local lock record
_, err = tx.Exec(localSQL,
sm.UniqueID, StateLocked, sm.LookupKey, sm.ResumeFromStep, sm.SaveAfterEachStep, sm.KafkaEventTopic, string(serializedState), sm.CreatedTimestamp.UTC(), sm.UpdatedTimestamp.UTC(), unlockedTimestamp, lastRetry, usesGlobalLock, usesLocalLock)
if err != nil {
return err
}
// Lock obtained successfully.
return nil
}
func insertStateMachineSQL(tableName string) string {
return fmt.Sprintf(`
INSERT INTO %s (ID,
CurrentState,
LookupKey,
ResumeFromStep,
SaveAfterStep,
KafkaEventTopic,
SerializedState,
CreatedTimestamp,
UpdatedTimestamp,
UnlockedTimestamp,
LastRetryTimestamp,
UsesGlobalLock,
UsesLocalLock)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`, normalizeTableName(tableName))
}
func insertStateMachine(sm *StateMachine) error {
serializedState, err := sm.serializeToJSON()
if err != nil {
return err
}
var usesGlobalLock, usesLocalLock bool
if sm.LockType == GlobalLock {
usesGlobalLock = true
}
if sm.LockType == LocalLock {
usesLocalLock = true
}
// Convert zero time.Time to nil for SQL insertion
var unlockedTimestamp interface{}
if sm.UnlockedTimestamp == nil {
unlockedTimestamp = nil
} else {
unlockedTimestamp = sm.UnlockedTimestamp.UTC()
}
var lastRetry interface{}
if sm.LastRetry == nil {
lastRetry = nil
} else {
lastRetry = sm.LastRetry.UTC()
}
// Execute the SQL statement within the transaction
_, err = sm.DB.Exec(insertStateMachineSQL(sm.Name), sm.UniqueID, sm.CurrentState, sm.LookupKey, sm.ResumeFromStep, sm.SaveAfterEachStep, sm.KafkaEventTopic, string(serializedState), sm.CreatedTimestamp.UTC(), sm.UpdatedTimestamp.UTC(), unlockedTimestamp, lastRetry, usesGlobalLock, usesLocalLock)
return err
}
func queryStateMachinesByType(db *sql.DB, stateMachineName string) ([]StateMachine, error) {
tableName := stateMachineName
querySQL := fmt.Sprintf("SELECT * FROM %s;", tableName)
// Execute the SQL query
rows, err := db.Query(querySQL)
if err != nil {
return nil, err
}
defer rows.Close()
// Parse the query results into StateMachine structs
var stateMachines []StateMachine
var serializedState string
for rows.Next() {
var sm StateMachine
var usesGlobalLock, usesLocalLock sql.NullBool
// Scan row data into sm fields
err := rows.Scan(&sm.UniqueID, &sm.CurrentState, &sm.ResumeFromStep, &sm.SaveAfterEachStep, &sm.KafkaEventTopic, serializedState, &sm.CreatedTimestamp, &sm.UpdatedTimestamp, &usesGlobalLock, &usesLocalLock)
if err != nil {
return nil, err
}
if usesGlobalLock.Valid && usesGlobalLock.Bool {
sm.LockType = GlobalLock
}
if usesLocalLock.Valid && usesLocalLock.Bool {
sm.LockType = LocalLock
}
stateMachines = append(stateMachines, sm)
}
return stateMachines, nil
}
// TODO: create unit tests for this
// Load the serialized JSON state data from the database
func loadStateMachineFromDB(stateMachineType string, id string, db *sql.DB) (*StateMachine, error) {
// Load serialized state data from the database (replace with your database logic)
sm := &StateMachine{
UniqueID: id,
Name: stateMachineType,
DB: db,
}
sm, err := loadAndLockStateMachine(sm)
if err != nil {
return nil, NewDatabaseOperationError("loadAndLockStateMachine", err)
}
return sm, nil
}
func loadStateMachineWithNoLock(sm *StateMachine) (*StateMachine, error) {
tableName := normalizeTableName(sm.Name)
querySQL := fmt.Sprintf("SELECT * FROM %s WHERE ID = ?;", tableName)
// Execute the SQL query
rows, err := sm.DB.Query(querySQL, sm.UniqueID)
if err != nil {
return nil, err
}
defer rows.Close()
// Parse the query results into StateMachine structs
var serializedState string
var createdTimestampStr, updatedTimestampStr, unlockedTimestampStr sql.NullString
for rows.Next() {
var usesGlobalLock, usesLocalLock sql.NullBool
// Scan row data into sm fields
err := rows.Scan(&sm.UniqueID, &sm.CurrentState, &sm.ResumeFromStep, &sm.SaveAfterEachStep, &sm.KafkaEventTopic, serializedState, &sm.CreatedTimestamp, &sm.UpdatedTimestamp, &usesGlobalLock, &usesLocalLock)
if err != nil {
return nil, err
}
if usesGlobalLock.Valid && usesGlobalLock.Bool {
sm.LockType = GlobalLock
}
if usesLocalLock.Valid && usesLocalLock.Bool {
sm.LockType = LocalLock
}
if createdTimestampStr.Valid {
sm.CreatedTimestamp, err = parseTimestamp(createdTimestampStr.String)
if err != nil {
return nil, err
}
}
if updatedTimestampStr.Valid {
sm.UpdatedTimestamp, err = parseTimestamp(updatedTimestampStr.String)
if err != nil {
return nil, err
}
}
if unlockedTimestampStr.Valid {
unlockedTimestamp, err := parseTimestamp(unlockedTimestampStr.String)
if err != nil {
return nil, err
}
sm.UnlockedTimestamp = &unlockedTimestamp
}
}
return sm, nil
}
func loadStateMachineSQL(tableName string) string {
return fmt.Sprintf("SELECT * FROM %s WHERE ID = ?;", normalizeTableName(tableName))
}
func loadStateMachine(tx *sql.Tx, sm *StateMachine) (*StateMachine, error) {
var loadedSM StateMachine
var serializedState []byte
var createdTimestampStr, updatedTimestampStr, unlockedTimestampStr, lastRetryTimestamp sql.NullString
var usesGlobalLock, usesLocalLock sql.NullBool
err := tx.QueryRow(loadStateMachineSQL(sm.Name), sm.UniqueID).Scan(
&loadedSM.UniqueID,
&loadedSM.CurrentState,
&loadedSM.LookupKey,
&loadedSM.ResumeFromStep,
&loadedSM.SaveAfterEachStep,
&loadedSM.KafkaEventTopic,
&serializedState,
&createdTimestampStr,
&updatedTimestampStr,
&usesGlobalLock,
&usesLocalLock,
&unlockedTimestampStr,
&lastRetryTimestamp)
if err != nil {
if err == sql.ErrNoRows {
return nil, fmt.Errorf("state machine not found")
}
return nil, err
}
if usesGlobalLock.Valid && usesGlobalLock.Bool {
loadedSM.LockType = GlobalLock
}
if usesLocalLock.Valid && usesLocalLock.Bool {
loadedSM.LockType = LocalLock
}
if createdTimestampStr.Valid {
loadedSM.CreatedTimestamp, err = parseTimestamp(createdTimestampStr.String)
if err != nil {
tx.Rollback()
return nil, err
}
}
if updatedTimestampStr.Valid {
loadedSM.UpdatedTimestamp, err = parseTimestamp(updatedTimestampStr.String)
if err != nil {
tx.Rollback()
return nil, err
}
}
if unlockedTimestampStr.Valid {
unlockedTimestamp, err := parseTimestamp(unlockedTimestampStr.String)
if err != nil {
return nil, err
}
loadedSM.UnlockedTimestamp = &unlockedTimestamp
}
loadedSM.SerializedState = serializedState
return &loadedSM, nil
}
func lockStateMachineForTransactionSQL(tableName string) string {
return fmt.Sprintf("SELECT ID FROM %s WHERE ID = ? FOR UPDATE;", normalizeTableName(tableName))
}
func lockStateMachineForTransaction(tx *sql.Tx, sm *StateMachine) error {
_, err := tx.Exec(lockStateMachineForTransactionSQL(sm.Name), sm.UniqueID)
return err
}
func loadAndLockStateMachineSQL(tableName string, stateInProgress State) string {
return fmt.Sprintf("UPDATE %s SET CurrentState = '%s', UpdatedTimestamp = NOW() WHERE ID = ?;", tableName, StateLocked)
}
func loadAndLockStateMachine(sm *StateMachine) (*StateMachine, error) {
tableName := normalizeTableName(sm.Name)
// check if there is machine type lock
locks, err := checkStateMachineTypeLock(sm.DB, sm.Name, time.Now())
if err != nil {
return nil, err
}
tx, err := sm.DB.Begin()
if err != nil {
return nil, err
}
defer tx.Rollback()
// Lock the state machine
err = lockStateMachineForTransaction(tx, sm)
if err != nil {
return nil, err
}
// Load the state machine
loadedSM, err := loadStateMachine(tx, sm)
if err != nil {
return nil, err
}
if IsTerminalState(sm.CurrentState) {
// Row exists, but the state machine is in a terminal state
return nil, NewDatabaseOperationError("loadAndLockStateMachine",
fmt.Errorf("state machine is in a terminal state %s", sm.CurrentState))
}
if len(locks) > 0 {
// there is a machine type lock
sm.MachineLockInfo = &locks[0]
// check if the state machine is sleeping
if loadedSM.CurrentState == StateSleeping {
return loadedSM, NewSleepStateError(locks[0].Start, locks[0].End,
fmt.Errorf("state machine can't be locked and loaded because it's sleeping"))
}
// check if there is a lock for this machine type that halts all executions
for _, lock := range locks {
if lock.Type == MachineLockTypeHaltAll {
return loadedSM, NewHaltAllStateMachinesByTypeError(locks[0].Start, locks[0].End,
fmt.Errorf("state machine can't be locked and loaded because everything has been halted"))
}
}
}
// Set state to locked and update the database
_, err = tx.Exec(loadAndLockStateMachineSQL(tableName, StateLocked), sm.UniqueID)
if err != nil {
return nil, err
}
tx.Commit()
return loadedSM, nil
}
func removeLocalLockSQL(tableName string) string {
return fmt.Sprintf("UPDATE %s UnlockedTimestamp = NOW() WHERE ID = ?;", tableName)
}
func removeLocalLock(tx *sql.Tx, sm *StateMachine) error {
_, err := tx.Exec(lockStateMachineForTransactionSQL(sm.Name), sm.UniqueID)
return err
}
func updateStateMachineStateSQL(tableName string) string {
return fmt.Sprintf("UPDATE %s SET CurrentState = ?, SerializedState = ?, UpdatedTimestamp = NOW(), ResumeFromStep = ?, UnlockedTimestamp = ? WHERE ID = ?;", tableName)
}
func updateStateMachineState(sm *StateMachine, newState State) error {
tableName := normalizeTableName(sm.Name)
tx, err := sm.DB.Begin()
if err != nil {
return err
}
newSerializedState, err := sm.serializeToJSON()
if err != nil {
tx.Rollback()
return err
}
var unlockedTimestamp interface{}
if sm.UnlockedTimestamp != nil {
unlockedTimestamp = sm.UnlockedTimestamp.UTC()
}
_, err = tx.Exec(updateStateMachineStateSQL(tableName), newState, newSerializedState, sm.ResumeFromStep, unlockedTimestamp, sm.UniqueID)
if err != nil {
tx.Rollback()
return err
}
if !sm.UnlockGlobalLockAfterEachStep || IsTerminalState(newState) {
// remove global lock
removeGlobalLockOwnedByThisInstance(tx, sm)
}
return tx.Commit()
}
func createStateMachineLockTableIfNotExistsSQL() string {
return `
CREATE TABLE IF NOT EXISTS STATE_MACHINE_TYPE_LOCK (
StateMachineType VARCHAR(255),
LockType VARCHAR(50),
StartTimestamp TIMESTAMP NULL,
EndTimestamp TIMESTAMP NULL,
RecurInterval VARCHAR(10), -- 'None', 'Daily', 'Weekly', 'Monthly'
DayOfWeek INT NULL, -- 1 = Monday, 7 = Sunday, used for weekly recurrence
DayOfMonth INT NULL, -- 1-31, used for monthly recurrence
RecurStartTime TIME NULL,
RecurEndTime TIME NULL
);`
}
func CreateStateMachineLockTableIfNotExists(db *sql.DB) error {
_, err := db.Exec(createStateMachineLockTableIfNotExistsSQL())
if err != nil {
return err
}
return nil
}
func checkStateMachineTypeLockSQL() string {
return `SELECT LockType, StartTimestamp, EndTimestamp, RecurInterval, DayOfWeek, DayOfMonth, RecurStartTime, RecurEndTime
FROM STATE_MACHINE_TYPE_LOCK
WHERE StateMachineType = ? AND StartTimestamp <= ? AND EndTimestamp >= ?
ORDER BY StartTimestamp ASC`
}
func checkStateMachineTypeLock(db *sql.DB, stateMachineType string, queryTime time.Time) ([]StateMachineTypeLockInfo, error) {
var locks []StateMachineTypeLockInfo
rows, err := db.Query(checkStateMachineTypeLockSQL(), stateMachineType, &queryTime, &queryTime)
if err != nil {
if err == sql.ErrNoRows {
return locks, nil
}
return nil, err
}
defer rows.Close()
for rows.Next() {
var lockTypeStr, recurInterval string
var startTimestamp, endTimestamp time.Time
var dayOfWeek, dayOfMonth int
var recurStartTime, recurEndTime time.Time
if err := rows.Scan(&lockTypeStr, &startTimestamp, &endTimestamp, &recurInterval, &dayOfWeek, &dayOfMonth, &recurStartTime, &recurEndTime); err != nil {
return nil, err
}
lockType := ParseMachineLockType(lockTypeStr)
lockInfo := StateMachineTypeLockInfo{
StateMachineType: stateMachineType,
Type: lockType,
Start: startTimestamp,
End: endTimestamp,
RecurInterval: recurInterval,
DayOfWeek: dayOfWeek,
DayOfMonth: dayOfMonth,
RecurStartTime: recurStartTime,
RecurEndTime: recurEndTime,
}
if recurInterval != "None" {
// Logic to check if the current time falls within the recurring schedule
if isWithinRecurringSchedule(recurInterval, dayOfWeek, dayOfMonth, recurStartTime, recurEndTime, queryTime) {
locks = append(locks, lockInfo)
}
} else {
// Check for one-time lock
if queryTime.After(startTimestamp) && queryTime.Before(endTimestamp) {
locks = append(locks, lockInfo)
}
}
}
if err := rows.Err(); err != nil {
return nil, err
}
return locks, nil
}
// SQL Queries as separate functions
func createLockQuery() string {
return `INSERT INTO STATE_MACHINE_TYPE_LOCK
(StateMachineType, LockType, StartTimestamp, EndTimestamp, RecurInterval, DayOfWeek, DayOfMonth, RecurStartTime, RecurEndTime)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
}
func updateLockQuery() string {
return `UPDATE STATE_MACHINE_TYPE_LOCK
SET LockType = ?, StartTimestamp = ?, EndTimestamp = ?, RecurInterval = ?, DayOfWeek = ?, DayOfMonth = ?, RecurStartTime = ?, RecurEndTime = ?
WHERE StateMachineType = ?`
}
func deleteLockQuery() string {
return `DELETE FROM STATE_MACHINE_TYPE_LOCK WHERE StateMachineType = ? AND LockType = ?`
}
// Database functions using the query functions
func createLock(db *sql.DB, lock StateMachineTypeLockInfo) error {
_, err := db.Exec(createLockQuery(), lock.StateMachineType, lock.Type, lock.Start.UTC(), lock.End.UTC(), lock.RecurInterval, lock.DayOfWeek, lock.DayOfMonth, lock.RecurStartTime.UTC(), lock.RecurEndTime.UTC())
return err
}
func updateLock(db *sql.DB, lock StateMachineTypeLockInfo) error {
_, err := db.Exec(updateLockQuery(), lock.Type, lock.Start.UTC(), lock.End.UTC(), lock.RecurInterval, lock.DayOfWeek, lock.DayOfMonth, lock.RecurStartTime.UTC(), lock.RecurEndTime.UTC(), lock.StateMachineType)
return err
}
func deleteLock(db *sql.DB, stateMachineType string, lockType MachineLockType) error {
_, err := db.Exec(deleteLockQuery(), stateMachineType, lockType)
return err
}