github.com/matrixorigin/matrixone@v0.7.0/pkg/logservice/service_wrap.go (about)

     1  // Copyright 2021 - 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 logservice
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	"github.com/lni/dragonboat/v4"
    22  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    23  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    24  	pb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    25  	"github.com/matrixorigin/matrixone/pkg/taskservice"
    26  	"github.com/matrixorigin/matrixone/pkg/util/trace"
    27  )
    28  
    29  type WrappedService struct {
    30  	svc *Service
    31  }
    32  
    33  func NewWrappedService(
    34  	c Config,
    35  	fileService fileservice.FileService,
    36  	opts ...Option,
    37  ) (*WrappedService, error) {
    38  	svc, err := NewService(c, fileService, opts...)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	return &WrappedService{svc: svc}, nil
    43  }
    44  
    45  func (w *WrappedService) Start() error {
    46  	return nil
    47  }
    48  
    49  func (w *WrappedService) Close() error {
    50  	return w.svc.Close()
    51  }
    52  
    53  func (w *WrappedService) ID() string {
    54  	return w.svc.ID()
    55  }
    56  
    57  func (w *WrappedService) IsLeaderHakeeper() (bool, error) {
    58  	isLeader, _, err := w.svc.store.isLeaderHAKeeper()
    59  	return isLeader, err
    60  }
    61  
    62  func (w *WrappedService) GetClusterState() (*pb.CheckerState, error) {
    63  	return w.svc.store.getCheckerState()
    64  }
    65  
    66  func (w *WrappedService) SetInitialClusterInfo(
    67  	logShardNum, dnShardNum, logReplicaNum uint64,
    68  ) error {
    69  	return w.svc.store.setInitialClusterInfo(
    70  		logShardNum, dnShardNum, logReplicaNum,
    71  	)
    72  }
    73  
    74  func (w *WrappedService) CreateInitTasks() error {
    75  	ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
    76  	defer cancel()
    77  	ctx, span := trace.Start(ctx, "CreateInitTasks")
    78  	defer span.End()
    79  
    80  	for i := 0; i < checkBootstrapCycles; i++ {
    81  		select {
    82  		case <-ctx.Done():
    83  			return moerr.NewInternalError(ctx, "failed to create init tasks")
    84  		default:
    85  		}
    86  		if err := w.svc.createInitTasks(ctx); err == nil {
    87  			break
    88  		}
    89  		time.Sleep(time.Second)
    90  	}
    91  	return nil
    92  }
    93  
    94  func (w *WrappedService) GetTaskService() (taskservice.TaskService, bool) {
    95  	w.svc.task.RLock()
    96  	defer w.svc.task.RUnlock()
    97  	if w.svc.task.holder == nil {
    98  		return nil, false
    99  	}
   100  	return w.svc.task.holder.Get()
   101  }
   102  
   103  // StartHAKeeperReplica
   104  // TODO: start hakeeper with specified log store, specified by caller
   105  func (w *WrappedService) StartHAKeeperReplica(
   106  	replicaID uint64, replicas map[uint64]dragonboat.Target, join bool,
   107  ) error {
   108  	return w.svc.store.startHAKeeperReplica(replicaID, replicas, join)
   109  }