Search in sources :

Example 81 with ParameterizedMessage

use of org.apache.logging.log4j.message.ParameterizedMessage 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 82 with ParameterizedMessage

use of org.apache.logging.log4j.message.ParameterizedMessage 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 83 with ParameterizedMessage

use of org.apache.logging.log4j.message.ParameterizedMessage in project elasticsearch by elastic.

the class PublishClusterStateAction method sendCommitToNode.

private void sendCommitToNode(final DiscoveryNode node, final ClusterState clusterState, final SendingController sendingController) {
    try {
        logger.trace("sending commit for cluster state (uuid: [{}], version [{}]) to [{}]", clusterState.stateUUID(), clusterState.version(), node);
        TransportRequestOptions options = TransportRequestOptions.builder().withType(TransportRequestOptions.Type.STATE).build();
        // no need to put a timeout on the options here, because we want the response to eventually be received
        // and not log an error if it arrives after the timeout
        transportService.sendRequest(node, COMMIT_ACTION_NAME, new CommitClusterStateRequest(clusterState.stateUUID()), options, new EmptyTransportResponseHandler(ThreadPool.Names.SAME) {

            @Override
            public void handleResponse(TransportResponse.Empty response) {
                if (sendingController.getPublishingTimedOut()) {
                    logger.debug("node {} responded to cluster state commit [{}]", node, clusterState.version());
                }
                sendingController.getPublishResponseHandler().onResponse(node);
            }

            @Override
            public void handleException(TransportException exp) {
                logger.debug((org.apache.logging.log4j.util.Supplier<?>) () -> new ParameterizedMessage("failed to commit cluster state (uuid [{}], version [{}]) to {}", clusterState.stateUUID(), clusterState.version(), node), exp);
                sendingController.getPublishResponseHandler().onFailure(node, exp);
            }
        });
    } catch (Exception t) {
        logger.warn((org.apache.logging.log4j.util.Supplier<?>) () -> new ParameterizedMessage("error sending cluster state commit (uuid [{}], version [{}]) to {}", clusterState.stateUUID(), clusterState.version(), node), t);
        sendingController.getPublishResponseHandler().onFailure(node, t);
    }
}
Also used : TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) Supplier(java.util.function.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) EmptyTransportResponseHandler(org.elasticsearch.transport.EmptyTransportResponseHandler) TransportResponse(org.elasticsearch.transport.TransportResponse) TransportException(org.elasticsearch.transport.TransportException) ElasticsearchException(org.elasticsearch.ElasticsearchException) IncompatibleClusterStateVersionException(org.elasticsearch.cluster.IncompatibleClusterStateVersionException) IOException(java.io.IOException) TransportException(org.elasticsearch.transport.TransportException)

Example 84 with ParameterizedMessage

use of org.apache.logging.log4j.message.ParameterizedMessage 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 85 with ParameterizedMessage

use of org.apache.logging.log4j.message.ParameterizedMessage 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)

Aggregations

ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)131 Supplier (org.apache.logging.log4j.util.Supplier)90 IOException (java.io.IOException)75 ElasticsearchException (org.elasticsearch.ElasticsearchException)38 ArrayList (java.util.ArrayList)28 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)26 ClusterState (org.elasticsearch.cluster.ClusterState)25 HashMap (java.util.HashMap)16 TimeValue (org.elasticsearch.common.unit.TimeValue)14 TransportException (org.elasticsearch.transport.TransportException)14 List (java.util.List)13 Supplier (java.util.function.Supplier)13 Map (java.util.Map)12 CountDownLatch (java.util.concurrent.CountDownLatch)12 ExecutionException (java.util.concurrent.ExecutionException)12 Settings (org.elasticsearch.common.settings.Settings)12 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)12 AbstractRunnable (org.elasticsearch.common.util.concurrent.AbstractRunnable)11 Index (org.elasticsearch.index.Index)11 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)10