Search in sources :

Example 61 with Supplier

use of org.apache.logging.log4j.util.Supplier in project elasticsearch by elastic.

the class ZenDiscovery method joinElectedMaster.

/**
     * Join a newly elected master.
     *
     * @return true if successful
     */
private boolean joinElectedMaster(DiscoveryNode masterNode) {
    try {
        // first, make sure we can connect to the master
        transportService.connectToNode(masterNode);
    } catch (Exception e) {
        logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to connect to master [{}], retrying...", masterNode), e);
        return false;
    }
    // we retry on illegal state if the master is not yet ready
    int joinAttempt = 0;
    while (true) {
        try {
            logger.trace("joining master {}", masterNode);
            membership.sendJoinRequestBlocking(masterNode, clusterService.localNode(), joinTimeout);
            return true;
        } catch (Exception e) {
            final Throwable unwrap = ExceptionsHelper.unwrapCause(e);
            if (unwrap instanceof NotMasterException) {
                if (++joinAttempt == this.joinRetryAttempts) {
                    logger.info("failed to send join request to master [{}], reason [{}], tried [{}] times", masterNode, ExceptionsHelper.detailedMessage(e), joinAttempt);
                    return false;
                } else {
                    logger.trace("master {} failed with [{}]. retrying... (attempts done: [{}])", masterNode, ExceptionsHelper.detailedMessage(e), joinAttempt);
                }
            } else {
                if (logger.isTraceEnabled()) {
                    logger.trace((Supplier<?>) () -> new ParameterizedMessage("failed to send join request to master [{}]", masterNode), e);
                } else {
                    logger.info("failed to send join request to master [{}], reason [{}]", masterNode, ExceptionsHelper.detailedMessage(e));
                }
                return false;
            }
        }
        try {
            Thread.sleep(this.joinRetryDelay.millis());
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
Also used : Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) NotMasterException(org.elasticsearch.cluster.NotMasterException) ElasticsearchException(org.elasticsearch.ElasticsearchException) TransportException(org.elasticsearch.transport.TransportException) NotMasterException(org.elasticsearch.cluster.NotMasterException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 62 with Supplier

use of org.apache.logging.log4j.util.Supplier in project elasticsearch by elastic.

the class ZenDiscovery method handleMinimumMasterNodesChanged.

private void handleMinimumMasterNodesChanged(final int minimumMasterNodes) {
    if (lifecycleState() != Lifecycle.State.STARTED) {
        // not started, ignore a node failure
        return;
    }
    final int prevMinimumMasterNode = ZenDiscovery.this.electMaster.minimumMasterNodes();
    ZenDiscovery.this.electMaster.minimumMasterNodes(minimumMasterNodes);
    if (!localNodeMaster()) {
        // We only set the new value. If the master doesn't see enough nodes it will revoke it's mastership.
        return;
    }
    clusterService.submitStateUpdateTask("zen-disco-min-master-nodes-changed", new LocalClusterUpdateTask(Priority.IMMEDIATE) {

        @Override
        public ClusterTasksResult<LocalClusterUpdateTask> execute(ClusterState currentState) {
            // check if we have enough master nodes, if not, we need to move into joining the cluster again
            if (!electMaster.hasEnoughMasterNodes(currentState.nodes())) {
                return rejoin(currentState, "not enough master nodes on change of minimum_master_nodes from [" + prevMinimumMasterNode + "] to [" + minimumMasterNodes + "]");
            }
            return unchanged();
        }

        @Override
        public void onNoLongerMaster(String source) {
        // ignoring (already logged)
        }

        @Override
        public void onFailure(String source, Exception e) {
            logger.error((Supplier<?>) () -> new ParameterizedMessage("unexpected failure during [{}]", source), e);
        }

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
            electMaster.logMinimumMasterNodesWarningIfNecessary(oldState, newState);
        }
    });
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) LocalClusterUpdateTask(org.elasticsearch.cluster.LocalClusterUpdateTask) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ElasticsearchException(org.elasticsearch.ElasticsearchException) TransportException(org.elasticsearch.transport.TransportException) NotMasterException(org.elasticsearch.cluster.NotMasterException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 63 with Supplier

use of org.apache.logging.log4j.util.Supplier in project elasticsearch by elastic.

the class AbstractScopedSettings method applySettings.

/**
     * Applies the given settings to all the settings consumers or to none of them. The settings
     * will be merged with the node settings before they are applied while given settings override existing node
     * settings.
     * @param newSettings the settings to apply
     * @return the unmerged applied settings
    */
public synchronized Settings applySettings(Settings newSettings) {
    if (lastSettingsApplied != null && newSettings.equals(lastSettingsApplied)) {
        // nothing changed in the settings, ignore
        return newSettings;
    }
    final Settings current = Settings.builder().put(this.settings).put(newSettings).build();
    final Settings previous = Settings.builder().put(this.settings).put(this.lastSettingsApplied).build();
    try {
        List<Runnable> applyRunnables = new ArrayList<>();
        for (SettingUpdater<?> settingUpdater : settingUpdaters) {
            try {
                applyRunnables.add(settingUpdater.updater(current, previous));
            } catch (Exception ex) {
                logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to prepareCommit settings for [{}]", settingUpdater), ex);
                throw ex;
            }
        }
        for (Runnable settingUpdater : applyRunnables) {
            settingUpdater.run();
        }
    } catch (Exception ex) {
        logger.warn("failed to apply settings", ex);
        throw ex;
    } finally {
    }
    return lastSettingsApplied = newSettings;
}
Also used : ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 64 with Supplier

use of org.apache.logging.log4j.util.Supplier in project elasticsearch by elastic.

the class MetaDataStateFormat method loadLatestState.

/**
     * Tries to load the latest state from the given data-locations. It tries to load the latest state determined by
     * the states version from one or more data directories and if none of the latest states can be loaded an exception
     * is thrown to prevent accidentally loading a previous state and silently omitting the latest state.
     *
     * @param logger a logger instance
     * @param dataLocations the data-locations to try.
     * @return the latest state or <code>null</code> if no state was found.
     */
public T loadLatestState(Logger logger, NamedXContentRegistry namedXContentRegistry, Path... dataLocations) throws IOException {
    List<PathAndStateId> files = new ArrayList<>();
    long maxStateId = -1;
    boolean maxStateIdIsLegacy = true;
    if (dataLocations != null) {
        // select all eligible files first
        for (Path dataLocation : dataLocations) {
            final Path stateDir = dataLocation.resolve(STATE_DIR_NAME);
            // we don't pass a glob since we need the group part for parsing
            try (DirectoryStream<Path> paths = Files.newDirectoryStream(stateDir)) {
                for (Path stateFile : paths) {
                    final Matcher matcher = stateFilePattern.matcher(stateFile.getFileName().toString());
                    if (matcher.matches()) {
                        final long stateId = Long.parseLong(matcher.group(1));
                        maxStateId = Math.max(maxStateId, stateId);
                        final boolean legacy = MetaDataStateFormat.STATE_FILE_EXTENSION.equals(matcher.group(2)) == false;
                        // on purpose, see NOTE below
                        maxStateIdIsLegacy &= legacy;
                        PathAndStateId pav = new PathAndStateId(stateFile, stateId, legacy);
                        logger.trace("found state file: {}", pav);
                        files.add(pav);
                    }
                }
            } catch (NoSuchFileException | FileNotFoundException ex) {
            // no _state directory -- move on
            }
        }
    }
    final List<Throwable> exceptions = new ArrayList<>();
    T state = null;
    // NOTE: we might have multiple version of the latest state if there are multiple data dirs.. for this case
    //       we iterate only over the ones with the max version. If we have at least one state file that uses the
    //       new format (ie. legacy == false) then we know that the latest version state ought to use this new format.
    //       In case the state file with the latest version does not use the new format while older state files do,
    //       the list below will be empty and loading the state will fail
    Collection<PathAndStateId> pathAndStateIds = files.stream().filter(new StateIdAndLegacyPredicate(maxStateId, maxStateIdIsLegacy)).collect(Collectors.toCollection(ArrayList::new));
    for (PathAndStateId pathAndStateId : pathAndStateIds) {
        try {
            final Path stateFile = pathAndStateId.file;
            final long id = pathAndStateId.id;
            if (pathAndStateId.legacy) {
                // read the legacy format -- plain XContent
                final byte[] data = Files.readAllBytes(stateFile);
                if (data.length == 0) {
                    logger.debug("{}: no data for [{}], ignoring...", prefix, stateFile.toAbsolutePath());
                    continue;
                }
                try (XContentParser parser = XContentHelper.createParser(namedXContentRegistry, new BytesArray(data))) {
                    state = fromXContent(parser);
                }
                if (state == null) {
                    logger.debug("{}: no data for [{}], ignoring...", prefix, stateFile.toAbsolutePath());
                }
            } else {
                state = read(namedXContentRegistry, stateFile);
                logger.trace("state id [{}] read from [{}]", id, stateFile.getFileName());
            }
            return state;
        } catch (Exception e) {
            exceptions.add(new IOException("failed to read " + pathAndStateId.toString(), e));
            logger.debug((Supplier<?>) () -> new ParameterizedMessage("{}: failed to read [{}], ignoring...", pathAndStateId.file.toAbsolutePath(), prefix), e);
        }
    }
    // if we reach this something went wrong
    ExceptionsHelper.maybeThrowRuntimeAndSuppress(exceptions);
    if (files.size() > 0) {
        // We have some state files but none of them gave us a usable state
        throw new IllegalStateException("Could not find a state file to recover from among " + files);
    }
    return state;
}
Also used : Path(java.nio.file.Path) BytesArray(org.elasticsearch.common.bytes.BytesArray) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) NoSuchFileException(java.nio.file.NoSuchFileException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) NoSuchFileException(java.nio.file.NoSuchFileException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) IOException(java.io.IOException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) FileNotFoundException(java.io.FileNotFoundException) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 65 with Supplier

use of org.apache.logging.log4j.util.Supplier in project elasticsearch by elastic.

the class MetaStateService method writeIndex.

/**
     * Writes the index state.
     *
     * This method is public for testing purposes.
     */
public void writeIndex(String reason, IndexMetaData indexMetaData) throws IOException {
    final Index index = indexMetaData.getIndex();
    logger.trace("[{}] writing state, reason [{}]", index, reason);
    try {
        IndexMetaData.FORMAT.write(indexMetaData, nodeEnv.indexPaths(indexMetaData.getIndex()));
    } catch (Exception ex) {
        logger.warn((Supplier<?>) () -> new ParameterizedMessage("[{}]: failed to write index state", index), ex);
        throw new IOException("failed to write state for [" + index + "]", ex);
    }
}
Also used : Index(org.elasticsearch.index.Index) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

Supplier (org.apache.logging.log4j.util.Supplier)94 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)91 IOException (java.io.IOException)55 ElasticsearchException (org.elasticsearch.ElasticsearchException)27 ArrayList (java.util.ArrayList)25 ClusterState (org.elasticsearch.cluster.ClusterState)21 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)21 TimeValue (org.elasticsearch.common.unit.TimeValue)14 HashMap (java.util.HashMap)12 Map (java.util.Map)11 Settings (org.elasticsearch.common.settings.Settings)11 TransportException (org.elasticsearch.transport.TransportException)11 List (java.util.List)10 ExecutionException (java.util.concurrent.ExecutionException)10 Index (org.elasticsearch.index.Index)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 NotMasterException (org.elasticsearch.cluster.NotMasterException)8 ClusterStateUpdateResponse (org.elasticsearch.cluster.ack.ClusterStateUpdateResponse)8 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)8 NoSuchFileException (java.nio.file.NoSuchFileException)7