Skip to content
Snippets Groups Projects
Commit d1a79e49 authored by ale's avatar ale
Browse files

Split buildGroupFromRequest() for readability

parent 44d2d719
No related branches found
No related tags found
1 merge request!20Refactor the Engine with stronger guarantees
Pipeline #88166 passed
......@@ -179,11 +179,9 @@ func (e *Engine) CreateGroup(req *CreateGroupRequest) (*CreateGroupResponse, err
}
gctx := newGroupContext(ttl)
tx := newTX()
// Populate the groupContext with the final Group object, and
// register it while we're still holding the lock.
err := e.buildGroupFromRequest(gctx, tx, req)
tx, err := e.createAndBuildGroup(gctx, req)
if err == nil {
if _, ok := e.groups[gctx.group.ID]; ok {
err = errors.New("group already exists")
......@@ -211,13 +209,24 @@ func (e *Engine) CreateGroup(req *CreateGroupRequest) (*CreateGroupResponse, err
}, nil
}
func (e *Engine) buildGroupFromRequest(gctx *groupContext, tx Transaction, req *CreateGroupRequest) error {
// Create a new Group from the CreateGroupRequest, and prepare its
// environment in a new Transaction.
func (e *Engine) createAndBuildGroup(gctx *groupContext, req *CreateGroupRequest) (Transaction, error) {
if err := e.newGroupFromRequest(gctx, req); err != nil {
return nil, err
}
return e.buildTransaction(gctx)
}
// Build a Group from a CreateGroupRequest.
func (e *Engine) newGroupFromRequest(gctx *groupContext, req *CreateGroupRequest) error {
req, err := fillCreateGroupRequest(gctx, req, e.subnets)
if err != nil {
return err
}
// Create the group.
// Create the group and attach it to the groupContext.
group, err := newGroup(req, e.registry)
if err != nil {
return err
......@@ -229,8 +238,7 @@ func (e *Engine) buildGroupFromRequest(gctx *groupContext, tx Transaction, req *
return err
}
// Checking if the memory is available and agreeing to start
// the group should both happen while the lock is held.
// Allocate memory, if we're keeping track of it.
if e.memory != nil {
ram := group.ram()
if !e.memory.Acquire(ram) {
......@@ -241,10 +249,20 @@ func (e *Engine) buildGroupFromRequest(gctx *groupContext, tx Transaction, req *
})
}
// Build the group resources and set up the transaction.
return group.BuildWithProviders(tx, e.net, e.vm)
return nil
}
// Setup the group resources by attaching them to a new Transaction.
func (e *Engine) buildTransaction(gctx *groupContext) (Transaction, error) {
tx := newTX()
err := gctx.group.BuildWithProviders(tx, e.net, e.vm)
return tx, err
}
// Fill in missing values in the CreateGroupRequest, possibly by
// allocating shared resources such as subnets, IP addresses, etc.
func fillCreateGroupRequest(gctx *groupContext, req *CreateGroupRequest, netAlloc *allocator.SubnetAllocator) (*CreateGroupRequest, error) {
var ipnet *net.IPNet
var err error
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment