use of org.apache.samza.checkpoint.CheckpointId in project samza by apache.
the class TaskStorageCommitManager method writeCheckpointToStoreDirectories.
/**
* Writes the {@link Checkpoint} information returned by {@link #upload(CheckpointId, Map)}
* in each store directory and store checkpoint directory. Written content depends on the type of {@code checkpoint}.
* For {@link CheckpointV2}, writes the entire task {@link CheckpointV2}.
* For {@link CheckpointV1}, only writes the changelog ssp offsets in the OFFSET* files.
*
* Note: The assumption is that this method will be invoked once for each {@link Checkpoint} version that the
* task needs to write as determined by {@link org.apache.samza.config.TaskConfig#getCheckpointWriteVersions()}.
* This is required for upgrade and rollback compatibility.
*
* @param checkpoint the latest checkpoint to be persisted to local file system
*/
public void writeCheckpointToStoreDirectories(Checkpoint checkpoint) {
if (checkpoint instanceof CheckpointV1) {
LOG.debug("Writing CheckpointV1 to store and checkpoint directories for taskName: {} with checkpoint: {}", taskName, checkpoint);
// Write CheckpointV1 changelog offsets to store and checkpoint directories
writeChangelogOffsetFiles(checkpoint.getOffsets());
} else if (checkpoint instanceof CheckpointV2) {
LOG.debug("Writing CheckpointV2 to store and checkpoint directories for taskName: {} with checkpoint: {}", taskName, checkpoint);
storageEngines.forEach((storeName, storageEngine) -> {
// Only write the checkpoint file if the store is durable and persisted to disk
if (storageEngine.getStoreProperties().isDurableStore() && storageEngine.getStoreProperties().isPersistedToDisk()) {
CheckpointV2 checkpointV2 = (CheckpointV2) checkpoint;
try {
File storeDir = storageManagerUtil.getTaskStoreDir(durableStoreBaseDir, storeName, taskName, TaskMode.Active);
storageManagerUtil.writeCheckpointV2File(storeDir, checkpointV2);
CheckpointId checkpointId = checkpointV2.getCheckpointId();
File checkpointDir = Paths.get(storageManagerUtil.getStoreCheckpointDir(storeDir, checkpointId)).toFile();
storageManagerUtil.writeCheckpointV2File(checkpointDir, checkpointV2);
} catch (Exception e) {
throw new SamzaException(String.format("Write checkpoint file failed for task: %s, storeName: %s, checkpointId: %s", taskName, storeName, ((CheckpointV2) checkpoint).getCheckpointId()), e);
}
}
});
} else {
throw new SamzaException("Unsupported checkpoint version: " + checkpoint.getVersion());
}
}
use of org.apache.samza.checkpoint.CheckpointId in project samza by apache.
the class TestCheckpointId method testSerializationDeserialization.
@Test
public void testSerializationDeserialization() {
CheckpointId checkpointId = CheckpointId.create();
CheckpointId deserializedCheckpointId = CheckpointId.deserialize(checkpointId.serialize());
assertEquals(checkpointId.getMillis(), deserializedCheckpointId.getMillis());
assertEquals(checkpointId.getNanoId(), deserializedCheckpointId.getNanoId());
assertEquals(checkpointId, deserializedCheckpointId);
}
use of org.apache.samza.checkpoint.CheckpointId in project samza by apache.
the class TestCheckpointId method testSerializationFormatForBackwardsCompatibility.
@Test
public void testSerializationFormatForBackwardsCompatibility() {
CheckpointId checkpointId = CheckpointId.create();
String serializedCheckpointId = checkpointId.serialize();
// WARNING: This format is written to persisted remotes stores and local files, making a change in the format
// would be backwards incompatible
String legacySerializedFormat = serializeLegacy(checkpointId);
assertEquals(checkpointId, CheckpointId.deserialize(legacySerializedFormat));
}
use of org.apache.samza.checkpoint.CheckpointId in project samza by apache.
the class KafkaChangelogSSPOffset method fromString.
public static KafkaChangelogSSPOffset fromString(String message) {
if (StringUtils.isBlank(message)) {
throw new IllegalArgumentException("Invalid checkpointed changelog message: " + message);
}
String[] checkpointIdAndOffset = message.split(SEPARATOR);
if (checkpointIdAndOffset.length != 2) {
throw new IllegalArgumentException("Invalid checkpointed changelog offset: " + message);
}
CheckpointId checkpointId = CheckpointId.deserialize(checkpointIdAndOffset[0]);
String offset = null;
if (!"null".equals(checkpointIdAndOffset[1])) {
offset = checkpointIdAndOffset[1];
}
return new KafkaChangelogSSPOffset(checkpointId, offset);
}
use of org.apache.samza.checkpoint.CheckpointId in project samza by apache.
the class TestBlobStoreUtil method testPutDir.
@Test
public // TODO HIGH shesharm test with empty (0 byte) files
void testPutDir() throws IOException, InterruptedException, ExecutionException {
BlobStoreManager blobStoreManager = mock(BlobStoreManager.class);
// File, dir and recursive dir added, retained and removed in local
String local = "[a, c, z/1, y/1, p/m/1, q/n/1]";
String remote = "[a, b, z/1, x/1, p/m/1, p/m/2, r/o/1]";
String expectedAdded = "[c, y/1, q/n/1]";
String expectedRetained = "[a, z/1, p/m/1]";
String expectedRemoved = "[b, x/1, r/o/1, p/m/2]";
SortedSet<String> expectedAddedFiles = BlobStoreTestUtil.getExpected(expectedAdded);
SortedSet<String> expectedRetainedFiles = BlobStoreTestUtil.getExpected(expectedRetained);
SortedSet<String> expectedPresentFiles = new TreeSet<>(expectedAddedFiles);
expectedPresentFiles.addAll(expectedRetainedFiles);
SortedSet<String> expectedRemovedFiles = BlobStoreTestUtil.getExpected(expectedRemoved);
// Set up environment
Path localSnapshotDir = BlobStoreTestUtil.createLocalDir(local);
String basePath = localSnapshotDir.toAbsolutePath().toString();
DirIndex remoteSnapshotDir = BlobStoreTestUtil.createDirIndex(remote);
SnapshotMetadata snapshotMetadata = new SnapshotMetadata(checkpointId, jobName, jobId, taskName, storeName);
DirDiff dirDiff = DirDiffUtil.getDirDiff(localSnapshotDir.toFile(), remoteSnapshotDir, (localFile, remoteFile) -> localFile.getName().equals(remoteFile.getFileName()));
SortedSet<String> allUploaded = new TreeSet<>();
// Set up mocks
when(blobStoreManager.put(any(InputStream.class), any(Metadata.class))).thenAnswer((Answer<CompletableFuture<String>>) invocation -> {
Metadata metadata = invocation.getArgumentAt(1, Metadata.class);
String path = metadata.getPayloadPath();
allUploaded.add(path.substring(localSnapshotDir.toAbsolutePath().toString().length() + 1));
return CompletableFuture.completedFuture(path);
});
// Execute
BlobStoreUtil blobStoreUtil = new BlobStoreUtil(blobStoreManager, EXECUTOR, null, null);
CompletionStage<DirIndex> dirIndexFuture = blobStoreUtil.putDir(dirDiff, snapshotMetadata);
DirIndex dirIndex = null;
try {
// should be already complete. if not, future composition in putDir is broken.
dirIndex = dirIndexFuture.toCompletableFuture().get(0, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
fail("Future returned from putDir should be already complete.");
}
SortedSet<String> allPresent = new TreeSet<>();
SortedSet<String> allRemoved = new TreeSet<>();
BlobStoreTestUtil.getAllPresentInIndex("", dirIndex, allPresent);
BlobStoreTestUtil.getAllRemovedInIndex("", dirIndex, allRemoved);
// Assert
assertEquals(expectedAddedFiles, allUploaded);
assertEquals(expectedPresentFiles, allPresent);
assertEquals(expectedRemovedFiles, allRemoved);
}
Aggregations