github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/executor/openapi.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package executor
    15  
    16  import (
    17  	"context"
    18  	"net/http"
    19  	"strings"
    20  	"sync"
    21  
    22  	"github.com/gin-gonic/gin"
    23  	engineModel "github.com/pingcap/tiflow/engine/model"
    24  	"github.com/pingcap/tiflow/engine/pkg/openapi"
    25  	"github.com/pingcap/tiflow/pkg/errors"
    26  )
    27  
    28  func jobAPIBasePath(jobID engineModel.JobID) string {
    29  	return openapi.JobAPIPrefix + jobID + "/"
    30  }
    31  
    32  type jobAPIServer struct {
    33  	rwm      sync.RWMutex
    34  	handlers map[engineModel.JobID]http.Handler
    35  }
    36  
    37  func newJobAPIServer() *jobAPIServer {
    38  	return &jobAPIServer{
    39  		handlers: make(map[engineModel.JobID]http.Handler),
    40  	}
    41  }
    42  
    43  func (s *jobAPIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    44  	h, err := s.matchHandler(r.URL.Path)
    45  	if err != nil {
    46  		openapi.WriteHTTPError(w, err)
    47  		return
    48  	}
    49  	h.ServeHTTP(w, r)
    50  }
    51  
    52  func (s *jobAPIServer) matchHandler(path string) (http.Handler, error) {
    53  	if !strings.HasPrefix(path, openapi.JobAPIPrefix) {
    54  		return nil, errors.ErrInvalidArgument.GenWithStack("invalid job api path: %s", path)
    55  	}
    56  	path = strings.TrimPrefix(path, openapi.JobAPIPrefix)
    57  	parts := strings.SplitN(path, "/", 2)
    58  	if len(parts) != 2 {
    59  		return nil, errors.ErrInvalidArgument.GenWithStack("invalid job api path: %s", path)
    60  	}
    61  	JobID := parts[0]
    62  	s.rwm.RLock()
    63  	h, ok := s.handlers[JobID]
    64  	s.rwm.RUnlock()
    65  	if !ok {
    66  		// We can't tell whether the job exists or not, but at least the job is not running.
    67  		return nil, errors.ErrJobNotRunning.GenWithStackByArgs(JobID)
    68  	}
    69  	return h, nil
    70  }
    71  
    72  func (s *jobAPIServer) initialize(jobID engineModel.JobID, f func(apiGroup *gin.RouterGroup)) {
    73  	engine := gin.New()
    74  	apiGroup := engine.Group(jobAPIBasePath(jobID))
    75  	f(apiGroup)
    76  
    77  	s.rwm.Lock()
    78  	s.handlers[jobID] = engine
    79  	s.rwm.Unlock()
    80  }
    81  
    82  func (s *jobAPIServer) listenStoppedJobs(ctx context.Context, stoppedJobs <-chan engineModel.JobID) error {
    83  	for {
    84  		select {
    85  		case jobID, ok := <-stoppedJobs:
    86  			if !ok {
    87  				return nil
    88  			}
    89  			s.rwm.Lock()
    90  			delete(s.handlers, jobID)
    91  			s.rwm.Unlock()
    92  		case <-ctx.Done():
    93  			return ctx.Err()
    94  		}
    95  	}
    96  }