Search in sources :

Example 6 with RetryStrategy

use of io.cdap.cdap.common.service.RetryStrategy in project cdap by caskdata.

the class SystemArgumentsTest method testRetryStrategies.

@Test
public void testRetryStrategies() {
    CConfiguration cConf = CConfiguration.create();
    Map<String, String> args = Collections.emptyMap();
    // Get default, expect exponential back-off behavior, until the max delay
    RetryStrategy strategy = SystemArguments.getRetryStrategy(args, ProgramType.CUSTOM_ACTION, cConf);
    long startTime = System.currentTimeMillis();
    Assert.assertEquals(1000L, strategy.nextRetry(1, startTime));
    Assert.assertEquals(2000L, strategy.nextRetry(2, startTime));
    Assert.assertEquals(4000L, strategy.nextRetry(3, startTime));
    Assert.assertEquals(8000L, strategy.nextRetry(4, startTime));
    Assert.assertEquals(16000L, strategy.nextRetry(5, startTime));
    Assert.assertEquals(30000L, strategy.nextRetry(6, startTime));
    Assert.assertEquals(30000L, strategy.nextRetry(7, startTime));
    // It should give up (returning -1) when exceeding the max retries
    Assert.assertEquals(-1L, strategy.nextRetry(1001, startTime));
    // Override the strategy type and max retry time
    args = ImmutableMap.of("system." + Constants.Retry.TYPE, RetryStrategyType.FIXED_DELAY.toString(), "system." + Constants.Retry.MAX_TIME_SECS, "5");
    strategy = SystemArguments.getRetryStrategy(args, ProgramType.CUSTOM_ACTION, cConf);
    startTime = System.currentTimeMillis();
    // Expects the delay doesn't change
    Assert.assertEquals(1000L, strategy.nextRetry(1, startTime));
    Assert.assertEquals(1000L, strategy.nextRetry(2, startTime));
    Assert.assertEquals(1000L, strategy.nextRetry(3, startTime));
    Assert.assertEquals(1000L, strategy.nextRetry(4, startTime));
    // Should give up (returning -1) after passing the max retry time
    Assert.assertEquals(-1L, strategy.nextRetry(1, startTime - 6000));
}
Also used : CConfiguration(io.cdap.cdap.common.conf.CConfiguration) RetryStrategy(io.cdap.cdap.common.service.RetryStrategy) Test(org.junit.Test)

Example 7 with RetryStrategy

use of io.cdap.cdap.common.service.RetryStrategy in project cdap by caskdata.

the class MessagingUsageWriter method doRegisterAll.

private void doRegisterAll(Iterable<? extends EntityId> users, EntityId entityId) throws Exception {
    // Only record usage from program
    StoreRequest request = StoreRequestBuilder.of(topic).addPayloads(StreamSupport.stream(users.spliterator(), false).filter(ProgramId.class::isInstance).map(ProgramId.class::cast).map(id -> new MetadataMessage(MetadataMessage.Type.USAGE, id, GSON.toJsonTree(new DatasetUsage(entityId)))).map(GSON::toJson).map(s -> s.getBytes(StandardCharsets.UTF_8)).iterator()).build();
    Retries.callWithRetries(() -> messagingService.publish(request), retryStrategy, Retries.ALWAYS_TRUE);
}
Also used : Retries(io.cdap.cdap.common.service.Retries) RetryStrategy(io.cdap.cdap.common.service.RetryStrategy) Iterables(com.google.common.collect.Iterables) StoreRequestBuilder(io.cdap.cdap.messaging.client.StoreRequestBuilder) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Inject(com.google.inject.Inject) MessagingService(io.cdap.cdap.messaging.MessagingService) ProgramId(io.cdap.cdap.proto.id.ProgramId) RetryStrategies(io.cdap.cdap.common.service.RetryStrategies) StoreRequest(io.cdap.cdap.messaging.StoreRequest) EntityId(io.cdap.cdap.proto.id.EntityId) TopicId(io.cdap.cdap.proto.id.TopicId) StandardCharsets(java.nio.charset.StandardCharsets) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) DatasetId(io.cdap.cdap.proto.id.DatasetId) Gson(com.google.gson.Gson) Constants(io.cdap.cdap.common.conf.Constants) StreamSupport(java.util.stream.StreamSupport) MetadataMessage(io.cdap.cdap.data2.metadata.writer.MetadataMessage) StoreRequest(io.cdap.cdap.messaging.StoreRequest) MetadataMessage(io.cdap.cdap.data2.metadata.writer.MetadataMessage) ProgramId(io.cdap.cdap.proto.id.ProgramId)

Example 8 with RetryStrategy

use of io.cdap.cdap.common.service.RetryStrategy in project cdap by cdapio.

the class SystemArgumentsTest method testRetryStrategies.

@Test
public void testRetryStrategies() {
    CConfiguration cConf = CConfiguration.create();
    Map<String, String> args = Collections.emptyMap();
    // Get default, expect exponential back-off behavior, until the max delay
    RetryStrategy strategy = SystemArguments.getRetryStrategy(args, ProgramType.CUSTOM_ACTION, cConf);
    long startTime = System.currentTimeMillis();
    Assert.assertEquals(1000L, strategy.nextRetry(1, startTime));
    Assert.assertEquals(2000L, strategy.nextRetry(2, startTime));
    Assert.assertEquals(4000L, strategy.nextRetry(3, startTime));
    Assert.assertEquals(8000L, strategy.nextRetry(4, startTime));
    Assert.assertEquals(16000L, strategy.nextRetry(5, startTime));
    Assert.assertEquals(30000L, strategy.nextRetry(6, startTime));
    Assert.assertEquals(30000L, strategy.nextRetry(7, startTime));
    // It should give up (returning -1) when exceeding the max retries
    Assert.assertEquals(-1L, strategy.nextRetry(1001, startTime));
    // Override the strategy type and max retry time
    args = ImmutableMap.of("system." + Constants.Retry.TYPE, RetryStrategyType.FIXED_DELAY.toString(), "system." + Constants.Retry.MAX_TIME_SECS, "5");
    strategy = SystemArguments.getRetryStrategy(args, ProgramType.CUSTOM_ACTION, cConf);
    startTime = System.currentTimeMillis();
    // Expects the delay doesn't change
    Assert.assertEquals(1000L, strategy.nextRetry(1, startTime));
    Assert.assertEquals(1000L, strategy.nextRetry(2, startTime));
    Assert.assertEquals(1000L, strategy.nextRetry(3, startTime));
    Assert.assertEquals(1000L, strategy.nextRetry(4, startTime));
    // Should give up (returning -1) after passing the max retry time
    Assert.assertEquals(-1L, strategy.nextRetry(1, startTime - 6000));
}
Also used : CConfiguration(io.cdap.cdap.common.conf.CConfiguration) RetryStrategy(io.cdap.cdap.common.service.RetryStrategy) Test(org.junit.Test)

Example 9 with RetryStrategy

use of io.cdap.cdap.common.service.RetryStrategy in project cdap by cdapio.

the class RuntimeServiceMainTest method createProgramStateWriter.

/**
 * Creates a {@link ProgramStateWriter} that writes to {@link RuntimeClient} directly.
 *
 * @param injector the injector for creating the {@link RuntimeClient}
 * @param programRunId the {@link ProgramRunId} for the program state change
 * @return a {@link ProgramStateWriter}
 */
private ProgramStateWriter createProgramStateWriter(Injector injector, ProgramRunId programRunId) {
    RuntimeClient runtimeClient = injector.getInstance(RuntimeClient.class);
    // We write to the record event directly to skip the app-fabric to process it
    // This is because we don't follow the normal event flow here for testing
    TopicId topicId = NamespaceId.SYSTEM.topic(injector.getInstance(CConfiguration.class).get(Constants.AppFabric.PROGRAM_STATUS_RECORD_EVENT_TOPIC));
    RetryStrategy retryStrategy = RetryStrategies.timeLimit(5, TimeUnit.SECONDS, RetryStrategies.fixDelay(200, TimeUnit.MILLISECONDS));
    return new MessagingProgramStateWriter((notificationType, properties) -> {
        Notification notification = new Notification(notificationType, properties);
        try {
            Retries.callWithRetries((Retries.Callable<Void, Exception>) () -> {
                runtimeClient.sendMessages(programRunId, topicId, Collections.singleton(createMessage(notification)).iterator());
                return null;
            }, retryStrategy, t -> t instanceof IOException || t instanceof RetryableException);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : RuntimeClient(io.cdap.cdap.internal.app.runtime.monitor.RuntimeClient) RetryableException(io.cdap.cdap.api.retry.RetryableException) Retries(io.cdap.cdap.common.service.Retries) TopicId(io.cdap.cdap.proto.id.TopicId) IOException(java.io.IOException) MessagingProgramStateWriter(io.cdap.cdap.internal.app.program.MessagingProgramStateWriter) RetryStrategy(io.cdap.cdap.common.service.RetryStrategy) Notification(io.cdap.cdap.proto.Notification) RetryableException(io.cdap.cdap.api.retry.RetryableException) IOException(java.io.IOException)

Example 10 with RetryStrategy

use of io.cdap.cdap.common.service.RetryStrategy in project cdap by cdapio.

the class DistributedLogFramework method createService.

@Override
@SuppressWarnings("unchecked")
protected Service createService(Set<Integer> partitions) {
    Map<String, LogPipelineSpecification<AppenderContext>> specs = new LogPipelineLoader(cConf).load(contextProvider);
    int pipelineCount = specs.size();
    // Create one KafkaLogProcessorPipeline per spec
    final List<Service> pipelines = new ArrayList<>();
    for (final LogPipelineSpecification<AppenderContext> pipelineSpec : specs.values()) {
        final CConfiguration cConf = pipelineSpec.getConf();
        final AppenderContext context = pipelineSpec.getContext();
        long bufferSize = getBufferSize(pipelineCount, cConf, partitions.size());
        final String topic = cConf.get(Constants.Logging.KAFKA_TOPIC);
        final KafkaPipelineConfig config = new KafkaPipelineConfig(topic, partitions, bufferSize, cConf.getLong(Constants.Logging.PIPELINE_EVENT_DELAY_MS), cConf.getInt(Constants.Logging.PIPELINE_KAFKA_FETCH_SIZE), cConf.getLong(Constants.Logging.PIPELINE_CHECKPOINT_INTERVAL_MS));
        RetryStrategy retryStrategy = RetryStrategies.fromConfiguration(cConf, "system.log.process.");
        pipelines.add(new RetryOnStartFailureService(() -> new KafkaLogProcessorPipeline(new LogProcessorPipelineContext(cConf, context.getName(), context, context.getMetricsContext(), context.getInstanceId()), checkpointManagerFactory.create(pipelineSpec.getCheckpointPrefix() + topic, CheckpointManagerFactory.Type.KAFKA), brokerService, config), retryStrategy));
    }
    // Returns a Service that start/stop all pipelines.
    return new AbstractIdleService() {

        @Override
        protected void startUp() throws Exception {
            // Starts all pipeline
            validateAllFutures(Iterables.transform(pipelines, Service::start));
        }

        @Override
        protected void shutDown() throws Exception {
            // Stops all pipeline
            validateAllFutures(Iterables.transform(pipelines, Service::stop));
        }
    };
}
Also used : LogPipelineSpecification(io.cdap.cdap.logging.framework.LogPipelineSpecification) ArrayList(java.util.ArrayList) KafkaPipelineConfig(io.cdap.cdap.logging.pipeline.kafka.KafkaPipelineConfig) RetryOnStartFailureService(io.cdap.cdap.common.service.RetryOnStartFailureService) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) DiscoveryService(org.apache.twill.discovery.DiscoveryService) ResourceBalancerService(io.cdap.cdap.common.resource.ResourceBalancerService) Service(com.google.common.util.concurrent.Service) BrokerService(org.apache.twill.kafka.client.BrokerService) LogPipelineLoader(io.cdap.cdap.logging.framework.LogPipelineLoader) LogProcessorPipelineContext(io.cdap.cdap.logging.pipeline.LogProcessorPipelineContext) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) KafkaLogProcessorPipeline(io.cdap.cdap.logging.pipeline.kafka.KafkaLogProcessorPipeline) AppenderContext(io.cdap.cdap.api.logging.AppenderContext) RetryOnStartFailureService(io.cdap.cdap.common.service.RetryOnStartFailureService) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) RetryStrategy(io.cdap.cdap.common.service.RetryStrategy)

Aggregations

RetryStrategy (io.cdap.cdap.common.service.RetryStrategy)26 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)18 ProgramId (io.cdap.cdap.proto.id.ProgramId)10 IOException (java.io.IOException)10 Retries (io.cdap.cdap.common.service.Retries)8 Gson (com.google.gson.Gson)6 Inject (com.google.inject.Inject)6 RetryableException (io.cdap.cdap.api.retry.RetryableException)6 Constants (io.cdap.cdap.common.conf.Constants)6 RetryStrategies (io.cdap.cdap.common.service.RetryStrategies)6 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)6 StandardCharsets (java.nio.charset.StandardCharsets)6 Service (com.google.common.util.concurrent.Service)4 ProgramOptions (io.cdap.cdap.app.runtime.ProgramOptions)4 ProgramStateWriter (io.cdap.cdap.app.runtime.ProgramStateWriter)4 ClientConfig (io.cdap.cdap.client.config.ClientConfig)4 ConnectionConfig (io.cdap.cdap.client.config.ConnectionConfig)4 NotFoundException (io.cdap.cdap.common.NotFoundException)4 BasicArguments (io.cdap.cdap.internal.app.runtime.BasicArguments)4 ProgramOptionConstants (io.cdap.cdap.internal.app.runtime.ProgramOptionConstants)4