Search in sources :

Example 1 with NameUtils.getMetadataSegmentName

use of io.pravega.shared.NameUtils.getMetadataSegmentName in project pravega by pravega.

the class ContainerRecoveryUtils method updateCoreAttributes.

/**
 * Updates Core Attributes for all Segments for the given Containers.
 * This method iterates through all the back copies of Container Metadata Segments, interprets all entries as
 * Segment-SegmentInfo mappings and extracts the Core Attributes for each. These Core Attributes are then applied
 * to the same segments in the given Containers.
 * @param backUpMetadataSegments    A map of back copies of metadata segments along with their container Ids.
 * @param containersMap             A map of {@link DebugStreamSegmentContainer} instances with their container Ids.
 * @param executorService           A thread pool for execution.
 * @param timeout                   Timeout for the operation.
 * @throws InterruptedException     If the operation was interrupted while waiting.
 * @throws TimeoutException         If the timeout expired prior to being able to complete update attributes for all segments.
 * @throws ExecutionException       When execution of update attributes to all segments encountered an error.
 */
public static void updateCoreAttributes(Map<Integer, String> backUpMetadataSegments, Map<Integer, DebugStreamSegmentContainer> containersMap, ExecutorService executorService, Duration timeout) throws InterruptedException, ExecutionException, TimeoutException {
    Preconditions.checkState(backUpMetadataSegments.size() == containersMap.size(), "The number of " + "back-up metadata segments = %s and the number of containers = %s should match.", backUpMetadataSegments.size(), containersMap.size());
    val args = IteratorArgs.builder().fetchTimeout(timeout).build();
    SegmentToContainerMapper segToConMapper = new SegmentToContainerMapper(containersMap.size(), true);
    // Iterate through all back up metadata segments
    for (val backUpMetadataSegmentEntry : backUpMetadataSegments.entrySet()) {
        // Get the name of original metadata segment
        val metadataSegment = NameUtils.getMetadataSegmentName(backUpMetadataSegmentEntry.getKey());
        // Get the name of back up metadata segment
        val backUpMetadataSegment = backUpMetadataSegmentEntry.getValue();
        // Get the container for back up metadata segment
        val containerForBackUpMetadataSegment = containersMap.get(segToConMapper.getContainerId(backUpMetadataSegment));
        log.info("Back up container metadata segment name: {} and its container id: {}", backUpMetadataSegment, containerForBackUpMetadataSegment.getId());
        // Get the container for segments inside back up metadata segment
        val container = containersMap.get(backUpMetadataSegmentEntry.getKey());
        // Make sure the backup segment is registered as a table segment.
        val bmsInfo = containerForBackUpMetadataSegment.getStreamSegmentInfo(backUpMetadataSegment, timeout).get(timeout.toMillis(), TimeUnit.MILLISECONDS);
        if (bmsInfo.getAttributes().getOrDefault(TableAttributes.INDEX_OFFSET, Attributes.NULL_ATTRIBUTE_VALUE) == Attributes.NULL_ATTRIBUTE_VALUE) {
            log.info("Back up container metadata segment name: {} does not have INDEX_OFFSET set; setting to 0 (forcing reindexing).", backUpMetadataSegment);
            containerForBackUpMetadataSegment.forSegment(backUpMetadataSegment, timeout).thenCompose(s -> s.updateAttributes(AttributeUpdateCollection.from(new AttributeUpdate(TableAttributes.INDEX_OFFSET, AttributeUpdateType.Replace, 0)), timeout)).get(timeout.toMillis(), TimeUnit.MILLISECONDS);
            refreshDerivedProperties(backUpMetadataSegment, containerForBackUpMetadataSegment);
        }
        // Get the iterator to iterate through all segments in the back up metadata segment
        val tableExtension = containerForBackUpMetadataSegment.getExtension(ContainerTableExtension.class);
        val entryIterator = tableExtension.entryIterator(backUpMetadataSegment, args).get(timeout.toMillis(), TimeUnit.MILLISECONDS);
        val futures = new ArrayList<CompletableFuture<Void>>();
        // Iterating through all segments in the back up metadata segment
        entryIterator.forEachRemaining(item -> {
            for (val entry : item.getEntries()) {
                val segmentInfo = MetadataStore.SegmentInfo.deserialize(entry.getValue());
                val properties = segmentInfo.getProperties();
                // skip, if this is original metadata segment
                if (properties.getName().equals(metadataSegment)) {
                    continue;
                }
                // Get the attributes for the current segment
                val attributeUpdates = properties.getAttributes().entrySet().stream().map(e -> new AttributeUpdate(e.getKey(), AttributeUpdateType.Replace, e.getValue())).collect(Collectors.toCollection(AttributeUpdateCollection::new));
                log.info("Segment Name: {} Attributes Updates: {}", properties.getName(), attributeUpdates);
                // Update attributes for the current segment
                futures.add(Futures.exceptionallyExpecting(container.updateAttributes(properties.getName(), attributeUpdates, timeout).thenRun(() -> refreshDerivedProperties(properties.getName(), container)), ex -> ex instanceof StreamSegmentNotExistsException, null));
            }
        }, executorService).get(timeout.toMillis(), TimeUnit.MILLISECONDS);
        // Waiting for update attributes for all segments in each back up metadata segment.
        Futures.allOf(futures).get(timeout.toMillis(), TimeUnit.MILLISECONDS);
    }
}
Also used : lombok.val(lombok.val) TableAttributes(io.pravega.segmentstore.contracts.tables.TableAttributes) Storage(io.pravega.segmentstore.storage.Storage) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException) Date(java.util.Date) Exceptions(io.pravega.common.Exceptions) SimpleDateFormat(java.text.SimpleDateFormat) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) IteratorArgs(io.pravega.segmentstore.contracts.tables.IteratorArgs) ArrayList(java.util.ArrayList) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) HashSet(java.util.HashSet) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) ExecutorService(java.util.concurrent.ExecutorService) NameUtils(io.pravega.shared.NameUtils) Attributes(io.pravega.segmentstore.contracts.Attributes) Iterator(java.util.Iterator) lombok.val(lombok.val) Set(java.util.Set) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) AttributeUpdateCollection(io.pravega.segmentstore.contracts.AttributeUpdateCollection) ContainerTableExtension(io.pravega.segmentstore.server.tables.ContainerTableExtension) SegmentToContainerMapper(io.pravega.shared.segment.SegmentToContainerMapper) Preconditions(com.google.common.base.Preconditions) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) Futures(io.pravega.common.concurrent.Futures) NameUtils.getMetadataSegmentName(io.pravega.shared.NameUtils.getMetadataSegmentName) AttributeUpdateCollection(io.pravega.segmentstore.contracts.AttributeUpdateCollection) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) SegmentToContainerMapper(io.pravega.shared.segment.SegmentToContainerMapper) ArrayList(java.util.ArrayList) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)

Example 2 with NameUtils.getMetadataSegmentName

use of io.pravega.shared.NameUtils.getMetadataSegmentName in project pravega by pravega.

the class ContainerRecoveryUtils method createBackUpMetadataSegments.

/**
 * This method creates a back up segment of container metadata segment and its attribute segment for each
 * container Id. The original metadata segments and its attribute segments are deleted and the back up copy
 * of original metadata segments are stored in a map and returned.
 *
 * @param storage                   A {@link Storage} instance to get the segments from.
 * @param containerCount            The number of containers for which renaming of container metadata segment and its
 *                                  attributes segment has to be performed.
 * @param executorService           A thread pool for execution.
 * @param timeout                   Timeout for the operation.
 * @return                          A CompletableFuture that, when completed normally, will have a Map of Container
 * Ids to new container metadata segment names.
 */
public static synchronized CompletableFuture<Map<Integer, String>> createBackUpMetadataSegments(Storage storage, int containerCount, ExecutorService executorService, Duration timeout) {
    String fileSuffix = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    Map<Integer, String> backUpMetadataSegments = new HashMap<>();
    val futures = new ArrayList<CompletableFuture<Void>>();
    for (int containerId = 0; containerId < containerCount; containerId++) {
        String backUpMetadataSegment = NameUtils.getMetadataSegmentName(containerId) + fileSuffix;
        String backUpAttributeSegment = NameUtils.getAttributeSegmentName(backUpMetadataSegment);
        log.debug("Created '{}' as a back of metadata segment of container Id '{}'", backUpMetadataSegment, containerId);
        val finalContainerId = containerId;
        futures.add(Futures.exceptionallyExpecting(ContainerRecoveryUtils.backUpMetadataAndAttributeSegments(storage, containerId, backUpMetadataSegment, backUpAttributeSegment, executorService, timeout).thenCompose(x -> ContainerRecoveryUtils.deleteMetadataAndAttributeSegments(storage, finalContainerId, timeout)), ex -> Exceptions.unwrap(ex) instanceof StreamSegmentNotExistsException, null));
        backUpMetadataSegments.put(finalContainerId, backUpMetadataSegment);
    }
    return Futures.allOf(futures).thenApply(v -> backUpMetadataSegments);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) lombok.val(lombok.val) TableAttributes(io.pravega.segmentstore.contracts.tables.TableAttributes) Storage(io.pravega.segmentstore.storage.Storage) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException) Date(java.util.Date) Exceptions(io.pravega.common.Exceptions) SimpleDateFormat(java.text.SimpleDateFormat) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) IteratorArgs(io.pravega.segmentstore.contracts.tables.IteratorArgs) ArrayList(java.util.ArrayList) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) HashSet(java.util.HashSet) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) ExecutorService(java.util.concurrent.ExecutorService) NameUtils(io.pravega.shared.NameUtils) Attributes(io.pravega.segmentstore.contracts.Attributes) Iterator(java.util.Iterator) lombok.val(lombok.val) Set(java.util.Set) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) AttributeUpdateCollection(io.pravega.segmentstore.contracts.AttributeUpdateCollection) ContainerTableExtension(io.pravega.segmentstore.server.tables.ContainerTableExtension) SegmentToContainerMapper(io.pravega.shared.segment.SegmentToContainerMapper) Preconditions(com.google.common.base.Preconditions) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) Futures(io.pravega.common.concurrent.Futures) NameUtils.getMetadataSegmentName(io.pravega.shared.NameUtils.getMetadataSegmentName) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)

Aggregations

Preconditions (com.google.common.base.Preconditions)2 Exceptions (io.pravega.common.Exceptions)2 Futures (io.pravega.common.concurrent.Futures)2 AttributeUpdate (io.pravega.segmentstore.contracts.AttributeUpdate)2 AttributeUpdateCollection (io.pravega.segmentstore.contracts.AttributeUpdateCollection)2 AttributeUpdateType (io.pravega.segmentstore.contracts.AttributeUpdateType)2 Attributes (io.pravega.segmentstore.contracts.Attributes)2 SegmentProperties (io.pravega.segmentstore.contracts.SegmentProperties)2 StreamSegmentNotExistsException (io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)2 IteratorArgs (io.pravega.segmentstore.contracts.tables.IteratorArgs)2 TableAttributes (io.pravega.segmentstore.contracts.tables.TableAttributes)2 ContainerTableExtension (io.pravega.segmentstore.server.tables.ContainerTableExtension)2 Storage (io.pravega.segmentstore.storage.Storage)2 NameUtils (io.pravega.shared.NameUtils)2 NameUtils.getMetadataSegmentName (io.pravega.shared.NameUtils.getMetadataSegmentName)2 SegmentToContainerMapper (io.pravega.shared.segment.SegmentToContainerMapper)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2