Search in sources :

Example 16 with Tuple

use of org.elasticsearch.common.collect.Tuple in project elasticsearch by elastic.

the class FsProbe method stats.

public FsInfo stats(FsInfo previous, @Nullable ClusterInfo clusterInfo) throws IOException {
    if (!nodeEnv.hasNodeFile()) {
        return new FsInfo(System.currentTimeMillis(), null, new FsInfo.Path[0]);
    }
    NodePath[] dataLocations = nodeEnv.nodePaths();
    FsInfo.Path[] paths = new FsInfo.Path[dataLocations.length];
    for (int i = 0; i < dataLocations.length; i++) {
        paths[i] = getFSInfo(dataLocations[i]);
    }
    FsInfo.IoStats ioStats = null;
    if (Constants.LINUX) {
        Set<Tuple<Integer, Integer>> devicesNumbers = new HashSet<>();
        for (int i = 0; i < dataLocations.length; i++) {
            if (dataLocations[i].majorDeviceNumber != -1 && dataLocations[i].minorDeviceNumber != -1) {
                devicesNumbers.add(Tuple.tuple(dataLocations[i].majorDeviceNumber, dataLocations[i].minorDeviceNumber));
            }
        }
        ioStats = ioStats(devicesNumbers, previous);
    }
    DiskUsage leastDiskEstimate = null;
    DiskUsage mostDiskEstimate = null;
    if (clusterInfo != null) {
        leastDiskEstimate = clusterInfo.getNodeLeastAvailableDiskUsages().get(nodeEnv.nodeId());
        mostDiskEstimate = clusterInfo.getNodeMostAvailableDiskUsages().get(nodeEnv.nodeId());
    }
    return new FsInfo(System.currentTimeMillis(), ioStats, paths, leastDiskEstimate, mostDiskEstimate);
}
Also used : NodePath(org.elasticsearch.env.NodeEnvironment.NodePath) DiskUsage(org.elasticsearch.cluster.DiskUsage) NodePath(org.elasticsearch.env.NodeEnvironment.NodePath) Tuple(org.elasticsearch.common.collect.Tuple) HashSet(java.util.HashSet)

Example 17 with Tuple

use of org.elasticsearch.common.collect.Tuple in project elasticsearch by elastic.

the class FsProbe method ioStats.

final FsInfo.IoStats ioStats(final Set<Tuple<Integer, Integer>> devicesNumbers, final FsInfo previous) {
    try {
        final Map<Tuple<Integer, Integer>, FsInfo.DeviceStats> deviceMap = new HashMap<>();
        if (previous != null && previous.getIoStats() != null && previous.getIoStats().devicesStats != null) {
            for (int i = 0; i < previous.getIoStats().devicesStats.length; i++) {
                FsInfo.DeviceStats deviceStats = previous.getIoStats().devicesStats[i];
                deviceMap.put(Tuple.tuple(deviceStats.majorDeviceNumber, deviceStats.minorDeviceNumber), deviceStats);
            }
        }
        List<FsInfo.DeviceStats> devicesStats = new ArrayList<>();
        List<String> lines = readProcDiskStats();
        if (!lines.isEmpty()) {
            for (String line : lines) {
                String[] fields = line.trim().split("\\s+");
                final int majorDeviceNumber = Integer.parseInt(fields[0]);
                final int minorDeviceNumber = Integer.parseInt(fields[1]);
                if (!devicesNumbers.contains(Tuple.tuple(majorDeviceNumber, minorDeviceNumber))) {
                    continue;
                }
                final String deviceName = fields[2];
                final long readsCompleted = Long.parseLong(fields[3]);
                final long sectorsRead = Long.parseLong(fields[5]);
                final long writesCompleted = Long.parseLong(fields[7]);
                final long sectorsWritten = Long.parseLong(fields[9]);
                final FsInfo.DeviceStats deviceStats = new FsInfo.DeviceStats(majorDeviceNumber, minorDeviceNumber, deviceName, readsCompleted, sectorsRead, writesCompleted, sectorsWritten, deviceMap.get(Tuple.tuple(majorDeviceNumber, minorDeviceNumber)));
                devicesStats.add(deviceStats);
            }
        }
        return new FsInfo.IoStats(devicesStats.toArray(new FsInfo.DeviceStats[devicesStats.size()]));
    } catch (Exception e) {
        // do not fail Elasticsearch if something unexpected
        // happens here
        logger.debug((Supplier<?>) () -> new ParameterizedMessage("unexpected exception processing /proc/diskstats for devices {}", devicesNumbers), e);
        return null;
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Tuple(org.elasticsearch.common.collect.Tuple)

Example 18 with Tuple

use of org.elasticsearch.common.collect.Tuple in project elasticsearch by elastic.

the class PluginsService method loadBundles.

private List<Tuple<PluginInfo, Plugin>> loadBundles(List<Bundle> bundles) {
    List<Tuple<PluginInfo, Plugin>> plugins = new ArrayList<>();
    for (Bundle bundle : bundles) {
        // pluginmanager does it, but we do it again, in case lusers mess with jar files manually
        try {
            final List<URL> jars = new ArrayList<>();
            jars.addAll(Arrays.asList(JarHell.parseClassPath()));
            jars.addAll(bundle.urls);
            JarHell.checkJarHell(jars.toArray(new URL[0]));
        } catch (Exception e) {
            throw new IllegalStateException("failed to load bundle " + bundle.urls + " due to jar hell", e);
        }
        // create a child to load the plugins in this bundle
        ClassLoader loader = URLClassLoader.newInstance(bundle.urls.toArray(new URL[0]), getClass().getClassLoader());
        for (PluginInfo pluginInfo : bundle.plugins) {
            // reload lucene SPI with any new services from the plugin
            reloadLuceneSPI(loader);
            final Class<? extends Plugin> pluginClass = loadPluginClass(pluginInfo.getClassname(), loader);
            final Plugin plugin = loadPlugin(pluginClass, settings);
            plugins.add(new Tuple<>(pluginInfo, plugin));
        }
    }
    return Collections.unmodifiableList(plugins);
}
Also used : ArrayList(java.util.ArrayList) URL(java.net.URL) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) URLClassLoader(java.net.URLClassLoader) Tuple(org.elasticsearch.common.collect.Tuple)

Example 19 with Tuple

use of org.elasticsearch.common.collect.Tuple in project elasticsearch by elastic.

the class RestUpgradeAction method handlePost.

private RestChannelConsumer handlePost(final RestRequest request, NodeClient client) {
    UpgradeRequest upgradeReq = new UpgradeRequest(Strings.splitStringByCommaToArray(request.param("index")));
    upgradeReq.indicesOptions(IndicesOptions.fromRequest(request, upgradeReq.indicesOptions()));
    upgradeReq.upgradeOnlyAncientSegments(request.paramAsBoolean("only_ancient_segments", false));
    return channel -> client.admin().indices().upgrade(upgradeReq, new RestBuilderListener<UpgradeResponse>(channel) {

        @Override
        public RestResponse buildResponse(UpgradeResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            buildBroadcastShardsHeader(builder, request, response);
            builder.startObject("upgraded_indices");
            for (Map.Entry<String, Tuple<Version, String>> entry : response.versions().entrySet()) {
                builder.startObject(entry.getKey());
                builder.field("upgrade_version", entry.getValue().v1());
                builder.field("oldest_lucene_segment_version", entry.getValue().v2());
                builder.endObject();
            }
            builder.endObject();
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
Also used : UpgradeRequest(org.elasticsearch.action.admin.indices.upgrade.post.UpgradeRequest) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) UpgradeStatusRequest(org.elasticsearch.action.admin.indices.upgrade.get.UpgradeStatusRequest) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) UpgradeStatusResponse(org.elasticsearch.action.admin.indices.upgrade.get.UpgradeStatusResponse) Strings(org.elasticsearch.common.Strings) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) POST(org.elasticsearch.rest.RestRequest.Method.POST) Version(org.elasticsearch.Version) Settings(org.elasticsearch.common.settings.Settings) RestActions.buildBroadcastShardsHeader(org.elasticsearch.rest.action.RestActions.buildBroadcastShardsHeader) UpgradeResponse(org.elasticsearch.action.admin.indices.upgrade.post.UpgradeResponse) Map(java.util.Map) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) Tuple(org.elasticsearch.common.collect.Tuple) UpgradeResponse(org.elasticsearch.action.admin.indices.upgrade.post.UpgradeResponse) Version(org.elasticsearch.Version) UpgradeRequest(org.elasticsearch.action.admin.indices.upgrade.post.UpgradeRequest) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

Example 20 with Tuple

use of org.elasticsearch.common.collect.Tuple in project elasticsearch by elastic.

the class SnapshotsService method beginSnapshot.

/**
     * Starts snapshot.
     * <p>
     * Creates snapshot in repository and updates snapshot metadata record with list of shards that needs to be processed.
     *
     * @param clusterState               cluster state
     * @param snapshot                   snapshot meta data
     * @param partial                    allow partial snapshots
     * @param userCreateSnapshotListener listener
     */
private void beginSnapshot(final ClusterState clusterState, final SnapshotsInProgress.Entry snapshot, final boolean partial, final CreateSnapshotListener userCreateSnapshotListener) {
    boolean snapshotCreated = false;
    try {
        Repository repository = repositoriesService.repository(snapshot.snapshot().getRepository());
        MetaData metaData = clusterState.metaData();
        if (!snapshot.includeGlobalState()) {
            // Remove global state from the cluster state
            MetaData.Builder builder = MetaData.builder();
            for (IndexId index : snapshot.indices()) {
                builder.put(metaData.index(index.getName()), false);
            }
            metaData = builder.build();
        }
        repository.initializeSnapshot(snapshot.snapshot().getSnapshotId(), snapshot.indices(), metaData);
        snapshotCreated = true;
        if (snapshot.indices().isEmpty()) {
            // No indices in this snapshot - we are done
            userCreateSnapshotListener.onResponse();
            endSnapshot(snapshot);
            return;
        }
        clusterService.submitStateUpdateTask("update_snapshot [" + snapshot.snapshot() + "]", new ClusterStateUpdateTask() {

            boolean accepted = false;

            SnapshotsInProgress.Entry updatedSnapshot;

            String failure = null;

            @Override
            public ClusterState execute(ClusterState currentState) {
                SnapshotsInProgress snapshots = currentState.custom(SnapshotsInProgress.TYPE);
                List<SnapshotsInProgress.Entry> entries = new ArrayList<>();
                for (SnapshotsInProgress.Entry entry : snapshots.entries()) {
                    if (entry.snapshot().equals(snapshot.snapshot())) {
                        // Replace the snapshot that was just created
                        ImmutableOpenMap<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shards = shards(currentState, entry.indices());
                        if (!partial) {
                            Tuple<Set<String>, Set<String>> indicesWithMissingShards = indicesWithMissingShards(shards, currentState.metaData());
                            Set<String> missing = indicesWithMissingShards.v1();
                            Set<String> closed = indicesWithMissingShards.v2();
                            if (missing.isEmpty() == false || closed.isEmpty() == false) {
                                StringBuilder failureMessage = new StringBuilder();
                                updatedSnapshot = new SnapshotsInProgress.Entry(entry, State.FAILED, shards);
                                entries.add(updatedSnapshot);
                                if (missing.isEmpty() == false) {
                                    failureMessage.append("Indices don't have primary shards ");
                                    failureMessage.append(missing);
                                }
                                if (closed.isEmpty() == false) {
                                    if (failureMessage.length() > 0) {
                                        failureMessage.append("; ");
                                    }
                                    failureMessage.append("Indices are closed ");
                                    failureMessage.append(closed);
                                }
                                failure = failureMessage.toString();
                                continue;
                            }
                        }
                        updatedSnapshot = new SnapshotsInProgress.Entry(entry, State.STARTED, shards);
                        entries.add(updatedSnapshot);
                        if (!completed(shards.values())) {
                            accepted = true;
                        }
                    } else {
                        entries.add(entry);
                    }
                }
                return ClusterState.builder(currentState).putCustom(SnapshotsInProgress.TYPE, new SnapshotsInProgress(Collections.unmodifiableList(entries))).build();
            }

            @Override
            public void onFailure(String source, Exception e) {
                logger.warn((Supplier<?>) () -> new ParameterizedMessage("[{}] failed to create snapshot", snapshot.snapshot().getSnapshotId()), e);
                removeSnapshotFromClusterState(snapshot.snapshot(), null, e, new CleanupAfterErrorListener(snapshot, true, userCreateSnapshotListener, e));
            }

            @Override
            public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                // The userCreateSnapshotListener.onResponse() notifies caller that the snapshot was accepted
                // for processing. If client wants to wait for the snapshot completion, it can register snapshot
                // completion listener in this method. For the snapshot completion to work properly, the snapshot
                // should still exist when listener is registered.
                userCreateSnapshotListener.onResponse();
                // go ahead and continue working on this snapshot rather then end here.
                if (!accepted && updatedSnapshot != null) {
                    endSnapshot(updatedSnapshot, failure);
                }
            }
        });
    } catch (Exception e) {
        logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to create snapshot [{}]", snapshot.snapshot().getSnapshotId()), e);
        removeSnapshotFromClusterState(snapshot.snapshot(), null, e, new CleanupAfterErrorListener(snapshot, snapshotCreated, userCreateSnapshotListener, e));
    }
}
Also used : IndexId(org.elasticsearch.repositories.IndexId) ClusterState(org.elasticsearch.cluster.ClusterState) Set(java.util.Set) HashSet(java.util.HashSet) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) RepositoryMissingException(org.elasticsearch.repositories.RepositoryMissingException) IOException(java.io.IOException) Repository(org.elasticsearch.repositories.Repository) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) SnapshotsInProgress(org.elasticsearch.cluster.SnapshotsInProgress) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Tuple(org.elasticsearch.common.collect.Tuple)

Aggregations

Tuple (org.elasticsearch.common.collect.Tuple)50 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)17 HashMap (java.util.HashMap)12 List (java.util.List)12 Map (java.util.Map)12 Settings (org.elasticsearch.common.settings.Settings)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 HashSet (java.util.HashSet)6 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 Supplier (org.apache.logging.log4j.util.Supplier)5 TaskInfo (org.elasticsearch.tasks.TaskInfo)5 Set (java.util.Set)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Consumer (java.util.function.Consumer)4 ElasticsearchException (org.elasticsearch.ElasticsearchException)4 Version (org.elasticsearch.Version)4 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)4