Skip to content

feat: add time travel experiment #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/chaosblade-io/chaosblade-exec-os/exec/process"
"github.com/chaosblade-io/chaosblade-exec-os/exec/script"
"github.com/chaosblade-io/chaosblade-exec-os/exec/systemd"
"github.com/chaosblade-io/chaosblade-exec-os/exec/time"
"log"
"os"

Expand Down Expand Up @@ -57,6 +58,7 @@ func getModels() *spec.Models {
file.NewFileCommandSpec(),
kernel.NewKernelInjectCommandSpec(),
systemd.NewSystemdCommandModelSpec(),
time.NewTimeCommandSpec(),
}
specModels := make([]*spec.Models, 0)
for _, modeSpec := range modelCommandSpecs {
Expand Down
1 change: 1 addition & 0 deletions exec/category/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ const (
SystemFile = "system_file"
SystemKernel = "system_kernel"
SystemSystemd = "system_systemd"
SystemTime = "system_time"
)
2 changes: 2 additions & 0 deletions exec/model/model_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/chaosblade-io/chaosblade-exec-os/exec/process"
"github.com/chaosblade-io/chaosblade-exec-os/exec/script"
"github.com/chaosblade-io/chaosblade-exec-os/exec/systemd"
"github.com/chaosblade-io/chaosblade-exec-os/exec/time"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
)

Expand All @@ -42,5 +43,6 @@ func GetAllExpModels() []spec.ExpModelCommandSpec {
file.NewFileCommandSpec(),
kernel.NewKernelInjectCommandSpec(),
systemd.NewSystemdCommandModelSpec(),
time.NewTimeCommandSpec(),
}
}
48 changes: 48 additions & 0 deletions exec/time/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package time

import (
"github.com/chaosblade-io/chaosblade-spec-go/spec"
)

type TimeCommandSpec struct {
spec.BaseExpModelCommandSpec
}

func NewTimeCommandSpec() spec.ExpModelCommandSpec {
return &TimeCommandSpec{
spec.BaseExpModelCommandSpec{
ExpFlags: []spec.ExpFlagSpec{},
ExpActions: []spec.ExpActionCommandSpec{
NewTravelTimeActionCommandSpec(),
},
},
}
}

func (*TimeCommandSpec) Name() string {
return "time"
}

func (*TimeCommandSpec) ShortDesc() string {
return "Time experiment"
}

func (*TimeCommandSpec) LongDesc() string {
return "Time experiment"
}
142 changes: 142 additions & 0 deletions exec/time/time_travel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package time

import (
"context"
"fmt"
"time"

"github.com/chaosblade-io/chaosblade-exec-os/exec/category"
"github.com/chaosblade-io/chaosblade-spec-go/log"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
)

const TravelTimeBin = "chaos_timetravel"

type TravelTimeActionCommandSpec struct {
spec.BaseExpActionCommandSpec
}

func NewTravelTimeActionCommandSpec() spec.ExpActionCommandSpec {
return &TravelTimeActionCommandSpec{
spec.BaseExpActionCommandSpec{
ActionMatchers: []spec.ExpFlagSpec{},
ActionFlags: []spec.ExpFlagSpec{
&spec.ExpFlag{
Name: "offset",
Desc: "Travel time offset, for example: -1d2h3m50s",
},
&spec.ExpFlag{
Name: "disableNtp",
Desc: "Whether to disable Network Time Protocol to synchronize time",
},
},
ActionExecutor: &TravelTimeExecutor{},
ActionExample: `
# Time travel 5 minutes and 30 seconds into the future
blade create time travel --offset 5m30s
`,
ActionPrograms: []string{TravelTimeBin},
ActionCategories: []string{category.SystemTime},
},
}
}

func (*TravelTimeActionCommandSpec) Name() string {
return "travel"
}

func (*TravelTimeActionCommandSpec) Aliases() []string {
return []string{"k"}
}

func (*TravelTimeActionCommandSpec) ShortDesc() string {
return "Time Travel"
}

func (k *TravelTimeActionCommandSpec) LongDesc() string {
if k.ActionLongDesc != "" {
return k.ActionLongDesc
}
return "Modify system time to fake processes"
}

func (*TravelTimeActionCommandSpec) Categories() []string {
return []string{category.SystemProcess}
}

type TravelTimeExecutor struct {
channel spec.Channel
}

func (tte *TravelTimeExecutor) Name() string {
return "travel"
}

func (tte *TravelTimeExecutor) Exec(uid string, ctx context.Context, model *spec.ExpModel) *spec.Response {
commands := []string{"date", "timedatectl"}
if response, ok := tte.channel.IsAllCommandsAvailable(ctx, commands); !ok {
return response
}

var disableNtp bool
timeOffsetStr := model.ActionFlags["offset"]
disableNtpStr := model.ActionFlags["disableNtp"]

if timeOffsetStr == "" {
log.Errorf(ctx, "offset is nil")
return spec.ResponseFailWithFlags(spec.ParameterLess, "offset")
}
disableNtp = disableNtpStr == "true" || disableNtpStr == ""

if _, ok := spec.IsDestroy(ctx); ok {
return tte.stop(ctx)
}

return tte.start(ctx, timeOffsetStr, disableNtp)
}

func (tte *TravelTimeExecutor) SetChannel(channel spec.Channel) {
tte.channel = channel
}

func (tte *TravelTimeExecutor) stop(ctx context.Context) *spec.Response {
response := tte.channel.Run(ctx, "timedatectl", fmt.Sprintf(`set-ntp true`))
if !response.Success {
return response
}
return tte.channel.Run(ctx, "hwclock", fmt.Sprintf(`--hctosys`))
}

func (tte *TravelTimeExecutor) start(ctx context.Context, timeOffsetStr string, disableNtp bool) *spec.Response {
duration, err := time.ParseDuration(timeOffsetStr)
if err != nil {
log.Errorf(ctx, "offset is invalid")
return spec.ResponseFailWithFlags(spec.ParameterInvalid, "offset", timeOffsetStr, err)
}
targetTime := time.Now().Add(duration).Format("01/02/2006 15:04:05")

if disableNtp {
response := tte.channel.Run(ctx, "timedatectl", fmt.Sprintf(`set-ntp false`))
if !response.Success {
return response
}
}

return tte.channel.Run(ctx, "date", fmt.Sprintf(`-s "%s" `, targetTime))
}