Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
3a7e682
Move process to create contexts
zeripath Sep 18, 2021
47bda44
display children processes
zeripath Sep 18, 2021
cd16cbb
Make requests a process
zeripath Sep 18, 2021
12039b9
Add context to repo and add ctx to OpenRepository
zeripath Sep 18, 2021
d2b01e4
minor comments
zeripath Sep 22, 2021
659fcf6
Merge remote-tracking branch 'origin/main' into request-as-process
zeripath Sep 23, 2021
9598eca
fix lint and children lock
zeripath Sep 23, 2021
3344e26
Merge remote-tracking branch 'origin/main' into request-as-process
zeripath Oct 10, 2021
7cf7749
separate remove and cancel functions
zeripath Oct 10, 2021
a8e228e
associate repo functions with the repo context
zeripath Oct 10, 2021
08b77d1
fix lint
zeripath Oct 13, 2021
47b0614
Merge remote-tracking branch 'origin/main' into request-as-process
zeripath Oct 13, 2021
518b79e
Merge remote-tracking branch 'origin/main' into request-as-process
zeripath Oct 15, 2021
9bb820b
Simplify PID to strings using the time of start plus/minus a counter
zeripath Oct 15, 2021
9895680
extract process out of manager.go
zeripath Oct 15, 2021
afc5b41
fix test
zeripath Oct 15, 2021
7446c87
Merge remote-tracking branch 'origin/main' into request-as-process
zeripath Oct 15, 2021
86ab980
Merge branch 'main' into request-as-process
zeripath Oct 17, 2021
4ce4614
Make the Mirror Queue a queue (#17326)
zeripath Oct 17, 2021
633b041
make mirroring a process
zeripath Oct 17, 2021
377a384
Ensure that mirrors are al within the same context
zeripath Oct 17, 2021
32c58ee
Merge remote-tracking branch 'origin/main' into request-as-process
zeripath Oct 17, 2021
71dec99
add clarity to the difference between cancel and remove
zeripath Oct 19, 2021
9e95fdb
add explanatory notes for remove and close
zeripath Oct 19, 2021
3010f59
Merge remote-tracking branch 'origin/main' into request-as-process
zeripath Oct 19, 2021
9cc97d0
explicitly name the arguments in the blame reader
zeripath Oct 19, 2021
e4aebfb
Change remove to finished
zeripath Oct 20, 2021
217fbf7
update blame documentation
zeripath Oct 20, 2021
6b6ac80
as per review
zeripath Oct 20, 2021
0fcbc38
Merge remote-tracking branch 'origin/main' into request-as-process
zeripath Oct 27, 2021
e06216b
Close the cat-file batch and checks after the context cancellation
zeripath Oct 27, 2021
2062e43
Merge remote-tracking branch 'origin/main' into request-as-process
zeripath Oct 28, 2021
46c2b7a
Merge branch 'main' into request-as-process
zeripath Nov 1, 2021
37bfa14
Merge branch 'main' into request-as-process
zeripath Nov 5, 2021
ad2e278
Merge remote-tracking branch 'origin/main' into request-as-process
zeripath Nov 16, 2021
6062b04
Merge remote-tracking branch 'origin/main' into request-as-process
zeripath Nov 20, 2021
59dc919
Merge remote-tracking branch 'origin/main' into request-as-process
zeripath Nov 26, 2021
347e6a8
Merge branch 'main' into request-as-process
lafriks Nov 28, 2021
fd86412
Merge branch 'main' into request-as-process
lunny Nov 28, 2021
0d1ae72
Merge remote-tracking branch 'origin/main' into request-as-process
zeripath Nov 28, 2021
1d565bb
Merge branch 'main' into request-as-process
zeripath Nov 29, 2021
bbe69c8
Merge branch 'main' into request-as-process
zeripath Nov 30, 2021
1203fa9
Ensure that http requests use the same context as the request
zeripath Nov 30, 2021
772d31d
use the repo context in the diff
zeripath Nov 30, 2021
37f0716
improve code documentation
zeripath Nov 30, 2021
ec6b663
use the gitrepo context
zeripath Nov 30, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify PID to strings using the time of start plus/minus a counter
Signed-off-by: Andrew Thornton <[email protected]>
  • Loading branch information
zeripath committed Oct 15, 2021
commit 9bb820bb2d337941bea33314eb1bd026b8d0cf5a
12 changes: 6 additions & 6 deletions modules/process/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
// Context is a wrapper around context.Context for having the current pid for this context
type Context struct {
context.Context
pid int64
pid IDType
}

// GetPID returns the PID for this context
func (c *Context) GetPID() int64 {
func (c *Context) GetPID() IDType {
return c.pid
}

Expand Down Expand Up @@ -51,17 +51,17 @@ func GetContext(ctx context.Context) *Context {
}

// GetPID returns the PID for this context
func GetPID(ctx context.Context) int64 {
func GetPID(ctx context.Context) IDType {
pCtx := GetContext(ctx)
if pCtx == nil {
return 0
return ""
}
return pCtx.GetPID()
}

// GetParentPID returns the ParentPID for this context
func GetParentPID(ctx context.Context) int64 {
parentPID := int64(0)
func GetParentPID(ctx context.Context) IDType {
var parentPID IDType
if parentProcess := GetContext(ctx); parentProcess != nil {
parentPID = parentProcess.GetPID()
}
Expand Down
78 changes: 31 additions & 47 deletions modules/process/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io"
"os/exec"
"sort"
"strconv"
"sync"
"time"
)
Expand All @@ -26,18 +27,15 @@ var (

// DefaultContext is the default context to run processing commands in
DefaultContext = context.Background()

// RecyclePID is the PID number at which we will attempt to recycle PIDs
RecyclePID int64 = 1 << 16

// HuntSize is the size of the hunt for the lowest free PID
HuntSize int64 = 512
)

// IDType is a pid type
type IDType string

// Process represents a working process inheriting from Gitea.
type Process struct {
PID int64 // Process ID, not system one.
ParentPID int64
PID IDType // Process ID, not system one.
ParentPID IDType
children []*Process
Description string
Start time.Time
Expand All @@ -59,19 +57,18 @@ func (p *Process) Children() []*Process {
type Manager struct {
mutex sync.Mutex

next int64
low int64
next int64
lastTime time.Time

processes map[int64]*Process
processes map[IDType]*Process
}

// GetManager returns a Manager and initializes one as singleton if there's none yet
func GetManager() *Manager {
managerInit.Do(func() {
manager = &Manager{
processes: make(map[int64]*Process),
processes: make(map[IDType]*Process),
next: 1,
low: 1,
}
})
return manager
Expand Down Expand Up @@ -106,20 +103,20 @@ func (pm *Manager) AddContextTimeout(parent context.Context, timeout time.Durati
}

// Add create a new process
func (pm *Manager) Add(parentPID int64, description string, cancel context.CancelFunc) (int64, context.CancelFunc) {
func (pm *Manager) Add(parentPID IDType, description string, cancel context.CancelFunc) (IDType, context.CancelFunc) {
pm.mutex.Lock()
pid := pm.nextPID()
start, pid := pm.nextPID()

parent := pm.processes[parentPID]
if parent == nil {
parentPID = 0
parentPID = ""
}

process := &Process{
PID: pid,
ParentPID: parentPID,
Description: description,
Start: time.Now(),
Start: start,
Cancel: cancel,
}

Expand All @@ -138,38 +135,26 @@ func (pm *Manager) Add(parentPID int64, description string, cancel context.Cance
}

// nextPID will return the next available PID. pm.mutex should already be locked.
func (pm *Manager) nextPID() int64 {
if pm.next > RecyclePID {
for i := int64(0); i < HuntSize; i++ {
if pm.low >= pm.next {
pm.low = 1
break
}
if _, ok := pm.processes[pm.low]; !ok {
next := pm.low
pm.low++
return next
}
pm.low++
}
func (pm *Manager) nextPID() (start time.Time, pid IDType) {
start = time.Now()
if pm.lastTime == start {
pm.next++
} else {
pm.next = 1
}
next := pm.next
pm.next++
return next
}
pid = IDType(strconv.FormatInt(start.Unix(), 16))

// releasePID will release the PID. pm.mutex should already be locked.
func (pm *Manager) releasePID(pid int64) {
if pid < pm.low+RecyclePID {
pm.low = pid
if pm.next == 1 {
return
}
pid += IDType("-" + strconv.FormatInt(pm.next, 10))
return
}

// Remove a process from the ProcessManager.
func (pm *Manager) Remove(pid int64) {
func (pm *Manager) Remove(pid IDType) {
pm.mutex.Lock()
delete(pm.processes, pid)
pm.releasePID(pid)
pm.mutex.Unlock()
}

Expand All @@ -178,9 +163,8 @@ func (pm *Manager) remove(process *Process) {
defer pm.mutex.Unlock()
if p := pm.processes[process.PID]; p == process {
delete(pm.processes, process.PID)
pm.releasePID(process.PID)
for _, child := range process.children {
child.ParentPID = 0
child.ParentPID = ""
}
parent := pm.processes[process.ParentPID]
if parent != nil {
Expand All @@ -195,7 +179,7 @@ func (pm *Manager) remove(process *Process) {
}

// Cancel a process in the ProcessManager.
func (pm *Manager) Cancel(pid int64) {
func (pm *Manager) Cancel(pid IDType) {
pm.mutex.Lock()
process, ok := pm.processes[pid]
pm.mutex.Unlock()
Expand All @@ -210,7 +194,7 @@ func (pm *Manager) Processes(onlyRoots bool) []*Process {
processes := make([]*Process, 0, len(pm.processes))
if onlyRoots {
for _, process := range pm.processes {
if process.ParentPID == 0 {
if process.ParentPID == "" {
processes = append(processes, process)
}
}
Expand Down Expand Up @@ -299,7 +283,7 @@ func (pm *Manager) ExecDirEnvStdIn(timeout time.Duration, dir, desc string, env

// Error is a wrapped error describing the error results of Process Execution
type Error struct {
PID int64
PID IDType
Description string
Err error
CtxErr error
Expand All @@ -308,7 +292,7 @@ type Error struct {
}

func (err *Error) Error() string {
return fmt.Sprintf("exec(%d:%s) failed: %v(%v) stdout: %s stderr: %s", err.PID, err.Description, err.Err, err.CtxErr, err.Stdout, err.Stderr)
return fmt.Sprintf("exec(%s:%s) failed: %v(%v) stdout: %s stderr: %s", err.PID, err.Description, err.Err, err.CtxErr, err.Stdout, err.Stderr)
}

// Unwrap implements the unwrappable implicit interface for go1.13 Unwrap()
Expand Down
6 changes: 3 additions & 3 deletions modules/process/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestGetManager(t *testing.T) {
}

func TestManager_AddContext(t *testing.T) {
pm := Manager{processes: make(map[int64]*Process), next: 1, low: 1}
pm := Manager{processes: make(map[IDType]*Process), next: 1}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -39,7 +39,7 @@ func TestManager_AddContext(t *testing.T) {
}

func TestManager_Cancel(t *testing.T) {
pm := Manager{processes: make(map[int64]*Process), next: 1, low: 1}
pm := Manager{processes: make(map[IDType]*Process), next: 1}

ctx, _, remove := pm.AddContext(context.Background(), "foo")
defer remove()
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestManager_Cancel(t *testing.T) {
}

func TestManager_Remove(t *testing.T) {
pm := Manager{processes: make(map[int64]*Process), next: 1, low: 1}
pm := Manager{processes: make(map[IDType]*Process), next: 1}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
4 changes: 2 additions & 2 deletions routers/web/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ func Monitor(ctx *context.Context) {

// MonitorCancel cancels a process
func MonitorCancel(ctx *context.Context) {
pid := ctx.ParamsInt64("pid")
process.GetManager().Cancel(pid)
pid := ctx.Params("pid")
process.GetManager().Cancel(process.IDType(pid))
ctx.JSON(http.StatusOK, map[string]interface{}{
"redirect": setting.AppSubURL + "/admin/monitor",
})
Expand Down