Skip to content

Commit

Permalink
Merge pull request #1179 from DiceDB/last-min
Browse files Browse the repository at this point in the history
Removing the redundant passing of slog.Logger
  • Loading branch information
arpitbbhayani authored Oct 22, 2024
2 parents 2372b12 + cace85f commit b2e2af0
Show file tree
Hide file tree
Showing 37 changed files with 269 additions and 359 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ FROM gcr.io/distroless/static-debian12:nonroot
WORKDIR /app
COPY --from=builder /dicedb/dicedb ./
EXPOSE 7379
ENV DICE_ENV=prod
CMD ["/app/dicedb"]

ENTRYPOINT ["/app/dicedb"]
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PORT ?= 7379 #Port for dicedb
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)

VERSION=$(shell bash -c 'grep -oP "const Version = \"\K[^\"]+" main.go')
VERSION=$(shell bash -c 'grep -oP "DiceDBVersion string = \"\K[^\"]+" config/config.go')

.PHONY: build test build-docker run test-one

Expand Down Expand Up @@ -58,10 +58,10 @@ unittest:
unittest-one:
go test -v -race -count=1 --run $(TEST_FUNC) ./internal/...

build-docker:
release:
git tag v$(VERSION)
git push origin --tags
docker build --tag dicedb/dicedb:latest --tag dicedb/dicedb:$(VERSION) .

push-docker:
docker push dicedb/dicedb:$(VERSION)

GOLANGCI_LINT_VERSION := 1.60.1
Expand Down
16 changes: 1 addition & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,14 @@ With this, you can build truly real-time applications like [Leaderboard](https:/
The easiest way to get started with DiceDB is using [Docker](https://www.docker.com/) by running the following command.

```bash
docker run -p 7379:7379 dicedb/dicedb
docker run -p 7379:7379 dicedb/dicedb --enable-multithreading --enable-watch
```

The above command will start the DiceDB server running locally on the port `7379` and you can connect
to it using DiceDB CLI and SDKs, or even Redis CLIs and SDKs.

> Note: Given it is a drop-in replacement of Redis, you can also use any Redis CLI and SDK to connect to DiceDB.
### Multi-Threading Mode (Experimental)

Multi-threading is currently under active development. To run the server with multi-threading enabled, follow these steps:

```bash
git clone https://github.com/dicedb/dice
cd dice
go run main.go --enable-multithreading --enable-watch
```

**Note:** Only the following commands are optimized for multithreaded execution: `PING, AUTH, SET, GET, GETSET, ABORT`

### Setting up DiceDB from source for development and contributions

To run DiceDB for local development or running from source, you will need
Expand Down Expand Up @@ -109,8 +97,6 @@ cd dice
air
```

> The `DICE_ENV` environment variable is used set the environment, by default it is treated as production. `dev` is used to get pretty printed logs and lower log level.
### Local Setup with Custom Config

By default, DiceDB will look for the configuration file at `/etc/dice/config.toml`. (Linux, Darwin, and WSL)
Expand Down
19 changes: 5 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

const (
DiceVersion string = "0.0.5"
DiceDBVersion string = "0.0.5"

DefaultHost string = "0.0.0.0"
DefaultPort int = 7379
Expand Down Expand Up @@ -125,7 +125,7 @@ type Config struct {

// Default configurations for internal use
var baseConfig = Config{
Version: DiceVersion,
Version: DiceDBVersion,
AsyncServer: struct {
Addr string `mapstructure:"addr"`
Port int `mapstructure:"port"`
Expand Down Expand Up @@ -201,7 +201,7 @@ var baseConfig = Config{
LogLevel string `mapstructure:"loglevel"`
PrettyPrintLogs bool `mapstructure:"prettyprintlogs"`
}{
LogLevel: "debug",
LogLevel: "info",
PrettyPrintLogs: true,
},
Auth: struct {
Expand All @@ -224,16 +224,8 @@ var defaultConfig Config

func init() {
config := baseConfig
env := os.Getenv("DICE_ENV")
if env == "prod" {
config.Logging.LogLevel = "info"
config.Logging.PrettyPrintLogs = false
}

if logLevel := os.Getenv("DICE_LOG_LEVEL"); logLevel != "" {
config.Logging.LogLevel = logLevel
}

config.Logging.PrettyPrintLogs = false
config.Logging.LogLevel = "info"
defaultConfig = config
}

Expand Down Expand Up @@ -351,7 +343,6 @@ func setUpViperConfig(configFilePath string) {

// override default configurations with command line flags
mergeFlagsWithConfig()
slog.Info("configurations loaded successfully.")
}

func mergeFlagsWithConfig() {
Expand Down
4 changes: 0 additions & 4 deletions dice.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ AOFFile = './dice-master.aof'
PersistenceEnabled = true
WriteAOFOnCleanup = false

[Logging]
LogLevel = 'debug'
PrettyPrintLogs = true

[Auth]
UserName = 'dice'
Password = ''
Expand Down
9 changes: 1 addition & 8 deletions integration_tests/commands/async/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,21 @@ package async

import (
"context"
"log/slog"
"os"
"sync"
"testing"
"time"

"github.com/dicedb/dice/internal/logger"
)

func TestMain(m *testing.M) {
l := logger.New(logger.Opts{WithTimestamp: false})
slog.SetDefault(l)

var wg sync.WaitGroup

// Run the test server
// This is a synchronous method, because internally it
// checks for available port and then forks a goroutine
// to start the server
opts := TestServerOptions{
Port: 8739,
Logger: l,
Port: 8739,
}
RunTestServer(context.Background(), &wg, opts)

Expand Down
13 changes: 6 additions & 7 deletions integration_tests/commands/async/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

type TestServerOptions struct {
Port int
Logger *slog.Logger
MaxClients int32
}

Expand Down Expand Up @@ -122,9 +121,9 @@ func RunTestServer(ctx context.Context, wg *sync.WaitGroup, opt TestServerOption
var err error
watchChan := make(chan dstore.QueryWatchEvent, config.DiceConfig.Memory.KeysLimit)
gec := make(chan error)
shardManager := shard.NewShardManager(1, watchChan, nil, gec, opt.Logger)
shardManager := shard.NewShardManager(1, watchChan, nil, gec)
// Initialize the AsyncServer
testServer := server.NewAsyncServer(shardManager, watchChan, opt.Logger)
testServer := server.NewAsyncServer(shardManager, watchChan)

// Try to bind to a port with a maximum of `totalRetries` retries.
for i := 0; i < totalRetries; i++ {
Expand All @@ -133,19 +132,19 @@ func RunTestServer(ctx context.Context, wg *sync.WaitGroup, opt TestServerOption
}

if err.Error() == "address already in use" {
opt.Logger.Info("Port already in use, trying port",
slog.Info("Port already in use, trying port",
slog.Int("port", config.DiceConfig.AsyncServer.Port),
slog.Int("new_port", config.DiceConfig.AsyncServer.Port+1),
)
config.DiceConfig.AsyncServer.Port++
} else {
opt.Logger.Error("Failed to bind port", slog.Any("error", err))
slog.Error("Failed to bind port", slog.Any("error", err))
return
}
}

if err != nil {
opt.Logger.Error("Failed to bind to a port after retries",
slog.Error("Failed to bind to a port after retries",
slog.Any("error", err),
slog.Int("retry_count", totalRetries),
)
Expand All @@ -172,7 +171,7 @@ func RunTestServer(ctx context.Context, wg *sync.WaitGroup, opt TestServerOption
cancelShardManager()
return
}
opt.Logger.Error("Test server encountered an error", slog.Any("error", err))
slog.Error("Test server encountered an error", slog.Any("error", err))
os.Exit(1)
}
}()
Expand Down
7 changes: 1 addition & 6 deletions integration_tests/commands/http/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@ package http

import (
"context"
"github.com/dicedb/dice/internal/logger"
"log/slog"
"os"
"sync"
"testing"
"time"
)

func TestMain(m *testing.M) {
l := logger.New(logger.Opts{WithTimestamp: false})
slog.SetDefault(l)
var wg sync.WaitGroup

// Run the test server
// This is a synchronous method, because internally it
// checks for available port and then forks a goroutine
// to start the server
opts := TestServerOptions{
Port: 8083,
Logger: l,
Port: 8083,
}
ctx, cancel := context.WithCancel(context.Background())
RunHTTPServer(ctx, &wg, opts)
Expand Down
6 changes: 3 additions & 3 deletions integration_tests/commands/http/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ func RunHTTPServer(ctx context.Context, wg *sync.WaitGroup, opt TestServerOption

globalErrChannel := make(chan error)
watchChan := make(chan dstore.QueryWatchEvent, config.DiceConfig.Performance.WatchChanBufSize)
shardManager := shard.NewShardManager(1, watchChan, nil, globalErrChannel, opt.Logger)
queryWatcherLocal := querymanager.NewQueryManager(opt.Logger)
shardManager := shard.NewShardManager(1, watchChan, nil, globalErrChannel)
queryWatcherLocal := querymanager.NewQueryManager()
config.HTTPPort = opt.Port
// Initialize the HTTPServer
testServer := server.NewHTTPServer(shardManager, opt.Logger)
testServer := server.NewHTTPServer(shardManager)
// Inform the user that the server is starting
fmt.Println("Starting the test server on port", config.HTTPPort)
shardManagerCtx, cancelShardManager := context.WithCancel(ctx)
Expand Down
9 changes: 1 addition & 8 deletions integration_tests/commands/resp/main_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
package resp

import (
"log/slog"
"os"
"sync"
"testing"
"time"

"github.com/dicedb/dice/internal/logger"
)

func TestMain(m *testing.M) {
logger := logger.New(logger.Opts{WithTimestamp: false})
slog.SetDefault(logger)

var wg sync.WaitGroup
// Run the test server
// This is a synchronous method, because internally it
// checks for available port and then forks a goroutine
// to start the server
opts := TestServerOptions{
Port: 9739,
Logger: logger,
Port: 9739,
}
RunTestServer(&wg, opts)

Expand Down
9 changes: 4 additions & 5 deletions integration_tests/commands/resp/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ func fireCommandAndGetRESPParser(conn net.Conn, cmd string) *clientio.RESPParser
}

func RunTestServer(wg *sync.WaitGroup, opt TestServerOptions) {
logr := logger.New(logger.Opts{WithTimestamp: true})
slog.SetDefault(logr)
slog.SetDefault(logger.New())
config.DiceConfig.Network.IOBufferLength = 16
config.DiceConfig.Persistence.WriteAOFOnCleanup = false
if opt.Port != 0 {
Expand All @@ -125,10 +124,10 @@ func RunTestServer(wg *sync.WaitGroup, opt TestServerOptions) {
queryWatchChan := make(chan dstore.QueryWatchEvent, config.DiceConfig.Memory.KeysLimit)
cmdWatchChan := make(chan dstore.CmdWatchEvent, config.DiceConfig.Memory.KeysLimit)
gec := make(chan error)
shardManager := shard.NewShardManager(1, queryWatchChan, cmdWatchChan, gec, logr)
shardManager := shard.NewShardManager(1, queryWatchChan, cmdWatchChan, gec)
workerManager := worker.NewWorkerManager(20000, shardManager)
// Initialize the RESP Server
testServer := resp.NewServer(shardManager, workerManager, cmdWatchChan, gec, logr)
testServer := resp.NewServer(shardManager, workerManager, cmdWatchChan, gec)

ctx, cancel := context.WithCancel(context.Background())
fmt.Println("Starting the test server on port", config.DiceConfig.AsyncServer.Port)
Expand All @@ -149,7 +148,7 @@ func RunTestServer(wg *sync.WaitGroup, opt TestServerOptions) {
cancelShardManager()
return
}
opt.Logger.Error("Test server encountered an error", slog.Any("error", err))
slog.Error("Test server encountered an error", slog.Any("error", err))
os.Exit(1)
}
}()
Expand Down
8 changes: 1 addition & 7 deletions integration_tests/commands/websocket/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,21 @@ package websocket

import (
"context"
"log/slog"
"os"
"sync"
"testing"
"time"

"github.com/dicedb/dice/internal/logger"
)

func TestMain(m *testing.M) {
l := logger.New(logger.Opts{WithTimestamp: false})
slog.SetDefault(l)
var wg sync.WaitGroup

// Run the test server
// This is a synchronous method, because internally it
// checks for available port and then forks a goroutine
// to start the server
opts := TestServerOptions{
Port: testPort1,
Logger: l,
Port: testPort1,
}
ctx, cancel := context.WithCancel(context.Background())
RunWebsocketServer(ctx, &wg, opts)
Expand Down
9 changes: 4 additions & 5 deletions integration_tests/commands/websocket/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,16 @@ func (e *WebsocketCommandExecutor) Name() string {
}

func RunWebsocketServer(ctx context.Context, wg *sync.WaitGroup, opt TestServerOptions) {
logger := opt.Logger
config.DiceConfig.Network.IOBufferLength = 16
config.DiceConfig.Persistence.WriteAOFOnCleanup = false

// Initialize WebsocketServer
globalErrChannel := make(chan error)
watchChan := make(chan dstore.QueryWatchEvent, config.DiceConfig.Performance.WatchChanBufSize)
shardManager := shard.NewShardManager(1, watchChan, nil, globalErrChannel, opt.Logger)
queryWatcherLocal := querymanager.NewQueryManager(opt.Logger)
shardManager := shard.NewShardManager(1, watchChan, nil, globalErrChannel)
queryWatcherLocal := querymanager.NewQueryManager()
config.WebsocketPort = opt.Port
testServer := server.NewWebSocketServer(shardManager, testPort1, opt.Logger)
testServer := server.NewWebSocketServer(shardManager, testPort1)
shardManagerCtx, cancelShardManager := context.WithCancel(ctx)

// run shard manager
Expand All @@ -138,7 +137,7 @@ func RunWebsocketServer(ctx context.Context, wg *sync.WaitGroup, opt TestServerO
if errors.Is(srverr, derrors.ErrAborted) {
return
}
logger.Debug("Websocket test server encountered an error: %v", slog.Any("error", srverr))
slog.Debug("Websocket test server encountered an error: %v", slog.Any("error", srverr))
}
}()
}
1 change: 0 additions & 1 deletion integration_tests/server/max_conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func TestMaxConnection(t *testing.T) {
var maxConnTestOptions = commands.TestServerOptions{
Port: 8741,
MaxClients: 50,
Logger: slog.Default(),
}
commands.RunTestServer(context.Background(), &wg, maxConnTestOptions)

Expand Down
Loading

0 comments on commit b2e2af0

Please sign in to comment.