// Copyright 2019 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.
func init() {
	var (
		taskName    = "test"
		sourceName0 = "source-0"
		sourceName1 = "source-1"
		name        = "dm_sharding_optimistic_basic_sharding"
		env         = newCommonEnv()
	)
	var _ = AddTestCase(spec.CaseName(name), func() steps.Step {
		env.init().b.
			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=" + name, "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=" + name, "tablecount=1"},
				Threads:        20,
				Duration:       3 * time.Minute,
			}).
			DmCreateSources(env.Dm, env.DmHost, Path.SourcePaths).
			DmStartTasks(env.Dm, env.DmHost, Path.TaskPaths).
			DmWaitTaskSynced(env.DmHost, taskName, []string{sourceName0, sourceName1}).
			DmCheckSyncDiff(env.Dm, env.SyncDiff, env.DmHost, Path.DiffPaths[0]).
			DmRunWorkload(env.UpStreams[0], env.Dm, env.Workload, &workload.QihooOption{
				Command:        workload.CommandTypeRun,
				PropertyFile:   "/workloads/workloadd",
				PropertyValues: []string{"operationcount=500000", "autocommit=1", "databasename=" + name, "tablecount=1"},
				Threads:        20,
				Duration:       5 * time.Minute,
			}).
			DmRunWorkload(env.UpStreams[1], env.Dm, env.Workload, &workload.QihooOption{
				Command:        workload.CommandTypeRun,
				PropertyFile:   "/workloads/workloadp",
				PropertyValues: []string{"operationcount=500000", "autocommit=1", "databasename=" + name, "tablecount=1"},
				Threads:        20,
				Duration:       5 * time.Minute,
			}).
			DmWaitTaskSynced(env.DmHost, taskName, []string{sourceName0, sourceName1}).
			DmCheckSyncDiff(env.Dm, env.SyncDiff, env.DmHost, Path.DiffPaths[0])
		return env.toBuilder().Build()

	})
}
