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

Split the pr to support named blob ttl update only #2542

Merged
merged 4 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ default CompletableFuture<NamedBlobRecord> get(String accountName, String contai
return get(accountName, containerName, blobName, GetOption.None);
}

/**
* Support ttl update for {@link NamedBlobRecord}
* @param record the {@link NamedBlobRecord}
* @param state the {@link NamedBlobState}
* @return a {@link CompletableFuture} that will eventually contain either the {@link NamedBlobRecord} for the named
* blob or an exception if an error occurred.
*/
CompletableFuture<PutResult> ttlUpdate(NamedBlobRecord record, NamedBlobState state);

/**
* List blobs that start with a provided prefix in a container. This returns paginated results. If there are
* additional pages to read, {@link Page#getNextPageToken()} will be non null.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public class NamedBlobRecord {
private final String accountName;
private final String containerName;
private final String blobName;
private final String blobId;
private final long expirationTimeMs;
private long version;
private String blobId;

/**
* @param accountName the account name.
Expand Down Expand Up @@ -98,6 +98,10 @@ public void setVersion(long newVersion) {
version = newVersion;
}

public void setBlobId(String blobId) {
this.blobId = blobId;
}

/**
* @return the expiration time in milliseconds since epoch, or -1 if the blob should be permanent.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.github.ambry.rest.RestServiceException;
import com.github.ambry.utils.Pair;
import com.github.ambry.utils.Time;
import com.github.ambry.utils.Utils;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
Expand Down Expand Up @@ -70,7 +71,7 @@ public CompletableFuture<NamedBlobRecord> get(String accountName, String contain
} else if (recordWithDelete.getSecond() != 0 && recordWithDelete.getSecond() < time.milliseconds()
&& !includeDeletedOptions.contains(option)) {
future.completeExceptionally(new RestServiceException("Deleted", RestServiceErrorCode.Deleted));
} else if (recordWithDelete.getFirst().getExpirationTimeMs() != 0
} else if (recordWithDelete.getFirst().getExpirationTimeMs() != Utils.Infinite_Time
&& recordWithDelete.getFirst().getExpirationTimeMs() < time.milliseconds() && !includeExpiredOptions.contains(
option)) {
future.completeExceptionally(new RestServiceException("Deleted", RestServiceErrorCode.Deleted));
Expand Down Expand Up @@ -140,6 +141,12 @@ public CompletableFuture<PutResult> put(NamedBlobRecord record, NamedBlobState s
return future;
}

@Override
public CompletableFuture<PutResult> ttlUpdate(NamedBlobRecord record, NamedBlobState state) {
//TODO: implement later
return null;
}

@Override
public CompletableFuture<PutResult> updateBlobStateToReady(NamedBlobRecord record) {
CompletableFuture<PutResult> future = new CompletableFuture<>();
Expand Down Expand Up @@ -278,6 +285,6 @@ private void setException(Exception exception) {

@Override
public void close() throws IOException {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,31 @@ public void testPutGetListDeleteSequence() throws Exception {
}
}

/**
* Test ttl update for named blob.
* @throws Exception
*/
@Test
public void testUpdateTtlForNamedBlob() throws Exception {
Account account = accountService.getAllAccounts().iterator().next();
Container container = account.getAllContainers().iterator().next();
String blobId = getBlobId(account, container);
String blobName = "blobName";
long expirationTime = System.currentTimeMillis() + TimeUnit.HOURS.toMillis(1);
NamedBlobRecord record =
new NamedBlobRecord(account.getName(), container.getName(), blobName, blobId, expirationTime);
namedBlobDb.put(record).get();
time.setCurrentMilliseconds(System.currentTimeMillis());
PutResult ttlUpdateResult = namedBlobDb.ttlUpdate(record, NamedBlobState.IN_PROGRESS).get();
record.setVersion(ttlUpdateResult.getInsertedRecord().getVersion());
time.setCurrentMilliseconds(System.currentTimeMillis());
namedBlobDb.updateBlobStateToReady(record).get();
time.setCurrentMilliseconds(System.currentTimeMillis());
NamedBlobRecord recordFromDb = namedBlobDb.get(account.getName(), container.getName(), blobName).get();
assertEquals("Mismatch on blob version", ttlUpdateResult.getInsertedRecord().getVersion(), recordFromDb.getVersion());
assertEquals("Mismatch on blob expiration time", Utils.Infinite_Time, recordFromDb.getExpirationTimeMs());
}

/**
* Test behavior with expired blobs
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,30 @@ private void applySoftDelete(short accountId, short containerId, String blobName
}
}

@Override
public CompletableFuture<PutResult> ttlUpdate(NamedBlobRecord record, NamedBlobState state) {
Copy link
Collaborator

@justinlin-linkedin justinlin-linkedin Aug 18, 2023

Choose a reason for hiding this comment

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

can we have a dedicated sql statement for updating ttl. And there should be some limitation to when a named blob's ttl can be updated, like if there is an expiration date, it needs to be more than one day. We should share the same configuration with what is in the server nodes. Maybe we can leave this logic out side of mysql database and have it in the http request handler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As we discussed offline, I think I can re-use this updateBlobStateToReady method, just change the name to updateBlobTtlAndStateToReady to be more clear.
And in covertId logic, when operation is TTL_UPDATE, we just get the named blob for id and version.

conversionFuture = getNamedBlobDb().get(namedBlobPath.getAccountName(), namedBlobPath.getContainerName(), namedBlobPath.getBlobName(), getOption).thenApply(result -> { restRequest.setArg(RestUtils.InternalKeys.NAMED_BLOB_VERSION, result.getInsertedRecord().getVersion()); restRequest.setArg(RestUtils.InternalKeys.NAMED_BLOB_MAPPED_ID, result.getInsertedRecord().getBlobId()); return result.getInsertedRecord().getBlobId(); });

And in ttlUpdateHanlder -> routerCallback we can directly call this method. updateBlobTtlAndStateToReady with the blobId and Version updated in the newNamedBlobRecord.
NamedBlobRecord record = new NamedBlobRecord(namedBlobPath.getAccountName(), namedBlobPath.getContainerName(), namedBlobPath.getBlobName(), (String) restRequest.getArgs().get(NAMED_BLOB_MAPPED_ID), Utils.Infinite_Time, namedBlobVersion); namedBlobDb.updateBlobStateToReady(record).get();

return executeTransactionAsync(record.getAccountName(), record.getContainerName(), true,
(accountId, containerId, connection) -> {
long startTime = this.time.milliseconds();
logger.trace("NamedBlobPutInfo: accountId='{}', containerId='{}', blobName='{}'", accountId, containerId,
record.getBlobName());
//for ttl update, get the blob id first before insert a new record.
NamedBlobRecord recordCurrent;
try {
recordCurrent =
run_get_v2(record.getAccountName(), record.getContainerName(), record.getBlobName(), GetOption.None,
accountId, containerId, connection);
} catch (RestServiceException e) {
throw buildException("Failed to do ttl update due to not able to found existing record", e.getErrorCode(),
record.getAccountName(), record.getContainerName(), record.getBlobName());
}
record.setBlobId(recordCurrent.getBlobId());
PutResult putResult = run_put_v2(record, state, accountId, containerId, connection);
metricsRecoder.namedBlobPutTimeInMs.update(this.time.milliseconds() - startTime);
return putResult;
}, null);
}

/**
* Build the version for Named Blob row based on timestamp and uuid postfix.
* @return a long number whose rightmost 5 digits are uuid postfix, and the remaining digits are current timestamp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.github.ambry.rest.RestServiceErrorCode;
import com.github.ambry.rest.RestServiceException;
import com.github.ambry.utils.TestUtils;
import com.github.ambry.utils.Utils;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
Expand Down Expand Up @@ -164,6 +165,25 @@ public void testUpdateBlobStateToReady() throws Exception {
assertEquals("Blob Id is not matched with the record", id, namedBlobRecord.getBlobId());
}

/**
* Test update ttl for named blob.
* @throws Exception
*/
@Test
public void testUpdateTtlForNamedBlob() throws Exception {
dataSourceFactory.setLocalDatacenter(foundDatacenter);
dataSourceFactory.triggerDataResultSet(datacenters);
long expirationTimeMs = System.currentTimeMillis() + 3600;
NamedBlobRecord record = new NamedBlobRecord(account.getName(), container.getName(), "blobName", id, expirationTimeMs);
namedBlobDb.put(record, NamedBlobState.READY, true).get();
assertEquals("Mismatch on expiration time", expirationTimeMs , record.getExpirationTimeMs());
PutResult putResult = namedBlobDb.ttlUpdate(record, NamedBlobState.IN_PROGRESS).get();
record.setVersion(putResult.getInsertedRecord().getVersion());
namedBlobDb.updateBlobStateToReady(record).get();
record = namedBlobDb.get(account.getName(), container.getName(), "blobName").get();
assertEquals("Mismatch on expiration time", Utils.Infinite_Time , record.getExpirationTimeMs());
}

/**
* @param callable an async call, where the {@link Future} is expected to be completed with an exception.
* @param errorCode the expected {@link RestServiceErrorCode}.
Expand Down
Loading