package tikvrel

import (
	"context"
	"io"
	"time"

	_ "github.com/go-sql-driver/mysql"
	. "github.com/pingcap/endless/pkg/matcher"
	"github.com/pingcap/endless/testcase/oltp/oltppkg"

	"github.com/pingcap/test-infra/sdk/resource"
	_ "github.com/pingcap/test-infra/sdk/resource/impl/k8s"
)

func OLTP_TiKV_REL_NETWORK_PARTITION_001(
	ctx context.Context,
	tc resource.TiDBCluster,
	pr *io.PipeReader,
	caseCmdParms oltppkg.CaseCmdParms,
) {
	var RTO time.Duration
	faultDuration := oltppkg.GetRandDuration(time.Minute, 10) + 10*time.Minute
	tidbReplicaRead := Try(oltppkg.GetTidbVariable(ctx, tc, "tidb_replica_read")).(string)
	// know issue INCALL-113 (https://github.com/tikv/client-go/issues/521), avoid this check due to issue do not fixed
	if tidbReplicaRead == "follower" {
		RTO = faultDuration + 2*time.Minute
	} else {
		RTO = 2 * time.Minute
	}
	tikvRelOptions := &TiKVRelOptions{
		WarmUpTime:    10 * time.Minute,
		FaultType:     "network-partition-between-2-tikv",
		FaultNum:      1,
		FaultDuration: faultDuration,
		RTO:           RTO,
		RecoveryTime:  20 * time.Minute,
	}
	tikvRelOptions.NetworkPartitionTest(ctx, tc, pr, caseCmdParms)
}

func OLTP_TiKV_REL_NETWORK_PARTITION_002(
	ctx context.Context,
	tc resource.TiDBCluster,
	pr *io.PipeReader,
	caseCmdParms oltppkg.CaseCmdParms,
) {
	tikvRelOptions := &TiKVRelOptions{
		WarmUpTime:    10 * time.Minute,
		FaultType:     "network-partition-between-1IDC-and-others",
		FaultNum:      1,
		FaultDuration: 10 * time.Minute,
		RTO:           2 * time.Minute,
		RecoveryTime:  10 * time.Minute,
	}
	tikvRelOptions.NetworkPartitionTest(ctx, tc, pr, caseCmdParms)
}

func OLTP_TiKV_REL_NETWORK_PARTITION_003(
	ctx context.Context,
	tc resource.TiDBCluster,
	pr *io.PipeReader,
	caseCmdParms oltppkg.CaseCmdParms,
) {
	tikvRelOptions := &TiKVRelOptions{
		WarmUpTime:    10 * time.Minute,
		FaultType:     "network-partition-between-2IDC",
		FaultNum:      1,
		FaultDuration: 10 * time.Minute,
		RTO:           2 * time.Minute,
		RecoveryTime:  10 * time.Minute,
	}
	tikvRelOptions.NetworkPartitionTest(ctx, tc, pr, caseCmdParms)
}
