-
Notifications
You must be signed in to change notification settings - Fork 286
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
feat: Implement automatic refresh functionality for TableMetaCache #739
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bc605ce
support auto refresh table mate cache
invalid-email-address d602569
fix test
invalid-email-address 6846561
Refactor the code to address the issue of duplicate definitions
Code-Fight 84b8079
Refactor the code to address the issue of duplicate definitions
Code-Fight a54a9d9
Refactor the code to address the issue of duplicate definitions
Code-Fight dbce868
Refactor the code to address the issue of duplicate definitions
Code-Fight 7ab06df
Merge branch 'master' into feature_refresh_cache
luky116 24c6cef
Merge branch 'master' into feature_refresh_cache
luky116 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package base | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/agiledragon/gomonkey/v2" | ||
"github.com/go-sql-driver/mysql" | ||
"github.com/stretchr/testify/assert" | ||
"seata.apache.org/seata-go/pkg/datasource/sql/types" | ||
"seata.apache.org/seata-go/testdata" | ||
) | ||
|
||
var ( | ||
capacity int32 = 1024 | ||
EexpireTime = 15 * time.Minute | ||
tableMetaOnce sync.Once | ||
) | ||
|
||
type mockTrigger struct { | ||
} | ||
|
||
func (m *mockTrigger) LoadOne(ctx context.Context, dbName string, table string, conn *sql.Conn) (*types.TableMeta, error) { | ||
return nil, nil | ||
} | ||
|
||
func (m *mockTrigger) LoadAll(ctx context.Context, dbName string, conn *sql.Conn, tables ...string) ([]types.TableMeta, error) { | ||
return nil, nil | ||
} | ||
|
||
func TestBaseTableMetaCache_refresh(t *testing.T) { | ||
type fields struct { | ||
lock sync.RWMutex | ||
expireDuration time.Duration | ||
capity int32 | ||
size int32 | ||
cache map[string]*entry | ||
cancel context.CancelFunc | ||
trigger trigger | ||
db *sql.DB | ||
cfg *mysql.Config | ||
} | ||
type args struct { | ||
ctx context.Context | ||
} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want types.TableMeta | ||
}{ | ||
{name: "test-1", | ||
fields: fields{ | ||
lock: sync.RWMutex{}, | ||
capity: capacity, | ||
size: 0, | ||
expireDuration: EexpireTime, | ||
cache: map[string]*entry{ | ||
"test": { | ||
value: types.TableMeta{}, | ||
lastAccess: time.Now(), | ||
}, | ||
}, | ||
cancel: cancel, | ||
trigger: &mockTrigger{}, | ||
cfg: &mysql.Config{}, | ||
db: &sql.DB{}, | ||
}, args: args{ctx: ctx}, | ||
want: testdata.MockWantTypesMeta("test")}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
connStub := gomonkey.ApplyMethodFunc(tt.fields.db, "Conn", | ||
func(_ context.Context) (*sql.Conn, error) { | ||
return &sql.Conn{}, nil | ||
}) | ||
|
||
defer connStub.Reset() | ||
|
||
loadAllStub := gomonkey.ApplyMethodFunc(tt.fields.trigger, "LoadAll", | ||
func(_ context.Context, _ string, _ *sql.Conn, _ ...string) ([]types.TableMeta, error) { | ||
return []types.TableMeta{tt.want}, nil | ||
}) | ||
|
||
defer loadAllStub.Reset() | ||
|
||
c := &BaseTableMetaCache{ | ||
lock: tt.fields.lock, | ||
expireDuration: tt.fields.expireDuration, | ||
capity: tt.fields.capity, | ||
size: tt.fields.size, | ||
cache: tt.fields.cache, | ||
cancel: tt.fields.cancel, | ||
trigger: tt.fields.trigger, | ||
db: tt.fields.db, | ||
cfg: tt.fields.cfg, | ||
} | ||
go c.refresh(tt.args.ctx) | ||
time.Sleep(time.Second * 3) | ||
|
||
assert.Equal(t, c.cache["test"].value, tt.want) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
这里是不是都得去刷新,而不用去判断原来是否存在?
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.
我看java的实现也是先判断有没有,然后再去更新有的
如果直接更新也行,不过也要判断,要加个初始化