github.com/matrixorigin/matrixone@v1.2.0/pkg/frontend/task.go (about)

     1  // Copyright 2021 - 2023 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 frontend
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    21  	"github.com/matrixorigin/matrixone/pkg/pb/task"
    22  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    23  	"github.com/matrixorigin/matrixone/pkg/taskservice"
    24  )
    25  
    26  func handlePauseDaemonTask(ctx context.Context, ses *Session, st *tree.PauseDaemonTask) error {
    27  	ts := getGlobalPu().TaskService
    28  	if ts == nil {
    29  		return moerr.NewInternalError(ctx,
    30  			"task service not ready yet, please try again later.")
    31  	}
    32  	tasks, err := ts.QueryDaemonTask(ctx,
    33  		taskservice.WithTaskIDCond(taskservice.EQ, st.TaskID),
    34  		taskservice.WithAccountID(taskservice.EQ, ses.accountId),
    35  	)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	if len(tasks) != 1 {
    40  		return moerr.NewErrTaskNotFound(ctx, st.TaskID)
    41  	}
    42  	// Already in paused status.
    43  	if tasks[0].TaskStatus == task.TaskStatus_Paused || tasks[0].TaskStatus == task.TaskStatus_PauseRequested {
    44  		return nil
    45  	} else if tasks[0].TaskStatus != task.TaskStatus_Running {
    46  		return moerr.NewInternalError(ctx,
    47  			"task can not be paused only if it is in %s statue, now it is %s",
    48  			task.TaskStatus_Running,
    49  			tasks[0].TaskStatus)
    50  	}
    51  	tasks[0].TaskStatus = task.TaskStatus_PauseRequested
    52  	c, err := ts.UpdateDaemonTask(ctx, tasks)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	if c != 1 {
    57  		return moerr.NewErrTaskNotFound(ctx, st.TaskID)
    58  	}
    59  	return nil
    60  }
    61  
    62  func handleCancelDaemonTask(ctx context.Context, ses *Session, taskID uint64) error {
    63  	ts := getGlobalPu().TaskService
    64  	if ts == nil {
    65  		return moerr.NewInternalError(ctx,
    66  			"task service not ready yet, please try again later.")
    67  	}
    68  	tasks, err := ts.QueryDaemonTask(ctx,
    69  		taskservice.WithTaskIDCond(taskservice.EQ, taskID),
    70  		taskservice.WithAccountID(taskservice.EQ, ses.accountId),
    71  	)
    72  	if err != nil {
    73  		return err
    74  	}
    75  	if len(tasks) != 1 {
    76  		return moerr.NewErrTaskNotFound(ctx, taskID)
    77  	}
    78  	// Already in canceled status.
    79  	if tasks[0].TaskStatus == task.TaskStatus_Canceled || tasks[0].TaskStatus == task.TaskStatus_CancelRequested {
    80  		return nil
    81  	} else if tasks[0].TaskStatus == task.TaskStatus_Error {
    82  		return moerr.NewInternalError(ctx,
    83  			"task can not be canceled because it is in %s state", task.TaskStatus_Error)
    84  	}
    85  	tasks[0].TaskStatus = task.TaskStatus_CancelRequested
    86  	c, err := ts.UpdateDaemonTask(ctx, tasks)
    87  	if err != nil {
    88  		return err
    89  	}
    90  	if c != 1 {
    91  		return moerr.NewErrTaskNotFound(ctx, taskID)
    92  	}
    93  	return nil
    94  }
    95  
    96  func handleResumeDaemonTask(ctx context.Context, ses *Session, st *tree.ResumeDaemonTask) error {
    97  	ts := getGlobalPu().TaskService
    98  	if ts == nil {
    99  		return moerr.NewInternalError(ctx,
   100  			"task service not ready yet, please try again later.")
   101  	}
   102  	tasks, err := ts.QueryDaemonTask(ctx,
   103  		taskservice.WithTaskIDCond(taskservice.EQ, st.TaskID),
   104  		taskservice.WithAccountID(taskservice.EQ, ses.accountId),
   105  	)
   106  	if err != nil {
   107  		return err
   108  	}
   109  	if len(tasks) != 1 {
   110  		return moerr.NewErrTaskNotFound(ctx, st.TaskID)
   111  	}
   112  	// Already in canceled status.
   113  	if tasks[0].TaskStatus == task.TaskStatus_Running || tasks[0].TaskStatus == task.TaskStatus_ResumeRequested {
   114  		return nil
   115  	} else if tasks[0].TaskStatus != task.TaskStatus_Paused {
   116  		return moerr.NewInternalError(ctx,
   117  			"task can be resumed only if it is in %s state, now it is %s",
   118  			task.TaskStatus_Paused,
   119  			tasks[0].TaskStatus)
   120  	}
   121  	tasks[0].TaskStatus = task.TaskStatus_ResumeRequested
   122  	c, err := ts.UpdateDaemonTask(ctx, tasks)
   123  	if err != nil {
   124  		return err
   125  	}
   126  	if c != 1 {
   127  		return moerr.NewErrTaskNotFound(ctx, st.TaskID)
   128  	}
   129  	return nil
   130  }