Search in sources :

Example 1 with OpenIndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.open.OpenIndexRequest in project elasticsearch by elastic.

the class IndicesClusterStateServiceRandomUpdatesTests method randomlyUpdateClusterState.

public ClusterState randomlyUpdateClusterState(ClusterState state, Map<DiscoveryNode, IndicesClusterStateService> clusterStateServiceMap, Supplier<MockIndicesService> indicesServiceSupplier) {
    // randomly create new indices (until we have 200 max)
    for (int i = 0; i < randomInt(5); i++) {
        if (state.metaData().indices().size() > 200) {
            break;
        }
        String name = "index_" + randomAsciiOfLength(15).toLowerCase(Locale.ROOT);
        Settings.Builder settingsBuilder = Settings.builder().put(SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 3)).put(SETTING_NUMBER_OF_REPLICAS, randomInt(2));
        if (randomBoolean()) {
            settingsBuilder.put(IndexMetaData.SETTING_SHADOW_REPLICAS, true).put(IndexMetaData.SETTING_SHARED_FILESYSTEM, true);
        }
        CreateIndexRequest request = new CreateIndexRequest(name, settingsBuilder.build()).waitForActiveShards(ActiveShardCount.NONE);
        state = cluster.createIndex(state, request);
        assertTrue(state.metaData().hasIndex(name));
    }
    // randomly delete indices
    Set<String> indicesToDelete = new HashSet<>();
    int numberOfIndicesToDelete = randomInt(Math.min(2, state.metaData().indices().size()));
    for (String index : randomSubsetOf(numberOfIndicesToDelete, state.metaData().indices().keys().toArray(String.class))) {
        indicesToDelete.add(state.metaData().index(index).getIndex().getName());
    }
    if (indicesToDelete.isEmpty() == false) {
        DeleteIndexRequest deleteRequest = new DeleteIndexRequest(indicesToDelete.toArray(new String[indicesToDelete.size()]));
        state = cluster.deleteIndices(state, deleteRequest);
        for (String index : indicesToDelete) {
            assertFalse(state.metaData().hasIndex(index));
        }
    }
    // randomly close indices
    int numberOfIndicesToClose = randomInt(Math.min(1, state.metaData().indices().size()));
    for (String index : randomSubsetOf(numberOfIndicesToClose, state.metaData().indices().keys().toArray(String.class))) {
        CloseIndexRequest closeIndexRequest = new CloseIndexRequest(state.metaData().index(index).getIndex().getName());
        state = cluster.closeIndices(state, closeIndexRequest);
    }
    // randomly open indices
    int numberOfIndicesToOpen = randomInt(Math.min(1, state.metaData().indices().size()));
    for (String index : randomSubsetOf(numberOfIndicesToOpen, state.metaData().indices().keys().toArray(String.class))) {
        OpenIndexRequest openIndexRequest = new OpenIndexRequest(state.metaData().index(index).getIndex().getName());
        state = cluster.openIndices(state, openIndexRequest);
    }
    // randomly update settings
    Set<String> indicesToUpdate = new HashSet<>();
    boolean containsClosedIndex = false;
    int numberOfIndicesToUpdate = randomInt(Math.min(2, state.metaData().indices().size()));
    for (String index : randomSubsetOf(numberOfIndicesToUpdate, state.metaData().indices().keys().toArray(String.class))) {
        indicesToUpdate.add(state.metaData().index(index).getIndex().getName());
        if (state.metaData().index(index).getState() == IndexMetaData.State.CLOSE) {
            containsClosedIndex = true;
        }
    }
    if (indicesToUpdate.isEmpty() == false) {
        UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indicesToUpdate.toArray(new String[indicesToUpdate.size()]));
        Settings.Builder settings = Settings.builder();
        if (containsClosedIndex == false) {
            settings.put(SETTING_NUMBER_OF_REPLICAS, randomInt(2));
        }
        settings.put("index.refresh_interval", randomIntBetween(1, 5) + "s");
        updateSettingsRequest.settings(settings.build());
        state = cluster.updateSettings(state, updateSettingsRequest);
    }
    // randomly reroute
    if (rarely()) {
        state = cluster.reroute(state, new ClusterRerouteRequest());
    }
    // randomly start and fail allocated shards
    List<ShardRouting> startedShards = new ArrayList<>();
    List<FailedShard> failedShards = new ArrayList<>();
    for (DiscoveryNode node : state.nodes()) {
        IndicesClusterStateService indicesClusterStateService = clusterStateServiceMap.get(node);
        MockIndicesService indicesService = (MockIndicesService) indicesClusterStateService.indicesService;
        for (MockIndexService indexService : indicesService) {
            for (MockIndexShard indexShard : indexService) {
                ShardRouting persistedShardRouting = indexShard.routingEntry();
                if (persistedShardRouting.initializing() && randomBoolean()) {
                    startedShards.add(persistedShardRouting);
                } else if (rarely()) {
                    failedShards.add(new FailedShard(persistedShardRouting, "fake shard failure", new Exception()));
                }
            }
        }
    }
    state = cluster.applyFailedShards(state, failedShards);
    state = cluster.applyStartedShards(state, startedShards);
    // randomly add and remove nodes (except current master)
    if (rarely()) {
        if (randomBoolean()) {
            // add node
            if (state.nodes().getSize() < 10) {
                DiscoveryNodes newNodes = DiscoveryNodes.builder(state.nodes()).add(createNode()).build();
                state = ClusterState.builder(state).nodes(newNodes).build();
                // always reroute after node leave
                state = cluster.reroute(state, new ClusterRerouteRequest());
                updateNodes(state, clusterStateServiceMap, indicesServiceSupplier);
            }
        } else {
            // remove node
            if (state.nodes().getDataNodes().size() > 3) {
                DiscoveryNode discoveryNode = randomFrom(state.nodes().getNodes().values().toArray(DiscoveryNode.class));
                if (discoveryNode.equals(state.nodes().getMasterNode()) == false) {
                    DiscoveryNodes newNodes = DiscoveryNodes.builder(state.nodes()).remove(discoveryNode.getId()).build();
                    state = ClusterState.builder(state).nodes(newNodes).build();
                    state = cluster.deassociateDeadNodes(state, true, "removed and added a node");
                    updateNodes(state, clusterStateServiceMap, indicesServiceSupplier);
                }
                if (randomBoolean()) {
                    // and add it back
                    DiscoveryNodes newNodes = DiscoveryNodes.builder(state.nodes()).add(discoveryNode).build();
                    state = ClusterState.builder(state).nodes(newNodes).build();
                    state = cluster.reroute(state, new ClusterRerouteRequest());
                    updateNodes(state, clusterStateServiceMap, indicesServiceSupplier);
                }
            }
        }
    }
    return state;
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) UpdateSettingsRequest(org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest) ArrayList(java.util.ArrayList) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) ClusterRerouteRequest(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequest) Settings(org.elasticsearch.common.settings.Settings) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) HashSet(java.util.HashSet) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) FailedShard(org.elasticsearch.cluster.routing.allocation.FailedShard) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 2 with OpenIndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.open.OpenIndexRequest in project elasticsearch by elastic.

the class RestOpenIndexAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    OpenIndexRequest openIndexRequest = new OpenIndexRequest(Strings.splitStringByCommaToArray(request.param("index")));
    openIndexRequest.timeout(request.paramAsTime("timeout", openIndexRequest.timeout()));
    openIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", openIndexRequest.masterNodeTimeout()));
    openIndexRequest.indicesOptions(IndicesOptions.fromRequest(request, openIndexRequest.indicesOptions()));
    return channel -> client.admin().indices().open(openIndexRequest, new AcknowledgedRestListener<OpenIndexResponse>(channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Settings(org.elasticsearch.common.settings.Settings) OpenIndexResponse(org.elasticsearch.action.admin.indices.open.OpenIndexResponse) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) Strings(org.elasticsearch.common.Strings) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) OpenIndexResponse(org.elasticsearch.action.admin.indices.open.OpenIndexResponse)

Example 3 with OpenIndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.open.OpenIndexRequest in project main by JohnPeng739.

the class ElasticAccessorRest method afterPropertiesSet.

/**
 * {@inheritDoc}
 *
 * @see InitializingBean#afterPropertiesSet()
 */
@Override
public void afterPropertiesSet() throws Exception {
    int servers = env.getProperty("elastic.servers", Integer.class, 0);
    if (servers <= 0) {
        if (logger.isErrorEnabled()) {
            logger.error("There are not define the elastic api server.");
        }
        return;
    }
    this.index = env.getProperty("elastic.index", "default");
    HttpHost[] elasticServers = new HttpHost[servers];
    for (int index = 1; index <= servers; index++) {
        String protocol = env.getProperty("elastic.api.protocol", "http");
        String server = env.getProperty("elastic.api.server", "localhost");
        int port = env.getProperty("elastic.api.port", Integer.class, 9200);
        elasticServers[index - 1] = new HttpHost(server, port, protocol);
    }
    client = new RestHighLevelClient(RestClient.builder(elasticServers));
    // 初始化Index
    try {
        OpenIndexRequest request = new OpenIndexRequest(this.index);
        client.indices().open(request);
    } catch (ElasticsearchStatusException ex) {
        if (ex.status() == RestStatus.NOT_FOUND) {
            CreateIndexRequest request = new CreateIndexRequest(this.index);
            client.indices().create(request);
        }
    } catch (IOException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Open index fail.", ex);
        }
    }
    if (logger.isInfoEnabled()) {
        logger.info("Create the elastic search api connection successfully.");
    }
}
Also used : HttpHost(org.apache.http.HttpHost) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) IOException(java.io.IOException) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException)

Example 4 with OpenIndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.open.OpenIndexRequest in project ranger by apache.

the class ElasticSearchAuditDestination method newClient.

private RestHighLevelClient newClient() {
    try {
        if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password) && password.contains("keytab") && new File(password).exists()) {
            subject = CredentialsProviderUtil.login(user, password);
        }
        RestClientBuilder restClientBuilder = getRestClientBuilder(hosts, protocol, user, password, port);
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Initialized client");
        }
        boolean exits = false;
        try {
            exits = restHighLevelClient.indices().open(new OpenIndexRequest(this.index), RequestOptions.DEFAULT).isShardsAcknowledged();
        } catch (Exception e) {
            LOG.warn("Error validating index " + this.index);
        }
        if (exits) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Index exists");
            }
        } else {
            LOG.info("Index does not exist");
        }
        return restHighLevelClient;
    } catch (Throwable t) {
        lastLoggedAt.updateAndGet(lastLoggedAt -> {
            long now = System.currentTimeMillis();
            long elapsed = now - lastLoggedAt;
            if (elapsed > TimeUnit.MINUTES.toMillis(1)) {
                LOG.error("Can't connect to ElasticSearch server: " + connectionString(), t);
                return now;
            } else {
                return lastLoggedAt;
            }
        });
        return null;
    }
}
Also used : RestClient(org.elasticsearch.client.RestClient) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) RegistryBuilder(org.apache.http.config.RegistryBuilder) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) AuthSchemes(org.apache.http.client.config.AuthSchemes) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) ArrayList(java.util.ArrayList) IndexRequest(org.elasticsearch.action.index.IndexRequest) KerberosCredentialsProvider(org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider) Locale(java.util.Locale) Map(java.util.Map) Lookup(org.apache.http.config.Lookup) RequestOptions(org.elasticsearch.client.RequestOptions) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) AuthzAuditEvent(org.apache.ranger.audit.model.AuthzAuditEvent) PrivilegedActionException(java.security.PrivilegedActionException) Properties(java.util.Properties) Logger(org.slf4j.Logger) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) Collection(java.util.Collection) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) KerberosTicket(javax.security.auth.kerberos.KerberosTicket) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) File(java.io.File) Subject(javax.security.auth.Subject) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) MiscUtil(org.apache.ranger.audit.provider.MiscUtil) CredentialsProviderUtil(org.apache.ranger.authorization.credutils.CredentialsProviderUtil) AuditEventBase(org.apache.ranger.audit.model.AuditEventBase) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpHost(org.apache.http.HttpHost) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) File(java.io.File) PrivilegedActionException(java.security.PrivilegedActionException)

Example 5 with OpenIndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.open.OpenIndexRequest in project ranger by apache.

the class ElasticSearchIndexBootStrapper method createIndex.

private boolean createIndex() {
    boolean exits = false;
    if (client == null) {
        connect();
    }
    if (client != null) {
        try {
            exits = client.indices().open(new OpenIndexRequest(this.index), RequestOptions.DEFAULT).isShardsAcknowledged();
        } catch (Exception e) {
            LOG.info("Index " + this.index + " not available.");
        }
        if (!exits) {
            LOG.info("Index does not exist. Attempting to create index:" + this.index);
            CreateIndexRequest request = new CreateIndexRequest(this.index);
            if (this.no_of_shards >= 0 && this.no_of_replicas >= 0) {
                request.settings(Settings.builder().put("index.number_of_shards", this.no_of_shards).put("index.number_of_replicas", this.no_of_replicas));
            }
            request.mapping(es_ranger_audit_schema_json, XContentType.JSON);
            request.setMasterTimeout(TimeValue.timeValueMinutes(1));
            request.setTimeout(TimeValue.timeValueMinutes(2));
            try {
                CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
                if (createIndexResponse != null) {
                    exits = client.indices().open(new OpenIndexRequest(this.index), RequestOptions.DEFAULT).isShardsAcknowledged();
                    if (exits) {
                        LOG.info("Index " + this.index + " created successfully.");
                    }
                }
            } catch (Exception e) {
                LOG.severe("Unable to create Index. Reason:" + e.toString());
                e.printStackTrace();
            }
        } else {
            LOG.info("Index " + this.index + " is already created.");
        }
    }
    return exits;
}
Also used : OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.client.indices.CreateIndexResponse) IOException(java.io.IOException)

Aggregations

OpenIndexRequest (org.elasticsearch.action.admin.indices.open.OpenIndexRequest)8 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)3 UpdateSettingsRequest (org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest)3 HttpHost (org.apache.http.HttpHost)2 CloseIndexRequest (org.elasticsearch.action.admin.indices.close.CloseIndexRequest)2 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)2 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)2 Settings (org.elasticsearch.common.settings.Settings)2 File (java.io.File)1 PrivilegedActionException (java.security.PrivilegedActionException)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Properties (java.util.Properties)1