-
Notifications
You must be signed in to change notification settings - Fork 42
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
refactor wal stage 1 #561
refactor wal stage 1 #561
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,23 +22,21 @@ const ( | |
// WAL for calcium. | ||
type WAL struct { | ||
wal.WAL | ||
config types.Config | ||
calcium *Calcium | ||
} | ||
|
||
func newCalciumWAL(cal *Calcium) (*WAL, error) { | ||
w := &WAL{ | ||
WAL: wal.NewHydro(), | ||
config: cal.config, | ||
calcium: cal, | ||
func newWAL(config types.Config, calcium *Calcium) (*WAL, error) { | ||
hydro, err := wal.NewHydro(config.WALFile, config.WALOpenTimeout) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := w.WAL.Open(w.config.WALFile, w.config.WALOpenTimeout); err != nil { | ||
return nil, err | ||
w := &WAL{ | ||
WAL: hydro, | ||
calcium: calcium, | ||
} | ||
|
||
w.registerHandlers() | ||
|
||
return w, nil | ||
} | ||
|
||
|
@@ -49,26 +47,92 @@ func (w *WAL) registerHandlers() { | |
w.Register(newProcessingCreatedHandler(w.calcium)) | ||
} | ||
|
||
func (w *WAL) logCreateLambda(opts *types.CreateWorkloadMessage) (wal.Commit, error) { | ||
return w.Log(eventCreateLambda, opts.WorkloadID) | ||
// CreateLambdaHandler indicates event handler for creating lambda. | ||
type CreateLambdaHandler struct { | ||
typ string | ||
calcium *Calcium | ||
} | ||
|
||
func newCreateLambdaHandler(calcium *Calcium) *CreateLambdaHandler { | ||
return &CreateLambdaHandler{ | ||
typ: eventCreateLambda, | ||
calcium: calcium, | ||
} | ||
} | ||
|
||
// Event . | ||
func (h *CreateLambdaHandler) Typ() string { | ||
return h.typ | ||
} | ||
|
||
// Check . | ||
func (h *CreateLambdaHandler) Check(context.Context, interface{}) (bool, error) { | ||
return true, nil | ||
} | ||
|
||
// Encode . | ||
func (h *CreateLambdaHandler) Encode(raw interface{}) ([]byte, error) { | ||
workloadID, ok := raw.(string) | ||
if !ok { | ||
return nil, types.NewDetailedErr(types.ErrInvalidType, raw) | ||
} | ||
return []byte(workloadID), nil | ||
} | ||
|
||
// Decode . | ||
func (h *CreateLambdaHandler) Decode(bs []byte) (interface{}, error) { | ||
return string(bs), nil | ||
} | ||
|
||
// Handle . | ||
func (h *CreateLambdaHandler) Handle(ctx context.Context, raw interface{}) error { | ||
workloadID, ok := raw.(string) | ||
if !ok { | ||
return types.NewDetailedErr(types.ErrInvalidType, raw) | ||
} | ||
|
||
logger := log.WithField("WAL.Handle", "RunAndWait").WithField("ID", workloadID) | ||
go func() { | ||
workload, err := h.calcium.GetWorkload(ctx, workloadID) | ||
if err != nil { | ||
logger.Errorf(ctx, "Get workload failed: %v", err) | ||
return | ||
} | ||
|
||
r, err := workload.Engine.VirtualizationWait(ctx, workloadID, "") | ||
if err != nil { | ||
logger.Errorf(ctx, "Wait failed: %+v", err) | ||
return | ||
} | ||
if r.Code != 0 { | ||
logger.Errorf(ctx, "Run failed: %s", r.Message) | ||
} | ||
|
||
if err := h.calcium.doRemoveWorkloadSync(ctx, []string{workloadID}); err != nil { | ||
logger.Errorf(ctx, "Remove failed: %+v", err) | ||
} | ||
logger.Infof(ctx, "waited and removed") | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
// CreateWorkloadHandler indicates event handler for creating workload. | ||
type CreateWorkloadHandler struct { | ||
event string | ||
typ string | ||
calcium *Calcium | ||
} | ||
|
||
func newCreateWorkloadHandler(cal *Calcium) *CreateWorkloadHandler { | ||
func newCreateWorkloadHandler(calcium *Calcium) *CreateWorkloadHandler { | ||
return &CreateWorkloadHandler{ | ||
event: eventWorkloadCreated, | ||
calcium: cal, | ||
typ: eventWorkloadCreated, | ||
calcium: calcium, | ||
} | ||
} | ||
|
||
// Event . | ||
func (h *CreateWorkloadHandler) Event() string { | ||
return h.event | ||
func (h *CreateWorkloadHandler) Typ() string { | ||
return h.typ | ||
} | ||
|
||
// Check . | ||
|
@@ -132,96 +196,22 @@ func (h *CreateWorkloadHandler) Handle(ctx context.Context, raw interface{}) (er | |
return nil | ||
} | ||
|
||
// CreateLambdaHandler indicates event handler for creating lambda. | ||
type CreateLambdaHandler struct { | ||
event string | ||
calcium *Calcium | ||
} | ||
|
||
func newCreateLambdaHandler(cal *Calcium) *CreateLambdaHandler { | ||
return &CreateLambdaHandler{ | ||
event: eventCreateLambda, | ||
calcium: cal, | ||
} | ||
} | ||
|
||
// Event . | ||
func (h *CreateLambdaHandler) Event() string { | ||
return h.event | ||
} | ||
|
||
// Check . | ||
func (h *CreateLambdaHandler) Check(context.Context, interface{}) (bool, error) { | ||
return true, nil | ||
} | ||
|
||
// Encode . | ||
func (h *CreateLambdaHandler) Encode(raw interface{}) ([]byte, error) { | ||
workloadID, ok := raw.(string) | ||
if !ok { | ||
return nil, types.NewDetailedErr(types.ErrInvalidType, raw) | ||
} | ||
return []byte(workloadID), nil | ||
} | ||
|
||
// Decode . | ||
func (h *CreateLambdaHandler) Decode(bs []byte) (interface{}, error) { | ||
return string(bs), nil | ||
} | ||
|
||
// Handle . | ||
func (h *CreateLambdaHandler) Handle(ctx context.Context, raw interface{}) error { | ||
workloadID, ok := raw.(string) | ||
if !ok { | ||
return types.NewDetailedErr(types.ErrInvalidType, raw) | ||
} | ||
|
||
logger := log.WithField("WAL.Handle", "RunAndWait").WithField("ID", workloadID) | ||
go func() { | ||
workload, err := h.calcium.GetWorkload(ctx, workloadID) | ||
if err != nil { | ||
logger.Errorf(ctx, "Get workload failed: %v", err) | ||
return | ||
} | ||
|
||
r, err := workload.Engine.VirtualizationWait(ctx, workloadID, "") | ||
if err != nil { | ||
logger.Errorf(ctx, "Wait failed: %+v", err) | ||
return | ||
} | ||
if r.Code != 0 { | ||
logger.Errorf(ctx, "Run failed: %s", r.Message) | ||
} | ||
|
||
if err := h.calcium.doRemoveWorkloadSync(ctx, []string{workloadID}); err != nil { | ||
logger.Errorf(ctx, "Remove failed: %+v", err) | ||
} | ||
logger.Infof(ctx, "waited and removed") | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
func getReplayContext(ctx context.Context) (context.Context, context.CancelFunc) { | ||
return context.WithTimeout(ctx, time.Second*32) | ||
} | ||
|
||
// WorkloadResourceAllocatedHandler . | ||
type WorkloadResourceAllocatedHandler struct { | ||
event string | ||
typ string | ||
calcium *Calcium | ||
} | ||
|
||
func newWorkloadResourceAllocatedHandler(cal *Calcium) *WorkloadResourceAllocatedHandler { | ||
func newWorkloadResourceAllocatedHandler(calcium *Calcium) *WorkloadResourceAllocatedHandler { | ||
return &WorkloadResourceAllocatedHandler{ | ||
event: eventWorkloadResourceAllocated, | ||
calcium: cal, | ||
typ: eventWorkloadResourceAllocated, | ||
calcium: calcium, | ||
} | ||
} | ||
|
||
// Event . | ||
func (h *WorkloadResourceAllocatedHandler) Event() string { | ||
return h.event | ||
func (h *WorkloadResourceAllocatedHandler) Typ() string { | ||
return h.typ | ||
} | ||
|
||
// Check . | ||
|
@@ -276,20 +266,20 @@ func (h *WorkloadResourceAllocatedHandler) Handle(ctx context.Context, raw inter | |
|
||
// ProcessingCreatedHandler . | ||
type ProcessingCreatedHandler struct { | ||
event string | ||
typ string | ||
calcium *Calcium | ||
} | ||
|
||
func newProcessingCreatedHandler(cal *Calcium) *ProcessingCreatedHandler { | ||
func newProcessingCreatedHandler(calcium *Calcium) *ProcessingCreatedHandler { | ||
return &ProcessingCreatedHandler{ | ||
event: eventProcessingCreated, | ||
calcium: cal, | ||
typ: eventProcessingCreated, | ||
calcium: calcium, | ||
} | ||
} | ||
|
||
// Event . | ||
func (h *ProcessingCreatedHandler) Event() string { | ||
return h.event | ||
func (h *ProcessingCreatedHandler) Typ() string { | ||
return h.typ | ||
} | ||
|
||
// Check . | ||
|
@@ -329,3 +319,7 @@ func (h *ProcessingCreatedHandler) Handle(ctx context.Context, raw interface{}) | |
logger.Infof(ctx, "obsolete processing deleted") | ||
return | ||
} | ||
|
||
func getReplayContext(ctx context.Context) (context.Context, context.CancelFunc) { | ||
return context.WithTimeout(ctx, time.Second*32) // TODO why 32? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ctx这块确实有点乱了,以后要花点精力重整一下... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这种我都已经……弃疗了,你整理下吧 |
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
打错了...