-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check if server's and client's versions are compatible (#65)
* Check if server's and client's versions are compatible * Use slog * Address review
- Loading branch information
Showing
4 changed files
with
180 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package qdrant | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"strconv" | ||
"strings" | ||
"time" | ||
"unicode" | ||
) | ||
|
||
const fullVersionParts = 3 | ||
const reducedVersionParts = 2 | ||
const unknownVersion = "Unknown" | ||
|
||
type Version struct { | ||
Major int | ||
Minor int | ||
} | ||
|
||
func getServerVersion(clientConn *GrpcClient) string { | ||
logger := slog.Default() | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) | ||
defer cancel() | ||
healthCheckResult, err := clientConn.qdrant.HealthCheck(ctx, &HealthCheckRequest{}) | ||
if err != nil { | ||
logger.Warn("Unable to get server version, use default", "err", err, "default", unknownVersion) | ||
return unknownVersion | ||
} | ||
serverVersion := healthCheckResult.GetVersion() | ||
|
||
return serverVersion | ||
} | ||
|
||
func removeLeadingNonNumeric(versionStr string) string { | ||
return strings.TrimLeftFunc(versionStr, func(r rune) bool { | ||
return !unicode.IsDigit(r) | ||
}) | ||
} | ||
|
||
// ParseVersion converts a version string "x.y[.z]" into a Version struct. | ||
func ParseVersion(versionStr string) (*Version, error) { | ||
cleanedVersionStr := removeLeadingNonNumeric(versionStr) | ||
parts := strings.SplitN(cleanedVersionStr, ".", fullVersionParts) | ||
if len(parts) < reducedVersionParts { | ||
return nil, fmt.Errorf("unable to parse version, expected format: x.y[.z], found: %s", cleanedVersionStr) | ||
} | ||
|
||
major, err := strconv.Atoi(parts[0]) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse major version: %w", err) | ||
} | ||
|
||
minor, err := strconv.Atoi(parts[1]) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse minor version: %w", err) | ||
} | ||
|
||
return &Version{ | ||
Major: major, | ||
Minor: minor, | ||
}, nil | ||
} | ||
|
||
func IsCompatible(clientVersion, serverVersion string) bool { | ||
if clientVersion == serverVersion { | ||
return true | ||
} | ||
logger := slog.Default() | ||
client, err := ParseVersion(clientVersion) | ||
if err != nil { | ||
logger.Warn("Unable to compare versions", "err", err) | ||
return false | ||
} | ||
|
||
server, err := ParseVersion(serverVersion) | ||
if err != nil { | ||
logger.Warn("Unable to compare versions", "err", err) | ||
return false | ||
} | ||
|
||
if client.Major != server.Major { | ||
return false | ||
} | ||
|
||
diff := client.Minor - server.Minor | ||
return diff <= 1 && diff >= -1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package qdrant_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/qdrant/go-client/qdrant" | ||
) | ||
|
||
func TestParseVersion(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected *qdrant.Version | ||
hasError bool | ||
}{ | ||
{"v1.2.3", &qdrant.Version{Major: 1, Minor: 2}, false}, | ||
{"1.2.3", &qdrant.Version{Major: 1, Minor: 2}, false}, | ||
{"v1.2", &qdrant.Version{Major: 1, Minor: 2}, false}, | ||
{"1.2", &qdrant.Version{Major: 1, Minor: 2}, false}, | ||
{"v1.2.3.4", &qdrant.Version{Major: 1, Minor: 2}, false}, | ||
{"", nil, true}, | ||
{"1", nil, true}, | ||
{"1.", nil, true}, | ||
{".1.", nil, true}, | ||
{"1.something.1", nil, true}, | ||
} | ||
|
||
for _, test := range tests { | ||
result, err := qdrant.ParseVersion(test.input) | ||
if (err != nil) != test.hasError { | ||
t.Errorf("ParseVersion(%q) error = %v, wantErr %v", test.input, err, test.hasError) | ||
continue | ||
} | ||
if !test.hasError && result != nil && (result.Major != test.expected.Major || | ||
result.Minor != test.expected.Minor) { | ||
t.Errorf("ParseVersion(%q) = %v, want %v", test.input, result, test.expected) | ||
} | ||
} | ||
} | ||
|
||
func TestIsCompatible(t *testing.T) { | ||
tests := []struct { | ||
clientVersion string | ||
serverVersion string | ||
expected bool | ||
}{ | ||
{"1.9.3.dev0", "2.8.1.dev12-something", false}, | ||
{"1.9", "2.8", false}, | ||
{"1", "2", false}, | ||
{"1", "1", true}, | ||
{"1.9.0", "2.9.0", false}, | ||
{"1.1.0", "1.2.9", true}, | ||
{"1.2.7", "1.1.8.dev0", true}, | ||
{"1.2.1", "1.2.29", true}, | ||
{"1.2.0", "1.2.0", true}, | ||
{"1.2.0", "1.4.0", false}, | ||
{"1.4.0", "1.2.0", false}, | ||
{"1.9.0", "3.7.0", false}, | ||
{"3.0.0", "1.0.0", false}, | ||
} | ||
|
||
for _, test := range tests { | ||
clientVersion := test.clientVersion | ||
serverVersion := test.serverVersion | ||
result := qdrant.IsCompatible(clientVersion, serverVersion) | ||
if result != test.expected { | ||
t.Errorf("IsCompatible(%q, %q) = %v, want %v", clientVersion, serverVersion, result, test.expected) | ||
} | ||
} | ||
} |