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

handle unexpected exception on success callback of translog upload #12577

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package org.opensearch.index.translog.transfer;

import org.apache.logging.log4j.Logger;
import org.opensearch.common.logging.Loggers;
import org.opensearch.core.index.shard.ShardId;
import org.opensearch.index.remote.RemoteTranslogTransferTracker;
import org.opensearch.index.translog.transfer.FileSnapshot.TransferFileSnapshot;
Expand All @@ -33,11 +35,13 @@ public class FileTransferTracker implements FileTransferListener {
private final RemoteTranslogTransferTracker remoteTranslogTransferTracker;
private Map<String, Long> bytesForTlogCkpFileToUpload;
private long fileTransferStartTime = -1;
private final Logger logger;

public FileTransferTracker(ShardId shardId, RemoteTranslogTransferTracker remoteTranslogTransferTracker) {
this.shardId = shardId;
this.fileTransferTracker = new ConcurrentHashMap<>();
this.remoteTranslogTransferTracker = remoteTranslogTransferTracker;
this.logger = Loggers.getLogger(getClass(), shardId);
}

void recordFileTransferStartTime(long uploadStartTime) {
Expand All @@ -64,9 +68,14 @@ long getTotalBytesToUpload() {

@Override
public void onSuccess(TransferFileSnapshot fileSnapshot) {
long durationInMillis = (System.nanoTime() - fileTransferStartTime) / 1_000_000L;
remoteTranslogTransferTracker.addUploadTimeInMillis(durationInMillis);
remoteTranslogTransferTracker.addUploadBytesSucceeded(bytesForTlogCkpFileToUpload.get(fileSnapshot.getName()));
try {
long durationInMillis = (System.nanoTime() - fileTransferStartTime) / 1_000_000L;
remoteTranslogTransferTracker.addUploadTimeInMillis(durationInMillis);
remoteTranslogTransferTracker.addUploadBytesSucceeded(bytesForTlogCkpFileToUpload.get(fileSnapshot.getName()));
sachinpkale marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception ex) {
logger.error("Failure to update translog upload success stats", ex);
}

add(fileSnapshot.getName(), TransferState.SUCCESS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import java.util.List;
import java.util.Set;

import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.spy;

public class FileTransferTrackerTests extends OpenSearchTestCase {

protected final ShardId shardId = new ShardId("index", "_na_", 1);
Expand Down Expand Up @@ -94,6 +98,32 @@ public void testOnFailure() throws IOException {
}
}

public void testOnSuccessStatsFailure() throws IOException {
RemoteTranslogTransferTracker localRemoteTranslogTransferTracker = spy(remoteTranslogTransferTracker);
doAnswer((count) -> { throw new NullPointerException("Error while updating stats"); }).when(localRemoteTranslogTransferTracker)
.addUploadBytesSucceeded(anyLong());

FileTransferTracker localFileTransferTracker = new FileTransferTracker(shardId, localRemoteTranslogTransferTracker);

Path testFile = createTempFile();
int fileSize = 128;
Files.write(testFile, randomByteArrayOfLength(fileSize), StandardOpenOption.APPEND);
try (
FileSnapshot.TransferFileSnapshot transferFileSnapshot = new FileSnapshot.TransferFileSnapshot(
testFile,
randomNonNegativeLong(),
null
);
) {
Set<FileSnapshot.TransferFileSnapshot> toUpload = new HashSet<>(2);
toUpload.add(transferFileSnapshot);
localFileTransferTracker.recordBytesForFiles(toUpload);
localRemoteTranslogTransferTracker.addUploadBytesStarted(fileSize);
localFileTransferTracker.onSuccess(transferFileSnapshot);
assertEquals(localFileTransferTracker.allUploaded().size(), 1);
}
}

public void testUploaded() throws IOException {
Path testFile = createTempFile();
int fileSize = 128;
Expand Down
Loading