Skip to content

Commit

Permalink
[patch] add storage backup option to agent
Browse files Browse the repository at this point in the history
Signed-off-by: kpango <i.can.feel.gravity@gmail.com>
  • Loading branch information
kpango committed May 11, 2020
1 parent 54fba1f commit 41c2ba9
Show file tree
Hide file tree
Showing 11 changed files with 514 additions and 126 deletions.
1 change: 1 addition & 0 deletions Makefile.d/functions.mk
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ define gen-test
gotests -w -template_dir $${path} -all $${file}; \
done; \
done
rm $(ROOTDIR)/internal/core/ngt/*test.go
endef

define fix-test
Expand Down
29 changes: 29 additions & 0 deletions charts/vald/templates/agent/pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt )
#
# 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.
#
{{- if .Values.agent.volumeStore.enabled }}
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ .Values.agent.name }}-pvc
spec:
accessModes:
- {{ .Values.agent.volumeStore.accessMode }}
storageClassName: {{ .Values.agent.volumeStore.storageClass }}
spec:
resources:
requests:
storage: {{ .Values.agent.volumeStore.size }}
{{- end }}
9 changes: 9 additions & 0 deletions charts/vald/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,15 @@ agent:
terminationGracePeriodSeconds: 30
# agent.podManagementPolicy -- pod management policy: OrderedReady or Parallel
podManagementPolicy: OrderedReady
volumeStore:
# agent.volumeStore.enabled -- enables agent pod's file store functionality when non in-memory mode
enabled: false
# agent.volumeStore.accessMode -- agent pod storage accessMode
accessMode: ReadWriteOnce
# agent.volumeStore.storageClass -- storageClass name for agent pod volume
storageClass: vald-sc
# agent.volumeStore.size -- size of agent pod volume
size: 100Gi
podPriority:
# agent.podPriority.enabled -- agent pod PriorityClass enabled
enabled: true
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,6 @@ golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191203134012-c197fd4bf371/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200501005904-d351ea090f9b h1:2hSR2MyOaYEy6yJYg/CpErymr/m7xJEJpm9kfT7ZMg4=
golang.org/x/tools v0.0.0-20200501005904-d351ea090f9b/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200501065659-ab2804fb9c9d h1:lzLdP95xJmMpwQ6LUHwrc5V7js93hTiY7gkznu0BgmY=
golang.org/x/tools v0.0.0-20200501065659-ab2804fb9c9d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
14 changes: 8 additions & 6 deletions internal/core/ngt/ngt.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,16 @@ func (n *ngt) CreateIndex(poolSize uint32) error {

// SaveIndex stores NGT index to storage.
func (n *ngt) SaveIndex() error {
n.mu.RLock()
ret := C.ngt_save_index(n.index, C.CString(n.idxPath), n.ebuf)
if ret == ErrorCode {
ne := n.ebuf
if !n.inMemory {
n.mu.Lock()
ret := C.ngt_save_index(n.index, C.CString(n.idxPath), n.ebuf)
if ret == ErrorCode {
ne := n.ebuf
n.mu.Unlock()
return n.newGoError(ne)
}
n.mu.Unlock()
return n.newGoError(ne)
}
n.mu.RUnlock()

return nil
}
Expand Down
57 changes: 57 additions & 0 deletions internal/file/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt )
//
// 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 file provides file I/O functionality
package file

import (
"os"
"path/filepath"
)

func Open(path string, flg int, perm os.FileMode) *os.File {
if path == "" {
return nil
}

var err error
var file *os.File
if _, err = os.Stat(path); err != nil {
if _, err = os.Stat(filepath.Dir(path)); err != nil {
err = os.MkdirAll(filepath.Dir(path), perm)
if err != nil {
return nil
}
}
file, err = os.Create(path)
if err != nil {
return nil
}

err = file.Close()
if err != nil {
return nil
}
}

file, err = os.OpenFile(path, flg, perm)

if err != nil {
return nil
}

return file
}
104 changes: 104 additions & 0 deletions internal/file/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//
// Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt )
//
// 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 file provides file I/O functionality
package file

import (
"os"
"reflect"
"testing"

"github.com/vdaas/vald/internal/errors"
"go.uber.org/goleak"
)

func TestOpen(t *testing.T) {
type args struct {
path string
flg int
perm os.FileMode
}
type want struct {
want *os.File
}
type test struct {
name string
args args
want want
checkFunc func(want, *os.File) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, got *os.File) error {
if !reflect.DeepEqual(got, w.want) {
return errors.Errorf("got = %v, want %v", got, w.want)
}
return nil
}
tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
args: args {
path: "",
flg: 0,
perm: nil,
},
want: want{},
checkFunc: defaultCheckFunc,
},
*/

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
args: args {
path: "",
flg: 0,
perm: nil,
},
want: want{},
checkFunc: defaultCheckFunc,
}
}(),
*/
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(t)
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := Open(test.args.path, test.args.flg, test.args.perm)
if err := test.checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}

})
}
}
4 changes: 2 additions & 2 deletions pkg/agent/ngt/handler/grpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (s *server) SaveIndex(ctx context.Context, _ *payload.Empty) (res *payload.
}
}()
res = new(payload.Empty)
err = s.ngt.SaveIndex()
err = s.ngt.SaveIndex(ctx)
if err != nil {
log.Errorf("[SaveIndex]\tUnknown error\t%+v", err)
if span != nil {
Expand All @@ -394,7 +394,7 @@ func (s *server) CreateAndSaveIndex(ctx context.Context, c *payload.Control_Crea
}
}()
res = new(payload.Empty)
err = s.ngt.CreateAndSaveIndex(c.GetPoolSize())
err = s.ngt.CreateAndSaveIndex(ctx, c.GetPoolSize())
if err != nil {
log.Errorf("[CreateAndSaveIndex]\tUnknown error\t%+v", err)
if span != nil {
Expand Down
Loading

0 comments on commit 41c2ba9

Please sign in to comment.