Repository: mosn/layotto
Pull request #1022 (2023-12-11 07:48:29)

feat: Add ctx for lock and unlock interface
<!--  Thanks for sending a pull request!
Read contributing.md before commit pull request.
-->

**What this PR does**:

Add ctx for lock and unlock interface

**Which issue(s) this PR fixes**:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
-->
Fixes #

**Special notes for your reviewer**:

**Does this PR introduce a user-facing change?**:
<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".
-->
```release-note

```

github-actions[bot]: Hi @wenxuwan. Thanks for your PR! 🎉
If the PR is ready, use the `/cc` command to assign reviewer to review. 

<details>
  <summary>Details</summary>

The full list of commands accepted by this bot can be found [here](https://github.com/Xunzhuo/prowox/blob/main/COMMAND.md).

The pull request process is described [here](https://github.com/Xunzhuo/prowox/blob/main/PROCESS.md).

</details>

Patch:
diff --git a/components/lock/consul/consul_lock.go b/components/lock/consul/consul_lock.go
index 439cef06..5547978c 100644
--- a/components/lock/consul/consul_lock.go
+++ b/components/lock/consul/consul_lock.go
@@ -78,7 +78,7 @@ func getTTL(expire int32) string {
 	return strconv.Itoa(int(expire)) + "s"
 }
 
-func (c *ConsulLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
+func (c *ConsulLock) TryLock(ctx context.Context, req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
 
 	// create a session TTL
 	session, _, err := c.sessionFactory.Create(&api.SessionEntry{
@@ -112,7 +112,7 @@ func (c *ConsulLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse, e
 		Success: false,
 	}, nil
 }
-func (c *ConsulLock) Unlock(req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
+func (c *ConsulLock) Unlock(ctx context.Context, req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
 
 	session, ok := c.sMap.Load(req.LockOwner + "-" + req.ResourceId)
 
diff --git a/components/lock/etcd/etcd_lock.go b/components/lock/etcd/etcd_lock.go
index b0ea787d..663c5749 100644
--- a/components/lock/etcd/etcd_lock.go
+++ b/components/lock/etcd/etcd_lock.go
@@ -77,7 +77,7 @@ func (e *EtcdLock) Features() []lock.Feature {
 }
 
 // Node tries to acquire a etcd lock
-func (e *EtcdLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
+func (e *EtcdLock) TryLock(ctx context.Context, req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
 	var leaseId clientv3.LeaseID
 	//1.Create new lease
 	lease := clientv3.NewLease(e.client)
@@ -108,7 +108,7 @@ func (e *EtcdLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse, err
 }
 
 // Node tries to release a etcd lock
-func (e *EtcdLock) Unlock(req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
+func (e *EtcdLock) Unlock(ctx context.Context, req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
 	key := e.getKey(req.ResourceId)
 
 	// 1.Create new KV
diff --git a/components/lock/in-memory/in_memory_lock.go b/components/lock/in-memory/in_memory_lock.go
index d53aaaa4..1213f07c 100644
--- a/components/lock/in-memory/in_memory_lock.go
+++ b/components/lock/in-memory/in_memory_lock.go
@@ -66,7 +66,7 @@ func (s *InMemoryLock) Features() []lock.Feature {
 }
 
 // Try to add a lock. Currently this is a non-reentrant lock
-func (s *InMemoryLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
+func (s *InMemoryLock) TryLock(ctx context.Context, req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
 	s.data.Lock()
 	defer s.data.Unlock()
 	// 1. Find the memoryLock for this resourceId
@@ -109,7 +109,7 @@ func (s *InMemoryLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse,
 	}, nil
 }
 
-func (s *InMemoryLock) Unlock(req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
+func (s *InMemoryLock) Unlock(ctx context.Context, req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
 	s.data.Lock()
 	defer s.data.Unlock()
 	// 1. Find the memoryLock for this resourceId
diff --git a/components/lock/lock_store.go b/components/lock/lock_store.go
index a424c9b6..c09c1c44 100644
--- a/components/lock/lock_store.go
+++ b/components/lock/lock_store.go
@@ -20,9 +20,9 @@ type LockStore interface {
 	// Get lock's features
 	Features() []Feature
 	// Node tries to acquire a lock
-	TryLock(req *TryLockRequest) (*TryLockResponse, error)
+	TryLock(ctx context.Context, req *TryLockRequest) (*TryLockResponse, error)
 	// Node tries to release a lock
-	Unlock(req *UnlockRequest) (*UnlockResponse, error)
+	Unlock(ctx context.Context, req *UnlockRequest) (*UnlockResponse, error)
 	// Node tries to renewal lease
 	LockKeepAlive(context.Context, *LockKeepAliveRequest) (*LockKeepAliveResponse, error)
 }
diff --git a/components/lock/mongo/mongo_lock.go b/components/lock/mongo/mongo_lock.go
index 1bf05171..52f34211 100644
--- a/components/lock/mongo/mongo_lock.go
+++ b/components/lock/mongo/mongo_lock.go
@@ -113,7 +113,7 @@ func (e *MongoLock) LockKeepAlive(ctx context.Context, request *lock.LockKeepAli
 	return nil, nil
 }
 
-func (e *MongoLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
+func (e *MongoLock) TryLock(ctx context.Context, req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
 	var err error
 	// create mongo session
 	e.session, err = e.client.StartSession()
@@ -171,7 +171,7 @@ func (e *MongoLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse, er
 	}, nil
 }
 
-func (e *MongoLock) Unlock(req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
+func (e *MongoLock) Unlock(ctx context.Context, req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
 	var err error
 	// create mongo session
 	e.session, err = e.client.StartSession()
diff --git a/components/lock/redis/cluster_redis_lock.go b/components/lock/redis/cluster_redis_lock.go
index 78e71e3b..2f38b6dd 100644
--- a/components/lock/redis/cluster_redis_lock.go
+++ b/components/lock/redis/cluster_redis_lock.go
@@ -86,7 +86,7 @@ func (c *ClusterRedisLock) LockKeepAlive(ctx context.Context, request *lock.Lock
 	return nil, nil
 }
 
-func (c *ClusterRedisLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
+func (c *ClusterRedisLock) TryLock(ctx context.Context, req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
 	//try to get lock on all redis nodes
 	intervalStart := utils.GetMiliTimestamp(time.Now().UnixNano())
 	//intervalLimit must be 1/10 of expire time to make sure time of lock far less than expire time
@@ -154,7 +154,7 @@ func (c *ClusterRedisLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockRespo
 	}, err
 }
 
-func (c *ClusterRedisLock) Unlock(req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
+func (c *ClusterRedisLock) Unlock(ctx context.Context, req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
 	wg := sync.WaitGroup{}
 	//err means there were some internal errors,then the status must be INTERNAL_ERROR
 	//the LOCK_UNEXIST and LOCK_BELONG_TO_OTHERS status codes can be ignore
diff --git a/components/lock/redis/standalone_redis_lock.go b/components/lock/redis/standalone_redis_lock.go
index 0b297e23..e0977a73 100644
--- a/components/lock/redis/standalone_redis_lock.go
+++ b/components/lock/redis/standalone_redis_lock.go
@@ -76,7 +76,7 @@ func (p *StandaloneRedisLock) LockKeepAlive(ctx context.Context, request *lock.L
 }
 
 // Node tries to acquire a redis lock
-func (p *StandaloneRedisLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
+func (p *StandaloneRedisLock) TryLock(ctx context.Context, req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
 	// 1.Setting redis expiration time
 	nx := p.client.SetNX(p.ctx, req.ResourceId, req.LockOwner, time.Second*time.Duration(req.Expire))
 	if nx == nil {
@@ -96,7 +96,7 @@ func (p *StandaloneRedisLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockRe
 const unlockScript = "local v = redis.call(\"get\",KEYS[1]); if v==false then return -1 end; if v~=ARGV[1] then return -2 else return redis.call(\"del\",KEYS[1]) end"
 
 // Node tries to release a redis lock
-func (p *StandaloneRedisLock) Unlock(req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
+func (p *StandaloneRedisLock) Unlock(ctx context.Context, req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
 	// 1. delegate to client.eval lua script
 	eval := p.client.Eval(p.ctx, unlockScript, []string{req.ResourceId}, req.LockOwner)
 	// 2. check error
diff --git a/components/lock/zookeeper/zookeeper_lock.go b/components/lock/zookeeper/zookeeper_lock.go
index b8fb2f47..e456e498 100644
--- a/components/lock/zookeeper/zookeeper_lock.go
+++ b/components/lock/zookeeper/zookeeper_lock.go
@@ -84,7 +84,7 @@ func (p *ZookeeperLock) LockKeepAlive(ctx context.Context, request *lock.LockKee
 }
 
 // TryLock Node tries to acquire a zookeeper lock
-func (p *ZookeeperLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
+func (p *ZookeeperLock) TryLock(ctx context.Context, req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
 
 	conn, err := p.factory.NewConnection(time.Duration(req.Expire)*time.Second, p.metadata)
 	if err != nil {
@@ -118,7 +118,7 @@ func (p *ZookeeperLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse
 }
 
 // Unlock Node tries to release a zookeeper lock
-func (p *ZookeeperLock) Unlock(req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
+func (p *ZookeeperLock) Unlock(ctx context.Context, req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
 
 	conn := p.unlockConn
 
diff --git a/pkg/grpc/default_api/api_lock.go b/pkg/grpc/default_api/api_lock.go
index 0cf28d5f..28d9d1b8 100644
--- a/pkg/grpc/default_api/api_lock.go
+++ b/pkg/grpc/default_api/api_lock.go
@@ -63,7 +63,7 @@ func (a *api) TryLock(ctx context.Context, req *runtimev1pb.TryLockRequest) (*ru
 		return &runtimev1pb.TryLockResponse{}, err
 	}
 	// 4. delegate to the component
-	compResp, err := store.TryLock(compReq)
+	compResp, err := store.TryLock(ctx, compReq)
 	if err != nil {
 		log.DefaultLogger.Errorf("[runtime] [grpc.TryLock] error: %v", err)
 		return &runtimev1pb.TryLockResponse{}, err
@@ -103,7 +103,7 @@ func (a *api) Unlock(ctx context.Context, req *runtimev1pb.UnlockRequest) (*runt
 		return newInternalErrorUnlockResponse(), err
 	}
 	// 4. delegate to the component
-	compResp, err := store.Unlock(compReq)
+	compResp, err := store.Unlock(ctx, compReq)
 	if err != nil {
 		log.DefaultLogger.Errorf("[runtime] [grpc.Unlock] error: %v", err)
 		return newInternalErrorUnlockResponse(), err
diff --git a/pkg/mock/components/lock/lock.go b/pkg/mock/components/lock/lock.go
index 5fe85c18..293d6bea 100644
--- a/pkg/mock/components/lock/lock.go
+++ b/pkg/mock/components/lock/lock.go
@@ -9,7 +9,6 @@ import (
 	reflect "reflect"
 
 	gomock "github.com/golang/mock/gomock"
-
 	lock "mosn.io/layotto/components/lock"
 )
 
@@ -80,31 +79,31 @@ func (mr *MockLockStoreMockRecorder) LockKeepAlive(arg0, arg1 interface{}) *gomo
 }
 
 // TryLock mocks base method.
-func (m *MockLockStore) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
+func (m *MockLockStore) TryLock(ctx context.Context, req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
 	m.ctrl.T.Helper()
-	ret := m.ctrl.Call(m, "TryLock", req)
+	ret := m.ctrl.Call(m, "TryLock", ctx, req)
 	ret0, _ := ret[0].(*lock.TryLockResponse)
 	ret1, _ := ret[1].(error)
 	return ret0, ret1
 }
 
 // TryLock indicates an expected call of TryLock.
-func (mr *MockLockStoreMockRecorder) TryLock(req interface{}) *gomock.Call {
+func (mr *MockLockStoreMockRecorder) TryLock(ctx, req interface{}) *gomock.Call {
 	mr.mock.ctrl.T.Helper()
-	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TryLock", reflect.TypeOf((*MockLockStore)(nil).TryLock), req)
+	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TryLock", reflect.TypeOf((*MockLockStore)(nil).TryLock), ctx, req)
 }
 
 // Unlock mocks base method.
-func (m *MockLockStore) Unlock(req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
+func (m *MockLockStore) Unlock(ctx context.Context, req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
 	m.ctrl.T.Helper()
-	ret := m.ctrl.Call(m, "Unlock", req)
+	ret := m.ctrl.Call(m, "Unlock", ctx, req)
 	ret0, _ := ret[0].(*lock.UnlockResponse)
 	ret1, _ := ret[1].(error)
 	return ret0, ret1
 }
 
 // Unlock indicates an expected call of Unlock.
-func (mr *MockLockStoreMockRecorder) Unlock(req interface{}) *gomock.Call {
+func (mr *MockLockStoreMockRecorder) Unlock(ctx, req interface{}) *gomock.Call {
 	mr.mock.ctrl.T.Helper()
-	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Unlock", reflect.TypeOf((*MockLockStore)(nil).Unlock), req)
+	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Unlock", reflect.TypeOf((*MockLockStore)(nil).Unlock), ctx, req)
 }


Test patch:
diff --git a/components/lock/consul/consul_lock_test.go b/components/lock/consul/consul_lock_test.go
index c002cbdf..14db8bb8 100644
--- a/components/lock/consul/consul_lock_test.go
+++ b/components/lock/consul/consul_lock_test.go
@@ -75,7 +75,7 @@ func TestConsulLock_TryLock(t *testing.T) {
 	kv.EXPECT().Release(&api.KVPair{Key: resouseId, Value: []byte(lockOwerA), Session: "session1"}, nil).
 		Return(true, nil, nil).Times(1)
 
-	tryLock, err := comp.TryLock(&lock.TryLockRequest{
+	tryLock, err := comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resouseId,
 		LockOwner:  lockOwerA,
 		Expire:     expireTime,
@@ -84,7 +84,7 @@ func TestConsulLock_TryLock(t *testing.T) {
 	assert.NoError(t, err)
 	assert.Equal(t, true, tryLock.Success)
 
-	unlock, err := comp.Unlock(&lock.UnlockRequest{
+	unlock, err := comp.Unlock(context.TODO(), &lock.UnlockRequest{
 		ResourceId: resouseId,
 		LockOwner:  lockOwerA,
 	})
@@ -121,7 +121,7 @@ func TestConsulLock_ALock_BLock(t *testing.T) {
 	kv.EXPECT().Acquire(&api.KVPair{Key: resouseId, Value: []byte(lockOwerB), Session: "session2"}, nil).
 		Return(false, nil, nil).Times(1)
 
-	tryLock, _ := comp.TryLock(&lock.TryLockRequest{
+	tryLock, _ := comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resouseId,
 		LockOwner:  lockOwerA,
 		Expire:     expireTime,
@@ -130,7 +130,7 @@ func TestConsulLock_ALock_BLock(t *testing.T) {
 	assert.NoError(t, err)
 	assert.Equal(t, true, tryLock.Success)
 
-	bLock, _ := comp.TryLock(&lock.TryLockRequest{
+	bLock, _ := comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resouseId,
 		LockOwner:  lockOwerB,
 		Expire:     expireTime,
@@ -166,7 +166,7 @@ func TestConsulLock_ALock_BUnlock(t *testing.T) {
 	kv.EXPECT().Release(&api.KVPair{Key: resouseId, Value: []byte(lockOwerA), Session: "session1"}, nil).
 		Return(true, nil, nil).Times(1)
 
-	tryLock, _ := comp.TryLock(&lock.TryLockRequest{
+	tryLock, _ := comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resouseId,
 		LockOwner:  lockOwerA,
 		Expire:     expireTime,
@@ -175,7 +175,7 @@ func TestConsulLock_ALock_BUnlock(t *testing.T) {
 	assert.NoError(t, err)
 	assert.Equal(t, true, tryLock.Success)
 
-	unlock, _ := comp.Unlock(&lock.UnlockRequest{
+	unlock, _ := comp.Unlock(context.TODO(), &lock.UnlockRequest{
 		ResourceId: resouseId,
 		LockOwner:  lockOwerB,
 	})
@@ -183,7 +183,7 @@ func TestConsulLock_ALock_BUnlock(t *testing.T) {
 	assert.NoError(t, err)
 	assert.Equal(t, lock.LOCK_UNEXIST, unlock.Status)
 
-	unlock2, err := comp.Unlock(&lock.UnlockRequest{
+	unlock2, err := comp.Unlock(context.TODO(), &lock.UnlockRequest{
 		ResourceId: resouseId,
 		LockOwner:  lockOwerA,
 	})
diff --git a/components/lock/etcd/etcd_lock_test.go b/components/lock/etcd/etcd_lock_test.go
index 41467b3f..4474ed68 100644
--- a/components/lock/etcd/etcd_lock_test.go
+++ b/components/lock/etcd/etcd_lock_test.go
@@ -123,7 +123,7 @@ func TestEtcdLock_TryLock(t *testing.T) {
 	assert.NoError(t, err)
 
 	ownerId1 := uuid.New().String()
-	resp, err = comp.TryLock(&lock.TryLockRequest{
+	resp, err = comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resourceId,
 		LockOwner:  ownerId1,
 		Expire:     10,
@@ -132,7 +132,7 @@ func TestEtcdLock_TryLock(t *testing.T) {
 	assert.Equal(t, true, resp.Success)
 
 	//repeat
-	resp, err = comp.TryLock(&lock.TryLockRequest{
+	resp, err = comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resourceId,
 		LockOwner:  ownerId1,
 		Expire:     10,
@@ -146,7 +146,7 @@ func TestEtcdLock_TryLock(t *testing.T) {
 	go func() {
 		//another owner
 		ownerId2 := uuid.New().String()
-		resp, err = comp.TryLock(&lock.TryLockRequest{
+		resp, err = comp.TryLock(context.TODO(), &lock.TryLockRequest{
 			ResourceId: resourceId,
 			LockOwner:  ownerId2,
 			Expire:     10,
@@ -159,7 +159,7 @@ func TestEtcdLock_TryLock(t *testing.T) {
 	wg.Wait()
 
 	//another resource
-	resp, err = comp.TryLock(&lock.TryLockRequest{
+	resp, err = comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resourceId2,
 		LockOwner:  ownerId1,
 		Expire:     10,
@@ -194,7 +194,7 @@ func TestEtcdLock_UnLock(t *testing.T) {
 	assert.NoError(t, err)
 
 	ownerId1 := uuid.New().String()
-	lockresp, err = comp.TryLock(&lock.TryLockRequest{
+	lockresp, err = comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resourceId3,
 		LockOwner:  ownerId1,
 		Expire:     10,
@@ -203,7 +203,7 @@ func TestEtcdLock_UnLock(t *testing.T) {
 	assert.Equal(t, true, lockresp.Success)
 
 	//error ownerid
-	resp, err = comp.Unlock(&lock.UnlockRequest{
+	resp, err = comp.Unlock(context.TODO(), &lock.UnlockRequest{
 		ResourceId: resourceId3,
 		LockOwner:  uuid.New().String(),
 	})
@@ -211,7 +211,7 @@ func TestEtcdLock_UnLock(t *testing.T) {
 	assert.Equal(t, lock.LOCK_BELONG_TO_OTHERS, resp.Status)
 
 	//error resourceid
-	resp, err = comp.Unlock(&lock.UnlockRequest{
+	resp, err = comp.Unlock(context.TODO(), &lock.UnlockRequest{
 		ResourceId: resourceId4,
 		LockOwner:  ownerId1,
 	})
@@ -219,7 +219,7 @@ func TestEtcdLock_UnLock(t *testing.T) {
 	assert.Equal(t, lock.LOCK_UNEXIST, resp.Status)
 
 	//success
-	resp, err = comp.Unlock(&lock.UnlockRequest{
+	resp, err = comp.Unlock(context.TODO(), &lock.UnlockRequest{
 		ResourceId: resourceId3,
 		LockOwner:  ownerId1,
 	})
diff --git a/components/lock/in-memory/in_memory_lock_test.go b/components/lock/in-memory/in_memory_lock_test.go
index c897b76a..4f5d344c 100644
--- a/components/lock/in-memory/in_memory_lock_test.go
+++ b/components/lock/in-memory/in_memory_lock_test.go
@@ -60,12 +60,12 @@ func TestTryLock(t *testing.T) {
 
 	var err error
 	var resp *lock.TryLockResponse
-	resp, err = s.TryLock(req)
+	resp, err = s.TryLock(context.TODO(), req)
 	assert.NoError(t, err)
 	assert.NotNil(t, req)
 	assert.True(t, resp.Success)
 
-	resp, err = s.TryLock(req)
+	resp, err = s.TryLock(context.TODO(), req)
 	assert.NoError(t, err)
 	assert.NotNil(t, req)
 	assert.False(t, resp.Success)
@@ -76,7 +76,7 @@ func TestTryLock(t *testing.T) {
 		Expire:     1,
 	}
 
-	resp, err = s.TryLock(req)
+	resp, err = s.TryLock(context.TODO(), req)
 	assert.NoError(t, err)
 	assert.NotNil(t, req)
 	assert.True(t, resp.Success)
@@ -87,14 +87,14 @@ func TestTryLock(t *testing.T) {
 		Expire:     1,
 	}
 
-	resp, err = s.TryLock(req)
+	resp, err = s.TryLock(context.TODO(), req)
 	assert.NoError(t, err)
 	assert.NotNil(t, req)
 	assert.False(t, resp.Success)
 
 	s.data.locks["key112"].expireTime = time.Now().Add(-2 * time.Second)
 
-	resp, err = s.TryLock(req)
+	resp, err = s.TryLock(context.TODO(), req)
 	assert.NoError(t, err)
 	assert.NotNil(t, req)
 	assert.True(t, resp.Success)
@@ -112,7 +112,7 @@ func TestUnLock(t *testing.T) {
 
 	var err error
 	var resp *lock.UnlockResponse
-	resp, err = s.Unlock(req)
+	resp, err = s.Unlock(context.TODO(), req)
 	assert.NoError(t, err)
 	assert.NotNil(t, req)
 	assert.Equal(t, lock.LOCK_UNEXIST, resp.Status)
@@ -124,24 +124,24 @@ func TestUnLock(t *testing.T) {
 	}
 
 	var lockResp *lock.TryLockResponse
-	lockResp, err = s.TryLock(lockReq)
+	lockResp, err = s.TryLock(context.TODO(), lockReq)
 	assert.NoError(t, err)
 	assert.NotNil(t, req)
 	assert.True(t, lockResp.Success)
 
-	resp, err = s.Unlock(req)
+	resp, err = s.Unlock(context.TODO(), req)
 	assert.NoError(t, err)
 	assert.NotNil(t, req)
 	assert.Equal(t, lock.SUCCESS, resp.Status)
 
-	lockResp, err = s.TryLock(lockReq)
+	lockResp, err = s.TryLock(context.TODO(), lockReq)
 	assert.NoError(t, err)
 	assert.NotNil(t, req)
 	assert.True(t, lockResp.Success)
 
 	req.LockOwner = "1"
 
-	resp, err = s.Unlock(req)
+	resp, err = s.Unlock(context.TODO(), req)
 	assert.NoError(t, err)
 	assert.NotNil(t, req)
 	assert.Equal(t, lock.LOCK_BELONG_TO_OTHERS, resp.Status)
@@ -150,17 +150,17 @@ func TestUnLock(t *testing.T) {
 	lockReq.ResourceId = "11"
 	req.LockOwner = "own1"
 	lockReq.LockOwner = "own1"
-	lockResp, err = s.TryLock(lockReq)
+	lockResp, err = s.TryLock(context.TODO(), lockReq)
 	assert.NoError(t, err)
 	assert.NotNil(t, req)
 	assert.True(t, lockResp.Success)
 
-	resp, err = s.Unlock(req)
+	resp, err = s.Unlock(context.TODO(), req)
 	assert.NoError(t, err)
 	assert.NotNil(t, req)
 	assert.Equal(t, lock.SUCCESS, resp.Status)
 
-	resp, err = s.Unlock(req)
+	resp, err = s.Unlock(context.TODO(), req)
 	assert.NoError(t, err)
 	assert.NotNil(t, req)
 	assert.Equal(t, lock.LOCK_UNEXIST, resp.Status)
diff --git a/components/lock/mongo/mongo_lock_test.go b/components/lock/mongo/mongo_lock_test.go
index cf766410..5ac4e117 100644
--- a/components/lock/mongo/mongo_lock_test.go
+++ b/components/lock/mongo/mongo_lock_test.go
@@ -87,7 +87,7 @@ func TestMongoLock_TryLock(t *testing.T) {
 	comp.client = &mockMongoClient
 
 	ownerId1 := uuid.New().String()
-	resp, err = comp.TryLock(&lock.TryLockRequest{
+	resp, err = comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resourceId,
 		LockOwner:  ownerId1,
 		Expire:     10,
@@ -95,7 +95,7 @@ func TestMongoLock_TryLock(t *testing.T) {
 	assert.NoError(t, err)
 	assert.Equal(t, true, resp.Success)
 
-	resp, err = comp.TryLock(&lock.TryLockRequest{
+	resp, err = comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resourceId,
 		LockOwner:  ownerId1,
 		Expire:     10,
@@ -108,7 +108,7 @@ func TestMongoLock_TryLock(t *testing.T) {
 
 	go func() {
 		ownerId2 := uuid.New().String()
-		resp, err = comp.TryLock(&lock.TryLockRequest{
+		resp, err = comp.TryLock(context.TODO(), &lock.TryLockRequest{
 			ResourceId: resourceId,
 			LockOwner:  ownerId2,
 			Expire:     10,
@@ -121,7 +121,7 @@ func TestMongoLock_TryLock(t *testing.T) {
 	wg.Wait()
 
 	//another resource
-	resp, err = comp.TryLock(&lock.TryLockRequest{
+	resp, err = comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resourceId2,
 		LockOwner:  ownerId1,
 		Expire:     10,
@@ -163,7 +163,7 @@ func TestMongoLock_Unlock(t *testing.T) {
 	comp.client = &mockMongoClient
 
 	ownerId1 := uuid.New().String()
-	lockresp, err = comp.TryLock(&lock.TryLockRequest{
+	lockresp, err = comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resourceId3,
 		LockOwner:  ownerId1,
 		Expire:     10,
@@ -172,7 +172,7 @@ func TestMongoLock_Unlock(t *testing.T) {
 	assert.Equal(t, true, lockresp.Success)
 
 	//error resourceid
-	resp, err = comp.Unlock(&lock.UnlockRequest{
+	resp, err = comp.Unlock(context.TODO(), &lock.UnlockRequest{
 		ResourceId: resourceId4,
 		LockOwner:  ownerId1,
 	})
@@ -180,7 +180,7 @@ func TestMongoLock_Unlock(t *testing.T) {
 	assert.Equal(t, lock.LOCK_UNEXIST, resp.Status)
 
 	//success
-	resp, err = comp.Unlock(&lock.UnlockRequest{
+	resp, err = comp.Unlock(context.TODO(), &lock.UnlockRequest{
 		ResourceId: resourceId3,
 		LockOwner:  ownerId1,
 	})
diff --git a/components/lock/redis/cluster_redis_lock_test.go b/components/lock/redis/cluster_redis_lock_test.go
index 93252555..812ab0cc 100644
--- a/components/lock/redis/cluster_redis_lock_test.go
+++ b/components/lock/redis/cluster_redis_lock_test.go
@@ -101,7 +101,7 @@ func TestClusterRedisLock_TryLock(t *testing.T) {
 	assert.NoError(t, err)
 	// 1. client1 trylock
 	ownerId1 := uuid.New().String()
-	resp, err := comp.TryLock(&lock.TryLockRequest{
+	resp, err := comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: cResourceId,
 		LockOwner:  ownerId1,
 		Expire:     10,
@@ -113,7 +113,7 @@ func TestClusterRedisLock_TryLock(t *testing.T) {
 	//	2. Client2 tryLock fail
 	go func() {
 		owner2 := uuid.New().String()
-		resp2, err2 := comp.TryLock(&lock.TryLockRequest{
+		resp2, err2 := comp.TryLock(context.TODO(), &lock.TryLockRequest{
 			ResourceId: cResourceId,
 			LockOwner:  owner2,
 			Expire:     10,
@@ -124,7 +124,7 @@ func TestClusterRedisLock_TryLock(t *testing.T) {
 	}()
 	wg.Wait()
 	// 3. client 1 unlock
-	unlockResp, err := comp.Unlock(&lock.UnlockRequest{
+	unlockResp, err := comp.Unlock(context.TODO(), &lock.UnlockRequest{
 		ResourceId: cResourceId,
 		LockOwner:  ownerId1,
 	})
@@ -134,7 +134,7 @@ func TestClusterRedisLock_TryLock(t *testing.T) {
 	wg.Add(1)
 	go func() {
 		owner2 := uuid.New().String()
-		resp2, err2 := comp.TryLock(&lock.TryLockRequest{
+		resp2, err2 := comp.TryLock(context.TODO(), &lock.TryLockRequest{
 			ResourceId: cResourceId,
 			LockOwner:  owner2,
 			Expire:     10,
@@ -142,7 +142,7 @@ func TestClusterRedisLock_TryLock(t *testing.T) {
 		assert.NoError(t, err2)
 		assert.True(t, resp2.Success, "client2 failed to get lock?!")
 		// 5. client2 unlock
-		unlockResp, err := comp.Unlock(&lock.UnlockRequest{
+		unlockResp, err := comp.Unlock(context.TODO(), &lock.UnlockRequest{
 			ResourceId: cResourceId,
 			LockOwner:  owner2,
 		})
diff --git a/components/lock/redis/standalone_redis_lock_test.go b/components/lock/redis/standalone_redis_lock_test.go
index 71e03c02..6e4ab0dd 100644
--- a/components/lock/redis/standalone_redis_lock_test.go
+++ b/components/lock/redis/standalone_redis_lock_test.go
@@ -13,6 +13,7 @@
 package redis
 
 import (
+	"context"
 	"sync"
 	"testing"
 
@@ -98,7 +99,7 @@ func TestStandaloneRedisLock_TryLock(t *testing.T) {
 	assert.NoError(t, err)
 	// 1. client1 trylock
 	ownerId1 := uuid.New().String()
-	resp, err := comp.TryLock(&lock.TryLockRequest{
+	resp, err := comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resourceId,
 		LockOwner:  ownerId1,
 		Expire:     10,
@@ -110,7 +111,7 @@ func TestStandaloneRedisLock_TryLock(t *testing.T) {
 	//	2. Client2 tryLock fail
 	go func() {
 		owner2 := uuid.New().String()
-		resp2, err2 := comp.TryLock(&lock.TryLockRequest{
+		resp2, err2 := comp.TryLock(context.TODO(), &lock.TryLockRequest{
 			ResourceId: resourceId,
 			LockOwner:  owner2,
 			Expire:     10,
@@ -121,7 +122,7 @@ func TestStandaloneRedisLock_TryLock(t *testing.T) {
 	}()
 	wg.Wait()
 	// 3. client 1 unlock
-	unlockResp, err := comp.Unlock(&lock.UnlockRequest{
+	unlockResp, err := comp.Unlock(context.TODO(), &lock.UnlockRequest{
 		ResourceId: resourceId,
 		LockOwner:  ownerId1,
 	})
@@ -131,7 +132,7 @@ func TestStandaloneRedisLock_TryLock(t *testing.T) {
 	wg.Add(1)
 	go func() {
 		owner2 := uuid.New().String()
-		resp2, err2 := comp.TryLock(&lock.TryLockRequest{
+		resp2, err2 := comp.TryLock(context.TODO(), &lock.TryLockRequest{
 			ResourceId: resourceId,
 			LockOwner:  owner2,
 			Expire:     10,
@@ -139,7 +140,7 @@ func TestStandaloneRedisLock_TryLock(t *testing.T) {
 		assert.NoError(t, err2)
 		assert.True(t, resp2.Success, "client2 failed to get lock?!")
 		// 5. client2 unlock
-		unlockResp, err := comp.Unlock(&lock.UnlockRequest{
+		unlockResp, err := comp.Unlock(context.TODO(), &lock.UnlockRequest{
 			ResourceId: resourceId,
 			LockOwner:  owner2,
 		})
diff --git a/components/lock/zookeeper/zookeeper_lock_test.go b/components/lock/zookeeper/zookeeper_lock_test.go
index a689e116..51268201 100644
--- a/components/lock/zookeeper/zookeeper_lock_test.go
+++ b/components/lock/zookeeper/zookeeper_lock_test.go
@@ -71,14 +71,14 @@ func TestZookeeperLock_ALock_AUnlock(t *testing.T) {
 	comp.unlockConn = unlockConn
 	comp.factory = factory
 
-	tryLock, err := comp.TryLock(&lock.TryLockRequest{
+	tryLock, err := comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resouseId,
 		LockOwner:  lockOwerA,
 		Expire:     expireTime,
 	})
 	assert.NoError(t, err)
 	assert.Equal(t, tryLock.Success, true)
-	unlock, _ := comp.Unlock(&lock.UnlockRequest{
+	unlock, _ := comp.Unlock(context.TODO(), &lock.UnlockRequest{
 		ResourceId: resouseId,
 		LockOwner:  lockOwerA,
 	})
@@ -105,14 +105,14 @@ func TestZookeeperLock_ALock_BUnlock(t *testing.T) {
 	comp.unlockConn = unlockConn
 	comp.factory = factory
 
-	tryLock, err := comp.TryLock(&lock.TryLockRequest{
+	tryLock, err := comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resouseId,
 		LockOwner:  lockOwerA,
 		Expire:     expireTime,
 	})
 	assert.NoError(t, err)
 	assert.Equal(t, tryLock.Success, true)
-	unlock, err := comp.Unlock(&lock.UnlockRequest{
+	unlock, err := comp.Unlock(context.TODO(), &lock.UnlockRequest{
 		ResourceId: resouseId,
 		LockOwner:  lockOwerB,
 	})
@@ -149,7 +149,7 @@ func TestZookeeperLock_ALock_BLock_AUnlock_BLock_BUnlock(t *testing.T) {
 	comp.factory = factory
 
 	//A lock
-	tryLock, err := comp.TryLock(&lock.TryLockRequest{
+	tryLock, err := comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resouseId,
 		LockOwner:  lockOwerA,
 		Expire:     expireTime,
@@ -157,7 +157,7 @@ func TestZookeeperLock_ALock_BLock_AUnlock_BLock_BUnlock(t *testing.T) {
 	assert.NoError(t, err)
 	assert.Equal(t, true, tryLock.Success)
 	//B lock
-	tryLock, err = comp.TryLock(&lock.TryLockRequest{
+	tryLock, err = comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resouseId,
 		LockOwner:  lockOwerB,
 		Expire:     expireTime,
@@ -165,7 +165,7 @@ func TestZookeeperLock_ALock_BLock_AUnlock_BLock_BUnlock(t *testing.T) {
 	assert.NoError(t, err)
 	assert.Equal(t, false, tryLock.Success)
 	//A unlock
-	unlock, _ := comp.Unlock(&lock.UnlockRequest{
+	unlock, _ := comp.Unlock(context.TODO(), &lock.UnlockRequest{
 		ResourceId: resouseId,
 		LockOwner:  lockOwerA,
 	})
@@ -173,7 +173,7 @@ func TestZookeeperLock_ALock_BLock_AUnlock_BLock_BUnlock(t *testing.T) {
 	assert.Equal(t, lock.SUCCESS, unlock.Status)
 
 	//B lock
-	tryLock, err = comp.TryLock(&lock.TryLockRequest{
+	tryLock, err = comp.TryLock(context.TODO(), &lock.TryLockRequest{
 		ResourceId: resouseId,
 		LockOwner:  lockOwerB,
 		Expire:     expireTime,
@@ -182,7 +182,7 @@ func TestZookeeperLock_ALock_BLock_AUnlock_BLock_BUnlock(t *testing.T) {
 	assert.Equal(t, true, tryLock.Success)
 
 	//B unlock
-	unlock, _ = comp.Unlock(&lock.UnlockRequest{
+	unlock, _ = comp.Unlock(context.TODO(), &lock.UnlockRequest{
 		ResourceId: resouseId,
 		LockOwner:  lockOwerB,
 	})
diff --git a/pkg/grpc/default_api/api_lock_test.go b/pkg/grpc/default_api/api_lock_test.go
index 2ab45cee..c1dfb0c2 100644
--- a/pkg/grpc/default_api/api_lock_test.go
+++ b/pkg/grpc/default_api/api_lock_test.go
@@ -128,7 +128,7 @@ func TestTryLock(t *testing.T) {
 
 	t.Run("normal", func(t *testing.T) {
 		mockLockStore := mock_lock.NewMockLockStore(gomock.NewController(t))
-		mockLockStore.EXPECT().TryLock(gomock.Any()).DoAndReturn(func(req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
+		mockLockStore.EXPECT().TryLock(context.TODO(), gomock.Any()).DoAndReturn(func(ctx context.Context, req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
 			assert.Equal(t, "lock|||resource", req.ResourceId)
 			assert.Equal(t, "owner", req.LockOwner)
 			assert.Equal(t, int32(1), req.Expire)
@@ -195,7 +195,7 @@ func TestUnlock(t *testing.T) {
 
 	t.Run("normal", func(t *testing.T) {
 		mockLockStore := mock_lock.NewMockLockStore(gomock.NewController(t))
-		mockLockStore.EXPECT().Unlock(gomock.Any()).DoAndReturn(func(req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
+		mockLockStore.EXPECT().Unlock(context.TODO(), gomock.Any()).DoAndReturn(func(ctx context.Context, req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
 			assert.Equal(t, "lock|||resource", req.ResourceId)
 			assert.Equal(t, "owner", req.LockOwner)
 			return &lock.UnlockResponse{