Search in sources :

Example 6 with FileUtil

use of org.apache.samza.util.FileUtil in project samza by apache.

the class CheckpointVersionIntegrationTest method setUp.

@Before
@Override
public void setUp() {
    super.setUp();
    // reset static state shared with task between each parameterized iteration
    MyStatefulApplication.resetTestState();
    // always clear local store on startup
    new FileUtil().rm(new File(LOGGED_STORE_BASE_DIR));
}
Also used : FileUtil(org.apache.samza.util.FileUtil) File(java.io.File) Before(org.junit.Before)

Example 7 with FileUtil

use of org.apache.samza.util.FileUtil in project samza by apache.

the class TaskSideInputStorageManager method initializeStoreDirectories.

/**
 * Initializes the store directories for all the stores:
 *  1. Cleans up the directories for invalid stores.
 *  2. Ensures that the directories exist.
 */
private void initializeStoreDirectories() {
    LOG.info("Initializing side input store directories.");
    stores.keySet().forEach(storeName -> {
        File storeLocation = getStoreLocation(storeName);
        String storePath = storeLocation.toPath().toString();
        if (!isValidSideInputStore(storeName, storeLocation)) {
            LOG.info("Cleaning up the store directory at {} for {}", storePath, storeName);
            new FileUtil().rm(storeLocation);
        }
        if (isPersistedStore(storeName) && !storeLocation.exists()) {
            LOG.info("Creating {} as the store directory for the side input store {}", storePath, storeName);
            storeLocation.mkdirs();
        }
    });
}
Also used : File(java.io.File) FileUtil(org.apache.samza.util.FileUtil)

Example 8 with FileUtil

use of org.apache.samza.util.FileUtil in project samza by apache.

the class StorageManagerUtil method writeCheckpointV2File.

/**
 * Writes the checkpoint to the store checkpoint directory based on the checkpointId.
 *
 * @param storeDir store or store checkpoint directory to write the checkpoint to
 * @param checkpoint checkpoint v2 containing the checkpoint Id
 */
public void writeCheckpointV2File(File storeDir, CheckpointV2 checkpoint) {
    File offsetFile = new File(storeDir, CHECKPOINT_FILE_NAME);
    byte[] fileContents = CHECKPOINT_V2_SERDE.toBytes(checkpoint);
    FileUtil fileUtil = new FileUtil();
    fileUtil.writeWithChecksum(offsetFile, new String(fileContents));
}
Also used : File(java.io.File) FileUtil(org.apache.samza.util.FileUtil)

Example 9 with FileUtil

use of org.apache.samza.util.FileUtil in project samza by apache.

the class StorageManagerUtil method readOffsetFile.

/**
 * Read and return the contents of the offset file.
 *
 * @param storagePartitionDir the base directory of the store
 * @param offsetFileName the name of the offset file
 * @param storeSSPs SSPs associated with the store (if any)
 * @return the content of the offset file if it exists for the store, null otherwise.
 */
private Map<SystemStreamPartition, String> readOffsetFile(File storagePartitionDir, String offsetFileName, Set<SystemStreamPartition> storeSSPs) {
    Map<SystemStreamPartition, String> offsets = new HashMap<>();
    String fileContents = null;
    File offsetFileRef = new File(storagePartitionDir, offsetFileName);
    String storePath = storagePartitionDir.getPath();
    if (offsetFileRef.exists()) {
        LOG.debug("Found offset file in storage partition directory: {}", storePath);
        try {
            fileContents = new FileUtil().readWithChecksum(offsetFileRef);
            offsets = OBJECT_MAPPER.readValue(fileContents, OFFSETS_TYPE_REFERENCE);
        } catch (JsonParseException | JsonMappingException e) {
            LOG.info("Exception in json-parsing offset file {} {}, reading as string offset-value", storagePartitionDir.toPath(), offsetFileName);
            final String finalFileContents = fileContents;
            offsets = (storeSSPs.size() == 1) ? storeSSPs.stream().collect(Collectors.toMap(ssp -> ssp, offset -> finalFileContents)) : offsets;
        } catch (Exception e) {
            LOG.warn("Failed to read offset file in storage partition directory: {}", storePath, e);
        }
    } else {
        LOG.info("No offset file found in storage partition directory: {}", storePath);
    }
    return offsets;
}
Also used : HashMap(java.util.HashMap) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) File(java.io.File) FileUtil(org.apache.samza.util.FileUtil) JsonParseException(com.fasterxml.jackson.core.JsonParseException) IOException(java.io.IOException) SamzaException(org.apache.samza.SamzaException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition)

Example 10 with FileUtil

use of org.apache.samza.util.FileUtil in project samza by apache.

the class StorageManagerUtil method writeOffsetFile.

/**
 * Write the given SSP-Offset map into the offsets file.
 * @param storeDir the directory of the store
 * @param offsets The SSP-offset to write
 * @param isSideInput true if store is a side-input store, false if it is a regular store
 * @throws IOException because of deserializing to json
 */
public void writeOffsetFile(File storeDir, Map<SystemStreamPartition, String> offsets, boolean isSideInput) throws IOException {
    // First, we write the new-format offset file
    File offsetFile = new File(storeDir, OFFSET_FILE_NAME_NEW);
    String fileContents = SSP_OFFSET_OBJECT_WRITER.writeValueAsString(offsets);
    FileUtil fileUtil = new FileUtil();
    fileUtil.writeWithChecksum(offsetFile, fileContents);
    // Now we write the old format offset file, which are different for store-offset and side-inputs
    if (isSideInput) {
        offsetFile = new File(storeDir, SIDE_INPUT_OFFSET_FILE_NAME_LEGACY);
        fileContents = SSP_OFFSET_OBJECT_WRITER.writeValueAsString(offsets);
        fileUtil.writeWithChecksum(offsetFile, fileContents);
    } else {
        offsetFile = new File(storeDir, OFFSET_FILE_NAME_LEGACY);
        fileUtil.writeWithChecksum(offsetFile, offsets.entrySet().iterator().next().getValue());
    }
}
Also used : File(java.io.File) FileUtil(org.apache.samza.util.FileUtil)

Aggregations

FileUtil (org.apache.samza.util.FileUtil)17 File (java.io.File)16 Path (java.nio.file.Path)5 IOException (java.io.IOException)3 SamzaException (org.apache.samza.SamzaException)3 Before (org.junit.Before)3 FileTime (java.nio.file.attribute.FileTime)2 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 CompletionStage (java.util.concurrent.CompletionStage)2 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)2 CheckpointId (org.apache.samza.checkpoint.CheckpointId)2 MyStatefulApplication (org.apache.samza.storage.MyStatefulApplication)2 DirIndex (org.apache.samza.storage.blobstore.index.DirIndex)2 SnapshotIndex (org.apache.samza.storage.blobstore.index.SnapshotIndex)2 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)2 Test (org.junit.Test)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1