package qihoo360testcase

import (
	"time"

	"github.com/pingcap/test-infra/caselib/pkg/model/workload"
	"github.com/pingcap/test-infra/caselib/pkg/steps"
	"github.com/pingcap/test-infra/caselib/ticdc/common"
)

// OwnerSwitchSync
// create tidb cluster / mysql / workload
// create changefeed
// run workload
// Switch Owner: by pod failure chaos, duration as 15s, for duration as 5m, it will be canceled when finish mark synced to downstream.
// wait for workload finish
// check consistencyowner_switch_sync

func Qihoo360OwnerSwitchSync() steps.Step {
	return steps.
		NewBuilder().
		LoadEnv().
		LoadResourceFromEnv("TI_PARAM_RES_upstream", common.ResourceHandleSource).
		LoadResourceFromEnv("TI_PARAM_RES_downstream", common.ResourceHandleMySQL).
		LoadResourceFromEnv("TI_PARAM_RES_workload", common.ResourceHandleWorkload).
		InitCDCComponents(common.ResourceHandleSource, common.ResourceHandleMySQL).
		InitWorkload(common.ResourceHandleWorkload).
		RunWorkload(&workload.QihooOption{
			Command:        workload.CommandTypeRun,
			PropertyFile:   "./workloads/oncall2929", // property file is located inside 360 workload image
			PropertyValues: []string{"operationcount=50000", "autocommit=1", "databasename=" + common.DBName, "tablecount=200", "exectime=3600"},
			Threads:        200,
			Duration:       3 * time.Hour, // we do not know how long the command will take, set it to 8 at the moment.
		}).
		Parallel(
			steps.NewPeriodicalTask(30*time.Minute, 9*time.Minute,
				steps.NewSwitchOwner(common.ResourceHandleSource, 20*time.Second)),

			steps.NewRunWorkload(&workload.QihooOption{
				Command:        workload.CommandTypeRun,
				PropertyFile:   "./workloads/oncall2929", // property file is located inside 360 workload image
				PropertyValues: []string{"operationcount=5000000", "autocommit=1", "databasename=" + common.DBName, "tablecount=200", "exectime=3600"},
				Threads:        200,
				Duration:       2 * time.Hour, // we do not know how long the command will take, set it to 24 at the moment.
			}),
		).
		SendFinishMark(common.DBName).
		WaitFinishMark(common.ResourceHandleMySQL, common.DBName, 2*time.Hour).
		CheckConsistency(common.ResourceHandleSource, common.ResourceHandleMySQL, common.DBName).
		Build()
}
