// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package testcases

import (
	"time"

	"github.com/pingcap/test-infra/caselib/pkg/model/workload"
	"github.com/pingcap/test-infra/caselib/pkg/spec"
	"github.com/pingcap/test-infra/caselib/pkg/steps"
	"github.com/pingcap/test-infra/caselib/pkg/utils"
	_ "github.com/pingcap/test-infra/sdk/resource/impl/k8s"
)

// In this case, use go-ycsb as workload
// The binary is put in hub-new.pingcap.net/jianxu/go-ycsb
// This work load can used for dm sharding test, it can generate same schema with different data(PK)
// Config file /workloads/workloadd means PK like user3213123123
// Config file /workloads/workloadp means PK like ssh23123213
// recordcount is table size; operationcount is workload action nums.
// In /workloads/workloadd and /workloads/workloadp all action is insert.
// DDLStatement can add in tcms web page
func init() {
	var (
		taskName       = "test"
		sourceName0    = "source-0"
		sourceName1    = "source-1"
		name           = "dm_sharding_optimistic_add_index"
		dbName         = "test"
		env            = newCommonEnv()
		sourceNameList = []string{sourceName0, sourceName1}
		ddlStatement   string
	)
	var _ = AddTestCase(spec.CaseName(name), func() steps.Step {
		// env.cleanUpEnv(name)
		env.init().b.
			LoadParamFromEnv(utils.ParamPrefix+"ddlStatement", &ddlStatement).
			CreateDatabase(env.UpStreams[0], "test").
			CreateDatabase(env.UpStreams[1], "test").
			DmRunWorkload(env.UpStreams[0], env.Dm, env.Workload, &workload.QihooOption{
				Command:        workload.CommandTypeLoad,
				PropertyFile:   "/workloads/workloadd",
				PropertyValues: []string{"recordcount=100000", "autocommit=1", "databasename=" + dbName, "tablecount=1"},
				Threads:        20,
				Duration:       3 * time.Minute,
			}).
			DmRunWorkload(env.UpStreams[1], env.Dm, env.Workload, &workload.QihooOption{
				Command:        workload.CommandTypeLoad,
				PropertyFile:   "/workloads/workloadp",
				PropertyValues: []string{"recordcount=100000", "autocommit=1", "databasename=" + dbName, "tablecount=1"},
				Threads:        20,
				Duration:       3 * time.Minute,
			}).
			DmCreateSources(env.Dm, env.DmHost, Path.SourcePaths).
			DmStartTasks(env.Dm, env.DmHost, Path.TaskPaths).
			// TODO: Rmove sync diff check for the moment for dm_sharding_optimistic_003/dm_sharding_optimistic_004
			// TODO: After use github.com/pingcap/test-infra/tiflow to replace github.com/pingcap/test-infra/ticdc
			// Will reopen this step.
			DmWaitTaskSynced(env.DmHost, taskName, []string{sourceName0, sourceName1}).
			DmCheckSyncDiff(env.Dm, env.SyncDiff, env.DmHost, Path.DiffPaths[0]).
			Parallel(
				steps.NewRunDMWorkload(env.UpStreams[0], env.Workload, &workload.QihooOption{
					Command:        workload.CommandTypeRun,
					PropertyFile:   "/workloads/workloadd",
					PropertyValues: []string{"operationcount=500000", "autocommit=1", "databasename=" + dbName, "tablecount=1"},
					Threads:        20,
					Duration:       5 * time.Minute,
				}),
				steps.NewExecuteSqlStatement(env.UpStreams[1], &ddlStatement),
			).
			Parallel(
				steps.NewRunDMWorkload(env.UpStreams[1], env.Workload, &workload.QihooOption{
					Command:        workload.CommandTypeRun,
					PropertyFile:   "/workloads/workloadp",
					PropertyValues: []string{"operationcount=500000", "autocommit=1", "databasename=" + dbName, "tablecount=1"},
					Threads:        20,
					Duration:       5 * time.Minute,
				}),
				steps.NewExecuteSqlStatement(env.UpStreams[0], &ddlStatement),
			).
			DmWaitTaskSynced(env.DmHost, taskName, sourceNameList).
			DmCheckSyncDiff(env.Dm, env.SyncDiff, env.DmHost, Path.DiffPaths[0])
		return env.toBuilder().Build()

	})
}
