-
Notifications
You must be signed in to change notification settings - Fork 492
/
storage.go
178 lines (141 loc) · 4.53 KB
/
storage.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright (c) 2017, The Easegress Authors
* All rights reserved.
*
* 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 storage provides the storage for FaaS.
package storage
import (
"fmt"
"time"
"go.etcd.io/etcd/api/v3/mvccpb"
"github.com/megaease/easegress/v2/pkg/cluster"
"github.com/megaease/easegress/v2/pkg/logger"
)
const (
allFunctionsSpecPrefix = "/faas/%s/spec" // +controllerName
allFunctionStatusPrefix = "/faas/%s/status" // +controllerName
functionSpecPrefix = "/faas/%s/spec/%s" // +controllerName + functionName
functionStatusPrefix = "/faas/%s/status/%s" // +controllerName + functionName
)
type (
// Storage is the interface to contain storage APIs.
Storage interface {
Lock() error
Unlock() error
Get(key string) (*string, error)
GetPrefix(prefix string) (map[string]string, error)
GetRaw(key string) (*mvccpb.KeyValue, error)
GetRawPrefix(prefix string) (map[string]*mvccpb.KeyValue, error)
Put(key, value string) error
PutUnderLease(key, value string) error
PutAndDelete(map[string]*string) error
PutAndDeleteUnderLease(map[string]*string) error
Delete(key string) error
DeletePrefix(prefix string) error
Syncer() (cluster.Syncer, error)
}
clusterStorage struct {
name string
cls cluster.Cluster
mutex cluster.Mutex
}
)
// NewStorage creates a storage.
func NewStorage(name string, cls cluster.Cluster) Storage {
cs := &clusterStorage{
name: name,
cls: cls,
}
err := cs.mutexGoReady()
if err != nil {
logger.Errorf(err.Error())
return cs
}
return cs
}
func (cs *clusterStorage) mutexGoReady() error {
if cs.mutex != nil {
return nil
}
mutex, err := cs.cls.Mutex(cs.name)
if err != nil {
return fmt.Errorf("create mutex for %s failed: %v", cs.name, err)
}
cs.mutex = mutex
return nil
}
func (cs *clusterStorage) Lock() error {
err := cs.mutexGoReady()
if err != nil {
return err
}
return cs.mutex.Lock()
}
func (cs *clusterStorage) Unlock() error {
err := cs.mutexGoReady()
if err != nil {
return err
}
return cs.mutex.Unlock()
}
func (cs *clusterStorage) Get(key string) (*string, error) {
return cs.cls.Get(key)
}
func (cs *clusterStorage) GetPrefix(prefix string) (map[string]string, error) {
return cs.cls.GetPrefix(prefix)
}
func (cs *clusterStorage) Put(key, value string) error {
return cs.cls.Put(key, value)
}
func (cs *clusterStorage) PutUnderLease(key, value string) error {
return cs.cls.PutUnderLease(key, value)
}
func (cs *clusterStorage) PutAndDelete(kvs map[string]*string) error {
return cs.cls.PutAndDelete(kvs)
}
func (cs *clusterStorage) PutAndDeleteUnderLease(kvs map[string]*string) error {
return cs.cls.PutAndDeleteUnderLease(kvs)
}
func (cs *clusterStorage) Delete(key string) error {
return cs.cls.Delete(key)
}
func (cs *clusterStorage) DeletePrefix(prefix string) error {
return cs.cls.DeletePrefix(prefix)
}
func (cs *clusterStorage) GetRaw(key string) (*mvccpb.KeyValue, error) {
return cs.cls.GetRaw(key)
}
func (cs *clusterStorage) GetRawPrefix(prefix string) (map[string]*mvccpb.KeyValue, error) {
return cs.cls.GetRawPrefix(prefix)
}
func (cs *clusterStorage) Syncer() (cluster.Syncer, error) {
return cs.cls.Syncer(time.Minute)
}
// GetFunctionStatusPrefix returns the prefix of one function statues.
func GetFunctionStatusPrefix(controllerName, functionName string) string {
return fmt.Sprintf(functionStatusPrefix, controllerName, functionName)
}
// GetFunctionSpecPrefix returns the prefix of one function spec.
func GetFunctionSpecPrefix(controllerName, functionName string) string {
return fmt.Sprintf(functionSpecPrefix, controllerName, functionName)
}
// GetAllFunctionSpecPrefix returns the prefix of one faasController's all functions specs.
func GetAllFunctionSpecPrefix(controllerName string) string {
return fmt.Sprintf(allFunctionsSpecPrefix, controllerName)
}
// GetAllFunctionStatusPrefix returns the prefix of one faasController's all functions status.
func GetAllFunctionStatusPrefix(controllerName string) string {
return fmt.Sprintf(allFunctionStatusPrefix, controllerName)
}