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

[Backend]Cache - Cache logic with db interaction #3266

Merged
merged 69 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from 64 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
6994984
Initial execution cache
rui5i Feb 10, 2020
19a61e7
Add initial server logic
rui5i Feb 10, 2020
0448a12
Add const
rui5i Feb 10, 2020
d2fad48
Change folder name
rui5i Feb 10, 2020
c561ca4
Change execution key name
rui5i Feb 11, 2020
55ee8a7
Fix unit test
rui5i Feb 11, 2020
0f83144
Add Dockerfile and OWNERS file
rui5i Feb 14, 2020
1ecc855
fix go.sum
rui5i Feb 15, 2020
1b5b681
Add local deployment scripts
rui5i Feb 18, 2020
83126c7
Merge branch 'execution_cache' of https://github.com/rui5i/pipelines …
rui5i Feb 18, 2020
ae472d7
refactor src code
rui5i Feb 19, 2020
8c0cac8
Add standalone deployment scripts and yamls
rui5i Feb 20, 2020
cb075b2
Minor fix
rui5i Feb 20, 2020
4b0831a
Add execution cache image build in test folder
rui5i Feb 20, 2020
e1bedf7
Merge branch 'master' into execution_cache
rui5i Feb 20, 2020
ed1aa0e
fix test cloudbuild
rui5i Feb 20, 2020
3b1323c
Fix cloudbuild
rui5i Feb 20, 2020
da3eb42
Add execution cache deployer image to test folder
rui5i Feb 20, 2020
37521ec
Add copyright
rui5i Feb 20, 2020
5efe589
Fix deployer build
rui5i Feb 20, 2020
fa12c40
Add license for execution cache and cloudbuild for execution cache
rui5i Feb 21, 2020
3c302af
Refactor license intermediate data
rui5i Feb 21, 2020
17f714e
Fix execution cache image manifest
rui5i Feb 21, 2020
79e4711
Typo fix for cache and cache deployer images
rui5i Feb 21, 2020
a16335c
Add arguments in ca generation scripts and change deployer base image…
rui5i Feb 22, 2020
bb796c1
minor fix
rui5i Feb 22, 2020
db797b3
fix arg
rui5i Feb 22, 2020
57523c0
Mirror source code with MPL in execution_cache image
rui5i Feb 24, 2020
893e8b1
Minor fix
rui5i Feb 24, 2020
851ce25
minor refactor on error handling
rui5i Feb 24, 2020
1720ee6
Refactor cache source code, Docker image and manifest
rui5i Feb 25, 2020
f482c9f
Fix variable names
rui5i Feb 26, 2020
6523570
Add images in .release.cloudbuild.yaml
rui5i Feb 26, 2020
5c5298e
resolve merge conflict
rui5i Feb 27, 2020
914d9fd
Change execution_cache to generic name
rui5i Feb 27, 2020
4e7e3a9
revice readme
rui5i Feb 27, 2020
ad560f4
Move deployer job out of upgrade script
rui5i Feb 28, 2020
ba26e48
fix tests
rui5i Feb 28, 2020
065af7a
fix tests
rui5i Feb 28, 2020
a8aa15c
Seperate cache service and cache deployer job
rui5i Feb 28, 2020
e7aefad
mysql set up
rui5i Feb 28, 2020
d3e2755
wip
rui5i Mar 2, 2020
e986ca1
WIP
rui5i Mar 2, 2020
2df1c48
WIP
rui5i Mar 3, 2020
16b3e6d
work mysql connection
rui5i Mar 5, 2020
243307a
initial cache logic
rui5i Mar 5, 2020
da75b68
watcher
rui5i Mar 6, 2020
d79e909
WIP pod watching with mysql
rui5i Mar 9, 2020
980f089
worked crud
rui5i Mar 12, 2020
c0cf37e
Add sql unit test
rui5i Mar 13, 2020
616e968
Merge upstream master
rui5i Mar 13, 2020
25f0bcd
fix manifest
rui5i Mar 13, 2020
ca41d77
Add copyright
rui5i Mar 13, 2020
2b39036
Add watcher check and update cache key generation logic
rui5i Mar 13, 2020
28c77c0
test replace container images
rui5i Mar 16, 2020
8f987d9
work cache service
rui5i Mar 16, 2020
7e19564
Add configmap for cache service
rui5i Mar 16, 2020
f82b781
refactor
rui5i Mar 17, 2020
db9bc4e
fix manifest
rui5i Mar 18, 2020
4c49458
Add unit tests
rui5i Mar 18, 2020
c4ad659
Remove delete table
rui5i Mar 18, 2020
26c790d
Fix sql dialect
rui5i Mar 18, 2020
f702714
Add cached step log
rui5i Mar 18, 2020
14d05c0
Add metadata execution id
rui5i Mar 18, 2020
db1073b
minor fix
rui5i Mar 19, 2020
154d26e
revert go.mod and go.sum
rui5i Mar 19, 2020
901a7d2
revert go.sum and go.mod
rui5i Mar 19, 2020
c2156e6
revert go.sum and go.mod
rui5i Mar 19, 2020
075d255
revert go.mod and go.sum
rui5i Mar 19, 2020
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
58 changes: 58 additions & 0 deletions backend/src/cache/client/kubernetes_core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package client

import (
"time"

"github.com/cenkalti/backoff"
"github.com/golang/glog"
"github.com/pkg/errors"
"k8s.io/client-go/kubernetes"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
)

type KubernetesCoreInterface interface {
PodClient(namespace string) v1.PodInterface
}

type KubernetesCore struct {
coreV1Client v1.CoreV1Interface
}

func (c *KubernetesCore) PodClient(namespace string) v1.PodInterface {
return c.coreV1Client.Pods(namespace)
}

func createKubernetesCore() (KubernetesCoreInterface, error) {
restConfig, err := rest.InClusterConfig()
if err != nil {
return nil, errors.Wrap(err, "Failed to initialize kubernetes client.")
}

clientSet, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return nil, errors.Wrap(err, "Failed to initialize kubernetes client set.")
}
return &KubernetesCore{clientSet.CoreV1()}, nil
}

// CreateKubernetesCoreOrFatal creates a new client for the Kubernetes pod.
func CreateKubernetesCoreOrFatal(initConnectionTimeout time.Duration) KubernetesCoreInterface {
var client KubernetesCoreInterface
var err error
var operation = func() error {
client, err = createKubernetesCore()
if err != nil {
return err
}
return nil
}
b := backoff.NewExponentialBackOff()
b.MaxElapsedTime = initConnectionTimeout
err = backoff.Retry(operation, b)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the cases where client creation can fail, but then succeed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can think of cases where we deploy KFP from scratch and the mysql instance has not been ready yet. With retry here it can keep retrying until mysql instance gets ready.


if err != nil {
glog.Fatalf("Failed to create pod client. Error: %v", err)
}
return client
}
47 changes: 47 additions & 0 deletions backend/src/cache/client/kubernetes_core_fake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2020 Google LLC
//
// 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
//
// https://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 client

import (
"github.com/kubeflow/pipelines/backend/src/common/util"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
)

type FakeKuberneteCoreClient struct {
podClientFake *FakePodClient
}

func (c *FakeKuberneteCoreClient) PodClient(namespace string) v1.PodInterface {
if len(namespace) == 0 {
panic(util.NewResourceNotFoundError("Namespace", namespace))
}
return c.podClientFake
}

func NewFakeKuberneteCoresClient() *FakeKuberneteCoreClient {
return &FakeKuberneteCoreClient{&FakePodClient{}}
}

type FakeKubernetesCoreClientWithBadPodClient struct {
podClientFake *FakeBadPodClient
}

func NewFakeKubernetesCoreClientWithBadPodClient() *FakeKubernetesCoreClientWithBadPodClient {
return &FakeKubernetesCoreClientWithBadPodClient{&FakeBadPodClient{}}
}

func (c *FakeKubernetesCoreClientWithBadPodClient) PodClient(namespace string) v1.PodInterface {
return c.podClientFake
}
105 changes: 105 additions & 0 deletions backend/src/cache/client/pod_fake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2020 Google LLC
//
// 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
//
// https://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 client

import (
"errors"

"github.com/golang/glog"
corev1 "k8s.io/api/core/v1"
"k8s.io/api/policy/v1beta1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/rest"
)

type FakePodClient struct {
watchIsCalled bool
patchIsCalled bool
}

func (FakePodClient) Create(*corev1.Pod) (*corev1.Pod, error) {
glog.Error("This fake method is not yet implemented.")
return nil, nil
}

func (FakePodClient) Update(*corev1.Pod) (*corev1.Pod, error) {
glog.Error("This fake method is not yet implemented.")
return nil, nil
}

func (FakePodClient) UpdateStatus(*corev1.Pod) (*corev1.Pod, error) {
glog.Error("This fake method is not yet implemented.")
return nil, nil
}

func (FakePodClient) Delete(name string, options *v1.DeleteOptions) error {
return nil
}

func (FakePodClient) DeleteCollection(options *v1.DeleteOptions, listOptions v1.ListOptions) error {
glog.Error("This fake method is not yet implemented.")
return nil
}

func (FakePodClient) Get(name string, options v1.GetOptions) (*corev1.Pod, error) {
glog.Error("This fake method is not yet implemented.")
return nil, nil
}

func (FakePodClient) List(opts v1.ListOptions) (*corev1.PodList, error) {
glog.Error("This fake method is not yet implemented.")
return nil, nil
}

func (f FakePodClient) Watch(opts v1.ListOptions) (watch.Interface, error) {
f.watchIsCalled = true
event := watch.Event{
Type: watch.Added,
Object: &corev1.Pod{},
}
ch := make(chan watch.Event, 1)
ch <- event
return nil, nil
}

func (f FakePodClient) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *corev1.Pod, err error) {
f.patchIsCalled = true
return nil, nil
}

func (FakePodClient) Bind(binding *corev1.Binding) error {
glog.Error("This fake method is not yet implemented.")
return nil
}

func (FakePodClient) Evict(eviction *v1beta1.Eviction) error {
glog.Error("This fake method is not yet implemented.")
return nil
}

func (FakePodClient) GetLogs(name string, opts *corev1.PodLogOptions) *rest.Request {
glog.Error("This fake method is not yet implemented.")
return nil
}

type FakeBadPodClient struct {
FakePodClient
}

func (FakeBadPodClient) Delete(name string, options *v1.DeleteOptions) error {
return errors.New("failed to delete pod")
}
46 changes: 46 additions & 0 deletions backend/src/cache/client/sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2020 Google LLC
//
// 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
//
// https://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 client

import (
"fmt"

"github.com/go-sql-driver/mysql"
)

func CreateMySQLConfig(user, password string, mysqlServiceHost string,
mysqlServicePort string, dbName string, mysqlGroupConcatMaxLen string, mysqlExtraParams map[string]string) *mysql.Config {

params := map[string]string{
"charset": "utf8",
"parseTime": "True",
"loc": "Local",
"group_concat_max_len": mysqlGroupConcatMaxLen,
}

for k, v := range mysqlExtraParams {
params[k] = v
}

return &mysql.Config{
User: user,
Passwd: password,
Net: "tcp",
Addr: fmt.Sprintf("%s:%s", mysqlServiceHost, mysqlServicePort),
Params: params,
DBName: dbName,
AllowNativePasswords: true,
}
}
81 changes: 81 additions & 0 deletions backend/src/cache/client/sql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2020 Google LLC
//
// 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
//
// https://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 client

import (
"reflect"
"testing"

"github.com/go-sql-driver/mysql"
)

func TestCreateMySQLConfig(t *testing.T) {
type args struct {
user string
password string
host string
port string
dbName string
mysqlGroupConcatMaxLen string
mysqlExtraParams map[string]string
}
tests := []struct {
name string
args args
want *mysql.Config
}{
{
name: "default config",
args: args{
user: "root",
host: "mysql",
port: "3306",
mysqlGroupConcatMaxLen: "1024",
mysqlExtraParams: nil,
},
want: &mysql.Config{
User: "root",
Net: "tcp",
Addr: "mysql:3306",
Params: map[string]string{"charset": "utf8", "parseTime": "True", "loc": "Local", "group_concat_max_len": "1024"},
AllowNativePasswords: true,
},
},
{
name: "extra parameters",
args: args{
user: "root",
host: "mysql",
port: "3306",
mysqlGroupConcatMaxLen: "1024",
mysqlExtraParams: map[string]string{"tls": "true"},
},
want: &mysql.Config{
User: "root",
Net: "tcp",
Addr: "mysql:3306",
Params: map[string]string{"charset": "utf8", "parseTime": "True", "loc": "Local", "group_concat_max_len": "1024", "tls": "true"},
AllowNativePasswords: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CreateMySQLConfig(tt.args.user, tt.args.password, tt.args.host, tt.args.port, tt.args.dbName, tt.args.mysqlGroupConcatMaxLen, tt.args.mysqlExtraParams); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateMySQLConfig() = %#v, want %v", got, tt.want)
}
})
}
}
Loading