Search in sources :

Example 1 with RetryableException

use of org.apache.druid.java.util.RetryableException in project druid by druid-io.

the class KillCompactionConfig method run.

@Override
public DruidCoordinatorRuntimeParams run(DruidCoordinatorRuntimeParams params) {
    long currentTimeMillis = System.currentTimeMillis();
    if ((lastKillTime + period) < currentTimeMillis) {
        lastKillTime = currentTimeMillis;
        try {
            RetryUtils.retry(() -> {
                final byte[] currentBytes = CoordinatorCompactionConfig.getConfigInByteFromDb(connector, connectorConfig);
                final CoordinatorCompactionConfig current = CoordinatorCompactionConfig.convertByteToConfig(jacksonConfigManager, currentBytes);
                // If current compaction config is empty then there is nothing to do
                if (CoordinatorCompactionConfig.empty().equals(current)) {
                    log.info("Finished running KillCompactionConfig duty. Nothing to do as compaction config is already empty.");
                    emitMetric(params.getEmitter(), 0);
                    return ConfigManager.SetResult.ok();
                }
                // Get all active datasources
                // Note that we get all active datasources after getting compaction config to prevent race condition if new
                // datasource and config are added.
                Set<String> activeDatasources = sqlSegmentsMetadataManager.retrieveAllDataSourceNames();
                final Map<String, DataSourceCompactionConfig> updated = current.getCompactionConfigs().stream().filter(dataSourceCompactionConfig -> activeDatasources.contains(dataSourceCompactionConfig.getDataSource())).collect(Collectors.toMap(DataSourceCompactionConfig::getDataSource, Function.identity()));
                // Calculate number of compaction configs to remove for logging
                int compactionConfigRemoved = current.getCompactionConfigs().size() - updated.size();
                ConfigManager.SetResult result = jacksonConfigManager.set(CoordinatorCompactionConfig.CONFIG_KEY, currentBytes, CoordinatorCompactionConfig.from(current, ImmutableList.copyOf(updated.values())), new AuditInfo("KillCompactionConfig", "CoordinatorDuty for automatic deletion of compaction config", ""));
                if (result.isOk()) {
                    log.info("Finished running KillCompactionConfig duty. Removed %,d compaction configs", compactionConfigRemoved);
                    emitMetric(params.getEmitter(), compactionConfigRemoved);
                } else if (result.isRetryable()) {
                    // Failed but is retryable
                    log.debug("Retrying KillCompactionConfig duty");
                    throw new RetryableException(result.getException());
                } else {
                    // Failed and not retryable
                    log.error(result.getException(), "Failed to kill compaction configurations");
                    emitMetric(params.getEmitter(), 0);
                }
                return result;
            }, e -> e instanceof RetryableException, UPDATE_NUM_RETRY);
        } catch (Exception e) {
            log.error(e, "Failed to kill compaction configurations");
            emitMetric(params.getEmitter(), 0);
        }
    }
    return params;
}
Also used : Logger(org.apache.druid.java.util.common.logger.Logger) AuditInfo(org.apache.druid.audit.AuditInfo) MetadataStorageTablesConfig(org.apache.druid.metadata.MetadataStorageTablesConfig) ServiceMetricEvent(org.apache.druid.java.util.emitter.service.ServiceMetricEvent) Inject(com.google.inject.Inject) CoordinatorCompactionConfig(org.apache.druid.server.coordinator.CoordinatorCompactionConfig) DruidCoordinatorRuntimeParams(org.apache.druid.server.coordinator.DruidCoordinatorRuntimeParams) Set(java.util.Set) JacksonConfigManager(org.apache.druid.common.config.JacksonConfigManager) RetryableException(org.apache.druid.java.util.RetryableException) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) DataSourceCompactionConfig(org.apache.druid.server.coordinator.DataSourceCompactionConfig) MetadataStorageConnector(org.apache.druid.metadata.MetadataStorageConnector) ImmutableList(com.google.common.collect.ImmutableList) ConfigManager(org.apache.druid.common.config.ConfigManager) ServiceEmitter(org.apache.druid.java.util.emitter.service.ServiceEmitter) DruidCoordinatorConfig(org.apache.druid.server.coordinator.DruidCoordinatorConfig) Map(java.util.Map) Preconditions(com.google.common.base.Preconditions) SqlSegmentsMetadataManager(org.apache.druid.metadata.SqlSegmentsMetadataManager) RetryUtils(org.apache.druid.java.util.common.RetryUtils) AuditInfo(org.apache.druid.audit.AuditInfo) CoordinatorCompactionConfig(org.apache.druid.server.coordinator.CoordinatorCompactionConfig) JacksonConfigManager(org.apache.druid.common.config.JacksonConfigManager) ConfigManager(org.apache.druid.common.config.ConfigManager) RetryableException(org.apache.druid.java.util.RetryableException) RetryableException(org.apache.druid.java.util.RetryableException) DataSourceCompactionConfig(org.apache.druid.server.coordinator.DataSourceCompactionConfig)

Example 2 with RetryableException

use of org.apache.druid.java.util.RetryableException in project druid by druid-io.

the class RetryUtilsTest method testExceptionPredicateForRetryableException.

@Test
public void testExceptionPredicateForRetryableException() throws Exception {
    final AtomicInteger count = new AtomicInteger();
    String result = RetryUtils.retry(() -> {
        if (count.incrementAndGet() >= 2) {
            return "hey";
        } else {
            throw new RetryableException(new RuntimeException("uhh"));
        }
    }, e -> e instanceof RetryableException, 3);
    Assert.assertEquals(result, "hey");
    Assert.assertEquals("count", 2, count.get());
}
Also used : RetryableException(org.apache.druid.java.util.RetryableException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

RetryableException (org.apache.druid.java.util.RetryableException)2 Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 Inject (com.google.inject.Inject)1 Map (java.util.Map)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 AuditInfo (org.apache.druid.audit.AuditInfo)1 ConfigManager (org.apache.druid.common.config.ConfigManager)1 JacksonConfigManager (org.apache.druid.common.config.JacksonConfigManager)1 RetryUtils (org.apache.druid.java.util.common.RetryUtils)1 Logger (org.apache.druid.java.util.common.logger.Logger)1 ServiceEmitter (org.apache.druid.java.util.emitter.service.ServiceEmitter)1 ServiceMetricEvent (org.apache.druid.java.util.emitter.service.ServiceMetricEvent)1 MetadataStorageConnector (org.apache.druid.metadata.MetadataStorageConnector)1 MetadataStorageTablesConfig (org.apache.druid.metadata.MetadataStorageTablesConfig)1 SqlSegmentsMetadataManager (org.apache.druid.metadata.SqlSegmentsMetadataManager)1 CoordinatorCompactionConfig (org.apache.druid.server.coordinator.CoordinatorCompactionConfig)1