Skip to content

Commit

Permalink
feat: add static, dns discovery providers
Browse files Browse the repository at this point in the history
  • Loading branch information
Tochemey committed Sep 14, 2024
1 parent 9693d68 commit e2b056a
Show file tree
Hide file tree
Showing 23 changed files with 766 additions and 11 deletions.
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,59 @@
# Go-KV

Simple Distributed in-memory key/value store.
[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/Tochemey/gokv/build.yml)]((https://github.com/Tochemey/gokv/actions/workflows/build.yml))

Simple Distributed in-memory key/value store. GoKV provides high availability and fault tolerance which makes it suitable large-scale applications system without sacrificing performance and reliability.
With GoKV, you can instantly create a fast, scalable, distributed system across a cluster of computers.


## Installation

```bash
go get github.com/tochemey/gokv
```

## Features

- Robust APIs to manipulate key/value pairs. See: [APIs](#apis)
- Discovery API to implement custom nodes discovery provider. See: [Discovery API](./discovery/provider.go)
- Comes bundled with some discovery providers that can help you hit the ground running:
- Kubernetes provider [Soon]
- [NATS](https://nats.io/) [integration](./discovery/nats) is fully functional
- [Static](./discovery/static) is fully functional and for demo purpose
- [DNS](./discovery/dnssd) is fully functional

## APIs

- `Put`: create key/value pair that is eventually distributed in the cluster of nodes. The `key` is a string and the `value` is a byte array.
- `Get`: retrieves the value of a given `key` from the cluster of nodes.
- `Delete`: delete a given `key` from the cluster. At the moment the `key` is marked to be `deleted`.
- `Exists`: check the existence of a given `key` in the cluster.

## Discovery Providers

### NATS Discovery Provider Setup

To use the NATS discovery provider one needs to provide the following:

- `Server`: the NATS Server address
- `Subject`: the NATS subject to use
- `Timeout`: the nodes discovery timeout
- `MaxJoinAttempts`: the maximum number of attempts to connect an existing NATs server. Defaults to `5`
- `ReconnectWait`: the time to backoff after attempting a reconnect to a server that we were already connected to previously. Default to `2 seconds`
- `DiscoveryPort`: the discovery port of the running node
- `Host`: the host address of the running node

#### DNS Provider Setup

This provider performs nodes discovery based upon the domain name provided. This is very useful when doing local development
using docker.

To use the DNS discovery provider one needs to provide the following:

- `DomainName`: the domain name
- `IPv6`: it states whether to lookup for IPv6 addresses.

#### Static Provider Setup

This provider performs nodes discovery based upon the list of static hosts addresses.
The address of each host is the form of `host:port` where `port` is the discovery port.
File renamed without changes.
4 changes: 2 additions & 2 deletions internal/cluster/config.go → cluster/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func (config *Config) WithDiscoveryProvider(provider discovery.Provider) *Config
}

// WithDiscoveryPort sets the discovery port
func (config *Config) WithDiscoveryPort(gossipPort uint16) *Config {
config.discoveryPort = gossipPort
func (config *Config) WithDiscoveryPort(port uint16) *Config {
config.discoveryPort = port
return config
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion internal/cluster/node_test.go → cluster/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import (
natsserver "github.com/nats-io/nats-server/v2/server"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tochemey/goakt/v2/discovery"
"github.com/travisjeffery/go-dynaport"

"github.com/tochemey/gokv/discovery"
"github.com/tochemey/gokv/discovery/nats"
"github.com/tochemey/gokv/internal/lib"
"github.com/tochemey/gokv/log"
Expand Down
File renamed without changes.
43 changes: 43 additions & 0 deletions discovery/dnssd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* MIT License
*
* Copyright (c) 2022-2024 Tochemey
*
* 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 dnssd

import "github.com/tochemey/gokv/internal/validation"

// Config defines the discovery configuration
type Config struct {
// Domain specifies the dns name
DomainName string
// IPv6 states whether to fetch ipv6 address instead of ipv4
// if it is false then all addresses are extracted
IPv6 *bool
}

// Validate checks whether the given discovery configuration is valid
func (x Config) Validate() error {
return validation.New(validation.FailFast()).
AddValidator(validation.NewEmptyStringValidator("Namespace", x.DomainName)).
Validate()
}
46 changes: 46 additions & 0 deletions discovery/dnssd/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* MIT License
*
* Copyright (c) 2022-2024 Tochemey
*
* 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 dnssd

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestConfig(t *testing.T) {
t.Run("With valid configuration", func(t *testing.T) {
config := &Config{
DomainName: "google.com",
}
assert.NoError(t, config.Validate())
})
t.Run("With invalid configuration", func(t *testing.T) {
config := &Config{
DomainName: "",
}
assert.Error(t, config.Validate())
})
}
158 changes: 158 additions & 0 deletions discovery/dnssd/discovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* MIT License
*
* Copyright (c) 2022-2024 Tochemey
*
* 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 dnssd

import (
"context"
"net"
"sync"

goset "github.com/deckarep/golang-set/v2"
"go.uber.org/atomic"

"github.com/tochemey/gokv/discovery"
)

const (
DomainName = "domain-name"
IPv6 = "ipv6"
)

// Discovery represents the DNS service discovery
// IP addresses are looked up by querying the default
// DNS resolver for A and AAAA records associated with the DNS name.
type Discovery struct {
mu sync.Mutex
config *Config

// states whether the actor system has started or not
initialized *atomic.Bool
}

// enforce compilation error
var _ discovery.Provider = &Discovery{}

// NewDiscovery returns an instance of the DNS discovery provider
func NewDiscovery(config *Config) *Discovery {
return &Discovery{
mu: sync.Mutex{},
config: config,
initialized: atomic.NewBool(false),
}
}

// ID returns the discovery provider id
func (d *Discovery) ID() string {
return "dns-sd"
}

// Initialize initializes the plugin: registers some internal data structures, clients etc.
func (d *Discovery) Initialize() error {
d.mu.Lock()
defer d.mu.Unlock()
if d.initialized.Load() {
return discovery.ErrAlreadyInitialized
}

return d.config.Validate()
}

// Register registers this node to a service discovery directory.
func (d *Discovery) Register() error {
d.mu.Lock()
defer d.mu.Unlock()

if d.initialized.Load() {
return discovery.ErrAlreadyRegistered
}

d.initialized = atomic.NewBool(true)
return nil
}

// Deregister removes this node from a service discovery directory.
func (d *Discovery) Deregister() error {
d.mu.Lock()
defer d.mu.Unlock()

if !d.initialized.Load() {
return discovery.ErrNotInitialized
}
d.initialized = atomic.NewBool(false)
return nil
}

// DiscoverPeers returns a list of known nodes.
func (d *Discovery) DiscoverPeers() ([]string, error) {
d.mu.Lock()
defer d.mu.Unlock()

if !d.initialized.Load() {
return nil, discovery.ErrNotInitialized
}

ctx := context.Background()

// set ipv6 filter
v6 := false
if d.config.IPv6 != nil {
v6 = *d.config.IPv6
}

var err error

// only extract ipv6
if v6 {
ips, err := net.DefaultResolver.LookupIP(ctx, "ip6", d.config.DomainName)
if err != nil {
return nil, err
}

ipList := make([]string, len(ips))
for index, ip := range ips {
ipList[index] = ip.String()
}

return goset.NewSet[string](ipList...).ToSlice(), nil
}

// lookup the addresses based upon the dns name
addrs, err := net.DefaultResolver.LookupIPAddr(ctx, d.config.DomainName)
if err != nil {
return nil, err
}

ipList := make([]string, len(addrs))
for index, addr := range addrs {
ipList[index] = addr.IP.String()
}

return goset.NewSet[string](ipList...).ToSlice(), nil
}

// Close closes the provider
func (d *Discovery) Close() error {
return nil
}
Loading

0 comments on commit e2b056a

Please sign in to comment.