// 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/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"
		sourceNameList = []string{sourceName0, sourceName1}
		name           = "dm_sharding_pessimistic_online_ddl"
		env            = newCommonEnvWithOsc()
		checkOscStmt   = "select pt_col, gh_col from " + ycsbTable + " limit 1"
	)
	getYcbsOption := func(commandType workload.CommandType, workloadName string, duration time.Duration, properties ...string) *workload.QihooOption {
		properties = append(properties, "autocommit=1", "mysql.db="+name, "tablecount=1")
		return &workload.QihooOption{
			Command:        commandType,
			PropertyFile:   "/workloads/" + workloadName,
			PropertyValues: properties,
			Threads:        1, // it takes about 2 minutes to generate 1G data using 1 thread
			Duration:       duration,
		}
	}
	AddTestCase(spec.CaseName(name), func() steps.Step {
		return env.
			init().
			// cleanUpEnv(name).
			initDbsAndSourcesAndTasks(name).
			toBuilder().
			DmRunWorkload(env.UpStreams[0], env.Dm, env.Workload, getYcbsOption(workload.CommandTypeLoad, "workloadd", 3*time.Minute, "recordcount=100")).
			DmRunWorkload(env.UpStreams[1], env.Dm, env.Workload, getYcbsOption(workload.CommandTypeLoad, "workloadp", 3*time.Minute, "recordcount=100")).
			Parallel(
				// each shard has 1 KB * 500K = 500MB data
				steps.NewRunDMWorkload(env.UpStreams[0], env.Workload, getYcbsOption(workload.CommandTypeRun, "workloadd", 5*time.Minute, "operationcount=500000")),
				steps.NewRunDMWorkload(env.UpStreams[1], env.Workload, getYcbsOption(workload.CommandTypeRun, "workloadp", 5*time.Minute, "operationcount=500000")),
				steps.NewSerial(
					steps.NewSleepTask(10*time.Second),
					steps.NewParallel(
						steps.NewOnlineSchemaChangeStep(env.UpStreams[0], env.Osc, steps.PtOsc, "add column pt_col int default 111", name, ycsbTable),
						steps.NewOnlineSchemaChangeStep(env.UpStreams[1], env.Osc, steps.GhostOsc, "add column pt_col int default 111", name, ycsbTable),
					),
					steps.NewSleepTask(5*time.Second),
					steps.NewParallel(
						steps.NewOnlineSchemaChangeStep(env.UpStreams[0], env.Osc, steps.GhostOsc, "add column gh_col int default 222", name, ycsbTable),
						steps.NewOnlineSchemaChangeStep(env.UpStreams[1], env.Osc, steps.PtOsc, "add column gh_col int default 222", name, ycsbTable),
					),
					steps.NewExecuteSQL(env.UpStreams[0], name, checkOscStmt),
					steps.NewExecuteSQL(env.UpStreams[1], name, checkOscStmt),
				),
			).
			DmWaitTaskSynced(env.DmHost, taskName, sourceNameList).
			DmCheckSyncDiff(env.Dm, env.SyncDiff, env.DmHost, Path.DiffPaths[0]).
			Build()
	})
}
