2020-05-13 08:27:19 +00:00
|
|
|
/*
|
2020-05-25 16:19:20 +00:00
|
|
|
Copyright 2020 Qiniu Cloud (qiniu.com)
|
2020-05-13 08:27:19 +00:00
|
|
|
|
|
|
|
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 cover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-09-25 07:13:36 +00:00
|
|
|
"errors"
|
2020-05-13 08:27:19 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-09-25 07:13:36 +00:00
|
|
|
"path/filepath"
|
2020-05-13 08:27:19 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2020-09-20 12:29:17 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2020-05-13 08:27:19 +00:00
|
|
|
)
|
|
|
|
|
2020-09-23 06:14:01 +00:00
|
|
|
var ErrServiceAlreadyRegistered = errors.New("service already registered")
|
|
|
|
|
2020-05-13 08:27:19 +00:00
|
|
|
// Store persistents the registered service information
|
|
|
|
type Store interface {
|
|
|
|
// Add adds the given service to store
|
2020-09-23 06:14:01 +00:00
|
|
|
Add(s ServiceUnderTest) error
|
2020-05-13 08:27:19 +00:00
|
|
|
|
2020-06-07 06:03:47 +00:00
|
|
|
// Get returns the registered service information with the given service's name
|
2020-05-13 08:27:19 +00:00
|
|
|
Get(name string) []string
|
|
|
|
|
2020-06-07 06:03:47 +00:00
|
|
|
// Get returns all the registered service information as a map
|
2020-05-13 08:27:19 +00:00
|
|
|
GetAll() map[string][]string
|
|
|
|
|
2020-06-07 06:03:47 +00:00
|
|
|
// Init cleanup all the registered service information
|
2020-05-13 08:27:19 +00:00
|
|
|
Init() error
|
2020-06-15 09:16:50 +00:00
|
|
|
|
|
|
|
// Set stores the services information into internal state
|
2020-09-29 02:40:04 +00:00
|
|
|
Set(services map[string][]string) error
|
2020-09-16 09:16:57 +00:00
|
|
|
|
2020-09-20 12:29:17 +00:00
|
|
|
// Remove the service from the store by address
|
|
|
|
Remove(addr string) error
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 09:16:50 +00:00
|
|
|
// fileStore holds the registered services into memory and persistent to a local file
|
|
|
|
type fileStore struct {
|
2020-05-13 08:27:19 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
persistentFile string
|
2020-06-15 09:16:50 +00:00
|
|
|
|
|
|
|
memoryStore Store
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 09:16:50 +00:00
|
|
|
// NewFileStore creates a store using local file
|
2020-09-25 07:13:36 +00:00
|
|
|
func NewFileStore(persistenceFile string) (store Store, err error) {
|
2020-09-23 06:14:01 +00:00
|
|
|
path, err := filepath.Abs(persistenceFile)
|
|
|
|
if err != nil {
|
2020-09-25 07:13:36 +00:00
|
|
|
return nil, err
|
2020-09-23 06:14:01 +00:00
|
|
|
}
|
|
|
|
err = os.MkdirAll(filepath.Dir(path), os.ModePerm)
|
|
|
|
if err != nil {
|
2020-09-25 07:13:36 +00:00
|
|
|
return nil, err
|
2020-09-23 06:14:01 +00:00
|
|
|
}
|
2020-06-15 09:16:50 +00:00
|
|
|
l := &fileStore{
|
2020-09-23 06:14:01 +00:00
|
|
|
persistentFile: path,
|
2020-06-15 09:16:50 +00:00
|
|
|
memoryStore: NewMemoryStore(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := l.load(); err != nil {
|
|
|
|
log.Fatalf("load failed, file: %s, err: %v", l.persistentFile, err)
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 07:13:36 +00:00
|
|
|
return l, nil
|
2020-06-15 09:16:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds the given service to file Store
|
2020-09-23 06:14:01 +00:00
|
|
|
func (l *fileStore) Add(s ServiceUnderTest) error {
|
2020-09-25 07:13:36 +00:00
|
|
|
if err := l.memoryStore.Add(s); err != nil {
|
|
|
|
return err
|
2020-09-23 06:14:01 +00:00
|
|
|
}
|
2020-06-15 09:16:50 +00:00
|
|
|
|
2020-06-07 06:03:47 +00:00
|
|
|
// persistent to local store
|
2020-06-15 09:16:50 +00:00
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
2020-05-13 08:27:19 +00:00
|
|
|
return l.appendToFile(s)
|
|
|
|
}
|
|
|
|
|
2020-06-07 06:03:47 +00:00
|
|
|
// Get returns the registered service information with the given name
|
2020-06-15 09:16:50 +00:00
|
|
|
func (l *fileStore) Get(name string) []string {
|
|
|
|
return l.memoryStore.Get(name)
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 06:03:47 +00:00
|
|
|
// Get returns all the registered service information
|
2020-06-15 09:16:50 +00:00
|
|
|
func (l *fileStore) GetAll() map[string][]string {
|
|
|
|
return l.memoryStore.GetAll()
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-20 12:29:17 +00:00
|
|
|
// Remove the service from the memory store and the file store
|
|
|
|
func (l *fileStore) Remove(addr string) error {
|
|
|
|
err := l.memoryStore.Remove(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-29 02:40:04 +00:00
|
|
|
return l.Set(l.memoryStore.GetAll())
|
2020-09-20 12:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 06:03:47 +00:00
|
|
|
// Init cleanup all the registered service information
|
2020-05-13 08:27:19 +00:00
|
|
|
// and the local persistent file
|
2020-06-15 09:16:50 +00:00
|
|
|
func (l *fileStore) Init() error {
|
|
|
|
if err := l.memoryStore.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-13 08:27:19 +00:00
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
if err := os.Remove(l.persistentFile); err != nil && !os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("failed to delete file %s, err: %v", l.persistentFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-07 06:03:47 +00:00
|
|
|
// load all registered service from file to memory
|
2020-06-15 09:16:50 +00:00
|
|
|
func (l *fileStore) load() error {
|
2020-05-13 08:27:19 +00:00
|
|
|
var svrsMap = make(map[string][]string, 0)
|
|
|
|
|
|
|
|
f, err := os.Open(l.persistentFile)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2020-06-15 09:16:50 +00:00
|
|
|
return nil
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
2020-06-15 09:16:50 +00:00
|
|
|
return fmt.Errorf("failed to open file, path: %s, err: %v", l.persistentFile, err)
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
ns := bufio.NewScanner(f)
|
|
|
|
for ns.Scan() {
|
|
|
|
line := ns.Text()
|
|
|
|
ss := strings.FieldsFunc(line, split)
|
|
|
|
|
|
|
|
// TODO: use regex
|
|
|
|
if len(ss) == 2 {
|
|
|
|
if urls, ok := svrsMap[ss[0]]; ok {
|
|
|
|
urls = append(urls, ss[1])
|
|
|
|
svrsMap[ss[0]] = urls
|
|
|
|
} else {
|
|
|
|
svrsMap[ss[0]] = []string{ss[1]}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ns.Err(); err != nil {
|
2020-06-15 09:16:50 +00:00
|
|
|
return fmt.Errorf("read file failed, file: %s, err: %v", l.persistentFile, err)
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 09:16:50 +00:00
|
|
|
// set information to memory
|
|
|
|
l.memoryStore.Set(svrsMap)
|
|
|
|
return nil
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 02:40:04 +00:00
|
|
|
func (l *fileStore) Set(services map[string][]string) error {
|
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
|
|
|
|
// no error will return from memorystore.set
|
2020-10-21 02:08:16 +00:00
|
|
|
err := l.memoryStore.Set(services)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-29 02:40:04 +00:00
|
|
|
|
|
|
|
f, err := os.OpenFile(l.persistentFile, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := ""
|
|
|
|
for name, addrs := range services {
|
|
|
|
for _, addr := range addrs {
|
|
|
|
s += fmt.Sprintf("%s&%s\n", name, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = f.WriteString(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-21 02:08:16 +00:00
|
|
|
return f.Sync()
|
2020-06-15 09:16:50 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 06:14:01 +00:00
|
|
|
func (l *fileStore) appendToFile(s ServiceUnderTest) error {
|
2020-05-13 08:27:19 +00:00
|
|
|
f, err := os.OpenFile(l.persistentFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = f.WriteString(format(s) + "\n")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Sync()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-23 06:14:01 +00:00
|
|
|
func format(s ServiceUnderTest) string {
|
2020-05-13 08:27:19 +00:00
|
|
|
return fmt.Sprintf("%s&%s", s.Name, s.Address)
|
|
|
|
}
|
|
|
|
|
|
|
|
func split(r rune) bool {
|
|
|
|
return r == '&'
|
|
|
|
}
|
|
|
|
|
2020-06-15 09:16:50 +00:00
|
|
|
// memoryStore holds the registered services only into memory
|
|
|
|
type memoryStore struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
servicesMap map[string][]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMemoryStore creates a memory store
|
|
|
|
func NewMemoryStore() Store {
|
|
|
|
return &memoryStore{
|
|
|
|
servicesMap: make(map[string][]string, 0),
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
2020-06-15 09:16:50 +00:00
|
|
|
}
|
2020-05-13 08:27:19 +00:00
|
|
|
|
2020-06-15 09:16:50 +00:00
|
|
|
// Add adds the given service to MemoryStore
|
2020-09-23 06:14:01 +00:00
|
|
|
func (l *memoryStore) Add(s ServiceUnderTest) error {
|
2020-06-15 09:16:50 +00:00
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
// load to memory
|
|
|
|
if addrs, ok := l.servicesMap[s.Name]; ok {
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if addr == s.Address {
|
|
|
|
log.Printf("service registered already, name: %s, address: %s", s.Name, s.Address)
|
2020-09-23 06:14:01 +00:00
|
|
|
return ErrServiceAlreadyRegistered
|
2020-06-15 09:16:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
addrs = append(addrs, s.Address)
|
|
|
|
l.servicesMap[s.Name] = addrs
|
|
|
|
} else {
|
|
|
|
l.servicesMap[s.Name] = []string{s.Address}
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
2020-06-15 09:16:50 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the registered service information with the given name
|
|
|
|
func (l *memoryStore) Get(name string) []string {
|
|
|
|
l.mu.RLock()
|
|
|
|
defer l.mu.RUnlock()
|
|
|
|
return l.servicesMap[name]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns all the registered service information
|
|
|
|
func (l *memoryStore) GetAll() map[string][]string {
|
2020-12-10 15:35:15 +00:00
|
|
|
res := make(map[string][]string)
|
2020-06-15 09:16:50 +00:00
|
|
|
l.mu.RLock()
|
|
|
|
defer l.mu.RUnlock()
|
2020-12-13 06:29:40 +00:00
|
|
|
for k, v := range l.servicesMap {
|
|
|
|
res[k] = append(make([]string, 0, len(v)), v...)
|
2020-12-10 15:35:15 +00:00
|
|
|
}
|
|
|
|
return res
|
2020-06-15 09:16:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init cleanup all the registered service information
|
|
|
|
// and the local persistent file
|
|
|
|
func (l *memoryStore) Init() error {
|
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
|
|
|
|
l.servicesMap = make(map[string][]string, 0)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-29 02:40:04 +00:00
|
|
|
func (l *memoryStore) Set(services map[string][]string) error {
|
2020-06-15 09:16:50 +00:00
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
|
2020-05-13 08:27:19 +00:00
|
|
|
l.servicesMap = services
|
2020-09-29 02:40:04 +00:00
|
|
|
|
|
|
|
return nil
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
2020-09-16 09:16:57 +00:00
|
|
|
|
2020-09-20 12:29:17 +00:00
|
|
|
// Remove one service from the memory store
|
|
|
|
// if service is not fount, return "no service found" error
|
|
|
|
func (l *memoryStore) Remove(removeAddr string) error {
|
2020-09-16 09:16:57 +00:00
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
|
2020-09-20 12:29:17 +00:00
|
|
|
flag := false
|
|
|
|
for name, addrs := range l.servicesMap {
|
|
|
|
newAddrs := make([]string, 0)
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if removeAddr != addr {
|
|
|
|
newAddrs = append(newAddrs, addr)
|
|
|
|
} else {
|
|
|
|
flag = true
|
|
|
|
}
|
2020-09-16 09:16:57 +00:00
|
|
|
}
|
2020-09-20 12:29:17 +00:00
|
|
|
// if no services left, remove by name
|
|
|
|
if len(newAddrs) == 0 {
|
|
|
|
delete(l.servicesMap, name)
|
|
|
|
} else {
|
|
|
|
l.servicesMap[name] = newAddrs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !flag {
|
|
|
|
return fmt.Errorf("no service found")
|
2020-09-16 09:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|