Search in sources :

Example 6 with TimeValue

use of io.crate.common.unit.TimeValue in project crate by crate.

the class SslContextProviderService method doStart.

@Override
protected void doStart() {
    List<FingerPrint> filesToWatch = createFilesToWatch();
    if (filesToWatch.isEmpty()) {
        return;
    }
    TimeValue pollIntervalSetting = SslSettings.SSL_RESOURCE_POLL_INTERVAL.get(settings);
    watchRoutine = threadPool.scheduleWithFixedDelay(() -> pollForChanges(filesToWatch), pollIntervalSetting, ThreadPool.Names.GENERIC);
}
Also used : TimeValue(io.crate.common.unit.TimeValue)

Example 7 with TimeValue

use of io.crate.common.unit.TimeValue in project crate by crate.

the class TransportClusterStateAction method masterOperation.

@Override
protected void masterOperation(final ClusterStateRequest request, final ClusterState state, final ActionListener<ClusterStateResponse> listener) throws IOException {
    final Predicate<ClusterState> acceptableClusterStatePredicate = request.waitForMetadataVersion() == null ? clusterState -> true : clusterState -> clusterState.metadata().version() >= request.waitForMetadataVersion();
    final Predicate<ClusterState> acceptableClusterStateOrNotMasterPredicate = request.local() ? acceptableClusterStatePredicate : acceptableClusterStatePredicate.or(clusterState -> clusterState.nodes().isLocalNodeElectedMaster() == false);
    if (acceptableClusterStatePredicate.test(state)) {
        ActionListener.completeWith(listener, () -> buildResponse(request, state));
    } else {
        assert acceptableClusterStateOrNotMasterPredicate.test(state) == false;
        new ClusterStateObserver(state, clusterService, request.waitForTimeout(), logger).waitForNextChange(new ClusterStateObserver.Listener() {

            @Override
            public void onNewClusterState(ClusterState newState) {
                if (acceptableClusterStatePredicate.test(newState)) {
                    ActionListener.completeWith(listener, () -> buildResponse(request, newState));
                } else {
                    listener.onFailure(new NotMasterException("master stepped down waiting for metadata version " + request.waitForMetadataVersion()));
                }
            }

            @Override
            public void onClusterServiceClose() {
                listener.onFailure(new NodeClosedException(clusterService.localNode()));
            }

            @Override
            public void onTimeout(TimeValue timeout) {
                try {
                    listener.onResponse(new ClusterStateResponse(state.getClusterName(), null, true));
                } catch (Exception e) {
                    listener.onFailure(e);
                }
            }
        }, acceptableClusterStateOrNotMasterPredicate);
    }
}
Also used : Custom(org.elasticsearch.cluster.metadata.Metadata.Custom) Predicate(java.util.function.Predicate) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) NotMasterException(org.elasticsearch.cluster.NotMasterException) ClusterService(org.elasticsearch.cluster.service.ClusterService) IOException(java.io.IOException) NodeClosedException(org.elasticsearch.node.NodeClosedException) Inject(org.elasticsearch.common.inject.Inject) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) ClusterState(org.elasticsearch.cluster.ClusterState) Metadata(org.elasticsearch.cluster.metadata.Metadata) Settings(org.elasticsearch.common.settings.Settings) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) TransportMasterNodeReadAction(org.elasticsearch.action.support.master.TransportMasterNodeReadAction) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) StreamInput(org.elasticsearch.common.io.stream.StreamInput) TimeValue(io.crate.common.unit.TimeValue) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportService(org.elasticsearch.transport.TransportService) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) ActionListener(org.elasticsearch.action.ActionListener) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) NodeClosedException(org.elasticsearch.node.NodeClosedException) NotMasterException(org.elasticsearch.cluster.NotMasterException) TimeValue(io.crate.common.unit.TimeValue) NotMasterException(org.elasticsearch.cluster.NotMasterException) IOException(java.io.IOException) NodeClosedException(org.elasticsearch.node.NodeClosedException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException)

Example 8 with TimeValue

use of io.crate.common.unit.TimeValue in project crate by crate.

the class MetadataIndexTemplateService method putTemplate.

public void putTemplate(final PutRequest request, final PutListener listener) {
    Settings.Builder updatedSettingsBuilder = Settings.builder();
    updatedSettingsBuilder.put(request.settings).normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX);
    request.settings(updatedSettingsBuilder.build());
    if (request.name == null) {
        listener.onFailure(new IllegalArgumentException("index_template must provide a name"));
        return;
    }
    if (request.indexPatterns == null) {
        listener.onFailure(new IllegalArgumentException("index_template must provide a template"));
        return;
    }
    try {
        validate(request);
    } catch (Exception e) {
        listener.onFailure(e);
        return;
    }
    final IndexTemplateMetadata.Builder templateBuilder = IndexTemplateMetadata.builder(request.name);
    clusterService.submitStateUpdateTask("create-index-template [" + request.name + "], cause [" + request.cause + "]", new ClusterStateUpdateTask(Priority.URGENT) {

        @Override
        public TimeValue timeout() {
            return request.masterTimeout;
        }

        @Override
        public void onFailure(String source, Exception e) {
            listener.onFailure(e);
        }

        @Override
        public ClusterState execute(ClusterState currentState) throws Exception {
            if (request.create && currentState.metadata().templates().containsKey(request.name)) {
                throw new IllegalArgumentException("index_template [" + request.name + "] already exists");
            }
            validateAndAddTemplate(request, templateBuilder, indicesService, xContentRegistry);
            for (Alias alias : request.aliases) {
                AliasMetadata aliasMetadata = AliasMetadata.builder(alias.name()).filter(alias.filter()).indexRouting(alias.indexRouting()).searchRouting(alias.searchRouting()).build();
                templateBuilder.putAlias(aliasMetadata);
            }
            IndexTemplateMetadata template = templateBuilder.build();
            Metadata.Builder builder = Metadata.builder(currentState.metadata()).put(template);
            LOGGER.info("adding template [{}] for index patterns {}", request.name, request.indexPatterns);
            return ClusterState.builder(currentState).metadata(builder).build();
        }

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
            listener.onResponse(new PutResponse(true, templateBuilder.build()));
        }
    });
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) ValidationException(org.elasticsearch.common.ValidationException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) InvalidIndexTemplateException(org.elasticsearch.indices.InvalidIndexTemplateException) IndexTemplateMissingException(org.elasticsearch.indices.IndexTemplateMissingException) Alias(org.elasticsearch.action.admin.indices.alias.Alias) Settings(org.elasticsearch.common.settings.Settings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) TimeValue(io.crate.common.unit.TimeValue)

Example 9 with TimeValue

use of io.crate.common.unit.TimeValue in project crate by crate.

the class DeterministicTaskQueue method getThreadPool.

/**
 * @return A <code>ThreadPool</code> that uses this task queue and wraps <code>Runnable</code>s in the given wrapper.
 */
public ThreadPool getThreadPool(Function<Runnable, Runnable> runnableWrapper) {
    return new ThreadPool(settings) {

        {
            stopCachedTimeThread();
        }

        @Override
        public long relativeTimeInMillis() {
            return currentTimeMillis;
        }

        @Override
        public long absoluteTimeInMillis() {
            return currentTimeMillis;
        }

        @Override
        public ThreadPoolStats stats() {
            throw new UnsupportedOperationException();
        }

        @Override
        public ExecutorService generic() {
            return getExecutorService(runnableWrapper);
        }

        @Override
        public ExecutorService executor(String name) {
            return getExecutorService(runnableWrapper);
        }

        @Override
        public ScheduledCancellable schedule(Runnable command, TimeValue delay, String executor) {
            final int NOT_STARTED = 0;
            final int STARTED = 1;
            final int CANCELLED = 2;
            final AtomicInteger taskState = new AtomicInteger(NOT_STARTED);
            scheduleAt(currentTimeMillis + delay.millis(), runnableWrapper.apply(new Runnable() {

                @Override
                public void run() {
                    if (taskState.compareAndSet(NOT_STARTED, STARTED)) {
                        command.run();
                    }
                }

                @Override
                public String toString() {
                    return command.toString();
                }
            }));
            return new ScheduledCancellable() {

                @Override
                public long getDelay(TimeUnit unit) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public int compareTo(Delayed o) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public boolean cancel() {
                    return taskState.compareAndSet(NOT_STARTED, CANCELLED);
                }

                @Override
                public boolean isCancelled() {
                    return taskState.get() == CANCELLED;
                }
            };
        }

        @Override
        public Cancellable scheduleWithFixedDelay(Runnable command, TimeValue interval, String executor) {
            return super.scheduleWithFixedDelay(command, interval, executor);
        }

        @Override
        public Runnable preserveContext(Runnable command) {
            return command;
        }

        @Override
        public void shutdown() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void shutdownNow() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean awaitTermination(long timeout, TimeUnit unit) {
            throw new UnsupportedOperationException();
        }

        @Override
        public ScheduledExecutorService scheduler() {
            return new ScheduledExecutorService() {

                @Override
                public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public void shutdown() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public List<Runnable> shutdownNow() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public boolean isShutdown() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public boolean isTerminated() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public boolean awaitTermination(long timeout, TimeUnit unit) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public <T> Future<T> submit(Callable<T> task) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public <T> Future<T> submit(Runnable task, T result) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public Future<?> submit(Runnable task) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public <T> T invokeAny(Collection<? extends Callable<T>> tasks) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public void execute(Runnable command) {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Delayed(java.util.concurrent.Delayed) ThreadPool(org.elasticsearch.threadpool.ThreadPool) Callable(java.util.concurrent.Callable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TimeUnit(java.util.concurrent.TimeUnit) Collection(java.util.Collection) ScheduledFuture(java.util.concurrent.ScheduledFuture) Future(java.util.concurrent.Future) TimeValue(io.crate.common.unit.TimeValue)

Example 10 with TimeValue

use of io.crate.common.unit.TimeValue in project crate by crate.

the class SQLTransportExecutor method executeTransportOrJdbc.

private SQLResponse executeTransportOrJdbc(TestExecutionConfig config, String stmt, @Nullable Object[] args, TimeValue timeout) {
    String pgUrl = clientProvider.pgUrl();
    Random random = RandomizedContext.current().getRandom();
    List<String> sessionList = new ArrayList<>();
    sessionList.add("set search_path to " + StreamSupport.stream(searchPath.spliterator(), false).filter(s -> !s.equals(PgCatalogSchemaInfo.NAME)).collect(Collectors.joining(", ")));
    if (!config.isHashJoinEnabled()) {
        sessionList.add("set enable_hashjoin=false");
        LOGGER.trace("Executing with enable_hashjoin=false: {}", stmt);
    }
    if (pgUrl != null && config.isJdbcEnabled()) {
        LOGGER.trace("Executing with pgJDBC: {}", stmt);
        return executeWithPg(stmt, args, pgUrl, random, sessionList);
    }
    try {
        try (Session session = newSession()) {
            sessionList.forEach((setting) -> exec(setting, session));
            return FutureUtils.get(execute(stmt, args, session), timeout.millis(), TimeUnit.MILLISECONDS);
        }
    } catch (RuntimeException e) {
        var cause = e.getCause();
        // to figure out the root cause of an error, so we prefer the cause here
        if (e.getClass() == RuntimeException.class && cause != null) {
            Exceptions.rethrowUnchecked(cause);
        }
        throw e;
    }
}
Also used : Arrays(java.util.Arrays) Connection(java.sql.Connection) PGTypes(io.crate.protocols.postgres.types.PGTypes) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) JsonXContent(org.elasticsearch.common.xcontent.json.JsonXContent) Array(java.sql.Array) Unpooled(io.netty.buffer.Unpooled) Assert.assertThat(org.junit.Assert.assertThat) BaseResultReceiver(io.crate.action.sql.BaseResultReceiver) JtsSpatialContext(org.locationtech.spatial4j.context.jts.JtsSpatialContext) ResultSet(java.sql.ResultSet) Map(java.util.Map) AdapterActionFuture(org.elasticsearch.action.support.AdapterActionFuture) SQLOperations(io.crate.action.sql.SQLOperations) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) Priority(org.elasticsearch.common.Priority) RandomizedContext(com.carrotsearch.randomizedtesting.RandomizedContext) User(io.crate.user.User) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) PreparedStatement(java.sql.PreparedStatement) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl) List(java.util.List) Exceptions(io.crate.exceptions.Exceptions) Logger(org.apache.logging.log4j.Logger) Session(io.crate.action.sql.Session) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) DataTypes(io.crate.types.DataTypes) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TimeValue(io.crate.common.unit.TimeValue) PGpoint(org.postgresql.geometric.PGpoint) ResultSetMetaData(java.sql.ResultSetMetaData) Row1(io.crate.data.Row1) AccessControl(io.crate.auth.AccessControl) PGType(io.crate.protocols.postgres.types.PGType) CompletableFuture(java.util.concurrent.CompletableFuture) SearchPath(io.crate.metadata.SearchPath) PgCatalogSchemaInfo(io.crate.metadata.pgcatalog.PgCatalogSchemaInfo) ArrayList(java.util.ArrayList) PGobject(org.postgresql.util.PGobject) DeprecationHandler(org.elasticsearch.common.xcontent.DeprecationHandler) SQLException(java.sql.SQLException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) ByteBuf(io.netty.buffer.ByteBuf) Symbols(io.crate.expression.symbol.Symbols) StreamSupport(java.util.stream.StreamSupport) Requests(org.elasticsearch.client.Requests) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) FutureActionListener(io.crate.action.FutureActionListener) Properties(java.util.Properties) FutureUtils(org.elasticsearch.common.util.concurrent.FutureUtils) Client(org.elasticsearch.client.Client) Matchers(org.hamcrest.Matchers) DataType(io.crate.types.DataType) IOException(java.io.IOException) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) PgOidVectorType(io.crate.protocols.postgres.types.PgOidVectorType) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) XContentParser(org.elasticsearch.common.xcontent.XContentParser) PGArray(io.crate.protocols.postgres.types.PGArray) ClusterHealthStatus(org.elasticsearch.cluster.health.ClusterHealthStatus) SQLExceptions(io.crate.exceptions.SQLExceptions) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) DriverManager(java.sql.DriverManager) ActionListener(org.elasticsearch.action.ActionListener) ResultReceiver(io.crate.action.sql.ResultReceiver) UNNAMED(io.crate.action.sql.Session.UNNAMED) Random(java.util.Random) ArrayList(java.util.ArrayList) Session(io.crate.action.sql.Session)

Aggregations

TimeValue (io.crate.common.unit.TimeValue)75 Test (org.junit.Test)23 ClusterState (org.elasticsearch.cluster.ClusterState)20 IOException (java.io.IOException)17 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)12 ActionListener (org.elasticsearch.action.ActionListener)12 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)11 ArrayList (java.util.ArrayList)10 ThreadPool (org.elasticsearch.threadpool.ThreadPool)10 ElasticsearchException (org.elasticsearch.ElasticsearchException)9 Settings (org.elasticsearch.common.settings.Settings)9 Logger (org.apache.logging.log4j.Logger)8 ClusterStateUpdateTask (org.elasticsearch.cluster.ClusterStateUpdateTask)8 ClusterService (org.elasticsearch.cluster.service.ClusterService)8 List (java.util.List)7 LogManager (org.apache.logging.log4j.LogManager)7 Version (org.elasticsearch.Version)7 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)6 ClusterStateObserver (org.elasticsearch.cluster.ClusterStateObserver)6 StreamInput (org.elasticsearch.common.io.stream.StreamInput)6