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

Revert "Merge pull request #22 from brave/ttl" #23

Merged
merged 1 commit into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 0 additions & 38 deletions datastore/datastoretest/dynamo.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,41 +150,3 @@ func ScanClientItemCounts(dynamo *datastore.Dynamo) ([]datastore.ClientItemCount

return clientItemCounts, nil
}

// TTL is used to unmarshal result of TTL attribute from dynamoDB.
type TTL struct {
TTL *int64 `dynamodbav:"ttl,omitempty"`
}

// IsTTLSet checks if TTL is set for an item.
func IsTTLSet(dynamo *datastore.Dynamo, clientID string, id string) (bool, error) {
primaryKey := datastore.PrimaryKey{ClientID: clientID, ID: id}
key, err := dynamodbattribute.MarshalMap(primaryKey)
if err != nil {
return false, fmt.Errorf("error marshalling primary key: %w", err)
}

proj := expression.NamesList(expression.Name("ttl"))
expr, err := expression.NewBuilder().WithProjection(proj).Build()
if err != nil {
return false, fmt.Errorf("error building expr: %w", err)
}

input := &dynamodb.GetItemInput{
Key: key,
ExpressionAttributeNames: expr.Names(),
ProjectionExpression: expr.Projection(),
TableName: aws.String(datastore.Table),
}
out, err := dynamo.GetItem(input)
if err != nil {
return false, fmt.Errorf("error doing get TTL for an item: %w", err)
}

ttl := TTL{}
err = dynamodbattribute.UnmarshalMap(out.Item, &ttl)
if err != nil {
return false, fmt.Errorf("error unmarshalling TTL: %w", err)
}
return ttl.TTL != nil, nil
}
8 changes: 0 additions & 8 deletions datastore/sync_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ const (
clientTagItemPrefix = "Client#"
serverTagItemPrefix = "Server#"
conditionalCheckFailed = "ConditionalCheckFailed"
ttlAttrName = "ttl"
ttl = time.Hour * 24 * 90 // 90 days
)

// SyncEntity is used to marshal and unmarshal sync items in dynamoDB.
Expand Down Expand Up @@ -294,12 +292,6 @@ func (dynamo *Dynamo) UpdateSyncEntity(entity *SyncEntity) (bool, bool, error) {
}
if entity.Deleted != nil {
update = update.Set(expression.Name("Deleted"), expression.Value(entity.Deleted))

// Set TTL attribute when soft-deleting an item which will remove obsolete
// items automatically by dynamoDB.
if *entity.Deleted {
update = update.Set(expression.Name(ttlAttrName), expression.Value(time.Now().Add(ttl).Unix()))
}
}
if entity.Folder != nil {
update = update.Set(expression.Name("Folder"), expression.Value(entity.Folder))
Expand Down
40 changes: 0 additions & 40 deletions datastore/sync_entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,46 +311,6 @@ func (suite *SyncEntityTestSuite) TestUpdateSyncEntity_Basic() {
suite.Assert().Equal(syncItems, []datastore.SyncEntity{updateEntity1, updateEntity2, updateEntity3})
}

func (suite *SyncEntityTestSuite) TestUpdateSyncEntity_AddTTLWhenSoftDelete() {
entity1 := datastore.SyncEntity{
ClientID: "client1",
ID: "id1",
Version: aws.Int64(1),
Ctime: aws.Int64(12345678),
Mtime: aws.Int64(12345678),
DataType: aws.Int(123),
Folder: aws.Bool(false),
Deleted: aws.Bool(false),
DataTypeMtime: aws.String("123#12345678"),
Specifics: []byte{1, 2},
}
suite.Require().NoError(
suite.dynamo.InsertSyncEntity(&entity1), "InsertSyncEntity should succeed")

// Do a non-delete update.
entity1.Version = aws.Int64(2)
conflict, delete, err := suite.dynamo.UpdateSyncEntity(&entity1)
suite.Require().NoError(err, "UpdateSyncEntity should succeed")
suite.Assert().False(conflict, "Successful update should not have conflict")
suite.Assert().False(delete, "Non-delete operation should return false")

isTTLSet, err := datastoretest.IsTTLSet(suite.dynamo, entity1.ClientID, entity1.ID)
suite.Require().NoError(err, "IsTTLSet should succeed")
suite.Assert().False(isTTLSet, "TTL should not be set for non-delete operations")

// Do a soft-delete.
entity1.Version = aws.Int64(3)
entity1.Deleted = aws.Bool(true)
conflict, delete, err = suite.dynamo.UpdateSyncEntity(&entity1)
suite.Require().NoError(err, "UpdateSyncEntity should succeed")
suite.Assert().False(conflict, "Successful update should not have conflict")
suite.Assert().True(delete, "Delete operation should return false")

isTTLSet, err = datastoretest.IsTTLSet(suite.dynamo, entity1.ClientID, entity1.ID)
suite.Require().NoError(err, "IsTTLSet should succeed")
suite.Assert().True(isTTLSet, "TTL should be set when soft-delete")
}

func (suite *SyncEntityTestSuite) TestUpdateSyncEntity_ReuseClientTag() {
// Insert an item with client tag.
entity1 := datastore.SyncEntity{
Expand Down
3 changes: 0 additions & 3 deletions dynamo.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ RUN mkdir -p ${DB_LOCATION} && \
DYNAMO_PID=$! && \
aws dynamodb create-table --cli-input-json file://table.json \
--endpoint-url ${AWS_ENDPOINT} --region ${AWS_REGION} && \
aws dynamodb update-time-to-live --table-name ${TABLE_NAME} \
--endpoint-url ${AWS_ENDPOINT} --region ${AWS_REGION} \
--time-to-live-specification Enabled=true,AttributeName=ttl && \
kill $DYNAMO_PID

FROM amazon/dynamodb-local:1.12.0
Expand Down