-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
util/kvencoder: use reference count to keep single domain instance #7094
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,27 +77,43 @@ type KvEncoder interface { | |
Close() error | ||
} | ||
|
||
var ( | ||
// refCount is used to ensure that there is only one domain.Domain instance. | ||
refCount int64 | ||
storeGlobal kv.Storage | ||
domGlobal *domain.Domain | ||
) | ||
|
||
type kvEncoder struct { | ||
se session.Session | ||
store kv.Storage | ||
dom *domain.Domain | ||
se session.Session | ||
} | ||
|
||
// New new a KvEncoder | ||
func New(dbName string, idAlloc autoid.Allocator) (KvEncoder, error) { | ||
kvEnc := &kvEncoder{} | ||
if atomic.LoadInt64(&refCount) == 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about CAS(0, 1)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or just hold a lock during initGlobal? |
||
if err := initGlobal(); err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
} | ||
err := kvEnc.initial(dbName, idAlloc) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
|
||
atomic.AddInt64(&refCount, 1) | ||
return kvEnc, nil | ||
} | ||
|
||
func (e *kvEncoder) Close() error { | ||
e.dom.Close() | ||
if err := e.store.Close(); err != nil { | ||
return errors.Trace(err) | ||
e.se.Close() | ||
count := atomic.AddInt64(&refCount, -1) | ||
if count == 0 { | ||
e.dom.Close() | ||
if err := e.store.Close(); err != nil { | ||
return errors.Trace(err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
@@ -202,37 +218,7 @@ func newMockTikvWithBootstrap() (kv.Storage, *domain.Domain, error) { | |
} | ||
|
||
func (e *kvEncoder) initial(dbName string, idAlloc autoid.Allocator) (err error) { | ||
var ( | ||
store kv.Storage | ||
dom *domain.Domain | ||
se session.Session | ||
) | ||
defer func() { | ||
if err == nil { | ||
return | ||
} | ||
if store != nil { | ||
if err1 := store.Close(); err1 != nil { | ||
log.Error(errors.ErrorStack(err1)) | ||
} | ||
} | ||
if dom != nil { | ||
dom.Close() | ||
} | ||
if se != nil { | ||
se.Close() | ||
} | ||
}() | ||
|
||
// disable stats update. | ||
session.SetStatsLease(0) | ||
store, dom, err = newMockTikvWithBootstrap() | ||
if err != nil { | ||
err = errors.Trace(err) | ||
return | ||
} | ||
|
||
se, err = session.CreateSession(store) | ||
se, err := session.CreateSession(storeGlobal) | ||
if err != nil { | ||
err = errors.Trace(err) | ||
return | ||
|
@@ -254,7 +240,28 @@ func (e *kvEncoder) initial(dbName string, idAlloc autoid.Allocator) (err error) | |
se.GetSessionVars().ImportingData = true | ||
se.GetSessionVars().SkipUTF8Check = true | ||
e.se = se | ||
e.store = store | ||
e.dom = dom | ||
e.store = storeGlobal | ||
e.dom = domGlobal | ||
return nil | ||
} | ||
|
||
// initGlobal modify the global domain and store | ||
func initGlobal() error { | ||
// disable stats update. | ||
session.SetStatsLease(0) | ||
var err error | ||
storeGlobal, domGlobal, err = newMockTikvWithBootstrap() | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
if storeGlobal != nil { | ||
if err1 := storeGlobal.Close(); err1 != nil { | ||
log.Error(errors.ErrorStack(err1)) | ||
} | ||
} | ||
if domGlobal != nil { | ||
domGlobal.Close() | ||
} | ||
return nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return error instead of nil? |
||
} |
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.
can we remove
refCount
? seems we can usedomGlobal != nil
to replace?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.
No, we can't
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.
When refCount decrease to 0, we set
domGlobal = nil
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.
So
refCount == 0
is equal todomGolbal == nil
?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.
Nope