Search in sources :

Example 6 with ExponentialSleepStrategy

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

the class TestLocalityServlet method testReadProcessorLocalityWithNoLocality.

@Test
public void testReadProcessorLocalityWithNoLocality() throws Exception {
    final ProcessorLocality expectedProcessorLocality = new ProcessorLocality(PROCESSOR_ID2, "");
    URL url = new URL(webApp.getUrl().toString() + "locality?processorId=" + PROCESSOR_ID2);
    when(localityManager.readLocality()).thenReturn(new LocalityModel(ImmutableMap.of()));
    String response = HttpUtil.read(url, 1000, new ExponentialSleepStrategy());
    ProcessorLocality processorLocality = mapper.readValue(response, ProcessorLocality.class);
    assertEquals("Expected empty response for processor locality " + PROCESSOR_ID2 + " but got " + processorLocality, processorLocality, expectedProcessorLocality);
}
Also used : ProcessorLocality(org.apache.samza.job.model.ProcessorLocality) ExponentialSleepStrategy(org.apache.samza.util.ExponentialSleepStrategy) LocalityModel(org.apache.samza.job.model.LocalityModel) URL(java.net.URL) Test(org.junit.Test)

Example 7 with ExponentialSleepStrategy

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

the class KafkaSystemAdmin method getSSPMetadata.

/**
 * Given a set of SystemStreamPartition, fetch metadata from Kafka for each
 * of them, and return a map from ssp to SystemStreamPartitionMetadata for
 * each of them. This method will return null for oldest and newest offsets
 * if a given SystemStreamPartition is empty. This method will block and
 * retry indefinitely until it gets a successful response from Kafka.
 * @param ssps a set of strings of SSP
 * @param retryBackoff retry backoff strategy
 * @return a map from ssp to sspMetadata which has offsets
 */
Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata> getSSPMetadata(Set<SystemStreamPartition> ssps, ExponentialSleepStrategy retryBackoff) {
    LOG.info("Fetching SSP metadata for: {}", ssps);
    List<TopicPartition> topicPartitions = ssps.stream().map(ssp -> new TopicPartition(ssp.getStream(), ssp.getPartition().getPartitionId())).collect(Collectors.toList());
    Function1<ExponentialSleepStrategy.RetryLoop, Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata>> fetchTopicPartitionMetadataOperation = new AbstractFunction1<ExponentialSleepStrategy.RetryLoop, Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata>>() {

        @Override
        public Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata> apply(ExponentialSleepStrategy.RetryLoop loop) {
            OffsetsMaps topicPartitionsMetadata = fetchTopicPartitionsMetadata(topicPartitions);
            Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata> sspToSSPMetadata = new HashMap<>();
            for (SystemStreamPartition ssp : ssps) {
                String oldestOffset = topicPartitionsMetadata.getOldestOffsets().get(ssp);
                String newestOffset = topicPartitionsMetadata.getNewestOffsets().get(ssp);
                String upcomingOffset = topicPartitionsMetadata.getUpcomingOffsets().get(ssp);
                sspToSSPMetadata.put(ssp, new SystemStreamMetadata.SystemStreamPartitionMetadata(oldestOffset, newestOffset, upcomingOffset));
            }
            loop.done();
            return sspToSSPMetadata;
        }
    };
    Function2<Exception, ExponentialSleepStrategy.RetryLoop, BoxedUnit> onExceptionRetryOperation = new AbstractFunction2<Exception, ExponentialSleepStrategy.RetryLoop, BoxedUnit>() {

        @Override
        public BoxedUnit apply(Exception exception, ExponentialSleepStrategy.RetryLoop loop) {
            if (loop.sleepCount() < MAX_RETRIES_ON_EXCEPTION) {
                LOG.warn(String.format("Fetching SSP metadata for: %s threw an exception. Retrying.", ssps), exception);
            } else {
                LOG.error(String.format("Fetching SSP metadata for: %s threw an exception.", ssps), exception);
                loop.done();
                throw new SamzaException(exception);
            }
            return null;
        }
    };
    Function0<Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata>> fallbackOperation = new AbstractFunction0<Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata>>() {

        @Override
        public Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata> apply() {
            throw new SamzaException("Failed to get SSP metadata");
        }
    };
    return retryBackoff.run(fetchTopicPartitionMetadataOperation, onExceptionRetryOperation).getOrElse(fallbackOperation);
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) StartpointTimestamp(org.apache.samza.startpoint.StartpointTimestamp) Startpoint(org.apache.samza.startpoint.Startpoint) StringUtils(org.apache.commons.lang3.StringUtils) AdminClient(org.apache.kafka.clients.admin.AdminClient) StartpointSpecific(org.apache.samza.startpoint.StartpointSpecific) KafkaConfig(org.apache.samza.config.KafkaConfig) Map(java.util.Map) DeleteTopicsResult(org.apache.kafka.clients.admin.DeleteTopicsResult) MapConfig(org.apache.samza.config.MapConfig) TopicConfig(org.apache.kafka.common.config.TopicConfig) Consumer(org.apache.kafka.clients.consumer.Consumer) TopicPartition(org.apache.kafka.common.TopicPartition) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) PartitionInfo(org.apache.kafka.common.PartitionInfo) OffsetAndTimestamp(org.apache.kafka.clients.consumer.OffsetAndTimestamp) Collectors(java.util.stream.Collectors) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) List(java.util.List) StartpointUpcoming(org.apache.samza.startpoint.StartpointUpcoming) AbstractFunction0(scala.runtime.AbstractFunction0) AbstractFunction1(scala.runtime.AbstractFunction1) Optional(java.util.Optional) AbstractFunction2(scala.runtime.AbstractFunction2) Config(org.apache.samza.config.Config) NotImplementedException(org.apache.commons.lang3.NotImplementedException) StartpointOldest(org.apache.samza.startpoint.StartpointOldest) Function0(scala.Function0) StreamValidationException(org.apache.samza.system.StreamValidationException) Function1(scala.Function1) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Function2(scala.Function2) HashMap(java.util.HashMap) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) SystemStreamMetadata(org.apache.samza.system.SystemStreamMetadata) Function(java.util.function.Function) StreamConfig(org.apache.samza.config.StreamConfig) RecordsToDelete(org.apache.kafka.clients.admin.RecordsToDelete) HashSet(java.util.HashSet) StartpointVisitor(org.apache.samza.startpoint.StartpointVisitor) DescribeTopicsResult(org.apache.kafka.clients.admin.DescribeTopicsResult) SystemStream(org.apache.samza.system.SystemStream) CreateTopicsResult(org.apache.kafka.clients.admin.CreateTopicsResult) ApplicationConfig(org.apache.samza.config.ApplicationConfig) SystemConfig(org.apache.samza.config.SystemConfig) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) KafkaUtil(org.apache.samza.util.KafkaUtil) Logger(org.slf4j.Logger) Properties(java.util.Properties) ExponentialSleepStrategy(org.apache.samza.util.ExponentialSleepStrategy) NewTopic(org.apache.kafka.clients.admin.NewTopic) Partition(org.apache.samza.Partition) StreamSpec(org.apache.samza.system.StreamSpec) BoxedUnit(scala.runtime.BoxedUnit) SamzaException(org.apache.samza.SamzaException) TimeUnit(java.util.concurrent.TimeUnit) SystemAdmin(org.apache.samza.system.SystemAdmin) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) AbstractFunction2(scala.runtime.AbstractFunction2) HashMap(java.util.HashMap) ExponentialSleepStrategy(org.apache.samza.util.ExponentialSleepStrategy) SystemStreamMetadata(org.apache.samza.system.SystemStreamMetadata) AbstractFunction0(scala.runtime.AbstractFunction0) AbstractFunction1(scala.runtime.AbstractFunction1) SamzaException(org.apache.samza.SamzaException) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) StreamValidationException(org.apache.samza.system.StreamValidationException) SamzaException(org.apache.samza.SamzaException) TopicPartition(org.apache.kafka.common.TopicPartition) BoxedUnit(scala.runtime.BoxedUnit) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition)

Example 8 with ExponentialSleepStrategy

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

the class TestKafkaSystemAdminWithMock method testGetSSPMetadataWithRetry.

@Test
public void testGetSSPMetadataWithRetry() {
    SystemStreamPartition oneSSP = new SystemStreamPartition(TEST_SYSTEM, VALID_TOPIC, new Partition(0));
    SystemStreamPartition otherSSP = new SystemStreamPartition(TEST_SYSTEM, "otherTopic", new Partition(1));
    ImmutableSet<SystemStreamPartition> ssps = ImmutableSet.of(oneSSP, otherSSP);
    List<TopicPartition> topicPartitions = ssps.stream().map(ssp -> new TopicPartition(ssp.getStream(), ssp.getPartition().getPartitionId())).collect(Collectors.toList());
    Map<TopicPartition, Long> testBeginningOffsets = ImmutableMap.of(testTopicPartition0, KAFKA_BEGINNING_OFFSET_FOR_PARTITION0, testTopicPartition1, KAFKA_BEGINNING_OFFSET_FOR_PARTITION1);
    when(mockKafkaConsumer.beginningOffsets(topicPartitions)).thenThrow(new RuntimeException()).thenReturn(testBeginningOffsets);
    Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata> sspMetadata = kafkaSystemAdmin.getSSPMetadata(ssps, new ExponentialSleepStrategy(2, 1, 1));
    assertEquals("metadata should return for 2 topics", sspMetadata.size(), 2);
    // retried twice because the first fails and the second succeeds
    Mockito.verify(mockKafkaConsumer, Mockito.times(2)).beginningOffsets(topicPartitions);
}
Also used : Arrays(java.util.Arrays) JobConfig(org.apache.samza.config.JobConfig) HashMap(java.util.HashMap) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) SystemStreamMetadata(org.apache.samza.system.SystemStreamMetadata) ImmutableList(com.google.common.collect.ImmutableList) KafkaConfig(org.apache.samza.config.KafkaConfig) Map(java.util.Map) After(org.junit.After) MapConfig(org.apache.samza.config.MapConfig) Before(org.junit.Before) Consumer(org.apache.kafka.clients.consumer.Consumer) TopicPartition(org.apache.kafka.common.TopicPartition) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) ExponentialSleepStrategy(org.apache.samza.util.ExponentialSleepStrategy) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Partition(org.apache.samza.Partition) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Test(org.junit.Test) PartitionInfo(org.apache.kafka.common.PartitionInfo) Collectors(java.util.stream.Collectors) SamzaException(org.apache.samza.SamzaException) Mockito(org.mockito.Mockito) List(java.util.List) Config(org.apache.samza.config.Config) Assert(org.junit.Assert) Collections(java.util.Collections) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) TopicPartition(org.apache.kafka.common.TopicPartition) Partition(org.apache.samza.Partition) TopicPartition(org.apache.kafka.common.TopicPartition) ExponentialSleepStrategy(org.apache.samza.util.ExponentialSleepStrategy) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Test(org.junit.Test)

Example 9 with ExponentialSleepStrategy

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

the class TestKafkaSystemAdminWithMock method testGetSSPMetadataShouldTerminateAfterFiniteRetriesOnException.

@Test(expected = SamzaException.class)
public void testGetSSPMetadataShouldTerminateAfterFiniteRetriesOnException() throws Exception {
    SystemStreamPartition oneSSP = new SystemStreamPartition(TEST_SYSTEM, VALID_TOPIC, new Partition(0));
    SystemStreamPartition otherSSP = new SystemStreamPartition(TEST_SYSTEM, "otherTopic", new Partition(1));
    ImmutableSet<SystemStreamPartition> ssps = ImmutableSet.of(oneSSP, otherSSP);
    List<TopicPartition> topicPartitions = ssps.stream().map(ssp -> new TopicPartition(ssp.getStream(), ssp.getPartition().getPartitionId())).collect(Collectors.toList());
    when(mockKafkaConsumer.beginningOffsets(topicPartitions)).thenThrow(new RuntimeException()).thenThrow(new RuntimeException());
    kafkaSystemAdmin.getSSPMetadata(ssps, new ExponentialSleepStrategy(2, 1, 1));
}
Also used : Arrays(java.util.Arrays) JobConfig(org.apache.samza.config.JobConfig) HashMap(java.util.HashMap) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) SystemStreamMetadata(org.apache.samza.system.SystemStreamMetadata) ImmutableList(com.google.common.collect.ImmutableList) KafkaConfig(org.apache.samza.config.KafkaConfig) Map(java.util.Map) After(org.junit.After) MapConfig(org.apache.samza.config.MapConfig) Before(org.junit.Before) Consumer(org.apache.kafka.clients.consumer.Consumer) TopicPartition(org.apache.kafka.common.TopicPartition) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) ExponentialSleepStrategy(org.apache.samza.util.ExponentialSleepStrategy) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Partition(org.apache.samza.Partition) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Test(org.junit.Test) PartitionInfo(org.apache.kafka.common.PartitionInfo) Collectors(java.util.stream.Collectors) SamzaException(org.apache.samza.SamzaException) Mockito(org.mockito.Mockito) List(java.util.List) Config(org.apache.samza.config.Config) Assert(org.junit.Assert) Collections(java.util.Collections) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) TopicPartition(org.apache.kafka.common.TopicPartition) Partition(org.apache.samza.Partition) TopicPartition(org.apache.kafka.common.TopicPartition) ExponentialSleepStrategy(org.apache.samza.util.ExponentialSleepStrategy) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Test(org.junit.Test)

Example 10 with ExponentialSleepStrategy

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

the class TestLocalityServlet method testReadContainerLocalityWithNoLocality.

@Test
public void testReadContainerLocalityWithNoLocality() throws Exception {
    final LocalityModel expectedLocality = new LocalityModel(Collections.emptyMap());
    URL url = new URL(webApp.getUrl().toString() + "locality");
    when(localityManager.readLocality()).thenReturn(new LocalityModel(ImmutableMap.of()));
    String response = HttpUtil.read(url, 1000, new ExponentialSleepStrategy());
    LocalityModel locality = mapper.readValue(response, LocalityModel.class);
    assertEquals("Expected empty response but got " + locality, locality, expectedLocality);
}
Also used : ExponentialSleepStrategy(org.apache.samza.util.ExponentialSleepStrategy) LocalityModel(org.apache.samza.job.model.LocalityModel) URL(java.net.URL) Test(org.junit.Test)

Aggregations

ExponentialSleepStrategy (org.apache.samza.util.ExponentialSleepStrategy)11 Test (org.junit.Test)9 URL (java.net.URL)6 HashMap (java.util.HashMap)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 Map (java.util.Map)4 PartitionInfo (org.apache.kafka.common.PartitionInfo)4 TopicPartition (org.apache.kafka.common.TopicPartition)4 Partition (org.apache.samza.Partition)4 SamzaException (org.apache.samza.SamzaException)4 SystemStreamMetadata (org.apache.samza.system.SystemStreamMetadata)4 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)4 ImmutableSet (com.google.common.collect.ImmutableSet)3 Collections (java.util.Collections)3 List (java.util.List)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 Consumer (org.apache.kafka.clients.consumer.Consumer)3 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)3 Config (org.apache.samza.config.Config)3