forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
92 lines (78 loc) · 2.43 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2018 PingCAP, Inc.
//
// Licensed 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 store
import (
"net/url"
"strings"
"sync"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
)
var stores = make(map[string]kv.Driver)
var storesLock sync.RWMutex
// Register registers a kv storage with unique name and its associated Driver.
func Register(name string, driver kv.Driver) error {
storesLock.Lock()
defer storesLock.Unlock()
name = strings.ToLower(name)
if _, ok := stores[name]; ok {
return errors.Errorf("%s is already registered", name)
}
stores[name] = driver
return nil
}
// New creates a kv Storage with path.
//
// The path must be a URL format 'engine://path?params' like the one for
// session.Open() but with the dbname cut off.
// Examples:
// goleveldb://relative/path
// boltdb:///absolute/path
//
// The engine should be registered before creating storage.
func New(path string) (kv.Storage, error) {
return newStoreWithRetry(path, util.DefaultMaxRetries)
}
func newStoreWithRetry(path string, maxRetries int) (kv.Storage, error) {
storeURL, err := url.Parse(path)
if err != nil {
return nil, err
}
name := strings.ToLower(storeURL.Scheme)
d, ok := loadDriver(name)
if !ok {
return nil, errors.Errorf("invalid uri format, storage %s is not registered", name)
}
var s kv.Storage
err = util.RunWithRetry(maxRetries, util.RetryInterval, func() (bool, error) {
logutil.BgLogger().Info("new store", zap.String("path", path))
s, err = d.Open(path)
return kv.IsTxnRetryableError(err), err
})
if err == nil {
logutil.BgLogger().Info("new store with retry success")
} else {
logutil.BgLogger().Warn("new store with retry failed", zap.Error(err))
}
return s, errors.Trace(err)
}
func loadDriver(name string) (kv.Driver, bool) {
storesLock.RLock()
defer storesLock.RUnlock()
d, ok := stores[name]
return d, ok
}