Skip to content
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

initial commit for hot shard tool #1299

Merged
merged 27 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 25 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
170 changes: 170 additions & 0 deletions kubectl-fdb/cmd/profile_analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
* profile_analyzer.go
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2022 Apple Inc. and the FoundationDB project authors
*
* 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 cmd

import (
"bytes"
"context"
"fmt"
"log"
"strings"
"text/template"

fdbv1beta2 "github.com/FoundationDB/fdb-kubernetes-operator/api/v1beta2"
"github.com/spf13/cobra"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
yamlutil "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/cli-runtime/pkg/genericclioptions"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type profileConfig struct {
Namespace string
ClusterName string
JobName string
CommandArgs string
}

func newProfileAnalyzerCmd(streams genericclioptions.IOStreams) *cobra.Command {
o := newFDBOptions(streams)

cmd := &cobra.Command{
Use: "analyze-profile",
Short: "Analyze FDB shards to find the busiest team",
Long: "Analyze FDB shards to find the busiest team",
RunE: func(cmd *cobra.Command, args []string) error {
kubeClient, err := getKubeClient(o)
if err != nil {
return err
}

clusterName, err := cmd.Flags().GetString("fdb-cluster")
if err != nil {
return err
}
startTime, err := cmd.Flags().GetString("start-time")
if err != nil {
return err
}
endTime, err := cmd.Flags().GetString("end-time")
if err != nil {
return err
}
topRequests, err := cmd.Flags().GetInt("top-requests")
if err != nil {
return err
}
templateName, err := cmd.Flags().GetString("template-name")
if err != nil {
return err
}

namespace, err := getNamespace(*o.configFlags.Namespace)
if err != nil {
return err
}

return runProfileAnalyzer(kubeClient, namespace, clusterName, startTime, endTime, topRequests, templateName)
},
Example: `
# Run the profiler for cluster-1 in the default namespace for the provided state and end time.
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
kubectl fdb analyze-profile -c cluster-1 --start-time "01:01 20/07/2022 BST" --end-time "01:30 20/07/2022 BST" --top-requests 100 --template-name job.yaml
`,
}
cmd.SetOut(o.Out)
cmd.SetErr(o.ErrOut)
cmd.SetIn(o.In)

cmd.Flags().StringP("fdb-cluster", "c", "", "cluster name for running the analyze profile against.")
cmd.Flags().String("start-time", "", "start time for the analyzing transaction '01:30 30/07/2022 BST'.")
cmd.Flags().String("end-time", "", "end time for analyzing the transaction '02:30 30/07/2022 BST'.")
cmd.Flags().String("template-name", "", "name of the Job template.")
cmd.Flags().Int("top-requests", 100, "")
err := cmd.MarkFlagRequired("fdb-cluster")
if err != nil {
log.Fatal(err)
}
err = cmd.MarkFlagRequired("start-time")
if err != nil {
log.Fatal(err)
}
err = cmd.MarkFlagRequired("end-time")
if err != nil {
log.Fatal(err)
}
err = cmd.MarkFlagRequired("template-name")
if err != nil {
log.Fatal(err)
}

o.configFlags.AddFlags(cmd.Flags())
return cmd
}

func runProfileAnalyzer(kubeClient client.Client, namespace string, clusterName string, startTime string, endTime string, topRequests int, templateName string) error {
config := profileConfig{
Namespace: namespace,
ClusterName: clusterName,
JobName: clusterName + "-hot-shard-tool",
CommandArgs: fmt.Sprintf(" -C /var/dynamic-conf/fdb.cluster -s \"%s\" -e \"%s\" --filter-get-range --top-requests %d", startTime, endTime, topRequests),
}
t, err := template.ParseFiles(templateName)
if err != nil {
return err
}
buf := bytes.Buffer{}
err = t.Execute(&buf, config)
if err != nil {
return err
}
decoder := yamlutil.NewYAMLOrJSONDecoder(&buf, 100000)

job := &batchv1.Job{}
err = decoder.Decode(&job)
if err != nil {
return err
}
cluster, err := loadCluster(kubeClient, namespace, clusterName)
if err != nil {
return err
}

version, err := fdbv1beta2.ParseFdbVersion(cluster.GetRunningVersion())
if err != nil {
return err
}
for _, container := range job.Spec.Template.Spec.InitContainers {
if !strings.Contains(container.Image, version.Compact()) {
continue
}
imageVersion := strings.Split(container.Image, ":")
fdbVersion := strings.Split(imageVersion[1], "-1")
envValue := v1.EnvVar{
Name: "FDB_NETWORK_OPTION_EXTERNAL_CLIENT_DIRECTORY",
Value: fmt.Sprintf("/usr/bin/fdb/%s/lib/", fdbVersion[0]),
}
job.Spec.Template.Spec.Containers[0].Env = append(job.Spec.Template.Spec.Containers[0].Env, envValue)
break
}
log.Printf("creating job %s", config.JobName)
return kubeClient.Create(context.TODO(), job)
}
120 changes: 120 additions & 0 deletions kubectl-fdb/cmd/profile_analyzer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* profile_analyzer_test.go
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2022 Apple Inc. and the FoundationDB project authors
*
* 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 cmd

import (
fdbv1beta2 "github.com/FoundationDB/fdb-kubernetes-operator/api/v1beta2"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"

"sigs.k8s.io/controller-runtime/pkg/client/fake"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

ctx "context"
)

var _ = Describe("profile analyser", func() {
When("running profile analyzer with 6.3", func() {
clusterName := "test"
namespace := "test"
scheme := runtime.NewScheme()
var kubeClient client.Client

BeforeEach(func() {
var cluster *fdbv1beta2.FoundationDBCluster
_ = clientgoscheme.AddToScheme(scheme)
_ = fdbv1beta2.AddToScheme(scheme)
cluster = &fdbv1beta2.FoundationDBCluster{
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
ObjectMeta: metav1.ObjectMeta{
Name: clusterName,
Namespace: namespace,
},
Spec: fdbv1beta2.FoundationDBClusterSpec{
Version: "6.3.24",
},
}
kubeClient = fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(cluster).Build()
})
It("should match the command args", func() {
expectedEnv := v1.EnvVar{
Name: "FDB_NETWORK_OPTION_EXTERNAL_CLIENT_DIRECTORY",
Value: "/usr/bin/fdb/6.3.24/lib/",
ValueFrom: nil,
}
err := runProfileAnalyzer(kubeClient, namespace, clusterName, "21:30 08/24/2022 BST", "22:30 08/24/2022 BST", 100, "../../sample-apps/fdb-profile-analyzer/sample_template.yaml")
Expect(err).NotTo(HaveOccurred())
job := &batchv1.Job{}
err = kubeClient.Get(ctx.Background(), client.ObjectKey{
Namespace: namespace,
Name: "test-hot-shard-tool",
}, job)
Expect(err).NotTo(HaveOccurred())
Expect(job.Spec.Template.Spec.Containers[0].Args).To(Equal([]string{"-c",
"python3 ./transaction_profiling_analyzer.py -C /var/dynamic-conf/fdb.cluster -s \"21:30 08/24/2022 BST\" -e \"22:30 08/24/2022 BST\" --filter-get-range --top-requests 100"}))
Expect(job.Spec.Template.Spec.Containers[0].Env).To(ContainElements(expectedEnv))
})
})
When("running profile analyzer with 7.1", func() {
clusterName := "test"
namespace := "test"
scheme := runtime.NewScheme()
var kubeClient client.Client

BeforeEach(func() {
var cluster *fdbv1beta2.FoundationDBCluster
_ = clientgoscheme.AddToScheme(scheme)
_ = fdbv1beta2.AddToScheme(scheme)
cluster = &fdbv1beta2.FoundationDBCluster{
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
ObjectMeta: metav1.ObjectMeta{
Name: clusterName,
Namespace: namespace,
},
Spec: fdbv1beta2.FoundationDBClusterSpec{
Version: "7.1.19",
},
}
kubeClient = fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(cluster).Build()
})
It("should match the command args", func() {
expectedEnv := v1.EnvVar{
Name: "FDB_NETWORK_OPTION_EXTERNAL_CLIENT_DIRECTORY",
Value: "/usr/bin/fdb/7.1.19/lib/",
ValueFrom: nil,
}
err := runProfileAnalyzer(kubeClient, namespace, clusterName, "21:30 08/24/2022 BST", "22:30 08/24/2022 BST", 100, "../../sample-apps/fdb-profile-analyzer/sample_template.yaml")
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
Expect(err).NotTo(HaveOccurred())
job := &batchv1.Job{}
err = kubeClient.Get(ctx.Background(), client.ObjectKey{
Namespace: namespace,
Name: "test-hot-shard-tool",
}, job)
Expect(err).NotTo(HaveOccurred())
Expect(job.Spec.Template.Spec.Containers[0].Args).To(Equal([]string{"-c",
"python3 ./transaction_profiling_analyzer.py -C /var/dynamic-conf/fdb.cluster -s \"21:30 08/24/2022 BST\" -e \"22:30 08/24/2022 BST\" --filter-get-range --top-requests 100"}))
Expect(job.Spec.Template.Spec.Containers[0].Env).To(ContainElements(expectedEnv))
})
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
})
})
1 change: 1 addition & 0 deletions kubectl-fdb/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func NewRootCmd(streams genericclioptions.IOStreams) *cobra.Command {
newFixCoordinatorIPsCmd(streams),
newGetCmd(streams),
newBuggifyCmd(streams),
newProfileAnalyzerCmd(streams),
)

return cmd
Expand Down
24 changes: 24 additions & 0 deletions sample-apps/fdb-profile-analyzer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM python:3.9-slim
USER root

RUN apt-get update \
&& apt-get install -y wget \
bind9-utils \
lsof \
less \
net-tools \
jq \
openssl \
pkg-config

RUN groupadd -g 4059 foundationdb
RUN useradd -m -d / -s /bin/bash -u 4059 -o foundationdb -g 4059
RUN mkdir /app/
RUN chown foundationdb:foundationdb -R /app/
RUN pip install foundationdb==7.1.21
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
RUN pip install dateparser==1.1.1
RUN wget https://github.com/apple/foundationdb/releases/download/7.1.19/foundationdb-clients_7.1.19-1_amd64.deb && \
dpkg -i foundationdb-clients_7.1.19-1_amd64.deb
WORKDIR /app/
USER foundationdb
RUN wget https://raw.githubusercontent.com/apple/foundationdb/7.1.21/contrib/transaction_profiling_analyzer/transaction_profiling_analyzer.py
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
Loading