Skip to content

Commit

Permalink
[vm] delete vm intance cache (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
huangzhiran authored Jun 12, 2024
1 parent bd5bb31 commit 5dec44c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 90 deletions.
13 changes: 6 additions & 7 deletions vm/server.go → vm/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import (

type instance struct {
conn *grpc.ClientConn
resp *proto.CreateResponse
}

func newInstance(ctx context.Context, endpoint string, projectID uint64, executeBinary string, expParam string) (*instance, error) {
func newInstance(ctx context.Context, projectID uint64, endpoint, executeBinary, expParam string) (*instance, error) {
conn, err := grpc.Dial(endpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, errors.Wrap(err, "failed to dial vm server")
Expand All @@ -29,11 +28,10 @@ func newInstance(ctx context.Context, endpoint string, projectID uint64, execute
Content: executeBinary,
ExpParam: expParam,
}
resp, err := cli.Create(ctx, req)
if err != nil {
if _, err := cli.Create(ctx, req); err != nil {
return nil, errors.Wrap(err, "failed to create vm instance")
}
return &instance{conn: conn, resp: resp}, nil
return &instance{conn: conn}, nil
}

func (i *instance) execute(ctx context.Context, task *task.Task) ([]byte, error) {
Expand All @@ -51,12 +49,13 @@ func (i *instance) execute(ctx context.Context, task *task.Task) ([]byte, error)
cli := proto.NewVmRuntimeClient(i.conn)
resp, err := cli.ExecuteOperator(ctx, req)
if err != nil {
slog.Debug("request", "body", req)
return nil, errors.Wrap(err, "failed to execute vm instance")
}
return resp.Result, nil
}

func (i *instance) release() {
i.conn.Close()
if err := i.conn.Close(); err != nil {
slog.Error("failed to close grpc conn", "error", err)
}
}
6 changes: 3 additions & 3 deletions vm/server_test.go → vm/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestNewInstance(t *testing.T) {
defer p.Reset()

p.ApplyFuncReturn(grpc.Dial, nil, errors.New(t.Name()))
_, err := newInstance(context.Background(), "any", 100, "any", "any")
_, err := newInstance(context.Background(), 100, "any", "any", "any")
r.ErrorContains(err, t.Name())
})

Expand All @@ -43,7 +43,7 @@ func TestNewInstance(t *testing.T) {
p.ApplyFuncReturn(proto.NewVmRuntimeClient, &MockClient{})
p.ApplyMethodReturn(&MockClient{}, "Create", nil, errors.New(t.Name()))

_, err := newInstance(context.Background(), "any", 100, "any", "any")
_, err := newInstance(context.Background(), 100, "any", "any", "any")
r.ErrorContains(err, t.Name())
})

Expand All @@ -56,7 +56,7 @@ func TestNewInstance(t *testing.T) {
p.ApplyMethodReturn(&MockClient{}, "Create", &proto.CreateResponse{}, nil)
p.ApplyMethodReturn(&grpc.ClientConn{}, "Close", nil)

i, err := newInstance(context.Background(), "any", 100, "any", "any")
i, err := newInstance(context.Background(), 100, "any", "any", "any")
r.NoError(err, t.Name())
r.NotNil(i)
i.release()
Expand Down
35 changes: 0 additions & 35 deletions vm/manager.go

This file was deleted.

26 changes: 0 additions & 26 deletions vm/manager_test.go

This file was deleted.

11 changes: 4 additions & 7 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package vm

import (
"context"
"fmt"
"log/slog"

"github.com/pkg/errors"
Expand All @@ -21,7 +20,6 @@ const (

type Handler struct {
vmServerEndpoints map[Type]string
instanceManager *manager
}

func (r *Handler) Handle(task *task.Task, vmtype Type, code string, expParam string) ([]byte, error) {
Expand All @@ -30,12 +28,12 @@ func (r *Handler) Handle(task *task.Task, vmtype Type, code string, expParam str
return nil, errors.New("unsupported vm type")
}

ins, err := r.instanceManager.acquire(task.ProjectID, endpoint, code, expParam)
ins, err := newInstance(context.Background(), task.ProjectID, endpoint, code, expParam)
if err != nil {
return nil, errors.Wrap(err, "failed to get instance")
return nil, errors.Wrap(err, "failed to new instance")
}
slog.Debug(fmt.Sprintf("acquire %s instance success", vmtype))
defer r.instanceManager.release(task.ProjectID, ins)
defer ins.release()
slog.Debug("acquire vm instance success", "vm_type", vmtype)

res, err := ins.execute(context.Background(), task)
if err != nil {
Expand All @@ -47,6 +45,5 @@ func (r *Handler) Handle(task *task.Task, vmtype Type, code string, expParam str
func NewHandler(vmServerEndpoints map[Type]string) *Handler {
return &Handler{
vmServerEndpoints: vmServerEndpoints,
instanceManager: newManager(),
}
}
18 changes: 6 additions & 12 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ func TestHandler_Handle(t *testing.T) {
r.Error(err)
})

t.Run("FailedToAcquireVmInstance", func(t *testing.T) {
t.Run("FailedToNewVmInstance", func(t *testing.T) {
p := gomonkey.NewPatches()
defer p.Reset()

p.ApplyPrivateMethod(&manager{}, "acquire", func(uint64, string, string, string) (*instance, error) {
return nil, errors.New(t.Name())
})
p.ApplyFuncReturn(newInstance, nil, errors.New(t.Name()))
_, err := h.Handle(&task.Task{}, ZKwasm, "any", "any")
r.ErrorContains(err, t.Name())
})
Expand All @@ -48,10 +46,8 @@ func TestHandler_Handle(t *testing.T) {
p := gomonkey.NewPatches()
defer p.Reset()

p.ApplyPrivateMethod(&manager{}, "acquire", func(uint64, string, string, string) (*instance, error) {
return &instance{}, nil
})
p.ApplyPrivateMethod(&manager{}, "release", func(uint64, *instance) {})
p.ApplyFuncReturn(newInstance, &instance{}, nil)
p.ApplyPrivateMethod(&instance{}, "release", func() {})
p.ApplyPrivateMethod(&instance{}, "execute", func(context.Context, *task.Task) ([]byte, error) {
return nil, errors.New(t.Name())
})
Expand All @@ -64,10 +60,8 @@ func TestHandler_Handle(t *testing.T) {
p := gomonkey.NewPatches()
defer p.Reset()

p.ApplyPrivateMethod(&manager{}, "acquire", func(uint64, string, string, string) (*instance, error) {
return &instance{}, nil
})
p.ApplyPrivateMethod(&manager{}, "release", func(uint64, *instance) {})
p.ApplyFuncReturn(newInstance, &instance{}, nil)
p.ApplyPrivateMethod(&instance{}, "release", func() {})
p.ApplyPrivateMethod(&instance{}, "execute", func(context.Context, *task.Task) ([]byte, error) {
return []byte("any"), nil
})
Expand Down

0 comments on commit 5dec44c

Please sign in to comment.