Search in sources :

Example 16 with NodeEnvironment

use of org.elasticsearch.env.NodeEnvironment in project elasticsearch by elastic.

the class IndicesServiceTests method testVerifyIfIndexContentDeleted.

public void testVerifyIfIndexContentDeleted() throws Exception {
    final Index index = new Index("test", UUIDs.randomBase64UUID());
    final IndicesService indicesService = getIndicesService();
    final NodeEnvironment nodeEnv = getNodeEnvironment();
    final MetaStateService metaStateService = getInstanceFromNode(MetaStateService.class);
    final ClusterService clusterService = getInstanceFromNode(ClusterService.class);
    final Settings idxSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetaData.SETTING_INDEX_UUID, index.getUUID()).build();
    final IndexMetaData indexMetaData = new IndexMetaData.Builder(index.getName()).settings(idxSettings).numberOfShards(1).numberOfReplicas(0).build();
    metaStateService.writeIndex("test index being created", indexMetaData);
    final MetaData metaData = MetaData.builder(clusterService.state().metaData()).put(indexMetaData, true).build();
    final ClusterState csWithIndex = new ClusterState.Builder(clusterService.state()).metaData(metaData).build();
    try {
        indicesService.verifyIndexIsDeleted(index, csWithIndex);
        fail("Should not be able to delete index contents when the index is part of the cluster state.");
    } catch (IllegalStateException e) {
        assertThat(e.getMessage(), containsString("Cannot delete index"));
    }
    final ClusterState withoutIndex = new ClusterState.Builder(csWithIndex).metaData(MetaData.builder(csWithIndex.metaData()).remove(index.getName())).build();
    indicesService.verifyIndexIsDeleted(index, withoutIndex);
    assertFalse("index files should be deleted", FileSystemUtils.exists(nodeEnv.indexPaths(index)));
}
Also used : MetaStateService(org.elasticsearch.gateway.MetaStateService) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterService(org.elasticsearch.cluster.service.ClusterService) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Index(org.elasticsearch.index.Index) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 17 with NodeEnvironment

use of org.elasticsearch.env.NodeEnvironment in project crate by crate.

the class SysNodesExpressionsOnHandlerTest method setup.

@BeforeClass
public static void setup() throws IOException {
    // jvm
    JvmStats jvmStats = mock(JvmStats.class);
    JvmStats.Mem jvmStatsMem = mock(JvmStats.Mem.class);
    ByteSizeValue heapByteSizeValueMax = new ByteSizeValue(123456L);
    when(jvmStatsMem.getHeapMax()).thenReturn(heapByteSizeValueMax);
    when(jvmStatsMem.getHeapUsed()).thenReturn(heapByteSizeValueMax);
    when(jvmStats.getMem()).thenReturn(jvmStatsMem);
    // mem
    ByteSizeValue byteSizeValue = new ByteSizeValue(12345342234L);
    // os service
    OsService osService = mock(OsService.class);
    OsStats osStats = mock(OsStats.class);
    when(osService.stats()).thenReturn(osStats);
    OsStats.Mem mem = mock(OsStats.Mem.class);
    when(osStats.getMem()).thenReturn(mem);
    when(mem.getFree()).thenReturn(byteSizeValue);
    when(mem.getUsed()).thenReturn(byteSizeValue);
    when(mem.getUsedPercent()).thenReturn((short) 22);
    when(mem.getFreePercent()).thenReturn((short) 78);
    // os info
    OsInfo osInfo = mock(OsInfo.class);
    when(osService.info()).thenReturn(osInfo);
    when(osInfo.getAvailableProcessors()).thenReturn(4);
    // node info
    NodeEnvironment nodeEnv = mock(NodeEnvironment.class);
    Path[] dataLocations = new Path[] { new File("/foo").toPath(), new File("/bar").toPath() };
    when(nodeEnv.hasNodeFile()).then(invocation -> true);
    when(nodeEnv.nodeDataPaths()).thenReturn(dataLocations);
    ExtendedNodeInfo extendedNodeInfo = new DummyExtendedNodeInfo(nodeEnv);
    // process stats
    ProcessStats processStats = mock(ProcessStats.class);
    when(processStats.getOpenFileDescriptors()).thenReturn(42L);
    when(processStats.getMaxFileDescriptors()).thenReturn(1000L);
    CONTEXT.id(BytesRefs.toBytesRef("93c7ff92-52fa-11e6-aad8-3c15c2d3ad18"));
    CONTEXT.name(BytesRefs.toBytesRef("crate1"));
    CONTEXT.hostname(BytesRefs.toBytesRef("crate1.example.com"));
    CONTEXT.version(Version.CURRENT);
    CONTEXT.build(Build.CURRENT);
    CONTEXT.timestamp(100L);
    CONTEXT.restUrl(BytesRefs.toBytesRef("10.0.0.1:4200"));
    CONTEXT.port(new HashMap<String, Integer>(2) {

        {
            put("http", 4200);
            put("transport", 4300);
        }
    });
    CONTEXT.jvmStats(jvmStats);
    CONTEXT.osInfo(osInfo);
    CONTEXT.processStats(processStats);
    CONTEXT.osStats(osStats);
    CONTEXT.extendedOsStats(extendedNodeInfo.osStats());
    CONTEXT.networkStats(extendedNodeInfo.networkStats());
    CONTEXT.extendedProcessCpuStats(extendedNodeInfo.processCpuStats());
    CONTEXT.extendedFsStats(extendedNodeInfo.fsStats());
}
Also used : JvmStats(org.elasticsearch.monitor.jvm.JvmStats) Path(java.nio.file.Path) ProcessStats(org.elasticsearch.monitor.process.ProcessStats) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) DummyExtendedNodeInfo(io.crate.monitor.DummyExtendedNodeInfo) ExtendedNodeInfo(io.crate.monitor.ExtendedNodeInfo) OsStats(org.elasticsearch.monitor.os.OsStats) TestingHelpers.mapToSortedString(io.crate.testing.TestingHelpers.mapToSortedString) OsService(org.elasticsearch.monitor.os.OsService) OsInfo(org.elasticsearch.monitor.os.OsInfo) File(java.io.File) DummyExtendedNodeInfo(io.crate.monitor.DummyExtendedNodeInfo) BeforeClass(org.junit.BeforeClass)

Example 18 with NodeEnvironment

use of org.elasticsearch.env.NodeEnvironment in project elasticsearch by elastic.

the class ShardPath method selectNewPathForShard.

public static ShardPath selectNewPathForShard(NodeEnvironment env, ShardId shardId, IndexSettings indexSettings, long avgShardSizeInBytes, Map<Path, Integer> dataPathToShardCount) throws IOException {
    final Path dataPath;
    final Path statePath;
    if (indexSettings.hasCustomDataPath()) {
        dataPath = env.resolveCustomLocation(indexSettings, shardId);
        statePath = env.nodePaths()[0].resolve(shardId);
    } else {
        BigInteger totFreeSpace = BigInteger.ZERO;
        for (NodeEnvironment.NodePath nodePath : env.nodePaths()) {
            totFreeSpace = totFreeSpace.add(BigInteger.valueOf(nodePath.fileStore.getUsableSpace()));
        }
        // TODO: this is a hack!!  We should instead keep track of incoming (relocated) shards since we know
        // how large they will be once they're done copying, instead of a silly guess for such cases:
        // Very rough heuristic of how much disk space we expect the shard will use over its lifetime, the max of current average
        // shard size across the cluster and 5% of the total available free space on this node:
        BigInteger estShardSizeInBytes = BigInteger.valueOf(avgShardSizeInBytes).max(totFreeSpace.divide(BigInteger.valueOf(20)));
        // TODO - do we need something more extensible? Yet, this does the job for now...
        final NodeEnvironment.NodePath[] paths = env.nodePaths();
        NodeEnvironment.NodePath bestPath = null;
        BigInteger maxUsableBytes = BigInteger.valueOf(Long.MIN_VALUE);
        for (NodeEnvironment.NodePath nodePath : paths) {
            FileStore fileStore = nodePath.fileStore;
            BigInteger usableBytes = BigInteger.valueOf(fileStore.getUsableSpace());
            assert usableBytes.compareTo(BigInteger.ZERO) >= 0;
            // Deduct estimated reserved bytes from usable space:
            Integer count = dataPathToShardCount.get(nodePath.path);
            if (count != null) {
                usableBytes = usableBytes.subtract(estShardSizeInBytes.multiply(BigInteger.valueOf(count)));
            }
            if (bestPath == null || usableBytes.compareTo(maxUsableBytes) > 0) {
                maxUsableBytes = usableBytes;
                bestPath = nodePath;
            }
        }
        statePath = bestPath.resolve(shardId);
        dataPath = statePath;
    }
    return new ShardPath(indexSettings.hasCustomDataPath(), dataPath, statePath, shardId);
}
Also used : Path(java.nio.file.Path) BigInteger(java.math.BigInteger) FileStore(java.nio.file.FileStore) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) BigInteger(java.math.BigInteger)

Example 19 with NodeEnvironment

use of org.elasticsearch.env.NodeEnvironment in project elasticsearch by elastic.

the class MetaStateServiceTests method testLoadGlobal.

public void testLoadGlobal() throws Exception {
    try (NodeEnvironment env = newNodeEnvironment()) {
        MetaStateService metaStateService = new MetaStateService(Settings.EMPTY, env, xContentRegistry());
        IndexMetaData index = IndexMetaData.builder("test1").settings(indexSettings).build();
        MetaData metaData = MetaData.builder().persistentSettings(Settings.builder().put("test1", "value1").build()).put(index, true).build();
        metaStateService.writeGlobalState("test_write", metaData);
        metaStateService.writeIndex("test_write", index);
        MetaData loadedState = metaStateService.loadFullState();
        assertThat(loadedState.persistentSettings(), equalTo(metaData.persistentSettings()));
        assertThat(loadedState.hasIndex("test1"), equalTo(true));
        assertThat(loadedState.index("test1"), equalTo(index));
    }
}
Also used : NodeEnvironment(org.elasticsearch.env.NodeEnvironment) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 20 with NodeEnvironment

use of org.elasticsearch.env.NodeEnvironment in project elasticsearch by elastic.

the class MetaStateServiceTests method testWriteLoadGlobal.

public void testWriteLoadGlobal() throws Exception {
    try (NodeEnvironment env = newNodeEnvironment()) {
        MetaStateService metaStateService = new MetaStateService(Settings.EMPTY, env, xContentRegistry());
        MetaData metaData = MetaData.builder().persistentSettings(Settings.builder().put("test1", "value1").build()).build();
        metaStateService.writeGlobalState("test_write", metaData);
        assertThat(metaStateService.loadGlobalState().persistentSettings(), equalTo(metaData.persistentSettings()));
    }
}
Also used : NodeEnvironment(org.elasticsearch.env.NodeEnvironment) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Aggregations

NodeEnvironment (org.elasticsearch.env.NodeEnvironment)39 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)19 Path (java.nio.file.Path)17 Settings (org.elasticsearch.common.settings.Settings)15 Index (org.elasticsearch.index.Index)11 MetaData (org.elasticsearch.cluster.metadata.MetaData)10 IndexSettings (org.elasticsearch.index.IndexSettings)7 IOException (java.io.IOException)5 ClusterState (org.elasticsearch.cluster.ClusterState)5 ShardId (org.elasticsearch.index.shard.ShardId)4 ElasticsearchException (org.elasticsearch.ElasticsearchException)3 ClusterService (org.elasticsearch.cluster.service.ClusterService)3 Environment (org.elasticsearch.env.Environment)3 ShardPath (org.elasticsearch.index.shard.ShardPath)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 SigarModule (io.crate.module.SigarModule)1 DummyExtendedNodeInfo (io.crate.monitor.DummyExtendedNodeInfo)1 ExtendedNodeInfo (io.crate.monitor.ExtendedNodeInfo)1