Skip to content

Commit

Permalink
fix inconsistencies in the Client API and improve Docker setup
Browse files Browse the repository at this point in the history
  • Loading branch information
buraksezer committed Apr 10, 2022
1 parent 737cf74 commit b6b1ffb
Show file tree
Hide file tree
Showing 22 changed files with 497 additions and 214 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN CGO_ENABLED=1 go build -ldflags="-s -w" -o /usr/bin/olricd /src/cmd/olricd

FROM gcr.io/distroless/base-debian10
COPY --from=build /usr/bin/olricd /usr/bin/olricd
COPY --from=build /src/cmd/olricd/olricd.yaml /etc/olricd.yaml
COPY --from=build /src/olricd-docker.yaml /etc/olricd.yaml

EXPOSE 3320 3322
ENTRYPOINT ["/usr/bin/olricd", "-c", "/etc/olricd.yaml"]
24 changes: 21 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ type statsConfig struct {

type StatsOption func(*statsConfig)

func CollectRuntime() StatsOption {
return func(cfg *statsConfig) {
cfg.CollectRuntime = true
}
}

type pubsubConfig struct {
Address string
}
Expand All @@ -196,13 +202,25 @@ func ToAddress(addr string) PubSubOption {

type PubSubOption func(option *pubsubConfig)

// Client is an interface that denotes an Olric client.
type Client interface {
// NewDMap returns a new DMap client with the given options.
NewDMap(name string, options ...DMapOption) (DMap, error)

// NewPubSub returns a new PubSub client with the given options.
NewPubSub(options ...PubSubOption) (*PubSub, error)
Stats(ctx context.Context, options ...StatsOption) (stats.Stats, error)
Ping(ctx context.Context, addr string) error
PingWithMessage(ctx context.Context, addr, message string) (string, error)

// Stats returns stats.Stats with the given options.
Stats(ctx context.Context, address string, options ...StatsOption) (stats.Stats, error)

Ping(ctx context.Context, address, message string) (string, error)

// RoutingTable returns the latest version of the routing table.
RoutingTable(ctx context.Context) (RoutingTable, error)

// Members returns a list of cluster members.
Members(ctx context.Context) ([]Member, error)

// Close stops background routines and frees allocated resources.
Close(ctx context.Context) error
}
39 changes: 13 additions & 26 deletions cluster_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/buraksezer/olric/internal/discovery"
"log"
"os"
"time"

"github.com/buraksezer/olric/config"
"github.com/buraksezer/olric/internal/bufpool"
"github.com/buraksezer/olric/internal/discovery"
"github.com/buraksezer/olric/internal/dmap"
"github.com/buraksezer/olric/internal/kvstore/entry"
"github.com/buraksezer/olric/internal/protocol"
Expand Down Expand Up @@ -376,34 +376,24 @@ type ClusterClient struct {
logger *log.Logger
}

func (cl *ClusterClient) Ping(ctx context.Context, addr string) error {
cmd := protocol.NewPing().Command(ctx)
rc := cl.client.Get(addr)
err := rc.Process(ctx, cmd)
if err != nil {
return processProtocolError(err)
func (cl *ClusterClient) Ping(ctx context.Context, addr, message string) (string, error) {
pingCmd := protocol.NewPing()
if message != "" {
pingCmd.SetMessage(message)
}
return processProtocolError(cmd.Err())

}
cmd := pingCmd.Command(ctx)

func (cl *ClusterClient) PingWithMessage(ctx context.Context, addr, message string) (string, error) {
cmd := protocol.NewPing().SetMessage(message).Command(ctx)
rc := cl.client.Get(addr)
err := rc.Process(ctx, cmd)
if err != nil {
return "", processProtocolError(err)

}
if err = cmd.Err(); err != nil {
return "", processProtocolError(err)

}
res, err := cmd.Bytes()
err = processProtocolError(cmd.Err())
if err != nil {
return "", processProtocolError(err)
return "", nil
}
return string(res), nil

return cmd.Result()
}

func (cl *ClusterClient) RoutingTable(ctx context.Context) (RoutingTable, error) {
Expand All @@ -430,7 +420,7 @@ func (cl *ClusterClient) RoutingTable(ctx context.Context) (RoutingTable, error)
return mapToRoutingTable(result)
}

func (cl *ClusterClient) Stats(ctx context.Context, options ...StatsOption) (stats.Stats, error) {
func (cl *ClusterClient) Stats(ctx context.Context, address string, options ...StatsOption) (stats.Stats, error) {
var cfg statsConfig
for _, opt := range options {
opt(&cfg)
Expand All @@ -442,12 +432,9 @@ func (cl *ClusterClient) Stats(ctx context.Context, options ...StatsOption) (sta
}

cmd := statsCmd.Command(ctx)
rc, err := cl.client.Pick()
if err != nil {
return stats.Stats{}, err
}
rc := cl.client.Get(address)

err = rc.Process(ctx, cmd)
err := rc.Process(ctx, cmd)
if err != nil {
return stats.Stats{}, processProtocolError(err)
}
Expand Down
33 changes: 28 additions & 5 deletions cluster_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ func TestClusterClient_Ping(t *testing.T) {
require.NoError(t, c.Close(ctx))
}()

err = c.Ping(ctx, db.rt.This().String())
response, err := c.Ping(ctx, db.rt.This().String(), "")
require.NoError(t, err)
require.Equal(t, DefaultPingResponse, response)
}

func TestClusterClient_PingWithMessage(t *testing.T) {
func TestClusterClient_Ping_WithMessage(t *testing.T) {
cluster := newTestOlricCluster(t)
cluster.addMember(t)
db := cluster.addMember(t)
Expand All @@ -56,7 +57,7 @@ func TestClusterClient_PingWithMessage(t *testing.T) {
}()

message := "Olric is the best!"
result, err := c.PingWithMessage(ctx, db.rt.This().String(), message)
result, err := c.Ping(ctx, db.rt.This().String(), message)
require.NoError(t, err)
require.Equal(t, message, result)
}
Expand Down Expand Up @@ -593,12 +594,34 @@ func TestClusterClient_Stats(t *testing.T) {
}()

var empty stats.Stats
s, err := c.Stats(ctx)
s, err := c.Stats(ctx, db.rt.This().String())
require.NoError(t, err)
require.Nil(t, s.Runtime)
require.NotEqual(t, empty, s)
}

func TestClusterClient_Stats_Cluster(t *testing.T) {
cluster := newTestOlricCluster(t)
db := cluster.addMember(t)
db2 := cluster.addMember(t)

<-time.After(250 * time.Millisecond)

ctx := context.Background()
c, err := NewClusterClient([]string{db.name})
require.NoError(t, err)
defer func() {
require.NoError(t, c.Close(ctx))
}()

var empty stats.Stats
s, err := c.Stats(ctx, db2.rt.This().String())
require.NoError(t, err)
require.Nil(t, s.Runtime)
require.NotEqual(t, empty, s)
require.Equal(t, db2.rt.This().String(), s.Member.String())
}

func TestClusterClient_Stats_CollectRuntime(t *testing.T) {
cluster := newTestOlricCluster(t)
db := cluster.addMember(t)
Expand All @@ -611,7 +634,7 @@ func TestClusterClient_Stats_CollectRuntime(t *testing.T) {
}()

var empty stats.Stats
s, err := c.Stats(ctx, CollectRuntime())
s, err := c.Stats(ctx, db.rt.This().String(), CollectRuntime())
require.NoError(t, err)
require.NotNil(t, s.Runtime)
require.NotEqual(t, empty, s)
Expand Down
4 changes: 2 additions & 2 deletions cmd/olricd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Distributed cache and in-memory data structure server.
Options:
-h, --help Print this message and exit.
-v, --version Print the version number and exit.
-c, --config Sets configuration file path. Default is olricd.yaml in the
-c, --config Sets configuration file path. Default is olricd-local.yaml in the
current folder. Set OLRICD_CONFIG to overwrite it.
The Go runtime version %s
Expand All @@ -58,7 +58,7 @@ type arguments struct {

const (
// DefaultConfigFile is the default configuration file path on a Unix-based operating system.
DefaultConfigFile = "olricd.yaml"
DefaultConfigFile = "olricd-local.yaml"

// EnvConfigFile is the name of environment variable which can be used to override default configuration file path.
EnvConfigFile = "OLRICD_CONFIG"
Expand Down
3 changes: 3 additions & 0 deletions cmd/olricd/olricd.yaml → cmd/olricd/olricd-local.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#
# IMPORTANT NOTE: This configuration file is intended for testing and local development.
#
olricd:
# BindAddr denotes the address that Olric will bind to for communication
# with other Olric nodes.
Expand Down
70 changes: 0 additions & 70 deletions dmap.go

This file was deleted.

4 changes: 2 additions & 2 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ services:
image: docker.io/olricio/olric-dev:latest
restart: on-failure
environment:
- OLRICD_CONFIG=/etc/olricd.yaml
- OLRICD_CONFIG=/etc/olricd-local.yaml
- CGO_ENABLED=1
command: bash -c "
cd /go/src/github.com/buraksezer/olric-consul-plugin &&
go build -buildmode=plugin -o /usr/lib/olric-consul-plugin.so &&
cd /go/src/github.com/buraksezer/olric &&
go build -v -o /go/bin/olricd /go/src/github.com/buraksezer/olric/cmd/olricd/main.go &&
/go/bin/olricd -c /go/src/github.com/buraksezer/olric/cmd/olricd/olricd.yaml"
/go/bin/olricd -c /go/src/github.com/buraksezer/olric/cmd/olricd/olricd-local.yaml"
volumes:
- ${PWD}/olricd-consul.yaml:/etc/olricd.yaml:ro
- ../:/go/src/github.com/buraksezer/olric
Expand Down
Loading

0 comments on commit b6b1ffb

Please sign in to comment.