-
Notifications
You must be signed in to change notification settings - Fork 3
/
cleaner.go
209 lines (176 loc) · 4.69 KB
/
cleaner.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
package worker
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/adrianchifor/Bigbucket/store"
"github.com/adrianchifor/Bigbucket/utils"
"github.com/adrianchifor/go-parallel"
"github.com/gin-gonic/gin"
)
var (
stopCleaner = false
stopCleanerMutex = &sync.Mutex{}
)
// RunCleaner runs the cleaner once or on an interval
func RunCleaner(interval int) {
done := make(chan bool, 1)
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
deleteJobPool := parallel.LargeJobPool()
defer deleteJobPool.Close()
go cleanerGracefulShutdown(deleteJobPool, quit, done)
log.Printf("Running cleaner...")
cleanupTables(deleteJobPool)
cleanupColumns(deleteJobPool)
if interval > 0 {
log.Printf("Running cleaner every %d seconds...", interval)
ticker := time.NewTicker(time.Second * time.Duration(interval))
defer ticker.Stop()
loop:
for {
select {
case <-ticker.C:
cleanupTables(deleteJobPool)
cleanupColumns(deleteJobPool)
case <-done:
log.Println("Cleaner schedule has been cancelled")
break loop
}
}
}
log.Println("Cleaner process done")
}
// RunCleanerHttp runs an HTTP server+router for cleaner
func RunCleanerHttp(port int) {
deleteJobPool := parallel.LargeJobPool()
defer deleteJobPool.Close()
router := gin.Default()
router.POST("/", func(c *gin.Context) {
log.Printf("Running cleaner...")
cleanupTables(deleteJobPool)
cleanupColumns(deleteJobPool)
c.String(200, "OK")
})
router.GET("/health", func(c *gin.Context) {
c.String(200, "UP")
})
utils.RunServer(port, router)
}
func cleanerGracefulShutdown(jobPool *parallel.JobPool, quit <-chan os.Signal, done chan<- bool) {
<-quit
log.Println("Cleaner process is shutting down...")
stopCleanerMutex.Lock()
stopCleaner = true
stopCleanerMutex.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err := jobPool.WaitContext(ctx)
if err != nil {
log.Fatalf("Could not gracefully shutdown the cleaner process: %v\n", err)
}
close(done)
}
func cleanupTables(jobPool *parallel.JobPool) {
tablesToDelete := utils.GetState("bigbucket/.delete_tables")
if len(tablesToDelete) == 0 {
return
}
for i, table := range tablesToDelete {
objects, err := store.ListObjects(fmt.Sprintf("bigbucket/%s/", table), "", 0)
if err != nil {
log.Printf("Failed to list objects in table '%s': %v", table, err)
continue
}
if len(objects) == 0 {
err := utils.WriteState("bigbucket/.delete_tables", utils.RemoveIndex(tablesToDelete, i))
if err != nil {
log.Printf("Failed to update .delete_tables state: %v", err)
} else {
log.Printf("Table '%s' cleaned up", table)
}
continue
}
for _, object := range objects {
object := object
jobPool.AddJob(func() {
stopCleanerMutex.Lock()
if stopCleaner {
stopCleanerMutex.Unlock()
return
}
stopCleanerMutex.Unlock()
store.DeleteObject(object)
})
}
}
jobPool.Wait()
// Double check objects and update deleted tables state if nothing left
cleanupTables(jobPool)
}
func cleanupColumns(jobPool *parallel.JobPool) {
objects, err := store.ListObjects("bigbucket/", "/", 0)
if err != nil {
log.Printf("Failed to list tables: %v", err)
}
if len(objects) == 0 {
return
}
tables := utils.CleanupTables(objects)
noColumnsToDelete := true
for _, table := range tables {
columnsToDelete := utils.GetState(fmt.Sprintf("bigbucket/%s/.delete_columns", table))
if len(columnsToDelete) == 0 {
continue
}
if noColumnsToDelete {
noColumnsToDelete = false
}
objects, err = store.ListObjects(fmt.Sprintf("bigbucket/%s/", table), "", 0)
if err != nil {
log.Printf("Failed to list objects in table '%s': %v", table, err)
continue
}
for i, column := range columnsToDelete {
column := column
noColumnsFound := true
for _, object := range objects {
object := object
if strings.HasSuffix(object, column) {
if noColumnsFound {
noColumnsFound = false
}
jobPool.AddJob(func() {
stopCleanerMutex.Lock()
if stopCleaner {
stopCleanerMutex.Unlock()
return
}
stopCleanerMutex.Unlock()
store.DeleteObject(object)
})
}
}
jobPool.Wait()
if noColumnsFound {
err := utils.WriteState(fmt.Sprintf("bigbucket/%s/.delete_columns", table), utils.RemoveIndex(columnsToDelete, i))
if err != nil {
log.Printf("Failed to update %s/.delete_columns state: %v", table, err)
} else {
log.Printf("Column '%s' in table '%s' cleaned up", column, table)
}
}
}
}
if noColumnsToDelete {
return
}
// Double check objects and update deleted columns state if nothing left
cleanupColumns(jobPool)
}