github.com/matrixorigin/matrixone@v0.7.0/pkg/taskservice/types.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package taskservice
    16  
    17  import (
    18  	"context"
    19  
    20  	logservicepb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    21  	"github.com/matrixorigin/matrixone/pkg/pb/task"
    22  )
    23  
    24  // Condition options for query tasks
    25  type Condition func(*conditions)
    26  
    27  // Op condition op
    28  type Op int
    29  
    30  var (
    31  	// EQ record == condition
    32  	EQ = Op(1)
    33  	// GT record > condition
    34  	GT = Op(2)
    35  	// GE record >= condition
    36  	GE = Op(3)
    37  	// LT record < condition
    38  	LT = Op(4)
    39  	// LE record <= condition
    40  	LE = Op(5)
    41  
    42  	OpName = map[Op]string{
    43  		EQ: "=",
    44  		GT: ">",
    45  		GE: ">=",
    46  		LT: "<",
    47  		LE: "<=",
    48  	}
    49  )
    50  
    51  type conditions struct {
    52  	limit int
    53  
    54  	hasTaskIDCond bool
    55  	taskIDOp      Op
    56  	taskID        uint64
    57  
    58  	hasTaskRunnerCond bool
    59  	taskRunnerOp      Op
    60  	taskRunner        string
    61  
    62  	hasTaskStatusCond bool
    63  	taskStatusOp      Op
    64  	taskStatus        task.TaskStatus
    65  
    66  	hasTaskEpochCond bool
    67  	taskEpochOp      Op
    68  	taskEpoch        uint32
    69  
    70  	hasTaskParentIDCond bool
    71  	taskParentTaskIDOp  Op
    72  	taskParentTaskID    string
    73  
    74  	hasTaskExecutorCond bool
    75  	taskExecutorOp      Op
    76  	taskExecutor        task.TaskCode
    77  
    78  	orderByDesc bool
    79  }
    80  
    81  // WithTaskIDDesc set query with order by task id desc
    82  func WithTaskIDDesc() Condition {
    83  	return func(qo *conditions) {
    84  		qo.orderByDesc = true
    85  	}
    86  }
    87  
    88  // WithTaskExecutorCond set task executor condition
    89  func WithTaskExecutorCond(op Op, value task.TaskCode) Condition {
    90  	return func(qo *conditions) {
    91  		qo.hasTaskExecutorCond = true
    92  		qo.taskExecutorOp = op
    93  		qo.taskExecutor = value
    94  	}
    95  }
    96  
    97  // WithLimitCond set query result limit
    98  func WithLimitCond(limit int) Condition {
    99  	return func(qo *conditions) {
   100  		qo.limit = limit
   101  	}
   102  }
   103  
   104  // WithTaskIDCond set task id condition
   105  func WithTaskIDCond(op Op, value uint64) Condition {
   106  	return func(qo *conditions) {
   107  		qo.hasTaskIDCond = true
   108  		qo.taskIDOp = op
   109  		qo.taskID = value
   110  	}
   111  }
   112  
   113  // WithTaskRunnerCond set task runner condition
   114  func WithTaskRunnerCond(op Op, value string) Condition {
   115  	return func(qo *conditions) {
   116  		qo.hasTaskRunnerCond = true
   117  		qo.taskRunner = value
   118  		qo.taskRunnerOp = op
   119  	}
   120  }
   121  
   122  // WithTaskStatusCond set status condition
   123  func WithTaskStatusCond(op Op, value task.TaskStatus) Condition {
   124  	return func(qo *conditions) {
   125  		qo.hasTaskStatusCond = true
   126  		qo.taskStatus = value
   127  		qo.taskStatusOp = op
   128  	}
   129  }
   130  
   131  // WithTaskEpochCond set task epoch condition
   132  func WithTaskEpochCond(op Op, value uint32) Condition {
   133  	return func(qo *conditions) {
   134  		qo.hasTaskEpochCond = true
   135  		qo.taskEpoch = value
   136  		qo.taskEpochOp = op
   137  	}
   138  }
   139  
   140  // WithTaskParentTaskIDCond set task ParentTaskID condition
   141  func WithTaskParentTaskIDCond(op Op, value string) Condition {
   142  	return func(qo *conditions) {
   143  		qo.hasTaskParentIDCond = true
   144  		qo.taskParentTaskID = value
   145  		qo.taskParentTaskIDOp = op
   146  	}
   147  }
   148  
   149  // TaskService Asynchronous Task Service, which provides scheduling execution and management of
   150  // asynchronous tasks. CN, DN, HAKeeper, LogService will all hold this service.
   151  type TaskService interface {
   152  	// Close close the task service
   153  	Close() error
   154  
   155  	// Create Creates an asynchronous task that executes a single time, this method is idempotent, the
   156  	// same task is not created repeatedly based on multiple calls.
   157  	Create(context.Context, task.TaskMetadata) error
   158  	// CreateBatch is similar to Create, but with a batch task list
   159  	CreateBatch(context.Context, []task.TaskMetadata) error
   160  	// CreateCronTask is similar to Create, but create a task that runs periodically, with the period
   161  	// described using a Cron expression.
   162  	CreateCronTask(ctx context.Context, task task.TaskMetadata, cronExpr string) error
   163  	// Allocate allocate task runner fot spec task.
   164  	Allocate(ctx context.Context, value task.Task, taskRunner string) error
   165  	// Complete task completed. The result used to indicate whether the execution succeeded or failed
   166  	Complete(ctx context.Context, taskRunner string, task task.Task, result task.ExecuteResult) error
   167  	// Heartbeat sending a heartbeat tells the scheduler that the specified task is running normally.
   168  	// If the scheduler does not receive the heartbeat for a long time, it will reassign the task executor
   169  	// to execute the task. Returning `ErrInvalidTask` means that the Task has been reassigned or has
   170  	// ended, and the Task execution needs to be terminated immediately。
   171  	Heartbeat(ctx context.Context, task task.Task) error
   172  	// QueryTask query tasks by conditions
   173  	QueryTask(context.Context, ...Condition) ([]task.Task, error)
   174  	// QueryCronTask returns all cron task metadata
   175  	QueryCronTask(context.Context) ([]task.CronTask, error)
   176  
   177  	// StartScheduleCronTask start schedule cron tasks. A timer will be started to pull the latest CronTask
   178  	// from the TaskStore at regular intervals, and a timer will be maintained in memory for all Cron's to be
   179  	// triggered at regular intervals.
   180  	StartScheduleCronTask()
   181  	// StopScheduleCronTask stop schedule cron tasks.
   182  	StopScheduleCronTask()
   183  
   184  	// GetStorage returns the task storage
   185  	GetStorage() TaskStorage
   186  }
   187  
   188  // TaskExecutor which is responsible for the execution logic of a specific Task, and the function exits to
   189  // represent the completion of the task execution. In the process of task execution task may be interrupted
   190  // at any time, so the implementation needs to frequently check the state of the Context, in the
   191  // Context.Done(), as soon as possible to exit. Epoch is 1 means the task is executed for the first time,
   192  // otherwise it means that the task is rescheduled, the task may be completed or not.
   193  type TaskExecutor func(ctx context.Context, task task.Task) error
   194  
   195  // TaskRunner each runner can execute multiple task concurrently
   196  type TaskRunner interface {
   197  	// ID returns the TaskRunner ID
   198  	ID() string
   199  	// Start start the runner, after runner starts it will start to periodically load the tasks assigned to
   200  	// the current executor, as well as periodically send heartbeats.
   201  	Start() error
   202  	// Stop stop the runner, all running tasks will be terminated
   203  	Stop() error
   204  	// Parallelism maximum number of concurrently executing Tasks
   205  	Parallelism() int
   206  	// RegisterExecutor register the task executor
   207  	RegisterExecutor(code task.TaskCode, executor TaskExecutor)
   208  }
   209  
   210  // TaskStorage task storage
   211  type TaskStorage interface {
   212  	// Close close the task storage
   213  	Close() error
   214  
   215  	// Add add tasks and returns number of successful added
   216  	Add(context.Context, ...task.Task) (int, error)
   217  	// Update update tasks and returns number of successful updated
   218  	Update(context.Context, []task.Task, ...Condition) (int, error)
   219  	// Delete delete tasks and returns number of successful deleted
   220  	Delete(context.Context, ...Condition) (int, error)
   221  	// Query query tasks by conditions
   222  	Query(context.Context, ...Condition) ([]task.Task, error)
   223  
   224  	// AddCronTask add cron task and returns number of successful added
   225  	AddCronTask(context.Context, ...task.CronTask) (int, error)
   226  	// QueryCronTask query all cron tasks
   227  	QueryCronTask(context.Context) ([]task.CronTask, error)
   228  
   229  	// UpdateCronTask crontask generates tasks periodically, and this update
   230  	// needs to be in a transaction. Update cron task and insert a new task.
   231  	// This update must be transactional and needs to be done conditionally
   232  	// using CronTask.TriggerTimes and the task.Metadata.ID field.
   233  	UpdateCronTask(context.Context, task.CronTask, task.Task) (int, error)
   234  }
   235  
   236  // TaskServiceHolder create and hold the task service in the cn, dn and log node. Create
   237  // the TaskService from the heartbeat's CreateTaskService schedule command.
   238  type TaskServiceHolder interface {
   239  	// Close close the holder
   240  	Close() error
   241  	// Get returns the taskservice
   242  	Get() (TaskService, bool)
   243  	// Create create the taskservice
   244  	Create(command logservicepb.CreateTaskService) error
   245  }
   246  
   247  type TaskStorageFactory interface {
   248  	Create(address string) (TaskStorage, error)
   249  }