Exemplo n.º 1
0
 private void readIndexInputFullyWithRandomSeeks(IndexInput indexInput) throws IOException {
   BytesRef ref = new BytesRef(scaledRandomIntBetween(1, 1024));
   long pos = 0;
   while (pos < indexInput.length()) {
     assertEquals(pos, indexInput.getFilePointer());
     int op = random().nextInt(5);
     if (op == 0) {
       int shift = 100 - randomIntBetween(0, 200);
       pos = Math.min(indexInput.length() - 1, Math.max(0, pos + shift));
       indexInput.seek(pos);
     } else if (op == 1) {
       indexInput.readByte();
       pos++;
     } else {
       int min = (int) Math.min(indexInput.length() - pos, ref.bytes.length);
       indexInput.readBytes(ref.bytes, ref.offset, min);
       pos += min;
     }
   }
 }
  @Test
  public void updateMappingConcurrently() throws Throwable {
    // Test that we can concurrently update different indexes and types.
    int shardNo = Math.max(5, cluster().size());

    prepareCreate("test1").setSettings("index.number_of_shards", shardNo).execute().actionGet();
    prepareCreate("test2").setSettings("index.number_of_shards", shardNo).execute().actionGet();

    // This is important. The test assumes all nodes are aware of all indices. Due to initializing
    // shard throttling
    // not all shards are allocated with the initial create index. Wait for it..
    ensureYellow();

    final Throwable[] threadException = new Throwable[1];
    final AtomicBoolean stop = new AtomicBoolean(false);
    Thread[] threads = new Thread[3];
    final CyclicBarrier barrier = new CyclicBarrier(threads.length);
    final ArrayList<Client> clientArray = new ArrayList<Client>();
    for (Client c : clients()) {
      clientArray.add(c);
    }

    for (int j = 0; j < threads.length; j++) {
      threads[j] =
          new Thread(
              new Runnable() {
                @SuppressWarnings("unchecked")
                @Override
                public void run() {
                  try {
                    barrier.await();

                    for (int i = 0; i < 100; i++) {
                      if (stop.get()) {
                        return;
                      }

                      Client client1 = clientArray.get(i % clientArray.size());
                      Client client2 = clientArray.get((i + 1) % clientArray.size());
                      String indexName = i % 2 == 0 ? "test2" : "test1";
                      String typeName = "type" + (i % 10);
                      String fieldName = Thread.currentThread().getName() + "_" + i;

                      PutMappingResponse response =
                          client1
                              .admin()
                              .indices()
                              .preparePutMapping(indexName)
                              .setType(typeName)
                              .setSource(
                                  JsonXContent.contentBuilder()
                                      .startObject()
                                      .startObject(typeName)
                                      .startObject("properties")
                                      .startObject(fieldName)
                                      .field("type", "string")
                                      .endObject()
                                      .endObject()
                                      .endObject()
                                      .endObject())
                              .get();

                      assertThat(response.isAcknowledged(), equalTo(true));
                      GetMappingsResponse getMappingResponse =
                          client2.admin().indices().prepareGetMappings(indexName).get();
                      ImmutableOpenMap<String, MappingMetaData> mappings =
                          getMappingResponse.getMappings().get(indexName);
                      assertThat(mappings.containsKey(typeName), equalTo(true));
                      assertThat(
                          ((Map<String, Object>)
                                  mappings.get(typeName).getSourceAsMap().get("properties"))
                              .keySet(),
                          Matchers.hasItem(fieldName));
                    }
                  } catch (Throwable t) {
                    threadException[0] = t;
                    stop.set(true);
                  }
                }
              });

      threads[j].setName("t_" + j);
      threads[j].start();
    }

    for (Thread t : threads) t.join();

    if (threadException[0] != null) {
      throw threadException[0];
    }
  }