Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go: Implement Config Rewrite #3156

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
267b000
Implement Rewrite
EdricCua Feb 12, 2025
5f0e614
Add Example Doc
EdricCua Mar 4, 2025
208a2c4
Updates from main
EdricCua Mar 18, 2025
05b8e69
Fix review comment
EdricCua Mar 18, 2025
f77befd
Remove main.go
EdricCua Mar 18, 2025
15d6446
update from main
EdricCua Mar 18, 2025
7724d59
fix review comment
EdricCua Mar 18, 2025
652df62
update from main
EdricCua Mar 18, 2025
4f303aa
fix examples
EdricCua Mar 19, 2025
d04cee9
fix examples
EdricCua Mar 19, 2025
0679680
fix test
EdricCua Mar 20, 2025
6301745
update from main
EdricCua Mar 20, 2025
01254b7
fix test
EdricCua Mar 21, 2025
6d9a7e7
Fix test
EdricCua Mar 21, 2025
699d46f
update from main
EdricCua Mar 21, 2025
9768199
fix docs
EdricCua Mar 21, 2025
d1f2e29
fix unit test
EdricCua Mar 21, 2025
4528c4e
fix unit test
EdricCua Mar 21, 2025
dec046f
fix unit test
EdricCua Mar 21, 2025
ebbb0cd
fix unit test
EdricCua Mar 21, 2025
a823003
fix unit test
EdricCua Mar 21, 2025
0609398
fix unit test
EdricCua Mar 21, 2025
cb32f89
fix unit test
EdricCua Mar 21, 2025
a4375f9
fix unit test
EdricCua Mar 21, 2025
f6d92b3
fix unit test
EdricCua Mar 21, 2025
33939f8
fix unit test
EdricCua Mar 21, 2025
af7bbcf
fix unit test
EdricCua Mar 22, 2025
7de14cb
fix unit test
EdricCua Mar 22, 2025
683681c
fix unit test
EdricCua Mar 22, 2025
1b6a164
fix unit test
EdricCua Mar 22, 2025
16bbe89
fix unit test
EdricCua Mar 22, 2025
bf92f1d
fix unit test
EdricCua Mar 24, 2025
4d0bd87
add changelog
EdricCua Mar 24, 2025
bcbf740
update from main
EdricCua Mar 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* Go: Add password update api ([#3346](https://github.com/valkey-io/valkey-glide/pull/3346))
* Go: Add `GeoHash` ([#3439](https://github.com/valkey-io/valkey-glide/pull/3439))
* Go/Core: Move FFI to a dedicated folder for reusability ([#3372](https://github.com/valkey-io/valkey-glide/pull/3372))
* Go: Implement Rewrite ([#3156](https://github.com/valkey-io/valkey-glide/pull/3156))

#### Breaking Changes

Expand Down
15 changes: 15 additions & 0 deletions go/api/glide_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,18 @@ func (client *GlideClient) FlushDBWithOptions(mode options.FlushMode) (string, e
}
return handleStringResponse(result)
}

// Rewrites the configuration file with the current configuration.
//
// Return value:
//
// "OK" when the configuration was rewritten properly, otherwise an error is thrown.
//
// [valkey.io]: https://valkey.io/commands/config-rewrite/
func (client *GlideClient) ConfigRewrite() (string, error) {
response, err := client.executeCommand(C.ConfigRewrite, []string{})
if err != nil {
return DefaultStringResponse, err
}
return handleStringResponse(response)
}
36 changes: 36 additions & 0 deletions go/api/glide_cluster_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,39 @@ func (client *GlideClusterClient) ScanWithOptions(
nextCursor, keys, err := handleScanResponse(response)
return *options.NewClusterScanCursorWithId(nextCursor), keys, err
}

// Rewrites the configuration file with the current configuration.
// The command will be routed a random node.
//
// Return value:
//
// "OK" when the configuration was rewritten properly, otherwise an error is thrown.
//
// [valkey.io]: https://valkey.io/commands/config-rewrite/
func (client *GlideClusterClient) ConfigRewrite() (string, error) {
response, err := client.executeCommand(C.ConfigRewrite, []string{})
if err != nil {
return DefaultStringResponse, err
}
return handleStringResponse(response)
}

// Rewrites the configuration file with the current configuration.
//
// Parameters:
//
// opts - Specifies the routing configuration for the command. The client will route the
// command to the nodes defined by route.
//
// Return value:
//
// "OK" when the configuration was rewritten properly, otherwise an error is thrown.
//
// [valkey.io]: https://valkey.io/commands/config-rewrite/
func (client *GlideClusterClient) ConfigRewriteWithOptions(opts options.RouteOption) (string, error) {
response, err := client.executeCommandWithRoute(C.ConfigRewrite, []string{}, opts.Route)
if err != nil {
return DefaultStringResponse, err
}
return handleStringResponse(response)
}
4 changes: 4 additions & 0 deletions go/api/server_management_cluster_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ type ServerManagementClusterCommands interface {
FlushDB() (string, error)

FlushDBWithOptions(options options.FlushClusterOptions) (string, error)

ConfigRewrite() (string, error)

ConfigRewriteWithOptions(routeOption options.RouteOption) (string, error)
}
142 changes: 140 additions & 2 deletions go/api/server_management_cluster_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ func ExampleGlideClusterClient_DBSizeWithOptions() {
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}

fmt.Println(result)

// Output: 0
// Output:
// 0
}

func ExampleGlideClusterClient_FlushAll() {
Expand Down Expand Up @@ -152,3 +152,141 @@ func ExampleGlideClusterClient_FlushDBWithOptions() {

// Output: OK
}

func ExampleGlideClusterClient_ConfigRewrite() {
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function
var resultRewrite string
opts := options.ClusterInfoOptions{
InfoOptions: &options.InfoOptions{Sections: []options.Section{options.Server}},
}
res, err := client.InfoWithOptions(opts)
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
for _, data := range res.MultiValue() {
lines := strings.Split(data, "\n")
var configFile string
for _, line := range lines {
if strings.HasPrefix(line, "config_file:") {
configFile = strings.TrimSpace(strings.TrimPrefix(line, "config_file:"))
break
}
}
if len(configFile) > 0 {
responseRewrite, err := client.ConfigRewrite()
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
resultRewrite = responseRewrite
break
} else {
resultRewrite = "OK"
}

}
fmt.Println(resultRewrite)

// Output:
// OK
}

func ExampleGlideClusterClient_ConfigRewriteWithOptions() {
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function
sections := []options.Section{options.Server}

// info with option or with multiple options without route
var runResultNilRoute string
opts := options.ClusterInfoOptions{
InfoOptions: &options.InfoOptions{Sections: sections},
RouteOption: nil,
}
response, err := client.InfoWithOptions(opts)
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}

for _, data := range response.MultiValue() {
lines := strings.Split(data, "\n")
var configFile string
for _, line := range lines {
if strings.HasPrefix(line, "config_file:") {
configFile = strings.TrimSpace(strings.TrimPrefix(line, "config_file:"))
break
}
}
if len(configFile) > 0 {
responseRewrite, err := client.ConfigRewrite()
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
runResultNilRoute = responseRewrite
break
}
runResultNilRoute = "OK"
}

// same sections with random route
var runResultRandomRoute string
opts = options.ClusterInfoOptions{
InfoOptions: &options.InfoOptions{Sections: sections},
RouteOption: &options.RouteOption{Route: config.RandomRoute},
}
response, err = client.InfoWithOptions(opts)
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
lines := strings.Split(response.SingleValue(), "\n")
var configFile string
for _, line := range lines {
if strings.HasPrefix(line, "config_file:") {
configFile = strings.TrimSpace(strings.TrimPrefix(line, "config_file:"))
break
}
}
if len(configFile) > 0 {
responseRewrite, err := client.ConfigRewrite()
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
runResultRandomRoute = responseRewrite
}
runResultRandomRoute = "OK"

// default sections, multi node route
var runResultMultiNodeRoute string
opts = options.ClusterInfoOptions{
InfoOptions: nil,
RouteOption: &options.RouteOption{Route: config.AllPrimaries},
}
response, err = client.InfoWithOptions(opts)
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
for _, data := range response.MultiValue() {
lines := strings.Split(data, "\n")
var configFile string
for _, line := range lines {
if strings.HasPrefix(line, "config_file:") {
configFile = strings.TrimSpace(strings.TrimPrefix(line, "config_file:"))
break
}
}
if len(configFile) > 0 {
responseRewrite, err := client.ConfigRewrite()
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
runResultMultiNodeRoute = responseRewrite
break
}
runResultMultiNodeRoute = "OK"
}
fmt.Println("Multiple options without route result:", runResultNilRoute)
fmt.Println("Random route result:", runResultRandomRoute)
fmt.Println("Multi node route result:", runResultMultiNodeRoute)

// Output:
// Multiple options without route result: OK
// Random route result: OK
// Multi node route result: OK
}
2 changes: 2 additions & 0 deletions go/api/server_management_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ type ServerManagementCommands interface {
FlushDB() (string, error)

FlushDBWithOptions(mode options.FlushMode) (string, error)

ConfigRewrite() (string, error)
}
32 changes: 32 additions & 0 deletions go/api/server_management_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package api
import (
"fmt"
"strconv"
"strings"
"time"

"github.com/valkey-io/valkey-glide/go/api/options"
Expand Down Expand Up @@ -161,3 +162,34 @@ func ExampleGlideClient_FlushDBWithOptions() {

// Output: OK
}

func ExampleGlideClient_ConfigRewrite() {
var client *GlideClient = getExampleGlideClient() // example helper function
opts := options.InfoOptions{Sections: []options.Section{options.Server}}
var resultRewrite string
response, err := client.InfoWithOptions(opts)
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
lines := strings.Split(response, "\n")
var configFile string
for _, line := range lines {
if strings.HasPrefix(line, "config_file:") {
configFile = strings.TrimSpace(strings.TrimPrefix(line, "config_file:"))
break
}
}
if len(configFile) > 0 {
response, err = client.ConfigRewrite()
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
resultRewrite = response
} else {
resultRewrite = "OK"
}
fmt.Println(resultRewrite)

// Output:
// OK
}
100 changes: 100 additions & 0 deletions go/integTest/cluster_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1099,3 +1099,103 @@ func (suite *GlideTestSuite) TestUpdateConnectionPasswordCluster_ImmediateAuthWr
_, err = adminClient.CustomCommand([]string{"CONFIG", "SET", "requirepass", ""})
assert.NoError(suite.T(), err)
}

func (suite *GlideTestSuite) TestConfigRewriteCluster() {
client := suite.defaultClusterClient()
t := suite.T()
opts := options.ClusterInfoOptions{
InfoOptions: &options.InfoOptions{Sections: []options.Section{options.Server}},
}
res, err := client.InfoWithOptions(opts)
assert.NoError(t, err)
for _, data := range res.MultiValue() {
lines := strings.Split(data, "\n")
var configFile string
for _, line := range lines {
if strings.HasPrefix(line, "config_file:") {
configFile = strings.TrimSpace(strings.TrimPrefix(line, "config_file:"))
break
}
}
if len(configFile) > 0 {
responseRewrite, err := client.ConfigRewrite()
assert.NoError(t, err)
assert.Equal(t, "OK", responseRewrite)
}
}
}

func (suite *GlideTestSuite) TestConfigRewriteWithOptions() {
client := suite.defaultClusterClient()
t := suite.T()
sections := []options.Section{options.Server}

// info with option or with multiple options without route
opts := options.ClusterInfoOptions{
InfoOptions: &options.InfoOptions{Sections: sections},
RouteOption: nil,
}
response, err := client.InfoWithOptions(opts)
assert.NoError(t, err)
for _, data := range response.MultiValue() {
lines := strings.Split(data, "\n")
var configFile string
for _, line := range lines {
if strings.HasPrefix(line, "config_file:") {
configFile = strings.TrimSpace(strings.TrimPrefix(line, "config_file:"))
break
}
}
if len(configFile) > 0 {
responseRewrite, err := client.ConfigRewrite()
assert.NoError(t, err)
assert.Equal(t, "OK", responseRewrite)
break
}
}

// same sections with random route
opts = options.ClusterInfoOptions{
InfoOptions: &options.InfoOptions{Sections: sections},
RouteOption: &options.RouteOption{Route: config.RandomRoute},
}
response, err = client.InfoWithOptions(opts)
assert.NoError(t, err)
lines := strings.Split(response.SingleValue(), "\n")
var configFile string
for _, line := range lines {
if strings.HasPrefix(line, "config_file:") {
configFile = strings.TrimSpace(strings.TrimPrefix(line, "config_file:"))
break
}
}
if len(configFile) > 0 {
responseRewrite, err := client.ConfigRewrite()
assert.NoError(t, err)
assert.Equal(t, "OK", responseRewrite)
}

// default sections, multi node route
opts = options.ClusterInfoOptions{
InfoOptions: nil,
RouteOption: &options.RouteOption{Route: config.AllPrimaries},
}
response, err = client.InfoWithOptions(opts)
assert.NoError(t, err)
for _, data := range response.MultiValue() {
lines := strings.Split(data, "\n")
var configFile string
for _, line := range lines {
if strings.HasPrefix(line, "config_file:") {
configFile = strings.TrimSpace(strings.TrimPrefix(line, "config_file:"))
break
}
}
if len(configFile) > 0 {
responseRewrite, err := client.ConfigRewrite()
assert.NoError(t, err)
assert.Equal(t, "OK", responseRewrite)
break
}
}
}
Loading
Loading