-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmigrate.go
More file actions
235 lines (201 loc) · 7.43 KB
/
migrate.go
File metadata and controls
235 lines (201 loc) · 7.43 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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"net/http/cookiejar"
"sync"
)
// APIResponse matches the expected JSON structure for the submission list
type APIResponse struct {
Data struct {
Submissions []struct {
SubmissionID string `json:"submissionId"`
} `json:"submissions"`
} `json:"data"`
}
// LoginRequest defines the structure for the login request body
type LoginRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}
const baseApiUrl = "http://localhost:8080/api"
// loginUser authenticates the user and returns an http.Client with session cookies
func loginUser(username, password string) (*http.Client, error) {
loginURL := fmt.Sprintf("%s/auth/session", baseApiUrl)
loginPayload := LoginRequest{Username: username, Password: password}
payloadBytes, err := json.Marshal(loginPayload)
if err != nil {
return nil, fmt.Errorf("failed to marshal login payload: %w", err)
}
// Create a new cookie jar
jar, err := cookiejar.New(nil)
if err != nil {
return nil, fmt.Errorf("failed to create cookie jar: %w", err)
}
// Create an HTTP client with the cookie jar
client := &http.Client{
Jar: jar,
}
resp, err := client.Post(loginURL, "application/json", bytes.NewBuffer(payloadBytes))
if err != nil {
return nil, fmt.Errorf("login request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := ReadAll(resp)
return nil, fmt.Errorf("login failed: status code %d, body: %s", resp.StatusCode, string(bodyBytes))
}
// Cookies are now stored in client.Jar automatically
log.Println("Login successful, session cookies should be set.")
return client, nil
}
// fetchSubmissionIDs retrieves submission IDs from the API using the provided http.Client
func fetchSubmissionIDs(offset, count int, client *http.Client) ([]string, error) {
url := fmt.Sprintf("%s/submission?count=%d&offset=%d", baseApiUrl, count, offset)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to fetch submissions: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to fetch submissions: status code %d", resp.StatusCode)
}
var apiResp APIResponse
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
return nil, fmt.Errorf("failed to decode submission response: %w", err)
}
var ids []string
for _, sub := range apiResp.Data.Submissions {
ids = append(ids, sub.SubmissionID)
}
return ids, nil
}
// migrateSubmissionCode sends a POST request to migrate the code for a given submission ID using the provided http.Client
func migrateSubmissionCode(submissionID string, client *http.Client) {
log.Printf("Processing submissionId: %s", submissionID)
url := fmt.Sprintf("%s/submission/%s/migrate-code", baseApiUrl, submissionID)
req, err := http.NewRequest("POST", url, nil)
if err != nil {
log.Printf("Error creating request for submissionId %s: %v", submissionID, err)
return
}
resp, err := client.Do(req)
if err != nil {
log.Printf("Error migrating code for submissionId %s: %v", submissionID, err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusAccepted {
bodyBytes, err := ReadAll(resp)
if err != nil {
bodyBytes = []byte("failed to read response body")
}
log.Printf("Error migrating code for submissionId %s: status code %d, body: %s", submissionID, resp.StatusCode, string(bodyBytes))
return
}
log.Printf("Successfully triggered migration for submissionId: %s (Status: %s)", submissionID, resp.Status)
}
// migrateSubmissionOutput sends a POST request to migrate the output for a given submission ID using the provided http.Client
func migrateSubmissionOutput(submissionID string, client *http.Client) {
log.Printf("Processing submissionId: %s for output migration", submissionID)
url := fmt.Sprintf("%s/submission/%s/migrate-output", baseApiUrl, submissionID)
req, err := http.NewRequest("POST", url, nil)
if err != nil {
log.Printf("Error creating request for submissionId %s: %v", submissionID, err)
return
}
resp, err := client.Do(req)
if err != nil {
log.Printf("Error migrating output for submissionId %s: %v", submissionID, err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusAccepted {
bodyBytes, err := ReadAll(resp)
if err != nil {
bodyBytes = []byte("failed to read response body")
}
log.Printf("Error migrating output for submissionId %s: status code %d, body: %s", submissionID, resp.StatusCode, string(bodyBytes))
return
}
log.Printf("Successfully triggered output migration for submissionId: %s (Status: %s)", submissionID, resp.Status)
}
// ReadAll is a helper, as ioutil.ReadAll is deprecated in Go 1.16+
// For older Go versions, use ioutil.ReadAll
func ReadAll(r *http.Response) ([]byte, error) {
if r == nil || r.Body == nil {
return nil, fmt.Errorf("response or response body is nil")
}
b := bytes.NewBuffer(make([]byte, 0, 512))
_, err := b.ReadFrom(r.Body)
return b.Bytes(), err
}
func main() {
offset := flag.Int("offset", 0, "Offset for fetching submissions")
count := flag.Int("count", 10, "Number of submissions to fetch")
username := flag.String("username", "", "Username for login")
password := flag.String("password", "", "Password for login")
numConsumers := flag.Int("consumers", 5, "Number of consumer goroutines for migration")
flag.Parse()
log.Println("Attempting to login...")
httpClient, err := loginUser(*username, *password)
if err != nil {
log.Fatalf("Login failed: %v", err)
}
log.Println("Login successful.")
submissionIDChan := make(chan string, 1000) // Buffered channel for submission IDs
// Producer goroutine
go func() {
defer close(submissionIDChan) // Close channel when producer is done
const chunk = 100
fetchOffset := *offset
for fetchOffset < *offset+*count {
fetchCount := chunk
if fetchOffset+chunk > *offset+*count {
fetchCount = *offset + *count - fetchOffset
}
log.Printf("Fetching submission IDs with offset: %d, count: %d", fetchOffset, fetchCount)
submissionIDs, err := fetchSubmissionIDs(fetchOffset, fetchCount, httpClient)
if err != nil {
log.Printf("Error fetching submission IDs: %v. Producer stopping.", err)
return
}
if len(submissionIDs) == 0 {
log.Println("No submission IDs found to process.")
return
}
log.Printf("Found %d submission(s) to process. Pushing to channel.", len(submissionIDs))
for _, id := range submissionIDs {
submissionIDChan <- id
}
fetchOffset += chunk
}
log.Println("Producer finished sending all submission IDs.")
}()
// Consumer goroutines
log.Printf("Starting %d consumer goroutines...", numConsumers)
var consumerWg sync.WaitGroup
for i := range *numConsumers {
consumerWg.Add(1)
go func(workerID int) {
defer consumerWg.Done()
log.Printf("Consumer %d started", workerID)
for id := range submissionIDChan {
log.Printf("Consumer %d processing submissionId: %s", workerID, id)
migrateSubmissionCode(id, httpClient)
migrateSubmissionOutput(id, httpClient)
}
log.Printf("Consumer %d finished.", workerID)
}(i + 1)
}
consumerWg.Wait() // Wait for all consumers to finish
log.Println("All processing finished.")
}