Skip to content

Commit 5dec44c

Browse files
authored
[vm] delete vm intance cache (#544)
1 parent bd5bb31 commit 5dec44c

File tree

6 files changed

+19
-90
lines changed

6 files changed

+19
-90
lines changed

vm/server.go renamed to vm/instance.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ import (
1414

1515
type instance struct {
1616
conn *grpc.ClientConn
17-
resp *proto.CreateResponse
1817
}
1918

20-
func newInstance(ctx context.Context, endpoint string, projectID uint64, executeBinary string, expParam string) (*instance, error) {
19+
func newInstance(ctx context.Context, projectID uint64, endpoint, executeBinary, expParam string) (*instance, error) {
2120
conn, err := grpc.Dial(endpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
2221
if err != nil {
2322
return nil, errors.Wrap(err, "failed to dial vm server")
@@ -29,11 +28,10 @@ func newInstance(ctx context.Context, endpoint string, projectID uint64, execute
2928
Content: executeBinary,
3029
ExpParam: expParam,
3130
}
32-
resp, err := cli.Create(ctx, req)
33-
if err != nil {
31+
if _, err := cli.Create(ctx, req); err != nil {
3432
return nil, errors.Wrap(err, "failed to create vm instance")
3533
}
36-
return &instance{conn: conn, resp: resp}, nil
34+
return &instance{conn: conn}, nil
3735
}
3836

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

6057
func (i *instance) release() {
61-
i.conn.Close()
58+
if err := i.conn.Close(); err != nil {
59+
slog.Error("failed to close grpc conn", "error", err)
60+
}
6261
}

vm/server_test.go renamed to vm/instance_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestNewInstance(t *testing.T) {
3131
defer p.Reset()
3232

3333
p.ApplyFuncReturn(grpc.Dial, nil, errors.New(t.Name()))
34-
_, err := newInstance(context.Background(), "any", 100, "any", "any")
34+
_, err := newInstance(context.Background(), 100, "any", "any", "any")
3535
r.ErrorContains(err, t.Name())
3636
})
3737

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

46-
_, err := newInstance(context.Background(), "any", 100, "any", "any")
46+
_, err := newInstance(context.Background(), 100, "any", "any", "any")
4747
r.ErrorContains(err, t.Name())
4848
})
4949

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

59-
i, err := newInstance(context.Background(), "any", 100, "any", "any")
59+
i, err := newInstance(context.Background(), 100, "any", "any", "any")
6060
r.NoError(err, t.Name())
6161
r.NotNil(i)
6262
i.release()

vm/manager.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

vm/manager_test.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

vm/vm.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package vm
22

33
import (
44
"context"
5-
"fmt"
65
"log/slog"
76

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

2221
type Handler struct {
2322
vmServerEndpoints map[Type]string
24-
instanceManager *manager
2523
}
2624

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

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

4038
res, err := ins.execute(context.Background(), task)
4139
if err != nil {
@@ -47,6 +45,5 @@ func (r *Handler) Handle(task *task.Task, vmtype Type, code string, expParam str
4745
func NewHandler(vmServerEndpoints map[Type]string) *Handler {
4846
return &Handler{
4947
vmServerEndpoints: vmServerEndpoints,
50-
instanceManager: newManager(),
5148
}
5249
}

vm/vm_test.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@ func TestHandler_Handle(t *testing.T) {
3333
r.Error(err)
3434
})
3535

36-
t.Run("FailedToAcquireVmInstance", func(t *testing.T) {
36+
t.Run("FailedToNewVmInstance", func(t *testing.T) {
3737
p := gomonkey.NewPatches()
3838
defer p.Reset()
3939

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

51-
p.ApplyPrivateMethod(&manager{}, "acquire", func(uint64, string, string, string) (*instance, error) {
52-
return &instance{}, nil
53-
})
54-
p.ApplyPrivateMethod(&manager{}, "release", func(uint64, *instance) {})
49+
p.ApplyFuncReturn(newInstance, &instance{}, nil)
50+
p.ApplyPrivateMethod(&instance{}, "release", func() {})
5551
p.ApplyPrivateMethod(&instance{}, "execute", func(context.Context, *task.Task) ([]byte, error) {
5652
return nil, errors.New(t.Name())
5753
})
@@ -64,10 +60,8 @@ func TestHandler_Handle(t *testing.T) {
6460
p := gomonkey.NewPatches()
6561
defer p.Reset()
6662

67-
p.ApplyPrivateMethod(&manager{}, "acquire", func(uint64, string, string, string) (*instance, error) {
68-
return &instance{}, nil
69-
})
70-
p.ApplyPrivateMethod(&manager{}, "release", func(uint64, *instance) {})
63+
p.ApplyFuncReturn(newInstance, &instance{}, nil)
64+
p.ApplyPrivateMethod(&instance{}, "release", func() {})
7165
p.ApplyPrivateMethod(&instance{}, "execute", func(context.Context, *task.Task) ([]byte, error) {
7266
return []byte("any"), nil
7367
})

0 commit comments

Comments
 (0)