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 Time Command #2963

Merged
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
297caea
Implemented Time Command
niharikabhavaraju Jan 16, 2025
1177732
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 17, 2025
1244741
Fixed code review changes
niharikabhavaraju Jan 17, 2025
579f0f3
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 19, 2025
3716426
Added Time command (cluster)
niharikabhavaraju Jan 19, 2025
b13f411
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 20, 2025
d20ea15
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 22, 2025
bcc00a7
Fixed code review comments
niharikabhavaraju Jan 22, 2025
92886ba
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 27, 2025
2ce5dbc
Fixed code review comments for multi cluster value
niharikabhavaraju Jan 27, 2025
0e04a4e
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 27, 2025
5364841
Fixed linting error
niharikabhavaraju Jan 27, 2025
fb97914
Merge branch 'niharika-timecommand' of github.com:niharikabhavaraju/v…
niharikabhavaraju Jan 27, 2025
ff976a7
Fixed code review changes
niharikabhavaraju Jan 28, 2025
6a64db9
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 28, 2025
355b339
Fix linting error
niharikabhavaraju Jan 28, 2025
6e55e05
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 28, 2025
27aa766
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 29, 2025
55d5d83
Merge branch 'niharika-timecommand' of github.com:niharikabhavaraju/v…
niharikabhavaraju Jan 29, 2025
88c3e2d
Fixed code review comments and failing tests
niharikabhavaraju Jan 29, 2025
4ee885f
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 30, 2025
e1c22a0
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 30, 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
21 changes: 21 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6819,3 +6819,24 @@ func (client *baseClient) XRevRangeWithOptions(
}
return handleMapOfArrayOfStringArrayOrNilResponse(result)
}

// Returns the server time.
//
// Return value:
// The current server time as a String array with two elements:
// A UNIX TIME and the amount of microseconds already elapsed in the current second.
// The returned array is in a [UNIX TIME, Microseconds already elapsed] format.
//
// For example:
//
// result, err := client.Time()
// result: [{1737051660} {994688}]
//
// [valkey.io]: https://valkey.io/commands/time/
func (client *baseClient) Time() ([]string, error) {
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
result, err := client.executeCommand(C.Time, []string{})
if err != nil {
return nil, err
}
return handleRawStringArrayResponse(result)
}
45 changes: 45 additions & 0 deletions go/api/glide_cluster_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ package api
// #include "../lib.h"
import "C"

import (
"github.com/valkey-io/valkey-glide/go/glide/api/options"
)

// GlideClusterClient interface compliance check.
var _ GlideClusterClientCommands = (*GlideClusterClient)(nil)

// GlideClusterClientCommands is a client used for connection in cluster mode.
type GlideClusterClientCommands interface {
BaseClient
GenericClusterCommands
ServerManagementClusterCommands
}

// GlideClusterClient implements cluster mode operations by extending baseClient functionality.
Expand Down Expand Up @@ -69,3 +74,43 @@ func (client *GlideClusterClient) CustomCommand(args []string) (ClusterValue[int
}
return CreateClusterValue(data), nil
}

// Returns the server time.
//
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
// See [valkey.io] for details.
//
// Parameters:
//
// options - The TimeOptions type.
//
// Return value:
//
// The current server time as a String array with two elements: A UNIX TIME and the amount
// of microseconds already elapsed in the current second.
// The returned array is in a [UNIX TIME, Microseconds already elapsed] format.
//
// Example:
//
// route := api.SimpleNodeRoute(api.RandomRoute)
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
// route := config.Route(config.AllNodes)
// opts := options.ClusterTimeOptions{
// Route: &route,
// }
// fmt.Println(clusterResponse.Value()) // Output: [1737994354 547816 1737994354 547856]
//
// [valkey.io]: https://valkey.io/commands/time/
func (client *GlideClusterClient) TimeWithOptions(opts options.ClusterTimeOptions) (ClusterValue[[]string], error) {
if opts.Route == nil {
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
response, err := client.executeCommand(C.Time, []string{})
if err != nil {
return CreateEmptyStringArrayClusterValue(), err
}
return handleTimeClusterResponse(response)
} else {
result, err := client.executeCommandWithRoute(C.Time, []string{}, *opts.Route)
if err != nil {
return CreateEmptyStringArrayClusterValue(), err
}
return handleTimeClusterResponse(result)
}
}
7 changes: 7 additions & 0 deletions go/api/options/time_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package options
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved

import "github.com/valkey-io/valkey-glide/go/glide/api/config"

type ClusterTimeOptions struct {
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
Route *config.Route
}
86 changes: 86 additions & 0 deletions go/api/response_handlers.go
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -977,3 +977,89 @@ func handleXPendingDetailResponse(response *C.struct_CommandResponse) ([]XPendin

return pendingDetails, nil
}

func handleRawStringArrayResponse(response *C.struct_CommandResponse) ([]string, error) {
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
defer C.free_command_response(response)

typeErr := checkResponseType(response, C.Array, false)
if typeErr != nil {
return nil, typeErr
}

slice := make([]string, 0, response.array_value_len)
for _, v := range unsafe.Slice(response.array_value, response.array_value_len) {
err := checkResponseType(&v, C.String, false)
if err != nil {
return nil, err
}

if v.string_value == nil {
return nil, &errors.RequestError{Msg: "Unexpected nil string in array"}
}

byteSlice := C.GoBytes(unsafe.Pointer(v.string_value), C.int(int64(v.string_value_len)))
slice = append(slice, string(byteSlice))
}

return slice, nil
}

func handleRawStringArrayMapResponse(response *C.struct_CommandResponse) (map[string][]string, error) {
defer C.free_command_response(response)
typeErr := checkResponseType(response, C.Map, false)
if typeErr != nil {
return nil, typeErr
}

result := make(map[string][]string)
for _, v := range unsafe.Slice(response.array_value, response.array_value_len) {
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
key, err := convertCharArrayToString(v.map_key, true)
if err != nil {
return nil, err
}

err = checkResponseType(v.map_value, C.Array, false)
if err != nil {
return nil, err
}

timeStrings := make([]string, 0, v.map_value.array_value_len)
for _, strVal := range unsafe.Slice(v.map_value.array_value, v.map_value.array_value_len) {
err := checkResponseType(&strVal, C.String, false)
if err != nil {
return nil, err
}
if strVal.string_value == nil {
return nil, &errors.RequestError{Msg: "Unexpected nil string in array"}
}
byteSlice := C.GoBytes(unsafe.Pointer(strVal.string_value), C.int(int64(strVal.string_value_len)))
timeStrings = append(timeStrings, string(byteSlice))
}

result[key.Value()] = timeStrings
}

return result, nil
}

func handleTimeClusterResponse(response *C.struct_CommandResponse) (ClusterValue[[]string], error) {
// Handle multi-node response
if err := checkResponseType(response, C.Map, true); err == nil {
mapData, err := handleRawStringArrayMapResponse(response)
if err != nil {
return CreateEmptyStringArrayClusterValue(), err
}
var times []string
for _, nodeTimes := range mapData {
times = append(times, nodeTimes...)
}
return CreateClusterMultiValue(times), nil
}

// Handle single node response
data, err := handleRawStringArrayResponse(response)
if err != nil {
return CreateEmptyStringArrayClusterValue(), err
}
return CreateClusterSingleValue(data), nil
}
7 changes: 7 additions & 0 deletions go/api/response_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ func CreateEmptyClusterValue() ClusterValue[interface{}] {
}
}

func CreateEmptyStringArrayClusterValue() ClusterValue[[]string] {
var empty []string
return ClusterValue[[]string]{
value: Result[[]string]{val: empty, isNil: true},
}
}

// XPendingSummary represents a summary of pending messages in a stream group.
// It includes the total number of pending messages, the ID of the first and last pending messages,
// and a list of consumer pending messages.
Expand Down
14 changes: 14 additions & 0 deletions go/api/server_management_cluster_commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

package api

import "github.com/valkey-io/valkey-glide/go/glide/api/options"

// ServerManagementClusterCommands supports commands for the "Server Management Commands" group for cluster client.
//
// See [valkey.io] for details.
//
// [valkey.io]: https://valkey.io/commands/#server
type ServerManagementClusterCommands interface {
TimeWithOptions(timeOptions options.ClusterTimeOptions) (ClusterValue[[]string], error)
}
2 changes: 2 additions & 0 deletions go/api/server_management_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ type ServerManagementCommands interface {
ConfigSet(parameters map[string]string) (string, error)

DBSize() (int64, error)

Time() ([]string, error)
}
53 changes: 53 additions & 0 deletions go/integTest/cluster_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"

"github.com/stretchr/testify/assert"
"github.com/valkey-io/valkey-glide/go/glide/api/config"
"github.com/valkey-io/valkey-glide/go/glide/api/options"
)

func (suite *GlideTestSuite) TestClusterCustomCommandInfo() {
Expand All @@ -27,3 +29,54 @@ func (suite *GlideTestSuite) TestClusterCustomCommandEcho() {
// ECHO is routed to a single random node
assert.Equal(suite.T(), "GO GLIDE GO", result.Value().(string))
}

func (suite *GlideTestSuite) TestTimeWithoutRoute() {
client := suite.defaultClusterClient()
options := options.ClusterTimeOptions{Route: nil}
result, err := client.TimeWithOptions(options)

assert.NoError(suite.T(), err)
assert.NotNil(suite.T(), result)
assert.NotEmpty(suite.T(), result.Value())
assert.IsType(suite.T(), "", result.Value()[0])
assert.Equal(suite.T(), 2, len(result.Value()))
}

func (suite *GlideTestSuite) TestTimeWithAllNodesRoute() {
client := suite.defaultClusterClient()
route := config.Route(config.AllNodes)
options := options.ClusterTimeOptions{Route: &route}
result, err := client.TimeWithOptions(options)

assert.NoError(suite.T(), err)
assert.NotNil(suite.T(), result)
assert.NotEmpty(suite.T(), result.Value())
assert.Greater(suite.T(), len(result.Value()), 1)

for _, timeStr := range result.Value() {
assert.IsType(suite.T(), "", timeStr)
}
}

func (suite *GlideTestSuite) TestTimeWithRandomRoute() {
client := suite.defaultClusterClient()
route := config.Route(config.RandomRoute)
options := options.ClusterTimeOptions{Route: &route}
result, err := client.TimeWithOptions(options)

assert.NoError(suite.T(), err)
assert.NotNil(suite.T(), result)
assert.NotEmpty(suite.T(), result.Value())
assert.IsType(suite.T(), "", result.Value()[0])
assert.Equal(suite.T(), 2, len(result.Value()))
}

func (suite *GlideTestSuite) TestTimeWithInvalidRoute() {
client := suite.defaultClusterClient()
invalidRoute := config.Route(config.NewByAddressRoute("invalidHost", 9999))
options := options.ClusterTimeOptions{Route: &invalidRoute}
result, err := client.TimeWithOptions(options)

assert.NotNil(suite.T(), err)
assert.Empty(suite.T(), result.Value())
}
33 changes: 33 additions & 0 deletions go/integTest/standalone_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package integTest

import (
"fmt"
"strconv"
"strings"
"time"

"github.com/google/uuid"
"github.com/valkey-io/valkey-glide/go/glide/api"
Expand Down Expand Up @@ -392,3 +394,34 @@ func (suite *GlideTestSuite) TestDBSize() {
assert.Nil(suite.T(), err)
assert.Greater(suite.T(), result, int64(0))
}

func (suite *GlideTestSuite) TestTime_Success() {
client := suite.defaultClient()
results, err := client.Time()

assert.Nil(suite.T(), err)
assert.Len(suite.T(), results, 2)

now := time.Now().Unix() - 1

timestamp, err := strconv.ParseInt(results[0], 10, 64)
assert.Nil(suite.T(), err)
assert.Greater(suite.T(), timestamp, now)

microseconds, err := strconv.ParseInt(results[1], 10, 64)
assert.Nil(suite.T(), err)
assert.Less(suite.T(), microseconds, int64(1000000))
}

func (suite *GlideTestSuite) TestTime_Error() {
client := suite.defaultClient()

// Disconnect the client or simulate an error condition
client.Close()

results, err := client.Time()

assert.NotNil(suite.T(), err)
assert.Nil(suite.T(), results)
assert.IsType(suite.T(), &errors.ClosingError{}, err)
}
Loading