...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/time_server.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon

     1  // Copyright 2021 Chaos Mesh Authors.
     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  
    16  package chaosdaemon
    17  
    18  import (
    19  	"context"
    20  
    21  	"github.com/chaos-mesh/chaos-mesh/pkg/time"
    22  
    23  	"github.com/golang/protobuf/ptypes/empty"
    24  
    25  	pb "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
    26  )
    27  
    28  func (s *DaemonServer) SetTimeOffset(ctx context.Context, req *pb.TimeRequest) (*empty.Empty, error) {
    29  	log.Info("Shift time", "Request", req)
    30  
    31  	pid, err := s.crClient.GetPidFromContainerID(ctx, req.ContainerId)
    32  	if err != nil {
    33  		log.Error(err, "error while getting PID")
    34  		return nil, err
    35  	}
    36  
    37  	childPids, err := GetChildProcesses(pid)
    38  	if err != nil {
    39  		log.Error(err, "fail to get child processes")
    40  	}
    41  	allPids := append(childPids, pid)
    42  	log.Info("all related processes found", "pids", allPids)
    43  
    44  	for _, pid := range allPids {
    45  		err = time.ModifyTime(int(pid), req.Sec, req.Nsec, req.ClkIdsMask)
    46  		if err != nil {
    47  			log.Error(err, "error while modifying time", "pid", pid)
    48  			return nil, err
    49  		}
    50  	}
    51  
    52  	return &empty.Empty{}, nil
    53  }
    54  
    55  func (s *DaemonServer) RecoverTimeOffset(ctx context.Context, req *pb.TimeRequest) (*empty.Empty, error) {
    56  	log.Info("Recover time", "Request", req)
    57  
    58  	pid, err := s.crClient.GetPidFromContainerID(ctx, req.ContainerId)
    59  	if err != nil {
    60  		log.Error(err, "error while getting PID")
    61  		return nil, err
    62  	}
    63  
    64  	childPids, err := GetChildProcesses(pid)
    65  	if err != nil {
    66  		log.Error(err, "fail to get child processes")
    67  	}
    68  	allPids := append(childPids, pid)
    69  	log.Info("get all related process pids", "pids", allPids)
    70  
    71  	for _, pid := range allPids {
    72  		// FIXME: if the process has halted and no process with this pid exists, we will get an error.
    73  		err = time.ModifyTime(int(pid), int64(0), int64(0), 0)
    74  		if err != nil {
    75  			log.Error(err, "error while recovering", "pid", pid)
    76  			return nil, err
    77  		}
    78  	}
    79  
    80  	return &empty.Empty{}, nil
    81  }
    82