Skip to content

Commit

Permalink
automatically calculate duration (chaos-mesh#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
YangKeao authored Mar 26, 2020
1 parent 03383bd commit c9a7bda
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 61 deletions.
14 changes: 3 additions & 11 deletions api/v1alpha1/timechaos_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ type TimeChaosSpec struct {
// Selector is used to select pods that are used to inject chaos action.
Selector SelectorSpec `json:"selector"`

// TimeOffset defines the delta time of injected program
TimeOffset TimeOffset `json:"timeOffset"`
// TimeOffset defines the delta time of injected program. It's a possibly signed sequence of decimal numbers, such as
// "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
TimeOffset string `json:"timeOffset"`

// ClockIds defines all affected clock id
// All available options are ["CLOCK_REALTIME","CLOCK_MONOTONIC","CLOCK_PROCESS_CPUTIME_ID","CLOCK_THREAD_CPUTIME_ID",
Expand Down Expand Up @@ -102,15 +103,6 @@ func (in *TimeChaosSpec) GetValue() string {
return in.Value
}

// TimeOffset defines the delta time of injected program
// As `clock_gettime` return a struct contains two field: `tv_sec` and `tv_nsec`.
// `Sec` is the offset of seconds, corresponding to `tv_sec` field.
// `NSec` is the offset of nanoseconds, corresponding to `tv_nsec` field.
type TimeOffset struct {
Sec int64 `json:"sec"`
NSec int64 `json:"nsec"`
}

// TimeChaosStatus defines the observed state of TimeChaos
type TimeChaosStatus struct {
ChaosStatus `json:",inline"`
Expand Down
16 changes: 0 additions & 16 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 5 additions & 12 deletions config/crd/bases/pingcap.com_timechaos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,11 @@ spec:
type: object
type: object
timeOffset:
description: TimeOffset defines the delta time of injected program
properties:
nsec:
format: int64
type: integer
sec:
format: int64
type: integer
required:
- nsec
- sec
type: object
description: TimeOffset defines the delta time of injected program.
It's a possibly signed sequence of decimal numbers, such as "300ms",
"-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms",
"s", "m", "h".
type: string
value:
description: Value is required when the mode is set to `FixedPodMode`
/ `FixedPercentPodMod` / `RandomMaxPercentPodMod`. If `FixedPodMode`,
Expand Down
2 changes: 1 addition & 1 deletion controllers/timechaos/timechaos_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var _ = Describe("TimeChaos", func() {
Mode: v1alpha1.AllPodMode,
Value: "0",
Selector: v1alpha1.SelectorSpec{Namespaces: []string{metav1.NamespaceDefault}},
TimeOffset: v1alpha1.TimeOffset{},
TimeOffset: "0s0ns",
Duration: &duration,
Scheduler: nil,
},
Expand Down
21 changes: 18 additions & 3 deletions controllers/timechaos/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"errors"
"fmt"
"os"
"time"

"github.com/go-logr/logr"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -285,13 +286,27 @@ func (r *Reconciler) applyContainer(ctx context.Context, client chaosdaemon.Chao
return err
}

r.Log.Info("setting time shift", "mask", mask, "sec", chaos.Spec.TimeOffset.Sec, "nsec", chaos.Spec.TimeOffset.NSec)
duration, err := time.ParseDuration(chaos.Spec.TimeOffset)
if err != nil {
return err
}

sec, nsec := secAndNSecFromDuration(duration)

r.Log.Info("setting time shift", "mask", mask, "sec", sec, "nsec", nsec)
_, err = client.SetTimeOffset(ctx, &chaosdaemon.TimeRequest{
ContainerId: containerID,
Sec: chaos.Spec.TimeOffset.Sec,
Nsec: chaos.Spec.TimeOffset.NSec,
Sec: sec,
Nsec: nsec,
ClkIdsMask: mask,
})

return err
}

func secAndNSecFromDuration(duration time.Duration) (int64, int64) {
sec := duration.Nanoseconds() / 1e9
nsec := duration.Nanoseconds() - (sec * 1e9)

return sec, nsec
}
32 changes: 32 additions & 0 deletions controllers/timechaos/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package timechaos

import (
"testing"
"time"

. "github.com/onsi/gomega"
)

type SecAndNSecFromDurationTestCase struct {
Duration time.Duration
Sec int64
NSec int64
}

func TestSecAndNSecFromDuration(t *testing.T) {
g := NewGomegaWithT(t)
cases := []SecAndNSecFromDurationTestCase{
{time.Second * 100, 100, 0},
{time.Second * -100, -100, 0},
{time.Second*-100 + time.Microsecond*-20, -100, -20000},
{time.Second*-100 + time.Microsecond*20, -99, -999980000},
{time.Second*100 + time.Microsecond*20, 100, 20000},
{time.Second*100 + time.Microsecond*-20, 99, 999980000},
}

for _, c := range cases {
sec, nsec := secAndNSecFromDuration(c.Duration)
g.Expect(sec).Should(Equal(c.Sec))
g.Expect(nsec).Should(Equal(c.NSec))
}
}
4 changes: 1 addition & 3 deletions doc/time_chaos.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ spec:
selector:
labelSelectors:
"app.kubernetes.io/component": "pd"
timeOffset:
sec: 100000
nsec: 100000
timeOffset: "-10m100ns"
clockIds:
- CLOCK_REALTIME
containerNames:
Expand Down
4 changes: 1 addition & 3 deletions examples/time-chaos-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ spec:
selector:
labelSelectors:
"app.kubernetes.io/component": "tikv"
timeOffset:
sec: 100000
nsec: 100000
timeOffset: "-10m100ns"
duration: "30s"
scheduler:
cron: "@every 1m"
17 changes: 5 additions & 12 deletions manifests/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1212,18 +1212,11 @@ spec:
type: object
type: object
timeOffset:
description: TimeOffset defines the delta time of injected program
properties:
nsec:
format: int64
type: integer
sec:
format: int64
type: integer
required:
- nsec
- sec
type: object
description: TimeOffset defines the delta time of injected program.
It's a possibly signed sequence of decimal numbers, such as "300ms",
"-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms",
"s", "m", "h".
type: string
value:
description: Value is required when the mode is set to `FixedPodMode`
/ `FixedPercentPodMod` / `RandomMaxPercentPodMod`. If `FixedPodMode`,
Expand Down

0 comments on commit c9a7bda

Please sign in to comment.