Skip to content

Commit

Permalink
Fix issue 414 CLC-418 (#417)
Browse files Browse the repository at this point in the history
Adds the logic of throwing the error if migration becomes failed in WaitForMigrationToBeInProgress.

JSON queries that are expected to return an array should use WITH WRAPPER (see https://docs.hazelcast.com/hazelcast/5.3/sql/working-with-json), I fixed that as well.
  • Loading branch information
srknzl authored Oct 30, 2023
1 parent deff095 commit a7deaa0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
33 changes: 15 additions & 18 deletions base/commands/migration/migration_stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
"strings"
"time"

"github.com/hazelcast/hazelcast-go-client"
"github.com/hazelcast/hazelcast-go-client/serialization"

"github.com/hazelcast/hazelcast-commandline-client/clc/ux/stage"
clcerrors "github.com/hazelcast/hazelcast-commandline-client/errors"
"github.com/hazelcast/hazelcast-commandline-client/internal/plug"
"github.com/hazelcast/hazelcast-go-client"
"github.com/hazelcast/hazelcast-go-client/serialization"
)

var timeoutErr = fmt.Errorf("migration could not be completed: reached timeout while reading status: "+
Expand Down Expand Up @@ -188,6 +189,13 @@ func WaitForMigrationToBeInProgress(ctx context.Context, ci *hazelcast.ClientInt
}
return err
}
if Status(status) == StatusFailed {
errs, err := fetchMigrationErrors(ctx, ci, migrationID)
if err != nil {
return fmt.Errorf("migration failed and dmt cannot fetch migration errors: %w", err)
}
return errors.New(errs)
}
if Status(status) == StatusInProgress {
return nil
}
Expand Down Expand Up @@ -235,28 +243,17 @@ func fetchMigrationReport(ctx context.Context, ci *hazelcast.ClientInternal, mig
}

func fetchMigrationErrors(ctx context.Context, ci *hazelcast.ClientInternal, migrationID string) (string, error) {
q := fmt.Sprintf(`SELECT JSON_QUERY(this, '$.errors') FROM %s WHERE __key='%s'`, StatusMapName, migrationID)
res, err := ci.Client().SQL().Execute(ctx, q)
q := fmt.Sprintf(`SELECT JSON_QUERY(this, '$.errors' WITH WRAPPER) FROM %s WHERE __key='%s'`, StatusMapName, migrationID)
row, err := querySingleRow(ctx, ci, q)
if err != nil {
return "", err
}
it, err := res.Iterator()
var errs []string
err = json.Unmarshal(row.(serialization.JSON), &errs)
if err != nil {
return "", err
}
var errs []string
for it.HasNext() {
row, err := it.Next()
if err != nil {
return "", err
}
r, err := row.Get(0)
if err != nil {
return "", err
}
errs = append(errs, strings.TrimPrefix(strings.TrimSuffix(string(r.(serialization.JSON)), `"`), `"`))
}
return strings.Join(errs, "\n"), nil
return "* " + strings.Join(errs, "\n* "), nil
}

func finalizeMigration(ctx context.Context, ec plug.ExecContext, ci *hazelcast.ClientInternal, migrationID, reportOutputDir string) error {
Expand Down
9 changes: 5 additions & 4 deletions base/commands/migration/start_stages_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ import (
"testing"
"time"

hz "github.com/hazelcast/hazelcast-go-client"
"github.com/hazelcast/hazelcast-go-client/serialization"
"github.com/stretchr/testify/require"

_ "github.com/hazelcast/hazelcast-commandline-client/base"
_ "github.com/hazelcast/hazelcast-commandline-client/base/commands"
"github.com/hazelcast/hazelcast-commandline-client/base/commands/migration"
"github.com/hazelcast/hazelcast-commandline-client/clc/paths"
. "github.com/hazelcast/hazelcast-commandline-client/internal/check"
"github.com/hazelcast/hazelcast-commandline-client/internal/it"
hz "github.com/hazelcast/hazelcast-go-client"
"github.com/hazelcast/hazelcast-go-client/serialization"
"github.com/stretchr/testify/require"
)

func TestMigrationStages(t *testing.T) {
Expand All @@ -42,7 +43,7 @@ func TestMigrationStages(t *testing.T) {
"testdata/start/migration_success_initial.json",
"testdata/start/migration_success_failure.json",
},
expectedErr: errors.New("Failed migrating IMAP: imap5: some error"),
expectedErr: errors.New("Failed migrating IMAP: imap5: * some error\n* another error"),
},
}
for _, tc := range testCases {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@
}
],
"logs": ["some user friendly log message", "another user friendly log message"],
"errors": ["some error"],
"errors": ["some error", "another error"],
"report": "failed migration report"
}

0 comments on commit a7deaa0

Please sign in to comment.