Search in sources :

Example 6 with CheckpointV2

use of org.apache.samza.checkpoint.CheckpointV2 in project samza by apache.

the class TestContainerStorageManager method testCheckpointV2BasedRestoreFactoryCreation.

@Test
public void testCheckpointV2BasedRestoreFactoryCreation() {
    Set<String> storeNames = ImmutableSet.of("storeName0", "storeName1", "storeName2");
    StorageConfig mockConfig = mock(StorageConfig.class);
    when(mockConfig.getStoreRestoreFactories("storeName0")).thenReturn(ImmutableList.of("factory0", "factory1", "factory2"));
    when(mockConfig.getStoreRestoreFactories("storeName1")).thenReturn(ImmutableList.of("factory2", "factory1"));
    when(mockConfig.getStoreRestoreFactories("storeName2")).thenReturn(Collections.emptyList());
    when(mockConfig.getChangelogStream("storeName0")).thenReturn(Optional.empty());
    when(mockConfig.getChangelogStream("storeName1")).thenReturn(Optional.of("changelog"));
    when(mockConfig.getChangelogStream("storeName2")).thenReturn(Optional.of("changelog"));
    CheckpointV2 checkpointV2 = mock(CheckpointV2.class);
    when(checkpointV2.getVersion()).thenReturn((short) 2);
    when(checkpointV2.getStateCheckpointMarkers()).thenReturn(ImmutableMap.of("factory0", ImmutableMap.of("storeName0", "", "storeName1", "", "storeName2", ""), "factory1", ImmutableMap.of("storeName0", "", "storeName1", "", "storeName2", ""), "factory2", ImmutableMap.of("storeName0", "", "storeName1", "", "storeName2", "")));
    Map<String, Set<String>> factoriesToStores = this.containerStorageManager.getBackendFactoryStoreNames(checkpointV2, storeNames, mockConfig);
    Assert.assertEquals(2, factoriesToStores.size());
    Assert.assertEquals(ImmutableSet.of("storeName0"), factoriesToStores.get("factory0"));
    Assert.assertEquals(ImmutableSet.of("storeName1"), factoriesToStores.get("factory2"));
    when(checkpointV2.getStateCheckpointMarkers()).thenReturn(ImmutableMap.of("factory2", ImmutableMap.of("storeName0", "", "storeName1", "", "storeName2", "")));
    factoriesToStores = this.containerStorageManager.getBackendFactoryStoreNames(checkpointV2, storeNames, mockConfig);
    Assert.assertEquals(1, factoriesToStores.size());
    Assert.assertEquals(ImmutableSet.of("storeName1", "storeName0"), factoriesToStores.get("factory2"));
    when(checkpointV2.getStateCheckpointMarkers()).thenReturn(ImmutableMap.of("factory1", ImmutableMap.of("storeName0", "", "storeName1", "", "storeName2", ""), "factory2", ImmutableMap.of("storeName0", "", "storeName1", "", "storeName2", "")));
    factoriesToStores = this.containerStorageManager.getBackendFactoryStoreNames(checkpointV2, storeNames, mockConfig);
    Assert.assertEquals(2, factoriesToStores.size());
    Assert.assertEquals(ImmutableSet.of("storeName0"), factoriesToStores.get("factory1"));
    Assert.assertEquals(ImmutableSet.of("storeName1"), factoriesToStores.get("factory2"));
    when(checkpointV2.getStateCheckpointMarkers()).thenReturn(ImmutableMap.of("factory1", ImmutableMap.of("storeName0", "", "storeName1", "", "storeName2", ""), "factory2", ImmutableMap.of("storeName0", "", "storeName2", "")));
    factoriesToStores = this.containerStorageManager.getBackendFactoryStoreNames(checkpointV2, storeNames, mockConfig);
    Assert.assertEquals(1, factoriesToStores.size());
    Assert.assertEquals(ImmutableSet.of("storeName0", "storeName1"), factoriesToStores.get("factory1"));
    when(checkpointV2.getStateCheckpointMarkers()).thenReturn(ImmutableMap.of("factory1", ImmutableMap.of("storeName0", "", "storeName1", "", "storeName2", "")));
    factoriesToStores = this.containerStorageManager.getBackendFactoryStoreNames(checkpointV2, storeNames, mockConfig);
    Assert.assertEquals(1, factoriesToStores.size());
    Assert.assertEquals(ImmutableSet.of("storeName0", "storeName1"), factoriesToStores.get("factory1"));
    when(checkpointV2.getStateCheckpointMarkers()).thenReturn(Collections.emptyMap());
    factoriesToStores = this.containerStorageManager.getBackendFactoryStoreNames(checkpointV2, storeNames, mockConfig);
    Assert.assertEquals(2, factoriesToStores.size());
    Assert.assertEquals(ImmutableSet.of("storeName0"), factoriesToStores.get("factory0"));
    Assert.assertEquals(ImmutableSet.of("storeName1"), factoriesToStores.get("factory2"));
    when(checkpointV2.getStateCheckpointMarkers()).thenReturn(ImmutableMap.of("factory0", ImmutableMap.of("storeName1", "", "storeName2", ""), "factory1", ImmutableMap.of("storeName1", "", "storeName2", ""), "factory2", ImmutableMap.of("storeName0", "", "storeName2", "")));
    factoriesToStores = this.containerStorageManager.getBackendFactoryStoreNames(checkpointV2, storeNames, mockConfig);
    Assert.assertEquals(2, factoriesToStores.size());
    Assert.assertEquals(ImmutableSet.of("storeName1"), factoriesToStores.get("factory1"));
    Assert.assertEquals(ImmutableSet.of("storeName0"), factoriesToStores.get("factory2"));
}
Also used : CheckpointV2(org.apache.samza.checkpoint.CheckpointV2) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) StorageConfig(org.apache.samza.config.StorageConfig) Test(org.junit.Test)

Example 7 with CheckpointV2

use of org.apache.samza.checkpoint.CheckpointV2 in project samza by apache.

the class TransactionalStateTaskRestoreManager method getCheckpointId.

private CheckpointId getCheckpointId(Checkpoint checkpoint) {
    if (checkpoint == null)
        return null;
    if (checkpoint instanceof CheckpointV1) {
        for (Map.Entry<String, SystemStream> storeNameSystemStream : storeChangelogs.entrySet()) {
            SystemStreamPartition storeChangelogSSP = new SystemStreamPartition(storeNameSystemStream.getValue(), taskModel.getChangelogPartition());
            String checkpointMessage = checkpoint.getOffsets().get(storeChangelogSSP);
            if (StringUtils.isNotBlank(checkpointMessage)) {
                KafkaChangelogSSPOffset kafkaStateChanglogOffset = KafkaChangelogSSPOffset.fromString(checkpointMessage);
                return kafkaStateChanglogOffset.getCheckpointId();
            }
        }
    } else if (checkpoint instanceof CheckpointV2) {
        return ((CheckpointV2) checkpoint).getCheckpointId();
    } else {
        throw new SamzaException("Unsupported checkpoint version: " + checkpoint.getVersion());
    }
    return null;
}
Also used : CheckpointV2(org.apache.samza.checkpoint.CheckpointV2) SystemStream(org.apache.samza.system.SystemStream) CheckpointV1(org.apache.samza.checkpoint.CheckpointV1) KafkaChangelogSSPOffset(org.apache.samza.checkpoint.kafka.KafkaChangelogSSPOffset) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SamzaException(org.apache.samza.SamzaException) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition)

Example 8 with CheckpointV2

use of org.apache.samza.checkpoint.CheckpointV2 in project samza by apache.

the class TestBlobStoreUtil method testGetSSIReturnsEmptyMapIfNoStoreForBlobStoreBackendFactory.

@Test
public void testGetSSIReturnsEmptyMapIfNoStoreForBlobStoreBackendFactory() {
    CheckpointV2 mockCheckpoint = mock(CheckpointV2.class);
    when(mockCheckpoint.getVersion()).thenReturn((short) 2);
    when(mockCheckpoint.getStateCheckpointMarkers()).thenReturn(ImmutableMap.of(BlobStoreStateBackendFactory.class.getName(), ImmutableMap.of()));
    BlobStoreUtil blobStoreUtil = new BlobStoreUtil(mock(BlobStoreManager.class), MoreExecutors.newDirectExecutorService(), null, null);
    Map<String, Pair<String, SnapshotIndex>> snapshotIndexes = blobStoreUtil.getStoreSnapshotIndexes("testJobName", "testJobId", "taskName", mockCheckpoint, new HashSet<>());
    assertTrue(snapshotIndexes.isEmpty());
}
Also used : CheckpointV2(org.apache.samza.checkpoint.CheckpointV2) BlobStoreManager(org.apache.samza.storage.blobstore.BlobStoreManager) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 9 with CheckpointV2

use of org.apache.samza.checkpoint.CheckpointV2 in project samza by apache.

the class TestBlobStoreUtil method createCheckpointV2.

private CheckpointV2 createCheckpointV2(String stateBackendFactory, Map<String, String> storeSnapshotIndexBlobIds) {
    CheckpointId checkpointId = CheckpointId.create();
    Map<String, Map<String, String>> factoryStoreSCMs = new HashMap<>();
    Map<String, String> storeSCMs = new HashMap<>();
    for (Map.Entry<String, String> entry : storeSnapshotIndexBlobIds.entrySet()) {
        storeSCMs.put(entry.getKey(), entry.getValue());
    }
    factoryStoreSCMs.put(stateBackendFactory, storeSCMs);
    return new CheckpointV2(checkpointId, ImmutableMap.of(), factoryStoreSCMs);
}
Also used : CheckpointV2(org.apache.samza.checkpoint.CheckpointV2) HashMap(java.util.HashMap) CheckpointId(org.apache.samza.checkpoint.CheckpointId) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 10 with CheckpointV2

use of org.apache.samza.checkpoint.CheckpointV2 in project samza by apache.

the class TestBlobStoreUtil method testGetCheckpointIndexIgnoresStoresNotInStoresToBackupRestoreSet.

@Test
public void testGetCheckpointIndexIgnoresStoresNotInStoresToBackupRestoreSet() {
    String store = "storeName1";
    String anotherStore = "storeName2";
    String oneMoreStore = "storeName3";
    SnapshotIndex mockStoreSnapshotIndex = mock(SnapshotIndex.class);
    Set<String> storesToBackupOrRestore = ImmutableSet.of(store, anotherStore);
    CheckpointV2 checkpoint = createCheckpointV2(BlobStoreStateBackendFactory.class.getName(), ImmutableMap.of(store, "1", anotherStore, "2", oneMoreStore, "3"));
    BlobStoreUtil mockBlobStoreUtil = mock(BlobStoreUtil.class);
    when(mockBlobStoreUtil.getSnapshotIndex(any(String.class), any(Metadata.class))).thenReturn(CompletableFuture.completedFuture(mockStoreSnapshotIndex));
    when(mockBlobStoreUtil.getStoreSnapshotIndexes(anyString(), anyString(), anyString(), any(Checkpoint.class), anySetOf(String.class))).thenCallRealMethod();
    Map<String, Pair<String, SnapshotIndex>> snapshotIndexes = mockBlobStoreUtil.getStoreSnapshotIndexes("testJobName", "testJobId", "taskName", checkpoint, storesToBackupOrRestore);
    verify(mockBlobStoreUtil, times(storesToBackupOrRestore.size())).getSnapshotIndex(anyString(), any(Metadata.class));
}
Also used : CheckpointV2(org.apache.samza.checkpoint.CheckpointV2) BlobStoreStateBackendFactory(org.apache.samza.storage.blobstore.BlobStoreStateBackendFactory) SnapshotIndex(org.apache.samza.storage.blobstore.index.SnapshotIndex) Checkpoint(org.apache.samza.checkpoint.Checkpoint) FileMetadata(org.apache.samza.storage.blobstore.index.FileMetadata) SnapshotMetadata(org.apache.samza.storage.blobstore.index.SnapshotMetadata) Metadata(org.apache.samza.storage.blobstore.Metadata) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Aggregations

CheckpointV2 (org.apache.samza.checkpoint.CheckpointV2)24 Test (org.junit.Test)18 HashMap (java.util.HashMap)13 Checkpoint (org.apache.samza.checkpoint.Checkpoint)13 Map (java.util.Map)12 SamzaException (org.apache.samza.SamzaException)11 File (java.io.File)10 CheckpointId (org.apache.samza.checkpoint.CheckpointId)10 ImmutableMap (com.google.common.collect.ImmutableMap)9 Collections (java.util.Collections)9 CompletableFuture (java.util.concurrent.CompletableFuture)9 TaskName (org.apache.samza.container.TaskName)9 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)9 Optional (java.util.Optional)8 CheckpointV1 (org.apache.samza.checkpoint.CheckpointV1)8 Config (org.apache.samza.config.Config)8 MapConfig (org.apache.samza.config.MapConfig)8 TaskMode (org.apache.samza.job.model.TaskMode)8 SystemStream (org.apache.samza.system.SystemStream)8 IOException (java.io.IOException)7