...

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