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 10 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
150 changes: 150 additions & 0 deletions kubectl-fdb/cmd/profile_analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
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"
"log"
"strconv"
"text/template"

"github.com/spf13/cobra"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"sigs.k8s.io/controller-runtime/pkg/client"

yamlutil "k8s.io/apimachinery/pkg/util/yaml"
)

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. We require --cluster option explicitly because analyze commands take lot many arguments.
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
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
`,
}
cmd.SetOut(o.Out)
cmd.SetErr(o.ErrOut)
cmd.SetIn(o.In)

cmd.Flags().StringP("fdb-cluster", "c", "", "cluster name for running hot shard tool.")
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
cmd.Flags().String("start-time", "", "start time for the analyzing transaction '01:30 30/07/2022 BST'")
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
cmd.Flags().String("end-time", "", "end time for analyzing the transaction '02:30 30/07/2022 BST'")
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
cmd.Flags().String("template-name", "", "Name of the Job template")
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
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 {
pc := profileConfig{
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
Namespace: namespace,
ClusterName: clusterName,
JobName: clusterName + "-hot-shard-tool",
CommandArgs: " -C " + " /var/dynamic-conf/fdb.cluster" + " -s \"" + startTime + "\"" + " -e \"" + endTime + "\"" + " --filter-get-range " + " --top-requests " + strconv.Itoa(topRequests),
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
}
t, err := template.ParseFiles(templateName)
if err != nil {
return err
}
buf := bytes.Buffer{}
err = t.Execute(&buf, pc)
if err != nil {
return err
}
decoder := yamlutil.NewYAMLOrJSONDecoder(&buf, 100000)

job := &batchv1.Job{}
err = decoder.Decode(&job)
if err != nil {
return err
}
err = kubeClient.Create(context.TODO(), job)
if err != nil {
return err
}
log.Printf("%s Job created.", pc.JobName)
return nil
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 microdnf --enablerepo=ubi* --disablerepo=rhel* install -y wget \
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
bind-utils \
lsof \
nmap-ncat \
less \
vim-minimal \
net-tools \
jq \
openssl \
pkg-config \
procps-ng\
iputils && \
microdnf --enablerepo=ubi* --disablerepo=rhel* clean all \

RUN groupadd -g 4059 foundationdb
RUN useradd -m -d / -s /bin/bash -u 4059 -o foundationdb -g 4059 -G 266
RUN pip install foundationdb
RUN pip install dateparser
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
RUN rpm -ivh https://github.com/apple/foundationdb/releases/download/7.1.19/foundationdb-clients-7.1.19-1.el7.x86_64.rpm
USER foundationdb
RUN curl -O https://raw.githubusercontent.com/apple/foundationdb/main/contrib/transaction_profiling_analyzer/transaction_profiling_analyzer.py
74 changes: 74 additions & 0 deletions sample-apps/fdb-profile-analyzer/sample_template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
apiVersion: batch/v1
kind: Job
metadata:
generation: 1
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
labels:
job-name: {{ .JobName }}
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
app: fdb-profile-analyzer
name: {{ .JobName }}
namespace: {{ .Namespace }}
spec:
backoffLimit: 6
completionMode: NonIndexed
completions: 1
parallelism: 1
suspend: false
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
template:
metadata:
creationTimestamp: null
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
labels:
job-name: {{ .JobName }}
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
app: fdb-profile-analyzer
spec:
containers:
- command:
- /bin/bash
johscheuer marked this conversation as resolved.
Show resolved Hide resolved
args:
- -c
- python3.9 ./transaction_profiling_analyzer.py {{ .CommandArgs }}
env:
- name: FDB_CLUSTER_FILE
value: /var/dynamic-conf/fdb.cluster
- name: FDB_NETWORK_OPTION_TRACE_ENABLE
value: /var/log/fdb-profile-analyzer
- name: FDB_NETWORK_OPTION_TRACE_FORMAT
value: json
image: fdb-profile-analyzer:latest
johscheuer marked this conversation as resolved.
Show resolved Hide resolved
imagePullPolicy: Always
name: profile-analyzer
resources:
requests:
cpu: 100m
memory: 256Mi
limits:
cpu: 100m
memory: 256Mi
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/dynamic-conf
name: config-map
readOnly: true
- mountPath: /var/log/fdb-profile-analyzer
name: fdb-profile-analyzer-logs
dnsPolicy: ClusterFirst
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
restartPolicy: Never
schedulerName: default-scheduler
securityContext:
fsGroup: 4059
runAsGroup: 4059
runAsUser: 4059
terminationGracePeriodSeconds: 30
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
volumes:
- configMap:
defaultMode: 420
items:
- key: cluster-file
path: fdb.cluster
name: {{ .ClusterName }}-config
name: config-map
- emptyDir: {}
name: dynamic-conf
AnnigeriShambu marked this conversation as resolved.
Show resolved Hide resolved
- emptyDir: {}
name: fdb-profile-analyzer-logs
ttlSecondsAfterFinished: 7200