Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Race conditions #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 16 additions & 15 deletions pkg/runtime/job.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runtime

import (
"sync/atomic"
"time"
)

Expand All @@ -14,21 +15,21 @@ const (
)

type Job struct {
Handle string `json:"job_handle,omitempty"` //server job handle
Id string `json:"id,omitempty"`
Data []byte `json:"data,omitempty"`
Running bool `json:"is_running,omitempty"`
Percent int `json:"percent,omitempty"`
Denominator int `json:"denominator,omitempty"`
CreateAt time.Time `json:"created_at,omitempty"`
ProcessAt time.Time `json:"process_at,omitempty"`
TimeoutSec int32 `json:"timeout_sec,omitempty"`
CreateBy int64 `json:"created_by,omitempty"` //client sessionId
ProcessBy int64 `json:"process_by,omitempty"` //worker sessionId
FuncName string `json:"function_name,omitempty"`
IsBackGround bool `json:"is_background_job"`
Priority int `json:"priority"`
CronHandle string `json:"cronjob_handle,omitempty"`
Handle string `json:"job_handle,omitempty"` //server job handle
Id string `json:"id,omitempty"`
Data []byte `json:"data,omitempty"`
Running atomic.Bool `json:"is_running,omitempty"`
Percent int `json:"percent,omitempty"`
Denominator int `json:"denominator,omitempty"`
CreateAt time.Time `json:"created_at,omitempty"`
ProcessAt time.Time `json:"process_at,omitempty"`
TimeoutSec int32 `json:"timeout_sec,omitempty"`
CreateBy int64 `json:"created_by,omitempty"` //client sessionId
ProcessBy int64 `json:"process_by,omitempty"` //worker sessionId
FuncName string `json:"function_name,omitempty"`
IsBackGround bool `json:"is_background_job"`
Priority int `json:"priority"`
CronHandle string `json:"cronjob_handle,omitempty"`
}

type CronJob struct {
Expand Down
91 changes: 62 additions & 29 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"sync/atomic"
"time"

"github.com/appscode/go/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/quantcast/g2/pkg/metrics"
. "github.com/quantcast/g2/pkg/runtime"
"github.com/quantcast/g2/pkg/storage"
"github.com/quantcast/g2/pkg/storage/leveldb"
"github.com/appscode/go/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
lberror "github.com/syndtr/goleveldb/leveldb/errors"
"gopkg.in/robfig/cron.v2"
)
Expand All @@ -45,7 +45,8 @@ type Server struct {
forwardReport int64
cronSvc *cron.Cron
cronJobs map[string]*CronJob
mu *sync.RWMutex
cronMu *sync.RWMutex
jobMu *sync.RWMutex
}

var ( //const replys, to avoid building it every time
Expand All @@ -65,7 +66,8 @@ func NewServer(cfg Config) *Server {
opCounter: make(map[PT]int64),
cronSvc: cron.New(),
cronJobs: make(map[string]*CronJob),
mu: &sync.RWMutex{},
cronMu: &sync.RWMutex{},
jobMu: &sync.RWMutex{},
}

// Initiate data storage
Expand Down Expand Up @@ -94,8 +96,8 @@ func (s *Server) loadAllJobs() {
log.Errorln("invalid job")
continue
}
j.ProcessBy = 0 //no body handle it now
j.CreateBy = 0 //clear
atomic.StoreInt64(&j.ProcessBy, 0) //no body handle it now
atomic.StoreInt64(&j.CreateBy, 0) //clear
log.Debugf("handle: %v\tfunc: %v\tis_background: %v", j.Handle, j.FuncName, j.IsBackGround)
s.doAddJob(j)
}
Expand Down Expand Up @@ -225,9 +227,13 @@ func (s *Server) add2JobWorkerQueue(j *Job) {
}

func (s *Server) doAddJob(j *Job) {
s.jobMu.Lock()
s.jobs[j.Handle] = j
s.jobMu.Unlock()

j.ProcessBy = 0 //nobody handle it right now
s.add2JobWorkerQueue(j)
s.jobs[j.Handle] = j

s.wakeupWorker(j.FuncName)
if cron, ok := s.getCronJobFromMap(j.CronHandle); ok {
cron.Created++
Expand Down Expand Up @@ -313,9 +319,10 @@ func (s *Server) popJob(sessionId int64) (j *Job) {
for it := wj.jobs.Front(); it != nil; it = it.Next() {
jtmp := it.Value.(*Job)
//Don't return running job. This case arise when server restarted but some job still executing
if jtmp.Running {
if jtmp.Running.Load() {
continue
}

j = jtmp
wj.jobs.Remove(it)
return
Expand All @@ -334,7 +341,7 @@ func (s *Server) wakeupWorker(funcName string) bool {
var allRunning = true
for it := wj.jobs.Front(); it != nil; it = it.Next() {
j := it.Value.(*Job)
if !j.Running {
if !j.Running.Load() {
allRunning = false
break
}
Expand All @@ -358,11 +365,16 @@ func (s *Server) wakeupWorker(funcName string) bool {
}

func (s *Server) removeJob(j *Job, isSuccess bool) {
s.jobMu.Lock()
delete(s.jobs, j.Handle)
s.jobMu.Unlock()

if pw, found := s.worker[j.ProcessBy]; found {
delete(pw.runningJobs, j.Handle)
}

log.Debugf("job removed: %v", j.Handle)

if j.IsBackGround {
if cron, ok := s.getCronJobFromMap(j.CronHandle); ok {
if isSuccess {
Expand Down Expand Up @@ -471,9 +483,12 @@ func (s *Server) handleGetJob(e *event) (err error) {

if len(e.handle) == 0 {
jobs := []*Job{}
s.jobMu.RLock()
for _, v := range s.jobs {
jobs = append(jobs, v)
}
s.jobMu.RUnlock()

buf, err = json.Marshal(jobs)
if err != nil {
log.Error(err)
Expand All @@ -482,6 +497,9 @@ func (s *Server) handleGetJob(e *event) (err error) {
return nil
}

s.jobMu.RLock()
defer s.jobMu.RUnlock()

if job, ok := s.jobs[e.handle]; ok {
buf, err = json.Marshal(job)
if err != nil {
Expand All @@ -504,11 +522,11 @@ func (s *Server) handleGetCronJob(e *event) (err error) {
if len(e.handle) == 0 {
var cjs []*CronJob = make([]*CronJob, 0)

s.mu.RLock()
s.cronMu.RLock()
for _, v := range s.cronJobs {
cjs = append(cjs, v)
}
s.mu.RUnlock()
s.cronMu.RUnlock()

buf, err = json.Marshal(cjs)
if err != nil {
Expand Down Expand Up @@ -640,8 +658,10 @@ func (s *Server) handleWorkReport(e *event) {

j, ok := s.getRunningJobByHandle(jobhandle)
if !ok {
s.jobMu.RLock()
log.Warningf("job information lost, %v job handle %v, %+v",
e.tp, jobhandle, s.jobs)
s.jobMu.RUnlock()
return
}

Expand Down Expand Up @@ -733,10 +753,9 @@ func (s *Server) handleProtoEvt(e *event) {
j.ProcessBy = sessionId
j.TimeoutSec = w.canDo[j.FuncName]
//track this job
j.Running = true
j.Running.Store(true)
w.runningJobs[j.Handle] = j
s.saveJobInDB(j)

} else { //no job
w.status = wsPrepareForSleep
}
Expand Down Expand Up @@ -767,8 +786,13 @@ func (s *Server) handleProtoEvt(e *event) {
s.handleSubmitEpochJob(e)
case PT_GetStatus:
jobhandle := bytes2str(args.t0)
if job, ok := s.jobs[jobhandle]; ok {
e.result <- &Tuple{t0: args.t0, t1: true, t2: job.Running,

s.jobMu.RLock()
job, ok := s.jobs[jobhandle]
s.jobMu.RUnlock()

if ok {
e.result <- &Tuple{t0: args.t0, t1: true, t2: job.Running.Load(),
t3: job.Percent, t4: job.Denominator}
break
}
Expand Down Expand Up @@ -810,7 +834,7 @@ func (s *Server) WatcherLoop() {
rep := 0
one := 0

s.mu.RLock()
s.cronMu.RLock()
cLen := len(s.cronJobs)
for _, cj := range s.cronJobs {
if _, isOne := s.ExpressionToEpoch(cj.Expression); isOne {
Expand All @@ -819,27 +843,33 @@ func (s *Server) WatcherLoop() {
rep++
}
}
s.mu.RUnlock()
s.cronMu.RUnlock()

log.Infof("total cron job: %v #repeated job: %v #onetime job: %v", cLen, rep, one)
var b, r int = 0, 0

s.jobMu.RLock()
jobLen := len(s.jobs)
for _, j := range s.jobs {
if j.IsBackGround {
b++
}
if j.Running {
if j.Running.Load() {
r++
}
}
log.Infof("total job: %v #background: %v #running: %v", len(s.jobs), b, r)
s.jobMu.RUnlock()

log.Infof("total job: %v #background: %v #running: %v", jobLen, b, r)
}
}
}

func (s *Server) WatchJobTimeout() {
for range time.NewTicker(time.Second).C {
s.jobMu.RLock()
for _, job := range s.jobs {
if !job.Running {
if !job.Running.Load() {
continue
}
if time.Now().Sub(job.ProcessAt) > time.Duration(job.TimeoutSec)*time.Second {
Expand All @@ -857,8 +887,8 @@ func (s *Server) WatchJobTimeout() {
sendTimeoutException(c.in, job.Handle, "timeout expired")
s.forwardReport++
}

}
s.jobMu.RUnlock()
}
}

Expand Down Expand Up @@ -898,8 +928,11 @@ func (s *Server) isWorker(sessionId int64) bool {
}

func (s *Server) getRunningJobByHandle(handle string) (*Job, bool) {
s.jobMu.RLock()
defer s.jobMu.RUnlock()

for _, j := range s.jobs {
if j.Handle == handle && j.Running {
if j.Handle == handle && j.Running.Load() {
return j, true
}
}
Expand All @@ -916,9 +949,9 @@ func (s *Server) isClient(sessionId int64) bool {
}

func (s *Server) addCronJob(cj *CronJob) {
s.mu.Lock()
s.cronMu.Lock()
s.cronJobs[cj.Handle] = cj
s.mu.Unlock()
s.cronMu.Unlock()

if s.store != nil {
err := s.store.Add(cj)
Expand All @@ -938,9 +971,9 @@ func (s *Server) saveJobInDB(j *Job) {
}

func (s *Server) removeCronJob(cj *CronJob) error {
s.mu.Lock()
s.cronMu.Lock()
delete(s.cronJobs, cj.Handle)
s.mu.Unlock()
s.cronMu.Unlock()

if s.store != nil {
err := s.store.Delete(cj)
Expand All @@ -956,8 +989,8 @@ func (s *Server) removeCronJob(cj *CronJob) error {
}

func (s *Server) getCronJobFromMap(handle string) (cronJob *CronJob, ok bool) {
s.mu.RLock()
defer s.mu.RUnlock()
s.cronMu.RLock()
defer s.cronMu.RUnlock()
cronJob, ok = s.cronJobs[handle]
return
}
4 changes: 2 additions & 2 deletions pkg/server/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (se *session) handleAdminConnection(s *Server, conn net.Conn, r *bufio.Read
for fnName, v := range s.funcWorker {
runningCnt := 0
for _, j := range s.jobs {
if fnName == j.FuncName && j.Running {
if fnName == j.FuncName && j.Running.Load() {
runningCnt++
}
}
Expand All @@ -228,7 +228,7 @@ func (se *session) handleAdminConnection(s *Server, conn net.Conn, r *bufio.Read
normal := 0
low := 0
for _, j := range s.jobs {
if fnName == j.FuncName && !j.Running {
if fnName == j.FuncName && !j.Running.Load() {
switch j.Priority {
case JobHigh:
high++
Expand Down
Loading