Search in sources :

Example 31 with NamedWriteableRegistry

use of org.elasticsearch.common.io.stream.NamedWriteableRegistry in project elasticsearch by elastic.

the class AbstractSortTestCase method init.

@BeforeClass
public static void init() throws IOException {
    Path genericConfigFolder = createTempDir();
    Settings baseSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).put(Environment.PATH_CONF_SETTING.getKey(), genericConfigFolder).build();
    Environment environment = new Environment(baseSettings);
    ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList());
    ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(new TestEngineService()));
    ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    scriptService = new ScriptService(baseSettings, environment, new ResourceWatcherService(baseSettings, null), scriptEngineRegistry, scriptContextRegistry, scriptSettings) {

        @Override
        public CompiledScript compile(Script script, ScriptContext scriptContext) {
            return new CompiledScript(ScriptType.INLINE, "mockName", "test", script);
        }
    };
    SearchModule searchModule = new SearchModule(Settings.EMPTY, false, emptyList());
    namedWriteableRegistry = new NamedWriteableRegistry(searchModule.getNamedWriteables());
    xContentRegistry = new NamedXContentRegistry(searchModule.getNamedXContents());
}
Also used : Path(java.nio.file.Path) ContentPath(org.elasticsearch.index.mapper.ContentPath) CompiledScript(org.elasticsearch.script.CompiledScript) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ScriptContext(org.elasticsearch.script.ScriptContext) ScriptContextRegistry(org.elasticsearch.script.ScriptContextRegistry) ScriptService(org.elasticsearch.script.ScriptService) ScriptSettings(org.elasticsearch.script.ScriptSettings) ScriptEngineRegistry(org.elasticsearch.script.ScriptEngineRegistry) Environment(org.elasticsearch.env.Environment) SearchModule(org.elasticsearch.search.SearchModule) TestEngineService(org.elasticsearch.script.ScriptServiceTests.TestEngineService) ResourceWatcherService(org.elasticsearch.watcher.ResourceWatcherService) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) Settings(org.elasticsearch.common.settings.Settings) ScriptSettings(org.elasticsearch.script.ScriptSettings) IndexSettings(org.elasticsearch.index.IndexSettings) BeforeClass(org.junit.BeforeClass)

Example 32 with NamedWriteableRegistry

use of org.elasticsearch.common.io.stream.NamedWriteableRegistry in project crate by crate.

the class InboundHandlerTests method setUp.

@Before
public void setUp() throws Exception {
    super.setUp();
    channel = new FakeTcpChannel(randomBoolean(), buildNewFakeTransportAddress().address(), buildNewFakeTransportAddress().address());
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList());
    TransportHandshaker handshaker = new TransportHandshaker(version, threadPool, (n, c, r, v) -> {
    });
    TransportKeepAlive keepAlive = new TransportKeepAlive(threadPool, TcpChannel::sendMessage);
    OutboundHandler outboundHandler = new OutboundHandler("node", version, new StatsTracker(), threadPool, BigArrays.NON_RECYCLING_INSTANCE);
    requestHandlers = new Transport.RequestHandlers();
    responseHandlers = new Transport.ResponseHandlers();
    handler = new InboundHandler(threadPool, outboundHandler, namedWriteableRegistry, handshaker, keepAlive, requestHandlers, responseHandlers);
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) Before(org.junit.Before)

Example 33 with NamedWriteableRegistry

use of org.elasticsearch.common.io.stream.NamedWriteableRegistry in project crate by crate.

the class ESIntegTestCase method ensureClusterStateConsistency.

/**
 * Verifies that all nodes that have the same version of the cluster state as master have same cluster state
 */
protected void ensureClusterStateConsistency() throws IOException {
    if (cluster() != null && cluster().size() > 0) {
        final NamedWriteableRegistry namedWriteableRegistry = cluster().getNamedWriteableRegistry();
        final Client masterClient = client();
        ClusterState masterClusterState = masterClient.admin().cluster().prepareState().all().get().getState();
        byte[] masterClusterStateBytes = ClusterState.Builder.toBytes(masterClusterState);
        // remove local node reference
        masterClusterState = ClusterState.Builder.fromBytes(masterClusterStateBytes, null, namedWriteableRegistry);
        Map<String, Object> masterStateMap = convertToMap(masterClusterState);
        int masterClusterStateSize = ClusterState.Builder.toBytes(masterClusterState).length;
        String masterId = masterClusterState.nodes().getMasterNodeId();
        for (Client client : cluster().getClients()) {
            ClusterState localClusterState = client.admin().cluster().prepareState().all().setLocal(true).get().getState();
            byte[] localClusterStateBytes = ClusterState.Builder.toBytes(localClusterState);
            // remove local node reference
            localClusterState = ClusterState.Builder.fromBytes(localClusterStateBytes, null, namedWriteableRegistry);
            final Map<String, Object> localStateMap = convertToMap(localClusterState);
            final int localClusterStateSize = ClusterState.Builder.toBytes(localClusterState).length;
            // that the master node matches the master (otherwise there is no requirement for the cluster state to match)
            if (masterClusterState.version() == localClusterState.version() && masterId.equals(localClusterState.nodes().getMasterNodeId())) {
                try {
                    assertEquals("cluster state UUID does not match", masterClusterState.stateUUID(), localClusterState.stateUUID());
                    // We cannot compare serialization bytes since serialization order of maps is not guaranteed
                    // but we can compare serialization sizes - they should be the same
                    assertEquals("cluster state size does not match", masterClusterStateSize, localClusterStateSize);
                    // Compare JSON serialization
                    assertNull("cluster state JSON serialization does not match", differenceBetweenMapsIgnoringArrayOrder(masterStateMap, localStateMap));
                } catch (final AssertionError error) {
                    logger.error("Cluster state from master:\n{}\nLocal cluster state:\n{}", masterClusterState.toString(), localClusterState.toString());
                    throw error;
                }
            }
        }
    }
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) ClusterState(org.elasticsearch.cluster.ClusterState) AdminClient(org.elasticsearch.client.AdminClient) Client(org.elasticsearch.client.Client)

Example 34 with NamedWriteableRegistry

use of org.elasticsearch.common.io.stream.NamedWriteableRegistry in project crate by crate.

the class CoordinationMetadataTests method testCoordinationMetadataSerializationEqualsHashCode.

public void testCoordinationMetadataSerializationEqualsHashCode() {
    CoordinationMetadata initialMetadata = new CoordinationMetadata(randomNonNegativeLong(), randomVotingConfig(), randomVotingConfig(), randomVotingTombstones());
    // Note: the explicit cast of the CopyFunction is needed for some IDE (specifically Eclipse 4.8.0) to infer the right type
    EqualsHashCodeTestUtils.checkEqualsAndHashCode(initialMetadata, (CopyFunction<CoordinationMetadata>) orig -> ESTestCase.copyWriteable(orig, new NamedWriteableRegistry(Collections.emptyList()), CoordinationMetadata::new), meta -> {
        CoordinationMetadata.Builder builder = CoordinationMetadata.builder(meta);
        switch(randomInt(3)) {
            case 0:
                builder.term(randomValueOtherThan(meta.term(), ESTestCase::randomNonNegativeLong));
                break;
            case 1:
                builder.lastCommittedConfiguration(randomlyChangeVotingConfiguration(meta.getLastCommittedConfiguration()));
                break;
            case 2:
                builder.lastAcceptedConfiguration(randomlyChangeVotingConfiguration(meta.getLastAcceptedConfiguration()));
                break;
            case 3:
                if (meta.getVotingConfigExclusions().isEmpty() == false && randomBoolean()) {
                    builder.clearVotingConfigExclusions();
                } else {
                    randomVotingTombstones().forEach(dn -> builder.addVotingConfigExclusion(dn));
                }
                break;
        }
        return builder.build();
    });
}
Also used : EqualsHashCodeTestUtils(org.elasticsearch.test.EqualsHashCodeTestUtils) ToXContent(org.elasticsearch.common.xcontent.ToXContent) Set(java.util.Set) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) BytesReference(org.elasticsearch.common.bytes.BytesReference) JsonXContent(org.elasticsearch.common.xcontent.json.JsonXContent) XContentParser(org.elasticsearch.common.xcontent.XContentParser) CopyFunction(org.elasticsearch.test.EqualsHashCodeTestUtils.CopyFunction) HashSet(java.util.HashSet) VotingConfigExclusion(org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) Matchers.equalTo(org.hamcrest.Matchers.equalTo) VotingConfiguration(org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfiguration) ESTestCase(org.elasticsearch.test.ESTestCase) Collections(java.util.Collections) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry)

Example 35 with NamedWriteableRegistry

use of org.elasticsearch.common.io.stream.NamedWriteableRegistry in project elasticsearch by elastic.

the class TransportClient method buildTemplate.

private static ClientTemplate buildTemplate(Settings providedSettings, Settings defaultSettings, Collection<Class<? extends Plugin>> plugins, HostFailureListener failureListner) {
    if (Node.NODE_NAME_SETTING.exists(providedSettings) == false) {
        providedSettings = Settings.builder().put(providedSettings).put(Node.NODE_NAME_SETTING.getKey(), "_client_").build();
    }
    final PluginsService pluginsService = newPluginService(providedSettings, plugins);
    final Settings settings = Settings.builder().put(defaultSettings).put(pluginsService.updatedSettings()).build();
    final List<Closeable> resourcesToClose = new ArrayList<>();
    final ThreadPool threadPool = new ThreadPool(settings);
    resourcesToClose.add(() -> ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS));
    final NetworkService networkService = new NetworkService(settings, Collections.emptyList());
    try {
        final List<Setting<?>> additionalSettings = new ArrayList<>();
        final List<String> additionalSettingsFilter = new ArrayList<>();
        additionalSettings.addAll(pluginsService.getPluginSettings());
        additionalSettingsFilter.addAll(pluginsService.getPluginSettingsFilter());
        for (final ExecutorBuilder<?> builder : threadPool.builders()) {
            additionalSettings.addAll(builder.getRegisteredSettings());
        }
        SettingsModule settingsModule = new SettingsModule(settings, additionalSettings, additionalSettingsFilter);
        SearchModule searchModule = new SearchModule(settings, true, pluginsService.filterPlugins(SearchPlugin.class));
        List<NamedWriteableRegistry.Entry> entries = new ArrayList<>();
        entries.addAll(NetworkModule.getNamedWriteables());
        entries.addAll(searchModule.getNamedWriteables());
        entries.addAll(ClusterModule.getNamedWriteables());
        entries.addAll(pluginsService.filterPlugins(Plugin.class).stream().flatMap(p -> p.getNamedWriteables().stream()).collect(Collectors.toList()));
        NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(entries);
        NamedXContentRegistry xContentRegistry = new NamedXContentRegistry(Stream.of(searchModule.getNamedXContents().stream(), pluginsService.filterPlugins(Plugin.class).stream().flatMap(p -> p.getNamedXContent().stream())).flatMap(Function.identity()).collect(toList()));
        ModulesBuilder modules = new ModulesBuilder();
        // plugin modules must be added here, before others or we can get crazy injection errors...
        for (Module pluginModule : pluginsService.createGuiceModules()) {
            modules.add(pluginModule);
        }
        modules.add(b -> b.bind(ThreadPool.class).toInstance(threadPool));
        ActionModule actionModule = new ActionModule(true, settings, null, settingsModule.getIndexScopedSettings(), settingsModule.getClusterSettings(), settingsModule.getSettingsFilter(), threadPool, pluginsService.filterPlugins(ActionPlugin.class), null, null);
        modules.add(actionModule);
        CircuitBreakerService circuitBreakerService = Node.createCircuitBreakerService(settingsModule.getSettings(), settingsModule.getClusterSettings());
        resourcesToClose.add(circuitBreakerService);
        BigArrays bigArrays = new BigArrays(settings, circuitBreakerService);
        resourcesToClose.add(bigArrays);
        modules.add(settingsModule);
        NetworkModule networkModule = new NetworkModule(settings, true, pluginsService.filterPlugins(NetworkPlugin.class), threadPool, bigArrays, circuitBreakerService, namedWriteableRegistry, xContentRegistry, networkService, null);
        final Transport transport = networkModule.getTransportSupplier().get();
        final TransportService transportService = new TransportService(settings, transport, threadPool, networkModule.getTransportInterceptor(), boundTransportAddress -> DiscoveryNode.createLocal(settings, new TransportAddress(TransportAddress.META_ADDRESS, 0), UUIDs.randomBase64UUID()), null);
        modules.add((b -> {
            b.bind(BigArrays.class).toInstance(bigArrays);
            b.bind(PluginsService.class).toInstance(pluginsService);
            b.bind(CircuitBreakerService.class).toInstance(circuitBreakerService);
            b.bind(NamedWriteableRegistry.class).toInstance(namedWriteableRegistry);
            b.bind(Transport.class).toInstance(transport);
            b.bind(TransportService.class).toInstance(transportService);
            b.bind(NetworkService.class).toInstance(networkService);
        }));
        Injector injector = modules.createInjector();
        final TransportClientNodesService nodesService = new TransportClientNodesService(settings, transportService, threadPool, failureListner == null ? (t, e) -> {
        } : failureListner);
        final TransportProxyClient proxy = new TransportProxyClient(settings, transportService, nodesService, actionModule.getActions().values().stream().map(x -> x.getAction()).collect(Collectors.toList()));
        List<LifecycleComponent> pluginLifecycleComponents = new ArrayList<>();
        pluginLifecycleComponents.addAll(pluginsService.getGuiceServiceClasses().stream().map(injector::getInstance).collect(Collectors.toList()));
        resourcesToClose.addAll(pluginLifecycleComponents);
        transportService.start();
        transportService.acceptIncomingRequests();
        ClientTemplate transportClient = new ClientTemplate(injector, pluginLifecycleComponents, nodesService, proxy, namedWriteableRegistry);
        resourcesToClose.clear();
        return transportClient;
    } finally {
        IOUtils.closeWhileHandlingException(resourcesToClose);
    }
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) Arrays(java.util.Arrays) Injector(org.elasticsearch.common.inject.Injector) NetworkModule(org.elasticsearch.common.network.NetworkModule) BigArrays(org.elasticsearch.common.util.BigArrays) Settings(org.elasticsearch.common.settings.Settings) TimeValue.timeValueSeconds(org.elasticsearch.common.unit.TimeValue.timeValueSeconds) ThreadPool(org.elasticsearch.threadpool.ThreadPool) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) ActionRequest(org.elasticsearch.action.ActionRequest) PluginsService(org.elasticsearch.plugins.PluginsService) Transport(org.elasticsearch.transport.Transport) Setting(org.elasticsearch.common.settings.Setting) Collection(java.util.Collection) UUIDs(org.elasticsearch.common.UUIDs) ClusterModule(org.elasticsearch.cluster.ClusterModule) Collectors(java.util.stream.Collectors) AbstractClient(org.elasticsearch.client.support.AbstractClient) List(java.util.List) Stream(java.util.stream.Stream) TransportAddress(org.elasticsearch.common.transport.TransportAddress) SearchPlugin(org.elasticsearch.plugins.SearchPlugin) Module(org.elasticsearch.common.inject.Module) InternalSettingsPreparer(org.elasticsearch.node.InternalSettingsPreparer) Function(java.util.function.Function) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) ArrayList(java.util.ArrayList) ActionModule(org.elasticsearch.action.ActionModule) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) NetworkService(org.elasticsearch.common.network.NetworkService) SettingsModule(org.elasticsearch.common.settings.SettingsModule) NetworkPlugin(org.elasticsearch.plugins.NetworkPlugin) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) TcpTransport(org.elasticsearch.transport.TcpTransport) TimeValue(org.elasticsearch.common.unit.TimeValue) Node(org.elasticsearch.node.Node) ExecutorBuilder(org.elasticsearch.threadpool.ExecutorBuilder) TransportService(org.elasticsearch.transport.TransportService) ActionRequestBuilder(org.elasticsearch.action.ActionRequestBuilder) SearchModule(org.elasticsearch.search.SearchModule) ActionPlugin(org.elasticsearch.plugins.ActionPlugin) ActionResponse(org.elasticsearch.action.ActionResponse) IOUtils(org.apache.lucene.util.IOUtils) Plugin(org.elasticsearch.plugins.Plugin) Action(org.elasticsearch.action.Action) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) LifecycleComponent(org.elasticsearch.common.component.LifecycleComponent) Closeable(java.io.Closeable) ModulesBuilder(org.elasticsearch.common.inject.ModulesBuilder) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) NetworkPlugin(org.elasticsearch.plugins.NetworkPlugin) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Closeable(java.io.Closeable) ActionModule(org.elasticsearch.action.ActionModule) ArrayList(java.util.ArrayList) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ActionPlugin(org.elasticsearch.plugins.ActionPlugin) LifecycleComponent(org.elasticsearch.common.component.LifecycleComponent) Injector(org.elasticsearch.common.inject.Injector) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) Settings(org.elasticsearch.common.settings.Settings) PluginsService(org.elasticsearch.plugins.PluginsService) Setting(org.elasticsearch.common.settings.Setting) SearchPlugin(org.elasticsearch.plugins.SearchPlugin) BigArrays(org.elasticsearch.common.util.BigArrays) TransportService(org.elasticsearch.transport.TransportService) SettingsModule(org.elasticsearch.common.settings.SettingsModule) NetworkService(org.elasticsearch.common.network.NetworkService) SearchModule(org.elasticsearch.search.SearchModule) ModulesBuilder(org.elasticsearch.common.inject.ModulesBuilder) NetworkModule(org.elasticsearch.common.network.NetworkModule) ClusterModule(org.elasticsearch.cluster.ClusterModule) Module(org.elasticsearch.common.inject.Module) ActionModule(org.elasticsearch.action.ActionModule) SettingsModule(org.elasticsearch.common.settings.SettingsModule) SearchModule(org.elasticsearch.search.SearchModule) Transport(org.elasticsearch.transport.Transport) TcpTransport(org.elasticsearch.transport.TcpTransport) NetworkModule(org.elasticsearch.common.network.NetworkModule)

Aggregations

NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)71 NetworkService (org.elasticsearch.common.network.NetworkService)27 NoneCircuitBreakerService (org.elasticsearch.indices.breaker.NoneCircuitBreakerService)25 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)23 Settings (org.elasticsearch.common.settings.Settings)21 ThreadPool (org.elasticsearch.threadpool.ThreadPool)19 ArrayList (java.util.ArrayList)18 MockTransportService (org.elasticsearch.test.transport.MockTransportService)18 Transport (org.elasticsearch.transport.Transport)18 IOException (java.io.IOException)15 Collections (java.util.Collections)15 TransportAddress (org.elasticsearch.common.transport.TransportAddress)15 BigArrays (org.elasticsearch.common.util.BigArrays)15 SearchModule (org.elasticsearch.search.SearchModule)15 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)15 MockTcpTransport (org.elasticsearch.transport.MockTcpTransport)15 NamedXContentRegistry (org.elasticsearch.common.xcontent.NamedXContentRegistry)14 TransportService (org.elasticsearch.transport.TransportService)14 List (java.util.List)13 ESTestCase (org.elasticsearch.test.ESTestCase)13