-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
1,651 additions
and
173 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
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,22 @@ | ||
package common | ||
|
||
import ( | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
"log" | ||
"time" | ||
) | ||
|
||
func CreateGRPCClient(endpoint string) *grpc.ClientConn { | ||
var opts []grpc.DialOption | ||
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
for range 5 { | ||
conn, err := grpc.NewClient(endpoint, opts...) | ||
if err == nil { | ||
return conn | ||
} | ||
time.Sleep(time.Second) // Wait before retrying | ||
} | ||
log.Panicln("failed to dial") | ||
return nil | ||
} |
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,155 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
table_types "github.com/ydb-platform/ydb-go-sdk/v3/table/types" | ||
"google.golang.org/grpc" | ||
"google.golang.org/protobuf/types/known/durationpb" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
"log" | ||
"reflect" | ||
"time" | ||
"ydbcp/cmd/integration/common" | ||
"ydbcp/internal/config" | ||
"ydbcp/internal/connectors/db" | ||
"ydbcp/internal/connectors/db/yql/queries" | ||
"ydbcp/internal/types" | ||
pb "ydbcp/pkg/proto/ydbcp/v1alpha1" | ||
) | ||
|
||
const ( | ||
containerID = "abcde" | ||
databaseName = "/local" | ||
ydbcpEndpoint = "localhost:50051" | ||
connectionString = "grpcs://local-ydb:2135/local" | ||
) | ||
|
||
var ( | ||
t1 = time.Date(2024, 01, 01, 00, 00, 00, 0, time.UTC) | ||
t2 = time.Date(2024, 01, 02, 00, 00, 00, 0, time.UTC) | ||
) | ||
|
||
func OperationsToInsert() []types.TakeBackupWithRetryOperation { | ||
schedule := "schedule" | ||
ttl := time.Hour * 24 | ||
return []types.TakeBackupWithRetryOperation{ | ||
{ | ||
TakeBackupOperation: types.TakeBackupOperation{ | ||
ID: "1", | ||
ContainerID: containerID, | ||
State: "xz", | ||
Message: "nz", | ||
YdbConnectionParams: types.YdbConnectionParams{ | ||
Endpoint: ydbcpEndpoint, | ||
DatabaseName: databaseName, | ||
}, | ||
SourcePaths: []string{"path"}, | ||
SourcePathsToExclude: []string{"exclude"}, | ||
Audit: &pb.AuditInfo{ | ||
Creator: "ydbcp", | ||
CreatedAt: timestamppb.New(t1), | ||
CompletedAt: timestamppb.New(t2), | ||
}, | ||
UpdatedAt: timestamppb.New(t2), | ||
}, | ||
ScheduleID: &schedule, | ||
Ttl: &ttl, | ||
RetryConfig: &pb.RetryConfig{Retries: &pb.RetryConfig_Count{Count: 5}}, | ||
}, | ||
{ | ||
TakeBackupOperation: types.TakeBackupOperation{ | ||
ID: "1", | ||
ContainerID: containerID, | ||
State: "xz", | ||
Message: "nz", | ||
YdbConnectionParams: types.YdbConnectionParams{ | ||
Endpoint: ydbcpEndpoint, | ||
DatabaseName: databaseName, | ||
}, | ||
SourcePaths: []string{"path"}, | ||
SourcePathsToExclude: []string{"exclude"}, | ||
Audit: &pb.AuditInfo{ | ||
Creator: "ydbcp", | ||
CreatedAt: timestamppb.New(t1), | ||
CompletedAt: timestamppb.New(t2), | ||
}, | ||
UpdatedAt: timestamppb.New(t2), | ||
}, | ||
ScheduleID: &schedule, | ||
Ttl: &ttl, | ||
RetryConfig: &pb.RetryConfig{Retries: &pb.RetryConfig_MaxBackoff{MaxBackoff: durationpb.New(ttl)}}, | ||
}, | ||
} | ||
} | ||
|
||
func ReadTBWROperation(ctx context.Context, ydbConn *db.YdbConnector, id string) *types.TakeBackupWithRetryOperation { | ||
operations, err := ydbConn.SelectOperations(ctx, queries.NewReadTableQuery( | ||
queries.WithTableName("Operations"), | ||
queries.WithQueryFilters(queries.QueryFilter{ | ||
Field: "id", | ||
Values: []table_types.Value{table_types.StringValueFromString(id)}, | ||
}), | ||
)) | ||
if err != nil { | ||
log.Panicf("failed to select operations: %v", err) | ||
} | ||
if len(operations) != 1 { | ||
log.Panicf("expected 1 operation, got %d", len(operations)) | ||
} | ||
return operations[0].(*types.TakeBackupWithRetryOperation) | ||
} | ||
|
||
func TestTBWROperationORM(ctx context.Context, ydbConn *db.YdbConnector, operation types.TakeBackupWithRetryOperation) { | ||
err := ydbConn.ExecuteUpsert(ctx, queries.NewWriteTableQuery().WithCreateOperation(&operation)) | ||
if err != nil { | ||
log.Panicf("failed to insert operation: %v", err) | ||
} | ||
tbwr := ReadTBWROperation(ctx, ydbConn, operation.ID) | ||
if !reflect.DeepEqual(operation, *tbwr) { | ||
log.Panicf("operation %v corrupted after read and write\ngot %v", operation, *tbwr) | ||
} | ||
|
||
err = ydbConn.ExecuteUpsert(ctx, queries.NewWriteTableQuery().WithUpdateOperation(&operation)) | ||
if err != nil { | ||
log.Panicf("failed to insert operation: %v", err) | ||
} | ||
tbwr = ReadTBWROperation(ctx, ydbConn, operation.ID) | ||
if !reflect.DeepEqual(operation, *tbwr) { | ||
log.Panicf("operation %v corrupted after update and read\ngot %v", operation, *tbwr) | ||
} | ||
operation.Message = "xxx" | ||
err = ydbConn.ExecuteUpsert(ctx, queries.NewWriteTableQuery().WithUpdateOperation(&operation)) | ||
if err != nil { | ||
log.Panicf("failed to insert operation: %v", err) | ||
} | ||
tbwr = ReadTBWROperation(ctx, ydbConn, operation.ID) | ||
if "xxx" != tbwr.Message { | ||
log.Panicf("operation %v did not change after update", *tbwr) | ||
} | ||
} | ||
|
||
func main() { | ||
ctx := context.Background() | ||
conn := common.CreateGRPCClient(ydbcpEndpoint) | ||
defer func(conn *grpc.ClientConn) { | ||
err := conn.Close() | ||
if err != nil { | ||
log.Panicln("failed to close connection") | ||
} | ||
}(conn) | ||
ydbConn, err := db.NewYdbConnector( | ||
ctx, | ||
config.YDBConnectionConfig{ | ||
ConnectionString: connectionString, | ||
Insecure: true, | ||
Discovery: false, | ||
DialTimeoutSeconds: 10, | ||
}, | ||
) | ||
if err != nil { | ||
log.Panicf("failed to create ydb connector: %v", err) | ||
} | ||
for _, op := range OperationsToInsert() { | ||
TestTBWROperationORM(ctx, ydbConn, op) | ||
} | ||
} |
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
Oops, something went wrong.