Skip to content

Commit 1ab61c6

Browse files
teranДемин Михаил
andauthored
Add TTL setting for container versions in CLI, GC and manager (#269)
Reincarnation of staled PR #196 Closes #129 --------- Signed-off-by: Igor Shishkin <[email protected]> Co-authored-by: Демин Михаил <[email protected]>
1 parent bde668c commit 1ab61c6

File tree

24 files changed

+487
-143
lines changed

24 files changed

+487
-143
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ container rename <old-name> <new-name>
143143
container delete <name>
144144
delete the given container
145145

146+
container ttl <name> <ttl>
147+
set TTL (in hours) for container versions
148+
146149
container list
147150
list containers
148151

cli/service/pb_mock.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package service
22

33
import (
44
"context"
5+
"time"
56

67
v1proto "github.com/teran/archived/manager/presenter/grpc/proto/v1"
78
"github.com/teran/archived/repositories/blob/mock"
@@ -60,6 +61,11 @@ func (m *protoClientMock) DeleteContainer(ctx context.Context, in *v1proto.Delet
6061
return &v1proto.DeleteContainerResponse{}, args.Error(0)
6162
}
6263

64+
func (m *protoClientMock) SetContainerParameters(ctx context.Context, in *v1proto.SetContainerParametersRequest, opts ...grpc.CallOption) (*v1proto.SetContainerParametersResponse, error) {
65+
args := m.Called(in.GetNamespace(), in.GetName(), time.Duration(in.GetTtlSeconds())*time.Second)
66+
return &v1proto.SetContainerParametersResponse{}, args.Error(0)
67+
}
68+
6369
func (m *protoClientMock) ListContainers(ctx context.Context, in *v1proto.ListContainersRequest, opts ...grpc.CallOption) (*v1proto.ListContainersResponse, error) {
6470
args := m.Called(in.GetNamespace())
6571
return &v1proto.ListContainersResponse{

cli/service/service.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
"time"
89

910
"github.com/pkg/errors"
1011
log "github.com/sirupsen/logrus"
1112

1213
"github.com/teran/archived/cli/service/source"
1314
cache "github.com/teran/archived/cli/service/stat_cache"
1415
v1proto "github.com/teran/archived/manager/presenter/grpc/proto/v1"
16+
ptr "github.com/teran/go-ptr"
1517
)
1618

1719
type Service interface {
@@ -20,11 +22,12 @@ type Service interface {
2022
ListNamespaces() func(ctx context.Context) error
2123
DeleteNamespace(namespaceName string) func(ctx context.Context) error
2224

23-
CreateContainer(namespaceName, containerName string) func(ctx context.Context) error
25+
CreateContainer(namespaceName, containerName string, ttl time.Duration) func(ctx context.Context) error
2426
MoveContainer(namespaceName, containerName, destinationNamespace string) func(ctx context.Context) error
2527
RenameContainer(namespaceName, oldName, newName string) func(ctx context.Context) error
2628
ListContainers(namespaceName string) func(ctx context.Context) error
2729
DeleteContainer(namespaceName, containerName string) func(ctx context.Context) error
30+
SetContainerParameters(namespaceName, containerName string, ttl time.Duration) func(ctx context.Context) error
2831

2932
CreateVersion(namespaceName, containerName string, shouldPublish bool, src source.Source) func(ctx context.Context) error
3033
DeleteVersion(namespaceName, containerName, versionID string) func(ctx context.Context) error
@@ -103,11 +106,12 @@ func (s *service) DeleteNamespace(namespaceName string) func(ctx context.Context
103106
}
104107
}
105108

106-
func (s *service) CreateContainer(namespaceName, containerName string) func(ctx context.Context) error {
109+
func (s *service) CreateContainer(namespaceName, containerName string, ttl time.Duration) func(ctx context.Context) error {
107110
return func(ctx context.Context) error {
108111
_, err := s.cli.CreateContainer(ctx, &v1proto.CreateContainerRequest{
109-
Namespace: namespaceName,
110-
Name: containerName,
112+
Namespace: namespaceName,
113+
Name: containerName,
114+
TtlSeconds: ptr.Int64(int64(ttl.Seconds())),
111115
})
112116
if err != nil {
113117
return errors.Wrap(err, "error creating container")
@@ -178,6 +182,22 @@ func (s *service) DeleteContainer(namespaceName, containerName string) func(ctx
178182
}
179183
}
180184

185+
func (s *service) SetContainerParameters(namespaceName, containerName string, ttl time.Duration) func(ctx context.Context) error {
186+
return func(ctx context.Context) error {
187+
_, err := s.cli.SetContainerParameters(ctx, &v1proto.SetContainerParametersRequest{
188+
Namespace: namespaceName,
189+
Name: containerName,
190+
TtlSeconds: ptr.Int64(int64(ttl.Seconds())),
191+
})
192+
if err != nil {
193+
return errors.Wrap(err, "error setting container versions TTL")
194+
}
195+
196+
fmt.Printf("container `%s` versions TTL set to %s\n", containerName, ttl)
197+
return nil
198+
}
199+
}
200+
181201
func (s *service) CreateVersion(namespaceName, containerName string, shouldPublish bool, src source.Source) func(ctx context.Context) error {
182202
return func(ctx context.Context) error {
183203
log.Tracef("creating version ...")

cli/service/service_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33
import (
44
"context"
55
"testing"
6+
"time"
67

78
log "github.com/sirupsen/logrus"
89
"github.com/stretchr/testify/suite"
@@ -50,7 +51,7 @@ func (s *serviceTestSuite) TestDeleteNamespace() {
5051
func (s *serviceTestSuite) TestCreateContainer() {
5152
s.cliMock.On("CreateContainer", defaultNamespace, "test-container").Return(nil).Once()
5253

53-
fn := s.svc.CreateContainer(defaultNamespace, "test-container")
54+
fn := s.svc.CreateContainer(defaultNamespace, "test-container", -1)
5455
s.Require().NoError(fn(s.ctx))
5556
}
5657

@@ -82,6 +83,13 @@ func (s *serviceTestSuite) TestDeleteContainer() {
8283
s.Require().NoError(fn(s.ctx))
8384
}
8485

86+
func (s *serviceTestSuite) TestSetContainerParameters() {
87+
s.cliMock.On("SetContainerParameters", defaultNamespace, "test-container1", 3600*time.Second).Return(nil).Once()
88+
89+
fn := s.svc.SetContainerParameters(defaultNamespace, "test-container1", 3600*time.Second)
90+
s.Require().NoError(fn(s.ctx))
91+
}
92+
8593
func (s *serviceTestSuite) TestCreateVersion() {
8694
s.cliMock.On("CreateVersion", defaultNamespace, "container1").Return("version_id", nil).Once()
8795

cmd/cli/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ var (
8383
container = app.Command("container", "container operations")
8484
containerCreate = container.Command("create", "create new container")
8585
containerCreateName = containerCreate.Arg("name", "name of the container to create").Required().String()
86+
containerCreateTTL = containerCreate.Flag("ttl", "Default container TTL").Default("-1ns").Duration()
8687

8788
containerMove = container.Command("move", "move container to another namespace")
8889
containerMoveName = containerMove.Arg("name", "container namespace to move").Required().String()
@@ -95,6 +96,10 @@ var (
9596
containerDelete = container.Command("delete", "delete the given container")
9697
containerDeleteName = containerDelete.Arg("name", "name of the container to delete").Required().String()
9798

99+
containerSet = container.Command("set", "set parameters for container")
100+
containerSetContainer = containerSet.Arg("name", "name of the container").Required().String()
101+
containerSetTTL = containerSet.Flag("ttl", "Container TTL").Default("-1ns").Duration()
102+
98103
containerList = container.Command("list", "list containers")
99104

100105
version = app.Command("version", "version operations")
@@ -233,11 +238,12 @@ func main() {
233238
r.Register(namespaceList.FullCommand(), cliSvc.ListNamespaces())
234239
r.Register(namespaceDelete.FullCommand(), cliSvc.DeleteNamespace(*namespaceDeleteName))
235240

236-
r.Register(containerCreate.FullCommand(), cliSvc.CreateContainer(*namespaceName, *containerCreateName))
241+
r.Register(containerCreate.FullCommand(), cliSvc.CreateContainer(*namespaceName, *containerCreateName, *containerCreateTTL))
237242
r.Register(containerMove.FullCommand(), cliSvc.MoveContainer(*namespaceName, *containerMoveName, *containerMoveNamespace))
238243
r.Register(containerRename.FullCommand(), cliSvc.RenameContainer(*namespaceName, *containerRenameOldName, *containerRenameNewName))
239244
r.Register(containerList.FullCommand(), cliSvc.ListContainers(*namespaceName))
240245
r.Register(containerDelete.FullCommand(), cliSvc.DeleteContainer(*namespaceName, *containerDeleteName))
246+
r.Register(containerSet.FullCommand(), cliSvc.SetContainerParameters(*namespaceName, *containerSetContainer, *containerSetTTL))
241247

242248
r.Register(versionList.FullCommand(), cliSvc.ListVersions(*namespaceName, *versionListContainer))
243249
r.Register(versionCreate.FullCommand(), cliSvc.CreateVersion(
@@ -249,7 +255,6 @@ func main() {
249255
r.Register(objectList.FullCommand(), cliSvc.ListObjects(*namespaceName, *objectListContainer, *objectListVersion))
250256
r.Register(objectURL.FullCommand(), cliSvc.GetObjectURL(*namespaceName, *objectURLContainer, *objectURLVersion, *objectURLKey))
251257
r.Register(deleteObject.FullCommand(), cliSvc.DeleteObject(*namespaceName, *deleteObjectContainer, *deleteObjectVersion, *deleteObjectKey))
252-
253258
r.Register(statCacheShowPath.FullCommand(), func(ctx context.Context) error {
254259
fmt.Println(*cacheDir)
255260
return nil

cmd/seeder/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func main() {
110110
}
111111
for j := 0; j <= cfg.CreateContainersPerNamespace; j++ {
112112
container := fmt.Sprintf("container-%06d", j)
113-
if err := managerSvc.CreateContainer(ctx, namespace, container); err != nil {
113+
if err := managerSvc.CreateContainer(ctx, namespace, container, -1); err != nil {
114114
panic(err)
115115
}
116116
for k := 0; k <= cfg.CreateVersionsPerContainer; k++ {

0 commit comments

Comments
 (0)