コード例 #1
0
 @Test
 public void testSimpleApis() throws Exception {
   RestRequest request = new RestRequest(Method.POST, "/test/type1");
   request.setBody(
       ByteBuffer.wrap(
           XContentFactory.jsonBuilder()
               .startObject()
               .field("field", "value")
               .endObject()
               .copiedBytes()));
   RestResponse response = client.execute(request);
   Map<String, Object> map = parseBody(response);
   assertThat(response.getStatus(), equalTo(Status.OK));
   assertThat(map.get("ok").toString(), equalTo("true"));
   assertThat(map.get("_index").toString(), equalTo("test"));
   assertThat(map.get("_type").toString(), equalTo("type1"));
 }
コード例 #2
0
  @Test
  public void testRebalanceOnlyAfterAllShardsAreActive() {
    ShardsAllocation strategy =
        new ShardsAllocation(
            settingsBuilder().put("cluster.routing.allocation.concurrent_recoveries", 10).build());

    logger.info("Building initial routing table");

    MetaData metaData =
        newMetaDataBuilder()
            .put(newIndexMetaDataBuilder("test").numberOfShards(5).numberOfReplicas(1))
            .build();

    RoutingTable routingTable =
        routingTable()
            .add(indexRoutingTable("test").initializeEmpty(metaData.index("test")))
            .build();

    ClusterState clusterState =
        newClusterStateBuilder().metaData(metaData).routingTable(routingTable).build();

    assertThat(routingTable.index("test").shards().size(), equalTo(5));
    for (int i = 0; i < routingTable.index("test").shards().size(); i++) {
      assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2));
      assertThat(routingTable.index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED));
      assertThat(routingTable.index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED));
      assertThat(routingTable.index("test").shard(i).shards().get(0).currentNodeId(), nullValue());
      assertThat(routingTable.index("test").shard(i).shards().get(1).currentNodeId(), nullValue());
    }

    logger.info("start two nodes and fully start the shards");
    clusterState =
        newClusterStateBuilder()
            .state(clusterState)
            .nodes(newNodesBuilder().put(newNode("node1")).put(newNode("node2")))
            .build();
    RoutingTable prevRoutingTable = routingTable;
    routingTable = strategy.reroute(clusterState).routingTable();
    clusterState = newClusterStateBuilder().state(clusterState).routingTable(routingTable).build();

    for (int i = 0; i < routingTable.index("test").shards().size(); i++) {
      assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2));
      assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING));
      assertThat(
          routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED));
    }

    logger.info("start all the primary shards, replicas will start initializing");
    RoutingNodes routingNodes = clusterState.routingNodes();
    prevRoutingTable = routingTable;
    routingTable =
        strategy
            .applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING))
            .routingTable();
    clusterState = newClusterStateBuilder().state(clusterState).routingTable(routingTable).build();
    routingNodes = clusterState.routingNodes();

    for (int i = 0; i < routingTable.index("test").shards().size(); i++) {
      assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2));
      assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED));
      assertThat(
          routingTable.index("test").shard(i).replicaShards().get(0).state(),
          equalTo(INITIALIZING));
    }

    logger.info("now, start 8 more nodes, and check that no rebalancing/relocation have happened");
    clusterState =
        newClusterStateBuilder()
            .state(clusterState)
            .nodes(
                newNodesBuilder()
                    .putAll(clusterState.nodes())
                    .put(newNode("node3"))
                    .put(newNode("node4"))
                    .put(newNode("node5"))
                    .put(newNode("node6"))
                    .put(newNode("node7"))
                    .put(newNode("node8"))
                    .put(newNode("node9"))
                    .put(newNode("node10")))
            .build();
    prevRoutingTable = routingTable;
    routingTable = strategy.reroute(clusterState).routingTable();
    clusterState = newClusterStateBuilder().state(clusterState).routingTable(routingTable).build();
    routingNodes = clusterState.routingNodes();

    for (int i = 0; i < routingTable.index("test").shards().size(); i++) {
      assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2));
      assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED));
      assertThat(
          routingTable.index("test").shard(i).replicaShards().get(0).state(),
          equalTo(INITIALIZING));
    }

    logger.info("start the replica shards, rebalancing should start");
    routingNodes = clusterState.routingNodes();
    prevRoutingTable = routingTable;
    routingTable =
        strategy
            .applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING))
            .routingTable();
    clusterState = newClusterStateBuilder().state(clusterState).routingTable(routingTable).build();
    routingNodes = clusterState.routingNodes();

    // we only allow one relocation at a time
    assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(5));
    assertThat(routingTable.shardsWithState(RELOCATING).size(), equalTo(5));

    logger.info("complete relocation, other half of relocation should happen");
    routingNodes = clusterState.routingNodes();
    prevRoutingTable = routingTable;
    routingTable =
        strategy
            .applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING))
            .routingTable();
    clusterState = newClusterStateBuilder().state(clusterState).routingTable(routingTable).build();
    routingNodes = clusterState.routingNodes();

    // we now only relocate 3, since 2 remain where they are!
    assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(7));
    assertThat(routingTable.shardsWithState(RELOCATING).size(), equalTo(3));

    logger.info("complete relocation, thats it!");
    routingNodes = clusterState.routingNodes();
    prevRoutingTable = routingTable;
    routingTable =
        strategy
            .applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING))
            .routingTable();
    clusterState = newClusterStateBuilder().state(clusterState).routingTable(routingTable).build();
    routingNodes = clusterState.routingNodes();

    assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(10));
    // make sure we have an even relocation
    for (RoutingNode routingNode : routingNodes) {
      assertThat(routingNode.shards().size(), equalTo(1));
    }
  }
コード例 #3
0
  private void testSimpleConfiguration(Settings settings) {
    Index index = new Index("test");
    Injector parentInjector =
        new ModulesBuilder()
            .add(
                new SettingsModule(settings),
                new EnvironmentModule(new Environment(settings)),
                new IndicesAnalysisModule())
            .createInjector();
    Injector injector =
        new ModulesBuilder()
            .add(
                new IndexSettingsModule(index, settings),
                new IndexNameModule(index),
                new AnalysisModule(
                    settings, parentInjector.getInstance(IndicesAnalysisService.class)))
            .createChildInjector(parentInjector);

    AnalysisService analysisService = injector.getInstance(AnalysisService.class);

    Analyzer analyzer = analysisService.analyzer("custom1").analyzer();

    assertThat(analyzer, instanceOf(CustomAnalyzer.class));
    CustomAnalyzer custom1 = (CustomAnalyzer) analyzer;
    assertThat(custom1.tokenizerFactory(), instanceOf(StandardTokenizerFactory.class));
    assertThat(custom1.tokenFilters().length, equalTo(2));

    StopTokenFilterFactory stop1 = (StopTokenFilterFactory) custom1.tokenFilters()[0];
    assertThat(stop1.stopWords().size(), equalTo(1));
    assertThat((Iterable<String>) stop1.stopWords(), hasItem("test-stop"));

    analyzer = analysisService.analyzer("custom2").analyzer();
    assertThat(analyzer, instanceOf(CustomAnalyzer.class));
    CustomAnalyzer custom2 = (CustomAnalyzer) analyzer;

    //        HtmlStripCharFilterFactory html = (HtmlStripCharFilterFactory)
    // custom2.charFilters()[0];
    //        assertThat(html.readAheadLimit(), equalTo(HTMLStripCharFilter.DEFAULT_READ_AHEAD));
    //
    //        html = (HtmlStripCharFilterFactory) custom2.charFilters()[1];
    //        assertThat(html.readAheadLimit(), equalTo(1024));

    // verify characters  mapping
    analyzer = analysisService.analyzer("custom5").analyzer();
    assertThat(analyzer, instanceOf(CustomAnalyzer.class));
    CustomAnalyzer custom5 = (CustomAnalyzer) analyzer;
    assertThat(custom5.tokenFilters()[0], instanceOf(MappingCharFilterFactory.class));

    // verify aliases
    analyzer = analysisService.analyzer("alias1").analyzer();
    assertThat(analyzer, instanceOf(StandardAnalyzer.class));

    // check phonetic
    analyzer = analysisService.analyzer("custom3").analyzer();
    assertThat(analyzer, instanceOf(CustomAnalyzer.class));
    CustomAnalyzer custom3 = (CustomAnalyzer) analyzer;
    assertThat(custom3.tokenFilters()[0], instanceOf(PhoneticTokenFilterFactory.class));

    // check custom class name (my)
    analyzer = analysisService.analyzer("custom4").analyzer();
    assertThat(analyzer, instanceOf(CustomAnalyzer.class));
    CustomAnalyzer custom4 = (CustomAnalyzer) analyzer;
    assertThat(custom4.tokenFilters()[0], instanceOf(MyFilterTokenFilterFactory.class));

    //        // verify Czech stemmer
    //        analyzer = analysisService.analyzer("czechAnalyzerWithStemmer").analyzer();
    //        assertThat(analyzer, instanceOf(CustomAnalyzer.class));
    //        CustomAnalyzer czechstemmeranalyzer = (CustomAnalyzer) analyzer;
    //        assertThat(czechstemmeranalyzer.tokenizerFactory(),
    // instanceOf(StandardTokenizerFactory.class));
    //        assertThat(czechstemmeranalyzer.tokenFilters().length, equalTo(4));
    //        assertThat(czechstemmeranalyzer.tokenFilters()[3],
    // instanceOf(CzechStemTokenFilterFactory.class));
    //
    //        // check dictionary decompounder
    //        analyzer = analysisService.analyzer("decompoundingAnalyzer").analyzer();
    //        assertThat(analyzer, instanceOf(CustomAnalyzer.class));
    //        CustomAnalyzer dictionaryDecompounderAnalyze = (CustomAnalyzer) analyzer;
    //        assertThat(dictionaryDecompounderAnalyze.tokenizerFactory(),
    // instanceOf(StandardTokenizerFactory.class));
    //        assertThat(dictionaryDecompounderAnalyze.tokenFilters().length, equalTo(1));
    //        assertThat(dictionaryDecompounderAnalyze.tokenFilters()[0],
    // instanceOf(DictionaryCompoundWordTokenFilterFactory.class));

    Set<String> wordList =
        Analysis.getWordSet(null, settings, "index.analysis.filter.dict_dec.word_list");
    MatcherAssert.assertThat(wordList.size(), equalTo(6));
    MatcherAssert.assertThat(
        wordList, hasItems("donau", "dampf", "schiff", "spargel", "creme", "suppe"));
  }