-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathjob_watcher_test.go
208 lines (179 loc) · 5.72 KB
/
job_watcher_test.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
// Copyright 2024 Ant Group Co., Ltd.
//
// 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 executor
import (
"fmt"
"net"
"sync/atomic"
"testing"
"time"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
gormlog "gorm.io/gorm/logger"
"github.com/secretflow/scql/pkg/broker/storage"
)
func TestJobWatcher(t *testing.T) {
r := require.New(t)
manager, _, err := dbSetUp()
r.NoError(err)
var jobDead atomic.Bool
cb := func(jobID, reason string) {
jobDead.Store(true)
}
w, err := NewJobWatcher(manager, cb, WithMaxUnavailableDuration(time.Second*3))
r.NoError(err)
stopFn, err := StartJobWatcherScheduler(w, time.Second)
r.NoError(err)
defer stopFn()
jobId := "test-job-id"
// err = w.metaManager.ExecInMetaTransaction(func(txn *storage.MetaTransaction) (err error) {
// return txn.SetSessionInfo(&storage.SessionInfo{SessionID: jobId, Status: int8(storage.SessionRunning), EngineUrl: unreachableAddr, EngineUrlForSelf: unreachableAddr})
// })
// r.NoError(err)
// sleep 5s to wait query job been mark dead, since 5s > 1s*3
time.Sleep(time.Second * 5)
r.True(jobDead.Load())
var sessionInfo *storage.SessionInfo
err = manager.ExecInMetaTransaction(func(txn *storage.MetaTransaction) (err error) {
sessionInfo, err = txn.GetSessionInfo(jobId)
return err
})
r.NoError(err)
r.Equal(sessionInfo.Status, int8(storage.SessionFailed))
}
func TestJobWatcherBeforeEngineExecute(t *testing.T) {
r := require.New(t)
manager, _, err := dbSetUp()
r.NoError(err)
jobId := "test-job-id"
err = manager.ExecInMetaTransaction(func(txn *storage.MetaTransaction) (err error) {
return txn.UpdateSessionInfoStatusWithCondition(jobId, storage.SessionRunning, storage.SessionSubmitted)
})
r.NoError(err)
var jobDead atomic.Bool
cb := func(jobID, reason string) {
jobDead.Store(true)
}
w, err := NewJobWatcher(manager, cb, WithMaxUnavailableDuration(time.Second*3))
r.NoError(err)
stopFn, err := StartJobWatcherScheduler(w, time.Second)
r.NoError(err)
defer stopFn()
// job isn't watched because the statu is SessionSubmitted
time.Sleep(time.Second * 5)
r.False(jobDead.Load())
err = manager.ExecInMetaTransaction(func(txn *storage.MetaTransaction) (err error) {
return txn.UpdateSessionInfoStatusWithCondition(jobId, storage.SessionSubmitted, storage.SessionRunning)
})
r.NoError(err)
// sleep 5s to wait query job been mark dead, since 5s > 1s*3
time.Sleep(time.Second * 5)
r.True(jobDead.Load())
var sessionInfo *storage.SessionInfo
err = manager.ExecInMetaTransaction(func(txn *storage.MetaTransaction) (err error) {
sessionInfo, err = txn.GetSessionInfo(jobId)
return err
})
r.NoError(err)
r.Equal(sessionInfo.Status, int8(storage.SessionFailed))
}
func TestJobWatcherReStart(t *testing.T) {
r := require.New(t)
manager, unreachableAddr, err := dbSetUp()
r.NoError(err)
var jobDead atomic.Bool
cb := func(jobID, reason string) {
jobDead.Store(true)
}
w, err := NewJobWatcher(manager, cb, WithMaxUnavailableDuration(time.Second*3))
r.NoError(err)
jobId := "test-job-id"
err = w.metaManager.ExecInMetaTransaction(func(txn *storage.MetaTransaction) (err error) {
return txn.SetSessionInfo(&storage.SessionInfo{SessionID: jobId, Status: int8(storage.SessionRunning), EngineUrl: unreachableAddr, EngineUrlForSelf: unreachableAddr})
})
time.Sleep(time.Second)
stopFn, err := StartJobWatcherScheduler(w, time.Second)
r.NoError(err)
defer stopFn()
// restart
w, err = NewJobWatcher(manager, cb)
r.NoError(err)
stopFn, err = StartJobWatcherScheduler(w, time.Second)
r.NoError(err)
defer stopFn()
// sleep 5s to wait query job been mark dead, since 5s > 1s*3
time.Sleep(time.Second * 5)
r.True(jobDead.Load())
var sessionInfo *storage.SessionInfo
err = manager.ExecInMetaTransaction(func(txn *storage.MetaTransaction) (err error) {
sessionInfo, err = txn.GetSessionInfo(jobId)
return err
})
r.NoError(err)
r.Equal(sessionInfo.Status, int8(storage.SessionFailed))
}
func dbSetUp() (*storage.MetaManager, string, error) {
id, err := uuid.NewUUID()
if err != nil {
return nil, "", err
}
connStr := fmt.Sprintf("file:%s?mode=memory&cache=shared", id)
db, err := gorm.Open(sqlite.Open(connStr),
&gorm.Config{
SkipDefaultTransaction: true,
Logger: gormlog.New(
logrus.StandardLogger(),
gormlog.Config{
SlowThreshold: 200 * time.Millisecond,
Colorful: false,
LogLevel: gormlog.Info,
}),
})
if err != nil {
return nil, "", err
}
manager := storage.NewMetaManager(db)
err = manager.Bootstrap()
if err != nil {
return nil, "", err
}
port, err := getFreePort()
if err != nil {
return nil, "", err
}
unreachableAddr := fmt.Sprintf("127.0.0.1:%d", port)
sessionInfo := storage.SessionInfo{
SessionID: "test-job-id",
EngineUrlForSelf: unreachableAddr,
Status: int8(storage.SessionRunning),
}
err = manager.ExecInMetaTransaction(func(txn *storage.MetaTransaction) (err error) {
return txn.SetSessionInfo(&sessionInfo)
})
if err != nil {
return nil, "", err
}
return manager, unreachableAddr, nil
}
func getFreePort() (int, error) {
l, err := net.Listen("tcp", ":0")
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}