-
Notifications
You must be signed in to change notification settings - Fork 452
/
mem.go
142 lines (118 loc) · 4.11 KB
/
mem.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package memcluster
import (
"errors"
"sync"
"github.com/m3db/m3/src/cluster/client"
"github.com/m3db/m3/src/cluster/kv"
"github.com/m3db/m3/src/cluster/kv/mem"
"github.com/m3db/m3/src/cluster/services"
)
const (
_kvPrefix = "_kv"
)
var (
// assert the interface matches.
_ client.Client = (*Client)(nil)
)
// Client provides a cluster/client.Client backed by kv/mem transaction store,
// which stores data in memory instead of in etcd.
type Client struct {
mu sync.Mutex
serviceOpts kv.OverrideOptions
cache map[cacheKey]kv.TxnStore
}
// New instantiates a client which defaults its stores to the given zone/env/namespace.
func New(serviceOpts kv.OverrideOptions) *Client {
return &Client{
serviceOpts: serviceOpts,
cache: make(map[cacheKey]kv.TxnStore),
}
}
// Services constructs a gateway to all cluster services, backed by a mem store.
func (c *Client) Services(opts services.OverrideOptions) (services.Services, error) {
if opts == nil {
opts = services.NewOverrideOptions()
}
errUnsupported := errors.New("currently unsupported for inMemoryClusterClient")
kvGen := func(zone string) (kv.Store, error) {
return c.Store(kv.NewOverrideOptions().SetZone(zone))
}
heartbeatGen := func(sid services.ServiceID) (services.HeartbeatService, error) {
return nil, errUnsupported
}
leaderGen := func(sid services.ServiceID, opts services.ElectionOptions) (services.LeaderService, error) {
return nil, errUnsupported
}
return services.NewServices(
services.NewOptions().
SetKVGen(kvGen).
SetHeartbeatGen(heartbeatGen).
SetLeaderGen(leaderGen).
SetNamespaceOptions(opts.NamespaceOptions()),
)
}
// KV returns/constructs a mem backed kv.Store for the default zone/env/namespace.
func (c *Client) KV() (kv.Store, error) {
return c.TxnStore(kv.NewOverrideOptions())
}
// Txn returns/constructs a mem backed kv.TxnStore for the default zone/env/namespace.
func (c *Client) Txn() (kv.TxnStore, error) {
return c.TxnStore(kv.NewOverrideOptions())
}
// Store returns/constructs a mem backed kv.Store for the given env/zone/namespace.
func (c *Client) Store(opts kv.OverrideOptions) (kv.Store, error) {
return c.TxnStore(opts)
}
// TxnStore returns/constructs a mem backed kv.TxnStore for the given env/zone/namespace.
func (c *Client) TxnStore(opts kv.OverrideOptions) (kv.TxnStore, error) {
c.mu.Lock()
defer c.mu.Unlock()
opts = mergeOpts(c.serviceOpts, opts)
key := cacheKey{
Env: opts.Environment(),
Zone: opts.Zone(),
Namespace: opts.Namespace(),
}
if s, ok := c.cache[key]; ok {
return s, nil
}
store := mem.NewStore()
c.cache[key] = store
return store, nil
}
type cacheKey struct {
Env string
Zone string
Namespace string
}
func mergeOpts(defaults kv.OverrideOptions, opts kv.OverrideOptions) kv.OverrideOptions {
if opts.Zone() == "" {
opts = opts.SetZone(defaults.Zone())
}
if opts.Environment() == "" {
opts = opts.SetEnvironment(defaults.Environment())
}
if opts.Namespace() == "" {
opts = opts.SetNamespace(_kvPrefix)
}
return opts
}