Search in sources :

Example 61 with Supplier

use of java.util.function.Supplier in project opennms by OpenNMS.

the class WSManCollectorTest method canGenerateManyResources.

/**
 * NMS-8924: Verifies that the generated collection set includes a resource
 * for every node (XML) in the response.
 */
@Test
public void canGenerateManyResources() {
    // Define our resource type, and create a supplier that returns a new instance on every call
    NodeLevelResource node = mock(NodeLevelResource.class);
    ResourceType rt = new ResourceType();
    rt.setName("wsProcIndex");
    rt.setLabel("Processor (wsman)");
    rt.setResourceLabel("Processor (${wmiOSCpuName})");
    StorageStrategy strategy = new StorageStrategy();
    strategy.setClazz(SiblingColumnStorageStrategy.class.getCanonicalName());
    strategy.addParameter(new Parameter("sibling-column-name", "wmiOSCpuName"));
    rt.setStorageStrategy(strategy);
    PersistenceSelectorStrategy pstrategy = new PersistenceSelectorStrategy();
    pstrategy.setClazz(PersistAllSelectorStrategy.class.getCanonicalName());
    rt.setPersistenceSelectorStrategy(pstrategy);
    final AtomicInteger instanceId = new AtomicInteger();
    Supplier<Resource> resourceSupplier = () -> {
        return new GenericTypeResource(node, rt, Integer.toString(instanceId.getAndIncrement()));
    };
    // Define our group
    Group group = new Group();
    group.setName("windows-os-wmi-processor");
    addAttribute(group, "Name", "wmiOSCpuName", AttributeType.STRING);
    addAttribute(group, "InterruptsPersec", "wmiOSCpuIntsPerSec", AttributeType.GAUGE);
    addAttribute(group, "PercentProcessorTime", "wmiOSCpuPctProcTime", AttributeType.GAUGE);
    addAttribute(group, "PercentDPCTime", "wmiOSCpuPctDPCTime", AttributeType.GAUGE);
    addAttribute(group, "PercentInterruptTime", "wmiOSCpuPctIntrTime", AttributeType.GAUGE);
    addAttribute(group, "PercentUserTime", "wmiOSCpuPctUserTime", AttributeType.GAUGE);
    // Mock the agent
    CollectionAgent agent = mock(CollectionAgent.class);
    when(agent.getStorageResourcePath()).thenReturn(ResourcePath.get());
    CollectionSetBuilder builder = new CollectionSetBuilder(agent);
    // Sample data
    XMLTag xmlTag = XMLDoc.newDocument(true).addRoot("body").addTag("Win32_PerfFormattedData_PerfOS_Processor").addTag("Name").setText("c0").addTag("InterruptsPersec").setText("95").gotoRoot().addTag("Win32_PerfFormattedData_PerfOS_Processor").addTag("Name").setText("c1").addTag("InterruptsPersec").setText("100");
    List<Node> nodes = xmlTag.gotoRoot().getChildElement().stream().map(el -> (Node) el).collect(Collectors.toList());
    // Process the data and generate the collection set
    WsManCollector.processEnumerationResults(group, builder, resourceSupplier, nodes);
    // Verify the result
    CollectionSet collectionSet = builder.build();
    assertEquals(Arrays.asList("wsProcIndex/c0/windows-os-wmi-processor/wmiOSCpuName[c0,null]", "wsProcIndex/c0/windows-os-wmi-processor/wmiOSCpuIntsPerSec[null,95.0]", "wsProcIndex/c1/windows-os-wmi-processor/wmiOSCpuName[c1,null]", "wsProcIndex/c1/windows-os-wmi-processor/wmiOSCpuIntsPerSec[null,100.0]"), CollectionSetUtils.flatten(collectionSet));
    assertEquals(Sets.newHashSet("c0", "c1"), CollectionSetUtils.getResourcesByLabel(collectionSet).keySet());
}
Also used : StorageStrategy(org.opennms.netmgt.config.datacollection.StorageStrategy) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) WSManDataCollectionConfigDao(org.opennms.netmgt.dao.WSManDataCollectionConfigDao) Arrays(java.util.Arrays) CollectionStatus(org.opennms.netmgt.collection.api.CollectionStatus) ResourceType(org.opennms.netmgt.config.datacollection.ResourceType) WSManConfigDao(org.opennms.netmgt.dao.WSManConfigDao) Supplier(java.util.function.Supplier) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) XMLDoc(com.mycila.xmltool.XMLDoc) SiblingColumnStorageStrategy(org.opennms.netmgt.dao.support.SiblingColumnStorageStrategy) CollectionException(org.opennms.netmgt.collection.api.CollectionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Node(org.w3c.dom.Node) AttributeType(org.opennms.netmgt.collection.api.AttributeType) GenericTypeResource(org.opennms.netmgt.collection.support.builder.GenericTypeResource) Collection(org.opennms.netmgt.config.wsman.Collection) Attrib(org.opennms.netmgt.config.wsman.Attrib) OnmsNode(org.opennms.netmgt.model.OnmsNode) Resource(org.opennms.netmgt.collection.support.builder.Resource) NodeDao(org.opennms.netmgt.dao.api.NodeDao) InetAddressUtils(org.opennms.core.utils.InetAddressUtils) CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) WSManClientFactory(org.opennms.core.wsman.WSManClientFactory) XMLTag(com.mycila.xmltool.XMLTag) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Group(org.opennms.netmgt.config.wsman.Group) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) Sets(com.google.common.collect.Sets) Definition(org.opennms.netmgt.config.wsman.Definition) Matchers.any(org.mockito.Matchers.any) PersistenceSelectorStrategy(org.opennms.netmgt.config.datacollection.PersistenceSelectorStrategy) CollectionInitializationException(org.opennms.netmgt.collection.api.CollectionInitializationException) List(java.util.List) PersistAllSelectorStrategy(org.opennms.netmgt.collection.support.PersistAllSelectorStrategy) Assert.assertFalse(org.junit.Assert.assertFalse) CollectionSetUtils(org.opennms.core.collection.test.CollectionSetUtils) ResourcePath(org.opennms.netmgt.model.ResourcePath) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) Parameter(org.opennms.netmgt.config.datacollection.Parameter) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Group(org.opennms.netmgt.config.wsman.Group) StorageStrategy(org.opennms.netmgt.config.datacollection.StorageStrategy) SiblingColumnStorageStrategy(org.opennms.netmgt.dao.support.SiblingColumnStorageStrategy) PersistAllSelectorStrategy(org.opennms.netmgt.collection.support.PersistAllSelectorStrategy) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) GenericTypeResource(org.opennms.netmgt.collection.support.builder.GenericTypeResource) Node(org.w3c.dom.Node) OnmsNode(org.opennms.netmgt.model.OnmsNode) GenericTypeResource(org.opennms.netmgt.collection.support.builder.GenericTypeResource) Resource(org.opennms.netmgt.collection.support.builder.Resource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) XMLTag(com.mycila.xmltool.XMLTag) ResourceType(org.opennms.netmgt.config.datacollection.ResourceType) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Parameter(org.opennms.netmgt.config.datacollection.Parameter) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) SiblingColumnStorageStrategy(org.opennms.netmgt.dao.support.SiblingColumnStorageStrategy) PersistenceSelectorStrategy(org.opennms.netmgt.config.datacollection.PersistenceSelectorStrategy) Test(org.junit.Test)

Example 62 with Supplier

use of java.util.function.Supplier in project jersey by jersey.

the class DisposableSupplierTest method testDisposeComposedObjectWithPerLookupFields.

/**
     * PerLookup fields are not disposed therefore they should never be used as a DisposedSupplier because the field stay in
     * {@link SupplierFactoryBridge} forever.
     */
@Test
public void testDisposeComposedObjectWithPerLookupFields() {
    BindingTestHelper.bind(injectionManager, binder -> {
        binder.bindFactory(DisposableSupplierImpl.class, Singleton.class).to(String.class);
        binder.bindAsContract(ComposedObject.class).in(RequestScoped.class);
        binder.bind(new RequestScope()).to(RequestScope.class);
    });
    RequestScope request = injectionManager.getInstance(RequestScope.class);
    AtomicReference<Supplier<String>> atomicSupplier = new AtomicReference<>();
    request.runInScope(() -> {
        // Save Singleton Supplier for later check that the instance was disposed.
        Supplier<String> supplier = injectionManager.getInstance(DISPOSABLE_SUPPLIER_TYPE);
        atomicSupplier.set(supplier);
        // All instances should be the same because they are request scoped.
        ComposedObject instance = injectionManager.getInstance(ComposedObject.class);
        assertEquals("1", instance.first);
        assertEquals("2", instance.second);
        assertEquals("3", instance.third);
    });
    Supplier<String> cleanedSupplier = atomicSupplier.get();
    // Next should be 4
    assertEquals("4", cleanedSupplier.get());
}
Also used : Singleton(javax.inject.Singleton) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) DisposableSupplier(org.glassfish.jersey.internal.inject.DisposableSupplier) RequestScope(org.glassfish.jersey.process.internal.RequestScope) Test(org.junit.Test)

Example 63 with Supplier

use of java.util.function.Supplier in project elasticsearch by elastic.

the class IndicesService method removeIndex.

@Override
public void removeIndex(final Index index, final IndexRemovalReason reason, final String extraInfo) {
    final String indexName = index.getName();
    try {
        final IndexService indexService;
        final IndexEventListener listener;
        synchronized (this) {
            if (hasIndex(index) == false) {
                return;
            }
            logger.debug("[{}] closing ... (reason [{}])", indexName, reason);
            Map<String, IndexService> newIndices = new HashMap<>(indices);
            indexService = newIndices.remove(index.getUUID());
            assert indexService != null : "IndexService is null for index: " + index;
            indices = unmodifiableMap(newIndices);
            listener = indexService.getIndexEventListener();
        }
        listener.beforeIndexRemoved(indexService, reason);
        logger.debug("{} closing index service (reason [{}][{}])", index, reason, extraInfo);
        indexService.close(extraInfo, reason == IndexRemovalReason.DELETED);
        logger.debug("{} closed... (reason [{}][{}])", index, reason, extraInfo);
        final IndexSettings indexSettings = indexService.getIndexSettings();
        listener.afterIndexRemoved(indexService.index(), indexSettings, reason);
        if (reason == IndexRemovalReason.DELETED) {
            // now we are done - try to wipe data on disk if possible
            deleteIndexStore(extraInfo, indexService.index(), indexSettings);
        }
    } catch (Exception e) {
        logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to remove index {} ([{}][{}])", index, reason, extraInfo), e);
    }
}
Also used : IndexEventListener(org.elasticsearch.index.shard.IndexEventListener) IndexService(org.elasticsearch.index.IndexService) HashMap(java.util.HashMap) IndexSettings(org.elasticsearch.index.IndexSettings) Supplier(java.util.function.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ShardLockObtainFailedException(org.elasticsearch.env.ShardLockObtainFailedException) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) ResourceAlreadyExistsException(org.elasticsearch.ResourceAlreadyExistsException)

Example 64 with Supplier

use of java.util.function.Supplier in project elasticsearch by elastic.

the class IndicesService method stats.

public NodeIndicesStats stats(boolean includePrevious, CommonStatsFlags flags) {
    CommonStats oldStats = new CommonStats(flags);
    if (includePrevious) {
        Flag[] setFlags = flags.getFlags();
        for (Flag flag : setFlags) {
            switch(flag) {
                case Get:
                    oldStats.get.add(oldShardsStats.getStats);
                    break;
                case Indexing:
                    oldStats.indexing.add(oldShardsStats.indexingStats);
                    break;
                case Search:
                    oldStats.search.add(oldShardsStats.searchStats);
                    break;
                case Merge:
                    oldStats.merge.add(oldShardsStats.mergeStats);
                    break;
                case Refresh:
                    oldStats.refresh.add(oldShardsStats.refreshStats);
                    break;
                case Recovery:
                    oldStats.recoveryStats.add(oldShardsStats.recoveryStats);
                    break;
                case Flush:
                    oldStats.flush.add(oldShardsStats.flushStats);
                    break;
            }
        }
    }
    Map<Index, List<IndexShardStats>> statsByShard = new HashMap<>();
    for (IndexService indexService : this) {
        for (IndexShard indexShard : indexService) {
            try {
                if (indexShard.routingEntry() == null) {
                    continue;
                }
                IndexShardStats indexShardStats = new IndexShardStats(indexShard.shardId(), new ShardStats[] { new ShardStats(indexShard.routingEntry(), indexShard.shardPath(), new CommonStats(indicesQueryCache, indexShard, flags), indexShard.commitStats(), indexShard.seqNoStats()) });
                if (!statsByShard.containsKey(indexService.index())) {
                    statsByShard.put(indexService.index(), arrayAsArrayList(indexShardStats));
                } else {
                    statsByShard.get(indexService.index()).add(indexShardStats);
                }
            } catch (IllegalIndexShardStateException e) {
                // we can safely ignore illegal state on ones that are closing for example
                logger.trace((Supplier<?>) () -> new ParameterizedMessage("{} ignoring shard stats", indexShard.shardId()), e);
            }
        }
    }
    return new NodeIndicesStats(oldStats, statsByShard);
}
Also used : IndexShardStats(org.elasticsearch.action.admin.indices.stats.IndexShardStats) ShardStats(org.elasticsearch.action.admin.indices.stats.ShardStats) HashMap(java.util.HashMap) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) Index(org.elasticsearch.index.Index) IndexShardStats(org.elasticsearch.action.admin.indices.stats.IndexShardStats) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) Flag(org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag) CommonStats(org.elasticsearch.action.admin.indices.stats.CommonStats) CollectionUtils.arrayAsArrayList(org.elasticsearch.common.util.CollectionUtils.arrayAsArrayList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Supplier(java.util.function.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 65 with Supplier

use of java.util.function.Supplier in project elasticsearch by elastic.

the class IndicesService method verifyIndexIsDeleted.

/**
     * Verify that the contents on disk for the given index is deleted; if not, delete the contents.
     * This method assumes that an index is already deleted in the cluster state and/or explicitly
     * through index tombstones.
     * @param index {@code Index} to make sure its deleted from disk
     * @param clusterState {@code ClusterState} to ensure the index is not part of it
     * @return IndexMetaData for the index loaded from disk
     */
@Override
@Nullable
public IndexMetaData verifyIndexIsDeleted(final Index index, final ClusterState clusterState) {
    // this method should only be called when we know the index (name + uuid) is not part of the cluster state
    if (clusterState.metaData().index(index) != null) {
        throw new IllegalStateException("Cannot delete index [" + index + "], it is still part of the cluster state.");
    }
    if (nodeEnv.hasNodeFile() && FileSystemUtils.exists(nodeEnv.indexPaths(index))) {
        final IndexMetaData metaData;
        try {
            metaData = metaStateService.loadIndexState(index);
        } catch (Exception e) {
            logger.warn((Supplier<?>) () -> new ParameterizedMessage("[{}] failed to load state file from a stale deleted index, folders will be left on disk", index), e);
            return null;
        }
        final IndexSettings indexSettings = buildIndexSettings(metaData);
        try {
            deleteIndexStoreIfDeletionAllowed("stale deleted index", index, indexSettings, ALWAYS_TRUE);
        } catch (Exception e) {
            // we just warn about the exception here because if deleteIndexStoreIfDeletionAllowed
            // throws an exception, it gets added to the list of pending deletes to be tried again
            logger.warn((Supplier<?>) () -> new ParameterizedMessage("[{}] failed to delete index on disk", metaData.getIndex()), e);
        }
        return metaData;
    }
    return null;
}
Also used : IndexSettings(org.elasticsearch.index.IndexSettings) Supplier(java.util.function.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ShardLockObtainFailedException(org.elasticsearch.env.ShardLockObtainFailedException) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) ResourceAlreadyExistsException(org.elasticsearch.ResourceAlreadyExistsException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Nullable(org.elasticsearch.common.Nullable)

Aggregations

Supplier (java.util.function.Supplier)104 Test (org.junit.Test)43 List (java.util.List)41 ArrayList (java.util.ArrayList)28 Map (java.util.Map)24 Collectors (java.util.stream.Collectors)23 HashMap (java.util.HashMap)21 Function (java.util.function.Function)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 IOException (java.io.IOException)17 Arrays (java.util.Arrays)16 TimeUnit (java.util.concurrent.TimeUnit)14 Consumer (java.util.function.Consumer)14 Assert (org.junit.Assert)14 Collections (java.util.Collections)13 CompletableFuture (java.util.concurrent.CompletableFuture)13 Rule (org.junit.Rule)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 Cleanup (lombok.Cleanup)12 Timeout (org.junit.rules.Timeout)11