This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1456 from ansinlee/master
add preheat implements
- Loading branch information
Showing
16 changed files
with
1,010 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright The Dragonfly Authors. | ||
* | ||
* 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 preheat | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/dragonflyoss/Dragonfly/supernode/daemon/mgr" | ||
) | ||
|
||
var _ Preheater = &BasePreheater{} | ||
|
||
type BasePreheater struct {} | ||
|
||
/** | ||
* The type of this preheater | ||
*/ | ||
func (p *BasePreheater) Type() string { | ||
panic("not implement") | ||
} | ||
|
||
/** | ||
* Create a worker to preheat the task. | ||
*/ | ||
func (p *BasePreheater) NewWorker(task *mgr.PreheatTask , service *PreheatService) IWorker { | ||
panic("not implement") | ||
} | ||
|
||
/** | ||
* cancel the running task | ||
*/ | ||
func (p *BasePreheater) Cancel(id string) { | ||
woker, ok := workerMap.Load(id) | ||
if !ok { | ||
return | ||
} | ||
woker.(IWorker).Stop() | ||
} | ||
|
||
/** | ||
* remove a running preheat task | ||
*/ | ||
func (p *BasePreheater) Remove(id string) { | ||
p.Cancel(id) | ||
workerMap.Delete(id) | ||
} | ||
|
||
/** | ||
* add a worker to workerMap. | ||
*/ | ||
func (p *BasePreheater) addWorker(id string, worker IWorker) { | ||
workerMap.Store(id, worker) | ||
} | ||
|
||
var workerMap = new(sync.Map) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright The Dragonfly Authors. | ||
* | ||
* 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 preheat | ||
|
||
import ( | ||
"runtime/debug" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/dragonflyoss/Dragonfly/apis/types" | ||
"github.com/dragonflyoss/Dragonfly/supernode/daemon/mgr" | ||
) | ||
|
||
const TIMEOUT = 30 * 60 | ||
|
||
var _ IWorker = &BaseWorker{} | ||
|
||
type IWorker interface{ | ||
Run() | ||
Stop() | ||
query() chan error | ||
preRun() bool | ||
failed(errMsg string) | ||
afterRun() | ||
} | ||
|
||
type BaseWorker struct { | ||
Task *mgr.PreheatTask | ||
Preheater Preheater | ||
PreheatService *PreheatService | ||
stop *atomic.Value | ||
worker IWorker | ||
} | ||
|
||
func newBaseWorker(task *mgr.PreheatTask, preheater Preheater, preheatService *PreheatService) *BaseWorker { | ||
worker := &BaseWorker{ | ||
Task: task, | ||
Preheater: preheater, | ||
PreheatService: preheatService, | ||
stop: new(atomic.Value), | ||
} | ||
worker.worker = worker | ||
return worker | ||
} | ||
|
||
func (w *BaseWorker) Run() { | ||
go func() { | ||
defer func(){ | ||
e := recover() | ||
if e != nil { | ||
debug.PrintStack() | ||
} | ||
}() | ||
|
||
if w.worker.preRun() { | ||
timer := time.NewTimer(time.Second*TIMEOUT) | ||
ch := w.worker.query() | ||
select { | ||
case <-timer.C: | ||
w.worker.failed("timeout") | ||
case err := <-ch: | ||
if err != nil { | ||
w.worker.failed(err.Error()) | ||
} | ||
} | ||
} | ||
w.worker.afterRun() | ||
}() | ||
} | ||
|
||
func (w *BaseWorker) Stop() { | ||
w.stop.Store(true) | ||
} | ||
|
||
func (w *BaseWorker) isRunning() bool { | ||
return w.stop.Load() == nil | ||
} | ||
|
||
func (w *BaseWorker) preRun() bool { | ||
panic("not implement") | ||
} | ||
|
||
func (w *BaseWorker) afterRun() { | ||
w.Preheater.Remove(w.Task.ID) | ||
} | ||
|
||
func (w *BaseWorker) query() chan error { | ||
panic("not implement") | ||
} | ||
|
||
func (w *BaseWorker) succeed() { | ||
w.Task.FinishTime = time.Now().UnixNano()/int64(time.Millisecond) | ||
w.Task.Status = types.PreheatStatusSUCCESS | ||
w.PreheatService.Update(w.Task.ID, w.Task) | ||
} | ||
|
||
func (w *BaseWorker) failed(errMsg string) { | ||
w.Task.FinishTime = time.Now().UnixNano()/int64(time.Millisecond) | ||
w.Task.Status = types.PreheatStatusFAILED | ||
w.Task.ErrorMsg = errMsg | ||
w.PreheatService.Update(w.Task.ID, w.Task) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright The Dragonfly Authors. | ||
* | ||
* 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 preheat | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/sirupsen/logrus" | ||
"time" | ||
|
||
"github.com/dragonflyoss/Dragonfly/apis/types" | ||
"github.com/dragonflyoss/Dragonfly/supernode/daemon/mgr" | ||
) | ||
|
||
func init() { | ||
RegisterPreheater("file", &FilePreheat{BasePreheater:new(BasePreheater)}) | ||
logrus.StandardLogger().SetLevel(logrus.DebugLevel) | ||
} | ||
|
||
type FilePreheat struct { | ||
*BasePreheater | ||
} | ||
|
||
func (p *FilePreheat) Type() string { | ||
return "file" | ||
} | ||
|
||
/** | ||
* Create a worker to preheat the task. | ||
*/ | ||
func (p *FilePreheat) NewWorker(task *mgr.PreheatTask , service *PreheatService) IWorker { | ||
worker := &FileWorker{BaseWorker: newBaseWorker(task, p, service)} | ||
worker.worker = worker | ||
p.addWorker(task.ID, worker) | ||
return worker | ||
} | ||
|
||
type FileWorker struct { | ||
*BaseWorker | ||
progress *PreheatProgress | ||
} | ||
|
||
func (w *FileWorker) preRun() bool { | ||
w.Task.Status = types.PreheatStatusRUNNING | ||
w.PreheatService.Update(w.Task.ID, w.Task) | ||
var err error | ||
w.progress, err = w.PreheatService.ExecutePreheat(w.Task) | ||
if err != nil { | ||
w.failed(err.Error()) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (w *FileWorker) afterRun() { | ||
if w.progress != nil { | ||
w.progress.cmd.Process.Kill() | ||
} | ||
w.BaseWorker.afterRun() | ||
} | ||
|
||
func (w *FileWorker) query() chan error { | ||
result := make(chan error, 1) | ||
go func(){ | ||
time.Sleep(time.Second*2) | ||
for w.isRunning() { | ||
if w.Task.FinishTime > 0 { | ||
w.Preheater.Cancel(w.Task.ID) | ||
return | ||
} | ||
if w.progress == nil { | ||
w.succeed() | ||
return | ||
} | ||
status := w.progress.cmd.ProcessState | ||
if status != nil && status.Exited() { | ||
if !status.Success() { | ||
errMsg := fmt.Sprintf("dfget failed: %s err: %s", status.String(), w.progress.errmsg.String()) | ||
w.failed(errMsg) | ||
w.Preheater.Cancel(w.Task.ID) | ||
result <- errors.New(errMsg) | ||
return | ||
} else { | ||
w.succeed() | ||
w.Preheater.Cancel(w.Task.ID) | ||
result <- nil | ||
return | ||
} | ||
} | ||
|
||
time.Sleep(time.Second*10) | ||
} | ||
}() | ||
return result | ||
} | ||
|
Oops, something went wrong.